methparse.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <ncurses.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) __NORETURN;
  46. static void badmethod(const char *pathname, const char *why) {
  47. ohshit("syntax error in method options file `%.250s' -- %s", pathname, why);
  48. }
  49. static void eofmethod(const char *pathname, FILE *f, const char *why) __NORETURN;
  50. static void eofmethod(const char *pathname, FILE *f, const char *why) {
  51. if (ferror(f)) ohshite("error reading options file `%.250s'",pathname);
  52. badmethod(pathname,why);
  53. }
  54. void readmethods(const char *pathbase, 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 *p, *pathinmeth, *pathbuf, *pathmeth;
  61. DIR *dir;
  62. FILE *names, *descfile;
  63. struct dirent *dent;
  64. struct varbuf vb;
  65. method *meth;
  66. option *opt, **optinsert;
  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) return;
  76. ohshite("unable to read `%.250s' directory for reading methods",pathbuf);
  77. }
  78. if (debug) fprintf(debug,"readmethods(`%s',...) directory open\n", pathbase);
  79. while ((dent= readdir(dir)) != 0) {
  80. c= dent->d_name[0];
  81. if (debug) fprintf(debug,"readmethods(`%s',...) considering `%s' ...\n",
  82. pathbase,dent->d_name);
  83. if (c != '_' && !isalpha(c)) continue;
  84. for (p= dent->d_name+1; (c= *p) != 0 && isalnum(c) && c != '_'; p++);
  85. if (c) continue;
  86. methodlen= strlen(dent->d_name);
  87. if (methodlen > IMETHODMAXLEN)
  88. ohshit("method `%.250s' has name that is too long (%d > %d characters)",
  89. dent->d_name, methodlen, IMETHODMAXLEN);
  90. strcpy(pathmeth, dent->d_name);
  91. strcpy(pathmeth+methodlen, "/");
  92. pathinmeth= pathmeth+methodlen+1;
  93. for (ccpp= methodprograms; *ccpp; ccpp++) {
  94. strcpy(pathinmeth,*ccpp);
  95. if (access(pathbuf,R_OK|X_OK))
  96. ohshite("unable to access method script `%.250s'",pathbuf);
  97. }
  98. if (debug) fprintf(debug," readmethods(`%s',...) scripts ok\n", pathbase);
  99. strcpy(pathinmeth,METHODOPTIONSFILE);
  100. names= fopen(pathbuf,"r");
  101. if (!names) ohshite("unable to read method options file `%.250s'",pathbuf);
  102. meth= new method;
  103. meth->name= new char[strlen(dent->d_name)+1];
  104. strcpy(meth->name,dent->d_name);
  105. meth->path= new char[baselen+1+methodlen+2+50];
  106. strncpy(meth->path,pathbuf,baselen+1+methodlen);
  107. strcpy(meth->path+baselen+1+methodlen,"/");
  108. meth->pathinmeth= meth->path+baselen+1+methodlen+1;
  109. meth->next= methods;
  110. meth->back= 0;
  111. if (methods) methods->back= meth;
  112. methods= meth;
  113. if (debug) fprintf(debug," readmethods(`%s',...) new method"
  114. " name=`%s' path=`%s' pathinmeth=`%s'\n",
  115. pathbase, meth->name, meth->path, meth->pathinmeth);
  116. while ((c= fgetc(names)) != EOF) {
  117. if (isspace(c)) continue;
  118. opt= new option;
  119. opt->meth= meth;
  120. vb.reset();
  121. do {
  122. if (!isdigit(c)) badmethod(pathbuf,"non-digit where digit wanted");
  123. vb(c);
  124. c= fgetc(names);
  125. if (c == EOF) eofmethod(pathbuf,names,"EOF in index string");
  126. } while (!isspace(c));
  127. if (strlen(vb.string()) > OPTIONINDEXMAXLEN)
  128. badmethod(pathbuf,"index string too long");
  129. strcpy(opt->index,vb.string());
  130. do {
  131. if (c == '\n') badmethod(pathbuf,"newline before option name start");
  132. c= fgetc(names);
  133. if (c == EOF) eofmethod(pathbuf,names,"EOF before option name start");
  134. } while (isspace(c));
  135. vb.reset();
  136. if (!isalpha(c) && c != '_')
  137. badmethod(pathbuf,"nonalpha where option name start wanted");
  138. do {
  139. if (!isalnum(c) && c != '_') badmethod(pathbuf,"non-alphanum in option name");
  140. vb(c);
  141. c= fgetc(names);
  142. if (c == EOF) eofmethod(pathbuf,names,"EOF in option name");
  143. } while (!isspace(c));
  144. opt->name= new char[strlen(vb.string())+1];
  145. strcpy(opt->name,vb.string());
  146. do {
  147. if (c == '\n') badmethod(pathbuf,"newline before summary");
  148. c= fgetc(names);
  149. if (c == EOF) eofmethod(pathbuf,names,"EOF before summary");
  150. } while (isspace(c));
  151. vb.reset();
  152. do {
  153. vb(c);
  154. c= fgetc(names);
  155. if (c == EOF) eofmethod(pathbuf,names,"EOF in summary - missing newline");
  156. } while (c != '\n');
  157. opt->summary= new char[strlen(vb.string())+1];
  158. strcpy(opt->summary,vb.string());
  159. strcpy(pathinmeth,OPTIONSDESCPFX);
  160. strcpy(pathinmeth+sizeof(OPTIONSDESCPFX)-1,opt->name);
  161. descfile= fopen(pathbuf,"r");
  162. if (!descfile) {
  163. if (errno != ENOENT)
  164. ohshite("unable to open option description file `%.250s'",pathbuf);
  165. opt->description= 0;
  166. } else { /* descfile != 0 */
  167. if (fstat(fileno(descfile),&stab))
  168. ohshite("unable to stat option description file `%.250s'",pathbuf);
  169. opt->description= new char[stab.st_size+1]; errno=0;
  170. unsigned long filelen= stab.st_size;
  171. if (fread(opt->description,1,stab.st_size+1,descfile) != filelen)
  172. ohshite("failed to read option description file `%.250s'",pathbuf);
  173. opt->description[stab.st_size]= 0;
  174. if (ferror(descfile))
  175. ohshite("error during read of option description file `%.250s'",pathbuf);
  176. fclose(descfile);
  177. }
  178. strcpy(pathinmeth,METHODOPTIONSFILE);
  179. if (debug) fprintf(debug," readmethods(`%s',...) new option"
  180. " index=`%s' name=`%s' summary=`%.20s'"
  181. " strlen(description=%s)=%d"
  182. " method name=`%s' path=`%s' pathinmeth=`%s'\n",
  183. pathbase,
  184. opt->index, opt->name, opt->summary,
  185. opt->description ? "`...'" : "null",
  186. opt->description ? strlen(opt->description) : -1,
  187. opt->meth->name, opt->meth->path, opt->meth->pathinmeth);
  188. for (optinsert= optionspp;
  189. *optinsert && strcmp(opt->index,(*optinsert)->index) > 0;
  190. optinsert= &(*optinsert)->next);
  191. opt->next= *optinsert;
  192. *optinsert= opt;
  193. (*nread)++;
  194. }
  195. if (ferror(names))
  196. ohshite("error during read of method options file `%.250s'",pathbuf);
  197. fclose(names);
  198. }
  199. closedir(dir);
  200. if (debug) fprintf(debug,"readmethods(`%s',...) done\n", pathbase);
  201. delete[] pathbuf;
  202. }
  203. static char *methoptfile= 0;
  204. void getcurrentopt() {
  205. char methoptbuf[IMETHODMAXLEN+1+IOPTIONMAXLEN+2];
  206. FILE *cmo;
  207. int l;
  208. int admindirlen;
  209. char *p;
  210. method *meth;
  211. option *opt;
  212. if (!methoptfile) {
  213. admindirlen= strlen(admindir);
  214. methoptfile= new char[admindirlen + sizeof(CMETHOPTFILE) + 2];
  215. strcpy(methoptfile,admindir);
  216. strcpy(methoptfile+admindirlen, "/" CMETHOPTFILE);
  217. }
  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. if (debug) fprintf(debug,"getcurrentopt() cmethopt open\n");
  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. if (debug) fprintf(debug,"getcurrentopt() cmethopt eof\n");
  229. fclose(cmo);
  230. if (debug) fprintf(debug,"getcurrentopt() cmethopt read\n");
  231. l= strlen(methoptbuf); if (!l || methoptbuf[l-1] != '\n') return;
  232. methoptbuf[--l]= 0;
  233. if (debug) fprintf(debug,"getcurrentopt() cmethopt len and newline\n");
  234. p= strchr(methoptbuf,' '); if (!p) return;
  235. if (debug) fprintf(debug,"getcurrentopt() cmethopt space\n");
  236. *p++= 0;
  237. if (debug) fprintf(debug,"getcurrentopt() cmethopt meth name `%s'\n", methoptbuf);
  238. for (meth= methods; meth && strcmp(methoptbuf,meth->name); meth= meth->next);
  239. if (!meth) return;
  240. if (debug) fprintf(debug,"getcurrentopt() cmethopt meth found; opt `%s'\n",p);
  241. for (opt= options; opt && (opt->meth != meth || strcmp(p,opt->name)); opt= opt->next);
  242. if (!opt) return;
  243. if (debug) fprintf(debug,"getcurrentopt() cmethopt opt found\n");
  244. coption= opt;
  245. }
  246. void writecurrentopt() {
  247. FILE *cmo;
  248. int l;
  249. static char *newfile=0;
  250. assert(methoptfile);
  251. if (!newfile) {
  252. l= strlen(methoptfile);
  253. newfile= new char[l + sizeof(NEWDBEXT) + 1];
  254. strcpy(newfile,methoptfile);
  255. strcpy(newfile+l, NEWDBEXT);
  256. }
  257. cmo= fopen(newfile,"w");
  258. if (!cmo) ohshite("unable to open new option file `%.250s'",newfile);
  259. if (fprintf(cmo,"%s %s\n",coption->meth->name,coption->name) == EOF) {
  260. fclose(cmo);
  261. ohshite("unable to write new option to `%.250s'",newfile);
  262. }
  263. if (fclose(cmo))
  264. ohshite("unable to close new option file `%.250s'",newfile);
  265. if (rename(newfile,methoptfile))
  266. ohshite("unable to install new option as `%.250s'",methoptfile);
  267. }