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

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     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
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     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>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include <signal.h>
                     37: #include <string.h>
                     38: #include <sysexits.h>
                     39:
                     40: #include "cvs.h"
                     41: #include "log.h"
1.8     ! jfb        42: #include "file.h"
1.1       jfb        43:
                     44:
                     45: extern char *__progname;
                     46:
                     47:
                     48: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
                     49: int verbosity = 2;
                     50:
                     51:
                     52:
                     53: /* compression level used with zlib, 0 meaning no compression taking place */
                     54: int   cvs_compress = 0;
                     55: int   cvs_trace = 0;
                     56: int   cvs_nolog = 0;
                     57: int   cvs_readonly = 0;
                     58:
                     59: /* name of the command we are running */
                     60: char *cvs_command;
1.6       jfb        61: int   cvs_cmdop;
1.1       jfb        62: char *cvs_rootstr;
                     63: char *cvs_rsh = CVS_RSH_DEFAULT;
                     64: char *cvs_editor = CVS_EDITOR_DEFAULT;
                     65:
                     66: struct cvsroot *cvs_root = NULL;
                     67:
                     68:
                     69: /*
                     70:  * Command dispatch table
                     71:  * ----------------------
                     72:  *
                     73:  * The synopsis field should only contain the list of arguments that the
                     74:  * command supports, without the actual command's name.
                     75:  *
                     76:  * Command handlers are expected to return 0 if no error occured, or one of
                     77:  * the values known in sysexits.h in case of an error.  In case the error
                     78:  * returned is EX_USAGE, the command's usage string is printed to standard
                     79:  * error before returning.
                     80:  */
                     81:
                     82: static struct cvs_cmd {
1.6       jfb        83:        int     cmd_op;
1.1       jfb        84:        char    cmd_name[CVS_CMD_MAXNAMELEN];
                     85:        char    cmd_alias[CVS_CMD_MAXALIAS][CVS_CMD_MAXNAMELEN];
                     86:        int   (*cmd_hdlr)(int, char **);
                     87:        char   *cmd_synopsis;
                     88:        char    cmd_descr[CVS_CMD_MAXDESCRLEN];
                     89: } cvs_cdt[] = {
                     90:        {
1.6       jfb        91:                CVS_OP_ADD, "add",      { "ad",  "new" }, cvs_add,
1.1       jfb        92:                "[-m msg] file ...",
                     93:                "Add a new file/directory to the repository",
                     94:        },
                     95:        {
1.6       jfb        96:                -1, "admin",    { "adm", "rcs" }, NULL,
1.1       jfb        97:                "",
                     98:                "Administration front end for rcs",
                     99:        },
                    100:        {
1.6       jfb       101:                CVS_OP_ANNOTATE, "annotate", { "ann"        }, NULL,
1.1       jfb       102:                "",
                    103:                "Show last revision where each line was modified",
                    104:        },
                    105:        {
1.6       jfb       106:                CVS_OP_CHECKOUT, "checkout", { "co",  "get" }, cvs_checkout,
1.1       jfb       107:                "",
                    108:                "Checkout sources for editing",
                    109:        },
                    110:        {
1.6       jfb       111:                CVS_OP_COMMIT, "commit",   { "ci",  "com" }, cvs_commit,
1.4       jfb       112:                "[-flR] [-F logfile | -m msg] [-r rev] ...",
1.1       jfb       113:                "Check files into the repository",
                    114:        },
                    115:        {
1.6       jfb       116:                CVS_OP_DIFF, "diff",     { "di",  "dif" }, cvs_diff,
1.3       jfb       117:                "[-cilu] [-D date] [-r rev] ...",
1.1       jfb       118:                "Show differences between revisions",
                    119:        },
                    120:        {
1.6       jfb       121:                -1, "edit",     {              }, NULL,
1.1       jfb       122:                "",
                    123:                "Get ready to edit a watched file",
                    124:        },
                    125:        {
1.6       jfb       126:                -1, "editors",  {              }, NULL,
1.1       jfb       127:                "",
                    128:                "See who is editing a watched file",
                    129:        },
                    130:        {
1.6       jfb       131:                -1, "export",   { "ex",  "exp" }, NULL,
1.1       jfb       132:                "",
                    133:                "Export sources from CVS, similar to checkout",
                    134:        },
                    135:        {
1.6       jfb       136:                CVS_OP_HISTORY, "history",  { "hi",  "his" }, cvs_history,
1.1       jfb       137:                "",
                    138:                "Show repository access history",
                    139:        },
                    140:        {
1.6       jfb       141:                CVS_OP_IMPORT, "import",   { "im",  "imp" }, NULL,
1.1       jfb       142:                "",
                    143:                "Import sources into CVS, using vendor branches",
                    144:        },
                    145:        {
1.6       jfb       146:                CVS_OP_INIT, "init",     {              }, cvs_init,
1.1       jfb       147:                "",
                    148:                "Create a CVS repository if it doesn't exist",
                    149:        },
                    150: #if defined(HAVE_KERBEROS)
                    151:        {
                    152:                "kserver",  {}, NULL
                    153:                "",
                    154:                "Start a Kerberos authentication CVS server",
                    155:        },
                    156: #endif
                    157:        {
1.6       jfb       158:                CVS_OP_LOG, "log",      { "lo"         }, cvs_getlog,
1.1       jfb       159:                "",
                    160:                "Print out history information for files",
                    161:        },
                    162:        {
1.6       jfb       163:                -1, "login",    {}, NULL,
1.1       jfb       164:                "",
                    165:                "Prompt for password for authenticating server",
                    166:        },
                    167:        {
1.6       jfb       168:                -1, "logout",   {}, NULL,
1.1       jfb       169:                "",
                    170:                "Removes entry in .cvspass for remote repository",
                    171:        },
                    172:        {
1.6       jfb       173:                -1, "rdiff",    {}, NULL,
1.1       jfb       174:                "",
                    175:                "Create 'patch' format diffs between releases",
                    176:        },
                    177:        {
1.6       jfb       178:                -1, "release",  {}, NULL,
1.1       jfb       179:                "",
                    180:                "Indicate that a Module is no longer in use",
                    181:        },
                    182:        {
1.6       jfb       183:                CVS_OP_REMOVE, "remove",   {}, NULL,
1.1       jfb       184:                "",
                    185:                "Remove an entry from the repository",
                    186:        },
                    187:        {
1.6       jfb       188:                -1, "rlog",     {}, NULL,
1.1       jfb       189:                "",
                    190:                "Print out history information for a module",
                    191:        },
                    192:        {
1.6       jfb       193:                -1, "rtag",     {}, NULL,
1.1       jfb       194:                "",
                    195:                "Add a symbolic tag to a module",
                    196:        },
                    197:        {
1.6       jfb       198:                CVS_OP_SERVER, "server",   {}, cvs_server,
1.1       jfb       199:                "",
                    200:                "Server mode",
                    201:        },
                    202:        {
1.7       jfb       203:                CVS_OP_STATUS, "status",   {}, cvs_status,
1.1       jfb       204:                "",
                    205:                "Display status information on checked out files",
                    206:        },
                    207:        {
1.6       jfb       208:                CVS_OP_TAG, "tag",      { "ta", }, NULL,
1.1       jfb       209:                "",
                    210:                "Add a symbolic tag to checked out version of files",
                    211:        },
                    212:        {
1.6       jfb       213:                -1, "unedit",   {}, NULL,
1.1       jfb       214:                "",
                    215:                "Undo an edit command",
                    216:        },
                    217:        {
1.6       jfb       218:                CVS_OP_UPDATE, "update",   {}, cvs_update,
1.1       jfb       219:                "",
                    220:                "Bring work tree in sync with repository",
                    221:        },
                    222:        {
1.6       jfb       223:                CVS_OP_VERSION, "version",  {}, cvs_version,
1.1       jfb       224:                "",
                    225:                "Show current CVS version(s)",
                    226:        },
                    227:        {
1.6       jfb       228:                -1, "watch",    {}, NULL,
1.1       jfb       229:                "",
                    230:                "Set watches",
                    231:        },
                    232:        {
1.6       jfb       233:                -1, "watchers", {}, NULL,
1.1       jfb       234:                "",
                    235:                "See who is watching a file",
                    236:        },
                    237: };
                    238:
                    239: #define CVS_NBCMD  (sizeof(cvs_cdt)/sizeof(cvs_cdt[0]))
                    240:
                    241:
                    242:
                    243: void             usage        (void);
                    244: void             sigchld_hdlr (int);
                    245: void             cvs_readrc   (void);
                    246: struct cvs_cmd*  cvs_findcmd  (const char *);
                    247:
                    248:
                    249:
                    250: /*
                    251:  * sigchld_hdlr()
                    252:  *
                    253:  * Handler for the SIGCHLD signal, which can be received in case we are
                    254:  * running a remote server and it dies.
                    255:  */
                    256:
                    257: void
                    258: sigchld_hdlr(int signo)
                    259: {
                    260:        int status;
                    261:        pid_t pid;
                    262:
                    263:        if ((pid = wait(&status)) == -1) {
                    264:        }
                    265: }
                    266:
                    267:
                    268: /*
                    269:  * usage()
                    270:  *
                    271:  * Display usage information.
                    272:  */
                    273:
                    274: void
                    275: usage(void)
                    276: {
                    277:        fprintf(stderr,
                    278:            "Usage: %s [-lQqtv] [-d root] [-e editor] [-z level] "
                    279:            "command [options] ...\n",
                    280:            __progname);
                    281: }
                    282:
                    283:
                    284: int
                    285: main(int argc, char **argv)
                    286: {
                    287:        char *envstr, *ep;
                    288:        int ret;
                    289:        u_int i, readrc;
                    290:        struct cvs_cmd *cmdp;
                    291:
                    292:        readrc = 1;
                    293:
                    294:        if (cvs_log_init(LD_STD, 0) < 0)
                    295:                err(1, "failed to initialize logging");
                    296:
                    297:        /* by default, be very verbose */
                    298:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);
                    299:
                    300: #ifdef DEBUG
                    301:        (void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
                    302: #endif
                    303:
                    304:        /* check environment so command-line options override it */
                    305:        if ((envstr = getenv("CVS_RSH")) != NULL)
                    306:                cvs_rsh = envstr;
                    307:
                    308:        if (((envstr = getenv("CVSEDITOR")) != NULL) ||
                    309:            ((envstr = getenv("VISUAL")) != NULL) ||
                    310:            ((envstr = getenv("EDITOR")) != NULL))
                    311:                cvs_editor = envstr;
                    312:
                    313:        while ((ret = getopt(argc, argv, "d:e:fHlnQqrtvz:")) != -1) {
                    314:                switch (ret) {
                    315:                case 'd':
                    316:                        cvs_rootstr = optarg;
                    317:                        break;
                    318:                case 'e':
                    319:                        cvs_editor = optarg;
                    320:                        break;
                    321:                case 'f':
                    322:                        readrc = 0;
                    323:                        break;
                    324:                case 'l':
                    325:                        cvs_nolog = 1;
                    326:                        break;
                    327:                case 'n':
                    328:                        break;
                    329:                case 'Q':
                    330:                        verbosity = 0;
                    331:                        break;
                    332:                case 'q':
                    333:                        /* don't override -Q */
                    334:                        if (verbosity > 1)
                    335:                                verbosity = 1;
                    336:                        break;
                    337:                case 'r':
                    338:                        cvs_readonly = 1;
                    339:                        break;
                    340:                case 't':
                    341:                        cvs_trace = 1;
                    342:                        break;
                    343:                case 'v':
                    344:                        printf("%s\n", CVS_VERSION);
                    345:                        exit(0);
                    346:                        /* NOTREACHED */
                    347:                        break;
                    348:                case 'z':
                    349:                        cvs_compress = (int)strtol(optarg, &ep, 10);
                    350:                        if (*ep != '\0')
                    351:                                errx(1, "error parsing compression level");
                    352:                        if (cvs_compress < 0 || cvs_compress > 9)
                    353:                                errx(1, "gzip compression level must be "
                    354:                                    "between 0 and 9");
                    355:                        break;
                    356:                default:
                    357:                        usage();
                    358:                        exit(EX_USAGE);
                    359:                }
                    360:        }
                    361:
                    362:        argc -= optind;
                    363:        argv += optind;
                    364:
                    365:        /* reset getopt() for use by commands */
                    366:        optind = 1;
                    367:        optreset = 1;
                    368:
                    369:        if (argc == 0) {
                    370:                usage();
                    371:                exit(EX_USAGE);
                    372:        }
                    373:
                    374:        /* setup signal handlers */
                    375:        signal(SIGCHLD, sigchld_hdlr);
                    376:
1.2       jfb       377:        cvs_file_init();
                    378:
1.1       jfb       379:        if (readrc)
                    380:                cvs_readrc();
                    381:
                    382:        cvs_command = argv[0];
                    383:        ret = -1;
                    384:
                    385:        cmdp = cvs_findcmd(cvs_command);
                    386:        if (cmdp == NULL) {
                    387:                fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
                    388:                fprintf(stderr, "CVS commands are:\n");
                    389:                for (i = 0; i < CVS_NBCMD; i++)
                    390:                        fprintf(stderr, "\t%-16s%s\n",
                    391:                            cvs_cdt[i].cmd_name, cvs_cdt[i].cmd_descr);
                    392:                exit(EX_USAGE);
                    393:        }
                    394:
                    395:        if (cmdp->cmd_hdlr == NULL) {
                    396:                cvs_log(LP_ERR, "command `%s' not implemented", cvs_command);
                    397:                exit(1);
                    398:        }
1.6       jfb       399:
                    400:        cvs_cmdop = cmdp->cmd_op;
1.1       jfb       401:
                    402:        ret = (*cmdp->cmd_hdlr)(argc, argv);
                    403:        if (ret == EX_USAGE) {
                    404:                fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
                    405:                    cmdp->cmd_synopsis);
                    406:        }
                    407:
                    408:        return (ret);
                    409: }
                    410:
                    411:
                    412: /*
                    413:  * cvs_findcmd()
                    414:  *
                    415:  * Find the entry in the command dispatch table whose name or one of its
                    416:  * aliases matches <cmd>.
                    417:  * Returns a pointer to the command entry on success, NULL on failure.
                    418:  */
                    419:
                    420: struct cvs_cmd*
                    421: cvs_findcmd(const char *cmd)
                    422: {
                    423:        u_int i, j;
                    424:        struct cvs_cmd *cmdp;
                    425:
                    426:        cmdp = NULL;
                    427:
                    428:        for (i = 0; (i < CVS_NBCMD) && (cmdp == NULL); i++) {
                    429:                if (strcmp(cmd, cvs_cdt[i].cmd_name) == 0)
                    430:                        cmdp = &cvs_cdt[i];
                    431:                else {
                    432:                        for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
                    433:                                if (strcmp(cmd, cvs_cdt[i].cmd_alias[j]) == 0) {
                    434:                                        cmdp = &cvs_cdt[i];
                    435:                                        break;
                    436:                                }
                    437:                        }
                    438:                }
                    439:        }
                    440:
                    441:        return (cmdp);
                    442: }
                    443:
                    444:
                    445: /*
                    446:  * cvs_readrc()
                    447:  *
                    448:  * Read the CVS `.cvsrc' file in the user's home directory.  If the file
                    449:  * exists, it should contain a list of arguments that should always be given
                    450:  * implicitly to the specified commands.
                    451:  */
                    452:
                    453: void
                    454: cvs_readrc(void)
                    455: {
                    456:        char rcpath[MAXPATHLEN], linebuf[128], *lp;
                    457:        struct cvs_cmd *cmdp;
                    458:        struct passwd *pw;
                    459:        FILE *fp;
                    460:
                    461:        pw = getpwuid(getuid());
                    462:        if (pw == NULL) {
                    463:                cvs_log(LP_NOTICE, "failed to get user's password entry");
                    464:                return;
                    465:        }
                    466:
                    467:        snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
                    468:
                    469:        fp = fopen(rcpath, "r");
                    470:        if (fp == NULL) {
                    471:                if (errno != ENOENT)
                    472:                        cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
                    473:                            strerror(errno));
                    474:                return;
                    475:        }
                    476:
                    477:        while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
                    478:                lp = strchr(linebuf, ' ');
                    479:
                    480:                /* ignore lines with no arguments */
                    481:                if (lp == NULL)
                    482:                        continue;
                    483:
                    484:                *(lp++) = '\0';
                    485:                if (strcmp(linebuf, "cvs") == 0) {
                    486:                        /* global options */
                    487:                }
                    488:                else {
                    489:                        cmdp = cvs_findcmd(linebuf);
                    490:                        if (cmdp == NULL) {
                    491:                                cvs_log(LP_NOTICE,
                    492:                                    "unknown command `%s' in cvsrc",
                    493:                                    linebuf);
                    494:                                continue;
                    495:                        }
                    496:                }
                    497:        }
                    498:        if (ferror(fp)) {
                    499:                cvs_log(LP_NOTICE, "failed to read line from cvsrc");
                    500:        }
                    501:
                    502:        (void)fclose(fp);
                    503: }