2 * \file popt/poptconfig.c
5 /* (C) 1998-2002 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. */
11 /*@access poptContext @*/
13 /*@-compmempass@*/ /* FIX: item->option.longName kept, not dependent. */
14 static void configLine(poptContext con
, char * line
)
18 const char * entryType
;
20 poptItem item
= (poptItem
) alloca(sizeof(*item
));
23 if (con
->appName
== NULL
)
25 nameLength
= strlen(con
->appName
);
28 memset(item
, 0, sizeof(*item
));
30 if (strncmp(line
, con
->appName
, nameLength
)) return;
33 if (*line
== '\0' || !isSpace(line
)) return;
35 while (*line
!= '\0' && isSpace(line
)) line
++;
37 while (*line
== '\0' || !isSpace(line
)) line
++;
40 while (*line
!= '\0' && isSpace(line
)) line
++;
41 if (*line
== '\0') return;
43 while (*line
== '\0' || !isSpace(line
)) line
++;
46 while (*line
!= '\0' && isSpace(line
)) line
++;
47 if (*line
== '\0') return;
49 /*@-temptrans@*/ /* FIX: line alias is saved */
50 if (opt
[0] == '-' && opt
[1] == '-')
51 item
->option
.longName
= opt
+ 2;
52 else if (opt
[0] == '-' && opt
[2] == '\0')
53 item
->option
.shortName
= opt
[1];
56 if (poptParseArgvString(line
, &item
->argc
, &item
->argv
)) return;
59 item
->option
.argInfo
= POPT_ARGFLAG_DOC_HIDDEN
;
60 for (i
= 0, j
= 0; i
< item
->argc
; i
++, j
++) {
62 if (!strncmp(item
->argv
[i
], "--POPTdesc=", sizeof("--POPTdesc=")-1)) {
63 f
= item
->argv
[i
] + sizeof("--POPTdesc=");
64 if (f
[0] == '$' && f
[1] == '"') f
++;
65 item
->option
.descrip
= f
;
66 item
->option
.argInfo
&= ~POPT_ARGFLAG_DOC_HIDDEN
;
69 if (!strncmp(item
->argv
[i
], "--POPTargs=", sizeof("--POPTargs=")-1)) {
70 f
= item
->argv
[i
] + sizeof("--POPTargs=");
71 if (f
[0] == '$' && f
[1] == '"') f
++;
72 item
->option
.argDescrip
= f
;
73 item
->option
.argInfo
&= ~POPT_ARGFLAG_DOC_HIDDEN
;
74 item
->option
.argInfo
|= POPT_ARG_STRING
;
78 item
->argv
[j
] = item
->argv
[i
];
87 /*@-nullstate@*/ /* FIX: item->argv[] may be NULL */
88 if (!strcmp(entryType
, "alias"))
89 (void) poptAddItem(con
, item
, 0);
90 else if (!strcmp(entryType
, "exec"))
91 (void) poptAddItem(con
, item
, 1);
96 int poptReadConfigFile(poptContext con
, const char * fn
)
98 const char * file
, * chptr
, * end
;
100 /*@dependent@*/ char * dst
;
104 fd
= open(fn
, O_RDONLY
);
106 return (errno
== ENOENT
? 0 : POPT_ERROR_ERRNO
);
108 fileLength
= lseek(fd
, 0, SEEK_END
);
109 if (fileLength
== -1 || lseek(fd
, 0, 0) == -1) {
113 return POPT_ERROR_ERRNO
;
116 file
= alloca(fileLength
+ 1);
117 if (read(fd
, (char *)file
, fileLength
) != fileLength
) {
121 return POPT_ERROR_ERRNO
;
124 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;
163 int poptReadDefaultConfig(poptContext con
, /*@unused@*/ UNUSED(int useEnv
))
168 if (con
->appName
== NULL
) return 0;
170 rc
= poptReadConfigFile(con
, "/etc/popt");
173 if ((home
= getenv("HOME"))) {
174 size_t bufsize
= strlen(home
) + 20;
175 fn
= alloca(bufsize
);
176 if (fn
== NULL
) return 0;
177 snprintf(fn
, bufsize
, "%s/.popt", home
);
178 rc
= poptReadConfigFile(con
, fn
);