[BACK]Return to logmsg.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/logmsg.c, Revision 1.6

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