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

1.46    ! joris       1: /*     $OpenBSD: cvs.c,v 1.45 2005/03/30 17:43:04 joris 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: #include <sysexits.h>
                     40:
                     41: #include "cvs.h"
                     42: #include "log.h"
1.8       jfb        43: #include "file.h"
1.44      jfb        44: #include "strtab.h"
1.1       jfb        45:
                     46:
                     47: extern char *__progname;
                     48:
                     49:
                     50: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
                     51: int verbosity = 2;
                     52:
                     53: /* compression level used with zlib, 0 meaning no compression taking place */
                     54: int   cvs_compress = 0;
1.17      jfb        55: int   cvs_readrc = 1;          /* read .cvsrc on startup */
1.1       jfb        56: int   cvs_trace = 0;
                     57: int   cvs_nolog = 0;
                     58: int   cvs_readonly = 0;
1.17      jfb        59: int   cvs_nocase = 0;   /* set to 1 to disable filename case sensitivity */
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.1       jfb        84:  * the values known in sysexits.h in case of an error.  In case the error
                     85:  * returned is EX_USAGE, the command's usage string is printed to standard
                     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.37      jfb        91:                "[-k opt] [-m msg] file ...",
                     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 ...",
                    114:                "AcD:d:fj:k:lNnPRr: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.34      jfb       127:                "[-cilNpu] [-D date] [-r rev] ...",
                    128:                "cD:ilNpr: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.45      joris     187:                "d:hlRr:",
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.36      jfb       213:                CVS_OP_RELEASE, "release",  {}, NULL,
1.38      jfb       214:                "[-d]",
                    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.28      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.38      jfb       256:                "[-bcdFflR] [-D date | -r rev] tagname",
                    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:
                    302: void             usage        (void);
                    303: void             sigchld_hdlr (int);
1.42      joris     304: static void             cvs_read_rcfile   (void);
                    305: static struct cvs_cmd*  cvs_findcmd  (const char *);
1.17      jfb       306: int              cvs_getopt   (int, char **);
1.1       jfb       307:
                    308:
                    309: /*
                    310:  * usage()
                    311:  *
                    312:  * Display usage information.
                    313:  */
                    314: void
                    315: usage(void)
                    316: {
                    317:        fprintf(stderr,
1.27      jfb       318:            "Usage: %s [-flQqtv] [-d root] [-e editor] [-s var=val] [-z level] "
                    319:            "command [...]\n", __progname);
1.1       jfb       320: }
                    321:
                    322:
                    323: int
                    324: main(int argc, char **argv)
                    325: {
1.17      jfb       326:        char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
                    327:        int i, ret, cmd_argc;
1.1       jfb       328:        struct cvs_cmd *cmdp;
                    329:
1.27      jfb       330:        TAILQ_INIT(&cvs_variables);
                    331:
1.1       jfb       332:        if (cvs_log_init(LD_STD, 0) < 0)
                    333:                err(1, "failed to initialize logging");
                    334:
                    335:        /* by default, be very verbose */
                    336:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);
                    337:
                    338: #ifdef DEBUG
                    339:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
                    340: #endif
                    341:
1.44      jfb       342:        cvs_strtab_init();
                    343:
1.1       jfb       344:        /* check environment so command-line options override it */
                    345:        if ((envstr = getenv("CVS_RSH")) != NULL)
                    346:                cvs_rsh = envstr;
                    347:
                    348:        if (((envstr = getenv("CVSEDITOR")) != NULL) ||
                    349:            ((envstr = getenv("VISUAL")) != NULL) ||
                    350:            ((envstr = getenv("EDITOR")) != NULL))
                    351:                cvs_editor = envstr;
                    352:
1.17      jfb       353:        ret = cvs_getopt(argc, argv);
                    354:
                    355:        argc -= ret;
                    356:        argv += ret;
                    357:        if (argc == 0) {
                    358:                usage();
                    359:                exit(EX_USAGE);
                    360:        }
                    361:        cvs_command = argv[0];
                    362:
                    363:        if (cvs_readrc) {
                    364:                cvs_read_rcfile();
                    365:
                    366:                if (cvs_defargs != NULL) {
                    367:                        targv = cvs_makeargv(cvs_defargs, &i);
                    368:                        if (targv == NULL) {
                    369:                                cvs_log(LP_ERR,
                    370:                                    "failed to load default arguments to %s",
                    371:                                    __progname);
                    372:                                exit(EX_OSERR);
                    373:                        }
                    374:
                    375:                        cvs_getopt(i, targv);
                    376:                        cvs_freeargv(targv, i);
                    377:                        free(targv);
                    378:                }
                    379:        }
                    380:
                    381:        /* setup signal handlers */
                    382:        signal(SIGPIPE, SIG_IGN);
                    383:
