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

1.60    ! jfb         1: /*     $OpenBSD$       */
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:
                     30: #include <err.h>
                     31: #include <pwd.h>
                     32: #include <errno.h>
                     33: #include <stdio.h>
1.27      jfb        34: #include <ctype.h>
1.1       jfb        35: #include <stdlib.h>
                     36: #include <unistd.h>
                     37: #include <signal.h>
                     38: #include <string.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.1       jfb        59:
1.17      jfb        60: char *cvs_defargs;             /* default global arguments from .cvsrc */
                     61: char *cvs_command;             /* name of the command we are running */
1.6       jfb        62: int   cvs_cmdop;
1.1       jfb        63: char *cvs_rootstr;
                     64: char *cvs_rsh = CVS_RSH_DEFAULT;
                     65: char *cvs_editor = CVS_EDITOR_DEFAULT;
                     66:
1.13      krapht     67: char *cvs_msg = NULL;
                     68:
1.10      jfb        69: /* hierarchy of all the files affected by the command */
                     70: CVSFILE *cvs_files;
                     71:
                     72:
1.27      jfb        73: static TAILQ_HEAD(, cvs_var) cvs_variables;
                     74:
1.1       jfb        75: /*
                     76:  * Command dispatch table
                     77:  * ----------------------
                     78:  *
                     79:  * The synopsis field should only contain the list of arguments that the
                     80:  * command supports, without the actual command's name.
                     81:  *
1.29      david      82:  * Command handlers are expected to return 0 if no error occurred, or one of
1.52      xsa        83:  * the CVS_EX_* error codes in case of an error.  In case the error
1.49      joris      84:  * returned is 1, the command's usage string is printed to standard
1.1       jfb        85:  * error before returning.
                     86:  */
