Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / command-line.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "command-line.h"
19 #include <getopt.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include "util.h"
23 #include "vlog.h"
24
25 VLOG_DEFINE_THIS_MODULE(command_line);
26
27 /* Given the GNU-style long options in 'options', returns a string that may be
28  * passed to getopt() with the corresponding short options.  The caller is
29  * responsible for freeing the string. */
30 char *
31 long_options_to_short_options(const struct option options[])
32 {
33     char short_options[UCHAR_MAX * 3 + 1];
34     char *p = short_options;
35
36     for (; options->name; options++) {
37         const struct option *o = options;
38         if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
39             *p++ = o->val;
40             if (o->has_arg == required_argument) {
41                 *p++ = ':';
42             } else if (o->has_arg == optional_argument) {
43                 *p++ = ':';
44                 *p++ = ':';
45             }
46         }
47     }
48     *p = '\0';
49
50     return xstrdup(short_options);
51 }
52
53 /* Runs the command designated by argv[0] within the command table specified by
54  * 'commands', which must be terminated by a command whose 'name' member is a
55  * null pointer.
56  *
57  * Command-line options should be stripped off, so that a typical invocation
58  * looks like "run_command(argc - optind, argv + optind, my_commands);". */
59 void
60 run_command(int argc, char *argv[], const struct command commands[])
61 {
62     const struct command *p;
63
64     if (argc < 1) {
65         ovs_fatal(0, "missing command name; use --help for help");
66     }
67
68     for (p = commands; p->name != NULL; p++) {
69         if (!strcmp(p->name, argv[0])) {
70             int n_arg = argc - 1;
71             if (n_arg < p->min_args) {
72                 VLOG_FATAL( "'%s' command requires at least %d arguments",
73                             p->name, p->min_args);
74             } else if (n_arg > p->max_args) {
75                 VLOG_FATAL("'%s' command takes at most %d arguments",
76                            p->name, p->max_args);
77             } else {
78                 p->handler(argc, argv);
79                 if (ferror(stdout)) {
80                     VLOG_FATAL("write to stdout failed");
81                 }
82                 if (ferror(stderr)) {
83                     VLOG_FATAL("write to stderr failed");
84                 }
85                 return;
86             }
87         }
88     }
89
90     VLOG_FATAL("unknown command '%s'; use --help for help", argv[0]);
91 }
92 \f
93 /* Process title. */
94
95 #ifdef __linux__
96 static char *argv_start;       /* Start of command-line arguments in memory. */
97 static size_t argv_size;       /* Number of bytes of command-line arguments. */
98 static char *saved_proctitle;  /* Saved command-line arguments. */
99
100 /* Prepares the process so that proctitle_set() can later succeed.
101  *
102  * This modifies the argv[] array so that it no longer points into the memory
103  * that it originally does.  Later, proctitle_set() might overwrite that
104  * memory.  That means that this function should be called before anything else
105  * that accesses the process's argv[] array.  Ideally, it should be called
106  * before anything else, period, at the very beginning of program
107  * execution.  */
108 void
109 proctitle_init(int argc, char **argv)
110 {
111     int i;
112
113     if (!argc || !argv[0]) {
114         /* This situation should never occur, but... */
115         return;
116     }
117
118     /* Specialized version of first loop iteration below. */
119     argv_start = argv[0];
120     argv_size = strlen(argv[0]) + 1;
121     argv[0] = xstrdup(argv[0]);
122
123     for (i = 1; i < argc; i++) {
124         size_t size = strlen(argv[i]) + 1;
125
126         /* Add (argv[i], strlen(argv[i])+1) to (argv_start, argv_size). */
127         if (argv[i] + size == argv_start) {
128             /* Arguments grow downward in memory. */
129             argv_start -= size;
130             argv_size += size;
131         } else if (argv[i] == argv_start + argv_size) {
132             /* Arguments grow upward in memory. */
133             argv_size += size;
134         } else {
135             /* Arguments not contiguous.  (Is this really Linux?) */
136         }
137
138         /* Copy out the old argument so we can reuse the space. */
139         argv[i] = xstrdup(argv[i]);
140     }
141 }
142
143 /* Changes the name of the process, as shown by "ps", to 'format', which is
144  * formatted as if by printf(). */
145 void
146 proctitle_set(const char *format, ...)
147 {
148     va_list args;
149     int n;
150
151     if (!argv_start || argv_size < 8) {
152         return;
153     }
154
155     if (!saved_proctitle) {
156         saved_proctitle = xmemdup(argv_start, argv_size);
157     }
158
159     va_start(args, format);
160     n = vsnprintf(argv_start, argv_size, format, args);
161     if (n >= argv_size) {
162         /* The name is too long, so add an ellipsis at the end. */
163         strcpy(&argv_start[argv_size - 4], "...");
164     } else {
165         /* Fill the extra space with null bytes, so that trailing bytes don't
166          * show up in the command line. */
167         memset(&argv_start[n], '\0', argv_size - n);
168     }
169     va_end(args);
170 }
171
172 /* Restores the process's original command line, as seen by "ps". */
173 void
174 proctitle_restore(void)
175 {
176     if (saved_proctitle) {
177         memcpy(argv_start, saved_proctitle, argv_size);
178         free(saved_proctitle);
179         saved_proctitle = NULL;
180     }
181 }
182 #else  /* !__linux__ */
183 /* Stubs that don't do anything on non-Linux systems. */
184
185 void
186 proctitle_init(int argc OVS_UNUSED, char **argv OVS_UNUSED)
187 {
188 }
189
190 void
191 proctitle_set(const char *format OVS_UNUSED, ...)
192 {
193 }
194
195 void
196 proctitle_restore(void)
197 {
198 }
199 #endif  /* !__linux__ */