methparse.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * methparse.cc - access method list parsing
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2011, 2013-2015 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <limits.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/c-ctype.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include "dselect.h"
  39. #include "bindings.h"
  40. #include "method.h"
  41. int noptions=0;
  42. struct dselect_option *options = nullptr, *coption = nullptr;
  43. struct method *methods = nullptr;
  44. static void DPKG_ATTR_NORET
  45. badmethod(const char *pathname, const char *why)
  46. {
  47. ohshit(_("syntax error in method options file '%.250s' -- %s"), pathname, why);
  48. }
  49. static void DPKG_ATTR_NORET
  50. eofmethod(const char *pathname, FILE *f, const char *why)
  51. {
  52. if (ferror(f))
  53. 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,
  59. METHODUPDATESCRIPT,
  60. METHODINSTALLSCRIPT,
  61. nullptr
  62. };
  63. const char *const *ccpp;
  64. int methodlen, c, baselen;
  65. char *pathinmeth, *pathbuf, *pathmeth;
  66. DIR *dir;
  67. FILE *names, *descfile;
  68. struct dirent *dent;
  69. struct varbuf vb;
  70. method *meth;
  71. dselect_option *opt;
  72. struct stat stab;
  73. baselen= strlen(pathbase);
  74. pathbuf= new char[baselen+IMETHODMAXLEN+IOPTIONMAXLEN+sizeof(OPTIONSDESCPFX)+10];
  75. strcpy(pathbuf,pathbase);
  76. strcpy(pathbuf+baselen,"/");
  77. pathmeth= pathbuf+baselen+1;
  78. dir= opendir(pathbuf);
  79. if (!dir) {
  80. if (errno == ENOENT) {
  81. delete[] pathbuf;
  82. return;
  83. }
  84. ohshite(_("unable to read '%.250s' directory for reading methods"),
  85. pathbuf);
  86. }
  87. debug(dbg_general, "readmethods('%s',...) directory open", pathbase);
  88. while ((dent = readdir(dir)) != nullptr) {
  89. c= dent->d_name[0];
  90. debug(dbg_general, "readmethods('%s',...) considering '%s' ...",
  91. pathbase, dent->d_name);
  92. if (c != '_' && !c_isalpha(c))
  93. continue;
  94. char *p = dent->d_name + 1;
  95. while ((c = *p) != 0 && c_isalnum(c) && c != '_')
  96. p++;
  97. if (c) continue;
  98. methodlen= strlen(dent->d_name);
  99. if (methodlen > IMETHODMAXLEN)
  100. ohshit(_("method '%.250s' has name that is too long (%d > %d characters)"),
  101. dent->d_name, methodlen, IMETHODMAXLEN);
  102. /* Check if there is a localized version of this method */
  103. strcpy(pathmeth, dent->d_name);
  104. strcpy(pathmeth+methodlen, "/");
  105. pathinmeth= pathmeth+methodlen+1;
  106. for (ccpp= methodprograms; *ccpp; ccpp++) {
  107. strcpy(pathinmeth,*ccpp);
  108. if (access(pathbuf,R_OK|X_OK))
  109. ohshite(_("unable to access method script '%.250s'"), pathbuf);
  110. }
  111. debug(dbg_general, " readmethods('%s',...) scripts ok", pathbase);
  112. strcpy(pathinmeth,METHODOPTIONSFILE);
  113. names= fopen(pathbuf,"r");
  114. if (!names)
  115. ohshite(_("unable to read method options file '%.250s'"), pathbuf);
  116. meth= new method;
  117. meth->name= new char[strlen(dent->d_name)+1];
  118. strcpy(meth->name,dent->d_name);
  119. meth->path= new char[baselen+1+methodlen+2+50];
  120. strncpy(meth->path,pathbuf,baselen+1+methodlen);
  121. strcpy(meth->path+baselen+1+methodlen,"/");
  122. meth->pathinmeth= meth->path+baselen+1+methodlen+1;
  123. meth->next= methods;
  124. meth->prev = nullptr;
  125. if (methods)
  126. methods->prev = meth;
  127. methods= meth;
  128. debug(dbg_general, " readmethods('%s',...) new method"
  129. " name='%s' path='%s' pathinmeth='%s'",
  130. pathbase, meth->name, meth->path, meth->pathinmeth);
  131. while ((c= fgetc(names)) != EOF) {
  132. if (c_isspace(c))
  133. continue;
  134. opt= new dselect_option;
  135. opt->meth= meth;
  136. vb.reset();
  137. do {
  138. if (!c_isdigit(c))
  139. badmethod(pathbuf, _("non-digit where digit wanted"));
  140. vb(c);
  141. c= fgetc(names);
  142. if (c == EOF)
  143. eofmethod(pathbuf, names, _("end of file in index string"));
  144. } while (!c_isspace(c));
  145. if (strlen(vb.string()) > OPTIONINDEXMAXLEN)
  146. badmethod(pathbuf,_("index string too long"));
  147. strcpy(opt->index,vb.string());
  148. do {
  149. if (c == '\n') badmethod(pathbuf,_("newline before option name start"));
  150. c= fgetc(names);
  151. if (c == EOF)
  152. eofmethod(pathbuf, names, _("end of file before option name start"));
  153. } while (c_isspace(c));
  154. vb.reset();
  155. if (!c_isalpha(c) && c != '_')
  156. badmethod(pathbuf,_("nonalpha where option name start wanted"));
  157. do {
  158. if (!c_isalnum(c) && c != '_')
  159. badmethod(pathbuf, _("non-alphanum in option name"));
  160. vb(c);
  161. c= fgetc(names);
  162. if (c == EOF)
  163. eofmethod(pathbuf, names, _("end of file in option name"));
  164. } while (!c_isspace(c));
  165. opt->name= new char[strlen(vb.string())+1];
  166. strcpy(opt->name,vb.string());
  167. do {
  168. if (c == '\n') badmethod(pathbuf,_("newline before summary"));
  169. c= fgetc(names);
  170. if (c == EOF)
  171. eofmethod(pathbuf, names, _("end of file before summary"));
  172. } while (c_isspace(c));
  173. vb.reset();
  174. do {
  175. vb(c);
  176. c= fgetc(names);
  177. if (c == EOF)
  178. eofmethod(pathbuf, names, _("end of file in summary - missing newline"));
  179. } while (c != '\n');
  180. opt->summary= new char[strlen(vb.string())+1];
  181. strcpy(opt->summary,vb.string());
  182. strcpy(pathinmeth,OPTIONSDESCPFX);
  183. strcpy(pathinmeth+sizeof(OPTIONSDESCPFX)-1,opt->name);
  184. descfile= fopen(pathbuf,"r");
  185. if (!descfile) {
  186. if (errno != ENOENT)
  187. ohshite(_("unable to open option description file '%.250s'"), pathbuf);
  188. opt->description = nullptr;
  189. } else { /* descfile != 0 */
  190. if (fstat(fileno(descfile),&stab))
  191. ohshite(_("unable to stat option description file '%.250s'"), pathbuf);
  192. opt->description= new char[stab.st_size+1]; errno=0;
  193. size_t filelen = stab.st_size;
  194. if (fread(opt->description,1,stab.st_size+1,descfile) != filelen)
  195. ohshite(_("failed to read option description file '%.250s'"), pathbuf);
  196. opt->description[stab.st_size]= 0;
  197. if (ferror(descfile))
  198. ohshite(_("error during read of option description file '%.250s'"), pathbuf);
  199. fclose(descfile);
  200. }
  201. strcpy(pathinmeth,METHODOPTIONSFILE);
  202. debug(dbg_general,
  203. " readmethods('%s',...) new option index='%s' name='%s'"
  204. " summary='%.20s' strlen(description=%s)=%zu method name='%s'"
  205. " path='%s' pathinmeth='%s'",
  206. pathbase,
  207. opt->index, opt->name, opt->summary,
  208. opt->description ? "'...'" : "null",
  209. opt->description ? strlen(opt->description) : -1,
  210. opt->meth->name, opt->meth->path, opt->meth->pathinmeth);
  211. dselect_option **optinsert = optionspp;
  212. while (*optinsert && strcmp(opt->index, (*optinsert)->index) > 0)
  213. optinsert = &(*optinsert)->next;
  214. opt->next= *optinsert;
  215. *optinsert= opt;
  216. (*nread)++;
  217. }
  218. if (ferror(names))
  219. ohshite(_("error during read of method options file '%.250s'"), pathbuf);
  220. fclose(names);
  221. }
  222. closedir(dir);
  223. debug(dbg_general, "readmethods('%s',...) done", pathbase);
  224. delete[] pathbuf;
  225. }
  226. static char *methoptfile = nullptr;
  227. void getcurrentopt() {
  228. char methoptbuf[IMETHODMAXLEN+1+IOPTIONMAXLEN+2];
  229. FILE *cmo;
  230. int l;
  231. char *p;
  232. if (methoptfile == nullptr)
  233. methoptfile = dpkg_db_get_path(CMETHOPTFILE);
  234. coption = nullptr;
  235. cmo= fopen(methoptfile,"r");
  236. if (!cmo) {
  237. if (errno == ENOENT) return;
  238. ohshite(_("unable to open current option file '%.250s'"), methoptfile);
  239. }
  240. debug(dbg_general, "getcurrentopt() cmethopt open");
  241. if (!fgets(methoptbuf,sizeof(methoptbuf),cmo)) { fclose(cmo); return; }
  242. if (fgetc(cmo) != EOF) { fclose(cmo); return; }
  243. if (!feof(cmo)) { fclose(cmo); return; }
  244. debug(dbg_general, "getcurrentopt() cmethopt eof");
  245. fclose(cmo);
  246. debug(dbg_general, "getcurrentopt() cmethopt read");
  247. l= strlen(methoptbuf); if (!l || methoptbuf[l-1] != '\n') return;
  248. methoptbuf[--l]= 0;
  249. debug(dbg_general, "getcurrentopt() cmethopt len and newline");
  250. p= strchr(methoptbuf,' '); if (!p) return;
  251. debug(dbg_general, "getcurrentopt() cmethopt space");
  252. *p++= 0;
  253. debug(dbg_general, "getcurrentopt() cmethopt meth name '%s'", methoptbuf);
  254. method *meth = methods;
  255. while (meth && strcmp(methoptbuf, meth->name))
  256. meth = meth->next;
  257. if (!meth) return;
  258. debug(dbg_general, "getcurrentopt() cmethopt meth found; opt '%s'", p);
  259. dselect_option *opt = options;
  260. while (opt && (opt->meth != meth || strcmp(p, opt->name)))
  261. opt = opt->next;
  262. if (!opt) return;
  263. debug(dbg_general, "getcurrentopt() cmethopt opt found");
  264. coption= opt;
  265. }
  266. void writecurrentopt() {
  267. struct atomic_file *file;
  268. assert(methoptfile);
  269. file = atomic_file_new(methoptfile, (enum atomic_file_flags)0);
  270. atomic_file_open(file);
  271. if (fprintf(file->fp, "%s %s\n", coption->meth->name, coption->name) == EOF)
  272. ohshite(_("unable to write new option to '%.250s'"), file->name_new);
  273. atomic_file_close(file);
  274. atomic_file_commit(file);
  275. atomic_file_free(file);
  276. }