1.40      jfb       384:        if (cvs_file_init() < 0) {
                    385:                cvs_log(LP_ERR, "failed to initialize file support");
                    386:                exit(1);
                    387:        }
1.17      jfb       388:
                    389:        ret = -1;
                    390:
                    391:        cmdp = cvs_findcmd(cvs_command);
                    392:        if (cmdp == NULL) {
                    393:                fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
                    394:                fprintf(stderr, "CVS commands are:\n");
                    395:                for (i = 0; i < (int)CVS_NBCMD; i++)
                    396:                        fprintf(stderr, "\t%-16s%s\n",
                    397:                            cvs_cdt[i].cmd_name, cvs_cdt[i].cmd_descr);
                    398:                exit(EX_USAGE);
                    399:        }
                    400:
1.45      joris     401:        if (cmdp->cmd_info == NULL) {
1.17      jfb       402:                cvs_log(LP_ERR, "command `%s' not implemented", cvs_command);
                    403:                exit(1);
                    404:        }
                    405:
                    406:        cvs_cmdop = cmdp->cmd_op;
                    407:
                    408:        cmd_argc = 0;
                    409:        memset(cmd_argv, 0, sizeof(cmd_argv));
                    410:
                    411:        cmd_argv[cmd_argc++] = argv[0];
                    412:        if (cmdp->cmd_defargs != NULL) {
                    413:                /* transform into a new argument vector */
                    414:                ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
                    415:                    CVS_CMD_MAXARG - 1);
                    416:                if (ret < 0) {
                    417:                        cvs_log(LP_ERRNO, "failed to generate argument vector "
                    418:                            "from default arguments");
                    419:                        exit(EX_DATAERR);
                    420:                }
                    421:                cmd_argc += ret;
                    422:        }
                    423:        for (ret = 1; ret < argc; ret++)
                    424:                cmd_argv[cmd_argc++] = argv[ret];
                    425:
1.45      joris     426:        ret = cvs_startcmd(cmdp, cmd_argc, cmd_argv);
1.17      jfb       427:        if (ret == EX_USAGE) {
                    428:                fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
                    429:                    cmdp->cmd_synopsis);
                    430:        }
                    431:
1.45      joris     432:        if (cmdp->cmd_info->cmd_cleanup != NULL)
                    433:                cmdp->cmd_info->cmd_cleanup();
1.17      jfb       434:        if (cvs_files != NULL)
                    435:                cvs_file_free(cvs_files);
1.33      jfb       436:        if (cvs_msg != NULL)
                    437:                free(cvs_msg);
1.44      jfb       438:
                    439:        cvs_strtab_cleanup();
1.17      jfb       440:
                    441:        return (ret);
                    442: }
                    443:
                    444:
                    445: int
                    446: cvs_getopt(int argc, char **argv)
                    447: {
                    448:        int ret;
                    449:        char *ep;
                    450:
1.27      jfb       451:        while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:tvz:")) != -1) {
1.1       jfb       452:                switch (ret) {
1.11      jfb       453:                case 'b':
                    454:                        /*
                    455:                         * We do not care about the bin directory for RCS files
                    456:                         * as this program has no dependencies on RCS programs,
                    457:                         * so it is only here for backwards compatibility.
                    458:                         */
                    459:                        cvs_log(LP_NOTICE, "the -b argument is obsolete");
                    460:                        break;
1.1       jfb       461:                case 'd':
                    462:                        cvs_rootstr = optarg;
                    463:                        break;
                    464:                case 'e':
                    465:                        cvs_editor = optarg;
                    466:                        break;
                    467:                case 'f':
1.17      jfb       468:                        cvs_readrc = 0;
1.1       jfb       469:                        break;
                    470:                case 'l':
                    471:                        cvs_nolog = 1;
                    472:                        break;
                    473:                case 'n':
                    474:                        break;
                    475:                case 'Q':
                    476:                        verbosity = 0;
                    477:                        break;
                    478:                case 'q':
                    479:                        /* don't override -Q */
                    480:                        if (verbosity > 1)
                    481:                                verbosity = 1;
                    482:                        break;
                    483:                case 'r':
                    484:                        cvs_readonly = 1;
                    485:                        break;
1.27      jfb       486:                case 's':
                    487:                        ep = strchr(optarg, '=');
                    488:                        if (ep == NULL) {
                    489:                                cvs_log(LP_ERR, "no = in variable assignment");
                    490:                                exit(EX_USAGE);
                    491:                        }
                    492:                        *(ep++) = '\0';
                    493:                        if (cvs_var_set(optarg, ep) < 0)
                    494:                                exit(EX_USAGE);
                    495:                        break;
1.1       jfb       496:                case 't':
1.24      jfb       497:                        (void)cvs_log_filter(LP_FILTER_UNSET, LP_TRACE);
1.1       jfb       498:                        cvs_trace = 1;
                    499:                        break;
                    500:                case 'v':
                    501:                        printf("%s\n", CVS_VERSION);
                    502:                        exit(0);
                    503:                        /* NOTREACHED */
1.11      jfb       504:                        break;
                    505:                case 'x':
                    506:                        /*
                    507:                         * Kerberos encryption support, kept for compatibility
                    508:                         */
1.1       jfb       509:                        break;
                    510:                case 'z':
1.18      tedu      511:                        cvs_compress = (int)strtol(optarg, &ep, 10);
1.1       jfb       512:                        if (*ep != '\0')
                    513:                                errx(1, "error parsing compression level");
                    514:                        if (cvs_compress < 0 || cvs_compress > 9)
                    515:                                errx(1, "gzip compression level must be "
                    516:                                    "between 0 and 9");
                    517:                        break;
                    518:                default:
                    519:                        usage();
                    520:                        exit(EX_USAGE);
                    521:                }
                    522:        }
                    523:
