Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.2
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:
1.2 ! jfb 42: #define CVS_LOGMSG_TERMWIDTH 80
1.1 jfb 43: #define CVS_LOGMSG_BIGMSG 32000
44: #define CVS_LOGMSG_FTMPL "/tmp/cvsXXXXXXXXXX"
45: #define CVS_LOGMSG_LOGPREFIX "CVS:"
46: #define CVS_LOGMSG_LOGLINE \
47: "----------------------------------------------------------------------"
48:
49:
50: /*
51: * cvs_logmsg_open()
52: *
53: * Open the file specified by <path> and allocate a buffer large enough to
54: * hold all of the file's contents. Lines starting with the log prefix
55: * are not included in the result.
56: * The returned value must later be free()d.
57: * Returns a pointer to the allocated buffer on success, or NULL on failure.
58: */
59:
60: char*
61: cvs_logmsg_open(const char *path)
62: {
63: int lcont;
64: size_t len;
65: char lbuf[256], *msg;
66: struct stat st;
67: FILE *fp;
68: BUF *bp;
69:
70: if (stat(path, &st) == -1) {
71: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
72: return (NULL);
73: }
74:
75: if (!S_ISREG(st.st_mode)) {
76: cvs_log(LP_ERR, "message file must be a regular file");
77: return (NULL);
78: }
79:
80: if (st.st_size > CVS_LOGMSG_BIGMSG) {
81: do {
82: fprintf(stderr,
83: "The specified message file seems big. "
84: "Proceed anyways? (y/n) ");
85: if (fgets(lbuf, sizeof(lbuf), stdin) == NULL) {
86: cvs_log(LP_ERRNO,
87: "failed to read from standard input");
88: return (NULL);
89: }
90:
91: len = strlen(lbuf);
92: if ((len == 0) || (len > 2) ||
93: ((lbuf[0] != 'y') && (lbuf[0] != 'n'))) {
94: fprintf(stderr, "invalid input\n");
95: continue;
96: }
97: else if (lbuf[0] == 'y')
98: break;
99: else if (lbuf[0] == 'n') {
100: cvs_log(LP_ERR, "aborted by user");
101: return (NULL);
102: }
103:
104: } while (1);
105: }
106:
107: if ((fp = fopen(path, "r")) == NULL) {
108: cvs_log(LP_ERRNO, "failed to open message file `%s'", path);
109: return (NULL);
110: }
111:
112: bp = cvs_buf_alloc(128, BUF_AUTOEXT);
113: if (bp == NULL) {
114: return (NULL);
115: }
116:
117: /* lcont is used to tell if a buffer returned by fgets is a start
118: * of line or just line continuation because the buffer isn't
119: * large enough to hold the entire line.
120: */
121: lcont = 0;
122:
123: while (fgets(lbuf, sizeof(lbuf), fp) != NULL) {
124: len = strlen(lbuf);
125: if (len == 0)
126: continue;
127: else if ((lcont == 0) && (strncmp(lbuf, CVS_LOGMSG_LOGPREFIX,
128: strlen(CVS_LOGMSG_LOGPREFIX)) == 0))
129: /* skip lines starting with the prefix */
130: continue;
131:
132: cvs_buf_append(bp, lbuf, strlen(lbuf));
133:
134: lcont = (lbuf[len - 1] == '\n') ? 0 : 1;
135: }
136: cvs_buf_putc(bp, '\0');
137:
138: msg = (char *)cvs_buf_release(bp);
139:
140: return (msg);
141: }
142:
143:
144: /*
145: * cvs_logmsg_get()
146: *
147: * Get a log message by forking and executing the user's editor.
148: * Returns the message in a dynamically allocated string on success, NULL on
149: * failure.
150: */
151:
152: char*
153: cvs_logmsg_get(const char *dir)
154: {
155: int ret, fd, argc, fds[3];
156: size_t len;
157: char *argv[4], buf[16], path[MAXPATHLEN], *msg;
158: FILE *fp;
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");
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"
183: "%s\n%s Modified Files:\n",
184: CVS_LOGMSG_LOGPREFIX, CVS_LOGMSG_LOGLINE,
185: CVS_LOGMSG_LOGPREFIX, CVS_LOGMSG_LOGPREFIX,
186: CVS_LOGMSG_LOGPREFIX, CVS_LOGMSG_LOGPREFIX,
187: dir, CVS_LOGMSG_LOGPREFIX, CVS_LOGMSG_LOGPREFIX);
188:
189: /* XXX list files here */
190:
191: fprintf(fp, "%s %s\n", CVS_LOGMSG_LOGPREFIX,
192: CVS_LOGMSG_LOGLINE);
193: }
194: (void)fflush(fp);
195:
196: if (fstat(fd, &st1) == -1) {
197: cvs_log(LP_ERRNO, "failed to stat log message file");
198:
199: (void)fclose(fp);
200: if (unlink(path) == -1)
201: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
202: return (NULL);
203: }
204:
205: for (;;) {
206: ret = cvs_exec(argc, argv, fds);
207: if (ret == -1)
208: break;
209: if (fstat(fd, &st2) == -1) {
210: cvs_log(LP_ERRNO, "failed to stat log message file");
211: break;
212: }
213:
214: if (st2.st_mtime != st1.st_mtime) {
215: msg = cvs_logmsg_open(path);
216: break;
217: }
218:
219: /* nothing was entered */
220: fprintf(stderr,
221: "Log message unchanged or not specified\na)bort, "
222: "c)ontinue, e)dit, !)reuse this message unchanged "
223: "for remaining dirs\nAction: (continue) ");
224:
225: if (fgets(buf, sizeof(buf), stdin) == NULL) {
226: cvs_log(LP_ERRNO, "failed to read from standard input");
227: break;
228: }
229:
230: len = strlen(buf);
231: if ((len == 0) || (len > 2)) {
232: fprintf(stderr, "invalid input\n");
233: continue;
234: }
235: else if (buf[0] == 'a') {
236: cvs_log(LP_ERR, "aborted by user");
237: break;
238: } else if ((buf[0] == '\n') || (buf[0] == 'c')) {
239: /* empty message */
240: msg = strdup("");
241: break;
242: } else if (ret == 'e')
243: continue;
244: else if (ret == '!') {
245: /* XXX do something */
246: }
247: }
248:
249: (void)fclose(fp);
250: (void)close(fd);
251:
252: if (unlink(path) == -1)
253: cvs_log(LP_ERRNO, "failed to unlink log file %s", path);
254:
255: return (msg);
1.2 ! jfb 256: }
! 257:
! 258:
! 259: /*
! 260: * cvs_logmsg_send()
! 261: *
! 262: */
! 263:
! 264: int
! 265: cvs_logmsg_send(struct cvsroot *root, const char *msg)
! 266: {
! 267: const char *mp, *np;
! 268:
! 269: for (np = msg;; np = strchr(np, '\n')) {
! 270: if (np == NULL)
! 271: break;
! 272:
! 273: if (cvs_sendarg(root, np, (np == msg) ? 0 : 1) < 0)
! 274: return (-1);
! 275: }
! 276:
! 277: return (0);
1.1 jfb 278: }