Annotation of src/usr.bin/cvs/cvs.c, Revision 1.102
1.102 ! david 1: /* $OpenBSD: cvs.c,v 1.101 2006/05/30 06:36:09 joris Exp $ */
1.1 jfb 2: /*
1.98 joris 3: * Copyright (c) 2006 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.90 xsa 28: #include "includes.h"
1.1 jfb 29:
30: #include "cvs.h"
1.99 joris 31: #include "config.h"
1.1 jfb 32: #include "log.h"
1.8 jfb 33: #include "file.h"
1.1 jfb 34:
35: extern char *__progname;
36:
37: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
38: int verbosity = 2;
39:
40: /* compression level used with zlib, 0 meaning no compression taking place */
1.98 joris 41: int cvs_compress = 0;
42: int cvs_readrc = 1; /* read .cvsrc on startup */
43: int cvs_trace = 0;
44: int cvs_nolog = 0;
45: int cvs_readonly = 0;
46: int cvs_nocase = 0; /* set to 1 to disable filename case sensitivity */
47: int cvs_noexec = 0; /* set to 1 to disable disk operations (-n option) */
48: int cvs_error = -1; /* set to the correct error code on failure */
49: int cvs_cmdop;
1.99 joris 50: int cvs_umask = CVS_UMASK_DEFAULT;
1.98 joris 51:
1.99 joris 52: char *cvs_tagname = NULL;
1.98 joris 53: char *cvs_defargs; /* default global arguments from .cvsrc */
54: char *cvs_command; /* name of the command we are running */
55: char *cvs_rootstr;
56: char *cvs_rsh = CVS_RSH_DEFAULT;
57: char *cvs_editor = CVS_EDITOR_DEFAULT;
58: char *cvs_homedir = NULL;
59: char *cvs_msg = NULL;
60: char *cvs_tmpdir = CVS_TMPDIR_DEFAULT;
1.13 krapht 61:
1.98 joris 62: struct cvsroot *current_cvsroot = NULL;
1.10 jfb 63:
1.27 jfb 64: static TAILQ_HEAD(, cvs_var) cvs_variables;
65:
1.98 joris 66: int cvs_getopt(int, char **);
1.75 xsa 67: void usage(void);
68: static void cvs_read_rcfile(void);
1.1 jfb 69:
1.98 joris 70: struct cvs_wklhead temp_files;
71:
72: void sighandler(int);
73: volatile sig_atomic_t cvs_quit = 0;
74: volatile sig_atomic_t sig_received = 0;
75:
76: void
77: sighandler(int sig)
78: {
79: sig_received = sig;
80:
81: switch (sig) {
82: case SIGINT:
83: case SIGTERM:
84: cvs_quit = 1;
85: break;
86: default:
87: break;
88: }
89: }
90:
91: void
92: cvs_cleanup(void)
93: {
94: cvs_log(LP_TRACE, "cvs_cleanup: removing locks");
95: cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
96:
97: cvs_log(LP_TRACE, "cvs_cleanup: removing temp files");
98: cvs_worklist_run(&temp_files, cvs_worklist_unlink);
99: }
100:
1.1 jfb 101: void
102: usage(void)
103: {
104: fprintf(stderr,
1.83 xsa 105: "Usage: %s [-flnQqrtvw] [-d root] [-e editor] [-s var=val] "
1.81 xsa 106: "[-T tmpdir] [-z level] command [...]\n", __progname);
1.1 jfb 107: }
108:
109: int
110: main(int argc, char **argv)
111: {
1.17 jfb 112: char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
113: int i, ret, cmd_argc;
1.1 jfb 114: struct cvs_cmd *cmdp;
1.79 xsa 115: struct passwd *pw;
1.80 xsa 116: struct stat st;
1.100 joris 117: char fpath[MAXPATHLEN];
1.87 joris 118:
119: tzset();
1.1 jfb 120:
1.27 jfb 121: TAILQ_INIT(&cvs_variables);
1.98 joris 122: SLIST_INIT(&repo_locks);
123: SLIST_INIT(&temp_files);
1.1 jfb 124:
125: /* check environment so command-line options override it */
126: if ((envstr = getenv("CVS_RSH")) != NULL)
127: cvs_rsh = envstr;
128:
129: if (((envstr = getenv("CVSEDITOR")) != NULL) ||
130: ((envstr = getenv("VISUAL")) != NULL) ||
131: ((envstr = getenv("EDITOR")) != NULL))
132: cvs_editor = envstr;
1.76 xsa 133:
134: if ((envstr = getenv("CVSREAD")) != NULL)
135: cvs_readonly = 1;
1.1 jfb 136:
1.79 xsa 137: if ((cvs_homedir = getenv("HOME")) == NULL) {
1.89 xsa 138: if ((pw = getpwuid(getuid())) == NULL)
139: fatal("getpwuid failed");
1.79 xsa 140: cvs_homedir = pw->pw_dir;
1.85 reyk 141: }
1.79 xsa 142:
1.80 xsa 143: if ((envstr = getenv("TMPDIR")) != NULL)
144: cvs_tmpdir = envstr;
145:
1.17 jfb 146: ret = cvs_getopt(argc, argv);
147:
148: argc -= ret;
149: argv += ret;
150: if (argc == 0) {
151: usage();
1.98 joris 152: exit(1);
1.17 jfb 153: }
1.80 xsa 154:
1.17 jfb 155: cvs_command = argv[0];
156:
1.80 xsa 157: /*
158: * check the tmp dir, either specified through
159: * the environment variable TMPDIR, or via
160: * the global option -T <dir>
161: */
1.94 xsa 162: if (stat(cvs_tmpdir, &st) == -1)
163: fatal("stat failed on `%s': %s", cvs_tmpdir, strerror(errno));
164: else if (!S_ISDIR(st.st_mode))
165: fatal("`%s' is not valid temporary directory", cvs_tmpdir);
1.80 xsa 166:
1.74 xsa 167: if (cvs_readrc == 1) {
1.17 jfb 168: cvs_read_rcfile();
169:
170: if (cvs_defargs != NULL) {
1.94 xsa 171: if ((targv = cvs_makeargv(cvs_defargs, &i)) == NULL)
172: fatal("failed to load default arguments to %s",
1.17 jfb 173: __progname);
174:
175: cvs_getopt(i, targv);
176: cvs_freeargv(targv, i);
1.88 joris 177: xfree(targv);
1.17 jfb 178: }
179: }
180:
181: /* setup signal handlers */
1.98 joris 182: signal(SIGTERM, sighandler);
183: signal(SIGINT, sighandler);
184: signal(SIGHUP, sighandler);
185: signal(SIGABRT, sighandler);
186: signal(SIGALRM, sighandler);
187: signal(SIGPIPE, sighandler);
1.17 jfb 188:
189: cmdp = cvs_findcmd(cvs_command);
190: if (cmdp == NULL) {
191: fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
192: fprintf(stderr, "CVS commands are:\n");
1.67 jfb 193: for (i = 0; cvs_cdt[i] != NULL; i++)
1.17 jfb 194: fprintf(stderr, "\t%-16s%s\n",
1.67 jfb 195: cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
1.98 joris 196: exit(1);
1.17 jfb 197: }
198:
199: cvs_cmdop = cmdp->cmd_op;
200:
201: cmd_argc = 0;
202: memset(cmd_argv, 0, sizeof(cmd_argv));
203:
204: cmd_argv[cmd_argc++] = argv[0];
205: if (cmdp->cmd_defargs != NULL) {
206: /* transform into a new argument vector */
207: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
208: CVS_CMD_MAXARG - 1);
1.94 xsa 209: if (ret < 0)
210: fatal("main: cvs_getargv failed");
211:
1.17 jfb 212: cmd_argc += ret;
213: }
1.98 joris 214:
1.17 jfb 215: for (ret = 1; ret < argc; ret++)
216: cmd_argv[cmd_argc++] = argv[ret];
217:
1.98 joris 218: cvs_file_init();
219:
220: if ((current_cvsroot = cvsroot_get(".")) == NULL) {
1.71 xsa 221: cvs_log(LP_ERR,
1.98 joris 222: "No CVSROOT specified! Please use the '-d' option");
1.102 ! david 223: fatal("or set the CVSROOT environment variable.");
1.17 jfb 224: }
225:
1.98 joris 226: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL)
227: fatal("remote setups are not supported yet");
1.100 joris 228:
229: i = snprintf(fpath, sizeof(fpath), "%s/%s", current_cvsroot->cr_dir,
230: CVS_PATH_ROOT);
231: if (stat(fpath, &st) == -1) {
232: if (errno == ENOENT)
233: fatal("'%s' does not seem to be a valid repository",
234: current_cvsroot->cr_dir);
235: else
236: fatal("%s: %s", current_cvsroot->cr_dir,
237: strerror(errno));
238: } else {
239: if (!S_ISDIR(st.st_mode))
240: fatal("'%s' is not a directory",
241: current_cvsroot->cr_dir);
242: }
1.99 joris 243:
244: cvs_parse_configfile();
245:
246: umask(cvs_umask);
1.98 joris 247:
248: cmdp->cmd(cmd_argc, cmd_argv);
249: cvs_cleanup();
1.17 jfb 250:
1.98 joris 251: return (0);
1.17 jfb 252: }
253:
254: int
255: cvs_getopt(int argc, char **argv)
256: {
257: int ret;
258: char *ep;
259:
1.83 xsa 260: while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:T:tvwz:")) != -1) {
1.1 jfb 261: switch (ret) {
1.11 jfb 262: case 'b':
263: /*
264: * We do not care about the bin directory for RCS files
265: * as this program has no dependencies on RCS programs,
266: * so it is only here for backwards compatibility.
267: */
268: cvs_log(LP_NOTICE, "the -b argument is obsolete");
269: break;
1.1 jfb 270: case 'd':
271: cvs_rootstr = optarg;
272: break;
273: case 'e':
274: cvs_editor = optarg;
275: break;
276: case 'f':
1.17 jfb 277: cvs_readrc = 0;
1.1 jfb 278: break;
279: case 'l':
280: cvs_nolog = 1;
281: break;
282: case 'n':
1.65 xsa 283: cvs_noexec = 1;
1.1 jfb 284: break;
285: case 'Q':
286: verbosity = 0;
287: break;
288: case 'q':
289: /* don't override -Q */
290: if (verbosity > 1)
291: verbosity = 1;
292: break;
293: case 'r':
294: cvs_readonly = 1;
295: break;
1.27 jfb 296: case 's':
297: ep = strchr(optarg, '=');
298: if (ep == NULL) {
299: cvs_log(LP_ERR, "no = in variable assignment");
1.98 joris 300: exit(1);
1.27 jfb 301: }
302: *(ep++) = '\0';
303: if (cvs_var_set(optarg, ep) < 0)
1.98 joris 304: exit(1);
1.80 xsa 305: break;
306: case 'T':
307: cvs_tmpdir = optarg;
1.27 jfb 308: break;
1.1 jfb 309: case 't':
310: cvs_trace = 1;
311: break;
312: case 'v':
313: printf("%s\n", CVS_VERSION);
314: exit(0);
315: /* NOTREACHED */
1.83 xsa 316: break;
317: case 'w':
318: cvs_readonly = 0;
1.11 jfb 319: break;
320: case 'x':
321: /*
322: * Kerberos encryption support, kept for compatibility
323: */
1.1 jfb 324: break;
325: case 'z':
1.18 tedu 326: cvs_compress = (int)strtol(optarg, &ep, 10);
1.1 jfb 327: if (*ep != '\0')
1.91 xsa 328: fatal("error parsing compression level");
1.1 jfb 329: if (cvs_compress < 0 || cvs_compress > 9)
1.91 xsa 330: fatal("gzip compression level must be "
1.1 jfb 331: "between 0 and 9");
332: break;
333: default:
334: usage();
1.98 joris 335: exit(1);
1.1 jfb 336: }
337: }
338:
1.17 jfb 339: ret = optind;
1.1 jfb 340: optind = 1;
1.17 jfb 341: optreset = 1; /* for next call */
1.12 jfb 342:
1.1 jfb 343: return (ret);
344: }
345:
346: /*
1.17 jfb 347: * cvs_read_rcfile()
1.1 jfb 348: *
349: * Read the CVS `.cvsrc' file in the user's home directory. If the file
350: * exists, it should contain a list of arguments that should always be given
351: * implicitly to the specified commands.
352: */
1.42 joris 353: static void
1.17 jfb 354: cvs_read_rcfile(void)
1.1 jfb 355: {
1.79 xsa 356: char rcpath[MAXPATHLEN], linebuf[128], *lp, *p;
1.93 xsa 357: int linenum = 0;
1.17 jfb 358: size_t len;
1.1 jfb 359: struct cvs_cmd *cmdp;
360: FILE *fp;
361:
1.93 xsa 362: if (strlcpy(rcpath, cvs_homedir, sizeof(rcpath)) >= sizeof(rcpath) ||
363: strlcat(rcpath, "/", sizeof(rcpath)) >= sizeof(rcpath) ||
364: strlcat(rcpath, CVS_PATH_RC, sizeof(rcpath)) >= sizeof(rcpath)) {
1.54 xsa 365: errno = ENAMETOOLONG;
1.98 joris 366: cvs_log(LP_ERR, "%s", rcpath);
1.54 xsa 367: return;
368: }
1.1 jfb 369:
370: fp = fopen(rcpath, "r");
371: if (fp == NULL) {
372: if (errno != ENOENT)
373: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
374: strerror(errno));
375: return;
376: }
377:
1.84 xsa 378: while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
1.23 xsa 379: linenum++;
1.17 jfb 380: if ((len = strlen(linebuf)) == 0)
381: continue;
382: if (linebuf[len - 1] != '\n') {
1.98 joris 383: cvs_log(LP_ERR, "line too long in `%s:%d'", rcpath,
1.23 xsa 384: linenum);
1.17 jfb 385: break;
386: }
387: linebuf[--len] = '\0';
388:
1.70 joris 389: /* skip any whitespaces */
390: p = linebuf;
391: while (*p == ' ')
1.95 deraadt 392: p++;
1.70 joris 393:
394: /* allow comments */
395: if (*p == '#')
396: continue;
397:
398: lp = strchr(p, ' ');
1.1 jfb 399: if (lp == NULL)
1.17 jfb 400: continue; /* ignore lines with no arguments */
401: *lp = '\0';
1.70 joris 402: if (strcmp(p, "cvs") == 0) {
1.17 jfb 403: /*
404: * Global default options. In the case of cvs only,
405: * we keep the 'cvs' string as first argument because
406: * getopt() does not like starting at index 0 for
407: * argument processing.
408: */
409: *lp = ' ';
1.88 joris 410: cvs_defargs = xstrdup(p);
1.15 deraadt 411: } else {
1.17 jfb 412: lp++;
1.70 joris 413: cmdp = cvs_findcmd(p);
1.1 jfb 414: if (cmdp == NULL) {
415: cvs_log(LP_NOTICE,
1.25 xsa 416: "unknown command `%s' in `%s:%d'",
1.70 joris 417: p, rcpath, linenum);
1.1 jfb 418: continue;
419: }
1.17 jfb 420:
1.88 joris 421: cmdp->cmd_defargs = xstrdup(lp);
1.1 jfb 422: }
423: }
1.98 joris 424:
1.1 jfb 425: if (ferror(fp)) {
1.23 xsa 426: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 427: }
428:
429: (void)fclose(fp);
1.27 jfb 430: }
431:
432: /*
433: * cvs_var_set()
434: *
435: * Set the value of the variable <var> to <val>. If there is no such variable,
436: * a new entry is created, otherwise the old value is overwritten.
437: * Returns 0 on success, or -1 on failure.
438: */
439: int
440: cvs_var_set(const char *var, const char *val)
441: {
442: char *valcp;
443: const char *cp;
444: struct cvs_var *vp;
445:
1.97 deraadt 446: if (var == NULL || *var == '\0') {
1.27 jfb 447: cvs_log(LP_ERR, "no variable name");
448: return (-1);
449: }
450:
451: /* sanity check on the name */
452: for (cp = var; *cp != '\0'; cp++)
453: if (!isalnum(*cp) && (*cp != '_')) {
454: cvs_log(LP_ERR,
455: "variable name `%s' contains invalid characters",
456: var);
457: return (-1);
458: }
459:
460: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
461: if (strcmp(vp->cv_name, var) == 0)
462: break;
463:
1.88 joris 464: valcp = xstrdup(val);
1.27 jfb 465: if (vp == NULL) {
1.96 ray 466: vp = xcalloc(1, sizeof(*vp));
1.27 jfb 467:
1.88 joris 468: vp->cv_name = xstrdup(var);
1.27 jfb 469: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
470:
471: } else /* free the previous value */
1.88 joris 472: xfree(vp->cv_val);
1.27 jfb 473:
474: vp->cv_val = valcp;
475:
476: return (0);
477: }
478:
479: /*
480: * cvs_var_set()
481: *
482: * Remove any entry for the variable <var>.
483: * Returns 0 on success, or -1 on failure.
484: */
485: int
486: cvs_var_unset(const char *var)
487: {
488: struct cvs_var *vp;
489:
490: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
491: if (strcmp(vp->cv_name, var) == 0) {
492: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
1.88 joris 493: xfree(vp->cv_name);
494: xfree(vp->cv_val);
495: xfree(vp);
1.27 jfb 496: return (0);
497: }
498:
499: return (-1);
500: }
501:
502: /*
503: * cvs_var_get()
504: *
505: * Get the value associated with the variable <var>. Returns a pointer to the
506: * value string on success, or NULL if the variable does not exist.
507: */
508:
1.75 xsa 509: const char *
1.27 jfb 510: cvs_var_get(const char *var)
511: {
512: struct cvs_var *vp;
513:
514: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
515: if (strcmp(vp->cv_name, var) == 0)
516: return (vp->cv_val);
517:
518: return (NULL);
1.1 jfb 519: }