1.17      jfb       524:        ret = optind;
1.1       jfb       525:        optind = 1;
1.17      jfb       526:        optreset = 1;   /* for next call */
1.12      jfb       527:
1.1       jfb       528:        return (ret);
                    529: }
                    530:
                    531:
                    532: /*
                    533:  * cvs_findcmd()
                    534:  *
                    535:  * Find the entry in the command dispatch table whose name or one of its
                    536:  * aliases matches <cmd>.
                    537:  * Returns a pointer to the command entry on success, NULL on failure.
                    538:  */
1.42      joris     539: static struct cvs_cmd*
1.1       jfb       540: cvs_findcmd(const char *cmd)
                    541: {
                    542:        u_int i, j;
                    543:        struct cvs_cmd *cmdp;
                    544:
                    545:        cmdp = NULL;
                    546:
                    547:        for (i = 0; (i < CVS_NBCMD) && (cmdp == NULL); i++) {
                    548:                if (strcmp(cmd, cvs_cdt[i].cmd_name) == 0)
                    549:                        cmdp = &cvs_cdt[i];
                    550:                else {
                    551:                        for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
                    552:                                if (strcmp(cmd, cvs_cdt[i].cmd_alias[j]) == 0) {
                    553:                                        cmdp = &cvs_cdt[i];
                    554:                                        break;
                    555:                                }
                    556:                        }
                    557:                }
                    558:        }
                    559:
                    560:        return (cmdp);
                    561: }
                    562:
                    563:
                    564: /*
1.17      jfb       565:  * cvs_read_rcfile()
1.1       jfb       566:  *
                    567:  * Read the CVS `.cvsrc' file in the user's home directory.  If the file
                    568:  * exists, it should contain a list of arguments that should always be given
                    569:  * implicitly to the specified commands.
                    570:  */
1.42      joris     571: static void
1.17      jfb       572: cvs_read_rcfile(void)
1.1       jfb       573: {
                    574:        char rcpath[MAXPATHLEN], linebuf[128], *lp;
1.23      xsa       575:        int linenum = 0;
1.17      jfb       576:        size_t len;
1.1       jfb       577:        struct cvs_cmd *cmdp;
                    578:        struct passwd *pw;
                    579:        FILE *fp;
                    580:
                    581:        pw = getpwuid(getuid());
                    582:        if (pw == NULL) {
                    583:                cvs_log(LP_NOTICE, "failed to get user's password entry");
                    584:                return;
                    585:        }
                    586:
                    587:        snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
                    588:
                    589:        fp = fopen(rcpath, "r");
                    590:        if (fp == NULL) {
                    591:                if (errno != ENOENT)
                    592:                        cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
                    593:                            strerror(errno));
                    594:                return;
                    595:        }
                    596:
                    597:        while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
1.23      xsa       598:                linenum++;
1.17      jfb       599:                if ((len = strlen(linebuf)) == 0)
                    600:                        continue;
                    601:                if (linebuf[len - 1] != '\n') {
1.23      xsa       602:                        cvs_log(LP_WARN, "line too long in `%s:%d'", rcpath,
                    603:                                linenum);
1.17      jfb       604:                        break;
                    605:                }
                    606:                linebuf[--len] = '\0';
                    607:
1.1       jfb       608:                lp = strchr(linebuf, ' ');
                    609:                if (lp == NULL)
1.17      jfb       610:                        continue;       /* ignore lines with no arguments */
                    611:                *lp = '\0';
