/contrib/cvs/src/logmsg.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 988 lines · 666 code · 104 blank · 218 comment · 221 complexity · c590bc013fc8b639e8fe57f0884a2036 MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2005 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 <assert.h>
  16. #include "cvs.h"
  17. #include "getline.h"
  18. static int find_type PROTO((Node * p, void *closure));
  19. static int fmt_proc PROTO((Node * p, void *closure));
  20. static int logfile_write PROTO((const char *repository, const char *filter,
  21. const char *message, FILE * logfp,
  22. List * changes));
  23. static int rcsinfo_proc PROTO((const char *repository, const char *template));
  24. static int title_proc PROTO((Node * p, void *closure));
  25. static int update_logfile_proc PROTO((const char *repository,
  26. const char *filter));
  27. static void setup_tmpfile PROTO((FILE * xfp, char *xprefix, List * changes));
  28. static int editinfo_proc PROTO((const char *repository, const char *template));
  29. static int verifymsg_proc PROTO((const char *repository, const char *script));
  30. static FILE *fp;
  31. static char *str_list;
  32. static char *str_list_format; /* The format for str_list's contents. */
  33. static char *editinfo_editor;
  34. static char *verifymsg_script;
  35. static Ctype type;
  36. /*
  37. * Should the logmsg be re-read during the do_verify phase?
  38. * RereadLogAfterVerify=no|stat|yes
  39. * LOGMSG_REREAD_NEVER - never re-read the logmsg
  40. * LOGMSG_REREAD_STAT - re-read the logmsg only if it has changed
  41. * LOGMSG_REREAD_ALWAYS - always re-read the logmsg
  42. */
  43. int RereadLogAfterVerify = LOGMSG_REREAD_ALWAYS;
  44. /*
  45. * Puts a standard header on the output which is either being prepared for an
  46. * editor session, or being sent to a logfile program. The modified, added,
  47. * and removed files are included (if any) and formatted to look pretty. */
  48. static char *prefix;
  49. static int col;
  50. static char *tag;
  51. static void
  52. setup_tmpfile (xfp, xprefix, changes)
  53. FILE *xfp;
  54. char *xprefix;
  55. List *changes;
  56. {
  57. /* set up statics */
  58. fp = xfp;
  59. prefix = xprefix;
  60. type = T_MODIFIED;
  61. if (walklist (changes, find_type, NULL) != 0)
  62. {
  63. (void) fprintf (fp, "%sModified Files:\n", prefix);
  64. col = 0;
  65. (void) walklist (changes, fmt_proc, NULL);
  66. (void) fprintf (fp, "\n");
  67. if (tag != NULL)
  68. {
  69. free (tag);
  70. tag = NULL;
  71. }
  72. }
  73. type = T_ADDED;
  74. if (walklist (changes, find_type, NULL) != 0)
  75. {
  76. (void) fprintf (fp, "%sAdded Files:\n", prefix);
  77. col = 0;
  78. (void) walklist (changes, fmt_proc, NULL);
  79. (void) fprintf (fp, "\n");
  80. if (tag != NULL)
  81. {
  82. free (tag);
  83. tag = NULL;
  84. }
  85. }
  86. type = T_REMOVED;
  87. if (walklist (changes, find_type, NULL) != 0)
  88. {
  89. (void) fprintf (fp, "%sRemoved Files:\n", prefix);
  90. col = 0;
  91. (void) walklist (changes, fmt_proc, NULL);
  92. (void) fprintf (fp, "\n");
  93. if (tag != NULL)
  94. {
  95. free (tag);
  96. tag = NULL;
  97. }
  98. }
  99. }
  100. /*
  101. * Looks for nodes of a specified type and returns 1 if found
  102. */
  103. static int
  104. find_type (p, closure)
  105. Node *p;
  106. void *closure;
  107. {
  108. struct logfile_info *li = p->data;
  109. if (li->type == type)
  110. return (1);
  111. else
  112. return (0);
  113. }
  114. /*
  115. * Breaks the files list into reasonable sized lines to avoid line wrap...
  116. * all in the name of pretty output. It only works on nodes whose types
  117. * match the one we're looking for
  118. */
  119. static int
  120. fmt_proc (p, closure)
  121. Node *p;
  122. void *closure;
  123. {
  124. struct logfile_info *li;
  125. li = p->data;
  126. if (li->type == type)
  127. {
  128. if (li->tag == NULL
  129. ? tag != NULL
  130. : tag == NULL || strcmp (tag, li->tag) != 0)
  131. {
  132. if (col > 0)
  133. (void) fprintf (fp, "\n");
  134. (void) fputs (prefix, fp);
  135. col = strlen (prefix);
  136. while (col < 6)
  137. {
  138. (void) fprintf (fp, " ");
  139. ++col;
  140. }
  141. if (li->tag == NULL)
  142. (void) fprintf (fp, "No tag");
  143. else
  144. (void) fprintf (fp, "Tag: %s", li->tag);
  145. if (tag != NULL)
  146. free (tag);
  147. tag = xstrdup (li->tag);
  148. /* Force a new line. */
  149. col = 70;
  150. }
  151. if (col == 0)
  152. {
  153. (void) fprintf (fp, "%s\t", prefix);
  154. col = 8;
  155. }
  156. else if (col > 8 && (col + (int) strlen (p->key)) > 70)
  157. {
  158. (void) fprintf (fp, "\n%s\t", prefix);
  159. col = 8;
  160. }
  161. (void) fprintf (fp, "%s ", p->key);
  162. col += strlen (p->key) + 1;
  163. }
  164. return (0);
  165. }
  166. /*
  167. * Builds a temporary file using setup_tmpfile() and invokes the user's
  168. * editor on the file. The header garbage in the resultant file is then
  169. * stripped and the log message is stored in the "message" argument.
  170. *
  171. * If REPOSITORY is non-NULL, process rcsinfo for that repository; if it
  172. * is NULL, use the CVSADM_TEMPLATE file instead. REPOSITORY should be
  173. * NULL when running in client mode.
  174. */
  175. void
  176. do_editor (dir, messagep, repository, changes)
  177. const char *dir;
  178. char **messagep;
  179. const char *repository;
  180. List *changes;
  181. {
  182. static int reuse_log_message = 0;
  183. char *line;
  184. int line_length;
  185. size_t line_chars_allocated;
  186. char *fname;
  187. struct stat pre_stbuf, post_stbuf;
  188. int retcode = 0;
  189. assert (!current_parsed_root->isremote != !repository);
  190. if (noexec || reuse_log_message)
  191. return;
  192. /* Abort creation of temp file if no editor is defined */
  193. if (strcmp (Editor, "") == 0 && !editinfo_editor)
  194. error(1, 0, "no editor defined, must use -e or -m");
  195. /* Create a temporary file */
  196. /* FIXME - It's possible we should be relying on cvs_temp_file to open
  197. * the file here - we get race conditions otherwise.
  198. */
  199. fname = cvs_temp_name ();
  200. again:
  201. if ((fp = CVS_FOPEN (fname, "w+")) == NULL)
  202. error (1, 0, "cannot create temporary file %s", fname);
  203. if (*messagep)
  204. {
  205. (void) fputs (*messagep, fp);
  206. if ((*messagep)[0] == '\0' ||
  207. (*messagep)[strlen (*messagep) - 1] != '\n')
  208. (void) fprintf (fp, "\n");
  209. }
  210. else
  211. (void) fprintf (fp, "\n");
  212. if (repository != NULL)
  213. /* tack templates on if necessary */
  214. (void) Parse_Info (CVSROOTADM_RCSINFO, repository, rcsinfo_proc, 1);
  215. else
  216. {
  217. FILE *tfp;
  218. char buf[1024];
  219. size_t n;
  220. size_t nwrite;
  221. /* Why "b"? */
  222. tfp = CVS_FOPEN (CVSADM_TEMPLATE, "rb");
  223. if (tfp == NULL)
  224. {
  225. if (!existence_error (errno))
  226. error (1, errno, "cannot read %s", CVSADM_TEMPLATE);
  227. }
  228. else
  229. {
  230. while (!feof (tfp))
  231. {
  232. char *p = buf;
  233. n = fread (buf, 1, sizeof buf, tfp);
  234. nwrite = n;
  235. while (nwrite > 0)
  236. {
  237. n = fwrite (p, 1, nwrite, fp);
  238. nwrite -= n;
  239. p += n;
  240. }
  241. if (ferror (tfp))
  242. error (1, errno, "cannot read %s", CVSADM_TEMPLATE);
  243. }
  244. if (fclose (tfp) < 0)
  245. error (0, errno, "cannot close %s", CVSADM_TEMPLATE);
  246. }
  247. }
  248. (void) fprintf (fp,
  249. "%s----------------------------------------------------------------------\n",
  250. CVSEDITPREFIX);
  251. (void) fprintf (fp,
  252. "%sEnter Log. Lines beginning with `%.*s' are removed automatically\n%s\n",
  253. CVSEDITPREFIX, CVSEDITPREFIXLEN, CVSEDITPREFIX,
  254. CVSEDITPREFIX);
  255. if (dir != NULL && *dir)
  256. (void) fprintf (fp, "%sCommitting in %s\n%s\n", CVSEDITPREFIX,
  257. dir, CVSEDITPREFIX);
  258. if (changes != NULL)
  259. setup_tmpfile (fp, CVSEDITPREFIX, changes);
  260. (void) fprintf (fp,
  261. "%s----------------------------------------------------------------------\n",
  262. CVSEDITPREFIX);
  263. /* finish off the temp file */
  264. if (fclose (fp) == EOF)
  265. error (1, errno, "%s", fname);
  266. if ( CVS_STAT (fname, &pre_stbuf) == -1)
  267. pre_stbuf.st_mtime = 0;
  268. if (editinfo_editor)
  269. free (editinfo_editor);
  270. editinfo_editor = (char *) NULL;
  271. if (!current_parsed_root->isremote && repository != NULL)
  272. (void) Parse_Info (CVSROOTADM_EDITINFO, repository, editinfo_proc, 0);
  273. /* run the editor */
  274. run_setup (editinfo_editor ? editinfo_editor : Editor);
  275. run_arg (fname);
  276. if ((retcode = run_exec (RUN_TTY, RUN_TTY, RUN_TTY,
  277. RUN_NORMAL | RUN_SIGIGNORE)) != 0)
  278. error (editinfo_editor ? 1 : 0, retcode == -1 ? errno : 0,
  279. editinfo_editor ? "Logfile verification failed" :
  280. "warning: editor session failed");
  281. /* put the entire message back into the *messagep variable */
  282. fp = open_file (fname, "r");
  283. if (*messagep)
  284. free (*messagep);
  285. if ( CVS_STAT (fname, &post_stbuf) != 0)
  286. error (1, errno, "cannot find size of temp file %s", fname);
  287. if (post_stbuf.st_size == 0)
  288. *messagep = NULL;
  289. else
  290. {
  291. /* On NT, we might read less than st_size bytes, but we won't
  292. read more. So this works. */
  293. *messagep = (char *) xmalloc (post_stbuf.st_size + 1);
  294. (*messagep)[0] = '\0';
  295. }
  296. line = NULL;
  297. line_chars_allocated = 0;
  298. if (*messagep)
  299. {
  300. size_t message_len = post_stbuf.st_size + 1;
  301. size_t offset = 0;
  302. while (1)
  303. {
  304. line_length = getline (&line, &line_chars_allocated, fp);
  305. if (line_length == -1)
  306. {
  307. if (ferror (fp))
  308. error (0, errno, "warning: cannot read %s", fname);
  309. break;
  310. }
  311. if (strncmp (line, CVSEDITPREFIX, CVSEDITPREFIXLEN) == 0)
  312. continue;
  313. if (offset + line_length >= message_len)
  314. expand_string (messagep, &message_len,
  315. offset + line_length + 1);
  316. (void) strcpy (*messagep + offset, line);
  317. offset += line_length;
  318. }
  319. }
  320. if (fclose (fp) < 0)
  321. error (0, errno, "warning: cannot close %s", fname);
  322. /* canonicalize emply messages */
  323. if (*messagep != NULL &&
  324. (**messagep == '\0' || strcmp (*messagep, "\n") == 0))
  325. {
  326. free (*messagep);
  327. *messagep = NULL;
  328. }
  329. if (pre_stbuf.st_mtime == post_stbuf.st_mtime || *messagep == NULL)
  330. {
  331. for (;;)
  332. {
  333. (void) printf ("\nLog message unchanged or not specified\n");
  334. (void) printf ("a)bort, c)ontinue, e)dit, !)reuse this message unchanged for remaining dirs\n");
  335. (void) printf ("Action: (continue) ");
  336. (void) fflush (stdout);
  337. line_length = getline (&line, &line_chars_allocated, stdin);
  338. if (line_length < 0)
  339. {
  340. error (0, errno, "cannot read from stdin");
  341. if (unlink_file (fname) < 0)
  342. error (0, errno,
  343. "warning: cannot remove temp file %s", fname);
  344. error (1, 0, "aborting");
  345. }
  346. else if (line_length == 0
  347. || *line == '\n' || *line == 'c' || *line == 'C')
  348. break;
  349. if (*line == 'a' || *line == 'A')
  350. {
  351. if (unlink_file (fname) < 0)
  352. error (0, errno, "warning: cannot remove temp file %s", fname);
  353. error (1, 0, "aborted by user");
  354. }
  355. if (*line == 'e' || *line == 'E')
  356. goto again;
  357. if (*line == '!')
  358. {
  359. reuse_log_message = 1;
  360. break;
  361. }
  362. (void) printf ("Unknown input\n");
  363. }
  364. }
  365. if (line)
  366. free (line);
  367. if (unlink_file (fname) < 0)
  368. error (0, errno, "warning: cannot remove temp file %s", fname);
  369. free (fname);
  370. }
  371. /* Runs the user-defined verification script as part of the commit or import
  372. process. This verification is meant to be run whether or not the user
  373. included the -m atribute. unlike the do_editor function, this is
  374. independant of the running of an editor for getting a message.
  375. */
  376. void
  377. do_verify (messagep, repository)
  378. char **messagep;
  379. const char *repository;
  380. {
  381. FILE *fp;
  382. char *fname;
  383. int retcode = 0;
  384. struct stat pre_stbuf, post_stbuf;
  385. if (current_parsed_root->isremote)
  386. /* The verification will happen on the server. */
  387. return;
  388. /* FIXME? Do we really want to skip this on noexec? What do we do
  389. for the other administrative files? */
  390. if (noexec || repository == NULL)
  391. return;
  392. /* Get the name of the verification script to run */
  393. if (Parse_Info (CVSROOTADM_VERIFYMSG, repository, verifymsg_proc, 0) > 0)
  394. error (1, 0, "Message verification failed");
  395. if (!verifymsg_script)
  396. return;
  397. /* open a temporary file, write the message to the
  398. temp file, and close the file. */
  399. if ((fp = cvs_temp_file (&fname)) == NULL)
  400. error (1, errno, "cannot create temporary file %s",
  401. fname ? fname : "(null)");
  402. if (*messagep != NULL)
  403. fputs (*messagep, fp);
  404. if (*messagep == NULL ||
  405. (*messagep)[0] == '\0' ||
  406. (*messagep)[strlen (*messagep) - 1] != '\n')
  407. putc ('\n', fp);
  408. if (fclose (fp) == EOF)
  409. error (1, errno, "%s", fname);
  410. if (RereadLogAfterVerify == LOGMSG_REREAD_STAT)
  411. {
  412. /* Remember the status of the temp file for later */
  413. if ( CVS_STAT (fname, &pre_stbuf) != 0 )
  414. error (1, errno, "cannot stat temp file %s", fname);
  415. /*
  416. * See if we need to sleep before running the verification
  417. * script to avoid time-stamp races.
  418. */
  419. sleep_past (pre_stbuf.st_mtime);
  420. }
  421. run_setup (verifymsg_script);
  422. run_arg (fname);
  423. if ((retcode = run_exec (RUN_TTY, RUN_TTY, RUN_TTY,
  424. RUN_NORMAL | RUN_SIGIGNORE)) != 0)
  425. {
  426. /* Since following error() exits, delete the temp file now. */
  427. if (unlink_file (fname) < 0)
  428. error (0, errno, "cannot remove %s", fname);
  429. error (1, retcode == -1 ? errno : 0,
  430. "Message verification failed");
  431. }
  432. /* Get the mod time and size of the possibly new log message
  433. * in always and stat modes.
  434. */
  435. if (RereadLogAfterVerify == LOGMSG_REREAD_ALWAYS ||
  436. RereadLogAfterVerify == LOGMSG_REREAD_STAT)
  437. {
  438. if ( CVS_STAT (fname, &post_stbuf) != 0 )
  439. error (1, errno, "cannot find size of temp file %s", fname);
  440. }
  441. /* And reread the log message in `always' mode or in `stat' mode when it's
  442. * changed
  443. */
  444. if (RereadLogAfterVerify == LOGMSG_REREAD_ALWAYS ||
  445. (RereadLogAfterVerify == LOGMSG_REREAD_STAT &&
  446. (pre_stbuf.st_mtime != post_stbuf.st_mtime ||
  447. pre_stbuf.st_size != post_stbuf.st_size)))
  448. {
  449. /* put the entire message back into the *messagep variable */
  450. if (*messagep) free (*messagep);
  451. if (post_stbuf.st_size == 0)
  452. *messagep = NULL;
  453. else
  454. {
  455. char *line = NULL;
  456. int line_length;
  457. size_t line_chars_allocated = 0;
  458. char *p;
  459. if ( (fp = open_file (fname, "r")) == NULL )
  460. error (1, errno, "cannot open temporary file %s", fname);
  461. /* On NT, we might read less than st_size bytes,
  462. but we won't read more. So this works. */
  463. p = *messagep = (char *) xmalloc (post_stbuf.st_size + 1);
  464. *messagep[0] = '\0';
  465. while (1)
  466. {
  467. line_length = getline (&line,
  468. &line_chars_allocated,
  469. fp);
  470. if (line_length == -1)
  471. {
  472. if (ferror (fp))
  473. /* Fail in this case because otherwise we will have no
  474. * log message
  475. */
  476. error (1, errno, "cannot read %s", fname);
  477. break;
  478. }
  479. if (strncmp (line, CVSEDITPREFIX, CVSEDITPREFIXLEN) == 0)
  480. continue;
  481. (void) strcpy (p, line);
  482. p += line_length;
  483. }
  484. if (line) free (line);
  485. if (fclose (fp) < 0)
  486. error (0, errno, "warning: cannot close %s", fname);
  487. }
  488. }
  489. /* Delete the temp file */
  490. if (unlink_file (fname) < 0)
  491. error (0, errno, "cannot remove %s", fname);
  492. free (fname);
  493. free (verifymsg_script);
  494. verifymsg_script = NULL;
  495. }
  496. /*
  497. * callback proc for Parse_Info for rcsinfo templates this routine basically
  498. * copies the matching template onto the end of the tempfile we are setting
  499. * up
  500. */
  501. /* ARGSUSED */
  502. static int
  503. rcsinfo_proc (repository, template)
  504. const char *repository;
  505. const char *template;
  506. {
  507. static char *last_template;
  508. FILE *tfp;
  509. /* nothing to do if the last one included is the same as this one */
  510. if (last_template && strcmp (last_template, template) == 0)
  511. return (0);
  512. if (last_template)
  513. free (last_template);
  514. last_template = xstrdup (template);
  515. if ((tfp = CVS_FOPEN (template, "r")) != NULL)
  516. {
  517. char *line = NULL;
  518. size_t line_chars_allocated = 0;
  519. while (getline (&line, &line_chars_allocated, tfp) >= 0)
  520. (void) fputs (line, fp);
  521. if (ferror (tfp))
  522. error (0, errno, "warning: cannot read %s", template);
  523. if (fclose (tfp) < 0)
  524. error (0, errno, "warning: cannot close %s", template);
  525. if (line)
  526. free (line);
  527. return (0);
  528. }
  529. else
  530. {
  531. error (0, errno, "Couldn't open rcsinfo template file %s", template);
  532. return (1);
  533. }
  534. }
  535. /*
  536. * Uses setup_tmpfile() to pass the updated message on directly to any
  537. * logfile programs that have a regular expression match for the checked in
  538. * directory in the source repository. The log information is fed into the
  539. * specified program as standard input.
  540. */
  541. static FILE *logfp;
  542. static const char *message;
  543. static List *changes;
  544. void
  545. Update_Logfile (repository, xmessage, xlogfp, xchanges)
  546. const char *repository;
  547. const char *xmessage;
  548. FILE *xlogfp;
  549. List *xchanges;
  550. {
  551. /* nothing to do if the list is empty */
  552. if (xchanges == NULL || xchanges->list->next == xchanges->list)
  553. return;
  554. /* set up static vars for update_logfile_proc */
  555. message = xmessage;
  556. logfp = xlogfp;
  557. changes = xchanges;
  558. /* call Parse_Info to do the actual logfile updates */
  559. (void) Parse_Info (CVSROOTADM_LOGINFO, repository, update_logfile_proc, 1);
  560. }
  561. /*
  562. * callback proc to actually do the logfile write from Update_Logfile
  563. */
  564. static int
  565. update_logfile_proc (repository, filter)
  566. const char *repository;
  567. const char *filter;
  568. {
  569. return logfile_write (repository, filter, message, logfp, changes);
  570. }
  571. /*
  572. * concatenate each filename/version onto str_list
  573. */
  574. static int
  575. title_proc (p, closure)
  576. Node *p;
  577. void *closure;
  578. {
  579. char *c;
  580. struct logfile_info *li = p->data;
  581. if (li->type == type)
  582. {
  583. /* Until we decide on the correct logging solution when we add
  584. directories or perform imports, T_TITLE nodes will only
  585. tack on the name provided, regardless of the format string.
  586. You can verify that this assumption is safe by checking the
  587. code in add.c (add_directory) and import.c (import). */
  588. str_list = xrealloc (str_list, strlen (str_list) + 5);
  589. (void) strcat (str_list, " ");
  590. if (li->type == T_TITLE)
  591. {
  592. str_list = xrealloc (str_list,
  593. strlen (str_list) + strlen (p->key) + 5);
  594. (void) strcat (str_list, p->key);
  595. }
  596. else
  597. {
  598. /* All other nodes use the format string. */
  599. for (c = str_list_format; *c != '\0'; c++)
  600. {
  601. switch (*c)
  602. {
  603. case 's':
  604. str_list =
  605. xrealloc (str_list,
  606. strlen (str_list) + strlen (p->key) + 5);
  607. (void) strcat (str_list, p->key);
  608. break;
  609. case 'V':
  610. str_list =
  611. xrealloc (str_list,
  612. (strlen (str_list)
  613. + (li->rev_old ? strlen (li->rev_old) : 0)
  614. + 10)
  615. );
  616. (void) strcat (str_list, (li->rev_old
  617. ? li->rev_old : "NONE"));
  618. break;
  619. case 'v':
  620. str_list =
  621. xrealloc (str_list,
  622. (strlen (str_list)
  623. + (li->rev_new ? strlen (li->rev_new) : 0)
  624. + 10)
  625. );
  626. (void) strcat (str_list, (li->rev_new
  627. ? li->rev_new : "NONE"));
  628. break;
  629. /* All other characters, we insert an empty field (but
  630. we do put in the comma separating it from other
  631. fields). This way if future CVS versions add formatting
  632. characters, one can write a loginfo file which at least
  633. won't blow up on an old CVS. */
  634. /* Note that people who have to deal with spaces in file
  635. and directory names are using space to get a known
  636. delimiter for the directory name, so it's probably
  637. not a good idea to ever define that as a formatting
  638. character. */
  639. }
  640. if (*(c + 1) != '\0')
  641. {
  642. str_list = xrealloc (str_list, strlen (str_list) + 5);
  643. (void) strcat (str_list, ",");
  644. }
  645. }
  646. }
  647. }
  648. return (0);
  649. }
  650. /*
  651. * Writes some stuff to the logfile "filter" and returns the status of the
  652. * filter program.
  653. */
  654. static int
  655. logfile_write (repository, filter, message, logfp, changes)
  656. const char *repository;
  657. const char *filter;
  658. const char *message;
  659. FILE *logfp;
  660. List *changes;
  661. {
  662. FILE *pipefp;
  663. char *prog;
  664. char *cp;
  665. int c;
  666. int pipestatus;
  667. char *fmt_percent; /* the location of the percent sign
  668. that starts the format string. */
  669. assert (repository);
  670. /* The user may specify a format string as part of the filter.
  671. Originally, `%s' was the only valid string. The string that
  672. was substituted for it was:
  673. <repository-name> <file1> <file2> <file3> ...
  674. Each file was either a new directory/import (T_TITLE), or a
  675. added (T_ADDED), modified (T_MODIFIED), or removed (T_REMOVED)
  676. file.
  677. It is desirable to preserve that behavior so lots of commitlog
  678. scripts won't die when they get this new code. At the same
  679. time, we'd like to pass other information about the files (like
  680. version numbers, statuses, or checkin times).
  681. The solution is to allow a format string that allows us to
  682. specify those other pieces of information. The format string
  683. will be composed of `%' followed by a single format character,
  684. or followed by a set of format characters surrounded by `{' and
  685. `}' as separators. The format characters are:
  686. s = file name
  687. V = old version number (pre-checkin)
  688. v = new version number (post-checkin)
  689. For example, valid format strings are:
  690. %{}
  691. %s
  692. %{s}
  693. %{sVv}
  694. There's no reason that more items couldn't be added (like
  695. modification date or file status [added, modified, updated,
  696. etc.]) -- the code modifications would be minimal (logmsg.c
  697. (title_proc) and commit.c (check_fileproc)).
  698. The output will be a string of tokens separated by spaces. For
  699. backwards compatibility, the the first token will be the
  700. repository name. The rest of the tokens will be
  701. comma-delimited lists of the information requested in the
  702. format string. For example, if `/u/src/master' is the
  703. repository, `%{sVv}' is the format string, and three files
  704. (ChangeLog, Makefile, foo.c) were modified, the output might
  705. be:
  706. /u/src/master ChangeLog,1.1,1.2 Makefile,1.3,1.4 foo.c,1.12,1.13
  707. Why this duplicates the old behavior when the format string is
  708. `%s' is left as an exercise for the reader. */
  709. fmt_percent = strchr (filter, '%');
  710. if (fmt_percent)
  711. {
  712. int len;
  713. const char *srepos;
  714. char *fmt_begin, *fmt_end; /* beginning and end of the
  715. format string specified in
  716. filter. */
  717. char *fmt_continue; /* where the string continues
  718. after the format string (we
  719. might skip a '}') somewhere
  720. in there... */
  721. /* Grab the format string. */
  722. if ((*(fmt_percent + 1) == ' ') || (*(fmt_percent + 1) == '\0'))
  723. {
  724. /* The percent stands alone. This is an error. We could
  725. be treating ' ' like any other formatting character, but
  726. using it as a formatting character seems like it would be
  727. a mistake. */
  728. /* Would be nice to also be giving the line number. */
  729. error (0, 0, "loginfo: '%%' not followed by formatting character");
  730. fmt_begin = fmt_percent + 1;
  731. fmt_end = fmt_begin;
  732. fmt_continue = fmt_begin;
  733. }
  734. else if (*(fmt_percent + 1) == '{')
  735. {
  736. /* The percent has a set of characters following it. */
  737. fmt_begin = fmt_percent + 2;
  738. fmt_end = strchr (fmt_begin, '}');
  739. if (fmt_end)
  740. {
  741. /* Skip over the '}' character. */
  742. fmt_continue = fmt_end + 1;
  743. }
  744. else
  745. {
  746. /* There was no close brace -- assume that format
  747. string continues to the end of the line. */
  748. /* Would be nice to also be giving the line number. */
  749. error (0, 0, "loginfo: '}' missing");
  750. fmt_end = fmt_begin + strlen (fmt_begin);
  751. fmt_continue = fmt_end;
  752. }
  753. }
  754. else
  755. {
  756. /* The percent has a single character following it. FIXME:
  757. %% should expand to a regular percent sign. */
  758. fmt_begin = fmt_percent + 1;
  759. fmt_end = fmt_begin + 1;
  760. fmt_continue = fmt_end;
  761. }
  762. len = fmt_end - fmt_begin;
  763. str_list_format = xmalloc (len + 1);
  764. strncpy (str_list_format, fmt_begin, len);
  765. str_list_format[len] = '\0';
  766. /* Allocate an initial chunk of memory. As we build up the string
  767. we will realloc it. */
  768. if (!str_list)
  769. str_list = xmalloc (1);
  770. str_list[0] = '\0';
  771. /* Add entries to the string. Don't bother looking for
  772. entries if the format string is empty. */
  773. if (str_list_format[0] != '\0')
  774. {
  775. type = T_TITLE;
  776. (void) walklist (changes, title_proc, NULL);
  777. type = T_ADDED;
  778. (void) walklist (changes, title_proc, NULL);
  779. type = T_MODIFIED;
  780. (void) walklist (changes, title_proc, NULL);
  781. type = T_REMOVED;
  782. (void) walklist (changes, title_proc, NULL);
  783. }
  784. free (str_list_format);
  785. /* Construct the final string. */
  786. srepos = Short_Repository (repository);
  787. prog = cp = xmalloc ((fmt_percent - filter) + 2 * strlen (srepos)
  788. + 2 * strlen (str_list) + strlen (fmt_continue)
  789. + 10);
  790. (void) memcpy (cp, filter, fmt_percent - filter);
  791. cp += fmt_percent - filter;
  792. *cp++ = '"';
  793. cp = shell_escape (cp, srepos);
  794. cp = shell_escape (cp, str_list);
  795. *cp++ = '"';
  796. (void) strcpy (cp, fmt_continue);
  797. /* To be nice, free up some memory. */
  798. free (str_list);
  799. str_list = (char *) NULL;
  800. }
  801. else
  802. {
  803. /* There's no format string. */
  804. prog = xstrdup (filter);
  805. }
  806. if ((pipefp = run_popen (prog, "w")) == NULL)
  807. {
  808. if (!noexec)
  809. error (0, 0, "cannot write entry to log filter: %s", prog);
  810. free (prog);
  811. return (1);
  812. }
  813. (void) fprintf (pipefp, "Update of %s\n", repository);
  814. (void) fprintf (pipefp, "In directory %s:", hostname);
  815. cp = xgetwd ();
  816. if (cp == NULL)
  817. fprintf (pipefp, "<cannot get working directory: %s>\n\n",
  818. strerror (errno));
  819. else
  820. {
  821. fprintf (pipefp, "%s\n\n", cp);
  822. free (cp);
  823. }
  824. setup_tmpfile (pipefp, "", changes);
  825. (void) fprintf (pipefp, "Log Message:\n%s\n", (message) ? message : "");
  826. if (logfp != (FILE *) 0)
  827. {
  828. (void) fprintf (pipefp, "Status:\n");
  829. rewind (logfp);
  830. while ((c = getc (logfp)) != EOF)
  831. (void) putc ((char) c, pipefp);
  832. }
  833. free (prog);
  834. pipestatus = pclose (pipefp);
  835. return ((pipestatus == -1) || (pipestatus == 127)) ? 1 : 0;
  836. }
  837. /*
  838. * We choose to use the *last* match within the editinfo file for this
  839. * repository. This allows us to have a global editinfo program for the
  840. * root of some hierarchy, for example, and different ones within different
  841. * sub-directories of the root (like a special checker for changes made to
  842. * the "src" directory versus changes made to the "doc" or "test"
  843. * directories.
  844. */
  845. /* ARGSUSED */
  846. static int
  847. editinfo_proc(repository, editor)
  848. const char *repository;
  849. const char *editor;
  850. {
  851. /* nothing to do if the last match is the same as this one */
  852. if (editinfo_editor && strcmp (editinfo_editor, editor) == 0)
  853. return (0);
  854. if (editinfo_editor)
  855. free (editinfo_editor);
  856. editinfo_editor = xstrdup (editor);
  857. return (0);
  858. }
  859. /* This routine is calld by Parse_Info. it asigns the name of the
  860. * message verification script to the global variable verify_script
  861. */
  862. static int
  863. verifymsg_proc (repository, script)
  864. const char *repository;
  865. const char *script;
  866. {
  867. if (verifymsg_script && strcmp (verifymsg_script, script) == 0)
  868. return (0);
  869. if (verifymsg_script)
  870. free (verifymsg_script);
  871. verifymsg_script = xstrdup (script);
  872. return (0);
  873. }