Annotation of src/usr.bin/cvs/cvs.c, Revision 1.71
1.71 ! xsa 1: /* $OpenBSD: cvs.c,v 1.70 2005/06/10 13:34:23 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:
1.68 xsa 30: #include <ctype.h>
1.1 jfb 31: #include <err.h>
1.68 xsa 32: #include <errno.h>
1.1 jfb 33: #include <pwd.h>
1.68 xsa 34: #include <signal.h>
1.1 jfb 35: #include <stdio.h>
36: #include <stdlib.h>
1.68 xsa 37: #include <string.h>
1.1 jfb 38: #include <unistd.h>
39:
40: #include "cvs.h"
41: #include "log.h"
1.8 jfb 42: #include "file.h"
1.44 jfb 43: #include "strtab.h"
1.1 jfb 44:
45:
46: extern char *__progname;
47:
48:
49: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
50: int verbosity = 2;
51:
52: /* compression level used with zlib, 0 meaning no compression taking place */
53: int cvs_compress = 0;
1.17 jfb 54: int cvs_readrc = 1; /* read .cvsrc on startup */
1.1 jfb 55: int cvs_trace = 0;
56: int cvs_nolog = 0;
57: int cvs_readonly = 0;
1.17 jfb 58: int cvs_nocase = 0; /* set to 1 to disable filename case sensitivity */
1.65 xsa 59: int cvs_noexec = 0; /* set to 1 to disable disk operations (-n option) */
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;
1.69 joris 67: char *cvs_repo_base = NULL;
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:
1.48 jfb 77: void usage (void);
78: static void cvs_read_rcfile (void);
79: int cvs_getopt (int, char **);
1.1 jfb 80:
81:
82: /*
83: * usage()
84: *
85: * Display usage information.
86: */
87: void
88: usage(void)
89: {
90: fprintf(stderr,
1.66 joris 91: "Usage: %s [-flnQqtv] [-d root] [-e editor] [-s var=val] [-z level] "
1.27 jfb 92: "command [...]\n", __progname);
1.1 jfb 93: }
94:
95:
96: int
97: main(int argc, char **argv)
98: {
1.17 jfb 99: char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
100: int i, ret, cmd_argc;
1.1 jfb 101: struct cvs_cmd *cmdp;
102:
1.27 jfb 103: TAILQ_INIT(&cvs_variables);
104:
1.1 jfb 105: if (cvs_log_init(LD_STD, 0) < 0)
106: err(1, "failed to initialize logging");
107:
108: /* by default, be very verbose */
109: (void)cvs_log_filter(LP_FILTER_UNSET, LP_INFO);
110:
111: #ifdef DEBUG
112: (void)cvs_log_filter(LP_FILTER_UNSET, LP_DEBUG);
113: #endif
114:
1.44 jfb 115: cvs_strtab_init();
116:
1.1 jfb 117: /* check environment so command-line options override it */
118: if ((envstr = getenv("CVS_RSH")) != NULL)
119: cvs_rsh = envstr;
120:
121: if (((envstr = getenv("CVSEDITOR")) != NULL) ||
122: ((envstr = getenv("VISUAL")) != NULL) ||
123: ((envstr = getenv("EDITOR")) != NULL))
124: cvs_editor = envstr;
125:
1.17 jfb 126: ret = cvs_getopt(argc, argv);
127:
128: argc -= ret;
129: argv += ret;
130: if (argc == 0) {
131: usage();
1.49 joris 132: exit(1);
1.17 jfb 133: }
134: cvs_command = argv[0];
135:
136: if (cvs_readrc) {
137: cvs_read_rcfile();
138:
139: if (cvs_defargs != NULL) {
140: targv = cvs_makeargv(cvs_defargs, &i);
141: if (targv == NULL) {
142: cvs_log(LP_ERR,
143: "failed to load default arguments to %s",
144: __progname);
1.47 xsa 145: exit(1);
1.17 jfb 146: }
147:
148: cvs_getopt(i, targv);
149: cvs_freeargv(targv, i);
150: free(targv);
151: }
152: }
153:
154: /* setup signal handlers */
155: signal(SIGPIPE, SIG_IGN);
156:
1.40 jfb 157: if (cvs_file_init() < 0) {
158: cvs_log(LP_ERR, "failed to initialize file support");
159: exit(1);
160: }
1.17 jfb 161:
162: ret = -1;
163:
164: cmdp = cvs_findcmd(cvs_command);
165: if (cmdp == NULL) {
166: fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
167: fprintf(stderr, "CVS commands are:\n");
1.67 jfb 168: for (i = 0; cvs_cdt[i] != NULL; i++)
1.17 jfb 169: fprintf(stderr, "\t%-16s%s\n",
1.67 jfb 170: cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
171: exit(CVS_EX_USAGE);
1.17 jfb 172: }
173:
174: cvs_cmdop = cmdp->cmd_op;
175:
176: cmd_argc = 0;
177: memset(cmd_argv, 0, sizeof(cmd_argv));
178:
179: cmd_argv[cmd_argc++] = argv[0];
180: if (cmdp->cmd_defargs != NULL) {
181: /* transform into a new argument vector */
182: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
183: CVS_CMD_MAXARG - 1);
184: if (ret < 0) {
185: cvs_log(LP_ERRNO, "failed to generate argument vector "
186: "from default arguments");
1.47 xsa 187: exit(1);
1.17 jfb 188: }
189: cmd_argc += ret;
190: }
191: for (ret = 1; ret < argc; ret++)
192: cmd_argv[cmd_argc++] = argv[ret];
193:
1.45 joris 194: ret = cvs_startcmd(cmdp, cmd_argc, cmd_argv);
1.51 joris 195: switch (ret) {
196: case CVS_EX_USAGE:
1.56 jfb 197: fprintf(stderr, "Usage: %s %s %s\n", __progname, cvs_command,
198: cmdp->cmd_synopsis);
1.51 joris 199: break;
200: case CVS_EX_DATA:
1.55 jfb 201: cvs_log(LP_ABORT, "internal data error");
1.51 joris 202: break;
203: case CVS_EX_PROTO:
1.55 jfb 204: cvs_log(LP_ABORT, "protocol error");
1.51 joris 205: break;
206: case CVS_EX_FILE:
1.55 jfb 207: cvs_log(LP_ABORT, "an operation on a file or directory failed");
1.60 jfb 208: break;
209: case CVS_EX_BADROOT:
1.71 ! xsa 210: /* match GNU CVS output, thus the LP_ERR and LP_ABORT codes. */
! 211: cvs_log(LP_ERR,
1.60 jfb 212: "No CVSROOT specified! Please use the `-d' option");
213: cvs_log(LP_ABORT,
214: "or set the CVSROOT enviroment variable.");
1.51 joris 215: break;
216: default:
217: break;
1.17 jfb 218: }
219:
220: if (cvs_files != NULL)
221: cvs_file_free(cvs_files);
1.33 jfb 222: if (cvs_msg != NULL)
223: free(cvs_msg);
1.44 jfb 224:
225: cvs_strtab_cleanup();
1.17 jfb 226:
227: return (ret);
228: }
229:
230:
231: int
232: cvs_getopt(int argc, char **argv)
233: {
234: int ret;
235: char *ep;
236:
1.27 jfb 237: while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:tvz:")) != -1) {
1.1 jfb 238: switch (ret) {
1.11 jfb 239: case 'b':
240: /*
241: * We do not care about the bin directory for RCS files
242: * as this program has no dependencies on RCS programs,
243: * so it is only here for backwards compatibility.
244: */
245: cvs_log(LP_NOTICE, "the -b argument is obsolete");
246: break;
1.1 jfb 247: case 'd':
248: cvs_rootstr = optarg;
249: break;
250: case 'e':
251: cvs_editor = optarg;
252: break;
253: case 'f':
1.17 jfb 254: cvs_readrc = 0;
1.1 jfb 255: break;
256: case 'l':
257: cvs_nolog = 1;
258: break;
259: case 'n':
1.65 xsa 260: cvs_noexec = 1;
1.1 jfb 261: break;
262: case 'Q':
263: verbosity = 0;
264: break;
265: case 'q':
266: /* don't override -Q */
267: if (verbosity > 1)
268: verbosity = 1;
269: break;
270: case 'r':
271: cvs_readonly = 1;
272: break;
1.27 jfb 273: case 's':
274: ep = strchr(optarg, '=');
275: if (ep == NULL) {
276: cvs_log(LP_ERR, "no = in variable assignment");
1.49 joris 277: exit(1);
1.27 jfb 278: }
279: *(ep++) = '\0';
280: if (cvs_var_set(optarg, ep) < 0)
1.49 joris 281: exit(1);
1.27 jfb 282: break;
1.1 jfb 283: case 't':
1.24 jfb 284: (void)cvs_log_filter(LP_FILTER_UNSET, LP_TRACE);
1.1 jfb 285: cvs_trace = 1;
286: break;
287: case 'v':
288: printf("%s\n", CVS_VERSION);
289: exit(0);
290: /* NOTREACHED */
1.11 jfb 291: break;
292: case 'x':
293: /*
294: * Kerberos encryption support, kept for compatibility
295: */
1.1 jfb 296: break;
297: case 'z':
1.18 tedu 298: cvs_compress = (int)strtol(optarg, &ep, 10);
1.1 jfb 299: if (*ep != '\0')
300: errx(1, "error parsing compression level");
301: if (cvs_compress < 0 || cvs_compress > 9)
302: errx(1, "gzip compression level must be "
303: "between 0 and 9");
304: break;
305: default:
306: usage();
1.49 joris 307: exit(1);
1.1 jfb 308: }
309: }
310:
1.17 jfb 311: ret = optind;
1.1 jfb 312: optind = 1;
1.17 jfb 313: optreset = 1; /* for next call */
1.12 jfb 314:
1.1 jfb 315: return (ret);
316: }
317:
318:
319: /*
1.17 jfb 320: * cvs_read_rcfile()
1.1 jfb 321: *
322: * Read the CVS `.cvsrc' file in the user's home directory. If the file
323: * exists, it should contain a list of arguments that should always be given
324: * implicitly to the specified commands.
325: */
1.42 joris 326: static void
1.17 jfb 327: cvs_read_rcfile(void)
1.1 jfb 328: {
1.70 joris 329: char rcpath[MAXPATHLEN], linebuf[128], *lp, *p;
1.54 xsa 330: int l, linenum = 0;
1.17 jfb 331: size_t len;
1.1 jfb 332: struct cvs_cmd *cmdp;
333: struct passwd *pw;
334: FILE *fp;
335:
336: pw = getpwuid(getuid());
337: if (pw == NULL) {
338: cvs_log(LP_NOTICE, "failed to get user's password entry");
339: return;
340: }
341:
1.54 xsa 342: l = snprintf(rcpath, sizeof(rcpath), "%s/%s", pw->pw_dir, CVS_PATH_RC);
343: if (l == -1 || l >= (int)sizeof(rcpath)) {
344: errno = ENAMETOOLONG;
345: cvs_log(LP_ERRNO, "%s", rcpath);
346: return;
347: }
1.1 jfb 348:
349: fp = fopen(rcpath, "r");
350: if (fp == NULL) {
351: if (errno != ENOENT)
352: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
353: strerror(errno));
354: return;
355: }
356:
357: while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
1.23 xsa 358: linenum++;
1.17 jfb 359: if ((len = strlen(linebuf)) == 0)
360: continue;
361: if (linebuf[len - 1] != '\n') {
1.23 xsa 362: cvs_log(LP_WARN, "line too long in `%s:%d'", rcpath,
363: linenum);
1.17 jfb 364: break;
365: }
366: linebuf[--len] = '\0';
367:
1.70 joris 368: /* skip any whitespaces */
369: p = linebuf;
370: while (*p == ' ')
371: *p++;
372:
373: /* allow comments */
374: if (*p == '#')
375: continue;
376:
377: lp = strchr(p, ' ');
1.1 jfb 378: if (lp == NULL)
1.17 jfb 379: continue; /* ignore lines with no arguments */
380: *lp = '\0';
1.70 joris 381: if (strcmp(p, "cvs") == 0) {
1.17 jfb 382: /*
383: * Global default options. In the case of cvs only,
384: * we keep the 'cvs' string as first argument because
385: * getopt() does not like starting at index 0 for
386: * argument processing.
387: */
388: *lp = ' ';
1.70 joris 389: cvs_defargs = strdup(p);
1.17 jfb 390: if (cvs_defargs == NULL)
391: cvs_log(LP_ERRNO,
392: "failed to copy global arguments");
1.15 deraadt 393: } else {
1.17 jfb 394: lp++;
1.70 joris 395: cmdp = cvs_findcmd(p);
1.1 jfb 396: if (cmdp == NULL) {
397: cvs_log(LP_NOTICE,
1.25 xsa 398: "unknown command `%s' in `%s:%d'",
1.70 joris 399: p, rcpath, linenum);
1.1 jfb 400: continue;
401: }
1.17 jfb 402:
403: cmdp->cmd_defargs = strdup(lp);
404: if (cmdp->cmd_defargs == NULL)
405: cvs_log(LP_ERRNO,
406: "failed to copy default arguments for %s",
407: cmdp->cmd_name);
1.1 jfb 408: }
409: }
410: if (ferror(fp)) {
1.23 xsa 411: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 412: }
413:
414: (void)fclose(fp);
1.27 jfb 415: }
416:
417:
418: /*
419: * cvs_var_set()
420: *
421: * Set the value of the variable <var> to <val>. If there is no such variable,
422: * a new entry is created, otherwise the old value is overwritten.
423: * Returns 0 on success, or -1 on failure.
424: */
425: int
426: cvs_var_set(const char *var, const char *val)
427: {
428: char *valcp;
429: const char *cp;
430: struct cvs_var *vp;
431:
432: if ((var == NULL) || (*var == '\0')) {
433: cvs_log(LP_ERR, "no variable name");
434: return (-1);
435: }
436:
437: /* sanity check on the name */
438: for (cp = var; *cp != '\0'; cp++)
439: if (!isalnum(*cp) && (*cp != '_')) {
440: cvs_log(LP_ERR,
441: "variable name `%s' contains invalid characters",
442: var);
443: return (-1);
444: }
445:
446: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
447: if (strcmp(vp->cv_name, var) == 0)
448: break;
449:
450: valcp = strdup(val);
451: if (valcp == NULL) {
452: cvs_log(LP_ERRNO, "failed to allocate variable");
453: return (-1);
454: }
455:
456: if (vp == NULL) {
457: vp = (struct cvs_var *)malloc(sizeof(*vp));
458: if (vp == NULL) {
459: cvs_log(LP_ERRNO, "failed to allocate variable");
460: free(valcp);
461: return (-1);
462: }
463: memset(vp, 0, sizeof(*vp));
464:
465: vp->cv_name = strdup(var);
466: if (vp->cv_name == NULL) {
467: cvs_log(LP_ERRNO, "failed to allocate variable");
468: free(valcp);
469: free(vp);
470: return (-1);
471: }
472:
473: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
474:
475: } else /* free the previous value */
476: free(vp->cv_val);
477:
478: vp->cv_val = valcp;
479:
480: return (0);
481: }
482:
483:
484: /*
485: * cvs_var_set()
486: *
487: * Remove any entry for the variable <var>.
488: * Returns 0 on success, or -1 on failure.
489: */
490: int
491: cvs_var_unset(const char *var)
492: {
493: struct cvs_var *vp;
494:
495: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
496: if (strcmp(vp->cv_name, var) == 0) {
497: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
498: free(vp->cv_name);
499: free(vp->cv_val);
500: free(vp);
501: return (0);
502: }
503:
504: return (-1);
505:
506: }
507:
508:
509: /*
510: * cvs_var_get()
511: *
512: * Get the value associated with the variable <var>. Returns a pointer to the
513: * value string on success, or NULL if the variable does not exist.
514: */
515:
516: const char*
517: cvs_var_get(const char *var)
518: {
519: struct cvs_var *vp;
520:
521: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
522: if (strcmp(vp->cv_name, var) == 0)
523: return (vp->cv_val);
524:
525: return (NULL);
1.1 jfb 526: }