2 * \file popt/poptconfig.c
5 /* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist. */
12 /*@-compmempass@*/ /* FIX: item->option.longName kept, not dependent. */
13 static void configLine(poptContext con
, unsigned char * line
)
17 int nameLength
= strlen(con
->appName
);
19 const char * entryType
;
21 poptItem item
= (poptItem
) alloca(sizeof(*item
));
24 memset(item
, 0, sizeof(*item
));
27 if (strncmp(line
, con
->appName
, nameLength
)) return;
31 if (*line
== '\0' || !isspace(*line
)) return;
33 while (*line
!= '\0' && isspace(*line
)) line
++;
35 while (*line
== '\0' || !isspace(*line
)) line
++;
38 while (*line
!= '\0' && isspace(*line
)) line
++;
39 if (*line
== '\0') return;
41 while (*line
== '\0' || !isspace(*line
)) line
++;
44 while (*line
!= '\0' && isspace(*line
)) line
++;
45 if (*line
== '\0') return;
47 /*@-temptrans@*/ /* FIX: line alias is saved */
48 if (opt
[0] == '-' && opt
[1] == '-')
49 item
->option
.longName
= opt
+ 2;
50 else if (opt
[0] == '-' && opt
[2] == '\0')
51 item
->option
.shortName
= opt
[1];
54 if (poptParseArgvString(line
, &item
->argc
, &item
->argv
)) return;
57 item
->option
.argInfo
= POPT_ARGFLAG_DOC_HIDDEN
;
58 for (i
= 0, j
= 0; i
< item
->argc
; i
++, j
++) {
60 if (!strncmp(item
->argv
[i
], "--POPTdesc=", sizeof("--POPTdesc=")-1)) {
61 f
= item
->argv
[i
] + sizeof("--POPTdesc=");
62 if (f
[0] == '$' && f
[1] == '"') f
++;
63 item
->option
.descrip
= f
;
64 item
->option
.argInfo
&= ~POPT_ARGFLAG_DOC_HIDDEN
;
67 if (!strncmp(item
->argv
[i
], "--POPTargs=", sizeof("--POPTargs=")-1)) {
68 f
= item
->argv
[i
] + sizeof("--POPTargs=");
69 if (f
[0] == '$' && f
[1] == '"') f
++;
70 item
->option
.argDescrip
= f
;
71 item
->option
.argInfo
&= ~POPT_ARGFLAG_DOC_HIDDEN
;
72 item
->option
.argInfo
|= POPT_ARG_STRING
;
76 item
->argv
[j
] = item
->argv
[i
];
84 /*@-nullstate@*/ /* FIX: item->argv[] may be NULL */
85 if (!strcmp(entryType
, "alias"))
86 (void) poptAddItem(con
, item
, 0);
87 else if (!strcmp(entryType
, "exec"))
88 (void) poptAddItem(con
, item
, 1);
93 int poptReadConfigFile(poptContext con
, const char * fn
)
95 const unsigned char * file
, * chptr
, * end
;
97 /*@dependent@*/ unsigned char * dst
;
101 fd
= open(fn
, O_RDONLY
);
103 return (errno
== ENOENT
? 0 : POPT_ERROR_ERRNO
);
105 fileLength
= lseek(fd
, 0, SEEK_END
);
106 if (fileLength
== -1 || lseek(fd
, 0, 0) == -1) {
112 return POPT_ERROR_ERRNO
;
115 file
= alloca(fileLength
+ 1);
116 if (read(fd
, (char *)file
, fileLength
) != fileLength
) {
122 return POPT_ERROR_ERRNO
;
125 return POPT_ERROR_ERRNO
;
127 dst
= buf
= alloca(fileLength
+ 1);
130 end
= (file
+ fileLength
);
131 /*@-infloops@*/ /* LCL: can't detect chptr++ */
132 while (chptr
< end
) {
137 while (*dst
&& isspace(*dst
)) dst
++;
138 if (*dst
&& *dst
!= '#')
139 configLine(con
, dst
);
141 /*@switchbreak@*/ break;
147 /* \ at the end of a line does not insert a \n */
151 /*@switchbreak@*/ break;
154 /*@switchbreak@*/ break;
162 int poptReadDefaultConfig(poptContext con
, /*@unused@*/ UNUSED(int useEnv
))
168 if (!con
->appName
) return 0;
171 rc
= poptReadConfigFile(con
, "/etc/popt");
173 if (getuid() != geteuid()) return 0;
175 if ((home
= getenv("HOME"))) {
176 fn
= alloca(strlen(home
) + 20);
178 strcat(fn
, "/.popt");
179 rc
= poptReadConfigFile(con
, fn
);