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

Annotation of src/usr.bin/cvs/cvs.c, Revision 1.75

1.75    ! xsa         1: /*     $OpenBSD: cvs.c,v 1.74 2005/07/24 17:48:05 xsa Exp $    */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.18      tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.18      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.18      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.18      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.18      tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/types.h>
                     28: #include <sys/wait.h>
                     29:
1.68      xsa        30: #include <ctype.h>
1.1       jfb        31: #include <err.h>
1.68      xsa        32: #include <errno.h>
1.1       jfb        33: #include <pwd.h>
1.68      xsa        34: #include <signal.h>
1.1       jfb        35: #include <stdio.h>
                     36: #include <stdlib.h>
1.68      xsa        37: #include <string.h>
1.1       jfb        38: #include <unistd.h>
                     39:
                     40: #include "cvs.h"
                     41: #include "log.h"
1.8       jfb        42: #include "file.h"
1.44      jfb        43: #include "strtab.h"
1.1       jfb        44:
                     45:
                     46: extern char *__progname;
                     47:
                     48:
                     49: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
                     50: int verbosity = 2;
                     51:
                     52: /* compression level used with zlib, 0 meaning no compression taking place */
                     53: int   cvs_compress = 0;
1.17      jfb        54: int   cvs_readrc = 1;          /* read .cvsrc on startup */
1.1       jfb        55: int   cvs_trace = 0;
                     56: int   cvs_nolog = 0;
                     57: int   cvs_readonly = 0;
1.17      jfb        58: int   cvs_nocase = 0;   /* set to 1 to disable filename case sensitivity */
1.65      xsa        59: int   cvs_noexec = 0;  /* set to 1 to disable disk operations (-n option) */
1.73      joris      60: int   cvs_error = -1;  /* set to the correct error code on failure */
1.17      jfb        61: char *cvs_defargs;             /* default global arguments from .cvsrc */
                     62: char *cvs_command;             /* name of the command we are running */
1.6       jfb        63: int   cvs_cmdop;
1.1       jfb        64: char *cvs_rootstr;
                     65: char *cvs_rsh = CVS_RSH_DEFAULT;
                     66: char *cvs_editor = CVS_EDITOR_DEFAULT;
1.69      joris      67: char *cvs_repo_base = NULL;
1.13      krapht     68: char *cvs_msg = NULL;
                     69:
1.10      jfb        70: /* hierarchy of all the files affected by the command */
                     71: CVSFILE *cvs_files;
                     72:
1.27      jfb        73: static TAILQ_HEAD(, cvs_var) cvs_variables;
                     74:
1.1       jfb        75:
1.75    ! xsa        76: void           usage(void);
        !            77: static void    cvs_read_rcfile(void);
        !            78: int            cvs_getopt(int, char **);
1.1       jfb        79:
                     80: /*
                     81:  * usage()
                     82:  *
                     83:  * Display usage information.
                     84:  */
                     85: void
                     86: usage(void)
                     87: {
                     88:        fprintf(stderr,
1.66      joris      89:            "Usage: %s [-flnQqtv] [-d root] [-e editor] [-s var=val] [-z level] "
1.27      jfb        90:            "command [...]\n", __progname);
1.1       jfb        91: }
                     92:
                     93:
                     94: int
                     95: main(int argc, char **argv)
                     96: {
1.17      jfb        97:        char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
                     98:        int i, ret, cmd_argc;
1.1       jfb        99:        struct cvs_cmd *cmdp;
                    100:
1.27      jfb       101:        TAILQ_INIT(&cvs_variables);
                    102:
1.1       jfb       103:        if (cvs_log_init(LD_STD, 0) < 0)
                    104:                err(1, "failed to initialize logging");
                    105:
                    106:        /* by default, be very verbose */
                    107:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);
                    108:
                    109: #ifdef DEBUG
                    110:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
                    111: #endif
                    112:
1.44      jfb       113:        cvs_strtab_init();
                    114:
