treewide: Remove trailing whitespace
[sliver-openvswitch.git] / lib / command-line.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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
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. */
27 char *
28 long_options_to_short_options(const struct option options[])
29 {
30     char short_options[UCHAR_MAX * 3 + 1];
31     char *p = short_options;
32
33     for (; options->name; options++) {
34         const struct option *o = options;
35         if (o->flag == NULL && o->val > 0 && o->val <= UCHAR_MAX) {
36             *p++ = o->val;
37             if (o->has_arg == required_argument) {
38                 *p++ = ':';
39             } else if (o->has_arg == optional_argument) {
40                 *p++ = ':';
41                 *p++ = ':';
42             }
43         }
44     }
45     *p = '\0';
46
47     return xstrdup(short_options);
48 }
49
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
52  * null pointer.
53  *
54  * Command-line options should be stripped off, so that a typical invocation
55  * looks like "run_command(argc - optind, argv + optind, my_commands);". */
56 void
57 run_command(int argc, char *argv[], const struct command commands[])
58 {
59     const struct command *p;
60
61     if (argc < 1) {
62         ovs_fatal(0, "missing command name; use --help for help");
63     }
64
65     for (p = commands; p->name != NULL; p++) {
66         if (!strcmp(p->name, argv[0])) {
67             int n_arg = argc - 1;
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);
74             } else {
75                 p->handler(argc, argv);
76                 if (ferror(stdout)) {
77                     ovs_fatal(0, "write to stdout failed");
78                 }
79                 if (ferror(stderr)) {
80                     ovs_fatal(0, "write to stderr failed");
81                 }
82                 return;
83             }
84         }
85     }
86
87     ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
88 }
89 \f
90 /* Process title. */
91
92 #ifdef __linux__
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. */
96
97 /* Prepares the process so that proctitle_set() can later succeed.
98  *
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
104  * execution.  */
105 void
106 proctitle_init(int argc, char **argv)
107 {
108     int i;
109
110     if (!argc || !argv[0]) {
111         /* This situation should never occur, but... */
112         return;
113     }
114
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]);
119
120     for (i = 1; i < argc; i++) {
121         size_t size = strlen(argv[i]) + 1;
122
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. */
126             argv_start -= size;
127             argv_size += size;
128         } else if (argv[i] == argv_start + argv_size) {
129             /* Arguments grow upward in memory. */
130             argv_size += size;
131         } else {
132             /* Arguments not contiguous.  (Is this really Linux?) */
133         }
134
135         /* Copy out the old argument so we can reuse the space. */
136         argv[i] = xstrdup(argv[i]);
137     }
138 }
139
140 /* Changes the name of the process, as shown by "ps", to 'format', which is
141  * formatted as if by printf(). */
142 void
143 proctitle_set(const char *format, ...)
144 {
145     va_list args;
146     int n;
147
148     if (!argv_start || argv_size < 8) {
149         return;
150     }
151
152     if (!saved_proctitle) {
153         saved_proctitle = xmemdup(argv_start, argv_size);
154     }
155
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], "...");
161     } else {
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);
165     }
166     va_end(args);
167 }
168
169 /* Restores the process's original command line, as seen by "ps". */
170 void
171 proctitle_restore(void)
172 {
173     if (saved_proctitle) {
174         memcpy(argv_start, saved_proctitle, argv_size);
175         free(saved_proctitle);
176         saved_proctitle = NULL;
177     }
178 }
179 #else  /* !__linux__ */
180 /* Stubs that don't do anything on non-Linux systems. */
181
182 void
183 proctitle_init(int argc OVS_UNUSED, char **argv OVS_UNUSED)
184 {
185 }
186
187 void
188 proctitle_set(const char *format OVS_UNUSED, ...)
189 {
190 }
191
192 void
193 proctitle_restore(void)
194 {
195 }
196 #endif  /* !__linux__ */