Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.24
1.24 ! xsa 1: /* $OpenBSD: logmsg.c,v 1.23 2005/12/20 18:17:01 xsa 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:
1.24 ! xsa 72: if (stat(path, &st) == -1)
! 73: fatal("cvs_logmsg_open: stat: `%s': %s", path, strerror(errno));
1.1 jfb 74:
1.24 ! xsa 75: if (!S_ISREG(st.st_mode))
! 76: fatal("cvs_logmsg_open: message file must be a regular file");
1.1 jfb 77:
78: if (st.st_size > CVS_LOGMSG_BIGMSG) {
79: do {
80: fprintf(stderr,
81: "The specified message file seems big. "
82: "Proceed anyways? (y/n) ");
1.24 ! xsa 83: if (fgets(lbuf, (int)sizeof(lbuf), stdin) == NULL)
! 84: fatal("cvs_logmsg_open: fgets failed");
1.1 jfb 85:
86: len = strlen(lbuf);
87: if ((len == 0) || (len > 2) ||
88: ((lbuf[0] != 'y') && (lbuf[0] != 'n'))) {
89: fprintf(stderr, "invalid input\n");
90: continue;
1.7 tedu 91: } else if (lbuf[0] == 'y')
1.1 jfb 92: break;
1.24 ! xsa 93: else if (lbuf[0] == 'n')
! 94: fatal("aborted by user");
1.1 jfb 95:
96: } while (1);
97: }
98:
1.24 ! xsa 99: if ((fp = fopen(path, "r")) == NULL)
! 100: fatal("cvs_logmsg_open: fopen: `%s': %s",
! 101: path, strerror(errno));
1.1 jfb 102:
1.21 xsa 103: bp = cvs_buf_alloc((size_t)128, BUF_AUTOEXT);
1.1 jfb 104:
105: /* lcont is used to tell if a buffer returned by fgets is a start
106: * of line or just line continuation because the buffer isn't
107: * large enough to hold the entire line.
108: */
109: lcont = 0;
110:
1.20 xsa 111: while (fgets(lbuf, (int)sizeof(lbuf), fp) != NULL) {
1.1 jfb 112: len = strlen(lbuf);
113: if (len == 0)
114: continue;
1.3 jfb 115: else if ((lcont == 0) && (strncmp(lbuf, CVS_LOGMSG_PREFIX,
116: strlen(CVS_LOGMSG_PREFIX)) == 0))
1.1 jfb 117: /* skip lines starting with the prefix */
118: continue;
119:
1.24 ! xsa 120: cvs_buf_append(bp, lbuf, strlen(lbuf));
1.1 jfb 121:
122: lcont = (lbuf[len - 1] == '\n') ? 0 : 1;
123: }
1.11 jfb 124: (void)fclose(fp);
125:
1.23 xsa 126: cvs_buf_putc(bp, '\0');
1.1 jfb 127:
128: msg = (char *)cvs_buf_release(bp);
129:
130: return (msg);
131: }
132:
133:
134: /*
135: * cvs_logmsg_get()
136: *
1.8 jfb 137: * Get a log message by forking and executing the user's editor. The <dir>
138: * argument is a relative path to the directory for which the log message
1.12 david 139: * applies, and the 3 tail queue arguments contains all the files for which the
1.8 jfb 140: * log message will apply. Any of these arguments can be set to NULL in the
141: * case where there is no information to display.
1.1 jfb 142: * Returns the message in a dynamically allocated string on success, NULL on
143: * failure.
144: */
1.17 xsa 145: char *
1.8 jfb 146: cvs_logmsg_get(const char *dir, struct cvs_flist *added,
147: struct cvs_flist *modified, struct cvs_flist *removed)
1.1 jfb 148: {
1.8 jfb 149: int i, fd, argc, fds[3], nl;
1.3 jfb 150: size_t len, tlen;
151: char *argv[4], buf[16], path[MAXPATHLEN], fpath[MAXPATHLEN], *msg;
1.1 jfb 152: FILE *fp;
1.3 jfb 153: CVSFILE *cvsfp;
1.1 jfb 154: struct stat st1, st2;
1.8 jfb 155: struct cvs_flist *files[3];
156:
157: files[0] = added;
158: files[1] = modified;
159: files[2] = removed;
1.1 jfb 160:
161: msg = NULL;
162: fds[0] = -1;
163: fds[1] = -1;
164: fds[2] = -1;
1.19 xsa 165: strlcpy(path, cvs_tmpdir, sizeof(path));
166: strlcat(path, "/cvsXXXXXXXXXX", sizeof(path));
1.1 jfb 167: argc = 0;
168: argv[argc++] = cvs_editor;
169: argv[argc++] = path;
170: argv[argc] = NULL;
171:
1.24 ! xsa 172: if ((fd = mkstemp(path)) == -1)
! 173: fatal("cvs_logmsg_get: mkstemp: `%s': %s",
! 174: path, strerror(errno));
1.1 jfb 175:
1.24 ! xsa 176: if ((fp = fdopen(fd, "w")) == NULL) {
1.3 jfb 177: if (unlink(path) == -1)
178: cvs_log(LP_ERRNO, "failed to unlink temporary file");
1.24 ! xsa 179: fatal("cvs_logmsg_get: fdopen failed");
1.8 jfb 180: }
1.1 jfb 181:
1.8 jfb 182: fprintf(fp, "\n%s %s\n%s Enter Log. Lines beginning with `%s' are "
183: "removed automatically\n%s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE,
184: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX);
185:
186: if (dir != NULL)
187: fprintf(fp, "%s Commiting in %s\n%s\n", CVS_LOGMSG_PREFIX, dir,
188: CVS_LOGMSG_PREFIX);
189:
190: for (i = 0; i < 3; i++) {
191: if (files[i] == NULL)
1.16 joris 192: continue;
193:
194: if (SIMPLEQ_EMPTY(files[i]))
1.8 jfb 195: continue;
196:
197: fprintf(fp, "%s %s Files:", CVS_LOGMSG_PREFIX,
198: cvs_logmsg_ops[i]);
1.3 jfb 199: nl = 1;
1.13 jfb 200: SIMPLEQ_FOREACH(cvsfp, files[i], cf_list) {
1.3 jfb 201: /* take the space into account */
202: cvs_file_getpath(cvsfp, fpath, sizeof(fpath));
203: len = strlen(fpath) + 1;
204: if (tlen + len >= 72)
205: nl = 1;
206:
207: if (nl) {
208: fprintf(fp, "\n%s\t", CVS_LOGMSG_PREFIX);
209: tlen = 8;
210: nl = 0;
211: }
212:
213: fprintf(fp, " %s", fpath);
214: tlen += len;
215: }
1.9 jfb 216: fputc('\n', fp);
1.1 jfb 217:
218: }
1.9 jfb 219: fprintf(fp, "%s %s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE);
1.1 jfb 220: (void)fflush(fp);
221:
222: if (fstat(fd, &st1) == -1) {
223: if (unlink(path) == -1)
224: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
1.24 ! xsa 225: fatal("cvs_logmsg_get: fstat failed");
1.1 jfb 226: }
227:
228: for (;;) {
1.8 jfb 229: if (cvs_exec(argc, argv, fds) < 0)
1.1 jfb 230: break;
1.8 jfb 231:
1.1 jfb 232: if (fstat(fd, &st2) == -1) {
233: cvs_log(LP_ERRNO, "failed to stat log message file");
234: break;
235: }
236:
237: if (st2.st_mtime != st1.st_mtime) {
238: msg = cvs_logmsg_open(path);
239: break;
240: }
241:
242: /* nothing was entered */
243: fprintf(stderr,
1.15 xsa 244: "\nLog message unchanged or not specified\na)bort, "
1.1 jfb 245: "c)ontinue, e)dit, !)reuse this message unchanged "
246: "for remaining dirs\nAction: (continue) ");
247:
1.20 xsa 248: if (fgets(buf, (int)sizeof(buf), stdin) == NULL) {
1.1 jfb 249: cvs_log(LP_ERRNO, "failed to read from standard input");
250: break;
251: }
252:
253: len = strlen(buf);
254: if ((len == 0) || (len > 2)) {
255: fprintf(stderr, "invalid input\n");
256: continue;
1.7 tedu 257: } else if (buf[0] == 'a') {
1.15 xsa 258: cvs_log(LP_ABORT, "aborted by user");
1.1 jfb 259: break;
260: } else if ((buf[0] == '\n') || (buf[0] == 'c')) {
261: /* empty message */
1.22 joris 262: msg = xstrdup("");
1.1 jfb 263: break;
1.8 jfb 264: } else if (buf[0] == 'e')
1.1 jfb 265: continue;
1.8 jfb 266: else if (buf[0] == '!') {
1.1 jfb 267: /* XXX do something */
268: }
269: }
270:
271: (void)fclose(fp);
272: (void)close(fd);
273:
274: if (unlink(path) == -1)
275: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
276:
277: return (msg);
1.2 jfb 278: }
279:
280:
281: /*
282: * cvs_logmsg_send()
283: *
284: */
285: int
286: cvs_logmsg_send(struct cvsroot *root, const char *msg)
287: {
1.5 jfb 288: const char *mp;
289: char *np, buf[256];
1.2 jfb 290:
1.24 ! xsa 291: if (cvs_sendarg(root, "-m", 0) < 0)
! 292: fatal("cvs_logmsg_send: cvs_sendarg failed");
1.2 jfb 293:
1.5 jfb 294: for (mp = msg; mp != NULL; mp = strchr(mp, '\n')) {
295: if (*mp == '\n')
296: mp++;
1.4 jfb 297:
298: /* XXX ghetto */
1.5 jfb 299: strlcpy(buf, mp, sizeof(buf));
300: np = strchr(buf, '\n');
301: if (np != NULL)
302: *np = '\0';
1.24 ! xsa 303: if (cvs_sendarg(root, buf, (mp == msg) ? 0 : 1) < 0)
! 304: fatal("cvs_logmsg_send: cvs_sendarg failed");
1.2 jfb 305: }
306:
307: return (0);
1.1 jfb 308: }