1.1       jfb       115:        /* check environment so command-line options override it */
                    116:        if ((envstr = getenv("CVS_RSH")) != NULL)
                    117:                cvs_rsh = envstr;
                    118:
                    119:        if (((envstr = getenv("CVSEDITOR")) != NULL) ||
                    120:            ((envstr = getenv("VISUAL")) != NULL) ||
                    121:            ((envstr = getenv("EDITOR")) != NULL))
                    122:                cvs_editor = envstr;
                    123:
1.17      jfb       124:        ret = cvs_getopt(argc, argv);
                    125:
                    126:        argc -= ret;
                    127:        argv += ret;
                    128:        if (argc == 0) {
                    129:                usage();
1.49      joris     130:                exit(1);
1.17      jfb       131:        }
                    132:        cvs_command = argv[0];
                    133:
1.74      xsa       134:        if (cvs_readrc == 1) {
1.17      jfb       135:                cvs_read_rcfile();
                    136:
                    137:                if (cvs_defargs != NULL) {
                    138:                        targv = cvs_makeargv(cvs_defargs, &i);
                    139:                        if (targv == NULL) {
                    140:                                cvs_log(LP_ERR,
                    141:                                    "failed to load default arguments to %s",
                    142:                                    __progname);
1.47      xsa       143:                                exit(1);
1.17      jfb       144:                        }
                    145:
                    146:                        cvs_getopt(i, targv);
                    147:                        cvs_freeargv(targv, i);
                    148:                        free(targv);
                    149:                }
                    150:        }
                    151:
                    152:        /* setup signal handlers */
                    153:        signal(SIGPIPE, SIG_IGN);
                    154:
1.40      jfb       155:        if (cvs_file_init() < 0) {
                    156:                cvs_log(LP_ERR, "failed to initialize file support");
                    157:                exit(1);
                    158:        }
1.17      jfb       159:
                    160:        ret = -1;
                    161:
                    162:        cmdp = cvs_findcmd(cvs_command);
                    163:        if (cmdp == NULL) {
                    164:                fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
                    165:                fprintf(stderr, "CVS commands are:\n");
1.67      jfb       166:                for (i = 0; cvs_cdt[i] != NULL; i++)
1.17      jfb       167:                        fprintf(stderr, "\t%-16s%s\n",
1.67      jfb       168:                            cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
                    169:                exit(CVS_EX_USAGE);
1.17      jfb       170:        }
                    171:
                    172:        cvs_cmdop = cmdp->cmd_op;
                    173:
                    174:        cmd_argc = 0;
                    175:        memset(cmd_argv, 0, sizeof(cmd_argv));
                    176:
                    177:        cmd_argv[cmd_argc++] = argv[0];
                    178:        if (cmdp->cmd_defargs != NULL) {
                    179:                /* transform into a new argument vector */
                    180:                ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
                    181:                    CVS_CMD_MAXARG - 1);
                    182:                if (ret < 0) {
                    183:                        cvs_log(LP_ERRNO, "failed to generate argument vector "
                    184:                            "from default arguments");
1.47      xsa       185:                        exit(1);
1.17      jfb       186:                }
                    187:                cmd_argc += ret;
                    188:        }
                    189:        for (ret = 1; ret < argc; ret++)
                    190:                cmd_argv[cmd_argc++] = argv[ret];
                    191:
1.45      joris     192:        ret = cvs_startcmd(cmdp, cmd_argc, cmd_argv);
1.51      joris     193:        switch (ret) {
                    194:        case CVS_EX_USAGE:
1.56      jfb       195:                fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
                    196:                    cmdp->cmd_synopsis);
1.51      joris     197:                break;
                    198:        case CVS_EX_DATA:
1.55      jfb       199:                cvs_log(LP_ABORT, "internal data error");
1.51      joris     200:                break;
                    201:        case CVS_EX_PROTO:
1.55      jfb       202:                cvs_log(LP_ABORT, "protocol error");
1.51      joris     203:                break;
                    204:        case CVS_EX_FILE:
1.55      jfb       205:                cvs_log(LP_ABORT, "an operation on a file or directory failed");
1.60      jfb       206:                break;
                    207:        case CVS_EX_BADROOT:
