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

1.43    ! tobias      1: /*     $OpenBSD: logmsg.c,v 1.42 2007/08/30 11:07:18 joris Exp $       */
1.1       jfb         2: /*
1.30      joris       3:  * Copyright (c) 2007 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.30      joris       5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        16:  */
                     17:
1.38      otto       18: #include <sys/stat.h>
1.39      xsa        19: #include <sys/types.h>
                     20: #include <sys/wait.h>
1.38      otto       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.39      xsa        24: #include <paths.h>
                     25: #include <signal.h>
1.38      otto       26: #include <string.h>
                     27: #include <unistd.h>
1.1       jfb        28:
                     29: #include "cvs.h"
                     30:
1.30      joris      31: #define CVS_LOGMSG_PREFIX              "CVS:"
                     32: #define CVS_LOGMSG_LINE                \
1.1       jfb        33: "----------------------------------------------------------------------"
                     34:
1.39      xsa        35: int    cvs_logmsg_edit(const char *);
                     36:
1.17      xsa        37: char *
1.30      joris      38: cvs_logmsg_read(const char *path)
1.1       jfb        39: {
1.30      joris      40:        int fd;
                     41:        BUF *bp;
                     42:        FILE *fp;
1.1       jfb        43:        size_t len;
                     44:        struct stat st;
1.30      joris      45:        char *buf, *lbuf;
                     46:
                     47:        if ((fd = open(path, O_RDONLY)) == -1)
                     48:                fatal("cvs_logmsg_read: open %s", strerror(errno));
1.1       jfb        49:
1.30      joris      50:        if (fstat(fd, &st) == -1)
                     51:                fatal("cvs_logmsg_read: fstat %s", strerror(errno));
1.1       jfb        52:
1.24      xsa        53:        if (!S_ISREG(st.st_mode))
1.30      joris      54:                fatal("cvs_logmsg_read: file is not a regular file");
1.1       jfb        55:
1.30      joris      56:        if ((fp = fdopen(fd, "r")) == NULL)
                     57:                fatal("cvs_logmsg_read: fdopen %s", strerror(errno));
1.1       jfb        58:
1.30      joris      59:        lbuf = NULL;
                     60:        bp = cvs_buf_alloc(st.st_size, BUF_AUTOEXT);
                     61:        while ((buf = fgetln(fp, &len))) {
                     62:                if (buf[len - 1] == '\n') {
                     63:                        buf[len - 1] = '\0';
                     64:                } else {
                     65:                        lbuf = xmalloc(len + 1);
1.36      otto       66:                        memcpy(lbuf, buf, len);
                     67:                        lbuf[len] = '\0';
1.30      joris      68:                        buf = lbuf;
                     69:                }
1.1       jfb        70:
1.30      joris      71:                len = strlen(buf);
                     72:
                     73:                if (!strncmp(buf, CVS_LOGMSG_PREFIX,
                     74:                    strlen(CVS_LOGMSG_PREFIX)))
1.1       jfb        75:                        continue;
                     76:
1.30      joris      77:                cvs_buf_append(bp, buf, len);
                     78:                cvs_buf_putc(bp, '\n');
                     79:        }
                     80:
                     81:        if (lbuf != NULL)
                     82:                xfree(lbuf);
1.1       jfb        83:
1.11      jfb        84:        (void)fclose(fp);
                     85:
1.23      xsa        86:        cvs_buf_putc(bp, '\0');
1.30      joris      87:        return (cvs_buf_release(bp));
1.1       jfb        88: }
                     89:
1.17      xsa        90: char *
1.30      joris      91: cvs_logmsg_create(struct cvs_flisthead *added, struct cvs_flisthead *removed,
                     92:        struct cvs_flisthead *modified)