1.43      joris      87: struct cvs_cmd cvs_cdt[] = {
1.1       jfb        88:        {
1.45      joris      89:                CVS_OP_ADD, "add",      { "ad",  "new" }, &cvs_add,
1.59      xsa        90:                "[-k mode] [-m msg] file ...",
1.37      jfb        91:                "k:m:",
1.1       jfb        92:                "Add a new file/directory to the repository",
1.45      joris      93:                NULL
1.1       jfb        94:        },
                     95:        {
1.45      joris      96:                CVS_OP_ADMIN, "admin",    { "adm", "rcs" }, &cvs_admin,
1.13      krapht     97:                "",
1.45      joris      98:                "a:A:b::c:e::Ik:l::Lm:n:N:o:qs:t:u::U",
1.1       jfb        99:                "Administration front end for rcs",
1.45      joris     100:                NULL
1.1       jfb       101:        },
                    102:        {
1.45      joris     103:                CVS_OP_ANNOTATE, "annotate", { "ann"        }, &cvs_annotate,
1.38      jfb       104:                "[-flR] [-D date | -r rev] ...",
                    105:                "D:flRr:",
1.1       jfb       106:                "Show last revision where each line was modified",
1.45      joris     107:                NULL
1.1       jfb       108:        },
                    109:        {
1.45      joris     110:                CVS_OP_CHECKOUT, "checkout", { "co",  "get" }, &cvs_checkout,
1.39      xsa       111:                "[-AcflNnPpRs] [-D date | -r rev] [-d dir] [-j rev] [-k mode] "
1.38      jfb       112:                "[-t id] module ...",
                    113:                "AcD:d:fj:k:lNnPRr:st:",
1.1       jfb       114:                "Checkout sources for editing",
1.45      joris     115:                NULL
1.1       jfb       116:        },
                    117:        {
1.45      joris     118:                CVS_OP_COMMIT, "commit",   { "ci",  "com" }, &cvs_commit,
1.4       jfb       119:                "[-flR] [-F logfile | -m msg] [-r rev] ...",
1.13      krapht    120:                "F:flm:Rr:",
1.1       jfb       121:                "Check files into the repository",
1.45      joris     122:                NULL
1.1       jfb       123:        },
                    124:        {
1.45      joris     125:                CVS_OP_DIFF, "diff",     { "di",  "dif" }, &cvs_diff,
1.60    ! jfb       126:                "[-cilNpRu] [-D date] [-r rev] ...",
        !           127:                "cD:ilNpRr:u",
1.1       jfb       128:                "Show differences between revisions",
1.45      joris     129:                NULL
1.1       jfb       130:        },
                    131:        {
1.36      jfb       132:                CVS_OP_EDIT, "edit",     {              }, NULL,
1.1       jfb       133:                "",
1.13      krapht    134:                "",
1.1       jfb       135:                "Get ready to edit a watched file",
1.45      joris     136:                NULL
1.1       jfb       137:        },
                    138:        {
1.36      jfb       139:                CVS_OP_EDITORS, "editors",  {              }, NULL,
1.1       jfb       140:                "",
1.13      krapht    141:                "",
1.1       jfb       142:                "See who is editing a watched file",
1.45      joris     143:                NULL
1.1       jfb       144:        },
                    145:        {
1.36      jfb       146:                CVS_OP_EXPORT, "export",   { "ex",  "exp" }, NULL,
1.1       jfb       147:                "",
1.13      krapht    148:                "",
1.1       jfb       149:                "Export sources from CVS, similar to checkout",
1.45      joris     150:                NULL
1.1       jfb       151:        },
                    152:        {
1.45      joris     153:                CVS_OP_HISTORY, "history",  { "hi",  "his" }, &cvs_history,
1.13      krapht    154:                "",
1.45      joris     155:                "acelm:oTt:u:wx:z:",
1.1       jfb       156:                "Show repository access history",
1.45      joris     157:                NULL
1.1       jfb       158:        },
                    159:        {
1.45      joris     160:                CVS_OP_IMPORT, "import",   { "im",  "imp" }, &cvs_import,
1.13      krapht    161:                "[-d] [-b branch] [-I ign] [-k subst] [-m msg] "
                    162:                "repository vendor-tag release-tags ...",
                    163:                "b:dI:k:m:",
1.1       jfb       164:                "Import sources into CVS, using vendor branches",
1.45      joris     165:                NULL
1.1       jfb       166:        },
                    167:        {
1.45      joris     168:                CVS_OP_INIT, "init",     {              }, &cvs_init,
1.1       jfb       169:                "",
1.13      krapht    170:                "",
1.1       jfb       171:                "Create a CVS repository if it doesn't exist",
1.45      joris     172:                NULL
1.1       jfb       173:        },
                    174: #if defined(HAVE_KERBEROS)
                    175:        {
                    176:                "kserver",  {}, NULL
                    177:                "",
1.13      krapht    178:                "",
1.1       jfb       179:                "Start a Kerberos authentication CVS server",
1.45      joris     180:                NULL
1.1       jfb       181:        },
                    182: #endif
                    183:        {
1.45      joris     184:                CVS_OP_LOG, "log",      { "lo"         }, &cvs_getlog,
1.38      jfb       185:                "[-bhlNRt] [-d dates] [-r revisions] [-s states] [-w logins]",
1.45      joris     186:                "d:hlRr:",
1.1       jfb       187:                "Print out history information for files",
1.45      joris     188:                NULL
1.1       jfb       189:        },
                    190:        {
1.6       jfb       191:                -1, "login",    {}, NULL,
1.1       jfb       192:                "",
1.13      krapht    193:                "",
1.1       jfb       194:                "Prompt for password for authenticating server",
1.45      joris     195:                NULL
1.1       jfb       196:        },
                    197:        {
1.6       jfb       198:                -1, "logout",   {}, NULL,
1.1       jfb       199:                "",
1.13      krapht    200:                "",
1.1       jfb       201:                "Removes entry in .cvspass for remote repository",
1.45      joris     202:                NULL
1.1       jfb       203:        },
                    204:        {
1.36      jfb       205:                CVS_OP_RDIFF, "rdiff",    {}, NULL,
1.1       jfb       206:                "",
1.13      krapht    207:                "",
1.1       jfb       208:                "Create 'patch' format diffs between releases",
1.45      joris     209:                NULL
1.1       jfb       210:        },
                    211:        {
1.36      jfb       212:                CVS_OP_RELEASE, "release",  {}, NULL,
1.38      jfb       213:                "[-d]",
                    214:                "d",
1.1       jfb       215:                "Indicate that a Module is no longer in use",
1.45      joris     216:                NULL
1.1       jfb       217:        },
                    218:        {
1.45      joris     219:                CVS_OP_REMOVE, "remove",   { "rm", "delete" }, &cvs_remove,
1.57      xsa       220:                "[-flR] [file ...]",
1.38      jfb       221:                "flR",
1.1       jfb       222:                "Remove an entry from the repository",
1.45      joris     223:                NULL
1.1       jfb       224:        },
                    225:        {
1.36      jfb       226:                CVS_OP_RLOG, "rlog",     {}, NULL,
1.1       jfb       227:                "",
1.13      krapht    228:                "",
1.1       jfb       229:                "Print out history information for a module",
1.45      joris     230:                NULL
1.1       jfb       231:        },
                    232:        {
1.36      jfb       233:                CVS_OP_RTAG, "rtag",     {}, NULL,
1.1       jfb       234:                "",
1.13      krapht    235:                "",
1.1       jfb       236:                "Add a symbolic tag to a module",
1.45      joris     237:                NULL
1.1       jfb       238:        },
                    239:        {
1.46      joris     240:                CVS_OP_SERVER, "server",   {}, &cmd_server,
1.1       jfb       241:                "",
1.13      krapht    242:                "",
1.1       jfb       243:                "Server mode",
1.45      joris     244:                NULL
1.1       jfb       245:        },
                    246:        {
1.45      joris     247:                CVS_OP_STATUS, "status",   { "st", "stat" }, &cvs_status,
1.31      jfb       248:                "[-lRv]",
1.38      jfb       249:                "lRv",
1.1       jfb       250:                "Display status information on checked out files",
1.45      joris     251:                NULL
1.1       jfb       252:        },
                    253:        {
1.45      joris     254:                CVS_OP_TAG, "tag",      { "ta", "freeze" }, &cvs_tag,
1.56      jfb       255:                "[-bcdFflR] [-D date | -r rev] tagname ...",
1.38      jfb       256:                "bcD:dFflRr:",
1.1       jfb       257:                "Add a symbolic tag to checked out version of files",
1.45      joris     258:                NULL
1.1       jfb       259:        },
                    260:        {
1.36      jfb       261:                CVS_OP_UNEDIT, "unedit",   {}, NULL,
1.1       jfb       262:                "",
1.13      krapht    263:                "",
1.1       jfb       264:                "Undo an edit command",
1.45      joris     265:                NULL
1.1       jfb       266:        },
                    267:        {
1.45      joris     268:                CVS_OP_UPDATE, "update",   { "up", "upd" }, &cvs_update,
1.39      xsa       269:                "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
1.38      jfb       270:                "[-t id] ...",
1.45      joris     271:                "ACD:dflPpQqRr:",
1.1       jfb       272:                "Bring work tree in sync with repository",
1.45      joris     273:                NULL
1.1       jfb       274:        },
                    275:        {
1.45      joris     276:                CVS_OP_VERSION, "version",  { "ve", "ver" }, &cvs_version,
1.13      krapht    277:                "", "",
1.1       jfb       278:                "Show current CVS version(s)",
1.45      joris     279:                NULL
1.1       jfb       280:        },
                    281:        {
1.36      jfb       282:                CVS_OP_WATCH, "watch",    {}, NULL,
1.1       jfb       283:                "",
1.13      krapht    284:                "",
1.1       jfb       285:                "Set watches",
1.45      joris     286:                NULL
1.1       jfb       287:        },
                    288:        {
1.36      jfb       289:                CVS_OP_WATCHERS, "watchers", {}, NULL,
1.1       jfb       290:                "",
1.13      krapht    291:                "",
1.1       jfb       292:                "See who is watching a file",
1.45      joris     293:                NULL
1.1       jfb       294:        },
                    295: };
                    296:
                    297: #define CVS_NBCMD  (sizeof(cvs_cdt)/sizeof(cvs_cdt[0]))
                    298:
                    299:
                    300:
