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

1.46    ! tobias      1: /*     $OpenBSD: logmsg.c,v 1.45 2007/09/25 10:04:47 chl 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.45      chl        26: #include <stdlib.h>
1.38      otto       27: #include <string.h>
                     28: #include <unistd.h>
1.1       jfb        29:
                     30: #include "cvs.h"
                     31:
1.30      joris      32: #define CVS_LOGMSG_PREFIX              "CVS:"
                     33: #define CVS_LOGMSG_LINE                \
1.1       jfb        34: "----------------------------------------------------------------------"
                     35:
1.39      xsa        36: int    cvs_logmsg_edit(const char *);
                     37:
1.17      xsa        38: char *
1.30      joris      39: cvs_logmsg_read(const char *path)
1.1       jfb        40: {
1.30      joris      41:        int fd;
                     42:        BUF *bp;
                     43:        FILE *fp;
1.1       jfb        44:        size_t len;
                     45:        struct stat st;
1.30      joris      46:        char *buf, *lbuf;
                     47:
                     48:        if ((fd = open(path, O_RDONLY)) == -1)
                     49:                fatal("cvs_logmsg_read: open %s", strerror(errno));
1.1       jfb        50:
1.30      joris      51:        if (fstat(fd, &st) == -1)
                     52:                fatal("cvs_logmsg_read: fstat %s", strerror(errno));
1.1       jfb        53:
1.24      xsa        54:        if (!S_ISREG(st.st_mode))
1.30      joris      55:                fatal("cvs_logmsg_read: file is not a regular file");
1.1       jfb        56:
1.30      joris      57:        if ((fp = fdopen(fd, "r")) == NULL)
                     58:                fatal("cvs_logmsg_read: fdopen %s", strerror(errno));
1.1       jfb        59:
1.30      joris      60:        lbuf = NULL;
1.46    ! tobias     61:        bp = cvs_buf_alloc(st.st_size);
1.30      joris      62:        while ((buf = fgetln(fp, &len))) {
                     63:                if (buf[len - 1] == '\n') {
                     64:                        buf[len - 1] = '\0';
                     65:                } else {
                     66:                        lbuf = xmalloc(len + 1);
1.36      otto       67:                        memcpy(lbuf, buf, len);
                     68:                        lbuf[len] = '\0';
1.30      joris      69:                        buf = lbuf;
                     70:                }
1.1       jfb        71:
1.30      joris      72:                len = strlen(buf);
                     73:
                     74:                if (!strncmp(buf, CVS_LOGMSG_PREFIX,
1.44      tobias     75:                    sizeof(CVS_LOGMSG_PREFIX) - 1))
1.1       jfb        76:                        continue;
                     77:
1.30      joris      78:                cvs_buf_append(bp, buf, len);
                     79:                cvs_buf_putc(bp, '\n');
                     80:        }
                     81:
                     82:        if (lbuf != NULL)
                     83:                xfree(lbuf);
1.1       jfb        84:
1.11      jfb        85:        (void)fclose(fp);
                     86:
1.23      xsa        87:        cvs_buf_putc(bp, '\0');
1.30      joris      88:        return (cvs_buf_release(bp));
1.1       jfb        89: }
                     90:
1.17      xsa        91: char *
1.30      joris      92: cvs_logmsg_create(struct cvs_flisthead *added, struct cvs_flisthead *removed,
                     93:        struct cvs_flisthead *modified)
1.1       jfb        94: {
                     95:        FILE *fp;
1.39      xsa        96:        int c, fd, saved_errno;
1.30      joris      97:        struct cvs_filelist *cf;
1.1       jfb        98:        struct stat st1, st2;
1.39      xsa        99:        char *fpath, *logmsg;
1.8       jfb       100:
1.35      xsa       101:        (void)xasprintf(&fpath, "%s/cvsXXXXXXXXXX", cvs_tmpdir);
1.1       jfb       102:
1.45      chl       103:        if ((fd = mkstemp(fpath)) == -1)
1.30      joris     104:                fatal("cvs_logmsg_create: mkstemp %s", strerror(errno));
1.33      joris     105:
                    106:        cvs_worklist_add(fpath, &temp_files);
1.1       jfb       107:
1.24      xsa       108:        if ((fp = fdopen(fd, "w")) == NULL) {
1.34      xsa       109:                saved_errno = errno;
1.30      joris     110:                (void)unlink(fpath);
1.34      xsa       111:                fatal("cvs_logmsg_create: fdopen %s", strerror(saved_errno));
1.8       jfb       112:        }
1.1       jfb       113:
1.8       jfb       114:        fprintf(fp, "\n%s %s\n%s Enter Log.  Lines beginning with `%s' are "
                    115:            "removed automatically\n%s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE,
                    116:            CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX, CVS_LOGMSG_PREFIX);
                    117:
1.31      joris     118:        if (added != NULL && !TAILQ_EMPTY(added)) {
1.30      joris     119:                fprintf(fp, "%s Added Files:", CVS_LOGMSG_PREFIX);
                    120:                TAILQ_FOREACH(cf, added, flist)
                    121:                        fprintf(fp, "\n%s\t%s",
                    122:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    123:                fputs("\n", fp);
                    124:        }
                    125:
1.31      joris     126:        if (removed != NULL && !TAILQ_EMPTY(removed)) {
1.30      joris     127:                fprintf(fp, "%s Removed Files:", CVS_LOGMSG_PREFIX);
                    128:                TAILQ_FOREACH(cf, removed, flist)
                    129:                        fprintf(fp, "\n%s\t%s",
                    130:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    131:                fputs("\n", fp);
                    132:        }
                    133:
1.31      joris     134:        if (modified != NULL && !TAILQ_EMPTY(modified)) {
1.30      joris     135:                fprintf(fp, "%s Modified Files:", CVS_LOGMSG_PREFIX);
                    136:                TAILQ_FOREACH(cf, modified, flist)
                    137:                        fprintf(fp, "\n%s\t%s",
                    138:                            CVS_LOGMSG_PREFIX, cf->file_path);
                    139:                fputs("\n", fp);
                    140:        }
1.1       jfb       141:
1.9       jfb       142:        fprintf(fp, "%s %s\n", CVS_LOGMSG_PREFIX, CVS_LOGMSG_LINE);
1.1       jfb       143:        (void)fflush(fp);
                    144:
                    145:        if (fstat(fd, &st1) == -1) {
1.34      xsa       146:                saved_errno = errno;
1.30      joris     147:                (void)unlink(fpath);
1.34      xsa       148:                fatal("cvs_logmsg_create: fstat %s", strerror(saved_errno));
1.1       jfb       149:        }
                    150:
1.30      joris     151:        logmsg = NULL;
                    152:
1.1       jfb       153:        for (;;) {
1.41      ray       154:                if (cvs_logmsg_edit(fpath) == -1)
1.1       jfb       155:                        break;
1.8       jfb       156:
1.1       jfb       157:                if (fstat(fd, &st2) == -1) {
1.34      xsa       158:                        saved_errno = errno;
1.30      joris     159:                        (void)unlink(fpath);
1.34      xsa       160:                        fatal("cvs_logmsg_create: fstat %s",
                    161:                            strerror(saved_errno));
1.1       jfb       162:                }
                    163:
1.30      joris     164:                if (st1.st_mtime != st2.st_mtime) {
                    165:                        logmsg = cvs_logmsg_read(fpath);
1.1       jfb       166:                        break;
                    167:                }
                    168:
1.30      joris     169:                printf("\nLog message unchanged or not specified\n"
1.32      jasper    170:                    "a)bort, c)ontinue, e)dit\nAction: (continue) ");
1.30      joris     171:                (void)fflush(stdout);
                    172:
                    173:                c = getc(stdin);
1.43      tobias    174:                if (c == EOF || c == 'a') {
1.30      joris     175:                        fatal("Aborted by user");
                    176:                } else if (c == '\n' || c == 'c') {
                    177:                        logmsg = xstrdup("");
1.1       jfb       178:                        break;
1.30      joris     179:                } else if (c == 'e') {
1.1       jfb       180:                        continue;
1.30      joris     181:                } else {
                    182:                        cvs_log(LP_ERR, "invalid input");
1.1       jfb       183:                        continue;
                    184:                }
                    185:        }
                    186:
                    187:        (void)fclose(fp);
1.30      joris     188:        (void)unlink(fpath);
                    189:        xfree(fpath);
1.1       jfb       190:
1.30      joris     191:        return (logmsg);
1.39      xsa       192: }
                    193:
1.41      ray       194: /*
                    195:  * Execute an editor on the specified pathname, which is interpreted
                    196:  * from the shell.  This means flags may be included.
                    197:  *
                    198:  * Returns -1 on error, or the exit value on success.
                    199:  */
