new config file parser, simplified and usable for command-line options

This commit is contained in:
Albert Cahalan 2009-11-08 07:28:16 +00:00
parent e318a23c4f
commit dae8eda15d

View file

@ -18785,142 +18785,141 @@ void load_info_about_label_surface(char lfname[1024])
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
static void parse_options(char *filename) static void parse_one_option(const char *str, const char *arg)
{ {
FILE *fi = fopen(filename, "r"); // canonicalize the option
if(!fi) if(!strcmp(arg,"yes"))
arg=NULL;
if(isdigit(*str))
{
if(!strcmp(arg,"no"))
str = "640x480";
arg = str;
str = "windowsize";
}
if(str[0]=='n' && str[1]=='o')
{
str += 2;
if(!arg)
arg="no";
else if(!strcmp(arg,"no"))
arg=NULL;
else
{
printf("could not parse option '%s' with arg '%s'\n",str-2,arg);
return; return;
char str[256];
do
{
fgets(str, sizeof(str), fi);
strip_trailing_whitespace(str);
if (!feof(fi))
{
debug(str);
/* FIXME: This should be handled better! */
/* (e.g., complain on illegal lines, support comments, blanks, etc.) */
if (strcmp(str, "fullscreen=yes") == 0)
{
fullscreen = 1;
} }
else if (strcmp(str, "fullscreen=native") == 0) }
int boolval = 1;
if(arg && !strcmp(arg,"no"))
boolval = 0;
if (!strcmp(str, "fullscreen"))
{ {
fullscreen = 1; fullscreen = boolval;
if (!strcmp(arg, "native"))
native_screensize = 1; native_screensize = 1;
} }
else if (strcmp(str, "disablescreensaver=yes") == 0)
else if (!strcmp(str, "disablescreensaver"))
{ {
disable_screensaver = 1; disable_screensaver = boolval;
} }
else if (strcmp(str, "disablescreensaver=no") == 0 ||
strcmp(str, "allowscreensaver=yes") == 0) else if (!strcmp(str, "allowscreensaver"))
{ {
disable_screensaver = 0; disable_screensaver = !boolval;
} }
else if (strcmp(str, "native=yes") == 0)
else if (!strcmp(str, "native"))
{ {
native_screensize = 1; native_screensize = boolval;
} }
else if (strcmp(str, "native=no") == 0)
else if (!strcmp(str, "fullscreen"))
{ {
native_screensize = 0; fullscreen = boolval;
} }
else if (strcmp(str, "fullscreen=no") == 0 ||
strcmp(str, "windowed=yes") == 0) else if (!strcmp(str, "windowed"))
{ {
fullscreen = 0; fullscreen = !boolval;
} }
else if (strcmp(str, "startblank=yes") == 0)
else if (!strcmp(str, "startblank"))
{ {
start_blank = 1; start_blank = boolval;
} }
else if (strcmp(str, "startblank=no") == 0 ||
strcmp(str, "startlast=yes") == 0) else if (!strcmp(str, "startlast"))
{ {
start_blank = 0; start_blank = !boolval;
} }
else if (strcmp(str, "nostampcontrols=yes") == 0)
else if (!strcmp(str, "stampcontrols"))
{ {
disable_stamp_controls = 1; disable_stamp_controls = !boolval;
} }
else if (strcmp(str, "nostampcontrols=no") == 0 ||
strcmp(str, "stampcontrols=yes") == 0) else if (!strcmp(str, "alllocalefonts"))
{ {
disable_stamp_controls = 0; all_locale_fonts = boolval;
} }
else if (strcmp(str, "alllocalefonts=yes") == 0)
else if (!strcmp(str, "currentlocalefont"))
{ {
all_locale_fonts = 1; all_locale_fonts = !boolval;
} }
else if (strcmp(str, "alllocalefonts=no") == 0 ||
strcmp(str, "currentlocalefont=yes") == 0) else if (!strcmp(str, "magiccontrols"))
{ {
all_locale_fonts = 0; disable_magic_controls = !boolval;
} }
else if (strcmp(str, "nomagiccontrols=yes") == 0)
else if (!strcmp(str, "label"))
{ {
disable_magic_controls = 1; disable_label = !boolval;
} }
else if (strcmp(str, "nomagiccontrols=no") == 0 ||
strcmp(str, "magiccontrols=yes") == 0) else if (!strcmp(str, "mirrorstamps"))
{ {
disable_magic_controls = 0; mirrorstamps = boolval;
} }
else if (strcmp(str, "nolabel=yes") == 0) else if (!strcmp(str, "dontmirrorstamps"))
{ {
disable_label = 1; mirrorstamps = !boolval;
} }
else if (strcmp(str, "nolabel=no") == 0 ||
strcmp(str, "label=yes") == 0) else if (!strcmp(str, "stampsize"))
{
disable_label = 0;
}
else if (strcmp(str, "mirrorstamps=yes") == 0)
{
mirrorstamps = 1;
}
else if (strcmp(str, "mirrorstamps=no") == 0 ||
strcmp(str, "dontmirrorstamps=yes") == 0)
{
mirrorstamps = 0;
}
else if (strcmp(str, "stampsize=default") == 0)
{ {
if (!strcmp(arg, "default"))
stamp_size_override = -1; stamp_size_override = -1;
} else
else if (strstr(str, "stampsize=") == str)
{ {
// FIXME: needs to be a scaling factor // FIXME: needs to be a scaling factor
stamp_size_override = atoi(str + 10); stamp_size_override = atoi(arg);
if (stamp_size_override > 10) if (stamp_size_override > 10)
stamp_size_override = 10; stamp_size_override = 10;
} }
else if (strcmp(str, "noshortcuts=yes") == 0)
{
noshortcuts = 1;
} }
else if (strcmp(str, "noshortcuts=no") == 0 ||
strcmp(str, "shortcuts=yes") == 0) else if (!strcmp(str, "shortcuts"))
{ {
noshortcuts = 0; noshortcuts = !boolval;
} }
else if (!memcmp("windowsize=", str, 11))
else if (!strcmp(str, "windowsize"))
{ {
char *endp1; char *endp1;
char *endp2; char *endp2;
int w, h; int w, h;
w = strtoul(str + 11, &endp1, 10); w = strtoul(arg, &endp1, 10);
h = strtoul(endp1 + 1, &endp2, 10); h = strtoul(endp1 + 1, &endp2, 10);
/* sanity check it */ /* sanity check it */
if (str + 11 == endp1 || endp1 + 1 == endp2 || *endp1 != 'x' || *endp2 if (arg == endp1 || endp1 + 1 == endp2 || *endp1 != 'x' || *endp2
|| w < 500 || h < 480 || h > w * 3 || w > h * 4) || w < 500 || h < 480 || h > w * 3 || w > h * 4)
{ {
/* Oddly, config files have no error checking. */ /* Oddly, config files have no error checking. */
@ -18933,194 +18932,132 @@ static void parse_options(char *filename)
WINDOW_HEIGHT = h; WINDOW_HEIGHT = h;
} }
} }
else if (strcmp(str, "800x600=yes") == 0 ||
strcmp(str, "windowsize=800x600") == 0) else if (!strcmp(str, "outlines"))
{ {
/* to handle old config files */ dont_do_xor = !boolval;
WINDOW_WIDTH = 800;
WINDOW_HEIGHT = 600;
} }
else if (strcmp(str, "800x600=no") == 0 ||
strcmp(str, "640x480=yes") == 0 || else if (!strcmp(str, "keyboard"))
strcmp(str, "windowsize=640x480") == 0)
{ {
/* also for old config files */ keymouse = boolval;
WINDOW_WIDTH = 640;
WINDOW_HEIGHT = 480;
} }
else if (strcmp(str, "nooutlines=yes") == 0)
else if (!strcmp(str, "mouse"))
{ {
dont_do_xor = 1; keymouse = !boolval;
} }
else if (strcmp(str, "nooutlines=no") == 0 ||
strcmp(str, "outlines=yes") == 0) else if (!strcmp(str, "wheelmouse"))
{ {
dont_do_xor = 0; wheely = boolval;
} }
else if (strcmp(str, "keyboard=yes") == 0)
else if (!strcmp(str, "grab"))
{ {
keymouse = 1; grab_input = boolval;
} }
else if (strcmp(str, "keyboard=no") == 0 ||
strcmp(str, "mouse=yes") == 0) else if (!strcmp(str, "fancycursors"))
{ {
keymouse = 0; no_fancy_cursors = !boolval;
} }
else if (strcmp(str, "nowheelmouse=yes") == 0)
else if (!strcmp(str, "hidecursor"))
{ {
wheely = 0; hide_cursor = boolval;
} }
else if (strcmp(str, "nowheelmouse=no") == 0 ||
strcmp(str, "wheelmouse=yes") == 0) else if (!strcmp(str, "uppercase"))
{ {
wheely = 1; only_uppercase = boolval;
} }
else if (strcmp(str, "grab=yes") == 0)
else if (!strcmp(str, "mixedcase"))
{ {
grab_input = 1; only_uppercase = !boolval;
} }
else if (strcmp(str, "grab=no") == 0 || strcmp(str, "nograb=yes") == 0)
else if (!strcmp(str, "quit"))
{ {
grab_input = 0; disable_quit = !boolval;
} }
else if (strcmp(str, "nofancycursors=yes") == 0)
else if (!strcmp(str, "save"))
{ {
no_fancy_cursors = 1; disable_save = !boolval;
} }
else if (strcmp(str, "nofancycursors=no") == 0 ||
strcmp(str, "fancycursors=yes") == 0) else if (!strcmp(str, "print"))
{ {
no_fancy_cursors = 0; disable_print = !boolval;
} }
else if (strcmp(str, "hidecursor=yes") == 0)
else if (!strcmp(str, "stamps"))
{ {
hide_cursor = 1; dont_load_stamps = !boolval;
} }
else if (strcmp(str, "hidecursor=no") == 0 ||
strcmp(str, "showcursor=yes") == 0) else if (!strcmp(str, "sysfonts"))
{ {
hide_cursor = 0; no_system_fonts = !boolval;
} }
else if (strcmp(str, "uppercase=yes") == 0)
else if (!strcmp(str, "buttondistinction"))
{ {
only_uppercase = 1; no_button_distinction = !boolval;
} }
else if (strcmp(str, "uppercase=no") == 0 ||
strcmp(str, "mixedcase=yes") == 0) else if (!strcmp(str, "sound"))
{ {
only_uppercase = 0; use_sound = boolval;
} }
else if (strcmp(str, "noquit=yes") == 0)
else if (!strcmp(str, "simpleshapes"))
{ {
disable_quit = 1; simple_shapes = boolval;
} }
else if (strcmp(str, "noquit=no") == 0 || strcmp(str, "quit=yes") == 0) else if (!strcmp(str, "complexshapes"))
{ {
disable_quit = 0; simple_shapes = !boolval;
}
else if (strcmp(str, "nosave=yes") == 0)
{
disable_save = 1;
}
else if (strcmp(str, "nosave=no") == 0 || strcmp(str, "save=yes") == 0)
{
disable_save = 0;
}
else if (strcmp(str, "noprint=yes") == 0)
{
disable_print = 1;
}
else if (strcmp(str, "noprint=no") == 0 ||
strcmp(str, "print=yes") == 0)
{
disable_print = 0;
}
else if (strcmp(str, "nostamps=yes") == 0)
{
dont_load_stamps = 1;
}
else if (strcmp(str, "nostamps=no") == 0 ||
strcmp(str, "stamps=yes") == 0)
{
dont_load_stamps = 0;
}
else if (strcmp(str, "nosysfonts=yes") == 0 ||
strcmp(str, "sysfonts=no") == 0)
{
no_system_fonts = 1;
}
else if (strcmp(str, "nosysfonts=no") == 0 ||
strcmp(str, "sysfonts=yes") == 0)
{
no_system_fonts = 0;
}
else if (strcmp(str, "nobuttondistinction=yes") == 0)
{
no_button_distinction = 1;
}
else if (strcmp(str, "nobuttondistinction=no") == 0 ||
strcmp(str, "buttondistinction=yes") == 0)
{
no_button_distinction = 0;
}
else if (strcmp(str, "nosound=yes") == 0)
{
use_sound = 0;
}
else if (strcmp(str, "nosound=no") == 0 ||
strcmp(str, "sound=yes") == 0)
{
use_sound = 1;
}
else if (strcmp(str, "simpleshapes=yes") == 0)
{
simple_shapes = 1;
}
else if (strcmp(str, "simpleshapes=no") == 0 ||
strcmp(str, "complexshapes=yes") == 0)
{
simple_shapes = 1;
} }
/* Should "locale=" be here as well??? */ /* Should "locale=" be here as well??? */
/* Comments welcome ... bill@newbreedsoftware.com */ /* Comments welcome ... bill@newbreedsoftware.com */
else if (strstr(str, "lang=") == str) else if (!strcmp(str, "lang"))
{ {
set_langstr(str + 5); if(arg)
set_langstr(arg);
} }
else if (strstr(str, "colorfile=") == str)
else if (!strcmp(str, "colorfile"))
{ {
strcpy(colorfile, str + 10); strcpy(colorfile, arg); // FIXME can overflow
} }
else if (strstr(str, "printdelay=") == str)
else if (!strcmp(str, "printdelay"))
{ {
sscanf(str + 11, "%d", &print_delay); sscanf(arg, "%d", &print_delay);
#ifdef DEBUG #ifdef DEBUG
printf("Print delay set to %d seconds\n", print_delay); printf("Print delay set to %d seconds\n", print_delay);
#endif #endif
} }
else if (strcmp(str, "printcfg=yes") == 0)
else if (!strcmp(str, "printcfg"))
{ {
#if !defined(WIN32) && !defined(__APPLE__) #if !defined(WIN32) && !defined(__APPLE__)
fprintf(stderr, "Note: printcfg option only applies to Windows and Mac OS X!\n"); fprintf(stderr, "Note: printcfg option only applies to Windows and Mac OS X!\n");
#endif #endif
use_print_config = 1; use_print_config = boolval;
}
else if (strcmp(str, "printcfg=no") == 0 ||
strcmp(str, "noprintcfg=yes") == 0)
{
#if !defined(WIN32) && !defined(__APPLE__)
fprintf(stderr, "Note: printcfg option only applies to Windows and Mac OS X!\n");
#endif
use_print_config = 0;
} }
#if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__)
else if (strstr(str, "printcommand=") == str) else if (!strcmp(str, "printcommand"))
{ {
/* FIXME: This would need to be done one argument (space-delim'd) at a time */ // FIXME: This would need to be done one argument (space-delim'd) at a time */
/* #if 0 && defined(__linux__)
#ifdef __linux__
wordexp_t result; wordexp_t result;
char * dir = strdup(str + 13); char * dir = strdup(arg);
wordexp(dir, &result, 0); wordexp(dir, &result, 0);
free(dir); free(dir);
@ -19128,19 +19065,18 @@ static void parse_options(char *filename)
printcommand = strdup(result.we_wordv[0]); printcommand = strdup(result.we_wordv[0]);
wordfree(&result); wordfree(&result);
#else #else
*/ printcommand = strdup(arg);
printcommand = strdup(str + 13);
/*
#endif #endif
*/
} }
else if (strstr(str, "altprintcommand=") == str) #endif
#if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__)
else if (!strcmp(str, "altprintcommand"))
{ {
/* FIXME: This would need to be done one argument (space-delim'd) at a time */ // FIXME: This would need to be done one argument (space-delim'd) at a time
/* #if 0 && defined(__linux__)
#ifdef __linux__
wordexp_t result; wordexp_t result;
char * dir = strdup(str + 16); char * dir = strdup(arg);
wordexp(dir, &result, 0); wordexp(dir, &result, 0);
free(dir); free(dir);
@ -19148,60 +19084,51 @@ static void parse_options(char *filename)
altprintcommand = strdup(result.we_wordv[0]); altprintcommand = strdup(result.we_wordv[0]);
wordfree(&result); wordfree(&result);
#else #else
*/ altprintcommand = strdup(arg);
altprintcommand = strdup(str + 16);
/*
#endif #endif
*/
} }
#endif #endif
else if (strcmp(str, "saveover=yes") == 0)
else if (!strcmp(str, "saveover"))
{ {
if (!strcmp(arg, "yes"))
promptless_save = SAVE_OVER_ALWAYS; promptless_save = SAVE_OVER_ALWAYS;
} else if (!strcmp(arg, "ask"))
else if (strcmp(str, "saveover=ask") == 0) promptless_save = SAVE_OVER_PROMPT; // default
{ else if (!strcmp(arg, "new"))
/* (Default) */
promptless_save = SAVE_OVER_PROMPT;
}
else if (strcmp(str, "saveover=new") == 0)
{
promptless_save = SAVE_OVER_NO; promptless_save = SAVE_OVER_NO;
else if (!strcmp(arg, "no"))
printf("saveover can not have value 'no'\n");
} }
else if (strcmp(str, "autosave=yes") == 0)
{
autosave_on_quit = 1;
}
else if (strcmp(str, "autosave=no") == 0)
{
autosave_on_quit = 0;
}
else if (strcmp(str, "altprint=always") == 0)
{
alt_print_command_default = ALTPRINT_ALWAYS;
}
else if (strcmp(str, "altprint=mod") == 0)
{
/* (Default) */
alt_print_command_default = ALTPRINT_MOD; else if (!strcmp(str, "autosave"))
}
else if (strcmp(str, "altprint=never") == 0)
{ {
autosave_on_quit = boolval;
}
else if (!strcmp(str, "altprint"))
{
if (!strcmp(arg, "always"))
alt_print_command_default = ALTPRINT_ALWAYS;
else if (!strcmp(arg, "mod"))
alt_print_command_default = ALTPRINT_MOD; // default
else if (!strcmp(arg, "never"))
alt_print_command_default = ALTPRINT_NEVER; alt_print_command_default = ALTPRINT_NEVER;
} }
#if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__) #if !defined(WIN32) && !defined(__APPLE__) && !defined(__BEOS__)
else if (strstr(str, "papersize=") == str) else if (!strcmp(str, "papersize"))
{ {
papersize = strdup(str + strlen("papersize=")); papersize = strdup(arg);
} }
#endif #endif
else if (strstr(str, "savedir=") == str)
else if (!strcmp(str, "savedir"))
{ {
#ifdef __linux__ #ifdef __linux__
wordexp_t result; wordexp_t result;
char * dir = strdup(str + 8); char * dir = strdup(arg);
wordexp(dir, &result, 0); wordexp(dir, &result, 0);
free(dir); free(dir);
@ -19209,7 +19136,7 @@ static void parse_options(char *filename)
savedir = strdup(result.we_wordv[0]); savedir = strdup(result.we_wordv[0]);
wordfree(&result); wordfree(&result);
#else #else
savedir = strdup(str + 8); savedir = strdup(arg);
#endif #endif
remove_slash(savedir); remove_slash(savedir);
@ -19217,11 +19144,12 @@ static void parse_options(char *filename)
printf("savedir set to: %s\n", savedir); printf("savedir set to: %s\n", savedir);
#endif #endif
} }
else if (strstr(str, "datadir=") == str)
else if (!strcmp(str, "datadir"))
{ {
#ifdef __linux__ #ifdef __linux__
wordexp_t result; wordexp_t result;
char * dir = strdup(str + 8); char * dir = strdup(arg);
wordexp(dir, &result, 0); wordexp(dir, &result, 0);
free(dir); free(dir);
@ -19229,7 +19157,7 @@ static void parse_options(char *filename)
datadir = strdup(result.we_wordv[0]); datadir = strdup(result.we_wordv[0]);
wordfree(&result); wordfree(&result);
#else #else
datadir = strdup(str + 8); datadir = strdup(arg);
#endif #endif
remove_slash(datadir); remove_slash(datadir);
@ -19237,17 +19165,36 @@ static void parse_options(char *filename)
printf("datadir set to: %s\n", datadir); printf("datadir set to: %s\n", datadir);
#endif #endif
} }
else if (strcmp(str, "nolockfile=yes") == 0 ||
strcmp(str, "lockfile=no") == 0) else if (!strcmp(str, "lockfile"))
{ {
ok_to_use_lockfile = 0; ok_to_use_lockfile = boolval;
} }
}
}
while (!feof(fi));
fclose(fi);
} }
/////////////////////////////////////////////////////////////////////
static void parse_file_options(char *filename)
{
FILE *fi = fopen(filename, "r");
if(!fi)
return;
char str[256];
while(fgets(str, sizeof(str), fi))
{
if(!isalnum(*str))
continue;
strip_trailing_whitespace(str);
char *arg = strchr(str,'=');
if(arg)
*arg++ = '\0';
parse_one_option(str,arg);
}
fclose(fi);
}
@ -19448,7 +19395,7 @@ static void setup(int argc, char *argv[])
strcpy(str, "tuxpaint.cfg"); strcpy(str, "tuxpaint.cfg");
#endif #endif
parse_options(str); parse_file_options(str);
} }
@ -19486,7 +19433,8 @@ static void setup(int argc, char *argv[])
} }
#endif #endif
parse_options(str);
parse_file_options(str);
/* Handle command-line arguments: */ /* Handle command-line arguments: */