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 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 badmethod(const char *pathname, const char *why) DPKG_ATTR_NORET;
  44. static void badmethod(const char *pathname, const char *why)
  45. {
  46. ohshit(_("syntax error in method options file `%.250s' -- %s"), pathname, why);
  47. }
  48. static void eofmethod(const char *pathname, FILE *f, const char *why) DPKG_ATTR_NORET;
  49. static void eofmethod(const char *pathname, FILE *f, const char *why) {
  50. if (ferror(f)) ohshite(_("error reading options file `%.250s'"),pathname);
  51. badmethod(pathname,why);
  52. }
  53. void readmethods(const char *pathbase, dselect_option **optionspp, int *nread) {
  54. static const char *const methodprograms[]= {
  55. METHODSETUPSCRIPT, METHODUPDATESCRIPT, METHODINSTALLSCRIPT, 0
  56. };
  57. const char *const *ccpp;
  58. int methodlen, c, baselen;
  59. char *pathinmeth, *pathbuf, *pathmeth;
  60. DIR *dir;
  61. FILE *names, *descfile;
  62. struct dirent *dent;
  63. struct varbuf vb;
  64. method *meth;
  65. dselect_option *opt;
  66. struct stat stab;
  67. baselen= strlen(pathbase);
  68. pathbuf= new char[baselen+IMETHODMAXLEN+IOPTIONMAXLEN+sizeof(OPTIONSDESCPFX)+10];
  69. strcpy(pathbuf,pathbase);
  70. strcpy(pathbuf+baselen,"/");
  71. pathmeth= pathbuf+baselen+1;
  72. dir= opendir(pathbuf);
  73. if (!dir) {
  74. if (errno == ENOENT) return;
  75. ohshite(_("unable to read `%.250s' directory for reading methods"),pathbuf);
  76. }
  77. if (debug) fprintf(debug,"readmethods(`%s',...) directory open\n", pathbase);
  78. while ((dent= readdir(dir)) != 0) {
  79. c= dent->d_name[0];
  80. if (debug) fprintf(debug,"readmethods(`%s',...) considering `%s' ...\n",
  81. pathbase,dent->d_name);
  82. if (c != '_' && !isalpha(c)) continue;
  83. char *p = dent->d_name + 1;
  84. while ((c = *p) != 0 && isalnum(c) && c != '_')
  85. 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 dselect_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. dselect_option **optinsert = optionspp;
  191. while (*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. 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. method *meth = methods;
  239. while (meth && strcmp(methoptbuf, meth->name))
  240. meth = meth->next;
  241. if (!meth) return;
  242. if (debug) fprintf(debug,"getcurrentopt() cmethopt meth found; opt `%s'\n",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. if (debug) fprintf(debug,"getcurrentopt() cmethopt opt found\n");
  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. }