Annotation of src/usr.bin/cvs/cvs.c, Revision 1.26
1.26 ! xsa 1: /* $OpenBSD: cvs.c,v 1.25 2004/12/15 16:28:14 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>
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: /* compression level used with zlib, 0 meaning no compression taking place */
52: int cvs_compress = 0;
1.17 jfb 53: int cvs_readrc = 1; /* read .cvsrc on startup */
1.1 jfb 54: int cvs_trace = 0;
55: int cvs_nolog = 0;
56: int cvs_readonly = 0;
1.17 jfb 57: int cvs_nocase = 0; /* set to 1 to disable filename case sensitivity */
1.1 jfb 58:
1.17 jfb 59: char *cvs_defargs; /* default global arguments from .cvsrc */
60: char *cvs_command; /* name of the command we are running */
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:
1.13 krapht 66: char *cvs_msg = NULL;
67:
1.10 jfb 68: /* hierarchy of all the files affected by the command */
69: CVSFILE *cvs_files;
70:
71:
1.1 jfb 72: /*
73: * Command dispatch table
74: * ----------------------
75: *
76: * The synopsis field should only contain the list of arguments that the
77: * command supports, without the actual command's name.
78: *
79: * Command handlers are expected to return 0 if no error occured, or one of
80: * the values known in sysexits.h in case of an error. In case the error
81: * returned is EX_USAGE, the command's usage string is printed to standard
82: * error before returning.
83: */
84: static struct cvs_cmd {
1.6 jfb 85: int cmd_op;
1.1 jfb 86: char cmd_name[CVS_CMD_MAXNAMELEN];
87: char cmd_alias[CVS_CMD_MAXALIAS][CVS_CMD_MAXNAMELEN];
88: int (*cmd_hdlr)(int, char **);
89: char *cmd_synopsis;
1.13 krapht 90: char *cmd_opts;
1.1 jfb 91: char cmd_descr[CVS_CMD_MAXDESCRLEN];
1.17 jfb 92: char *cmd_defargs;
1.1 jfb 93: } cvs_cdt[] = {
94: {
1.6 jfb 95: CVS_OP_ADD, "add", { "ad", "new" }, cvs_add,
1.1 jfb 96: "[-m msg] file ...",
1.13 krapht 97: "",
1.1 jfb 98: "Add a new file/directory to the repository",
1.17 jfb 99: NULL,
1.1 jfb 100: },
101: {
1.6 jfb 102: -1, "admin", { "adm", "rcs" }, NULL,
1.1 jfb 103: "",
1.13 krapht 104: "",
1.1 jfb 105: "Administration front end for rcs",
1.17 jfb 106: NULL,
1.1 jfb 107: },
108: {
1.19 jfb 109: CVS_OP_ANNOTATE, "annotate", { "ann" }, cvs_annotate,
110: "[-FflR] [-D date | -r rev] file ...",
1.13 krapht 111: "",
1.1 jfb 112: "Show last revision where each line was modified",
1.17 jfb 113: NULL,
1.1 jfb 114: },
115: {
1.6 jfb 116: CVS_OP_CHECKOUT, "checkout", { "co", "get" }, cvs_checkout,
1.1 jfb 117: "",
1.13 krapht 118: "",
1.1 jfb 119: "Checkout sources for editing",
1.17 jfb 120: NULL,
1.1 jfb 121: },
122: {
1.6 jfb 123: CVS_OP_COMMIT, "commit", { "ci", "com" }, cvs_commit,
1.4 jfb 124: "[-flR] [-F logfile | -m msg] [-r rev] ...",
1.13 krapht 125: "F:flm:Rr:",
1.1 jfb 126: "Check files into the repository",
1.17 jfb 127: NULL,
1.1 jfb 128: },
129: {
1.6 jfb 130: CVS_OP_DIFF, "diff", { "di", "dif" }, cvs_diff,
1.3 jfb 131: "[-cilu] [-D date] [-r rev] ...",
1.13 krapht 132: "cD:ilur:",
1.1 jfb 133: "Show differences between revisions",
1.17 jfb 134: NULL,
1.1 jfb 135: },
136: {
1.6 jfb 137: -1, "edit", { }, NULL,
1.1 jfb 138: "",
1.13 krapht 139: "",
1.1 jfb 140: "Get ready to edit a watched file",
1.17 jfb 141: NULL,
1.1 jfb 142: },
143: {
1.6 jfb 144: -1, "editors", { }, NULL,
1.1 jfb 145: "",
1.13 krapht 146: "",
1.1 jfb 147: "See who is editing a watched file",
1.17 jfb 148: NULL,
1.1 jfb 149: },
150: {
1.6 jfb 151: -1, "export", { "ex", "exp" }, NULL,
1.1 jfb 152: "",
1.13 krapht 153: "",
1.1 jfb 154: "Export sources from CVS, similar to checkout",
1.17 jfb 155: NULL,
1.1 jfb 156: },
157: {
1.6 jfb 158: CVS_OP_HISTORY, "history", { "hi", "his" }, cvs_history,
1.1 jfb 159: "",
1.13 krapht 160: "",
1.1 jfb 161: "Show repository access history",
1.17 jfb 162: NULL,
1.1 jfb 163: },
164: {
1.14 krapht 165: CVS_OP_IMPORT, "import", { "im", "imp" }, NULL,
1.13 krapht 166: "[-d] [-b branch] [-I ign] [-k subst] [-m msg] "
167: "repository vendor-tag release-tags ...",
168: "b:dI:k:m:",
1.1 jfb 169: "Import sources into CVS, using vendor branches",
1.17 jfb 170: NULL,
1.1 jfb 171: },
172: {
1.6 jfb 173: CVS_OP_INIT, "init", { }, cvs_init,
1.1 jfb 174: "",
1.13 krapht 175: "",
1.1 jfb 176: "Create a CVS repository if it doesn't exist",
1.17 jfb 177: NULL,
1.1 jfb 178: },
179: #if defined(HAVE_KERBEROS)
180: {
181: "kserver", {}, NULL
182: "",
1.13 krapht 183: "",
1.1 jfb 184: "Start a Kerberos authentication CVS server",
1.17 jfb 185: NULL,
1.1 jfb 186: },
187: #endif
188: {
1.6 jfb 189: CVS_OP_LOG, "log", { "lo" }, cvs_getlog,
1.1 jfb 190: "",
1.13 krapht 191: "",
1.1 jfb 192: "Print out history information for files",
1.17 jfb 193: NULL,
1.1 jfb 194: },
195: {
1.6 jfb 196: -1, "login", {}, NULL,
1.1 jfb 197: "",
1.13 krapht 198: "",
1.1 jfb 199: "Prompt for password for authenticating server",
1.17 jfb 200: NULL,
1.1 jfb 201: },
202: {
1.6 jfb 203: -1, "logout", {}, NULL,
1.1 jfb 204: "",
1.13 krapht 205: "",
1.1 jfb 206: "Removes entry in .cvspass for remote repository",
1.17 jfb 207: NULL,
1.1 jfb 208: },
209: {
1.6 jfb 210: -1, "rdiff", {}, NULL,
1.1 jfb 211: "",
1.13 krapht 212: "",
1.1 jfb 213: "Create 'patch' format diffs between releases",
1.17 jfb 214: NULL,
1.1 jfb 215: },
216: {
1.6 jfb 217: -1, "release", {}, NULL,
1.1 jfb 218: "",
1.13 krapht 219: "",
1.1 jfb 220: "Indicate that a Module is no longer in use",
1.17 jfb 221: NULL,
1.1 jfb 222: },
223: {
1.26 ! xsa 224: CVS_OP_REMOVE, "remove", { "rm", "delete" }, cvs_remove,
! 225: "[-flR] files ...",
1.13 krapht 226: "",
1.1 jfb 227: "Remove an entry from the repository",
1.17 jfb 228: NULL,
1.1 jfb 229: },
230: {
1.6 jfb 231: -1, "rlog", {}, NULL,
1.1 jfb 232: "",
1.13 krapht 233: "",
1.1 jfb 234: "Print out history information for a module",
1.17 jfb 235: NULL,
1.1 jfb 236: },
237: {
1.6 jfb 238: -1, "rtag", {}, NULL,
1.1 jfb 239: "",
1.13 krapht 240: "",
1.1 jfb 241: "Add a symbolic tag to a module",
1.17 jfb 242: NULL,
1.1 jfb 243: },
244: {
1.6 jfb 245: CVS_OP_SERVER, "server", {}, cvs_server,
1.1 jfb 246: "",
1.13 krapht 247: "",
1.1 jfb 248: "Server mode",
1.17 jfb 249: NULL,
1.1 jfb 250: },
251: {
1.16 jfb 252: CVS_OP_STATUS, "status", { "st", "stat" }, cvs_status,
1.1 jfb 253: "",
1.13 krapht 254: "",
1.1 jfb 255: "Display status information on checked out files",
1.17 jfb 256: NULL,
1.1 jfb 257: },
258: {
1.22 jfb 259: CVS_OP_TAG, "tag", { "ta", "freeze" }, cvs_tag,
1.1 jfb 260: "",
1.13 krapht 261: "",
1.1 jfb 262: "Add a symbolic tag to checked out version of files",
1.17 jfb 263: NULL,
1.1 jfb 264: },
265: {
1.6 jfb 266: -1, "unedit", {}, NULL,
1.1 jfb 267: "",
1.13 krapht 268: "",
1.1 jfb 269: "Undo an edit command",
1.17 jfb 270: NULL,
1.1 jfb 271: },
272: {
1.16 jfb 273: CVS_OP_UPDATE, "update", { "up", "upd" }, cvs_update,
1.1 jfb 274: "",
1.13 krapht 275: "",
1.1 jfb 276: "Bring work tree in sync with repository",
1.17 jfb 277: NULL,
1.1 jfb 278: },
279: {
1.16 jfb 280: CVS_OP_VERSION, "version", { "ve", "ver" }, cvs_version,
1.13 krapht 281: "", "",
1.1 jfb 282: "Show current CVS version(s)",
1.17 jfb 283: NULL,
1.1 jfb 284: },
285: {
1.6 jfb 286: -1, "watch", {}, NULL,
1.1 jfb 287: "",
1.13 krapht 288: "",
1.1 jfb 289: "Set watches",
1.17 jfb 290: NULL,
1.1 jfb 291: },
292: {
1.6 jfb 293: -1, "watchers", {}, NULL,
1.1 jfb 294: "",
1.13 krapht 295: "",
1.1 jfb 296: "See who is watching a file",
1.17 jfb 297: NULL,
1.1 jfb 298: },
299: };
300:
301: #define CVS_NBCMD (sizeof(cvs_cdt)/sizeof(cvs_cdt[0]))
302:
303:
304:
305: void usage (void);
306: void sigchld_hdlr (int);
1.17 jfb 307: void cvs_read_rcfile (void);
1.1 jfb 308: struct cvs_cmd* cvs_findcmd (const char *);
1.17 jfb 309: int cvs_getopt (int, char **);
1.1 jfb 310:
311:
312: /*
313: * usage()
314: *
315: * Display usage information.
316: */
317: void
318: usage(void)
319: {
320: fprintf(stderr,
1.20 jmc 321: "Usage: %s [-flQqtv] [-d root] [-e editor] [-z level] command [...]\n", __progname);
1.1 jfb 322: }
323:
324:
325: int
326: main(int argc, char **argv)
327: {
1.17 jfb 328: char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
329: int i, ret, cmd_argc;
1.1 jfb 330: struct cvs_cmd *cmdp;
331:
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:
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();
357: exit(EX_USAGE);
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);
370: exit(EX_OSERR);
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:
382: cvs_file_init();
383:
384: ret = -1;
385:
386: cmdp = cvs_findcmd(cvs_command);
387: if (cmdp == NULL) {
388: fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
389: fprintf(stderr, "CVS commands are:\n");
390: for (i = 0; i < (int)CVS_NBCMD; i++)
391: fprintf(stderr, "\t%-16s%s\n",
392: cvs_cdt[i].cmd_name, cvs_cdt[i].cmd_descr);
393: exit(EX_USAGE);
394: }
395:
396: if (cmdp->cmd_hdlr == NULL) {
397: cvs_log(LP_ERR, "command `%s' not implemented", cvs_command);
398: exit(1);
399: }
400:
401: cvs_cmdop = cmdp->cmd_op;
402:
403: cmd_argc = 0;
404: memset(cmd_argv, 0, sizeof(cmd_argv));
405:
406: cmd_argv[cmd_argc++] = argv[0];
407: if (cmdp->cmd_defargs != NULL) {
408: /* transform into a new argument vector */
409: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
410: CVS_CMD_MAXARG - 1);
411: if (ret < 0) {
412: cvs_log(LP_ERRNO, "failed to generate argument vector "
413: "from default arguments");
414: exit(EX_DATAERR);
415: }
416: cmd_argc += ret;
417: }
418: for (ret = 1; ret < argc; ret++)
419: cmd_argv[cmd_argc++] = argv[ret];
420:
421: ret = (*cmdp->cmd_hdlr)(cmd_argc, cmd_argv);
422: if (ret == EX_USAGE) {
423: fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
424: cmdp->cmd_synopsis);
425: }
426:
427: if (cvs_files != NULL)
428: cvs_file_free(cvs_files);
429:
430: return (ret);
431: }
432:
433:
434: int
435: cvs_getopt(int argc, char **argv)
436: {
437: int ret;
438: char *ep;
439:
1.11 jfb 440: while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrtvz:")) != -1) {
1.1 jfb 441: switch (ret) {
1.11 jfb 442: case 'b':
443: /*
444: * We do not care about the bin directory for RCS files
445: * as this program has no dependencies on RCS programs,
446: * so it is only here for backwards compatibility.
447: */
448: cvs_log(LP_NOTICE, "the -b argument is obsolete");
449: break;
1.1 jfb 450: case 'd':
451: cvs_rootstr = optarg;
452: break;
453: case 'e':
454: cvs_editor = optarg;
455: break;
456: case 'f':
1.17 jfb 457: cvs_readrc = 0;
1.1 jfb 458: break;
459: case 'l':
460: cvs_nolog = 1;
461: break;
462: case 'n':
463: break;
464: case 'Q':
465: verbosity = 0;
466: break;
467: case 'q':
468: /* don't override -Q */
469: if (verbosity > 1)
470: verbosity = 1;
471: break;
472: case 'r':
473: cvs_readonly = 1;
474: break;
475: case 't':
1.24 jfb 476: (void)cvs_log_filter(LP_FILTER_UNSET, LP_TRACE);
1.1 jfb 477: cvs_trace = 1;
478: break;
479: case 'v':
480: printf("%s\n", CVS_VERSION);
481: exit(0);
482: /* NOTREACHED */
1.11 jfb 483: break;
484: case 'x':
485: /*
486: * Kerberos encryption support, kept for compatibility
487: */
1.1 jfb 488: break;
489: case 'z':
1.18 tedu 490: cvs_compress = (int)strtol(optarg, &ep, 10);
1.1 jfb 491: if (*ep != '\0')
492: errx(1, "error parsing compression level");
493: if (cvs_compress < 0 || cvs_compress > 9)
494: errx(1, "gzip compression level must be "
495: "between 0 and 9");
496: break;
497: default:
498: usage();
499: exit(EX_USAGE);
500: }
501: }
502:
1.17 jfb 503: ret = optind;
1.1 jfb 504: optind = 1;
1.17 jfb 505: optreset = 1; /* for next call */
1.12 jfb 506:
1.1 jfb 507: return (ret);
508: }
509:
510:
511: /*
512: * cvs_findcmd()
513: *
514: * Find the entry in the command dispatch table whose name or one of its
515: * aliases matches <cmd>.
516: * Returns a pointer to the command entry on success, NULL on failure.
517: */
518: struct cvs_cmd*
519: cvs_findcmd(const char *cmd)
520: {
521: u_int i, j;
522: struct cvs_cmd *cmdp;
523:
524: cmdp = NULL;
525:
526: for (i = 0; (i < CVS_NBCMD) && (cmdp == NULL); i++) {
527: if (strcmp(cmd, cvs_cdt[i].cmd_name) == 0)
528: cmdp = &cvs_cdt[i];
529: else {
530: for (j = 0; j < CVS_CMD_MAXALIAS; j++) {
531: if (strcmp(cmd, cvs_cdt[i].cmd_alias[j]) == 0) {
532: cmdp = &cvs_cdt[i];
533: break;
534: }
535: }
536: }
537: }
538:
539: return (cmdp);
540: }
541:
542:
543: /*
1.17 jfb 544: * cvs_read_rcfile()
1.1 jfb 545: *
546: * Read the CVS `.cvsrc' file in the user's home directory. If the file
547: * exists, it should contain a list of arguments that should always be given
548: * implicitly to the specified commands.
549: */
550: void
1.17 jfb 551: cvs_read_rcfile(void)
1.1 jfb 552: {
553: char rcpath[MAXPATHLEN], linebuf[128], *lp;
1.23 xsa 554: int linenum = 0;
1.17 jfb 555: size_t len;
1.1 jfb 556: struct cvs_cmd *cmdp;
557: struct passwd *pw;
558: FILE *fp;
559:
560: pw = getpwuid(getuid());
561: if (pw == NULL) {
562: cvs_log(LP_NOTICE, "failed to get user's password entry");
563: return;
564: }
565:
566: snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
567:
568: fp = fopen(rcpath, "r");
569: if (fp == NULL) {
570: if (errno != ENOENT)
571: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
572: strerror(errno));
573: return;
574: }
575:
576: while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
1.23 xsa 577: linenum++;
1.17 jfb 578: if ((len = strlen(linebuf)) == 0)
579: continue;
580: if (linebuf[len - 1] != '\n') {
1.23 xsa 581: cvs_log(LP_WARN, "line too long in `%s:%d'", rcpath,
582: linenum);
1.17 jfb 583: break;
584: }
585: linebuf[--len] = '\0';
586:
1.1 jfb 587: lp = strchr(linebuf, ' ');
588: if (lp == NULL)
1.17 jfb 589: continue; /* ignore lines with no arguments */
590: *lp = '\0';
1.1 jfb 591: if (strcmp(linebuf, "cvs") == 0) {
1.17 jfb 592: /*
593: * Global default options. In the case of cvs only,
594: * we keep the 'cvs' string as first argument because
595: * getopt() does not like starting at index 0 for
596: * argument processing.
597: */
598: *lp = ' ';
599: cvs_defargs = strdup(linebuf);
600: if (cvs_defargs == NULL)
601: cvs_log(LP_ERRNO,
602: "failed to copy global arguments");
1.15 deraadt 603: } else {
1.17 jfb 604: lp++;
1.1 jfb 605: cmdp = cvs_findcmd(linebuf);
606: if (cmdp == NULL) {
607: cvs_log(LP_NOTICE,
1.25 xsa 608: "unknown command `%s' in `%s:%d'",
609: linebuf, rcpath, linenum);
1.1 jfb 610: continue;
611: }
1.17 jfb 612:
613: cmdp->cmd_defargs = strdup(lp);
614: if (cmdp->cmd_defargs == NULL)
615: cvs_log(LP_ERRNO,
616: "failed to copy default arguments for %s",
617: cmdp->cmd_name);
1.1 jfb 618: }
619: }
620: if (ferror(fp)) {
1.23 xsa 621: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 622: }
623:
624: (void)fclose(fp);
625: }