]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - opts.c
Fix for loops
[xonotic/gmqcc.git] / opts.c
diff --git a/opts.c b/opts.c
index c0be2dd113215b3f7f5060a1d5c287885bad6a1f..7c597130ef3c813d2b6e4af626bacf876bb74ffe 100644 (file)
--- a/opts.c
+++ b/opts.c
@@ -1,7 +1,7 @@
 /*
- * Copyright (C) 2012, 2013
+ * Copyright (C) 2012, 2013, 2014, 2015
  *     Wolfgang Bumiller
- *     Dale Weiler 
+ *     Dale Weiler
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  * this software and associated documentation files (the "Software"), to deal in
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
+#include <string.h>
+#include <stdlib.h>
+
 #include "gmqcc.h"
+
+const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1] = {
+# define GMQCC_TYPE_OPTIMIZATIONS
+# define GMQCC_DEFINE_FLAG(NAME, MIN_O) MIN_O,
+#  include "opts.def"
+    0
+};
+
+const opts_flag_def_t opts_opt_list[COUNT_OPTIMIZATIONS+1] = {
+# define GMQCC_TYPE_OPTIMIZATIONS
+# define GMQCC_DEFINE_FLAG(NAME, MIN_O) { #NAME, LONGBIT(OPTIM_##NAME) },
+#  include "opts.def"
+    { NULL, LONGBIT(0) }
+};
+
+const opts_flag_def_t opts_warn_list[COUNT_WARNINGS+1] = {
+# define GMQCC_TYPE_WARNS
+# define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(WARN_##X) },
+#  include "opts.def"
+    { NULL, LONGBIT(0) }
+};
+
+const opts_flag_def_t opts_flag_list[COUNT_FLAGS+1] = {
+# define GMQCC_TYPE_FLAGS
+# define GMQCC_DEFINE_FLAG(X) { #X, LONGBIT(X) },
+#  include "opts.def"
+    { NULL, LONGBIT(0) }
+};
+
 unsigned int opts_optimizationcount[COUNT_OPTIMIZATIONS];
-opts_cmd_t   opts; /* command lien options */
+opts_cmd_t   opts; /* command line options */
 
