~ubuntu-branches/ubuntu/hoary/popt/hoary

« back to all changes in this revision

Viewing changes to poptconfig.c

  • Committer: Bazaar Package Importer
  • Author(s): Paul Martin
  • Date: 2004-08-29 16:36:43 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040829163643-rirf1ycw80g3000b
Tags: 1.7-5
* The "I hate automake" release.
* Make sure configure.in and configure.ac are the same, including
  timestamp. This allows a compile if you have two different versions of
  automake installed. (Closes: #245819)
* Standards-Version: 3.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * \file popt/poptconfig.c
3
3
 */
4
4
 
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. */
8
8
 
9
9
#include "system.h"
10
10
#include "poptint.h"
11
11
 
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)
 
14
        /*@modifies con @*/
 
15
{
 
16
    /*@-type@*/
13
17
    int nameLength = strlen(con->appName);
14
 
    char * opt;
15
 
    struct poptAlias alias;
16
 
    char * entryType;
17
 
    char * longName = NULL;
18
 
    char shortName = '\0';
 
18
    /*@=type@*/
 
19
    const char * entryType;
 
20
    const char * opt;
 
21
    poptItem item = alloca(sizeof(*item));
 
22
    int i, j;
19
23
    
 
24
/*@-boundswrite@*/
 
25
    memset(item, 0, sizeof(*item));
 
26
 
 
27
    /*@-type@*/
20
28
    if (strncmp(line, con->appName, nameLength)) return;
 
29
    /*@=type@*/
 
30
 
21
31
    line += nameLength;
22
 
    if (!*line || !isspace(*line)) return;
23
 
    while (*line && isspace(*line)) line++;
 
32
    if (*line == '\0' || !isspace(*line)) return;
 
33
 
 
34
    while (*line != '\0' && isspace(*line)) line++;
24
35
    entryType = line;
 
36
    while (*line == '\0' || !isspace(*line)) line++;
 
37
    *line++ = '\0';
25
38
 
26
 
    while (!*line || !isspace(*line)) line++;
27
 
    *line++ = '\0';
28
 
    while (*line && isspace(*line)) line++;
29
 
    if (!*line) return;
 
39
    while (*line != '\0' && isspace(*line)) line++;
 
40
    if (*line == '\0') return;
30
41
    opt = line;
31
 
 
32
 
    while (!*line || !isspace(*line)) line++;
 
42
    while (*line == '\0' || !isspace(*line)) line++;
33
43
    *line++ = '\0';
34
 
    while (*line && isspace(*line)) line++;
35
 
    if (!*line) return;
36
 
 
 
44
 
 
45
    while (*line != '\0' && isspace(*line)) line++;
 
46
    if (*line == '\0') return;
 
47
 
 
48
    /*@-temptrans@*/ /* FIX: line alias is saved */
37
49
    if (opt[0] == '-' && opt[1] == '-')
38
 
        longName = opt + 2;
39
 
    else if (opt[0] == '-' && !opt[2])
40
 
        shortName = opt[1];
41
 
 
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));
49
 
        if (longName)
50
 
            con->execs[con->numExecs].longName = xstrdup(longName);
51
 
        else
52
 
            con->execs[con->numExecs].longName = NULL;
53
 
 
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];
 
53
    /*@=temptrans@*/
 
54
 
 
55
    if (poptParseArgvString(line, &item->argc, &item->argv)) return;
 
56
 
 
57
    /*@-modobserver@*/
 
58
    item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
 
59
    for (i = 0, j = 0; i < item->argc; i++, j++) {
 
60
        const char * f;
 
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;
 
66
            j--;
 
67
        } else
 
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;
 
74
            j--;
 
75
        } else
 
76
        if (j != i)
 
77
            item->argv[j] = item->argv[i];
 
78
    }
 
79
    if (j != i) {
 
80
        item->argv[j] = NULL;
 
81
        item->argc = j;
 
82
    }
 
83
    /*@=modobserver@*/
 
84
/*@=boundswrite@*/
56
85
        
57
 
        con->numExecs++;
58
 
    }
 
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);
 
91
    /*@=nullstate@*/
