2
2
* \file popt/poptconfig.c
5
/* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
5
/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
6
6
file accompanying popt source distributions, available from
7
7
ftp://ftp.rpm.org/pub/rpm/dist. */
10
10
#include "poptint.h"
12
static void configLine(poptContext con, char * line) {
12
/*@-compmempass@*/ /* FIX: item->option.longName kept, not dependent. */
13
static void configLine(poptContext con, char * line)
13
17
int nameLength = strlen(con->appName);
15
struct poptAlias alias;
17
char * longName = NULL;
18
char shortName = '\0';
19
const char * entryType;
21
poptItem item = alloca(sizeof(*item));
25
memset(item, 0, sizeof(*item));
20
28
if (strncmp(line, con->appName, nameLength)) return;
21
31
line += nameLength;
22
if (!*line || !isspace(*line)) return;
23
while (*line && isspace(*line)) line++;
32
if (*line == '\0' || !isspace(*line)) return;
34
while (*line != '\0' && isspace(*line)) line++;
36
while (*line == '\0' || !isspace(*line)) line++;
26
while (!*line || !isspace(*line)) line++;
28
while (*line && isspace(*line)) line++;
39
while (*line != '\0' && isspace(*line)) line++;
40
if (*line == '\0') return;
32
while (!*line || !isspace(*line)) line++;
42
while (*line == '\0' || !isspace(*line)) line++;
34
while (*line && isspace(*line)) line++;
45
while (*line != '\0' && isspace(*line)) line++;
46
if (*line == '\0') return;
48
/*@-temptrans@*/ /* FIX: line alias is saved */
37
49
if (opt[0] == '-' && opt[1] == '-')
39
else if (opt[0] == '-' && !opt[2])
42
if (!strcmp(entryType, "alias")) {
43
if (poptParseArgvString(line, &alias.argc, &alias.argv)) return;
44
alias.longName = longName, alias.shortName = shortName;
45
poptAddAlias(con, alias, 0);
46
} else if (!strcmp(entryType, "exec")) {
47
con->execs = realloc(con->execs,
48
sizeof(*con->execs) * (con->numExecs + 1));
50
con->execs[con->numExecs].longName = xstrdup(longName);
52
con->execs[con->numExecs].longName = NULL;
54
con->execs[con->numExecs].shortName = shortName;
55
con->execs[con->numExecs].script = xstrdup(line);
50
item->option.longName = opt + 2;
51
else if (opt[0] == '-' && opt[2] == '\0')
52
item->option.shortName = opt[1];
55
if (poptParseArgvString(line, &item->argc, &item->argv)) return;
58
item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
59
for (i = 0, j = 0; i < item->argc; i++, j++) {
61
if (!strncmp(item->argv[i], "--POPTdesc=", sizeof("--POPTdesc=")-1)) {
62
f = item->argv[i] + sizeof("--POPTdesc=");
63
if (f[0] == '$' && f[1] == '"') f++;
64
item->option.descrip = f;
65
item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
68
if (!strncmp(item->argv[i], "--POPTargs=", sizeof("--POPTargs=")-1)) {
69
f = item->argv[i] + sizeof("--POPTargs=");
70
if (f[0] == '$' && f[1] == '"') f++;
71
item->option.argDescrip = f;
72
item->option.argInfo &= ~POPT_ARGFLAG_DOC_HIDDEN;
73
item->option.argInfo |= POPT_ARG_STRING;
77
item->argv[j] = item->argv[i];
86
/*@-nullstate@*/ /* FIX: item->argv[] may be NULL */
87
if (!strcmp(entryType, "alias"))
88
(void) poptAddItem(con, item, 0);
89
else if (!strcmp(entryType, "exec"))
90
(void) poptAddItem(con, item, 1);
61
int poptReadConfigFile(poptContext con, const char * fn) {
62
char * file, * chptr, * end;
95
int poptReadConfigFile(poptContext con, const char * fn)
97
const char * file, * chptr, * end;
99
/*@dependent@*/ char * dst;
67
103
fd = open(fn, O_RDONLY);
72
return POPT_ERROR_ERRNO;
105
return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO);
75
107
fileLength = lseek(fd, 0, SEEK_END);
76
(void) lseek(fd, 0, 0);
108
if (fileLength == -1 || lseek(fd, 0, 0) == -1) {
114
return POPT_ERROR_ERRNO;
78
117
file = alloca(fileLength + 1);
79
if (read(fd, file, fileLength) != fileLength) {
118
if (read(fd, (char *)file, fileLength) != fileLength) {
83
124
return POPT_ERROR_ERRNO;
127
return POPT_ERROR_ERRNO;
87
130
dst = buf = alloca(fileLength + 1);
90
133
end = (file + fileLength);
134
/*@-infloops@*/ /* LCL: can't detect chptr++ */
91
135
while (chptr < end) {
96
140
while (*dst && isspace(*dst)) dst++;
97
if (*dst && *dst != '#') {
141
if (*dst && *dst != '#')
98
142
configLine(con, dst);
144
/*@switchbreak@*/ break;
103
146
*dst++ = *chptr++;
104
147
if (chptr < end) {