1.48      jfb       301: void         usage           (void);
                    302: static void  cvs_read_rcfile (void);
                    303: int          cvs_getopt      (int, char **);
1.1       jfb       304:
                    305:
                    306: /*
                    307:  * usage()
                    308:  *
                    309:  * Display usage information.
                    310:  */
                    311: void
                    312: usage(void)
                    313: {
                    314:        fprintf(stderr,
1.27      jfb       315:            "Usage: %s [-flQqtv] [-d root] [-e editor] [-s var=val] [-z level] "
                    316:            "command [...]\n", __progname);
1.1       jfb       317: }
                    318:
                    319:
                    320: int
                    321: main(int argc, char **argv)
                    322: {
1.17      jfb       323:        char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
                    324:        int i, ret, cmd_argc;
1.1       jfb       325:        struct cvs_cmd *cmdp;
                    326:
1.27      jfb       327:        TAILQ_INIT(&cvs_variables);
                    328:
1.1       jfb       329:        if (cvs_log_init(LD_STD, 0) < 0)
                    330:                err(1, "failed to initialize logging");
                    331:
                    332:        /* by default, be very verbose */
                    333:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);
                    334:
                    335: #ifdef DEBUG
                    336:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
                    337: #endif
                    338:
1.44      jfb       339:        cvs_strtab_init();
                    340:
