methparse.cc 10 KB

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