Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.7
1.7 ! tedu 1: /* $OpenBSD: logmsg.c,v 1.6 2004/12/06 21:03:12 deraadt 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>
33: #include <unistd.h>
34: #include <string.h>
35:
36: #include "cvs.h"
37: #include "log.h"
38: #include "buf.h"
1.2 jfb 39: #include "proto.h"
1.1 jfb 40:
41:
42: #define CVS_LOGMSG_BIGMSG 32000
43: #define CVS_LOGMSG_FTMPL "/tmp/cvsXXXXXXXXXX"
1.3 jfb 44: #define CVS_LOGMSG_PREFIX "CVS:"
1.1 jfb 45: #define CVS_LOGMSG_LOGLINE \
46: "----------------------------------------------------------------------"
47:
48:
49: /*
50: * cvs_logmsg_open()
51: *
52: * Open the file specified by <path> and allocate a buffer large enough to
53: * hold all of the file's contents. Lines starting with the log prefix
54: * are not included in the result.
55: * The returned value must later be free()d.
56: * Returns a pointer to the allocated buffer on success, or NULL on failure.
57: */
58: char*
59: cvs_logmsg_open(const char *path)
60: {
61: int lcont;
62: size_t len;
63: char lbuf[256], *msg;
64: struct stat st;
65: FILE *fp;
66: BUF *bp;
67:
68: if (stat(path, &st) == -1) {
69: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
70: return (NULL);
71: }
72:
73: if (!S_ISREG(st.st_mode)) {
74: cvs_log(LP_ERR, "message file must be a regular file");
75: return (NULL);
76: }
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) ");
83: if (fgets(lbuf, sizeof(lbuf), stdin) == NULL) {
84: cvs_log(LP_ERRNO,
85: "failed to read from standard input");
86: return (NULL);
87: }
88:
89: len = strlen(lbuf);
90: if ((len == 0) || (len > 2) ||
91: ((lbuf[0] != 'y') && (lbuf[0] != 'n'))) {
92: fprintf(stderr, "invalid input\n");
93: continue;
1.7 ! tedu 94: } else if (lbuf[0] == 'y')
1.1 jfb 95: break;
96: else if (lbuf[0] == 'n') {
97: cvs_log(LP_ERR, "aborted by user");
98: return (NULL);
99: }
100:
101: } while (1);
102: }
103:
104: if ((fp = fopen(path, "r")) == NULL) {
105: cvs_log(LP_ERRNO, "failed to open message file `%s'", path);
106: return (NULL);
107: }
108:
109: bp = cvs_buf_alloc(128, BUF_AUTOEXT);
1.7 ! tedu 110: if (bp == NULL)
1.1 jfb 111: return (NULL);
112:
113: /* lcont is used to tell if a buffer returned by fgets is a start
114: * of line or just line continuation because the buffer isn't
115: * large enough to hold the entire line.
116: */
117: lcont = 0;
118:
119: while (fgets(lbuf, sizeof(lbuf), fp) != NULL) {
120: len = strlen(lbuf);
121: if (len == 0)
122: continue;
1.3 jfb 123: else if ((lcont == 0) && (strncmp(lbuf, CVS_LOGMSG_PREFIX,
124: strlen(CVS_LOGMSG_PREFIX)) == 0))
1.1 jfb 125: /* skip lines starting with the prefix */
126: continue;
127:
128: cvs_buf_append(bp, lbuf, strlen(lbuf));
129:
130: lcont = (lbuf[len - 1] == '\n') ? 0 : 1;
131: }
132: cvs_buf_putc(bp, '\0');
133:
134: msg = (char *)cvs_buf_release(bp);
135:
136: return (msg);
137: }
138:
139:
140: /*
141: * cvs_logmsg_get()
142: *
143: * Get a log message by forking and executing the user's editor.
144: * Returns the message in a dynamically allocated string on success, NULL on
145: * failure.
146: */
147: char*
1.3 jfb 148: cvs_logmsg_get(const char *dir, struct cvs_flist *files)
1.1 jfb 149: {
1.3 jfb 150: int ret, fd, argc, fds[3], nl;
151: size_t len, tlen;
152: char *argv[4], buf[16], path[MAXPATHLEN], fpath[MAXPATHLEN], *msg;
1.1 jfb 153: FILE *fp;
1.3 jfb 154: CVSFILE *cvsfp;
1.1 jfb 155: struct stat st1, st2;
156:
157: msg = NULL;
158: fds[0] = -1;
159: fds[1] = -1;
160: fds[2] = -1;
161: strlcpy(path, CVS_LOGMSG_FTMPL, sizeof(path));
162: argc = 0;
163: argv[argc++] = cvs_editor;
164: argv[argc++] = path;
165: argv[argc] = NULL;
166:
167: if ((fd = mkstemp(path)) == -1) {
168: cvs_log(LP_ERRNO, "failed to create temporary file");
169: return (NULL);
170: }
171:
172: fp = fdopen(fd, "w");
173: if (fp == NULL) {
174: cvs_log(LP_ERRNO, "failed to fdopen");
1.3 jfb 175: (void)close(fd);
176: if (unlink(path) == -1)
177: cvs_log(LP_ERRNO, "failed to unlink temporary file");
178: return (NULL);
1.1 jfb 179: } else {
180: fprintf(fp,
181: "\n%s %s\n%s Enter Log. Lines beginning with `%s' are "
182: "removed automatically\n%s\n%s Commiting in %s\n"
1.3 jfb 183: "%s\n",
184: CVS_LOGMSG_PREFIX, CVS_LOGMSG_LOGLINE,
185: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX,
186: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX,
187: dir, CVS_LOGMSG_PREFIX);
1.1 jfb 188:
189: /* XXX list files here */
1.3 jfb 190: fprintf(fp, "%s Modified Files:", CVS_LOGMSG_PREFIX);
191: nl = 1;
192: TAILQ_FOREACH(cvsfp, files, cf_list) {
193: /* take the space into account */
194: cvs_file_getpath(cvsfp, fpath, sizeof(fpath));
195: len = strlen(fpath) + 1;
196: if (tlen + len >= 72)
197: nl = 1;
198:
199: if (nl) {
200: fprintf(fp, "\n%s\t", CVS_LOGMSG_PREFIX);
201: tlen = 8;
202: nl = 0;
203: }
204:
205: fprintf(fp, " %s", fpath);
206: tlen += len;
207: }
1.1 jfb 208:
1.3 jfb 209: fprintf(fp, "\n%s %s\n", CVS_LOGMSG_PREFIX,
1.1 jfb 210: CVS_LOGMSG_LOGLINE);
211: }
212: (void)fflush(fp);
213:
214: if (fstat(fd, &st1) == -1) {
215: cvs_log(LP_ERRNO, "failed to stat log message file");
216:
217: (void)fclose(fp);
218: if (unlink(path) == -1)
219: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
220: return (NULL);
221: }
222:
223: for (;;) {
224: ret = cvs_exec(argc, argv, fds);
225: if (ret == -1)
226: break;
227: if (fstat(fd, &st2) == -1) {
228: cvs_log(LP_ERRNO, "failed to stat log message file");
229: break;
230: }
231:
232: if (st2.st_mtime != st1.st_mtime) {
233: msg = cvs_logmsg_open(path);
234: break;
235: }
236:
237: /* nothing was entered */
238: fprintf(stderr,
239: "Log message unchanged or not specified\na)bort, "
240: "c)ontinue, e)dit, !)reuse this message unchanged "
241: "for remaining dirs\nAction: (continue) ");
242:
243: if (fgets(buf, sizeof(buf), stdin) == NULL) {
244: cvs_log(LP_ERRNO, "failed to read from standard input");
245: break;
246: }
247:
248: len = strlen(buf);
249: if ((len == 0) || (len > 2)) {
250: fprintf(stderr, "invalid input\n");
251: continue;
1.7 ! tedu 252: } else if (buf[0] == 'a') {
1.1 jfb 253: cvs_log(LP_ERR, "aborted by user");
254: break;
255: } else if ((buf[0] == '\n') || (buf[0] == 'c')) {
256: /* empty message */
257: msg = strdup("");
258: break;
259: } else if (ret == 'e')
260: continue;
261: else if (ret == '!') {
262: /* XXX do something */
263: }
264: }
265:
266: (void)fclose(fp);
267: (void)close(fd);
268:
269: if (unlink(path) == -1)
270: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
271:
272: return (msg);
1.2 jfb 273: }
274:
275:
276: /*
277: * cvs_logmsg_send()
278: *
279: */
280: int
281: cvs_logmsg_send(struct cvsroot *root, const char *msg)
282: {
1.5 jfb 283: const char *mp;
284: char *np, buf[256];
1.2 jfb 285:
1.4 jfb 286: if (cvs_sendarg(root, "-m", 0) < 0)
287: return (-1);
1.2 jfb 288:
1.5 jfb 289: for (mp = msg; mp != NULL; mp = strchr(mp, '\n')) {
290: if (*mp == '\n')
291: mp++;
1.4 jfb 292:
293: /* XXX ghetto */
1.5 jfb 294: strlcpy(buf, mp, sizeof(buf));
295: np = strchr(buf, '\n');
296: if (np != NULL)
297: *np = '\0';
298: if (cvs_sendarg(root, buf, (mp == msg) ? 0 : 1) < 0)
1.2 jfb 299: return (-1);
300: }
301:
302: return (0);
1.1 jfb 303: }