1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
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 <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -26,7 +27,51 @@
#include <hurd.h>
#include <hurd/fshelp.h>
#include <device/device.h>
+
+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
|