1.1       jfb       341:        /* check environment so command-line options override it */
                    342:        if ((envstr = getenv("CVS_RSH")) != NULL)
                    343:                cvs_rsh = envstr;
                    344:
                    345:        if (((envstr = getenv("CVSEDITOR")) != NULL) ||
                    346:            ((envstr = getenv("VISUAL")) != NULL) ||
                    347:            ((envstr = getenv("EDITOR")) != NULL))
                    348:                cvs_editor = envstr;
                    349:
1.17      jfb       350:        ret = cvs_getopt(argc, argv);
                    351:
                    352:        argc -= ret;
                    353:        argv += ret;
                    354:        if (argc == 0) {
                    355:                usage();
1.49      joris     356:                exit(1);
1.17      jfb       357:        }
                    358:        cvs_command = argv[0];
                    359:
                    360:        if (cvs_readrc) {
                    361:                cvs_read_rcfile();
                    362:
                    363:                if (cvs_defargs != NULL) {
                    364:                        targv = cvs_makeargv(cvs_defargs, &i);
                    365:                        if (targv == NULL) {
                    366:                                cvs_log(LP_ERR,
                    367:                                    "failed to load default arguments to %s",
                    368:                                    __progname);
1.47      xsa       369:                                exit(1);
1.17      jfb       370:                        }
                    371:
                    372:                        cvs_getopt(i, targv);
                    373:                        cvs_freeargv(targv, i);
                    374:                        free(targv);
                    375:                }
                    376:        }
                    377:
                    378:        /* setup signal handlers */
                    379:        signal(SIGPIPE, SIG_IGN);
                    380:
1.40      jfb       381:        if (cvs_file_init() < 0) {
                    382:                cvs_log(LP_ERR, "failed to initialize file support");
                    383:                exit(1);
                    384:        }
