/contrib/cvs/src/parseinfo.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 511 lines · 354 code · 42 blank · 115 comment · 168 complexity · 1f319c5475ca26303ae7393b82eee7ad MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2008 The Free Software Foundation, Inc.
  3. *
  4. * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
  5. * and others.
  6. *
  7. * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
  8. * Portions Copyright (C) 1989-1992, Brian Berliner
  9. *
  10. * You may distribute under the terms of the GNU General Public License as
  11. * specified in the README file that comes with the CVS source distribution.
  12. *
  13. * $FreeBSD$
  14. */
  15. #include "cvs.h"
  16. #include "getline.h"
  17. #include <assert.h>
  18. #include "history.h"
  19. extern char *logHistory;
  20. /*
  21. * Parse the INFOFILE file for the specified REPOSITORY. Invoke CALLPROC for
  22. * the first line in the file that matches the REPOSITORY, or if ALL != 0, any
  23. * lines matching "ALL", or if no lines match, the last line matching
  24. * "DEFAULT".
  25. *
  26. * Return 0 for success, -1 if there was not an INFOFILE, and >0 for failure.
  27. */
  28. int
  29. Parse_Info (infofile, repository, callproc, all)
  30. const char *infofile;
  31. const char *repository;
  32. CALLPROC callproc;
  33. int all;
  34. {
  35. int err = 0;
  36. FILE *fp_info;
  37. char *infopath;
  38. char *line = NULL;
  39. size_t line_allocated = 0;
  40. char *default_value = NULL;
  41. int default_line = 0;
  42. char *expanded_value;
  43. int callback_done, line_number;
  44. char *cp, *exp, *value;
  45. const char *srepos;
  46. const char *regex_err;
  47. assert (repository);
  48. if (current_parsed_root == NULL)
  49. {
  50. /* XXX - should be error maybe? */
  51. error (0, 0, "CVSROOT variable not set");
  52. return (1);
  53. }
  54. /* find the info file and open it */
  55. infopath = xmalloc (strlen (current_parsed_root->directory)
  56. + strlen (infofile)
  57. + sizeof (CVSROOTADM)
  58. + 3);
  59. (void) sprintf (infopath, "%s/%s/%s", current_parsed_root->directory,
  60. CVSROOTADM, infofile);
  61. fp_info = CVS_FOPEN (infopath, "r");
  62. if (fp_info == NULL)
  63. {
  64. /* If no file, don't do anything special. */
  65. if (!existence_error (errno))
  66. error (0, errno, "cannot open %s", infopath);
  67. free (infopath);
  68. return 0;
  69. }
  70. /* strip off the CVSROOT if repository was absolute */
  71. srepos = Short_Repository (repository);
  72. if (trace)
  73. (void) fprintf (stderr, "%s-> Parse_Info (%s, %s, %s)\n",
  74. #ifdef SERVER_SUPPORT
  75. server_active ? "S" : " ",
  76. #else
  77. "",
  78. #endif
  79. infopath, srepos, all ? "ALL" : "not ALL");
  80. /* search the info file for lines that match */
  81. callback_done = line_number = 0;
  82. while (getline (&line, &line_allocated, fp_info) >= 0)
  83. {
  84. line_number++;
  85. /* skip lines starting with # */
  86. if (line[0] == '#')
  87. continue;
  88. /* skip whitespace at beginning of line */
  89. for (cp = line; *cp && isspace ((unsigned char) *cp); cp++)
  90. ;
  91. /* if *cp is null, the whole line was blank */
  92. if (*cp == '\0')
  93. continue;
  94. /* the regular expression is everything up to the first space */
  95. for (exp = cp; *cp && !isspace ((unsigned char) *cp); cp++)
  96. ;
  97. if (*cp != '\0')
  98. *cp++ = '\0';
  99. /* skip whitespace up to the start of the matching value */
  100. while (*cp && isspace ((unsigned char) *cp))
  101. cp++;
  102. /* no value to match with the regular expression is an error */
  103. if (*cp == '\0')
  104. {
  105. error (0, 0, "syntax error at line %d file %s; ignored",
  106. line_number, infofile);
  107. continue;
  108. }
  109. value = cp;
  110. /* strip the newline off the end of the value */
  111. if ((cp = strrchr (value, '\n')) != NULL)
  112. *cp = '\0';
  113. /*
  114. * At this point, exp points to the regular expression, and value
  115. * points to the value to call the callback routine with. Evaluate
  116. * the regular expression against srepos and callback with the value
  117. * if it matches.
  118. */
  119. /* save the default value so we have it later if we need it */
  120. if (strcmp (exp, "DEFAULT") == 0)
  121. {
  122. if (default_value != NULL)
  123. {
  124. error (0, 0, "Multiple `DEFAULT' lines (%d and %d) in %s file",
  125. default_line, line_number, infofile);
  126. free (default_value);
  127. }
  128. default_value = xstrdup(value);
  129. default_line = line_number;
  130. continue;
  131. }
  132. /*
  133. * For a regular expression of "ALL", do the callback always We may
  134. * execute lots of ALL callbacks in addition to *one* regular matching
  135. * callback or default
  136. */
  137. if (strcmp (exp, "ALL") == 0)
  138. {
  139. if (!all)
  140. error(0, 0, "Keyword `ALL' is ignored at line %d in %s file",
  141. line_number, infofile);
  142. else if ((expanded_value = expand_path (value, infofile,
  143. line_number)) != NULL)
  144. {
  145. err += callproc (repository, expanded_value);
  146. free (expanded_value);
  147. }
  148. else
  149. err++;
  150. continue;
  151. }
  152. if (callback_done)
  153. /* only first matching, plus "ALL"'s */
  154. continue;
  155. /* see if the repository matched this regular expression */
  156. if ((regex_err = re_comp (exp)) != NULL)
  157. {
  158. error (0, 0, "bad regular expression at line %d file %s: %s",
  159. line_number, infofile, regex_err);
  160. continue;
  161. }
  162. if (re_exec (srepos) == 0)
  163. continue; /* no match */
  164. /* it did, so do the callback and note that we did one */
  165. if ((expanded_value = expand_path (value, infofile, line_number)) != NULL)
  166. {
  167. err += callproc (repository, expanded_value);
  168. free (expanded_value);
  169. }
  170. else
  171. err++;
  172. callback_done = 1;
  173. }
  174. if (ferror (fp_info))
  175. error (0, errno, "cannot read %s", infopath);
  176. if (fclose (fp_info) < 0)
  177. error (0, errno, "cannot close %s", infopath);
  178. /* if we fell through and didn't callback at all, do the default */
  179. if (callback_done == 0 && default_value != NULL)
  180. {
  181. if ((expanded_value = expand_path (default_value, infofile, default_line)) != NULL)
  182. {
  183. err += callproc (repository, expanded_value);
  184. free (expanded_value);
  185. }
  186. else
  187. err++;
  188. }
  189. /* free up space if necessary */
  190. if (default_value != NULL)
  191. free (default_value);
  192. free (infopath);
  193. if (line != NULL)
  194. free (line);
  195. return (err);
  196. }
  197. /* Parse the CVS config file. The syntax right now is a bit ad hoc
  198. but tries to draw on the best or more common features of the other
  199. *info files and various unix (or non-unix) config file syntaxes.
  200. Lines starting with # are comments. Settings are lines of the form
  201. KEYWORD=VALUE. There is currently no way to have a multi-line
  202. VALUE (would be nice if there was, probably).
  203. CVSROOT is the $CVSROOT directory
  204. (current_parsed_root->directory might not be set yet, so this
  205. function takes the cvsroot as a function argument).
  206. Returns 0 for success, negative value for failure. Call
  207. error(0, ...) on errors in addition to the return value. */
  208. int
  209. parse_config (cvsroot)
  210. char *cvsroot;
  211. {
  212. char *infopath;
  213. FILE *fp_info;
  214. char *line = NULL;
  215. size_t line_allocated = 0;
  216. size_t len;
  217. char *p;
  218. /* FIXME-reentrancy: If we do a multi-threaded server, this would need
  219. to go to the per-connection data structures. */
  220. static int parsed = 0;
  221. int ignore_unknown_config_keys = 0;
  222. /* Authentication code and serve_root might both want to call us.
  223. Let this happen smoothly. */
  224. if (parsed)
  225. return 0;
  226. parsed = 1;
  227. infopath = xmalloc (strlen (cvsroot)
  228. + sizeof (CVSROOTADM_CONFIG)
  229. + sizeof (CVSROOTADM)
  230. + 10);
  231. if (infopath == NULL)
  232. {
  233. error (0, 0, "out of memory; cannot allocate infopath");
  234. goto error_return;
  235. }
  236. strcpy (infopath, cvsroot);
  237. strcat (infopath, "/");
  238. strcat (infopath, CVSROOTADM);
  239. strcat (infopath, "/");
  240. strcat (infopath, CVSROOTADM_CONFIG);
  241. fp_info = CVS_FOPEN (infopath, "r");
  242. if (fp_info == NULL)
  243. {
  244. /* If no file, don't do anything special. */
  245. if (!existence_error (errno))
  246. {
  247. /* Just a warning message; doesn't affect return
  248. value, currently at least. */
  249. error (0, errno, "cannot open %s", infopath);
  250. }
  251. goto set_defaults_and_return;
  252. }
  253. while (getline (&line, &line_allocated, fp_info) >= 0)
  254. {
  255. /* Skip comments. */
  256. if (line[0] == '#')
  257. continue;
  258. /* At least for the moment we don't skip whitespace at the start
  259. of the line. Too picky? Maybe. But being insufficiently
  260. picky leads to all sorts of confusion, and it is a lot easier
  261. to start out picky and relax it than the other way around.
  262. Is there any kind of written standard for the syntax of this
  263. sort of config file? Anywhere in POSIX for example (I guess
  264. makefiles are sort of close)? Red Hat Linux has a bunch of
  265. these too (with some GUI tools which edit them)...
  266. Along the same lines, we might want a table of keywords,
  267. with various types (boolean, string, &c), as a mechanism
  268. for making sure the syntax is consistent. Any good examples
  269. to follow there (Apache?)? */
  270. /* Strip the trailing newline. There will be one unless we
  271. read a partial line without a newline, and then got end of
  272. file (or error?). */
  273. len = strlen (line) - 1;
  274. if (line[len] == '\n')
  275. line[len] = '\0';
  276. /* Skip blank lines. */
  277. if (line[0] == '\0')
  278. continue;
  279. /* The first '=' separates keyword from value. */
  280. p = strchr (line, '=');
  281. if (p == NULL)
  282. {
  283. /* Probably should be printing line number. */
  284. error (0, 0, "syntax error in %s: line '%s' is missing '='",
  285. infopath, line);
  286. goto error_return;
  287. }
  288. *p++ = '\0';
  289. if (strcmp (line, "RCSBIN") == 0)
  290. {
  291. /* This option used to specify the directory for RCS
  292. executables. But since we don't run them any more,
  293. this is a noop. Silently ignore it so that a
  294. repository can work with either new or old CVS. */
  295. ;
  296. }
  297. else if (strcmp (line, "SystemAuth") == 0)
  298. {
  299. if (strcmp (p, "no") == 0)
  300. #ifdef AUTH_SERVER_SUPPORT
  301. system_auth = 0;
  302. #else
  303. /* Still parse the syntax but ignore the
  304. option. That way the same config file can
  305. be used for local and server. */
  306. ;
  307. #endif
  308. else if (strcmp (p, "yes") == 0)
  309. #ifdef AUTH_SERVER_SUPPORT
  310. system_auth = 1;
  311. #else
  312. ;
  313. #endif
  314. else
  315. {
  316. error (0, 0, "unrecognized value '%s' for SystemAuth", p);
  317. goto error_return;
  318. }
  319. }
  320. else if (strcmp (line, "tag") == 0) {
  321. RCS_setlocalid(p);
  322. }
  323. else if (strcmp (line, "umask") == 0) {
  324. cvsumask = (mode_t)(strtol(p, NULL, 8) & 0777);
  325. }
  326. else if (strcmp (line, "dlimit") == 0) {
  327. #ifdef BSD
  328. #include <sys/resource.h>
  329. struct rlimit rl;
  330. if (getrlimit(RLIMIT_DATA, &rl) != -1) {
  331. rl.rlim_cur = atoi(p);
  332. rl.rlim_cur *= 1024;
  333. (void) setrlimit(RLIMIT_DATA, &rl);
  334. }
  335. #endif /* BSD */
  336. }
  337. else if (strcmp (line, "PreservePermissions") == 0)
  338. {
  339. if (strcmp (p, "no") == 0)
  340. preserve_perms = 0;
  341. else if (strcmp (p, "yes") == 0)
  342. {
  343. #ifdef PRESERVE_PERMISSIONS_SUPPORT
  344. preserve_perms = 1;
  345. #else
  346. error (0, 0, "\
  347. warning: this CVS does not support PreservePermissions");
  348. #endif
  349. }
  350. else
  351. {
  352. error (0, 0, "unrecognized value '%s' for PreservePermissions",
  353. p);
  354. goto error_return;
  355. }
  356. }
  357. else if (strcmp (line, "TopLevelAdmin") == 0)
  358. {
  359. if (strcmp (p, "no") == 0)
  360. top_level_admin = 0;
  361. else if (strcmp (p, "yes") == 0)
  362. top_level_admin = 1;
  363. else
  364. {
  365. error (0, 0, "unrecognized value '%s' for TopLevelAdmin", p);
  366. goto error_return;
  367. }
  368. }
  369. else if (strcmp (line, "LockDir") == 0)
  370. {
  371. if (lock_dir != NULL)
  372. free (lock_dir);
  373. lock_dir = xstrdup (p);
  374. /* Could try some validity checking, like whether we can
  375. opendir it or something, but I don't see any particular
  376. reason to do that now rather than waiting until lock.c. */
  377. }
  378. else if (strcmp (line, "LogHistory") == 0)
  379. {
  380. if (strcmp (p, "all") != 0)
  381. {
  382. if (logHistory) free (logHistory);
  383. logHistory = xstrdup (p);
  384. }
  385. }
  386. else if (strcmp (line, "RereadLogAfterVerify") == 0)
  387. {
  388. if (strcmp (p, "no") == 0 || strcmp (p, "never") == 0)
  389. RereadLogAfterVerify = LOGMSG_REREAD_NEVER;
  390. else if (strcmp (p, "yes") == 0 || strcmp (p, "always") == 0)
  391. RereadLogAfterVerify = LOGMSG_REREAD_ALWAYS;
  392. else if (strcmp (p, "stat") == 0)
  393. RereadLogAfterVerify = LOGMSG_REREAD_STAT;
  394. }
  395. else if (strcmp(line, "LocalKeyword") == 0)
  396. {
  397. /* Recognize cvs-1.12-style keyword control rather than erroring out. */
  398. RCS_setlocalid(p);
  399. }
  400. else if (strcmp(line, "KeywordExpand") == 0)
  401. {
  402. /* Recognize cvs-1.12-style keyword control rather than erroring out. */
  403. RCS_setincexc(p);
  404. }
  405. else if (strcmp (line, "IgnoreUnknownConfigKeys") == 0)
  406. {
  407. if (strcmp (p, "no") == 0 || strcmp (p, "false") == 0
  408. || strcmp (p, "off") == 0 || strcmp (p, "0") == 0)
  409. ignore_unknown_config_keys = 0;
  410. else if (strcmp (p, "yes") == 0 || strcmp (p, "true") == 0
  411. || strcmp (p, "on") == 0 || strcmp (p, "1") == 0)
  412. ignore_unknown_config_keys = 1;
  413. else
  414. {
  415. error (0, 0, "%s: unrecognized value '%s' for '%s'",
  416. infopath, p, line);
  417. goto error_return;
  418. }
  419. }
  420. else if (ignore_unknown_config_keys)
  421. ;
  422. else
  423. {
  424. /* We may be dealing with a keyword which was added in a
  425. subsequent version of CVS. In that case it is a good idea
  426. to complain, as (1) the keyword might enable a behavior like
  427. alternate locking behavior, in which it is dangerous and hard
  428. to detect if some CVS's have it one way and others have it
  429. the other way, (2) in general, having us not do what the user
  430. had in mind when they put in the keyword violates the
  431. principle of least surprise. Note that one corollary is
  432. adding new keywords to your CVSROOT/config file is not
  433. particularly recommended unless you are planning on using
  434. the new features. */
  435. error (0, 0, "%s: unrecognized keyword '%s'",
  436. infopath, line);
  437. goto error_return;
  438. }
  439. }
  440. if (ferror (fp_info))
  441. {
  442. error (0, errno, "cannot read %s", infopath);
  443. goto error_return;
  444. }
  445. if (fclose (fp_info) < 0)
  446. {
  447. error (0, errno, "cannot close %s", infopath);
  448. goto error_return;
  449. }
  450. set_defaults_and_return:
  451. if (!logHistory)
  452. logHistory = xstrdup (ALL_HISTORY_REC_TYPES);
  453. free (infopath);
  454. if (line != NULL)
  455. free (line);
  456. return 0;
  457. error_return:
  458. if (!logHistory)
  459. logHistory = xstrdup (ALL_HISTORY_REC_TYPES);
  460. if (infopath != NULL)
  461. free (infopath);
  462. if (line != NULL)
  463. free (line);
  464. return -1;
  465. }