[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.8

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