Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.3
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
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
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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:
59: char*
60: cvs_logmsg_open(const char *path)
61: {
62: int lcont;
63: size_t len;
64: char lbuf[256], *msg;
65: struct stat st;
66: FILE *fp;
67: BUF *bp;
68:
69: if (stat(path, &st) == -1) {
70: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
71: return (NULL);
72: }
73:
74: if (!S_ISREG(st.st_mode)) {
75: cvs_log(LP_ERR, "message file must be a regular file");
76: return (NULL);
77: }
78:
79: if (st.st_size > CVS_LOGMSG_BIGMSG) {
80: do {
81: fprintf(stderr,
82: "The specified message file seems big. "
83: "Proceed anyways? (y/n) ");
84: if (fgets(lbuf, sizeof(lbuf), stdin) == NULL) {
85: cvs_log(LP_ERRNO,
86: "failed to read from standard input");
87: return (NULL);
88: }
89:
90: len = strlen(lbuf);
91: if ((len == 0) || (len > 2) ||
92: ((lbuf[0] != 'y') && (lbuf[0] != 'n'))) {
93: fprintf(stderr, "invalid input\n");
94: continue;
95: }
96: else if (lbuf[0] == 'y')
97: break;
98: else if (lbuf[0] == 'n') {
99: cvs_log(LP_ERR, "aborted by user");
100: return (NULL);
101: }
102:
103: } while (1);
104: }
105:
106: if ((fp = fopen(path, "r")) == NULL) {
107: cvs_log(LP_ERRNO, "failed to open message file `%s'", path);
108: return (NULL);
109: }
110:
111: bp = cvs_buf_alloc(128, BUF_AUTOEXT);
112: if (bp == NULL) {
113: return (NULL);
114: }
115:
116: /* lcont is used to tell if a buffer returned by fgets is a start
117: * of line or just line continuation because the buffer isn't
118: * large enough to hold the entire line.
119: */
120: lcont = 0;
121:
122: while (fgets(lbuf, sizeof(lbuf), fp) != NULL) {
123: len = strlen(lbuf);
124: if (len == 0)
125: continue;
1.3 ! jfb 126: else if ((lcont == 0) && (strncmp(lbuf, CVS_LOGMSG_PREFIX,
! 127: strlen(CVS_LOGMSG_PREFIX)) == 0))
1.1 jfb 128: /* skip lines starting with the prefix */
129: continue;
130:
131: cvs_buf_append(bp, lbuf, strlen(lbuf));
132:
133: lcont = (lbuf[len - 1] == '\n') ? 0 : 1;
134: }
135: cvs_buf_putc(bp, '\0');
136:
137: msg = (char *)cvs_buf_release(bp);
138:
139: return (msg);
140: }
141:
142:
143: /*
144: * cvs_logmsg_get()
145: *
146: * Get a log message by forking and executing the user's editor.
147: * Returns the message in a dynamically allocated string on success, NULL on
148: * failure.
149: */
150:
151: char*
1.3 ! jfb 152: cvs_logmsg_get(const char *dir, struct cvs_flist *files)
1.1 jfb 153: {
1.3 ! jfb 154: int ret, fd, argc, fds[3], nl;
! 155: size_t len, tlen;
! 156: char *argv[4], buf[16], path[MAXPATHLEN], fpath[MAXPATHLEN], *msg;
1.1 jfb 157: FILE *fp;
1.3 ! jfb 158: CVSFILE *cvsfp;
1.1 jfb 159: struct stat st1, st2;
160:
161: msg = NULL;
162: fds[0] = -1;
163: fds[1] = -1;
164: fds[2] = -1;
165: strlcpy(path, CVS_LOGMSG_FTMPL, sizeof(path));
166: argc = 0;
167: argv[argc++] = cvs_editor;
168: argv[argc++] = path;
169: argv[argc] = NULL;
170:
171: if ((fd = mkstemp(path)) == -1) {
172: cvs_log(LP_ERRNO, "failed to create temporary file");
173: return (NULL);
174: }
175:
176: fp = fdopen(fd, "w");
177: if (fp == NULL) {
178: cvs_log(LP_ERRNO, "failed to fdopen");
1.3 ! jfb 179: (void)close(fd);
! 180: if (unlink(path) == -1)
! 181: cvs_log(LP_ERRNO, "failed to unlink temporary file");
! 182: return (NULL);
1.1 jfb 183: } else {
184: fprintf(fp,
185: "\n%s %s\n%s Enter Log. Lines beginning with `%s' are "
186: "removed automatically\n%s\n%s Commiting in %s\n"
1.3 ! jfb 187: "%s\n",
! 188: CVS_LOGMSG_PREFIX, CVS_LOGMSG_LOGLINE,
! 189: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX,
! 190: CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX,
! 191: dir, CVS_LOGMSG_PREFIX);
1.1 jfb 192:
193: /* XXX list files here */
1.3 ! jfb 194: fprintf(fp, "%s Modified Files:", CVS_LOGMSG_PREFIX);
! 195: nl = 1;
! 196: TAILQ_FOREACH(cvsfp, files, cf_list) {
! 197: /* take the space into account */
! 198: cvs_file_getpath(cvsfp, fpath, sizeof(fpath));
! 199: len = strlen(fpath) + 1;
! 200: if (tlen + len >= 72)
! 201: nl = 1;
! 202:
! 203: if (nl) {
! 204: fprintf(fp, "\n%s\t", CVS_LOGMSG_PREFIX);
! 205: tlen = 8;
! 206: nl = 0;
! 207: }
! 208:
! 209: fprintf(fp, " %s", fpath);
! 210: tlen += len;
! 211: }
1.1 jfb 212:
1.3 ! jfb 213: fprintf(fp, "\n%s %s\n", CVS_LOGMSG_PREFIX,
1.1 jfb 214: CVS_LOGMSG_LOGLINE);
215: }
216: (void)fflush(fp);
217:
218: if (fstat(fd, &st1) == -1) {
219: cvs_log(LP_ERRNO, "failed to stat log message file");
220:
221: (void)fclose(fp);
222: if (unlink(path) == -1)
223: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
224: return (NULL);
225: }
226:
227: for (;;) {
228: ret = cvs_exec(argc, argv, fds);
229: if (ret == -1)
230: break;
231: if (fstat(fd, &st2) == -1) {
232: cvs_log(LP_ERRNO, "failed to stat log message file");
233: break;
234: }
235:
236: if (st2.st_mtime != st1.st_mtime) {
237: msg = cvs_logmsg_open(path);
238: break;
239: }
240:
241: /* nothing was entered */
242: fprintf(stderr,
243: "Log message unchanged or not specified\na)bort, "
244: "c)ontinue, e)dit, !)reuse this message unchanged "
245: "for remaining dirs\nAction: (continue) ");
246:
247: if (fgets(buf, sizeof(buf), stdin) == NULL) {
248: cvs_log(LP_ERRNO, "failed to read from standard input");
249: break;
250: }
251:
252: len = strlen(buf);
253: if ((len == 0) || (len > 2)) {
254: fprintf(stderr, "invalid input\n");
255: continue;
256: }
257: else if (buf[0] == 'a') {
258: cvs_log(LP_ERR, "aborted by user");
259: break;
260: } else if ((buf[0] == '\n') || (buf[0] == 'c')) {
261: /* empty message */
262: msg = strdup("");
263: break;
264: } else if (ret == 'e')
265: continue;
266: else if (ret == '!') {
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:
286: int
287: cvs_logmsg_send(struct cvsroot *root, const char *msg)
288: {
289: const char *mp, *np;
290:
291: for (np = msg;; np = strchr(np, '\n')) {
292: if (np == NULL)
293: break;
294:
295: if (cvs_sendarg(root, np, (np == msg) ? 0 : 1) < 0)
296: return (-1);
297: }
298:
299: return (0);
1.1 jfb 300: }