59
92
}
 
93
/*@=compmempass@*/
60
94
 
61
 
int poptReadConfigFile(poptContext con, const char * fn) {
62
 
    char * file, * chptr, * end;
63
 
    char * buf, * dst;
 
95
int poptReadConfigFile(poptContext con, const char * fn)
 
96
{
 
97
    const char * file, * chptr, * end;
 
98
    char * buf;
 
99
/*@dependent@*/ char * dst;
64
100
    int fd, rc;
65
 
    int fileLength;
 
101
    off_t fileLength;
66
102
 
67
103
    fd = open(fn, O_RDONLY);
68
 
    if (fd < 0) {
69
 
        if (errno == ENOENT)
70
 
            return 0;
71
 
        else 
72
 
            return POPT_ERROR_ERRNO;
73
 
    }
 
104
    if (fd < 0)
 
105
        return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO);
74
106
 
75
107
    fileLength = lseek(fd, 0, SEEK_END);
76
 
    (void) lseek(fd, 0, 0);
 
108
    if (fileLength == -1 || lseek(fd, 0, 0) == -1) {
 
109
        rc = errno;
 
110
        (void) close(fd);
 
111
        /*@-mods@*/
 
112
        errno = rc;
 
113
        /*@=mods@*/
 
114
        return POPT_ERROR_ERRNO;
 
115
    }
77
116
 
78
117
    file = alloca(fileLength + 1);
79
 
    if (read(fd, file, fileLength) != fileLength) {
 
118
    if (read(fd, (char *)file, fileLength) != fileLength) {
80
119
        rc = errno;
81
 
        close(fd);
 
120
        (void) close(fd);
 
121
        /*@-mods@*/
82
122
        errno = rc;
 
123
        /*@=mods@*/
83
124
        return POPT_ERROR_ERRNO;
84
125
    }
85
 
    close(fd);
 
126
    if (close(fd) == -1)
 
127
        return POPT_ERROR_ERRNO;
86
128
 
 
129
/*@-boundswrite@*/
87
130
    dst = buf = alloca(fileLength + 1);
88
131
 
89
132
    chptr = file;
90
133
    end = (file + fileLength);
 
134
    /*@-infloops@*/     /* LCL: can't detect chptr++ */
91
135
    while (chptr < end) {
92
136
        switch (*chptr) {
93
137
          case '\n':
94
138
            *dst = '\0';
95
139
            dst = buf;
96
140
            while (*dst && isspace(*dst)) dst++;
97
 
            if (*dst && *dst != '#') {
 
141
            if (*dst && *dst != '#')
98
142
                configLine(con, dst);
99
 
            }
100
143
            chptr++;
101
 
            break;
 
144
            /*@switchbreak@*/ break;
102
145
          case '\\':
103
146
            *dst++ = *chptr++;
104
147
            if (chptr < end) {
108
151
                else
109
152
                    *dst++ = *chptr++;
110
153
            }
111
 
            break;
 
154
            /*@switchbreak@*/ break;
112
155
          default:
113
156
            *dst++ = *chptr++;
114
 
            break;
 
157
            /*@switchbreak@*/ break;
115
158
        }
116
159
    }
 
160
    /*@=infloops@*/
 
161
/*@=boundswrite@*/
117
162
 
118
163
    return 0;
119
164
}
120
165
 
121
 
int poptReadDefaultConfig(poptContext con, /*@unused@*/ int useEnv) {
 
166
int poptReadDefaultConfig(poptContext con, /*@unused@*/ int useEnv)
 
167
{
122
168
    char * fn, * home;
123
169
    int rc;
124
170
 
 
171
    /*@-type@*/
125
172
    if (!con->appName) return 0;
 
173
    /*@=type@*/
126
174
 
127
175
    rc = poptReadConfigFile(con, "/etc/popt");
128
176
    if (rc) return rc;
 
177
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
129
178
    if (getuid() != geteuid()) return 0;
 
179
#endif
130
180
 
131
181
    if ((home = getenv("HOME"))) {
132
182
        fn = alloca(strlen(home) + 20);
138
188
 
139
189
    return 0;
140
190
}
141