1.1       jfb       612:                if (strcmp(linebuf, "cvs") == 0) {
1.17      jfb       613:                        /*
                    614:                         * Global default options.  In the case of cvs only,
                    615:                         * we keep the 'cvs' string as first argument because
                    616:                         * getopt() does not like starting at index 0 for
                    617:                         * argument processing.
                    618:                         */
                    619:                        *lp = ' ';
                    620:                        cvs_defargs = strdup(linebuf);
                    621:                        if (cvs_defargs == NULL)
                    622:                                cvs_log(LP_ERRNO,
                    623:                                    "failed to copy global arguments");
1.15      deraadt   624:                } else {
1.17      jfb       625:                        lp++;
1.1       jfb       626:                        cmdp = cvs_findcmd(linebuf);
                    627:                        if (cmdp == NULL) {
                    628:                                cvs_log(LP_NOTICE,
1.25      xsa       629:                                    "unknown command `%s' in `%s:%d'",
                    630:                                    linebuf, rcpath, linenum);
1.1       jfb       631:                                continue;
                    632:                        }
1.17      jfb       633:
                    634:                        cmdp->cmd_defargs = strdup(lp);
                    635:                        if (cmdp->cmd_defargs == NULL)
                    636:                                cvs_log(LP_ERRNO,
                    637:                                    "failed to copy default arguments for %s",
                    638:                                    cmdp->cmd_name);
1.1       jfb       639:                }
                    640:        }
                    641:        if (ferror(fp)) {
1.23      xsa       642:                cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1       jfb       643:        }
                    644:
                    645:        (void)fclose(fp);
1.27      jfb       646: }
                    647:
                    648:
                    649: /*
                    650:  * cvs_var_set()
                    651:  *
                    652:  * Set the value of the variable <var> to <val>.  If there is no such variable,
                    653:  * a new entry is created, otherwise the old value is overwritten.
                    654:  * Returns 0 on success, or -1 on failure.
                    655:  */
                    656: int
                    657: cvs_var_set(const char *var, const char *val)
                    658: {
                    659:        char *valcp;
                    660:        const char *cp;
                    661:        struct cvs_var *vp;
                    662:
                    663:        if ((var == NULL) || (*var == '\0')) {
                    664:                cvs_log(LP_ERR, "no variable name");
                    665:                return (-1);
                    666:        }
                    667:
                    668:        /* sanity check on the name */
                    669:        for (cp = var; *cp != '\0'; cp++)
                    670:                if (!isalnum(*cp) && (*cp != '_')) {
                    671:                        cvs_log(LP_ERR,
                    672:                            "variable name `%s' contains invalid characters",
                    673:                            var);
                    674:                        return (-1);
                    675:                }
                    676:
                    677:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    678:                if (strcmp(vp->cv_name, var) == 0)
                    679:                        break;
                    680:
                    681:        valcp = strdup(val);
                    682:        if (valcp == NULL) {
                    683:                cvs_log(LP_ERRNO, "failed to allocate variable");
                    684:                return (-1);
                    685:        }
                    686:
                    687:        if (vp == NULL) {
                    688:                vp = (struct cvs_var *)malloc(sizeof(*vp));
                    689:                if (vp == NULL) {
                    690:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    691:                        free(valcp);
                    692:                        return (-1);
                    693:                }
                    694:                memset(vp, 0, sizeof(*vp));
                    695:
                    696:                vp->cv_name = strdup(var);
                    697:                if (vp->cv_name == NULL) {
                    698:                        cvs_log(LP_ERRNO, "failed to allocate variable");
                    699:                        free(valcp);
                    700:                        free(vp);
                    701:                        return (-1);
                    702:                }
                    703:
                    704:                TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
                    705:
                    706:        } else  /* free the previous value */
                    707:                free(vp->cv_val);
                    708:
                    709:        vp->cv_val = valcp;
                    710:
                    711:        return (0);
                    712: }
                    713:
                    714:
                    715: /*
                    716:  * cvs_var_set()
                    717:  *
                    718:  * Remove any entry for the variable <var>.
                    719:  * Returns 0 on success, or -1 on failure.
                    720:  */
                    721: int
                    722: cvs_var_unset(const char *var)
                    723: {
                    724:        struct cvs_var *vp;
                    725:
                    726:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    727:                if (strcmp(vp->cv_name, var) == 0) {
                    728:                        TAILQ_REMOVE(&cvs_variables, vp, cv_link);
                    729:                        free(vp->cv_name);
                    730:                        free(vp->cv_val);
                    731:                        free(vp);
                    732:                        return (0);
                    733:                }
                    734:
                    735:        return (-1);
                    736:
                    737: }
                    738:
                    739:
                    740: /*
                    741:  * cvs_var_get()
                    742:  *
                    743:  * Get the value associated with the variable <var>.  Returns a pointer to the
                    744:  * value string on success, or NULL if the variable does not exist.
                    745:  */
                    746:
                    747: const char*
                    748: cvs_var_get(const char *var)
                    749: {
                    750:        struct cvs_var *vp;
                    751:
                    752:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    753:                if (strcmp(vp->cv_name, var) == 0)
                    754:                        return (vp->cv_val);
                    755:
                    756:        return (NULL);
1.1       jfb       757: }