1.1       jfb        93: {
                     94:        FILE *fp;
1.39      xsa        95:        int c, fd, saved_errno;
1.30      joris      96:        struct cvs_filelist *cf;
1.1       jfb        97:        struct stat st1, st2;
1.39      xsa        98:        char *fpath, *logmsg;
1.8       jfb        99:
1.35      xsa       100:        (void)xasprintf(&fpath, "%s/cvsXXXXXXXXXX", cvs_tmpdir);
1.1       jfb       101:
1.30      joris     102:        if ((fd = mkstemp(fpath)) == NULL)
                    103:                fatal("cvs_logmsg_create: mkstemp %s", strerror(errno));
1.33      joris     104:
                    105:        cvs_worklist_add(fpath, &temp_files);
1.1       jfb       106:
1.24      xsa       107:        if ((fp = fdopen(fd, "w")) == NULL) {
1.34      xsa       108:                saved_errno = errno;
1.30      joris     109:                (void)unlink(fpath);
1.34      xsa       110:                fatal("cvs_logmsg_create: fdopen %s", strerror(saved_errno));
1.8       jfb       111:        }
1.1       jfb       112:
1.8       jfb       113:        fprintf(fp, "\n%s %s\n%s Enter Log.  Lines beginning with `%s' are "
                    114:            "removed automatically\n%s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE,
                    115:            CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX);
                    116:
1.31      joris     117:        if (added != NULL && !TAILQ_EMPTY(added)) {
1.30      joris     118:                fprintf(fp, "%s Added Files:", CVS_LOGMSG_PREFIX);
                    119:                TAILQ_FOREACH(cf, added, flist)
                    120:                        fprintf(fp, "\n%s\t%s",
                    121:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    122:                fputs("\n", fp);
                    123:        }
                    124:
1.31      joris     125:        if (removed != NULL && !TAILQ_EMPTY(removed)) {
1.30      joris     126:                fprintf(fp, "%s Removed Files:", CVS_LOGMSG_PREFIX);
                    127:                TAILQ_FOREACH(cf, removed, flist)
                    128:                        fprintf(fp, "\n%s\t%s",
                    129:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    130:                fputs("\n", fp);
                    131:        }
                    132:
1.31      joris     133:        if (modified != NULL && !TAILQ_EMPTY(modified)) {
1.30      joris     134:                fprintf(fp, "%s Modified Files:", CVS_LOGMSG_PREFIX);
                    135:                TAILQ_FOREACH(cf, modified, flist)
                    136:                        fprintf(fp, "\n%s\t%s",
                    137:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    138:                fputs("\n", fp);
                    139:        }
1.1       jfb       140:
1.9       jfb       141:        fprintf(fp, "%s %s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE);
1.1       jfb       142:        (void)fflush(fp);
                    143:
                    144:        if (fstat(fd, &st1) == -1) {
1.34      xsa       145:                saved_errno = errno;
1.30      joris     146:                (void)unlink(fpath);
1.34      xsa       147:                fatal("cvs_logmsg_create: fstat %s", strerror(saved_errno));
1.1       jfb       148:        }
                    149:
1.30      joris     150:        logmsg = NULL;
                    151:
1.1       jfb       152:        for (;;) {
1.41      ray       153:                if (cvs_logmsg_edit(fpath) == -1)
1.1       jfb       154:                        break;
1.8       jfb       155:
1.1       jfb       156:                if (fstat(fd, &st2) == -1) {
1.34      xsa       157:                        saved_errno = errno;
1.30      joris     158:                        (void)unlink(fpath);
1.34      xsa       159:                        fatal("cvs_logmsg_create: fstat %s",
                    160:                            strerror(saved_errno));
1.1       jfb       161:                }
                    162:
1.30      joris     163:                if (st1.st_mtime != st2.st_mtime) {
                    164:                        logmsg = cvs_logmsg_read(fpath);
1.1       jfb       165:                        break;
                    166:                }
                    167:
1.30      joris     168:                printf("\nLog message unchanged or not specified\n"
1.32      jasper    169:                    "a)bort, c)ontinue, e)dit\nAction: (continue) ");
1.30      joris     170:                (void)fflush(stdout);
                    171:
                    172:                c = getc(stdin);
1.43    ! tobias    173:                if (c == EOF || c == 'a') {
1.30      joris     174:                        fatal("Aborted by user");
                    175:                } else if (c == '\n' || c == 'c') {
                    176:                        logmsg = xstrdup("");
1.1       jfb       177:                        break;
1.30      joris     178:                } else if (c == 'e') {
1.1       jfb       179:                        continue;
1.30      joris     180:                } else {
                    181:                        cvs_log(LP_ERR, "invalid input");
1.1       jfb       182:                        continue;
                    183:                }
                    184:        }
                    185:
                    186:        (void)fclose(fp);
1.30      joris     187:        (void)unlink(fpath);
                    188:        xfree(fpath);
1.1       jfb       189:
1.30      joris     190:        return (logmsg);
1.39      xsa       191: }
                    192:
1.41      ray       193: /*
                    194:  * Execute an editor on the specified pathname, which is interpreted
                    195:  * from the shell.  This means flags may be included.
                    196:  *
                    197:  * Returns -1 on error, or the exit value on success.
                    198:  */
1.39      xsa       199: int
                    200: cvs_logmsg_edit(const char *pathname)
                    201: {
                    202:        char *argp[] = {"sh", "-c", NULL, NULL}, *p;
                    203:        sig_t sighup, sigint, sigquit;
                    204:        pid_t pid;
1.40      ray       205:        int saved_errno, st;
1.39      xsa       206:
                    207:        (void)xasprintf(&p, "%s %s", cvs_editor, pathname);
                    208:        argp[2] = p;
                    209:
                    210:        sighup = signal(SIGHUP, SIG_IGN);
                    211:        sigint = signal(SIGINT, SIG_IGN);
                    212:        sigquit = signal(SIGQUIT, SIG_IGN);
1.40      ray       213:        if ((pid = fork()) == -1)
                    214:                goto fail;
1.39      xsa       215:        if (pid == 0) {
                    216:                execv(_PATH_BSHELL, argp);
                    217:                _exit(127);
                    218:        }
1.40      ray       219:        while (waitpid(pid, &st, 0) == -1)
                    220:                if (errno != EINTR)
                    221:                        goto fail;
1.39      xsa       222:        xfree(p);
                    223:        (void)signal(SIGHUP, sighup);
                    224:        (void)signal(SIGINT, sigint);
                    225:        (void)signal(SIGQUIT, sigquit);
1.40      ray       226:        if (!WIFEXITED(st)) {
                    227:                errno = EINTR;
1.39      xsa       228:                return (-1);
                    229:        }
1.40      ray       230:        return (WEXITSTATUS(st));
                    231:
                    232:  fail:
                    233:        saved_errno = errno;
                    234:        (void)signal(SIGHUP, sighup);
                    235:        (void)signal(SIGINT, sigint);
                    236:        (void)signal(SIGQUIT, sigquit);
                    237:        xfree(p);
                    238:        errno = saved_errno;
                    239:        return (-1);
1.1       jfb       240: }