2
* libdpkg - Debian packaging suite library routines
3
* myopt.c - my very own option parsing
5
* Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
6
* Copyright © 2000,2002 Wichert Akkerman <wichert@deephackmode.org>
8
* This is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as
10
* published by the Free Software Foundation; either version 2,
11
* or (at your option) any later version.
13
* This is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public
19
* License along with this file; if not, write to the Free Software
20
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
#include <dpkg-i18n.h>
37
badusage(const char *fmt, ...)
43
vsnprintf(buf, sizeof(buf), fmt, al);
46
ohshit("%s\n\n%s", buf, gettext(printforhelp));
49
void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
51
char linebuf[MAX_CONFIG_LINE];
57
warning(_("failed to open configuration file '%.255s' for reading: %s"),
62
while (fgets(linebuf, sizeof(linebuf), file)) {
64
const struct cmdinfo *cip;
67
if ((linebuf[0] == '#') || (linebuf[0] == '\n') || (linebuf[0] == '\0'))
70
if (linebuf[l - 1] == '\n')
71
linebuf[l - 1] = '\0';
72
for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
78
while (isspace(*opt)) opt++;
81
for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
83
if (!cip->olong) continue;
84
if (!strcmp(cip->olong,linebuf)) break;
86
if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
87
!opt && !strncmp(linebuf,cip->olong,l)) {
93
if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
95
if (cip->takesvalue) {
96
if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
97
if (cip->call) cip->call(cip,opt);
99
*cip->sassignto = m_strdup(opt);
101
if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
102
if (cip->call) cip->call(cip,NULL);
103
else *cip->iassignto= cip->arg;
106
if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
107
if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
110
void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
113
l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
114
file = m_malloc(l1 + 1);
115
sprintf(file, CONFIGDIR "/%s.cfg", prog);
116
myfileopt(file, cmdinfos);
117
if ((home = getenv("HOME")) != NULL) {
118
l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
121
file = m_malloc(l2 + 1);
123
sprintf(file, "%s/.%s.cfg", home, prog);
124
myfileopt(file, cmdinfos);
129
void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
130
const struct cmdinfo *cip;
131
const char *p, *value;
135
while ((p= **argvp) && *p == '-') {
137
if (!strcmp(p,"--")) break;
141
cip->olong || cip->oshort;
143
if (!cip->olong) continue;
144
if (!strcmp(p,cip->olong)) break;
145
l= strlen(cip->olong);
146
if (!strncmp(p,cip->olong,l) &&
147
(p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
149
if (!cip->olong) badusage(_("unknown option --%s"),p);
150
if (cip->takesvalue) {
153
if (!value) badusage(_("--%s option takes a value"),cip->olong);
155
if (cip->call) cip->call(cip,value);
156
else *cip->sassignto= value;
158
if (value) badusage(_("--%s option does not take a value"),cip->olong);
159
if (cip->call) cip->call(cip,NULL);
160
else *cip->iassignto= cip->arg;
164
for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
165
if (!cip->oshort) badusage(_("unknown option -%c"),*p);
167
if (cip->takesvalue) {
170
if (!value) badusage(_("-%c option takes a value"),cip->oshort);
173
if (*value == '=') value++;
175
if (cip->call) cip->call(cip,value);
176
else *cip->sassignto= value;
178
if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
179
if (cip->call) cip->call(cip,NULL);
180
else *cip->iassignto= cip->arg;