1.17      jfb       385:
                    386:        ret = -1;
                    387:
                    388:        cmdp = cvs_findcmd(cvs_command);
                    389:        if (cmdp == NULL) {
                    390:                fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
                    391:                fprintf(stderr, "CVS commands are:\n");
                    392:                for (i = 0; i < (int)CVS_NBCMD; i++)
                    393:                        fprintf(stderr, "\t%-16s%s\n",
                    394:                            cvs_cdt[i].cmd_name, cvs_cdt[i].cmd_descr);
1.49      joris     395:                exit(1);
1.17      jfb       396:        }
                    397:
1.45      joris     398:        if (cmdp->cmd_info == NULL) {
1.17      jfb       399:                cvs_log(LP_ERR, "command `%s' not implemented", cvs_command);
                    400:                exit(1);
                    401:        }
                    402:
                    403:        cvs_cmdop = cmdp->cmd_op;
                    404:
                    405:        cmd_argc = 0;
                    406:        memset(cmd_argv, 0, sizeof(cmd_argv));
                    407:
                    408:        cmd_argv[cmd_argc++] = argv[0];
                    409:        if (cmdp->cmd_defargs != NULL) {
                    410:                /* transform into a new argument vector */
                    411:                ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
                    412:                    CVS_CMD_MAXARG - 1);
                    413:                if (ret < 0) {
                    414:                        cvs_log(LP_ERRNO, "failed to generate argument vector "
                    415:                            "from default arguments");
1.47      xsa       416:                        exit(1);
1.17      jfb       417:                }
                    418:                cmd_argc += ret;
                    419:        }
                    420:        for (ret = 1; ret < argc; ret++)
                    421:                cmd_argv[cmd_argc++] = argv[ret];
                    422:
1.45      joris     423:        ret = cvs_startcmd(cmdp, cmd_argc, cmd_argv);
1.51      joris     424:        switch (ret) {
                    425:        case CVS_EX_USAGE:
1.56      jfb       426:                fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
                    427:                    cmdp->cmd_synopsis);
1.51      joris     428:                break;
                    429:        case CVS_EX_DATA:
1.55      jfb       430:                cvs_log(LP_ABORT, "internal data error");
1.51      joris     431:                break;
                    432:        case CVS_EX_PROTO:
1.55      jfb       433:                cvs_log(LP_ABORT, "protocol error");
1.51      joris     434:                break;
                    435:        case CVS_EX_FILE:
1.55      jfb       436:                cvs_log(LP_ABORT, "an operation on a file or directory failed");
1.60    ! jfb       437:                break;
        !           438:        case CVS_EX_BADROOT:
        !           439:                cvs_log(LP_ABORT,
        !           440:                    "No CVSROOT specified! Please use the `-d' option");
        !           441:                cvs_log(LP_ABORT,
        !           442:                    "or set the CVSROOT enviroment variable.");
1.51      joris     443:                break;
                    444:        default:
                    445:                break;
1.17      jfb       446:        }
                    447:
1.45      joris     448:        if (cmdp->cmd_info->cmd_cleanup != NULL)
                    449:                cmdp->cmd_info->cmd_cleanup();
1.17      jfb       450:        if (cvs_files != NULL)
                    451:                cvs_file_free(cvs_files);
1.33      jfb       452:        if (cvs_msg != NULL)
                    453:                free(cvs_msg);
1.44      jfb       454:
                    455:        cvs_strtab_cleanup();
