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

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