From 0a7bd889d164aced54fcf836535e878dc4d3482b Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Sun, 29 Nov 2009 18:25:14 +0000 Subject: [PATCH] Better command-line handling (issue 266) contributed by Pasi Kallinen git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1227 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- helpfile.h | 11 +- main.c | 373 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 224 insertions(+), 160 deletions(-) diff --git a/helpfile.h b/helpfile.h index 9fcac1e1..9cca271b 100644 --- a/helpfile.h +++ b/helpfile.h @@ -343,6 +343,9 @@ static const T_Help_table helptable_credits[] = HELP_TEXT (" Petter Lindquist") HELP_TEXT (" C64 file and image formats") HELP_TEXT ("") + HELP_TEXT (" Pasi Kallinen") + HELP_TEXT (" Better command-line handling") + HELP_TEXT ("") HELP_TEXT ("") HELP_TITLE(" ART") HELP_TEXT ("") @@ -414,10 +417,10 @@ static const T_Help_table helptable_credits[] = HELP_TEXT (" HoraK-FDF iLKke Iw2evk ") HELP_TEXT (" Jamon keito kusma ") HELP_TEXT (" Lord Graga MagerValp mind ") - HELP_TEXT (" MooZ the Peach petter ") - HELP_TEXT (" richienyhus tape.wyrm TeeEmCee ") - HELP_TEXT (" tempest Timo Kurrpa titus^Rab ") - HELP_TEXT (" Tobé 00ai99") + HELP_TEXT (" MooZ Pasi Kallinen the Peach ") + HELP_TEXT (" petter richienyhus tape.wyrm ") + HELP_TEXT (" TeeEmCee tempest Timo Kurrpa ") + HELP_TEXT (" titus^Rab Tobé 00ai99") HELP_TEXT ("") HELP_TEXT ("") HELP_TEXT (" ... posted the annoying bug reports.") diff --git a/main.c b/main.c index ca74de99..e50f6447 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ */ /* Grafx2 - The Ultimate 256-color bitmap paint program + Copyright 2009 Pasi Kallinen Copyright 2008 Peter Gordon Copyright 2008 Franck Charlet Copyright 2007 Adrien Destugues @@ -33,7 +34,7 @@ // There is no WM on the GP2X... #ifndef __GP2X__ - #include + #include #endif #include "const.h" @@ -81,29 +82,31 @@ void Display_syntax(void) { int mode_index; - printf("Syntax: grafx2 [] []\n\n"); + printf("Syntax: grafx2 [] [] []\n\n"); printf(" can be:]\n"); - printf("\t/? /h /help for this help screen\n"); - printf("\t/wide to emulate a video mode with wide pixels (2x1)\n"); - printf("\t/tall to emulate a video mode with tall pixels (1x2)\n"); - printf("\t/double to emulate a video mode with double pixels (2x2)\n"); - printf("\t/wide2 to emulate a video mode with double wide pixels (4x2)\n"); - printf("\t/tall2 to emulate a video mode with double tall pixels (2x4)\n"); - printf("\t/triple to emulate a video mode with triple pixels (3x3)\n"); - printf("\t/quadruple to emulate a video mode with quadruple pixels (4x4)\n"); - printf("\t/rgb n to reduce RGB precision from 256 to n levels\n"); - printf("\t/skin to use an alternate file with the menu graphics\n"); - printf("\t/mode to set a video mode\n\n"); + printf("\t-? -h -H -help for this help screen\n"); + printf("\t-wide to emulate a video mode with wide pixels (2x1)\n"); + printf("\t-tall to emulate a video mode with tall pixels (1x2)\n"); + printf("\t-double to emulate a video mode with double pixels (2x2)\n"); + printf("\t-wide2 to emulate a video mode with double wide pixels (4x2)\n"); + printf("\t-tall2 to emulate a video mode with double tall pixels (2x4)\n"); + printf("\t-triple to emulate a video mode with triple pixels (3x3)\n"); + printf("\t-quadruple to emulate a video mode with quadruple pixels (4x4)\n"); + printf("\t-rgb n to reduce RGB precision from 256 to n levels\n"); + printf("\t-skin to use an alternate file with the menu graphics\n"); + printf("\t-mode to set a video mode\n"); + printf("Arguments can be prefixed either by / - or --\n"); + printf("They can also be abbreviated.\n\n"); printf("Available video modes:\n\n"); for (mode_index = 0; mode_index < Nb_video_modes; mode_index += 12) { - int k; - for (k = 0; k < 6; k++) - { - if (mode_index + k >= Nb_video_modes) break; - printf("%12s",Mode_label(mode_index + k)); - } - puts(""); + int k; + for (k = 0; k < 6; k++) + { + if (mode_index + k >= Nb_video_modes) break; + printf("%12s",Mode_label(mode_index + k)); + } + puts(""); } } @@ -171,13 +174,49 @@ void Error_function(int error_code, const char *filename, int line_number, const } } +enum CMD_PARAMS +{ + CMDPARAM_HELP, + CMDPARAM_MODE, + CMDPARAM_PIXELRATIO_TALL, + CMDPARAM_PIXELRATIO_WIDE, + CMDPARAM_PIXELRATIO_DOUBLE, + CMDPARAM_PIXELRATIO_TRIPLE, + CMDPARAM_PIXELRATIO_QUAD, + CMDPARAM_PIXELRATIO_TALL2, + CMDPARAM_PIXELRATIO_WIDE2, + CMDPARAM_RGB, + CMDPARAM_SKIN +}; + +struct { + const char *param; + int id; +} cmdparams[] = { + {"?", CMDPARAM_HELP}, + {"h", CMDPARAM_HELP}, + {"H", CMDPARAM_HELP}, + {"help", CMDPARAM_HELP}, + {"mode", CMDPARAM_MODE}, + {"tall", CMDPARAM_PIXELRATIO_TALL}, + {"wide", CMDPARAM_PIXELRATIO_WIDE}, + {"double", CMDPARAM_PIXELRATIO_DOUBLE}, + {"triple", CMDPARAM_PIXELRATIO_TRIPLE}, + {"quadruple", CMDPARAM_PIXELRATIO_QUAD}, + {"tall2", CMDPARAM_PIXELRATIO_TALL2}, + {"wide2", CMDPARAM_PIXELRATIO_WIDE2}, + {"rgb", CMDPARAM_RGB}, + {"skin", CMDPARAM_SKIN} +}; + +#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof(x[0])) + // --------------------- Analyse de la ligne de commande --------------------- void Analyze_command_line(int argc, char * argv[]) { char *buffer ; int index; - File_in_command_line = 0; Resolution_in_command_line = 0; @@ -185,140 +224,162 @@ void Analyze_command_line(int argc, char * argv[]) for (index = 1; index 256) + if (!strcmp(s, cmdparams[tmpi].param)) { - Error(ERROR_COMMAND_LINE); - Display_syntax(); - exit(0); + paramtype = cmdparams[tmpi].id; + break; + } + else if (strstr(cmdparams[tmpi].param, s)) + { + param_matches++; + param_match = cmdparams[tmpi].id; } - Set_palette_RGB_scale(scale); } - else - { - Error(ERROR_COMMAND_LINE); - Display_syntax(); - exit(0); - } - } - else if ( !strcmp(argv[index],"/skin") ) - { - // GUI skin file - index++; - if (index 1) - { - // Il y a déjà 2 noms de fichiers et on vient d'en trouver un 3ème - Error(ERROR_COMMAND_LINE); - Display_syntax(); - exit(0); - } - else if (File_exists(argv[index])) - { - File_in_command_line ++; - buffer = Realpath(argv[index], NULL); + if (paramtype == -1 && param_matches == 1) + paramtype = param_match; - if (File_in_command_line == 1) - { - // Separate path from filename - Extract_path(Main_file_directory, buffer); - Extract_filename(Main_filename, buffer); - free(buffer); - } else { - Extract_path(Spare_file_directory, buffer); - Extract_filename(Spare_filename, buffer); - free(buffer); - } - } - else - { - Error(ERROR_COMMAND_LINE); + } + switch (paramtype) + { + case CMDPARAM_HELP: Display_syntax(); exit(0); - } + case CMDPARAM_MODE: + index++; + if (index 256) + { + Error(ERROR_COMMAND_LINE); + Display_syntax(); + exit(0); + } + Set_palette_RGB_scale(scale); + } + else + { + Error(ERROR_COMMAND_LINE); + Display_syntax(); + exit(0); + } + break; + case CMDPARAM_SKIN: + // GUI skin file + index++; + if (index 1) + { + // Il y a déjà 2 noms de fichiers et on vient d'en trouver un 3ème + Error(ERROR_COMMAND_LINE); + Display_syntax(); + exit(0); + } + else if (File_exists(argv[index])) + { + File_in_command_line ++; + buffer = Realpath(argv[index], NULL); + + if (File_in_command_line == 1) + { + // Separate path from filename + Extract_path(Main_file_directory, buffer); + Extract_filename(Main_filename, buffer); + free(buffer); + } + else + { + Extract_path(Spare_file_directory, buffer); + Extract_filename(Spare_filename, buffer); + free(buffer); + } + } + else + { + Error(ERROR_COMMAND_LINE); + Display_syntax(); + exit(0); + } + break; } } } @@ -772,7 +833,7 @@ int main(int argc,char * argv[]) char phoenix_filename2[MAX_PATH_CHARACTERS]; if(!Init_program(argc,argv)) { - Program_shutdown(); + Program_shutdown(); return 0; } @@ -819,17 +880,17 @@ int main(int argc,char * argv[]) switch (File_in_command_line) { - case 2: + case 2: Button_Reload(); - DEBUG(Main_filename, 0); - DEBUG(Spare_filename, 0); - Button_Page(); - // no break ! proceed with the other file now - case 1: - Button_Reload(); - Resolution_in_command_line = 0; - default: - break; + DEBUG(Main_filename, 0); + DEBUG(Spare_filename, 0); + Button_Page(); + // no break ! proceed with the other file now + case 1: + Button_Reload(); + Resolution_in_command_line = 0; + default: + break; } }