Correct type inconsistency in LC_OPT machinery.
[libfirm] / ir / libcore / lc_opts.c
index b9821f0..d68318f 100644 (file)
 #include <string.h>
 #include <ctype.h>
 
-#ifdef _WIN32
-#include <malloc.h>
-#endif
-#ifdef HAVE_ALLOCA_H
-#include <alloca.h>
-#endif
-
-/* Includes to determine user's home directory */
-#ifdef _WIN32
-#include <shlobj.h>
-#else
-#include <sys/types.h>
-#include <unistd.h>
-#include <pwd.h>
-#endif
-
-/* maximum length of a path. */
-#ifndef MAX_PATH
-#define MAX_PATH 2048
-#endif
-
-
-#include "lc_common_t.h"
 #include "lc_opts_t.h"
 #include "lc_opts_enum.h"
-#include "lc_parser_t.h"
 #include "hashptr.h"
 #include "lc_printf.h"
 #include "xmalloc.h"
+#include "obst.h"
 
 #define ERR_STRING "In argument \"%s\": "
 
@@ -112,9 +89,9 @@ static lc_opt_entry_t *init_entry(lc_opt_entry_t *ent, lc_opt_entry_t *parent,
        const char *copied_desc;
 
        obstack_grow0(&obst, name, strlen(name));
-       copied_name = obstack_finish(&obst);
+       copied_name = (char*)obstack_finish(&obst);
        obstack_grow0(&obst, desc, strlen(desc));
-       copied_desc = obstack_finish(&obst);
+       copied_desc = (char*)obstack_finish(&obst);
 
        memset(ent, 0, sizeof(*ent));
        set_name(ent, copied_name);
@@ -311,7 +288,7 @@ static lc_opt_entry_t *resolve_up_to_last_str_rec(lc_opt_entry_t *from,
                size_t next = strspn(path + end, path_delim);
 
                /* copy the part of the path into a buffer */
-               char *buf = malloc((end+1) * sizeof(buf[0]));
+               char *buf = (char*)malloc((end+1) * sizeof(buf[0]));
                strncpy(buf, path, end);
                buf[end] = '\0';
 
@@ -362,11 +339,12 @@ static char *strtolower(char *buf, size_t n, const char *str)
        return buf;
 }
 
-int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size_t length, ...)
+int lc_opt_std_cb(const char *name, lc_opt_type_t type, void *data, size_t length, ...)
 {
        va_list args;
        int res = 0;
        int integer;
+       (void) name;
 
        va_start(args, length);
 
@@ -376,17 +354,17 @@ int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size
                case lc_opt_type_bit:
                        integer = va_arg(args, int);
                        if (integer)
-                               *((int *) data) |= length;
+                               *(unsigned*)data |= length;
                        else
-                               *((int *) data) &= ~length;
+                               *(unsigned*)data &= ~length;
                        break;
 
                case lc_opt_type_negbit:
                        integer = va_arg(args, int);
                        if (integer)
-                               *((int *) data) &= ~length;
+                               *(unsigned*)data &= ~length;
                        else
-                               *((int *) data) |= length;
+                               *(unsigned*)data |= length;
                        break;
 
                case lc_opt_type_boolean:
@@ -398,7 +376,7 @@ int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size
                        break;
 
                case lc_opt_type_string:
-                       strncpy(data, va_arg(args, const char *), length);
+                       strncpy((char*)data, va_arg(args, const char *), length);
                        break;
 
                case lc_opt_type_int:
@@ -417,9 +395,11 @@ int lc_opt_std_cb(UNUSED(const char *name), lc_opt_type_t type, void *data, size
        return res;
 }
 
-int lc_opt_std_dump(char *buf, size_t n, UNUSED(const char *name), lc_opt_type_t type, void *data, UNUSED(size_t length))
+int lc_opt_std_dump(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length)
 {
        int res;
+       (void) name;
+       (void) length;
 
        if (data) {
                switch (type) {
@@ -432,7 +412,7 @@ int lc_opt_std_dump(char *buf, size_t n, UNUSED(const char *name), lc_opt_type_t
                        res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false");
                        break;
                case lc_opt_type_string:
-                       strncpy(buf, data, n);
+                       strncpy(buf, (const char*)data, n);
                        res = n;
                        break;
                case lc_opt_type_int:
@@ -455,8 +435,12 @@ int lc_opt_std_dump(char *buf, size_t n, UNUSED(const char *name), lc_opt_type_t
        return res;
 }
 
-int lc_opt_bool_dump_vals(char *buf, size_t n, UNUSED(const char *name), UNUSED(lc_opt_type_t type), UNUSED(void *data), UNUSED(size_t length))
+int lc_opt_bool_dump_vals(char *buf, size_t n, const char *name, lc_opt_type_t type, void *data, size_t length)
 {
+       (void) name;
+       (void) type;
+       (void) data;
+       (void) length;
        strncpy(buf, "true, false", n);
        return n;
 }
@@ -542,6 +526,8 @@ int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err
                        if (s->cb(opt->name, s->type, s->value, s->length, value))
                                error = lc_opt_err_none;
                        break;
+               case lc_opt_type_invalid:
+                       abort();
        }
 
        set_error(err, error, value);
@@ -709,54 +695,22 @@ static int lc_opts_default_error_handler(const char *prefix, const lc_opt_err_in
        return 0;
 }
 
-void lc_opt_from_file(const char *filename, FILE *f, lc_opt_error_handler_t *handler)
-{
-       if (handler == NULL)
-               handler = lc_opts_default_error_handler;
-       PMANGLE(in) = f;
-       lc_opt_init_parser(filename, handler);
-       PMANGLE(parse)();
-}
-
 int lc_opt_from_single_arg(const lc_opt_entry_t *root,
                                                   const char *opt_prefix,
                                                   const char *arg, lc_opt_error_handler_t *handler)
 {
        const lc_opt_entry_t *grp = root;
-       int n                     = strlen(arg);
-       int n_prefix              = opt_prefix ? strlen(opt_prefix) : 0;
+       size_t n                  = strlen(arg);
+       size_t n_prefix           = opt_prefix ? strlen(opt_prefix) : 0;
        int error                 = 0;
        int ret                   = 0;
 
        lc_opt_err_info_t err;
-       char *end, *buf, *eqsign;
+       const char *end, *eqsign;
 
        if (n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) {
                arg = arg + n_prefix;
 
-               /*
-                * check, if the next character is a @.
-                * This means, that we want to read options
-                * from a file.
-                */
-               if (arg[0] == '@') {
-                       size_t n    = strcspn(&arg[1], " \t\n");
-                       char *fname = alloca(n + 1);
-                       FILE *f;
-
-                       strncpy(fname, &arg[1], n);
-                       if ((f = fopen(fname, "rt")) != NULL) {
-                               lc_opt_from_file(fname, f, handler);
-                               fclose(f);
-                               set_error(&err, lc_opt_err_none, NULL);
-                       }
-
-                       else
-                               set_error(&err, lc_opt_err_file_not_found, arg);
-
-                       return !lc_opt_raise_error(&err, handler, "Unable to open file: %s", fname);
-               }
-
                /* find the next delimiter (the -) and extract the string up to
                 * there. */
                end    = strchr(arg, OPT_DELIM);
@@ -768,7 +722,7 @@ int lc_opt_from_single_arg(const lc_opt_entry_t *root,
                         * Copy the part of the option into the buffer and add the
                         * finalizing zero.
                         */
-                       buf = obstack_copy0(&obst, arg, end - arg);
+                       char *buf = (char*)obstack_copy0(&obst, arg, end - arg);
 
                        /* Resolve the group inside the group */
                        grp = lc_opt_find_grp(grp, buf, &err);
@@ -787,6 +741,7 @@ int lc_opt_from_single_arg(const lc_opt_entry_t *root,
 
                if (!error) {
                        lc_opt_entry_t *opt;
+                       char           *buf;
 
                        /*
                         * Now, we are at the last option part:
@@ -796,7 +751,7 @@ int lc_opt_from_single_arg(const lc_opt_entry_t *root,
                         * later.
                         */
                        end = strchr(arg, '=');
-                       buf = obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
+                       buf = (char*)obstack_copy0(&obst, arg, end ? end - arg : (int) strlen(arg));
                        opt = lc_opt_find_opt(grp, buf, &err);
                        error = lc_opt_raise_error(&err, handler, ERR_STRING, arg);
 
@@ -835,8 +790,9 @@ int lc_opt_from_argv(const lc_opt_entry_t *root,
        return options_set;
 }
 
-static int opt_arg_type(UNUSED(const lc_arg_occ_t *occ))
+static int opt_arg_type(const lc_arg_occ_t *occ)
 {
+       (void) occ;
        return lc_arg_type_ptr;
 }
 
@@ -844,7 +800,7 @@ static int opt_arg_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_
 {
        char buf[256];
 
-       lc_opt_entry_t *opt = arg->v_ptr;
+       lc_opt_entry_t *opt = (lc_opt_entry_t*)arg->v_ptr;
        const char     *s   = buf;
        size_t          res = 0;
 
@@ -894,58 +850,3 @@ const lc_arg_env_t *lc_opt_get_arg_env(void)
 
        return env;
 }
-
-void lc_opt_default_configs(const char *ini_name)
-{
-       FILE *f;
-       char path[MAX_PATH];
-       char local_ini_file[MAX_PATH];
-       char home_dir_ini_file[MAX_PATH];
-
-       /* <cmnt>.ini */
-       strncpy(local_ini_file, ini_name, sizeof(local_ini_file));
-       strncat(local_ini_file, ".ini", sizeof(local_ini_file)-1);
-       local_ini_file[sizeof(local_ini_file) - 1] = '\0';
-       path[0] = '\0';
-
-#ifdef _WIN32
-#if _MSC_VER > 1200
-       /* ARG: need newer SDK to compile this */
-       SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path);
-       strncat(path, "\\", sizeof(path)-1);
-#endif
-       strncpy(home_dir_ini_file, local_ini_file, sizeof(home_dir_ini_file));
-       home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
-#else
-       {
-               struct passwd *entry = getpwuid(getuid());
-               if (entry != NULL) {
-                       strcpy(path, entry->pw_dir);
-                       strncat(path, "/", sizeof(path)-1);
-                       /* .<cmnt>rc */
-                       snprintf(home_dir_ini_file, sizeof(home_dir_ini_file), ".%src", ini_name);
-                       home_dir_ini_file[sizeof(home_dir_ini_file) - 1] = '\0';
-               } else {
-                       /* FIXME: some error occurred */
-                       home_dir_ini_file[0] = '\0';
-               }
-       }
-#endif
-
-       strncat(path, home_dir_ini_file, sizeof(path)-1);
-       path[sizeof(path) - 1] = '\0';
-
-       /* Process ini file in user's home. */
-       f = fopen(path, "rt");
-       if (f) {
-               lc_opt_from_file(path, f, lc_opts_default_error_handler);
-               fclose(f);
-       }
-
-       /* Process ini file in current directory. */
-       f = fopen(local_ini_file, "rt");
-       if (f) {
-               lc_opt_from_file(local_ini_file, f, lc_opts_default_error_handler);
-               fclose(f);
-       }
-}