1
/* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2
* \file popt/poptconfig.c
5
/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
2
6
file accompanying popt source distributions, available from
3
ftp://ftp.redhat.com/pub/code/popt */
7
ftp://ftp.rpm.org/pub/rpm/dist. */
6
10
#include "poptint.h"
8
static void configLine(poptContext con, char * line) {
12
/*@-compmempass@*/ /* FIX: item->option.longName kept, not dependent. */
13
static void configLine(poptContext con, char * line)
9
17
int nameLength = strlen(con->appName);
11
struct poptAlias alias;
13
char * longName = NULL;
14
char shortName = '\0';
19
const char * entryType;
21
poptItem item = alloca(sizeof(*item));
25
memset(item, 0, sizeof(*item));
16
28
if (strncmp(line, con->appName, nameLength)) return;
17
31
line += nameLength;
18
if (!*line || !isspace(*line)) return;
19
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++;
22
while (!*line || !isspace(*line)) line++;
24
while (*line && isspace(*line)) line++;
39
while (*line != '\0' && isspace(*line)) line++;
40
if (*line == '\0') return;
28
while (!*line || !isspace(*line)) line++;
42
while (*line == '\0' || !isspace(*line)) line++;
30
while (*line && isspace(*line)) line++;
45
while (*line != '\0' && isspace(*line)) line++;
46
if (*line == '\0') return;
48
/*@-temptrans@*/ /* FIX: line alias is saved */
33
49
if (opt[0] == '-' && opt[1] == '-')
35
else if (opt[0] == '-' && !opt[2])
38
if (!strcmp(entryType, "alias")) {
39
if (poptParseArgvString(line, &alias.argc, &alias.argv)) return;
40
alias.longName = longName, alias.shortName = shortName;
41
poptAddAlias(con, alias, 0);
42
} else if (!strcmp(entryType, "exec")) {
43
con->execs = realloc(con->execs,
44
sizeof(*con->execs) * (con->numExecs + 1));
46
con->execs[con->numExecs].longName = xstrdup(longName);
48
con->execs[con->numExecs].longName = NULL;
50
con->execs[con->numExecs].shortName = shortName;
51
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);
57
int poptReadConfigFile(poptContext con, const char * fn) {
58
char * file=NULL, * chptr, * end;
59
char * buf=NULL, * dst;
95
int poptReadConfigFile(poptContext con, const char * fn)
97
const char * file, * chptr, * end;
99
/*@dependent@*/ char * dst;
63
103
fd = open(fn, O_RDONLY);
68
return POPT_ERROR_ERRNO;
105
return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO);
71
107
fileLength = lseek(fd, 0, SEEK_END);
72
(void) lseek(fd, 0, 0);
74
file = malloc(fileLength + 1);
75
if (read(fd, file, fileLength) != fileLength) {
80
return POPT_ERROR_ERRNO;
84
dst = buf = malloc(fileLength + 1);
108
if (fileLength == -1 || lseek(fd, 0, 0) == -1) {
114
return POPT_ERROR_ERRNO;
117
file = alloca(fileLength + 1);
118
if (read(fd, (char *)file, fileLength) != fileLength) {
124
return POPT_ERROR_ERRNO;
127
return POPT_ERROR_ERRNO;
130
dst = buf = alloca(fileLength + 1);
87
133
end = (file + fileLength);
134
/*@-infloops@*/ /* LCL: can't detect chptr++ */
88
135
while (chptr < end) {
93
140
while (*dst && isspace(*dst)) dst++;
94
if (*dst && *dst != '#') {
141
if (*dst && *dst != '#')
95
142
configLine(con, dst);
144
/*@switchbreak@*/ break;
100
146
*dst++ = *chptr++;
101
147
if (chptr < end) {