Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.23
1.23 ! xsa 1: /* $OpenBSD: logmsg.c,v 1.22 2005/12/10 20:27:45 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.7 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.7 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.7 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.7 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.7 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/stat.h>
29:
30: #include <errno.h>
31: #include <stdio.h>
32: #include <stdlib.h>
1.14 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
1.14 xsa 36: #include "buf.h"
1.1 jfb 37: #include "cvs.h"
38: #include "log.h"
1.2 jfb 39: #include "proto.h"
1.1 jfb 40:
41:
1.18 xsa 42: #define CVS_LOGMSG_BIGMSG 32000
43: #define CVS_LOGMSG_PREFIX "CVS:"
1.8 jfb 44: #define CVS_LOGMSG_LINE \
1.1 jfb 45: "----------------------------------------------------------------------"
46:
47:
1.8 jfb 48: static const char *cvs_logmsg_ops[3] = {
49: "Added", "Modified", "Removed",
50: };
51:
52:
1.1 jfb 53: /*
54: * cvs_logmsg_open()
55: *
56: * Open the file specified by <path> and allocate a buffer large enough to
57: * hold all of the file's contents. Lines starting with the log prefix
58: * are not included in the result.
59: * The returned value must later be free()d.
60: * Returns a pointer to the allocated buffer on success, or NULL on failure.
61: */
1.17 xsa 62: char *
1.1 jfb 63: cvs_logmsg_open(const char *path)
64: {
65: int lcont;
66: size_t len;
67: char lbuf[256], *msg;
68: struct stat st;
69: FILE *fp;
70: BUF *bp;
71:
72: if (stat(path, &st) == -1) {
73: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
74: return (NULL);
75: }
76:
77: if (!S_ISREG(st.st_mode)) {
78: cvs_log(LP_ERR, "message file must be a regular file");
79: return (NULL);
80: }
81:
82: if (st.st_size > CVS_LOGMSG_BIGMSG) {
83: do {
84: fprintf(stderr,
85: "The specified message file seems big. "
86: "Proceed anyways? (y/n) ");
1.20 xsa 87: if (fgets(lbuf, (int)sizeof(lbuf), stdin) == NULL) {
1.1 jfb 88: cvs_log(LP_ERRNO,
89: "failed to read from standard input");
90: return (NULL);
91: }
92:
93: len = strlen(lbuf);
94: if ((len == 0) || (len > 2) ||
95: ((lbuf[0] != 'y') && (lbuf[0] != 'n'))) {
96: fprintf(stderr, "invalid input\n");
97: continue;
1.7 tedu 98: } else if (lbuf[0] == 'y')
1.1 jfb 99: break;
100: else if (lbuf[0] == 'n') {
101: cvs_log(LP_ERR, "aborted by user");
102: return (NULL);
103: }
104:
105: } while (1);
106: }
107:
108: if ((fp = fopen(path, "r")) == NULL) {
109: cvs_log(LP_ERRNO, "failed to open message file `%s'", path);
110: return (NULL);
111: }
112:
1.21 xsa 113: bp = cvs_buf_alloc((size_t)128, BUF_AUTOEXT);
1.11 jfb 114: if (bp == NULL) {
115: (void)fclose(fp);
1.1 jfb 116: return (NULL);
1.11 jfb 117: }
1.1 jfb 118:
119: /* lcont is used to tell if a buffer returned by fgets is a start
120: * of line or just line continuation because the buffer isn't
121: * large enough to hold the entire line.
122: */
123: lcont = 0;
124:
1.20 xsa 125: while (fgets(lbuf, (int)sizeof(lbuf), fp) != NULL) {
1.1 jfb 126: len = strlen(lbuf);
127: if (len == 0)
128: continue;
1.3 jfb 129: else if ((lcont == 0) && (strncmp(lbuf, CVS_LOGMSG_PREFIX,
130: strlen(CVS_LOGMSG_PREFIX)) == 0))
1.1 jfb 131: /* skip lines starting with the prefix */
132: continue;
133:
1.11 jfb 134: if (cvs_buf_append(bp, lbuf, strlen(lbuf)) < 0) {
135: cvs_buf_free(bp);
136: (void)fclose(fp);
137: return (NULL);
138: }
1.1 jfb 139:
140: lcont = (lbuf[len - 1] == '\n') ? 0 : 1;
141: }
1.11 jfb 142: (void)fclose(fp);
143:
1.23 ! xsa 144: cvs_buf_putc(bp, '\0');
1.1 jfb 145:
146: msg = (char *)cvs_buf_release(bp);
147:
148: return (msg);
149: }
150:
151:
152: /*
153: * cvs_logmsg_get()
154: *
1.8 jfb 155: * Get a log message by forking and executing the user's editor. The <dir>
156: * argument is a relative path to the directory for which the log message
1.12 david 157: * applies, and the 3 tail queue arguments contains all the files for which the
1.8 jfb 158: * log message will apply. Any of these arguments can be set to NULL in the
159: * case where there is no information to display.
1.1 jfb 160: * Returns the message in a dynamically allocated string on success, NULL on
161: * failure.
162: */
1.17 xsa 163: char *
1.8 jfb 164: cvs_logmsg_get(const char *dir, struct cvs_flist *added,
165: struct cvs_flist *modified, struct cvs_flist *removed)
1.1 jfb 166: {
1.8 jfb 167: int i, fd, argc, fds[3], nl;
1.3 jfb 168: size_t len, tlen;
169: char *argv[4], buf[16], path[MAXPATHLEN], fpath[MAXPATHLEN], *msg;
1.1 jfb 170: FILE *fp;
1.3 jfb 171: CVSFILE *cvsfp;
1.1 jfb 172: struct stat st1, st2;
1.8 jfb 173: struct cvs_flist *files[3];
174:
175: files[0] = added;
176: files[1] = modified;
177: files[2] = removed;
1.1 jfb 178:
179: msg = NULL;
180: fds[0] = -1;
181: fds[1] = -1;
182: fds[2] = -1;
1.19 xsa 183: strlcpy(path, cvs_tmpdir, sizeof(path));
184: strlcat(path, "/cvsXXXXXXXXXX", sizeof(path));
1.1 jfb 185: argc = 0;
186: argv[argc++] = cvs_editor;
187: argv[argc++] = path;
188: argv[argc] = NULL;
189:
190: if ((fd = mkstemp(path)) == -1) {
191: cvs_log(LP_ERRNO, "failed to create temporary file");
192: return (NULL);
193: }
194:
195: fp = fdopen(fd, "w");
196: if (fp == NULL) {
197: cvs_log(LP_ERRNO, "failed to fdopen");
1.3 jfb 198: (void)close(fd);
199: if (unlink(path) == -1)
200: cvs_log(LP_ERRNO, "failed to unlink temporary file");
201: return (NULL);
1.8 jfb 202: }
1.1 jfb 203:
1.8 jfb 204: fprintf(fp, "\n%s %s\n%s Enter Log. Lines beginning with `%s' are "
205: "removed automatically\n%s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE,
206: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX);
207:
208: if (dir != NULL)
209: fprintf(fp, "%s Commiting in %s\n%s\n", CVS_LOGMSG_PREFIX, dir,
210: CVS_LOGMSG_PREFIX);
211:
212: for (i = 0; i < 3; i++) {
213: if (files[i] == NULL)
1.16 joris 214: continue;
215:
216: if (SIMPLEQ_EMPTY(files[i]))
1.8 jfb 217: continue;
218:
219: fprintf(fp, "%s %s Files:", CVS_LOGMSG_PREFIX,
220: cvs_logmsg_ops[i]);
1.3 jfb 221: nl = 1;
1.13 jfb 222: SIMPLEQ_FOREACH(cvsfp, files[i], cf_list) {
1.3 jfb 223: /* take the space into account */
224: cvs_file_getpath(cvsfp, fpath, sizeof(fpath));
225: len = strlen(fpath) + 1;
226: if (tlen + len >= 72)
227: nl = 1;
228:
229: if (nl) {
230: fprintf(fp, "\n%s\t", CVS_LOGMSG_PREFIX);
231: tlen = 8;
232: nl = 0;
233: }
234:
235: fprintf(fp, " %s", fpath);
236: tlen += len;
237: }
1.9 jfb 238: fputc('\n', fp);
1.1 jfb 239:
240: }
1.9 jfb 241: fprintf(fp, "%s %s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE);
1.1 jfb 242: (void)fflush(fp);
243:
244: if (fstat(fd, &st1) == -1) {
245: cvs_log(LP_ERRNO, "failed to stat log message file");
246:
247: (void)fclose(fp);
248: if (unlink(path) == -1)
249: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
250: return (NULL);
251: }
252:
253: for (;;) {
1.8 jfb 254: if (cvs_exec(argc, argv, fds) < 0)
1.1 jfb 255: break;
1.8 jfb 256:
1.1 jfb 257: if (fstat(fd, &st2) == -1) {
258: cvs_log(LP_ERRNO, "failed to stat log message file");
259: break;
260: }
261:
262: if (st2.st_mtime != st1.st_mtime) {
263: msg = cvs_logmsg_open(path);
264: break;
265: }
266:
267: /* nothing was entered */
268: fprintf(stderr,
1.15 xsa 269: "\nLog message unchanged or not specified\na)bort, "
1.1 jfb 270: "c)ontinue, e)dit, !)reuse this message unchanged "
271: "for remaining dirs\nAction: (continue) ");
272:
1.20 xsa 273: if (fgets(buf, (int)sizeof(buf), stdin) == NULL) {
1.1 jfb 274: cvs_log(LP_ERRNO, "failed to read from standard input");
275: break;
276: }
277:
278: len = strlen(buf);
279: if ((len == 0) || (len > 2)) {
280: fprintf(stderr, "invalid input\n");
281: continue;
1.7 tedu 282: } else if (buf[0] == 'a') {
1.15 xsa 283: cvs_log(LP_ABORT, "aborted by user");
1.1 jfb 284: break;
285: } else if ((buf[0] == '\n') || (buf[0] == 'c')) {
286: /* empty message */
1.22 joris 287: msg = xstrdup("");
1.1 jfb 288: break;
1.8 jfb 289: } else if (buf[0] == 'e')
1.1 jfb 290: continue;
1.8 jfb 291: else if (buf[0] == '!') {
1.1 jfb 292: /* XXX do something */
293: }
294: }
295:
296: (void)fclose(fp);
297: (void)close(fd);
298:
299: if (unlink(path) == -1)
300: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
301:
302: return (msg);
1.2 jfb 303: }
304:
305:
306: /*
307: * cvs_logmsg_send()
308: *
309: */
310: int
311: cvs_logmsg_send(struct cvsroot *root, const char *msg)
312: {
1.5 jfb 313: const char *mp;
314: char *np, buf[256];
1.2 jfb 315:
1.10 jfb 316: if (cvs_sendarg(root, "-m", 0) < 0) {
317: cvs_log(LP_ERR, "failed to send log message");
1.4 jfb 318: return (-1);
1.10 jfb 319: }
1.2 jfb 320:
1.5 jfb 321: for (mp = msg; mp != NULL; mp = strchr(mp, '\n')) {
322: if (*mp == '\n')
323: mp++;
1.4 jfb 324:
325: /* XXX ghetto */
1.5 jfb 326: strlcpy(buf, mp, sizeof(buf));
327: np = strchr(buf, '\n');
328: if (np != NULL)
329: *np = '\0';
1.10 jfb 330: if (cvs_sendarg(root, buf, (mp == msg) ? 0 : 1) < 0) {
331: cvs_log(LP_ERR, "failed to send log message");
1.2 jfb 332: return (-1);
1.10 jfb 333: }
1.2 jfb 334: }
335:
336: return (0);
1.1 jfb 337: }