* src/SDCCmain.c: (setParseWithComma) substituted brain damaged strtok
[fw/sdcc] / src / SDCCutil.c
index 381d53d04380cc3d679027aa9bfa1e9206037951..d4898f530e8025868af07e5cdb579b9f91412b76 100644 (file)
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
-#ifdef _WIN32
+#include <math.h>
 #include <ctype.h>
+
+#ifdef _WIN32
 #include <windows.h>
 #endif
 #include <sys/stat.h>
 #include "SDCCmacro.h"
 #include "SDCCutil.h"
 #include "newalloc.h"
+#ifndef _WIN32
+#include "findme.h"
+#endif
+
+#include "version.h"
 
 /** Given an array of name, value string pairs creates a new hash
     containing all of the pairs.
@@ -131,7 +138,7 @@ getBinPath(const char *prel)
     return path;
   }
   /* not enough info in prel; do it with module name */
-  else if (0 != GetModuleFileName(NULL, path, sizeof path) != 0 &&
+  else if (0 != GetModuleFileName(NULL, path, sizeof path) &&
     NULL != (p = strrchr(path, DIR_SEPARATOR_CHAR))) {
     *p = '\0';
     return path;
@@ -143,18 +150,29 @@ getBinPath(const char *prel)
 char *
 getBinPath(const char *prel)
 {
-  char *p;
-  size_t len;
   static char path[PATH_MAX];
-    
-  if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL)
-    return NULL;
+  const char *ret_path;
+
+  if (NULL != (ret_path = findProgramPath(prel))) {
+    char *p;
+    size_t len;
 
-  len = min((sizeof path) - 1, p - prel);
-  strncpy(path, prel, len);
-  path[len] = '\0';
+    if (NULL != (p = strrchr(ret_path, DIR_SEPARATOR_CHAR)) &&
+      PATH_MAX > (len = p - ret_path)) {
+      memcpy(path, ret_path, len);
+      path[len] = '\0';
+      free((void *)ret_path);
 
-  return path;
+      return path;
+    }
+    else {
+      free((void *)ret_path);
+
+      return NULL;
+    }
+  }
+  else
+    return NULL;
 }
 #endif
 
@@ -179,14 +197,22 @@ setMainValue (const char *pname, const char *pvalue)
 }
 
 void
-buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len)
+buildCmdLine2 (char *pbuffer, size_t len, const char *pcmd, ...)
 {
+  va_list ap;
   char *poutcmd;
+
   assert(pbuffer && pcmd);
   assert(_mainValues);
 
-  poutcmd = msprintf(_mainValues, pcmd);
+  va_start(ap, pcmd);
+
+  poutcmd = mvsprintf(_mainValues, pcmd, ap);
+
+  va_end(ap);
+
   strncpyz(pbuffer, poutcmd, len);
+  Safe_free(poutcmd);
 }
 
 void
@@ -264,6 +290,14 @@ char *strncatz(char *dest, const char *src, size_t n)
 }
 
 
+/*-----------------------------------------------------------------*/
+/* getBuildNumber - return build number                            */
+/*-----------------------------------------------------------------*/
+const char *getBuildNumber(void)
+{
+  return (SDCC_BUILD_NUMBER);
+}
+
 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
 {
@@ -291,5 +325,66 @@ size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
 
   return len;
 }
-
 #endif
+
+/** Pragma tokenizer
+ */
+void
+init_pragma_token(struct pragma_token_s *token)
+{
+  dbuf_init(&token->dbuf, 16);
+  token->type = TOKEN_UNKNOWN;
+}
+
+char *
+get_pragma_token(const char *s, struct pragma_token_s *token)
+{
+  dbuf_set_size(&token->dbuf, 0);
+
+  /* skip leading spaces */
+  while ('\n' != *s && isspace(*s))
+    ++s;
+
+  if ('\0' == *s || '\n' == *s)
+    {
+      token->type = TOKEN_EOL;
+    }
+  else
+    {
+      char *end;
+
+      long val = strtol(s, &end, 0);
+
+      if (end != s && ('\0' == *end || isspace(*s)))
+        {
+          token->val.int_val = val;
+          token->type = TOKEN_INT;
+          dbuf_append(&token->dbuf, s, end - s);
+          s = end;
+        }
+      else
+        {
+          while ('\0' != *s && !isspace(*s))
+            {
+              dbuf_append(&token->dbuf, s, 1);
+              ++s;
+            }
+
+          token->type = TOKEN_STR;
+        }
+    }
+
+  return (char *)s;
+}
+
+const char *
+get_pragma_string(struct pragma_token_s *token)
+{
+  return dbuf_c_str(&token->dbuf);
+}
+
+void
+free_pragma_token(struct pragma_token_s *token)
+{
+  dbuf_destroy(&token->dbuf);
+}