1.39      xsa       200: int
                    201: cvs_logmsg_edit(const char *pathname)
                    202: {
                    203:        char *argp[] = {"sh", "-c", NULL, NULL}, *p;
                    204:        sig_t sighup, sigint, sigquit;
                    205:        pid_t pid;
1.40      ray       206:        int saved_errno, st;
1.39      xsa       207:
                    208:        (void)xasprintf(&p, "%s %s", cvs_editor, pathname);
                    209:        argp[2] = p;
                    210:
                    211:        sighup = signal(SIGHUP, SIG_IGN);
                    212:        sigint = signal(SIGINT, SIG_IGN);
                    213:        sigquit = signal(SIGQUIT, SIG_IGN);
1.40      ray       214:        if ((pid = fork()) == -1)
                    215:                goto fail;
1.39      xsa       216:        if (pid == 0) {
                    217:                execv(_PATH_BSHELL, argp);
                    218:                _exit(127);
                    219:        }
1.40      ray       220:        while (waitpid(pid, &st, 0) == -1)
                    221:                if (errno != EINTR)
                    222:                        goto fail;
1.39      xsa       223:        xfree(p);
                    224:        (void)signal(SIGHUP, sighup);
                    225:        (void)signal(SIGINT, sigint);
                    226:        (void)signal(SIGQUIT, sigquit);
1.40      ray       227:        if (!WIFEXITED(st)) {
                    228:                errno = EINTR;
1.39      xsa       229:                return (-1);
                    230:        }
1.40      ray       231:        return (WEXITSTATUS(st));
                    232:
                    233:  fail:
                    234:        saved_errno = errno;
                    235:        (void)signal(SIGHUP, sighup);
                    236:        (void)signal(SIGINT, sigint);
                    237:        (void)signal(SIGQUIT, sigquit);
                    238:        xfree(p);
                    239:        errno = saved_errno;
                    240:        return (-1);
1.1       jfb       241: }