]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/command-line.c
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "command-line.h"
24 /* Given the GNU-style long options in 'options', returns a string that may be
25 * passed to getopt() with the corresponding short options. The caller is
26 * responsible for freeing the string. */
28 long_options_to_short_options(const struct option options
[])
30 char short_options
[UCHAR_MAX
* 3 + 1];
31 char *p
= short_options
;
33 for (; options
->name
; options
++) {
34 const struct option
*o
= options
;
35 if (o
->flag
== NULL
&& o
->val
> 0 && o
->val
<= UCHAR_MAX
) {
37 if (o
->has_arg
== required_argument
) {
39 } else if (o
->has_arg
== optional_argument
) {
47 return xstrdup(short_options
);
50 /* Runs the command designated by argv[0] within the command table specified by
51 * 'commands', which must be terminated by a command whose 'name' member is a
54 * Command-line options should be stripped off, so that a typical invocation
55 * looks like "run_command(argc - optind, argv + optind, my_commands);". */
57 run_command(int argc
, char *argv
[], const struct command commands
[])
59 const struct command
*p
;
62 ovs_fatal(0, "missing command name; use --help for help");
65 for (p
= commands
; p
->name
!= NULL
; p
++) {
66 if (!strcmp(p
->name
, argv
[0])) {
68 if (n_arg
< p
->min_args
) {
69 ovs_fatal(0, "'%s' command requires at least %d arguments",
70 p
->name
, p
->min_args
);
71 } else if (n_arg
> p
->max_args
) {
72 ovs_fatal(0, "'%s' command takes at most %d arguments",
73 p
->name
, p
->max_args
);
75 p
->handler(argc
, argv
);
77 ovs_fatal(0, "write to stdout failed");
80 ovs_fatal(0, "write to stderr failed");
87 ovs_fatal(0, "unknown command '%s'; use --help for help", argv
[0]);
93 static char *argv_start
; /* Start of command-line arguments in memory. */
94 static size_t argv_size
; /* Number of bytes of command-line arguments. */
95 static char *saved_proctitle
; /* Saved command-line arguments. */
97 /* Prepares the process so that proctitle_set() can later succeed.
99 * This modifies the argv[] array so that it no longer points into the memory
100 * that it originally does. Later, proctitle_set() might overwrite that
101 * memory. That means that this function should be called before anything else
102 * that accesses the process's argv[] array. Ideally, it should be called
103 * before anything else, period, at the very beginning of program
106 proctitle_init(int argc
, char **argv
)
110 if (!argc
|| !argv
[0]) {
111 /* This situation should never occur, but... */
115 /* Specialized version of first loop iteration below. */
116 argv_start
= argv
[0];
117 argv_size
= strlen(argv
[0]) + 1;
118 argv
[0] = xstrdup(argv
[0]);
120 for (i
= 1; i
< argc
; i
++) {
121 size_t size
= strlen(argv
[i
]) + 1;
123 /* Add (argv[i], strlen(argv[i])+1) to (argv_start, argv_size). */
124 if (argv
[i
] + size
== argv_start
) {
125 /* Arguments grow downward in memory. */
128 } else if (argv
[i
] == argv_start
+ argv_size
) {
129 /* Arguments grow upward in memory. */
132 /* Arguments not contiguous. (Is this really Linux?) */
135 /* Copy out the old argument so we can reuse the space. */
136 argv
[i
] = xstrdup(argv
[i
]);
140 /* Changes the name of the process, as shown by "ps", to 'format', which is
141 * formatted as if by printf(). */
143 proctitle_set(const char *format
, ...)
148 if (!argv_start
|| argv_size
< 8) {
152 if (!saved_proctitle
) {
153 saved_proctitle
= xmemdup(argv_start
, argv_size
);
156 va_start(args
, format
);
157 n
= vsnprintf(argv_start
, argv_size
, format
, args
);
158 if (n
>= argv_size
) {
159 /* The name is too long, so add an ellipsis at the end. */
160 strcpy(&argv_start
[argv_size
- 4], "...");
162 /* Fill the extra space with null bytes, so that trailing bytes don't
163 * show up in the command line. */
164 memset(&argv_start
[n
], '\0', argv_size
- n
);
169 /* Restores the process's original command line, as seen by "ps". */
171 proctitle_restore(void)
173 if (saved_proctitle
) {
174 memcpy(argv_start
, saved_proctitle
, argv_size
);
175 free(saved_proctitle
);
176 saved_proctitle
= NULL
;
179 #else /* !__linux__ */
180 /* Stubs that don't do anything on non-Linux systems. */
183 proctitle_init(int argc OVS_UNUSED
, char **argv OVS_UNUSED
)
188 proctitle_set(const char *format OVS_UNUSED
, ...)
193 proctitle_restore(void)
196 #endif /* !__linux__ */