1.71      xsa       208:                /* match GNU CVS output, thus the LP_ERR and LP_ABORT codes. */
                    209:                cvs_log(LP_ERR,
1.60      jfb       210:                    "No CVSROOT specified! Please use the `-d' option");
                    211:                cvs_log(LP_ABORT,
                    212:                    "or set the CVSROOT enviroment variable.");
1.73      joris     213:                break;
                    214:        case CVS_EX_ERR:
                    215:                cvs_log(LP_ABORT, "yeah, we failed, and we don't know why");
1.51      joris     216:                break;
                    217:        default:
                    218:                break;
1.17      jfb       219:        }
                    220:
                    221:        if (cvs_files != NULL)
                    222:                cvs_file_free(cvs_files);
1.33      jfb       223:        if (cvs_msg != NULL)
                    224:                free(cvs_msg);
1.44      jfb       225:
                    226:        cvs_strtab_cleanup();
1.17      jfb       227:
                    228:        return (ret);
                    229: }
                    230:
                    231:
                    232: int
                    233: cvs_getopt(int argc, char **argv)
                    234: {
                    235:        int ret;
                    236:        char *ep;
                    237:
1.27      jfb       238:        while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:tvz:")) != -1) {
1.1       jfb       239:                switch (ret) {
1.11      jfb       240:                case 'b':
                    241:                        /*
                    242:                         * We do not care about the bin directory for RCS files
                    243:                         * as this program has no dependencies on RCS programs,
                    244:                         * so it is only here for backwards compatibility.
                    245:                         */
                    246:                        cvs_log(LP_NOTICE, "the -b argument is obsolete");
                    247:                        break;
1.1       jfb       248:                case 'd':
                    249:                        cvs_rootstr = optarg;
                    250:                        break;
                    251:                case 'e':
                    252:                        cvs_editor = optarg;
                    253:                        break;
                    254:                case 'f':
1.17      jfb       255:                        cvs_readrc = 0;
1.1       jfb       256:                        break;
                    257:                case 'l':
                    258:                        cvs_nolog = 1;
                    259:                        break;
                    260:                case 'n':
1.65      xsa       261:                        cvs_noexec = 1;
1.1       jfb       262:                        break;
                    263:                case 'Q':
                    264:                        verbosity = 0;
                    265:                        break;
                    266:                case 'q':
                    267:                        /* don't override -Q */
                    268:                        if (verbosity > 1)
                    269:                                verbosity = 1;
                    270:                        break;
                    271:                case 'r':
                    272:                        cvs_readonly = 1;
                    273:                        break;
1.27      jfb       274:                case 's':
                    275:                        ep = strchr(optarg, '=');
                    276:                        if (ep == NULL) {
                    277:                                cvs_log(LP_ERR, "no = in variable assignment");
1.49      joris     278:                                exit(1);
1.27      jfb       279:                        }
                    280:                        *(ep++) = '\0';
                    281:                        if (cvs_var_set(optarg, ep) < 0)
1.49      joris     282:                                exit(1);
1.27      jfb       283:                        break;
1.1       jfb       284:                case 't':
1.24      jfb       285:                        (void)cvs_log_filter(LP_FILTER_UNSET, LP_TRACE);
1.1       jfb       286:                        cvs_trace = 1;
                    287:                        break;
                    288:                case 'v':
                    289:                        printf("%s\n", CVS_VERSION);
                    290:                        exit(0);
                    291:                        /* NOTREACHED */
1.11      jfb       292:                        break;
                    293:                case 'x':
                    294:                        /*
                    295:                         * Kerberos encryption support, kept for compatibility
                    296:                         */
1.1       jfb       297:                        break;
                    298:                case 'z':
1.18      tedu      299:                        cvs_compress = (int)strtol(optarg, &ep, 10);
1.1       jfb       300:                        if (*ep != '\0')
                    301:                                errx(1, "error parsing compression level");
                    302:                        if (cvs_compress < 0 || cvs_compress > 9)
                    303:                                errx(1, "gzip compression level must be "
                    304:                                    "between 0 and 9");
                    305:                        break;
                    306:                default:
                    307:                        usage();
1.49      joris     308:                        exit(1);
1.1       jfb       309:                }
                    310:        }
                    311:
