methparse.cc 10 KB

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