From 897424ad683d5ca19d8fc50f4d664b0a68d2289f Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Fri, 30 Jan 2015 10:01:31 +0100 Subject: [PATCH hurd 01/10] daemons/console-run: add `--console' argument to select the device Parse the command line arguments, and honor `--console' to select the console device. * daemons/console-run.c (console_device, cmd_argv, doc, args_doc, argp): New variables. (parse_opt): New function. (main): Use `argp_parse', adapt accordingly. (open_console): Use `console_device' instead of hardcoded value. --- daemons/console-run.c | 58 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/daemons/console-run.c b/daemons/console-run.c index e1bfe64..a76ec8a 100644 --- a/daemons/console-run.c +++ b/daemons/console-run.c @@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ +#include #include #include #include @@ -26,7 +27,51 @@ #include #include #include + +char *console_device = _PATH_CONSOLE; +char **cmd_argv; + +static const struct argp_option options[] = +{ + {"console", 'c', "DEVICE", 0, "use this device ["_PATH_CONSOLE"]"}, + {0} +}; + +static error_t +parse_opt (int opt, char *arg, struct argp_state *state) +{ + switch (opt) + { + case 'c': + console_device = arg; + break; + + case ARGP_KEY_NO_ARGS: + argp_usage (state); + return EINVAL; + + case ARGP_KEY_ARG: + cmd_argv = &state->argv[state->next - 1]; + state->next = state->argc; + break; + + default: + return ARGP_ERR_UNKNOWN; + case ARGP_KEY_INIT: + case ARGP_KEY_SUCCESS: + case ARGP_KEY_ERROR: + break; + } + return 0; +} +static const char doc[] = + "Open a terminal and run the given program"; +static const char args_doc[] = "COMMAND [ARG...]"; + +static const struct argp argp = +{ options, parse_opt, args_doc, doc }; + static mach_port_t get_console () { @@ -60,20 +105,19 @@ main (int argc, char **argv) if (!stderr) _exit (127); - if (argc < 2) - error (1, 0, "Usage: %s PROGRAM [ARG...]", program_invocation_short_name); + argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, 0); /* Check whether runsystem exists before opening a console for it. */ - runsystem = file_name_lookup (argv[1], O_RDONLY, 0); + runsystem = file_name_lookup (cmd_argv[0], O_RDONLY, 0); if (runsystem == MACH_PORT_NULL) - error (127, errno, "cannot open file `%s' for execution", argv[1]); + error (127, errno, "cannot open file `%s' for execution", cmd_argv[0]); mach_port_deallocate (mach_task_self (), runsystem); if (open_console (&consname)) setenv ("FALLBACK_CONSOLE", consname, 1); - execv (argv[1], &argv[1]); - error (5, errno, "cannot execute %s", argv[1]); + execv (cmd_argv[0], &cmd_argv[0]); + error (5, errno, "cannot execute %s", cmd_argv[0]); /* NOTREACHED */ return 127; } @@ -97,7 +141,7 @@ open_console (char **namep) int fd; int fallback; - termname = _PATH_CONSOLE; + termname = console_device; term = file_name_lookup (termname, O_RDWR, 0); if (term != MACH_PORT_NULL) err = io_stat (term, &st); -- 2.1.4