1.17      jfb       312:        ret = optind;
1.1       jfb       313:        optind = 1;
1.17      jfb       314:        optreset = 1;   /* for next call */
1.12      jfb       315:
1.1       jfb       316:        return (ret);
                    317: }
                    318:
                    319:
                    320: /*
1.17      jfb       321:  * cvs_read_rcfile()
1.1       jfb       322:  *
                    323:  * Read the CVS `.cvsrc' file in the user's home directory.  If the file
                    324:  * exists, it should contain a list of arguments that should always be given
                    325:  * implicitly to the specified commands.
                    326:  */
1.42      joris     327: static void
1.17      jfb       328: cvs_read_rcfile(void)
1.1       jfb       329: {
1.70      joris     330:        char rcpath[MAXPATHLEN], linebuf[128], *lp, *p;
1.54      xsa       331:        int l, linenum = 0;
1.17      jfb       332:        size_t len;
1.1       jfb       333:        struct cvs_cmd *cmdp;
                    334:        struct passwd *pw;
                    335:        FILE *fp;
                    336:
                    337:        pw = getpwuid(getuid());
                    338:        if (pw == NULL) {
                    339:                cvs_log(LP_NOTICE, "failed to get user's password entry");
                    340:                return;
                    341:        }
                    342:
1.54      xsa       343:        l = snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
                    344:        if (l == -1 || l >= (int)sizeof(rcpath)) {
                    345:                errno = ENAMETOOLONG;
                    346:                cvs_log(LP_ERRNO, "%s", rcpath);
                    347:                return;
                    348:        }
1.1       jfb       349:
                    350:        fp = fopen(rcpath, "r");
                    351:        if (fp == NULL) {
                    352:                if (errno != ENOENT)
                    353:                        cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
                    354:                            strerror(errno));
                    355:                return;
                    356:        }
                    357:
                    358:        while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
1.23      xsa       359:                linenum++;
1.17      jfb       360:                if ((len = strlen(linebuf)) == 0)
                    361:                        continue;
                    362:                if (linebuf[len - 1] != '\n') {
1.23      xsa       363:                        cvs_log(LP_WARN, "line too long in `%s:%d'", rcpath,
                    364:                                linenum);
1.17      jfb       365:                        break;
                    366:                }
                    367:                linebuf[--len] = '\0';
                    368:
1.70      joris     369:                /* skip any whitespaces */
                    370:                p = linebuf;
                    371:                while (*p == ' ')
                    372:                        *p++;
                    373:
                    374:                /* allow comments */
                    375:                if (*p == '#')
                    376:                        continue;
                    377:
                    378:                lp = strchr(p, ' ');
1.1       jfb       379:                if (lp == NULL)
1.17      jfb       380:                        continue;       /* ignore lines with no arguments */
                    381:                *lp = '\0';
1.70      joris     382:                if (strcmp(p, "cvs") == 0) {
1.17      jfb       383:                        /*
                    384:                         * Global default options.  In the case of cvs only,
                    385:                         * we keep the 'cvs' string as first argument because
                    386:                         * getopt() does not like starting at index 0 for
                    387:                         * argument processing.
                    388:                         */
                    389:                        *lp = ' ';
1.70      joris     390:                        cvs_defargs = strdup(p);
1.17      jfb       391:                        if (cvs_defargs == NULL)
                    392:                                cvs_log(LP_ERRNO,
                    393:                                    "failed to copy global arguments");
1.15      deraadt   394:                } else {
1.17      jfb       395:                        lp++;
1.70      joris     396:                        cmdp = cvs_findcmd(p);
1.1       jfb       397:                        if (cmdp == NULL) {
                    398:                                cvs_log(LP_NOTICE,
1.25      xsa       399:                                    "unknown command `%s' in `%s:%d'",
1.70      joris     400:                                    p, rcpath, linenum);
1.1       jfb       401:                                continue;
                    402:                        }
1.17      jfb       403:
                    404:                        cmdp->cmd_defargs = strdup(lp);
                    405:                        if (cmdp->cmd_defargs == NULL)
                    406:                                cvs_log(LP_ERRNO,
                    407:                                    "failed to copy default arguments for %s",
                    408:                                    cmdp->cmd_name);
1.1       jfb       409:                }
                    410:        }
                    411:        if (ferror(fp)) {
1.23      xsa       412:                cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1       jfb       413:        }
                    414:
                    415:        (void)fclose(fp);
