Hi, Here is a patch that adds support for argp argument parsing from console client plugins. I've also changed the existing plugins to do so. And as a bonus I've done task #2479: "Generic Speaker Bell Style Option".
I've also fixed all warnings and changed a function call in ncursesw. This fixes a small (but not serious) mistake I introduced in a previous patch. I've tested this already, and for some magic reason it works even without Marcus' patch applied. But still I don't advise using it because argp is not reentrant yet. There is another small fix to set the current locale. If this is not done some people could experience some problems with some console client plugins (Like locale dependent capitalization). Thanks, Marco 2004-03-06 Marco Gerards <[EMAIL PROTECTED]> * bdf.c (bdf_read): Silence a gcc warning by casting the strlen return value to an int. * console.c: Include <locale.h>. (main): Set the locale. Make sure the arguments are parsed in order. Fix proposed by Göran Weinholt <[EMAIL PROTECTED]>. * driver.c (driver_fini): Change `i' from int to unsigned int to silence a gcc warning. (driver_start): Likewise. (ADD_REMOVE_COMPONENT): Likewise. (driver_add): Cast the return value of driver_len_list to an int to silence a gcc warning. Set up argc and argv so it can be used by argp. * driver.h (struct driver_ops): Remove the *NEXT argument from the interface of the member `init'. * generic-speaker.c: Include <string.h>. Include <argp.h>. (struct melody): New member `name'. Every melody was given a name. (doc): New variable. (options): Likewise. (argp): Likewise. (parse_opt): New function. (generic_speaker_init): Parse the arguments using argp. (generic_speaker_play_melody): Use a cast to silence a gcc warning. * ncursesw.c: Changed all calls to the function `ncurses_refresh' to `refresh_screen'. (input_loop): Change `i' from int to unsigned int to silence a gcc warning. (ncursesw_set_cursor_pos): Use a cast to silence a gcc warning. * vga.c (current_width): Changed type to unsigned int to silence a gcc warning. (current_heigh): Likewise. (struct vga_display): Changed the type of the members `width' and `height' to unsigned int to silence a gcc warning. (doc): New variable. (options): Likewise. (argp): Likewise. (parse_opt): New function. (parse_startup_args): Function removed. (vga_display_init): Use argp now. (vga_display_set_cursor_pos): Use a cast to silence a gcc warning. (vga_display_change_font): Disabled the unused code for now. Index: bdf.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/bdf.c,v retrieving revision 1.1 diff -u -p -r1.1 bdf.c --- bdf.c 17 Sep 2002 12:26:10 -0000 1.1 +++ bdf.c 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* bdf.c - Parser for the Adobe Glyph Bitmap Distribution Format (BDF). - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann <[EMAIL PROTECTED]>. This file is part of the GNU Hurd. @@ -553,7 +553,7 @@ bdf_read (FILE *filep, bdf_font_t *font, } else { - if (strlen (line) != 2 * parser.glyph_bwidth) + if ((int) strlen (line) != 2 * parser.glyph_bwidth) err = BDF_SYNTAX_ERROR; else if (parser.glyph_blines == parser.glyph_bheight) err = BDF_COUNT_MISMATCH; Index: console.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/console.c,v retrieving revision 1.3 diff -u -p -r1.3 console.c --- console.c 15 Aug 2003 21:07:31 -0000 1.3 +++ console.c 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* console.c -- A pluggable console client. - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This program is free software; you can redistribute it and/or @@ -24,6 +24,7 @@ #include <wchar.h> #include <error.h> #include <assert.h> +#include <locale.h> #include <cthreads.h> @@ -452,10 +453,11 @@ main (int argc, char *argv[]) error_t err; char *errname; + setlocale (LC_ALL, ""); driver_init (); /* Parse our command line. This shouldn't ever return an error. */ - argp_parse (&startup_argp, argc, argv, 0, 0, 0); + argp_parse (&startup_argp, argc, argv, ARGP_IN_ORDER, 0, 0); err = driver_start (&errname); if (err) Index: driver.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/driver.c,v retrieving revision 1.1 diff -u -p -r1.1 driver.c --- driver.c 17 Sep 2002 12:26:10 -0000 1.1 +++ driver.c 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* driver.c - The console client driver code. - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This file is part of the GNU Hurd. @@ -65,7 +65,7 @@ driver_init (void) error_t driver_fini (void) { - int i; + unsigned int i; mutex_lock (&driver_list_lock); for (i = 0; i < driver_list_len; i++) @@ -99,9 +99,10 @@ error_t driver_add (const char *const na int i; char *dir = driver_path; int defpath = 0; + char *opt_backup; mutex_lock (&driver_list_lock); - for (i = 0; i < driver_list_len; i++) + for (i = 0; i < (int) driver_list_len; i++) if (driver_list[i].name && !strcmp (driver_list[i].name, name)) { mutex_unlock (&driver_list_lock); @@ -213,8 +214,28 @@ error_t driver_add (const char *const na return ENOMEM; } + /* Go to the currently used argument. */ + argv += *next; + argc -= *next; + + for (i = 0; i < argc - 1; i++) + { + if (! strcmp (argv[i], "-d")) + break; + if (! strncmp (argv[i], "--driver", 8)) + break; + } + argc = i; + *next += argc; + + /* Use the driver name as program name. */ + argv--; + opt_backup = argv[0]; + argv[0] = (char *) name; + /* If we will start the driver, the init function must not exit. */ - err = (*drv->ops->init) (&drv->handle, start, argc, argv, next); + err = (*drv->ops->init) (&drv->handle, start, argc + 1, argv); + argv[0] = opt_backup; if (!err && start && drv->ops->start) err = (*drv->ops->start) (drv->handle); if (err) @@ -242,7 +263,7 @@ error_t driver_start (char **name) { error_t err = 0; - int i; + unsigned int i; mutex_lock (&driver_list_lock); for (i = 0; i < driver_list_len; i++) @@ -268,7 +289,7 @@ driver_start (char **name) error_t driver_remove (const char *const name) { error_t err; - int i; + unsigned int i; mutex_lock (&driver_list_lock); for (i = 0; i < driver_list_len; i++) @@ -330,7 +351,7 @@ driver_add_##component (component##_ops_ error_t \ driver_remove_##component (component##_ops_t ops, void *handle) \ { \ - int i; \ + unsigned int i; \ \ mutex_lock (&component##_list_lock); \ for (i = 0; i < component##_list_len; i++) \ Index: driver.h =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/driver.h,v retrieving revision 1.1 diff -u -p -r1.1 driver.h --- driver.h 17 Sep 2002 12:26:10 -0000 1.1 +++ driver.h 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* driver.h - The interface to and for a console client driver. - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This file is part of the GNU Hurd. @@ -136,8 +136,7 @@ struct driver_ops success and an appropriate error value otherwise. If it returns success, the function start() will be called next, otherwise nothing happens. */ - error_t (*init) (void **handle, int no_exit, - int argc, char *argv[], int *next); + error_t (*init) (void **handle, int no_exit, int argc, char *argv[]); /* Activate the driver instance. This function should load all the display, input and bell river components for this driver Index: generic-speaker.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/generic-speaker.c,v retrieving revision 1.2 diff -u -p -r1.2 generic-speaker.c --- generic-speaker.c 18 Nov 2002 07:35:47 -0000 1.2 +++ generic-speaker.c 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* generic-speaker.c - The simple speaker bell driver. - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This file is part of the GNU Hurd. @@ -20,6 +20,8 @@ #include <errno.h> #include <sys/io.h> +#include <string.h> +#include <argp.h> #include <cthreads.h> @@ -202,31 +204,32 @@ struct note struct melody { + char *name; int measure; struct note *next; struct note note[]; }; static struct melody beep1 = - { 160, NULL, { + { "classic", 160, NULL, { /* The classical bell. */ { T_a_1, 4 }, { T_FINE, 0 } } }; static struct melody beep2 = - { 60, NULL, { + { "linux", 60, NULL, { /* The Linux bell. */ { 750, 1 }, { T_FINE, 0 } } }; static struct melody beep3 = - { 160, NULL, { + { "alarm", 160, NULL, { /* The tritonus. Quite alarming. */ { T_f_2, 2 }, { T_b_1, 4 }, { T_FINE, 0 } } }; static struct melody beep4 = - { 160, NULL, { + { "cmajor", 160, NULL, { /* C-Major chord. A bit playful. */ { T_c_2, 2 }, { T_e_2, 2 }, { T_g_2, 2 }, { T_FINE, 0 } @@ -237,7 +240,7 @@ static int active_beep; #if QUAERENDO_INVENIETIS struct melody tune1 = - { 160, NULL, { + { "fsf-song", 160, NULL, { /* The Free Software Song. Measure: 7/4. */ { T_d_2, 16 }, { T_c_2, 8 }, { T_b_1, 16 }, { T_a_1, 16 }, { T_b_1, 16 }, { T_c_2, 8 }, { T_b_1, 8 }, { T_a_1, 8 }, { T_g_1, 16 }, @@ -254,7 +257,7 @@ struct melody tune1 = } }; struct melody tune2 = - { 160, NULL, { + { "ifeelpretty", 160, NULL, { /* I feel pretty. Measure: 3/4. By Leonard Bernstein. */ { T_c_1, 8 }, { T_e_1, 8 }, { T_f_1, 4 }, { T_a_1, 20 }, @@ -273,7 +276,7 @@ struct melody tune2 = } }; struct melody tune3 = - { 120, NULL, { + { "Summertime", 120, NULL, { /* Summertime. Measure: 4/4. By George & Ira Gershwin. */ { T_b_1, 8 }, { T_g_1, 8 }, { T_b_1, 36 }, { T_REST, 4 }, { T_a_1, 6 }, { T_g_1, 2 }, @@ -299,7 +302,7 @@ struct melody tune3 = } }; struct melody tune4 = - { 250, NULL, { + { "indianajones", 250, NULL, { /* Indiana Jones Theme. Measure: 4/4. By John Williams. */ { T_e_1, 4 }, { T_REST, 8 }, { T_f_1, 4 }, { T_g_1, 4 }, { T_REST, 4 }, { T_c_2, 24 }, @@ -342,7 +345,7 @@ beep_on (short pitch) if (pitch < 20) pitch = 20; - else if (pitch > 32767) + else if (pitch > 32766) pitch = 32767; counter = PIT_FREQUENCY / pitch; @@ -386,11 +389,64 @@ next_note (void *handle) } +static const char doc[] = "Generic speaker driver"; + +static const struct argp_option options[] = + { + {"bell-style", 'b', "BELL", 0, "Use one of the bells: " + "clasic, linux, alarm or cmajor"}, + { 0 } + }; + +static error_t +parse_opt (int key, char *arg, struct argp_state *state) +{ + switch (key) + { + case 'b': + { + int i; + int found = 0; + + for (i = 0; i < (int) sizeof (*beep); i++) + { + if (! strcasecmp (beep[i]->name, arg)) + { + found = 1; + break; + } + } + + if (! found) + argp_usage (state); + + active_beep = i; + break; + } + + case ARGP_KEY_END: + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +static struct argp argp = {options, parse_opt, 0, doc}; + /* Initialization of the generic speaker driver. */ static error_t -generic_speaker_init (void **handle, int no_exit, - int argc, char *argv[], int *next) +generic_speaker_init (void **handle, int no_exit, int argc, char *argv[]) { + error_t err; + + /* Parse the arguments. */ + err = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, 0); + if (err) + return err; + timer_clear (&generic_speaker_timer); generic_speaker_timer.fnc = &next_note; @@ -454,7 +510,7 @@ generic_speaker_beep (void *handle) static void generic_speaker_play_melody (void *handle, int key) { - if (key < 0 || key >= sizeof (tune) / sizeof (tune[0])) + if (key < 0 || key >= (int) (sizeof (tune) / sizeof (tune[0]))) return; if (timer_remove (&generic_speaker_timer)) Index: ncursesw.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/ncursesw.c,v retrieving revision 1.3 diff -u -p -r1.3 ncursesw.c --- ncursesw.c 15 Aug 2003 21:07:31 -0000 1.3 +++ ncursesw.c 5 Mar 2004 19:49:44 -0000 @@ -1,5 +1,5 @@ /* ncursesw.c - The ncursesw console driver. - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This program is free software; you can redistribute it and/or @@ -310,7 +310,7 @@ input_loop (any_t unused) mutex_lock (&ncurses_lock); while ((ret = wgetch (conspad)) != ERR) { - int i; + unsigned int i; int found; if (w_escaped) @@ -360,7 +360,7 @@ input_loop (any_t unused) if (padx < current_width - COLS) { padx++; - ncurses_refresh (); + refresh_screen (); } break; case 'i': @@ -507,36 +507,36 @@ ncursesw_set_cursor_pos (void *handle, u if (autoscroll) { /* Autoscroll to the right. */ - if (col > COLS + padx) + if ((int) col > COLS + padx) { padx += COLS / 2; if (padx > COLS + current_width) padx = current_width - COLS; - ncurses_refresh (); + refresh_screen (); } /* Autoscroll to the left. */ - else if (col < padx) + else if ((int) col < padx) { padx -= COLS / 2; if (padx < 0) padx = 0; - ncurses_refresh (); + refresh_screen (); } /* Autoscroll down. */ - if (row > LINES + pady) + if ((int) row > LINES + pady) { pady += LINES / 2; if (pady > LINES + current_height) pady = current_height - LINES; - ncurses_refresh (); + refresh_screen (); } /* Autoscroll up. */ - else if (row < pady) + else if ((int) row < pady) { pady -= LINES / 2; if (pady < 0) pady = 0; - ncurses_refresh (); + refresh_screen (); } } @@ -619,8 +619,7 @@ ncursesw_beep (void *handle) static error_t -ncursesw_driver_init (void **handle, int no_exit, - int argc, char *argv[], int *next) +ncursesw_driver_init (void **handle, int no_exit, int argc, char *argv[]) { mutex_init (&ncurses_lock); return 0; Index: pc-kbd.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/pc-kbd.c,v retrieving revision 1.9 diff -u -p -r1.9 pc-kbd.c --- pc-kbd.c 2 Feb 2004 22:08:14 -0000 1.9 +++ pc-kbd.c 5 Mar 2004 19:49:44 -0000 @@ -1090,7 +1090,7 @@ input_loop (any_t unused) /* Initialize the PC keyboard driver. */ static error_t -pc_kbd_init (void **handle, int no_exit, int argc, char *argv[], int *next) +pc_kbd_init (void **handle, int no_exit, int argc, char *argv[]) { return 0; } Index: vga.c =================================================================== RCS file: /cvsroot/hurd/hurd/console-client/vga.c,v retrieving revision 1.3 diff -u -p -r1.3 vga.c --- vga.c 15 Aug 2003 21:07:31 -0000 1.3 +++ vga.c 5 Mar 2004 19:49:45 -0000 @@ -1,5 +1,5 @@ /* vga.c - The VGA device display driver. - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. Written by Marcus Brinkmann. This file is part of the GNU Hurd. @@ -75,8 +75,8 @@ static struct mutex vga_display_lock; static struct display_ops vga_display_ops; /* The current width and height the ncursesw driver is using. */ -static int current_width; -static int current_height; +static unsigned int current_width; +static unsigned int current_height; /* The cursor state to restore the state to. */ static int cursor_state; @@ -102,8 +102,8 @@ struct vga_display /* The color palette. */ dynacolor_t dc; - int width; - int height; + unsigned int width; + unsigned int height; /* Current attribute. */ int cur_conchar_attr_init; @@ -152,65 +152,72 @@ vga_display_flash (void *handle) } -/* Parse arguments at startup. */ + +static const char doc[] = "VGA Driver"; + +static const struct argp_option options[] = + { + {"font", 'f', "FONT", 0, "Use FONT for normal text"}, + {"font-italic", 'i', "FONT", 0, "Use FONT for italic text"}, + {"font-bold", 'b', "FONT", 0, "Use FONT for bold text"}, + {"font-bold-italic",'a', "FONT", 0, "Use FONT for text that is both bold and italic"}, + {"max-colors", 'm', 0 , 0, "Prefer a lot of colors above a lot of glyphs"}, + {"max-glyphs", 'g', 0 , 0, "Prefer a lot of glyphs above a lot of colors"}, + { 0 } + }; + static error_t -parse_startup_args (int no_exit, int argc, char *argv[], int *next) +parse_opt (int key, char *arg, struct argp_state *state) { -#define PARSE_FONT_OPT(x,y) \ - do { \ - if (!strcmp (argv[*next], x)) \ - { \ - (*next)++; \ - if (*next == argc) \ - { \ - if (no_exit) \ - return EINVAL; \ - else \ - error (1, 0, "option " x \ - " requires an argument"); \ - } \ - if (vga_display_##y) \ - free (vga_display_##y); \ - vga_display_##y = strdup (argv[*next]); \ - if (!vga_display_##y) \ - { \ - if (no_exit) \ - return errno; \ - else \ - error (1, errno, "malloc failed"); \ - } \ - (*next)++; \ - continue; \ - } \ - } while (0) - -#define PARSE_FONT_OPT_NOARGS(x,y,z) \ - { \ - if (!strcmp (argv[*next], x)) \ - { \ - (*next)++; \ - vga_display_##y = z; \ - } \ - } - - while (*next < argc) - { - PARSE_FONT_OPT ("--font", font); - PARSE_FONT_OPT ("--font-italic", font_italic); - PARSE_FONT_OPT ("--font-bold", font_bold); - PARSE_FONT_OPT ("--font-bold-italic", font_bold_italic); - PARSE_FONT_OPT_NOARGS ("--max-colors", max_glyphs, 1); - PARSE_FONT_OPT_NOARGS ("--max-glyphs", max_glyphs, 0); + switch (key) + { + case 'f': + vga_display_font = strdup (arg); + if (! vga_display_font) + return 0; + break; + + case 'i': + vga_display_font_italic = strdup (arg); + if (! vga_display_font_italic) + return 0; + break; + + case 'b': + vga_display_font_bold = strdup (arg); + if (! vga_display_font_bold) + return 0; + break; + case 'a': + vga_display_font_bold_italic = strdup (arg); + if (! vga_display_font_bold_italic) + return 0; + break; + + case 'm': + vga_display_max_glyphs = 1; + break; + + case 'g': + vga_display_max_glyphs = 0; + break; + case ARGP_KEY_END: break; + + default: + return ARGP_ERR_UNKNOWN; } + return 0; } +static struct argp argp = {options, parse_opt, 0, doc}; + /* Initialize the subsystem. */ static error_t -vga_display_init (void **handle, int no_exit, int argc, char *argv[], int *next) +vga_display_init (void **handle, int no_exit, int argc, char *argv[]) { error_t err; struct vga_display *disp; @@ -221,7 +228,7 @@ vga_display_init (void **handle, int no_ vga_display_timer.fnc = &vga_display_flash_off; /* Parse the arguments. */ - err = parse_startup_args (no_exit, argc, argv, next); + err = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, 0); if (err) return err; @@ -360,7 +367,7 @@ vga_display_set_cursor_pos (void *handle /* Make sure the cursor can only be moved to a position on te physical screen. */ - if (col < disp->width && row < disp->height) + if (col < (unsigned int) disp->width && row < disp->height) { vga_set_cursor_pos (pos); if (cursor_hidden) @@ -441,7 +448,7 @@ vga_display_scroll (void *handle, int de return 0; } - +#if 0 /* Change the font on the console CONSOLE to font. The old font will not be accessed by the vga console subsystem anymore after this call completed. */ @@ -452,6 +459,7 @@ vga_display_change_font (void *handle, b dynafont_change_font (disp->df, font); } +#endif static inline char _______________________________________________ Bug-hurd mailing list [EMAIL PROTECTED] http://mail.gnu.org/mailman/listinfo/bug-hurd