1.17      jfb       456:
                    457:        return (ret);
                    458: }
                    459:
                    460:
                    461: int
                    462: cvs_getopt(int argc, char **argv)
                    463: {
                    464:        int ret;
                    465:        char *ep;
                    466:
1.27      jfb       467:        while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:tvz:")) != -1) {
1.1       jfb       468:                switch (ret) {
1.11      jfb       469:                case 'b':
                    470:                        /*
                    471:                         * We do not care about the bin directory for RCS files
                    472:                         * as this program has no dependencies on RCS programs,
                    473:                         * so it is only here for backwards compatibility.
                    474:                         */
                    475:                        cvs_log(LP_NOTICE, "the -b argument is obsolete");
                    476:                        break;
1.1       jfb       477:                case 'd':
                    478:                        cvs_rootstr = optarg;
                    479:                        break;
                    480:                case 'e':
                    481:                        cvs_editor = optarg;
                    482:                        break;
                    483:                case 'f':
1.17      jfb       484:                        cvs_readrc = 0;
1.1       jfb       485:                        break;
                    486:                case 'l':
                    487:                        cvs_nolog = 1;
                    488:                        break;
                    489:                case 'n':
                    490:                        break;
                    491:                case 'Q':
                    492:                        verbosity = 0;
                    493:                        break;
                    494:                case 'q':
                    495:                        /* don't override -Q */
                    496:                        if (verbosity > 1)
                    497:                                verbosity = 1;
                    498:                        break;
                    499:                case 'r':
                    500:                        cvs_readonly = 1;
                    501:                        break;
1.27      jfb       502:                case 's':
                    503:                        ep = strchr(optarg, '=');
                    504:                        if (ep == NULL) {
                    505:                                cvs_log(LP_ERR, "no = in variable assignment");
1.49      joris     506:                                exit(1);
1.27      jfb       507:                        }
                    508:                        *(ep++) = '\0';
                    509:                        if (cvs_var_set(optarg, ep) < 0)
1.49      joris     510:                                exit(1);
1.27      jfb       511:                        break;
1.1       jfb       512:                case 't':
1.24      jfb       513:                        (void)cvs_log_filter(LP_FILTER_UNSET, LP_TRACE);
1.1       jfb       514:                        cvs_trace = 1;
                    515:                        break;
                    516:                case 'v':
                    517:                        printf("%s\n", CVS_VERSION);
                    518:                        exit(0);
                    519:                        /* NOTREACHED */
1.11      jfb       520:                        break;
                    521:                case 'x':
                    522:                        /*
                    523:                         * Kerberos encryption support, kept for compatibility
                    524:                         */
1.1       jfb       525:                        break;
                    526:                case 'z':
1.18      tedu      527:                        cvs_compress = (int)strtol(optarg, &ep, 10);
1.1       jfb       528:                        if (*ep != '\0')
                    529:                                errx(1, "error parsing compression level");
                    530:                        if (cvs_compress < 0 || cvs_compress > 9)
                    531:                                errx(1, "gzip compression level must be "
                    532:                                    "between 0 and 9");
                    533:                        break;
                    534:                default:
                    535:                        usage();
1.49      joris     536:                        exit(1);
1.1       jfb       537:                }
                    538:        }
                    539:
1.17      jfb       540:        ret = optind;
1.1       jfb       541:        optind = 1;
1.17      jfb       542:        optreset = 1;   /* for next call */
1.12      jfb       543:
1.1       jfb       544:        return (ret);
                    545: }
                    546:
                    547:
                    548: /*
                    549:  * cvs_findcmd()
                    550:  *
                    551:  * Find the entry in the command dispatch table whose name or one of its
                    552:  * aliases matches <cmd>.
                    553:  * Returns a pointer to the command entry on success, NULL on failure.
                    554:  */
