methparse.cc 9.8 KB

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