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