1.48      jfb       555: struct cvs_cmd*
1.1       jfb       556: cvs_findcmd(const char *cmd)
                    557: {
                    558:        u_int i, j;
                    559:        struct cvs_cmd *cmdp;
                    560:
                    561:        cmdp = NULL;
                    562:
                    563:        for (i = 0; (i < CVS_NBCMD) && (cmdp == NULL); i++) {
                    564:                if (strcmp(cmd, cvs_cdt[i].cmd_name) == 0)
                    565:                        cmdp = &cvs_cdt[i];
                    566:                else {
                    567:                        for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
                    568:                                if (strcmp(cmd, cvs_cdt[i].cmd_alias[j]) == 0) {
                    569:                                        cmdp = &cvs_cdt[i];
                    570:                                        break;
                    571:                                }
                    572:                        }
                    573:                }
                    574:        }
                    575:
                    576:        return (cmdp);
                    577: }
                    578:
                    579:
                    580: /*
1.17      jfb       581:  * cvs_read_rcfile()
1.1       jfb       582:  *
                    583:  * Read the CVS `.cvsrc' file in the user's home directory.  If the file
                    584:  * exists, it should contain a list of arguments that should always be given
                    585:  * implicitly to the specified commands.
                    586:  */
1.42      joris     587: static void
1.17      jfb       588: cvs_read_rcfile(void)
1.1       jfb       589: {
                    590:        char rcpath[MAXPATHLEN], linebuf[128], *lp;
1.54      xsa       591:        int l, linenum = 0;
1.17      jfb       592:        size_t len;
1.1       jfb       593:        struct cvs_cmd *cmdp;
                    594:        struct passwd *pw;
                    595:        FILE *fp;
                    596:
                    597:        pw = getpwuid(getuid());
                    598:        if (pw == NULL) {
                    599:                cvs_log(LP_NOTICE, "failed to get user's password entry");
                    600:                return;
                    601:        }
                    602:
1.54      xsa       603:        l = snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
                    604:        if (l == -1 || l >= (int)sizeof(rcpath)) {
                    605:                errno = ENAMETOOLONG;
                    606:                cvs_log(LP_ERRNO, "%s", rcpath);
                    607:                return;
                    608:        }
1.1       jfb       609:
                    610:        fp = fopen(rcpath, "r");
                    611:        if (fp == NULL) {
                    612:                if (errno != ENOENT)
                    613:                        cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
                    614:                            strerror(errno));
                    615:                return;
                    616:        }
                    617:
                    618:        while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
1.23      xsa       619:                linenum++;
1.17      jfb       620:                if ((len = strlen(linebuf)) == 0)
                    621:                        continue;
                    622:                if (linebuf[len - 1] != '\n') {
1.23      xsa       623:                        cvs_log(LP_WARN, "line too long in `%s:%d'", rcpath,
                    624:                                linenum);
1.17      jfb       625:                        break;
                    626:                }
                    627:                linebuf[--len] = '\0';
                    628:
1.1       jfb       629:                lp = strchr(linebuf, ' ');
                    630:                if (lp == NULL)
1.17      jfb       631:                        continue;       /* ignore lines with no arguments */
                    632:                *lp = '\0';
1.1       jfb       633:                if (strcmp(linebuf, "cvs") == 0) {
1.17      jfb       634:                        /*
                    635:                         * Global default options.  In the case of cvs only,
                    636:                         * we keep the 'cvs' string as first argument because
                    637:                         * getopt() does not like starting at index 0 for
                    638:                         * argument processing.
                    639:                         */
                    640:                        *lp = ' ';
                    641:                        cvs_defargs = strdup(linebuf);
                    642:                        if (cvs_defargs == NULL)
                    643:                                cvs_log(LP_ERRNO,
                    644:                                    "failed to copy global arguments");
1.15      deraadt   645:                } else {
1.17      jfb       646:                        lp++;
1.1       jfb       647:                        cmdp = cvs_findcmd(linebuf);
                    648:                        if (cmdp == NULL) {
                    649:                                cvs_log(LP_NOTICE,
1.25      xsa       650:                                    "unknown command `%s' in `%s:%d'",
                    651:                                    linebuf, rcpath, linenum);
1.1       jfb       652:                                continue;
                    653:                        }
1.17      jfb       654:
                    655:                        cmdp->cmd_defargs = strdup(lp);
                    656:                        if (cmdp->cmd_defargs == NULL)
                    657:                                cvs_log(LP_ERRNO,
                    658:                                    "failed to copy default arguments for %s",
                    659:                                    cmdp->cmd_name);
1.1       jfb       660:                }
                    661:        }
                    662:        if (ferror(fp)) {
1.23      xsa       663:                cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1       jfb       664:        }
                    665:
                    666:        (void)fclose(fp);
