Annotation of src/usr.bin/cvs/cvs.c, Revision 1.141
1.141 ! tobias 1: /* $OpenBSD: cvs.c,v 1.140 2008/01/10 10:05:40 tobias Exp $ */
1.1 jfb 2: /*
1.113 joris 3: * Copyright (c) 2006, 2007 Joris Vink <joris@openbsd.org>
1.1 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.18 tedu 5: * All rights reserved.
1.1 jfb 6: *
1.18 tedu 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.18 tedu 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.18 tedu 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.18 tedu 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.117 otto 28: #include <sys/stat.h>
29:
30: #include <ctype.h>
31: #include <errno.h>
32: #include <pwd.h>
33: #include <stdlib.h>
34: #include <string.h>
1.120 xsa 35: #include <time.h>
1.117 otto 36: #include <unistd.h>
1.1 jfb 37:
38: #include "cvs.h"
1.106 joris 39: #include "remote.h"
1.1 jfb 40:
41: extern char *__progname;
42:
43: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
1.105 reyk 44: int verbosity = 1;
1.1 jfb 45:
46: /* compression level used with zlib, 0 meaning no compression taking place */
1.98 joris 47: int cvs_compress = 0;
48: int cvs_readrc = 1; /* read .cvsrc on startup */
49: int cvs_trace = 0;
50: int cvs_nolog = 0;
51: int cvs_readonly = 0;
1.111 xsa 52: int cvs_readonlyfs = 0;
1.98 joris 53: int cvs_nocase = 0; /* set to 1 to disable filename case sensitivity */
54: int cvs_noexec = 0; /* set to 1 to disable disk operations (-n option) */
55: int cvs_error = -1; /* set to the correct error code on failure */
56: int cvs_cmdop;
1.99 joris 57: int cvs_umask = CVS_UMASK_DEFAULT;
1.106 joris 58: int cvs_server_active = 0;
1.98 joris 59:
1.99 joris 60: char *cvs_tagname = NULL;
1.98 joris 61: char *cvs_defargs; /* default global arguments from .cvsrc */
62: char *cvs_rootstr;
63: char *cvs_rsh = CVS_RSH_DEFAULT;
64: char *cvs_editor = CVS_EDITOR_DEFAULT;
65: char *cvs_homedir = NULL;
66: char *cvs_msg = NULL;
67: char *cvs_tmpdir = CVS_TMPDIR_DEFAULT;
1.13 krapht 68:
1.98 joris 69: struct cvsroot *current_cvsroot = NULL;
1.140 tobias 70: struct cvs_cmd *cmdp; /* struct of command we are running */
1.27 jfb 71:
1.98 joris 72: int cvs_getopt(int, char **);
1.121 xsa 73: __dead void usage(void);
1.75 xsa 74: static void cvs_read_rcfile(void);
1.1 jfb 75:
1.98 joris 76: struct cvs_wklhead temp_files;
77:
78: void sighandler(int);
79: volatile sig_atomic_t cvs_quit = 0;
80: volatile sig_atomic_t sig_received = 0;
81:
82: void
83: sighandler(int sig)
84: {
85: sig_received = sig;
86:
87: switch (sig) {
88: case SIGINT:
89: case SIGTERM:
1.107 joris 90: case SIGPIPE:
1.98 joris 91: cvs_quit = 1;
92: break;
93: default:
94: break;
95: }
96: }
97:
98: void
99: cvs_cleanup(void)
100: {
101: cvs_log(LP_TRACE, "cvs_cleanup: removing locks");
102: cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
103:
104: cvs_log(LP_TRACE, "cvs_cleanup: removing temp files");
105: cvs_worklist_run(&temp_files, cvs_worklist_unlink);
1.106 joris 106:
1.124 ray 107: if (cvs_server_path != NULL) {
1.106 joris 108: if (cvs_rmdir(cvs_server_path) == -1)
109: cvs_log(LP_ERR,
110: "warning: failed to remove server directory: %s",
111: cvs_server_path);
112: xfree(cvs_server_path);
1.124 ray 113: cvs_server_path = NULL;
1.106 joris 114: }
1.98 joris 115: }
116:
1.121 xsa 117: __dead void
1.1 jfb 118: usage(void)
119: {
1.121 xsa 120: (void)fprintf(stderr,
1.127 ray 121: "usage: %s [-flnQqRrtVvw] [-d root] [-e editor] [-s var=val]\n"
1.129 sobrado 122: " [-T tmpdir] [-z level] command ...\n", __progname);
1.121 xsa 123: exit(1);
1.1 jfb 124: }
125:
126: int
127: main(int argc, char **argv)
128: {
1.17 jfb 129: char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
130: int i, ret, cmd_argc;
1.79 xsa 131: struct passwd *pw;
1.80 xsa 132: struct stat st;
1.100 joris 133: char fpath[MAXPATHLEN];
1.87 joris 134:
135: tzset();
1.1 jfb 136:
1.27 jfb 137: TAILQ_INIT(&cvs_variables);
1.98 joris 138: SLIST_INIT(&repo_locks);
139: SLIST_INIT(&temp_files);
1.1 jfb 140:
141: /* check environment so command-line options override it */
142: if ((envstr = getenv("CVS_RSH")) != NULL)
143: cvs_rsh = envstr;
144:
145: if (((envstr = getenv("CVSEDITOR")) != NULL) ||
146: ((envstr = getenv("VISUAL")) != NULL) ||
147: ((envstr = getenv("EDITOR")) != NULL))
148: cvs_editor = envstr;
1.76 xsa 149:
150: if ((envstr = getenv("CVSREAD")) != NULL)
151: cvs_readonly = 1;
1.1 jfb 152:
1.111 xsa 153: if ((envstr = getenv("CVSREADONLYFS")) != NULL) {
154: cvs_readonlyfs = 1;
155: cvs_nolog = 1;
156: }
157:
1.79 xsa 158: if ((cvs_homedir = getenv("HOME")) == NULL) {
1.131 tobias 159: if ((pw = getpwuid(getuid())) != NULL)
160: cvs_homedir = pw->pw_dir;
161:
1.85 reyk 162: }
1.79 xsa 163:
1.80 xsa 164: if ((envstr = getenv("TMPDIR")) != NULL)
165: cvs_tmpdir = envstr;
166:
1.17 jfb 167: ret = cvs_getopt(argc, argv);
168:
169: argc -= ret;
170: argv += ret;
1.121 xsa 171: if (argc == 0)
1.17 jfb 172: usage();
1.80 xsa 173:
174: /*
175: * check the tmp dir, either specified through
176: * the environment variable TMPDIR, or via
177: * the global option -T <dir>
178: */
1.94 xsa 179: if (stat(cvs_tmpdir, &st) == -1)
180: fatal("stat failed on `%s': %s", cvs_tmpdir, strerror(errno));
181: else if (!S_ISDIR(st.st_mode))
182: fatal("`%s' is not valid temporary directory", cvs_tmpdir);
1.80 xsa 183:
1.140 tobias 184: cmdp = cvs_findcmd(argv[0]);
1.132 tobias 185: if (cmdp == NULL) {
1.140 tobias 186: fprintf(stderr, "Unknown command: `%s'\n\n", argv[0]);
1.132 tobias 187: fprintf(stderr, "CVS commands are:\n");
188: for (i = 0; cvs_cdt[i] != NULL; i++)
189: fprintf(stderr, "\t%-16s%s\n",
190: cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
191: exit(1);
192: }
193:
1.131 tobias 194: if (cvs_readrc == 1 && cvs_homedir != NULL) {
1.17 jfb 195: cvs_read_rcfile();
196:
197: if (cvs_defargs != NULL) {
1.94 xsa 198: if ((targv = cvs_makeargv(cvs_defargs, &i)) == NULL)
199: fatal("failed to load default arguments to %s",
1.17 jfb 200: __progname);
201:
202: cvs_getopt(i, targv);
203: cvs_freeargv(targv, i);
1.88 joris 204: xfree(targv);
1.17 jfb 205: }
206: }
207:
208: /* setup signal handlers */
1.98 joris 209: signal(SIGTERM, sighandler);
210: signal(SIGINT, sighandler);
211: signal(SIGHUP, sighandler);
212: signal(SIGABRT, sighandler);
213: signal(SIGALRM, sighandler);
214: signal(SIGPIPE, sighandler);
1.17 jfb 215:
216: cvs_cmdop = cmdp->cmd_op;
217:
218: cmd_argc = 0;
219: memset(cmd_argv, 0, sizeof(cmd_argv));
220:
221: cmd_argv[cmd_argc++] = argv[0];
222: if (cmdp->cmd_defargs != NULL) {
223: /* transform into a new argument vector */
224: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
225: CVS_CMD_MAXARG - 1);
1.94 xsa 226: if (ret < 0)
227: fatal("main: cvs_getargv failed");
228:
1.17 jfb 229: cmd_argc += ret;
230: }
1.98 joris 231:
1.128 ray 232: if (argc + cmd_argc >= CVS_CMD_MAXARG)
233: fatal("main: too many arguments for `%s'", cmd_argv[0]);
1.17 jfb 234: for (ret = 1; ret < argc; ret++)
235: cmd_argv[cmd_argc++] = argv[ret];
236:
1.98 joris 237: cvs_file_init();
238:
1.106 joris 239: if (cvs_cmdop == CVS_OP_SERVER) {
1.130 tobias 240: cmdp->cmd(cmd_argc, cmd_argv);
241: cvs_cleanup();
242: return (0);
1.106 joris 243: }
244:
1.98 joris 245: if ((current_cvsroot = cvsroot_get(".")) == NULL) {
1.71 xsa 246: cvs_log(LP_ERR,
1.98 joris 247: "No CVSROOT specified! Please use the '-d' option");
1.102 david 248: fatal("or set the CVSROOT environment variable.");
1.17 jfb 249: }
250:
1.106 joris 251: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
252: cmdp->cmd(cmd_argc, cmd_argv);
253: cvs_cleanup();
254: return (0);
255: }
1.100 joris 256:
1.116 xsa 257: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
258: current_cvsroot->cr_dir, CVS_PATH_ROOT);
1.110 xsa 259:
1.103 xsa 260: if (stat(fpath, &st) == -1 && cvs_cmdop != CVS_OP_INIT) {
1.100 joris 261: if (errno == ENOENT)
1.104 joris 262: fatal("repository '%s' does not exist",
1.100 joris 263: current_cvsroot->cr_dir);
264: else
265: fatal("%s: %s", current_cvsroot->cr_dir,
266: strerror(errno));
267: } else {
268: if (!S_ISDIR(st.st_mode))
269: fatal("'%s' is not a directory",
270: current_cvsroot->cr_dir);
271: }
1.99 joris 272:
1.103 xsa 273: if (cvs_cmdop != CVS_OP_INIT)
274: cvs_parse_configfile();
1.99 joris 275:
276: umask(cvs_umask);
1.98 joris 277:
278: cmdp->cmd(cmd_argc, cmd_argv);
279: cvs_cleanup();
1.17 jfb 280:
1.98 joris 281: return (0);
1.17 jfb 282: }
283:
284: int
285: cvs_getopt(int argc, char **argv)
286: {
287: int ret;
288: char *ep;
1.115 joris 289: const char *errstr;
1.17 jfb 290:
1.122 xsa 291: while ((ret = getopt(argc, argv, "b:d:e:flnQqRrs:T:tVvwxz:")) != -1) {
1.1 jfb 292: switch (ret) {
1.11 jfb 293: case 'b':
294: /*
295: * We do not care about the bin directory for RCS files
296: * as this program has no dependencies on RCS programs,
297: * so it is only here for backwards compatibility.
298: */
299: cvs_log(LP_NOTICE, "the -b argument is obsolete");
300: break;
1.1 jfb 301: case 'd':
302: cvs_rootstr = optarg;
303: break;
304: case 'e':
305: cvs_editor = optarg;
306: break;
307: case 'f':
1.17 jfb 308: cvs_readrc = 0;
1.1 jfb 309: break;
310: case 'l':
311: cvs_nolog = 1;
312: break;
313: case 'n':
1.65 xsa 314: cvs_noexec = 1;
1.112 xsa 315: cvs_nolog = 1;
1.1 jfb 316: break;
317: case 'Q':
318: verbosity = 0;
319: break;
320: case 'q':
1.105 reyk 321: /*
322: * Be quiet. This is the default in OpenCVS.
323: */
1.111 xsa 324: break;
325: case 'R':
326: cvs_readonlyfs = 1;
327: cvs_nolog = 1;
1.1 jfb 328: break;
329: case 'r':
330: cvs_readonly = 1;
331: break;
1.27 jfb 332: case 's':
333: ep = strchr(optarg, '=');
334: if (ep == NULL) {
335: cvs_log(LP_ERR, "no = in variable assignment");
1.98 joris 336: exit(1);
1.27 jfb 337: }
338: *(ep++) = '\0';
339: if (cvs_var_set(optarg, ep) < 0)
1.98 joris 340: exit(1);
1.80 xsa 341: break;
342: case 'T':
343: cvs_tmpdir = optarg;
1.27 jfb 344: break;
1.1 jfb 345: case 't':
346: cvs_trace = 1;
1.105 reyk 347: break;
348: case 'V':
349: /* don't override -Q */
350: if (verbosity)
351: verbosity = 2;
1.1 jfb 352: break;
353: case 'v':
354: printf("%s\n", CVS_VERSION);
355: exit(0);
356: /* NOTREACHED */
1.83 xsa 357: case 'w':
358: cvs_readonly = 0;
1.11 jfb 359: break;
360: case 'x':
361: /*
362: * Kerberos encryption support, kept for compatibility
363: */
1.1 jfb 364: break;
365: case 'z':
1.115 joris 366: cvs_compress = strtonum(optarg, 0, 9, &errstr);
367: if (errstr != NULL)
368: fatal("cvs_compress: %s", errstr);
1.1 jfb 369: break;
370: default:
371: usage();
1.121 xsa 372: /* NOTREACHED */
1.1 jfb 373: }
374: }
375:
1.17 jfb 376: ret = optind;
1.1 jfb 377: optind = 1;
1.17 jfb 378: optreset = 1; /* for next call */
1.12 jfb 379:
1.1 jfb 380: return (ret);
381: }
382:
383: /*
1.17 jfb 384: * cvs_read_rcfile()
1.1 jfb 385: *
386: * Read the CVS `.cvsrc' file in the user's home directory. If the file
387: * exists, it should contain a list of arguments that should always be given
388: * implicitly to the specified commands.
389: */
1.42 joris 390: static void
1.17 jfb 391: cvs_read_rcfile(void)
1.1 jfb 392: {
1.133 tobias 393: char rcpath[MAXPATHLEN], *buf, *lbuf, *lp, *p;
1.137 tobias 394: int cmd_parsed, cvs_parsed, i, linenum;
1.135 tobias 395: size_t len, pos;
1.140 tobias 396: struct cvs_cmd *tcmdp;
1.1 jfb 397: FILE *fp;
398:
1.116 xsa 399: linenum = 0;
400:
401: i = snprintf(rcpath, MAXPATHLEN, "%s/%s", cvs_homedir, CVS_PATH_RC);
402: if (i < 0 || i >= MAXPATHLEN) {
1.108 xsa 403: cvs_log(LP_ERRNO, "%s", rcpath);
1.54 xsa 404: return;
405: }
1.1 jfb 406:
407: fp = fopen(rcpath, "r");
408: if (fp == NULL) {
409: if (errno != ENOENT)
410: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
411: strerror(errno));
412: return;
413: }
1.137 tobias 414:
415: cmd_parsed = cvs_parsed = 0;
1.133 tobias 416: lbuf = NULL;
417: while ((buf = fgetln(fp, &len)) != NULL) {
418: if (buf[len - 1] == '\n') {
419: buf[len - 1] = '\0';
420: } else {
421: lbuf = xmalloc(len + 1);
422: memcpy(lbuf, buf, len);
423: lbuf[len] = '\0';
424: buf = lbuf;
425: }
426:
1.23 xsa 427: linenum++;
1.17 jfb 428:
1.70 joris 429: /* skip any whitespaces */
1.133 tobias 430: p = buf;
1.70 joris 431: while (*p == ' ')
1.95 deraadt 432: p++;
1.70 joris 433:
1.134 tobias 434: /*
435: * Allow comments.
436: * GNU cvs stops parsing a line if it encounters a \t
437: * in front of a command, stick at this behaviour for
438: * compatibility.
439: */
440: if (*p == '#' || *p == '\t')
1.70 joris 441: continue;
442:
1.135 tobias 443: pos = strcspn(p, " \t");
444: if (pos == strlen(p)) {
1.138 tobias 445: lp = NULL;
1.135 tobias 446: } else {
447: lp = p + pos;
448: *lp = '\0';
449: }
450:
1.137 tobias 451: if (strcmp(p, "cvs") == 0 && !cvs_parsed) {
1.17 jfb 452: /*
453: * Global default options. In the case of cvs only,
454: * we keep the 'cvs' string as first argument because
455: * getopt() does not like starting at index 0 for
456: * argument processing.
457: */
1.138 tobias 458: if (lp != NULL) {
459: *lp = ' ';
460: cvs_defargs = xstrdup(p);
461: }
1.137 tobias 462: cvs_parsed = 1;
1.15 deraadt 463: } else {
1.137 tobias 464: tcmdp = cvs_findcmd(p);
465: if (tcmdp == NULL && verbosity == 2)
466: cvs_log(LP_NOTICE,
467: "unknown command `%s' in `%s:%d'",
468: p, rcpath, linenum);
469:
470: if (tcmdp != cmdp || cmd_parsed)
1.1 jfb 471: continue;
1.17 jfb 472:
1.138 tobias 473: if (lp != NULL) {
474: lp++;
475: cmdp->cmd_defargs = xstrdup(lp);
476: }
1.137 tobias 477: cmd_parsed = 1;
1.1 jfb 478: }
479: }
1.133 tobias 480: if (lbuf != NULL)
481: xfree(lbuf);
1.98 joris 482:
1.1 jfb 483: if (ferror(fp)) {
1.23 xsa 484: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 485: }
486:
487: (void)fclose(fp);
1.27 jfb 488: }
489:
490: /*
491: * cvs_var_set()
492: *
493: * Set the value of the variable <var> to <val>. If there is no such variable,
494: * a new entry is created, otherwise the old value is overwritten.
495: * Returns 0 on success, or -1 on failure.
496: */
497: int
498: cvs_var_set(const char *var, const char *val)
499: {
500: const char *cp;
501: struct cvs_var *vp;
502:
1.97 deraadt 503: if (var == NULL || *var == '\0') {
1.27 jfb 504: cvs_log(LP_ERR, "no variable name");
505: return (-1);
506: }
507:
508: /* sanity check on the name */
509: for (cp = var; *cp != '\0'; cp++)
510: if (!isalnum(*cp) && (*cp != '_')) {
511: cvs_log(LP_ERR,
512: "variable name `%s' contains invalid characters",
513: var);
514: return (-1);
515: }
516:
517: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
518: if (strcmp(vp->cv_name, var) == 0)
519: break;
520:
521: if (vp == NULL) {
1.96 ray 522: vp = xcalloc(1, sizeof(*vp));
1.27 jfb 523:
1.88 joris 524: vp->cv_name = xstrdup(var);
1.27 jfb 525: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
526:
527: } else /* free the previous value */
1.88 joris 528: xfree(vp->cv_val);
1.27 jfb 529:
1.141 ! tobias 530: vp->cv_val = xstrdup(val);
1.27 jfb 531:
532: return (0);
533: }
534:
535: /*
1.118 otto 536: * cvs_var_unset()
1.27 jfb 537: *
538: * Remove any entry for the variable <var>.
539: * Returns 0 on success, or -1 on failure.
540: */
541: int
542: cvs_var_unset(const char *var)
543: {
544: struct cvs_var *vp;
545:
546: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
547: if (strcmp(vp->cv_name, var) == 0) {
548: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
1.88 joris 549: xfree(vp->cv_name);
550: xfree(vp->cv_val);
551: xfree(vp);
1.27 jfb 552: return (0);
553: }
554:
555: return (-1);
556: }
557:
558: /*
559: * cvs_var_get()
560: *
561: * Get the value associated with the variable <var>. Returns a pointer to the
562: * value string on success, or NULL if the variable does not exist.
563: */
564:
1.75 xsa 565: const char *
1.27 jfb 566: cvs_var_get(const char *var)
567: {
568: struct cvs_var *vp;
569:
570: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
571: if (strcmp(vp->cv_name, var) == 0)
572: return (vp->cv_val);
573:
574: return (NULL);
1.1 jfb 575: }