methparse.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * methparse.cc - access method list parsing
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <dirent.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <dpkg/i18n.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include "dselect.h"
  38. #include "bindings.h"
  39. #include "method.h"
  40. int noptions=0;
  41. struct dselect_option *options=0, *coption=0;
  42. struct method *methods=0;
  43. static void DPKG_ATTR_NORET
  44. badmethod(const char *pathname, const char *why)
  45. {
  46. ohshit(_("syntax error in method options file `%.250s' -- %s"), pathname, why);
  47. }
  48. static void DPKG_ATTR_NORET
  49. eofmethod(const char *pathname, FILE *f, const char *why)
  50. {
  51. if (ferror(f)) ohshite(_("error reading options file `%.250s'"),pathname);
  52. badmethod(pathname,why);
  53. }
  54. void readmethods(const char *pathbase, dselect_option **optionspp, int *nread) {
  55. static const char *const methodprograms[]= {
  56. METHODSETUPSCRIPT, METHODUPDATESCRIPT, METHODINSTALLSCRIPT, 0
  57. };
  58. const char *const *ccpp;
  59. int methodlen, c, baselen;
  60. char *pathinmeth, *pathbuf, *pathmeth;
  61. DIR *dir;
  62. FILE *names, *descfile;
  63. struct dirent *dent;
  64. struct varbuf vb;
  65. method *meth;
  66. dselect_option *opt;
  67. struct stat stab;
  68. baselen= strlen(pathbase);
  69. pathbuf= new char[baselen+IMETHODMAXLEN+IOPTIONMAXLEN+sizeof(OPTIONSDESCPFX)+10];
  70. strcpy(pathbuf,pathbase);
  71. strcpy(pathbuf+baselen,"/");
  72. pathmeth= pathbuf+baselen+1;
  73. dir= opendir(pathbuf);
  74. if (!dir) {
  75. if (errno == ENOENT) {
  76. delete[] pathbuf;
  77. return;
  78. }
  79. ohshite(_("unable to read `%.250s' directory for reading methods"),pathbuf);
  80. }
  81. debug(dbg_general, "readmethods('%s',...) directory open", pathbase);
  82. while ((dent= readdir(dir)) != 0) {
  83. c= dent->d_name[0];
  84. debug(dbg_general, "readmethods('%s',...) considering '%s' ...",
  85. pathbase, dent->d_name);
  86. if (c != '_' && !isalpha(c)) continue;
  87. char *p = dent->d_name + 1;
  88. while ((c = *p) != 0 && isalnum(c) && c != '_')
  89. p++;
  90. if (c) continue;
  91. methodlen= strlen(dent->d_name);
  92. if (methodlen > IMETHODMAXLEN)
  93. ohshit(_("method `%.250s' has name that is too long (%d > %d characters)"),
  94. dent->d_name, methodlen, IMETHODMAXLEN);
  95. /* Check if there is a localized version of this method */
  96. strcpy(pathmeth, dent->d_name);
  97. strcpy(pathmeth+methodlen, "/");
  98. pathinmeth= pathmeth+methodlen+1;
  99. for (ccpp= methodprograms; *ccpp; ccpp++) {
  100. strcpy(pathinmeth,*ccpp);
  101. if (access(pathbuf,R_OK|X_OK))
  102. ohshite(_("unable to access method script `%.250s'"),pathbuf);
  103. }
  104. debug(dbg_general, " readmethods('%s',...) scripts ok", pathbase);
  105. strcpy(pathinmeth,METHODOPTIONSFILE);
  106. names= fopen(pathbuf,"r");
  107. if (!names) ohshite(_("unable to read method options file `%.250s'"),pathbuf);
  108. meth= new method;
  109. meth->name= new char[strlen(dent->d_name)+1];
  110. strcpy(meth->name,dent->d_name);
  111. meth->path= new char[baselen+1+methodlen+2+50];
  112. strncpy(meth->path,pathbuf,baselen+1+methodlen);
  113. strcpy(meth->path+baselen+1+methodlen,"/");
  114. meth->pathinmeth= meth->path+baselen+1+methodlen+1;
  115. meth->next= methods;
  116. meth->prev = 0;
  117. if (methods)
  118. methods->prev = meth;
  119. methods= meth;
  120. debug(dbg_general, " readmethods('%s',...) new method"
  121. " name='%s' path='%s' pathinmeth='%s'",
  122. pathbase, meth->name, meth->path, meth->pathinmeth);
  123. while ((c= fgetc(names)) != EOF) {
  124. if (isspace(c)) continue;
  125. opt= new dselect_option;
  126. opt->meth= meth;
  127. vb.reset();
  128. do {
  129. if (!isdigit(c)) badmethod(pathbuf,_("non-digit where digit wanted"));
  130. vb(c);
  131. c= fgetc(names);
  132. if (c == EOF) eofmethod(pathbuf,names,_("EOF in index string"));
  133. } while (!isspace(c));
  134. if (strlen(vb.string()) > OPTIONINDEXMAXLEN)
  135. badmethod(pathbuf,_("index string too long"));
  136. strcpy(opt->index,vb.string());
  137. do {
  138. if (c == '\n') badmethod(pathbuf,_("newline before option name start"));
  139. c= fgetc(names);
  140. if (c == EOF) eofmethod(pathbuf,names,_("EOF before option name start"));
  141. } while (isspace(c));
  142. vb.reset();
  143. if (!isalpha(c) && c != '_')
  144. badmethod(pathbuf,_("nonalpha where option name start wanted"));
  145. do {
  146. if (!isalnum(c) && c != '_') badmethod(pathbuf,_("non-alphanum in option name"));
  147. vb(c);
  148. c= fgetc(names);
  149. if (c == EOF) eofmethod(pathbuf,names,_("EOF in option name"));
  150. } while (!isspace(c));
  151. opt->name= new char[strlen(vb.string())+1];
  152. strcpy(opt->name,vb.string());
  153. do {
  154. if (c == '\n') badmethod(pathbuf,_("newline before summary"));
  155. c= fgetc(names);
  156. if (c == EOF) eofmethod(pathbuf,names,_("EOF before summary"));
  157. } while (isspace(c));
  158. vb.reset();
  159. do {
  160. vb(c);
  161. c= fgetc(names);
  162. if (c == EOF) eofmethod(pathbuf,names,_("EOF in summary - missing newline"));
  163. } while (c != '\n');
  164. opt->summary= new char[strlen(vb.string())+1];
  165. strcpy(opt->summary,vb.string());
  166. strcpy(pathinmeth,OPTIONSDESCPFX);
  167. strcpy(pathinmeth+sizeof(OPTIONSDESCPFX)-1,opt->name);
  168. descfile= fopen(pathbuf,"r");
  169. if (!descfile) {
  170. if (errno != ENOENT)
  171. ohshite(_("unable to open option description file `%.250s'"),pathbuf);
  172. opt->description= 0;
  173. } else { /* descfile != 0 */
  174. if (fstat(fileno(descfile),&stab))
  175. ohshite(_("unable to stat option description file `%.250s'"),pathbuf);
  176. opt->description= new char[stab.st_size+1]; errno=0;
  177. size_t filelen = stab.st_size;
  178. if (fread(opt->description,1,stab.st_size+1,descfile) != filelen)
  179. ohshite(_("failed to read option description file `%.250s'"),pathbuf);
  180. opt->description[stab.st_size]= 0;
  181. if (ferror(descfile))
  182. ohshite(_("error during read of option description file `%.250s'"),pathbuf);
  183. fclose(descfile);
  184. }
  185. strcpy(pathinmeth,METHODOPTIONSFILE);
  186. debug(dbg_general,
  187. " readmethods('%s',...) new option index='%s' name='%s'"
  188. " summary='%.20s' strlen(description=%s)=%zu method name='%s'"
  189. " path='%s' pathinmeth='%s'",
  190. pathbase,
  191. opt->index, opt->name, opt->summary,
  192. opt->description ? "'...'" : "null",
  193. opt->description ? strlen(opt->description) : -1,
  194. opt->meth->name, opt->meth->path, opt->meth->pathinmeth);
  195. dselect_option **optinsert = optionspp;
  196. while (*optinsert && strcmp(opt->index, (*optinsert)->index) > 0)
  197. optinsert = &(*optinsert)->next;
  198. opt->next= *optinsert;
  199. *optinsert= opt;
  200. (*nread)++;
  201. }
  202. if (ferror(names))
  203. ohshite(_("error during read of method options file `%.250s'"),pathbuf);
  204. fclose(names);
  205. }
  206. closedir(dir);
  207. debug(dbg_general, "readmethods('%s',...) done", pathbase);
  208. delete[] pathbuf;
  209. }
  210. static char *methoptfile= 0;
  211. void getcurrentopt() {
  212. char methoptbuf[IMETHODMAXLEN+1+IOPTIONMAXLEN+2];
  213. FILE *cmo;
  214. int l;
  215. char *p;
  216. if (methoptfile == NULL)
  217. methoptfile = dpkg_db_get_path(CMETHOPTFILE);
  218. coption= 0;
  219. cmo= fopen(methoptfile,"r");
  220. if (!cmo) {
  221. if (errno == ENOENT) return;
  222. ohshite(_("unable to open current option file `%.250s'"),methoptfile);
  223. }
  224. debug(dbg_general, "getcurrentopt() cmethopt open");
  225. if (!fgets(methoptbuf,sizeof(methoptbuf),cmo)) { fclose(cmo); return; }
  226. if (fgetc(cmo) != EOF) { fclose(cmo); return; }
  227. if (!feof(cmo)) { fclose(cmo); return; }
  228. debug(dbg_general, "getcurrentopt() cmethopt eof");
  229. fclose(cmo);
  230. debug(dbg_general, "getcurrentopt() cmethopt read");
  231. l= strlen(methoptbuf); if (!l || methoptbuf[l-1] != '\n') return;
  232. methoptbuf[--l]= 0;
  233. debug(dbg_general, "getcurrentopt() cmethopt len and newline");
  234. p= strchr(methoptbuf,' '); if (!p) return;
  235. debug(dbg_general, "getcurrentopt() cmethopt space");
  236. *p++= 0;
  237. debug(dbg_general, "getcurrentopt() cmethopt meth name '%s'", methoptbuf);
  238. method *meth = methods;
  239. while (meth && strcmp(methoptbuf, meth->name))
  240. meth = meth->next;
  241. if (!meth) return;
  242. debug(dbg_general, "getcurrentopt() cmethopt meth found; opt '%s'", p);
  243. dselect_option *opt = options;
  244. while (opt && (opt->meth != meth || strcmp(p, opt->name)))
  245. opt = opt->next;
  246. if (!opt) return;
  247. debug(dbg_general, "getcurrentopt() cmethopt opt found");
  248. coption= opt;
  249. }
  250. void writecurrentopt() {
  251. FILE *cmo;
  252. int l;
  253. static char *newfile=0;
  254. assert(methoptfile);
  255. if (!newfile) {
  256. l= strlen(methoptfile);
  257. newfile= new char[l + sizeof(NEWDBEXT) + 1];
  258. strcpy(newfile,methoptfile);
  259. strcpy(newfile+l, NEWDBEXT);
  260. }
  261. cmo= fopen(newfile,"w");
  262. if (!cmo) ohshite(_("unable to open new option file `%.250s'"),newfile);
  263. if (fprintf(cmo,"%s %s\n",coption->meth->name,coption->name) == EOF) {
  264. fclose(cmo);
  265. ohshite(_("unable to write new option to `%.250s'"),newfile);
  266. }
  267. if (fclose(cmo))
  268. ohshite(_("unable to close new option file `%.250s'"),newfile);
  269. if (rename(newfile,methoptfile))
  270. ohshite(_("unable to install new option as `%.250s'"),methoptfile);
  271. }