1.27      jfb       667: }
                    668:
                    669:
                    670: /*
                    671:  * cvs_var_set()
                    672:  *
                    673:  * Set the value of the variable <var> to <val>.  If there is no such variable,
                    674:  * a new entry is created, otherwise the old value is overwritten.
                    675:  * Returns 0 on success, or -1 on failure.
                    676:  */
                    677: int
                    678: cvs_var_set(const char *var, const char *val)
                    679: {
                    680:        char *valcp;
                    681:        const char *cp;
                    682:        struct cvs_var *vp;
                    683:
                    684:        if ((var == NULL) || (*var == '\0')) {
                    685:                cvs_log(LP_ERR, "no variable name");
                    686:                return (-1);
                    687:        }
                    688:
                    689:        /* sanity check on the name */
                    690:        for (cp = var; *cp != '\0'; cp++)
                    691:                if (!isalnum(*cp) && (*cp != '_')) {
                    692:                        cvs_log(LP_ERR,
                    693:                            "variable name `%s' contains invalid characters",
                    694:                            var);
                    695:                        return (-1);
                    696:                }
                    697:
                    698:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    699:                if (strcmp(vp->cv_name, var) == 0)
                    700:                        break;
                    701:
                    702:        valcp = strdup(val);
                    703:        if (valcp == NULL) {
                    704:                cvs_log(LP_ERRNO, "failed to allocate variable");
                    705:                return (-1);
                    706:        }
                    707:
                    708:        if (vp == NULL) {
                    709:                vp = (struct cvs_var *)malloc(sizeof(*vp));
                    710:                if (vp == NULL) {
                    711:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    712:                        free(valcp);
                    713:                        return (-1);
                    714:                }
                    715:                memset(vp, 0, sizeof(*vp));
                    716:
                    717:                vp->cv_name = strdup(var);
                    718:                if (vp->cv_name == NULL) {
                    719:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    720:                        free(valcp);
                    721:                        free(vp);
                    722:                        return (-1);
                    723:                }
                    724:
                    725:                TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
                    726:
                    727:        } else  /* free the previous value */
                    728:                free(vp->cv_val);
                    729:
                    730:        vp->cv_val = valcp;
                    731:
                    732:        return (0);
                    733: }
                    734:
                    735:
                    736: /*
                    737:  * cvs_var_set()
                    738:  *
                    739:  * Remove any entry for the variable <var>.
                    740:  * Returns 0 on success, or -1 on failure.
                    741:  */
                    742: int
                    743: cvs_var_unset(const char *var)
                    744: {
                    745:        struct cvs_var *vp;
                    746:
                    747:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    748:                if (strcmp(vp->cv_name, var) == 0) {
                    749:                        TAILQ_REMOVE(&cvs_variables, vp, cv_link);
                    750:                        free(vp->cv_name);
                    751:                        free(vp->cv_val);
                    752:                        free(vp);
                    753:                        return (0);
                    754:                }
                    755:
                    756:        return (-1);
                    757:
                    758: }
                    759:
                    760:
                    761: /*
                    762:  * cvs_var_get()
                    763:  *
                    764:  * Get the value associated with the variable <var>.  Returns a pointer to the
                    765:  * value string on success, or NULL if the variable does not exist.
                    766:  */
                    767:
                    768: const char*
                    769: cvs_var_get(const char *var)
                    770: {
                    771:        struct cvs_var *vp;
                    772:
                    773:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    774:                if (strcmp(vp->cv_name, var) == 0)
                    775:                        return (vp->cv_val);
                    776:
                    777:        return (NULL);
1.1       jfb       778: }