-static void opts_setdefault() {
+static void opts_setdefault(void) {
     memset(&opts, 0, sizeof(opts_cmd_t));
     OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
+    OPTS_OPTION_STR(OPTION_PROGSRC)     = "progs.src";
 
     /* warnings */
     opts_set(opts.warn,  WARN_UNUSED_VARIABLE,           true);
@@ -57,12 +90,20 @@ static void opts_setdefault() {
     opts_set(opts.warn,  WARN_UNINITIALIZED_CONSTANT,    true);
     opts_set(opts.warn,  WARN_DEPRECATED,                true);
     opts_set(opts.warn,  WARN_PARENTHESIS,               true);
+    opts_set(opts.warn,  WARN_CONST_OVERWRITE,           true);
+    opts_set(opts.warn,  WARN_DIRECTIVE_INMACRO,         true);
+    opts_set(opts.warn,  WARN_BUILTINS,                  true);
+    opts_set(opts.warn,  WARN_INEXACT_COMPARES,          true);
 
     /* flags */
     opts_set(opts.flags, ADJUST_VECTOR_FIELDS,           true);
     opts_set(opts.flags, CORRECT_TERNARY,                true);
     opts_set(opts.flags, BAIL_ON_WERROR,                 true);
     opts_set(opts.flags, LEGACY_VECTOR_MATHS,            true);
+    opts_set(opts.flags, DARKPLACES_STRING_TABLE_BUG,    true);
+
+    /* options */
+    OPTS_OPTION_U32(OPTION_STATE_FPS) = 10;
 }
 
 void opts_backup_non_Wall() {
@@ -92,12 +133,13 @@ void opts_restore_non_Werror_all() {
 void opts_init(const char *output, int standard, size_t arraysize) {
     opts_setdefault();
 
-    OPTS_OPTION_STR(OPTION_OUTPUT)         = (char*)output;
+    OPTS_OPTION_STR(OPTION_OUTPUT)         = output;
     OPTS_OPTION_U32(OPTION_STANDARD)       = standard;
     OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize;
+    OPTS_OPTION_U16(OPTION_MEMDUMPCOLS)    = 16;
 }
 
-static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
+static bool opts_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def_t *list, size_t listsize) {
     size_t i;
 
     for (i = 0; i < listsize; ++i) {
@@ -141,6 +183,9 @@ void opts_setoptimlevel(unsigned int level) {
     size_t i;
     for (i = 0; i < COUNT_OPTIMIZATIONS; ++i)
         opts_set(opts.optimization, i, level >= opts_opt_oflag[i]);
+
+    if (!level)
+        opts.optimizeoff = true;
 }
 
 /*
@@ -151,14 +196,14 @@ void opts_setoptimlevel(unsigned int level) {
  * from a progs.src.
  */
 static char *opts_ini_rstrip(char *s) {
-    char *p = s + strlen(s);
-    while(p > s && isspace(*--p))
-        *p = '\0';
+    char *p = s + strlen(s) - 1;
+    while (p > s && util_isspace(*p))
+        *p = '\0', p--;
     return s;
 }
 
 static char *opts_ini_lskip(const char *s) {
-    while (*s && isspace(*s))
+    while (*s && util_isspace(*s))
         s++;
     return (char*)s;
 }
@@ -166,15 +211,16 @@ static char *opts_ini_lskip(const char *s) {
 static char *opts_ini_next(const char *s, char c) {
     bool last = false;
     while (*s && *s != c && !(last && *s == ';'))
-        last = !!isspace(*s), s++;
+        last = !!util_isspace(*s), s++;
 
     return (char*)s;
 }
 
 static size_t opts_ini_parse (
-    FILE   *filehandle,
-    char *(*loadhandle)(const char *, const char *, const char *),
-    char **errorhandle
+    fs_file_t *filehandle,
+    char *(*loadhandle)(const char *, const char *, const char *, char **),
+    char **errorhandle,
+    char **parse_file
 ) {
     size_t linesize;
     size_t lineno             = 1;
@@ -189,7 +235,7 @@ static size_t opts_ini_parse (
     char *read_name;
     char *read_value;
 
-    while (file_getline(&line, &linesize, filehandle) != EOF) {
+    while (fs_file_getline(&line, &linesize, filehandle) != FS_FILE_EOF) {
         parse_beg = line;
 
         /* handle BOM */
@@ -210,7 +256,7 @@ static size_t opts_ini_parse (
             /* section found */
             if (*(parse_end = opts_ini_next(parse_beg + 1, ']')) == ']') {
                 * parse_end = '\0'; /* terminate bro */
-                strncpy(section_data, parse_beg + 1, sizeof(section_data));
+                util_strncpy(section_data, parse_beg + 1, sizeof(section_data));
                 section_data[sizeof(section_data) - 1] = '\0';
                 *oldname_data                          = '\0';
             } else if (!error) {
@@ -220,7 +266,7 @@ static size_t opts_ini_parse (
         } else if (*parse_beg && *parse_beg != ';') {
             /* not a comment, must be a name value pair :) */
             if (*(parse_end = opts_ini_next(parse_beg, '=')) != '=')
-                  parse_end = opts_ini_next(parse_beg, ':');
+                parse_end = opts_ini_next(parse_beg, ':');
 
             if (*parse_end == '=' || *parse_end == ':') {
                 *parse_end = '\0'; /* terminate bro */
@@ -231,11 +277,23 @@ static size_t opts_ini_parse (
                 opts_ini_rstrip(read_value);
 
                 /* valid name value pair, lets call down to handler */
-                strncpy(oldname_data, read_name, sizeof(oldname_data));
+                util_strncpy(oldname_data, read_name, sizeof(oldname_data));
                 oldname_data[sizeof(oldname_data) - 1] ='\0';
 
-                if ((*errorhandle = loadhandle(section_data, read_name, read_value)) && !error)
+                if ((*errorhandle = loadhandle(section_data, read_name, read_value, parse_file)) && !error)
+                    error = lineno;
+            } else if (!strcmp(section_data, "includes")) {
+                /* Includes are special */
+                if (*(parse_end = opts_ini_next(parse_beg, '=')) == '='
+                ||  *(parse_end = opts_ini_next(parse_beg, ':')) == ':') {
+                    static const char *invalid_include = "invalid use of include";
+                    vec_append(*errorhandle, strlen(invalid_include), invalid_include);
                     error = lineno;
+                } else {
+                    read_name = opts_ini_rstrip(parse_beg);
+                    if ((*errorhandle = loadhandle(section_data, read_name, read_name, parse_file)) && !error)
+                        error = lineno;
+                }
             } else if (!error) {
                 /* otherwise set error to the current line number */
                 error = lineno;
@@ -245,6 +303,7 @@ static size_t opts_ini_parse (
     }
     mem_d(line);
     return error;
+
 }
 
 /*
@@ -253,24 +312,44 @@ static size_t opts_ini_parse (
 static bool opts_ini_bool(const char *value) {
     if (!strcmp(value, "true"))  return true;
     if (!strcmp(value, "false")) return false;
-    return !!atoi(value);
+    return !!strtol(value, NULL, 10);
 }
 
-static char *opts_ini_load(const char *section, const char *name, const char *value) {
+static char *opts_ini_load(const char *section, const char *name, const char *value, char **parse_file) {
     char *error = NULL;
     bool  found = false;
 
     /*
      * undef all of these because they may still be defined like in my
      * case they where.
-     */  
+     */
     #undef GMQCC_TYPE_FLAGS
     #undef GMQCC_TYPE_OPTIMIZATIONS
     #undef GMQCC_TYPE_WARNS
 
+    /* deal with includes */
+    if (!strcmp(section, "includes")) {
+        static const char *include_error_beg = "failed to open file `";
+        static const char *include_error_end = "' for inclusion";
+        fs_file_t *file = fs_file_open(value, "r");
+        found = true;
+        if (!file) {
+            vec_append(error, strlen(include_error_beg), include_error_beg);
+            vec_append(error, strlen(value), value);
+            vec_append(error, strlen(include_error_end), include_error_end);
+        } else {
+            if (opts_ini_parse(file, &opts_ini_load, &error, parse_file) != 0)
+                found = false;
+            /* Change the file name */
+            mem_d(*parse_file);
+            *parse_file = util_strdup(value);
+            fs_file_close(file);
+        }
+    }
+
     /* flags */
     #define GMQCC_TYPE_FLAGS
-    #define GMQCC_DEFINE_FLAG(X, Y)                                    \
+    #define GMQCC_DEFINE_FLAG(X)                                       \
     if (!strcmp(section, "flags") && !strcmp(name, #X)) {              \
         opts_set(opts.flags, X, opts_ini_bool(value));                 \
         found = true;                                                  \
@@ -279,7 +358,7 @@ static char *opts_ini_load(const char *section, const char *name, const char *va
 
     /* warnings */
     #define GMQCC_TYPE_WARNS
-    #define GMQCC_DEFINE_FLAG(X, Y)                                    \
+    #define GMQCC_DEFINE_FLAG(X)                                       \
     if (!strcmp(section, "warnings") && !strcmp(name, #X)) {           \
         opts_set(opts.warn, WARN_##X, opts_ini_bool(value));           \
         found = true;                                                  \
@@ -288,7 +367,7 @@ static char *opts_ini_load(const char *section, const char *name, const char *va
 
     /* Werror-individuals */
     #define GMQCC_TYPE_WARNS
-    #define GMQCC_DEFINE_FLAG(X, Y)                                    \
+    #define GMQCC_DEFINE_FLAG(X)                                       \
     if (!strcmp(section, "errors") && !strcmp(name, #X)) {             \
         opts_set(opts.werror, WARN_##X, opts_ini_bool(value));         \
         found = true;                                                  \
@@ -297,7 +376,7 @@ static char *opts_ini_load(const char *section, const char *name, const char *va
 
     /* optimizations */
     #define GMQCC_TYPE_OPTIMIZATIONS
-    #define GMQCC_DEFINE_FLAG(X,Y,Z)                                   \
+    #define GMQCC_DEFINE_FLAG(X,Y)                                     \
     if (!strcmp(section, "optimizations") && !strcmp(name, #X)) {      \
         opts_set(opts.optimization, OPTIM_##X, opts_ini_bool(value));  \
         found = true;                                                  \
@@ -306,24 +385,29 @@ static char *opts_ini_load(const char *section, const char *name, const char *va
 
     /* nothing was found ever! */
     if (!found) {
-        if (strcmp(section, "flags")         &&
-            strcmp(section, "warnings")      &&
+        if (strcmp(section, "includes") &&
+            strcmp(section, "flags")    &&
+            strcmp(section, "warnings") &&
             strcmp(section, "optimizations"))
         {
-            vec_upload(error, "invalid section `", 17);
-            vec_upload(error, section, strlen(section));
-            vec_push  (error, '`');
-            vec_push  (error, '\0');
+            static const char *invalid_section = "invalid_section `";
+            vec_append(error, strlen(invalid_section), invalid_section);
+            vec_append(error, strlen(section), section);
+            vec_push(error, '`');
+        } else if (strcmp(section, "includes")) {
+            static const char *invalid_variable = "invalid_variable `";
+            static const char *in_section = "` in section: `";
+            vec_append(error, strlen(invalid_variable), invalid_variable);
+            vec_append(error, strlen(name), name);
+            vec_append(error, strlen(in_section), in_section);
+            vec_append(error, strlen(section), section);
+            vec_push(error, '`');
         } else {
-            vec_upload(error, "invalid variable `", 18);
-            vec_upload(error, name, strlen(name));
-            vec_push  (error, '`');
-            vec_upload(error, " in section: `", 14);
-            vec_upload(error, section, strlen(section));
-            vec_push  (error, '`');
-            vec_push  (error, '\0');
+            static const char *expected_something = "expected something";
+            vec_append(error, strlen(expected_something), expected_something);
         }
     }
+    vec_push(error, '\0');
     return error;
 }
 
@@ -337,27 +421,29 @@ void opts_ini_init(const char *file) {
      *  gmqcc.ini
      *  gmqcc.cfg
      */
-    char       *error;
+    char       *error = NULL;
+    char       *parse_file = NULL;
     size_t     line;
-    FILE       *ini;
+    fs_file_t  *ini;
 
-    
     if (!file) {
         /* try ini */
-        if (!(ini = file_open((file = "gmqcc.ini"), "r")))
+        if (!(ini = fs_file_open((file = "gmqcc.ini"), "r")))
             /* try cfg */
-            if (!(ini = file_open((file = "gmqcc.cfg"), "r")))
+            if (!(ini = fs_file_open((file = "gmqcc.cfg"), "r")))
                 return;
-    } else if (!(ini = file_open(file, "r")))
+    } else if (!(ini = fs_file_open(file, "r")))
         return;
 
     con_out("found ini file `%s`\n", file);
 
-    if ((line = opts_ini_parse(ini, &opts_ini_load, &error)) != 0) {
+    parse_file = util_strdup(file);
+    if ((line = opts_ini_parse(ini, &opts_ini_load, &error, &parse_file)) != 0) {
         /* there was a parse error with the ini file */
-        con_printmsg(LVL_ERROR, file, line, "error", error);
+        con_printmsg(LVL_ERROR, parse_file, line, 0 /*TODO: column for ini error*/, "error", error);
         vec_free(error);
     }
+    mem_d(parse_file);
 
-    file_close(ini);
-}  
+    fs_file_close(ini);
+}