X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Flibcore%2Flc_opts.c;h=fc9c16207f5d3345adda05c39dd2fa2c4b5cf333;hb=998a7d33f7fe446db8e265d54138841fcfe018bf;hp=04b456b44f0a90b37a311e10aa4e315d1e106295;hpb=9c359401bbbb6cc870909cf556f87a375807bdb7;p=libfirm diff --git a/ir/libcore/lc_opts.c b/ir/libcore/lc_opts.c index 04b456b44..fc9c16207 100644 --- a/ir/libcore/lc_opts.c +++ b/ir/libcore/lc_opts.c @@ -1,21 +1,7 @@ /* - libcore: library for basic data structures and algorithms. - Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + * This file is part of libFirm. + * Copyright (C) 2012 IPD Goos, Universit"at Karlsruhe, Germany + */ #include "config.h" #include @@ -24,35 +10,13 @@ #include #include -#ifdef _WIN32 -#include -#endif -#ifdef HAVE_ALLOCA_H -#include -#endif - -/* Includes to determine user's home directory */ -#ifdef _WIN32 -#include -#else -#include -#include -#include -#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 "util.h" #include "xmalloc.h" +#include "obst.h" #define ERR_STRING "In argument \"%s\": " @@ -66,7 +30,7 @@ static struct obstack obst; static void set_name(lc_opt_entry_t *ent, const char *name) { ent->name = name; - ent->hash = HASH_STR(name, strlen(name)); + ent->hash = hash_str(name); } #define entry_matches(ent,hash_val,str) \ @@ -198,8 +162,6 @@ static const char *get_type_name(lc_opt_type_t type) XXX(double); XXX(boolean); XXX(string); - case lc_opt_type_negbit: res = "bit"; break; - case lc_opt_type_negboolean: res = "boolean"; break; default: res = ""; } @@ -254,9 +216,9 @@ lc_opt_entry_t *lc_opt_add_opt(lc_opt_entry_t *parent, static lc_opt_entry_t *lc_opt_find_ent(const struct list_head *head, const char *name, int error_to_use, lc_opt_err_info_t *err) { - lc_opt_entry_t *ent, *found = NULL; + lc_opt_entry_t *found = NULL; int error = error_to_use; - unsigned hash = HASH_STR(name, strlen(name)); + unsigned hash = hash_str(name); if (!list_empty(head)) { list_for_each_entry(lc_opt_entry_t, ent, head, list) { @@ -358,15 +320,16 @@ static char *strtolower(char *buf, size_t n, const char *str) { unsigned i; for (i = 0; i < n; ++i) - buf[i] = tolower(str[i]); + buf[i] = tolower((unsigned char)str[i]); 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,27 +339,15 @@ 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; - else - *((int *) 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: *((int *) data) = va_arg(args, int); break; - case lc_opt_type_negboolean: - *((int *) data) = !va_arg(args, int); - break; - case lc_opt_type_string: strncpy((char*)data, va_arg(args, const char *), length); break; @@ -417,18 +368,18 @@ 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) { case lc_opt_type_bit: - case lc_opt_type_negbit: res = snprintf(buf, n, "%x", *((unsigned *) data)); break; case lc_opt_type_boolean: - case lc_opt_type_negboolean: res = snprintf(buf, n, "%s", *((int *) data) ? "true" : "false"); break; case lc_opt_type_string: @@ -455,8 +406,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; } @@ -516,11 +471,9 @@ int lc_opt_occurs(lc_opt_entry_t *opt, const char *value, lc_opt_err_info_t *err break; case lc_opt_type_boolean: - case lc_opt_type_negboolean: case lc_opt_type_bit: - case lc_opt_type_negbit: strtolower(buf, sizeof(buf), value); - for (i = 0; i < LC_ARRSIZE(bool_strings); ++i) { + for (i = 0; i < ARRAY_SIZE(bool_strings); ++i) { if (strcmp(buf, bool_strings[i].str) == 0) { val->integer = bool_strings[i].val; error = lc_opt_err_none; @@ -626,7 +579,6 @@ static void lc_opt_print_help_rec(lc_opt_entry_t *ent, char separator, lc_opt_en char grp_name[512]; char value[256]; char values[512]; - lc_opt_entry_t *e; if (!list_empty(&s->opts)) { lc_opt_print_grp_path(grp_name, sizeof(grp_name), ent, separator, stop_ent); @@ -684,8 +636,6 @@ static void lc_opt_print_tree_grp_indent(lc_opt_entry_t *ent, FILE *f, int level lc_grp_special_t *s; if (ent->is_grp) { - lc_opt_entry_t *e; - s = lc_get_grp_special(ent); indent(f, level); fprintf(f, "/%s\n", ent->name); @@ -711,54 +661,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; size_t n = strlen(arg); - size_t n_prefix = opt_prefix ? strlen(opt_prefix) : 0; + size_t n_prefix = opt_prefix != NULL ? strlen(opt_prefix) : 0; int error = 0; int ret = 0; lc_opt_err_info_t err; const char *end, *eqsign; - if (n >= n_prefix && strncmp(opt_prefix, arg, n_prefix) == 0) { + if (n >= n_prefix && (n_prefix == 0 || 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 = (char*)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); @@ -838,8 +756,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; } @@ -897,58 +816,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]; - - /* .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); - /* .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); - } -}