2 * dselect - Debian package maintenance user interface
3 * methparse.cc - access method list parsing
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2011, 2013-2015 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <sys/types.h>
37 #include <dpkg/i18n.h>
38 #include <dpkg/c-ctype.h>
39 #include <dpkg/dpkg.h>
40 #include <dpkg/dpkg-db.h>
47 struct dselect_option
*options
= nullptr, *coption
= nullptr;
48 struct method
*methods
= nullptr;
50 static void DPKG_ATTR_NORET
51 badmethod(const char *pathname
, const char *why
)
53 ohshit(_("syntax error in method options file '%.250s' -- %s"), pathname
, why
);
56 static void DPKG_ATTR_NORET
57 eofmethod(const char *pathname
, FILE *f
, const char *why
)
60 ohshite(_("error reading options file '%.250s'"), pathname
);
61 badmethod(pathname
,why
);
64 void readmethods(const char *pathbase
, dselect_option
**optionspp
, int *nread
) {
65 static const char *const methodprograms
[]= {
71 const char *const *ccpp
;
72 int methodlen
, baselen
;
73 char *pathinmeth
, *pathbuf
, *pathmeth
;
75 FILE *names
, *descfile
;
82 baselen
= strlen(pathbase
);
83 pathbuf
= new char[baselen
+IMETHODMAXLEN
+IOPTIONMAXLEN
+sizeof(OPTIONSDESCPFX
)+10];
84 strcpy(pathbuf
,pathbase
);
85 strcpy(pathbuf
+baselen
,"/");
86 pathmeth
= pathbuf
+baselen
+1;
88 dir
= opendir(pathbuf
);
90 if (errno
== ENOENT
) {
94 ohshite(_("unable to read '%.250s' directory for reading methods"),
98 debug(dbg_general
, "readmethods('%s',...) directory open", pathbase
);
100 while ((dent
= readdir(dir
)) != nullptr) {
101 int c
= dent
->d_name
[0];
102 debug(dbg_general
, "readmethods('%s',...) considering '%s' ...",
103 pathbase
, dent
->d_name
);
104 if (c
!= '_' && !c_isalpha(c
))
106 char *p
= dent
->d_name
+ 1;
107 while ((c
= *p
) != 0 && c_isalnum(c
) && c
!= '_')
110 methodlen
= strlen(dent
->d_name
);
111 if (methodlen
> IMETHODMAXLEN
)
112 ohshit(_("method '%.250s' has name that is too long (%d > %d characters)"),
113 dent
->d_name
, methodlen
, IMETHODMAXLEN
);
114 /* Check if there is a localized version of this method */
116 strcpy(pathmeth
, dent
->d_name
);
117 strcpy(pathmeth
+methodlen
, "/");
118 pathinmeth
= pathmeth
+methodlen
+1;
120 for (ccpp
= methodprograms
; *ccpp
; ccpp
++) {
121 strcpy(pathinmeth
,*ccpp
);
122 if (access(pathbuf
,R_OK
|X_OK
))
123 ohshite(_("unable to access method script '%.250s'"), pathbuf
);
125 debug(dbg_general
, " readmethods('%s',...) scripts ok", pathbase
);
127 strcpy(pathinmeth
,METHODOPTIONSFILE
);
128 names
= fopen(pathbuf
,"r");
130 ohshite(_("unable to read method options file '%.250s'"), pathbuf
);
133 meth
->name
= new char[strlen(dent
->d_name
)+1];
134 strcpy(meth
->name
,dent
->d_name
);
135 meth
->path
= new char[baselen
+1+methodlen
+2+50];
136 strncpy(meth
->path
,pathbuf
,baselen
+1+methodlen
);
137 strcpy(meth
->path
+baselen
+1+methodlen
,"/");
138 meth
->pathinmeth
= meth
->path
+baselen
+1+methodlen
+1;
140 meth
->prev
= nullptr;
142 methods
->prev
= meth
;
144 debug(dbg_general
, " readmethods('%s',...) new method"
145 " name='%s' path='%s' pathinmeth='%s'",
146 pathbase
, meth
->name
, meth
->path
, meth
->pathinmeth
);
148 while ((c
= fgetc(names
)) != EOF
) {
151 opt
= new dselect_option
;
156 badmethod(pathbuf
, _("non-digit where digit wanted"));
160 eofmethod(pathbuf
, names
, _("end of file in index string"));
161 } while (!c_isspace(c
));
162 if (strlen(vb
.string()) > OPTIONINDEXMAXLEN
)
163 badmethod(pathbuf
,_("index string too long"));
164 strcpy(opt
->index
,vb
.string());
166 if (c
== '\n') badmethod(pathbuf
,_("newline before option name start"));
169 eofmethod(pathbuf
, names
, _("end of file before option name start"));
170 } while (c_isspace(c
));
172 if (!c_isalpha(c
) && c
!= '_')
173 badmethod(pathbuf
,_("nonalpha where option name start wanted"));
175 if (!c_isalnum(c
) && c
!= '_')
176 badmethod(pathbuf
, _("non-alphanum in option name"));
180 eofmethod(pathbuf
, names
, _("end of file in option name"));
181 } while (!c_isspace(c
));
182 opt
->name
= new char[strlen(vb
.string())+1];
183 strcpy(opt
->name
,vb
.string());
185 if (c
== '\n') badmethod(pathbuf
,_("newline before summary"));
188 eofmethod(pathbuf
, names
, _("end of file before summary"));
189 } while (c_isspace(c
));
195 eofmethod(pathbuf
, names
, _("end of file in summary - missing newline"));
197 opt
->summary
= new char[strlen(vb
.string())+1];
198 strcpy(opt
->summary
,vb
.string());
200 strcpy(pathinmeth
,OPTIONSDESCPFX
);
201 strcpy(pathinmeth
+sizeof(OPTIONSDESCPFX
)-1,opt
->name
);
202 descfile
= fopen(pathbuf
,"r");
205 ohshite(_("unable to open option description file '%.250s'"), pathbuf
);
206 opt
->description
= nullptr;
207 } else { /* descfile != 0 */
208 if (fstat(fileno(descfile
),&stab
))
209 ohshite(_("unable to stat option description file '%.250s'"), pathbuf
);
210 opt
->description
= new char[stab
.st_size
+1]; errno
=0;
211 size_t filelen
= stab
.st_size
;
212 if (fread(opt
->description
,1,stab
.st_size
+1,descfile
) != filelen
)
213 ohshite(_("failed to read option description file '%.250s'"), pathbuf
);
214 opt
->description
[stab
.st_size
]= 0;
215 if (ferror(descfile
))
216 ohshite(_("error during read of option description file '%.250s'"), pathbuf
);
219 strcpy(pathinmeth
,METHODOPTIONSFILE
);
222 " readmethods('%s',...) new option index='%s' name='%s'"
223 " summary='%.20s' strlen(description=%s)=%zu method name='%s'"
224 " path='%s' pathinmeth='%s'",
226 opt
->index
, opt
->name
, opt
->summary
,
227 opt
->description
? "'...'" : "null",
228 opt
->description
? strlen(opt
->description
) : -1,
229 opt
->meth
->name
, opt
->meth
->path
, opt
->meth
->pathinmeth
);
231 dselect_option
**optinsert
= optionspp
;
232 while (*optinsert
&& strcmp(opt
->index
, (*optinsert
)->index
) > 0)
233 optinsert
= &(*optinsert
)->next
;
234 opt
->next
= *optinsert
;
239 ohshite(_("error during read of method options file '%.250s'"), pathbuf
);
243 debug(dbg_general
, "readmethods('%s',...) done", pathbase
);
247 static char *methoptfile
= nullptr;
249 void getcurrentopt() {
250 char methoptbuf
[IMETHODMAXLEN
+1+IOPTIONMAXLEN
+2];
255 if (methoptfile
== nullptr)
256 methoptfile
= dpkg_db_get_path(CMETHOPTFILE
);
259 cmo
= fopen(methoptfile
,"r");
261 if (errno
== ENOENT
) return;
262 ohshite(_("unable to open current option file '%.250s'"), methoptfile
);
264 debug(dbg_general
, "getcurrentopt() cmethopt open");
265 if (!fgets(methoptbuf
,sizeof(methoptbuf
),cmo
)) { fclose(cmo
); return; }
266 if (fgetc(cmo
) != EOF
) { fclose(cmo
); return; }
267 if (!feof(cmo
)) { fclose(cmo
); return; }
268 debug(dbg_general
, "getcurrentopt() cmethopt eof");
270 debug(dbg_general
, "getcurrentopt() cmethopt read");
271 l
= strlen(methoptbuf
); if (!l
|| methoptbuf
[l
-1] != '\n') return;
273 debug(dbg_general
, "getcurrentopt() cmethopt len and newline");
274 p
= strchr(methoptbuf
,' '); if (!p
) return;
275 debug(dbg_general
, "getcurrentopt() cmethopt space");
277 debug(dbg_general
, "getcurrentopt() cmethopt meth name '%s'", methoptbuf
);
278 method
*meth
= methods
;
279 while (meth
&& strcmp(methoptbuf
, meth
->name
))
282 debug(dbg_general
, "getcurrentopt() cmethopt meth found; opt '%s'", p
);
283 dselect_option
*opt
= options
;
284 while (opt
&& (opt
->meth
!= meth
|| strcmp(p
, opt
->name
)))
287 debug(dbg_general
, "getcurrentopt() cmethopt opt found");
291 void writecurrentopt() {
292 struct atomic_file
*file
;
294 if (methoptfile
== nullptr)
295 internerr("method options filename is nullptr");
297 file
= atomic_file_new(methoptfile
, ATOMIC_FILE_NORMAL
);
298 atomic_file_open(file
);
299 if (fprintf(file
->fp
, "%s %s\n", coption
->meth
->name
, coption
->name
) == EOF
)
300 ohshite(_("unable to write new option to '%.250s'"), file
->name_new
);
301 atomic_file_close(file
);
302 atomic_file_commit(file
);
303 atomic_file_free(file
);