methparse.cc 9.7 KB

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