1.27      jfb       416: }
                    417:
                    418:
                    419: /*
                    420:  * cvs_var_set()
                    421:  *
                    422:  * Set the value of the variable <var> to <val>.  If there is no such variable,
                    423:  * a new entry is created, otherwise the old value is overwritten.
                    424:  * Returns 0 on success, or -1 on failure.
                    425:  */
                    426: int
                    427: cvs_var_set(const char *var, const char *val)
                    428: {
                    429:        char *valcp;
                    430:        const char *cp;
                    431:        struct cvs_var *vp;
                    432:
                    433:        if ((var == NULL) || (*var == '\0')) {
                    434:                cvs_log(LP_ERR, "no variable name");
                    435:                return (-1);
                    436:        }
                    437:
                    438:        /* sanity check on the name */
                    439:        for (cp = var; *cp != '\0'; cp++)
                    440:                if (!isalnum(*cp) && (*cp != '_')) {
                    441:                        cvs_log(LP_ERR,
                    442:                            "variable name `%s' contains invalid characters",
                    443:                            var);
                    444:                        return (-1);
                    445:                }
                    446:
                    447:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    448:                if (strcmp(vp->cv_name, var) == 0)
                    449:                        break;
                    450:
                    451:        valcp = strdup(val);
                    452:        if (valcp == NULL) {
                    453:                cvs_log(LP_ERRNO, "failed to allocate variable");
                    454:                return (-1);
                    455:        }
                    456:
                    457:        if (vp == NULL) {
                    458:                vp = (struct cvs_var *)malloc(sizeof(*vp));
                    459:                if (vp == NULL) {
                    460:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    461:                        free(valcp);
                    462:                        return (-1);
                    463:                }
                    464:                memset(vp, 0, sizeof(*vp));
                    465:
                    466:                vp->cv_name = strdup(var);
                    467:                if (vp->cv_name == NULL) {
                    468:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    469:                        free(valcp);
                    470:                        free(vp);
                    471:                        return (-1);
                    472:                }
                    473:
                    474:                TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
                    475:
                    476:        } else  /* free the previous value */
                    477:                free(vp->cv_val);
                    478:
                    479:        vp->cv_val = valcp;
                    480:
                    481:        return (0);
                    482: }
                    483:
                    484:
                    485: /*
                    486:  * cvs_var_set()
                    487:  *
                    488:  * Remove any entry for the variable <var>.
                    489:  * Returns 0 on success, or -1 on failure.
                    490:  */
                    491: int
                    492: cvs_var_unset(const char *var)
                    493: {
                    494:        struct cvs_var *vp;
                    495:
                    496:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    497:                if (strcmp(vp->cv_name, var) == 0) {
                    498:                        TAILQ_REMOVE(&cvs_variables, vp, cv_link);
                    499:                        free(vp->cv_name);
                    500:                        free(vp->cv_val);
                    501:                        free(vp);
                    502:                        return (0);
                    503:                }
                    504:
                    505:        return (-1);
                    506:
                    507: }
                    508:
                    509:
                    510: /*
                    511:  * cvs_var_get()
                    512:  *
                    513:  * Get the value associated with the variable <var>.  Returns a pointer to the
                    514:  * value string on success, or NULL if the variable does not exist.
                    515:  */
                    516:
1.75    ! xsa       517: const char *
1.27      jfb       518: cvs_var_get(const char *var)
                    519: {
                    520:        struct cvs_var *vp;
                    521:
                    522:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    523:                if (strcmp(vp->cv_name, var) == 0)
                    524:                        return (vp->cv_val);
                    525:
                    526:        return (NULL);
1.1       jfb       527: }