po: Update German man pages translation
[dpkg.git] / lib / dpkg / options.c
blob4244b7decbee0203d8e04344e27f1898f88b90b3
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * options.c - option parsing functions
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000,2002 Wichert Akkerman <wichert@deephackmode.org>
7 * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
9 * This is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #include <config.h>
24 #include <compat.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
33 #include <dpkg/i18n.h>
34 #include <dpkg/c-ctype.h>
35 #include <dpkg/dpkg.h>
36 #include <dpkg/string.h>
37 #include <dpkg/options.h>
39 static const char *printforhelp;
41 void
42 badusage(const char *fmt, ...)
44 char *buf = NULL;
45 va_list args;
47 va_start(args, fmt);
48 m_vasprintf(&buf, fmt, args);
49 va_end(args);
51 ohshit("%s\n\n%s", buf, gettext(printforhelp));
54 static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(3)
55 config_error(const char *file_name, int line_num, const char *fmt, ...)
57 char *buf = NULL;
58 va_list args;
60 va_start(args, fmt);
61 m_vasprintf(&buf, fmt, args);
62 va_end(args);
64 ohshit(_("configuration error: %s:%d: %s"), file_name, line_num, buf);
67 static void
68 dpkg_options_load_file(const char *fn, const struct cmdinfo *cmdinfos)
70 FILE *file;
71 int line_num = 0;
72 char linebuf[MAX_CONFIG_LINE];
74 file= fopen(fn, "r");
75 if (!file) {
76 if (errno==ENOENT)
77 return;
78 warning(_("failed to open configuration file '%.255s' for reading: %s"),
79 fn, strerror(errno));
80 return;
83 while (fgets(linebuf, sizeof(linebuf), file)) {
84 char *opt;
85 const struct cmdinfo *cip;
87 line_num++;
89 str_rtrim_spaces(linebuf, linebuf + strlen(linebuf));
91 if ((linebuf[0] == '#') || (linebuf[0] == '\0'))
92 continue;
93 for (opt = linebuf; c_isalnum(*opt) || *opt == '-'; opt++) ;
94 if (*opt == '\0')
95 opt=NULL;
96 else {
97 *opt++ = '\0';
98 if (*opt=='=') opt++;
99 while (c_isspace(*opt))
100 opt++;
102 opt = str_strip_quotes(opt);
103 if (opt == NULL)
104 config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
107 for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
108 int l;
110 if (!cip->olong) continue;
111 if (strcmp(cip->olong, linebuf) == 0)
112 break;
113 l=strlen(cip->olong);
114 if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
115 !opt && strncmp(linebuf, cip->olong, l) == 0) {
116 opt=linebuf+l+1;
117 break;
121 if (!cip->olong)
122 config_error(fn, line_num, _("unknown option '%s'"), linebuf);
124 if (cip->takesvalue) {
125 if (!opt)
126 config_error(fn, line_num, _("'%s' needs a value"), linebuf);
127 if (cip->call) cip->call(cip,opt);
128 else
129 *cip->sassignto = m_strdup(opt);
130 } else {
131 if (opt)
132 config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
133 if (cip->call) cip->call(cip,NULL);
134 else
135 *cip->iassignto = cip->arg_int;
138 if (ferror(file))
139 ohshite(_("read error in configuration file '%.255s'"), fn);
140 if (fclose(file))
141 ohshite(_("error closing configuration file '%.255s'"), fn);
144 static int
145 valid_config_filename(const struct dirent *dent)
147 const char *c;
149 if (dent->d_name[0] == '.')
150 return 0;
152 for (c = dent->d_name; *c; c++)
153 if (!c_isalnum(*c) && *c != '_' && *c != '-')
154 return 0;
156 if (*c == '\0')
157 return 1;
158 else
159 return 0;
162 static void
163 dpkg_options_load_dir(const char *prog, const struct cmdinfo *cmdinfos)
165 char *dirname;
166 struct dirent **dlist;
167 int dlist_n, i;
169 dirname = str_fmt("%s/%s.cfg.d", CONFIGDIR, prog);
171 dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
172 if (dlist_n < 0) {
173 if (errno == ENOENT) {
174 free(dirname);
175 return;
176 } else
177 ohshite(_("error opening configuration directory '%s'"), dirname);
180 for (i = 0; i < dlist_n; i++) {
181 char *filename;
183 filename = str_fmt("%s/%s", dirname, dlist[i]->d_name);
184 dpkg_options_load_file(filename, cmdinfos);
186 free(dlist[i]);
187 free(filename);
190 free(dirname);
191 free(dlist);
194 void
195 dpkg_options_load(const char *prog, const struct cmdinfo *cmdinfos)
197 char *home, *file;
199 dpkg_options_load_dir(prog, cmdinfos);
201 file = str_fmt("%s/%s.cfg", CONFIGDIR, prog);
202 dpkg_options_load_file(file, cmdinfos);
203 free(file);
205 home = getenv("HOME");
206 if (home != NULL) {
207 file = str_fmt("%s/.%s.cfg", home, prog);
208 dpkg_options_load_file(file, cmdinfos);
209 free(file);
213 void
214 dpkg_options_parse(const char *const **argvp, const struct cmdinfo *cmdinfos,
215 const char *help_str)
217 const struct cmdinfo *cip;
218 const char *p, *value;
219 int l;
221 if (**argvp == NULL)
222 badusage(_("missing program name in argv[0]"));
224 printforhelp = help_str;
226 ++(*argvp);
227 while ((p = **argvp) && p[0] == '-' && p[1] != '\0') {
228 ++(*argvp);
229 if (strcmp(p, "--") == 0)
230 break;
231 if (*++p == '-') {
232 ++p; value=NULL;
233 for (cip= cmdinfos;
234 cip->olong || cip->oshort;
235 cip++) {
236 if (!cip->olong) continue;
237 if (strcmp(p, cip->olong) == 0)
238 break;
239 l= strlen(cip->olong);
240 if (strncmp(p, cip->olong, l) == 0 &&
241 (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
243 if (!cip->olong) badusage(_("unknown option --%s"),p);
244 if (cip->takesvalue) {
245 if (!value) {
246 value= *(*argvp)++;
247 if (!value) badusage(_("--%s option takes a value"),cip->olong);
249 if (cip->call) cip->call(cip,value);
250 else *cip->sassignto= value;
251 } else {
252 if (value) badusage(_("--%s option does not take a value"),cip->olong);
253 if (cip->call) cip->call(cip,NULL);
254 else
255 *cip->iassignto = cip->arg_int;
257 } else {
258 while (*p) {
259 for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
260 if (!cip->oshort) badusage(_("unknown option -%c"),*p);
261 p++;
262 if (cip->takesvalue) {
263 if (!*p) {
264 value= *(*argvp)++;
265 if (!value) badusage(_("-%c option takes a value"),cip->oshort);
266 } else {
267 value= p; p="";
268 if (*value == '=') value++;
270 if (cip->call) cip->call(cip,value);
271 else *cip->sassignto= value;
272 } else {
273 if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
274 if (cip->call) cip->call(cip,NULL);
275 else
276 *cip->iassignto = cip->arg_int;
283 long
284 dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str)
286 long value;
287 char *end;
289 errno = 0;
290 value = strtol(str, &end, 0);
291 if (str == end || *end || value < 0 || value > INT_MAX || errno != 0) {
292 if (cmd->olong)
293 badusage(_("invalid integer for --%s: '%.250s'"), cmd->olong, str);
294 else
295 badusage(_("invalid integer for -%c: '%.250s'"), cmd->oshort, str);
298 return value;
301 void
302 setobsolete(const struct cmdinfo *cip, const char *value)
304 warning(_("obsolete option '--%s'"), cip->olong);
307 const struct cmdinfo *cipaction = NULL;
309 /* XXX: This function is a hack. */
310 static inline int
311 option_short(int c)
313 return c ? c : '\b';
316 void
317 setaction(const struct cmdinfo *cip, const char *value)
319 if (cipaction && cip)
320 badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
321 option_short(cip->oshort), cip->olong,
322 option_short(cipaction->oshort), cipaction->olong);
323 cipaction = cip;
324 if (cip && cip->takesvalue == 2 && cip->sassignto)
325 *cipaction->sassignto = value;