Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.47
1.47 ! xsa 1: /* $OpenBSD: rcsprog.c,v 1.46 2005/11/23 09:39:20 xsa Exp $ */
1.1 deraadt 2: /*
3: * Copyright (c) 2005 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:
1.6 joris 27: #include <sys/param.h>
1.1 deraadt 28: #include <sys/wait.h>
1.8 joris 29: #include <sys/stat.h>
1.1 deraadt 30:
1.3 joris 31: #include <ctype.h>
1.1 deraadt 32: #include <err.h>
1.3 joris 33: #include <errno.h>
1.1 deraadt 34: #include <pwd.h>
1.3 joris 35: #include <string.h>
1.1 deraadt 36: #include <stdio.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39:
40: #include "log.h"
41: #include "rcs.h"
1.9 joris 42: #include "rcsprog.h"
1.1 deraadt 43:
1.29 joris 44: #define RCS_CMD_MAXARG 128
1.42 xsa 45: #define RCS_DEFAULT_SUFFIX ",v/"
1.29 joris 46:
1.1 deraadt 47: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12 joris 48: int verbose = 1;
1.34 joris 49: int pipeout = 0;
1.1 deraadt 50:
1.36 xsa 51: int rcs_optind;
1.28 joris 52: char *rcs_optarg;
1.42 xsa 53: char *rcs_suffixes;
1.36 xsa 54: char *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28 joris 55:
1.1 deraadt 56: struct rcs_prog {
1.26 deraadt 57: char *prog_name;
58: int (*prog_hdlr)(int, char **);
59: void (*prog_usage)(void);
1.1 deraadt 60: } programs[] = {
1.2 deraadt 61: { "rcs", rcs_main, rcs_usage },
1.17 joris 62: { "ci", checkin_main, checkin_usage },
1.11 joris 63: { "co", checkout_main, checkout_usage },
1.20 joris 64: { "rcsclean", rcsclean_main, rcsclean_usage },
1.18 joris 65: { "rcsdiff", rcsdiff_main, rcsdiff_usage },
1.33 xsa 66: { "rcsmerge", rcsmerge_main, rcsmerge_usage },
1.21 joris 67: { "rlog", rlog_main, rlog_usage },
1.22 joris 68: { "ident", ident_main, ident_usage },
1.1 deraadt 69: };
1.31 joris 70:
71: void
72: rcs_set_rev(const char *str, RCSNUM **rev)
73: {
1.32 joris 74: if (str == NULL)
75: return;
76:
1.31 joris 77: if (*rev != RCS_HEAD_REV)
78: cvs_log(LP_WARN, "redefinition of revision number");
79:
80: if ((*rev = rcsnum_parse(str)) == NULL) {
81: cvs_log(LP_ERR, "bad revision number '%s'", str);
82: exit (1);
83: }
1.47 ! xsa 84: }
! 85:
! 86: /*
! 87: * rcs_get_mtime()
! 88: *
! 89: * Get <filename> last modified time.
! 90: * Returns last modified time on success, or -1 on failure.
! 91: */
! 92: time_t
! 93: rcs_get_mtime(const char *filename)
! 94: {
! 95: struct stat st;
! 96: time_t mtime;
! 97:
! 98: if (stat(filename, &st) == -1) {
! 99: cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
! 100: return (-1);
! 101: }
! 102: mtime = (time_t)st.st_mtimespec.tv_sec;
! 103:
! 104: return (mtime);
! 105: }
! 106:
! 107: /*
! 108: * rcs_set_mtime()
! 109: *
! 110: * Set <filename> last modified time to <mtime> if it's not set to -1.
! 111: * Returns 0 on success, or -1 on failure.
! 112: */
! 113: int
! 114: rcs_set_mtime(const char *filename, time_t mtime)
! 115: {
! 116: static struct timeval tv[2];
! 117:
! 118: if (mtime == -1)
! 119: return (0);
! 120:
! 121: tv[0].tv_sec = mtime;
! 122: tv[1].tv_sec = tv[0].tv_sec;
! 123:
! 124: if (utimes(filename, tv) == -1) {
! 125: cvs_log(LP_ERRNO, "error setting utimes");
! 126: return (-1);
! 127: }
! 128:
! 129: return (0);
1.31 joris 130: }
1.1 deraadt 131:
1.9 joris 132: int
1.29 joris 133: rcs_init(char *envstr, char **argv, int argvlen)
134: {
135: u_int i;
136: int argc, error;
137: char linebuf[256], *lp, *cp;
138:
139: strlcpy(linebuf, envstr, sizeof(linebuf));
140: memset(argv, 0, argvlen * sizeof(char *));
141:
142: error = argc = 0;
143: for (lp = linebuf; lp != NULL;) {
144: cp = strsep(&lp, " \t\b\f\n\r\t\v");;
145: if (cp == NULL)
146: break;
147: else if (*cp == '\0')
148: continue;
149:
150: if (argc == argvlen) {
151: error++;
152: break;
153: }
154:
155: argv[argc] = strdup(cp);
156: if (argv[argc] == NULL) {
157: cvs_log(LP_ERRNO, "failed to copy argument");
158: error++;
159: break;
160: }
161:
162: argc++;
163: }
164:
165: if (error != 0) {
166: for (i = 0; i < (u_int)argc; i++)
167: free(argv[i]);
168: argc = -1;
169: }
170:
171: return (argc);
172: }
173:
174: int
1.28 joris 175: rcs_getopt(int argc, char **argv, const char *optstr)
176: {
177: char *a;
178: const char *c;
179: static int i = 1;
180: int opt, hasargument, ret;
181:
182: hasargument = 0;
183: rcs_optarg = NULL;
184:
185: if (i >= argc)
186: return (-1);
187:
188: a = argv[i++];
189: if (*a++ != '-')
190: return (-1);
191:
192: ret = 0;
193: opt = *a;
194: for (c = optstr; *c != '\0'; c++) {
195: if (*c == opt) {
196: a++;
197: ret = opt;
198:
199: if (*(c + 1) == ':') {
200: if (*(c + 2) == ':') {
201: if (*a != '\0')
202: hasargument = 1;
203: } else {
204: if (*a != '\0') {
205: hasargument = 1;
206: } else {
207: ret = 1;
208: break;
209: }
210: }
211: }
212:
213: if (hasargument == 1)
214: rcs_optarg = a;
215:
216: if (ret == opt)
217: rcs_optind++;
218: break;
219: }
220: }
221:
222: if (ret == 0)
223: cvs_log(LP_ERR, "unknown option -%c", opt);
224: else if (ret == 1)
225: cvs_log(LP_ERR, "missing argument for option -%c", opt);
226:
227: return (ret);
228: }
229:
230: int
1.9 joris 231: rcs_statfile(char *fname, char *out, size_t len)
232: {
1.42 xsa 233: int l, found, strdir;
234: char defaultsuffix[] = RCS_DEFAULT_SUFFIX;
1.9 joris 235: char filev[MAXPATHLEN], fpath[MAXPATHLEN];
1.42 xsa 236: char *ext, *slash;
1.9 joris 237: struct stat st;
238:
1.42 xsa 239: strdir = found = 0;
240:
241: /* we might have gotten a RCS file as argument */
242: if ((ext = strchr(fname, ',')) != NULL)
243: *ext = '\0';
244:
245: /* we might have gotten the RCS/ dir in the argument string */
246: if (strstr(fname, RCSDIR) != NULL)
247: strdir = 1;
248:
249: if (rcs_suffixes != NULL)
250: ext = rcs_suffixes;
251: else
252: ext = defaultsuffix;
1.43 xsa 253:
1.42 xsa 254: for (;;) {
255: /*
256: * GNU documentation says -x,v/ specifies two suffixes,
257: * namely the ,v one and an empty one (which matches
258: * everything).
259: * The problem is that they don't follow this rule at
260: * all, and their documentation seems flawed.
261: * We try to be compatible, so let's do so.
262: */
263: if (*ext == '\0')
264: break;
265:
266: if ((slash = strchr(ext, '/')) != NULL)
267: *slash = '\0';
1.9 joris 268:
1.42 xsa 269: l = snprintf(filev, sizeof(filev), "%s%s", fname, ext);
1.43 xsa 270: if (l == -1 || l >= (int)sizeof(filev)) {
271: errno = ENAMETOOLONG;
272: cvs_log(LP_ERRNO, "%s", filev);
1.9 joris 273: return (-1);
1.43 xsa 274: }
1.42 xsa 275:
276: if ((strdir == 0) &&
277: (stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
278: l = snprintf(fpath, sizeof(fpath), "%s/%s",
279: RCSDIR, filev);
1.43 xsa 280: if (l == -1 || l >= (int)sizeof(fpath)) {
281: errno = ENAMETOOLONG;
282: cvs_log(LP_ERRNO, "%s", fpath);
1.42 xsa 283: return (-1);
1.43 xsa 284: }
1.42 xsa 285: } else {
286: strlcpy(fpath, filev, sizeof(fpath));
287: }
288:
289: if (stat(fpath, &st) != -1) {
290: found++;
291: break;
292: }
293:
294: if (slash == NULL)
295: break;
296:
297: *slash++ = '/';
298: ext = slash;
1.9 joris 299: }
300:
1.42 xsa 301: if (found != 1) {
1.44 niallo 302: if ((strcmp(__progname, "rcsclean") != 0)
303: && (strcmp(__progname, "ci") != 0))
1.20 joris 304: cvs_log(LP_ERRNO, "%s", fpath);
1.9 joris 305: return (-1);
306: }
307:
308: strlcpy(out, fpath, len);
309:
310: return (0);
311: }
1.1 deraadt 312:
313: int
314: main(int argc, char **argv)
315: {
316: u_int i;
1.29 joris 317: char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
318: int ret, cmd_argc;
1.1 deraadt 319:
320: ret = -1;
1.28 joris 321: rcs_optind = 1;
1.9 joris 322: cvs_log_init(LD_STD, 0);
1.1 deraadt 323:
1.29 joris 324: cmd_argc = 0;
1.30 joris 325: cmd_argv[cmd_argc++] = argv[0];
1.29 joris 326: if ((rcsinit = getenv("RCSINIT")) != NULL) {
327: ret = rcs_init(rcsinit, cmd_argv + 1,
328: RCS_CMD_MAXARG - 1);
329: if (ret < 0) {
330: cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
331: exit (1);
332: }
333:
334: cmd_argc += ret;
335: }
1.36 xsa 336:
337: if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
338: rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29 joris 339:
340: for (ret = 1; ret < argc; ret++)
341: cmd_argv[cmd_argc++] = argv[ret];
342:
1.1 deraadt 343: for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2 deraadt 344: if (strcmp(__progname, programs[i].prog_name) == 0) {
345: usage = programs[i].prog_usage;
1.29 joris 346: ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2 deraadt 347: break;
348: }
1.1 deraadt 349:
1.23 niallo 350: exit(ret);
1.1 deraadt 351: }
352:
353:
354: void
355: rcs_usage(void)
356: {
357: fprintf(stderr,
1.39 xsa 358: "usage: rcs [-hiLMUV] [-Aoldfile] [-ausers] [-b[rev]] [-cstring]\n"
1.38 xsa 359: " [-eusers] [-kmode] [-mrev:log] file ...\n");
1.1 deraadt 360: }
361:
362: /*
363: * rcs_main()
364: *
365: * Handler for the `rcs' program.
366: * Returns 0 on success, or >0 on error.
367: */
368: int
369: rcs_main(int argc, char **argv)
370: {
371: int i, ch, flags, kflag, lkmode;
1.39 xsa 372: char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.24 joris 373: char *logstr, *logmsg;
1.39 xsa 374: char *alist, *comment, *elist, *unp, *sp;
1.1 deraadt 375: mode_t fmode;
1.39 xsa 376: RCSFILE *file, *oldfile;
1.24 joris 377: RCSNUM *logrev;
1.39 xsa 378: struct rcs_access *acp;
1.1 deraadt 379:
380: kflag = lkmode = -1;
381: fmode = 0;
382: flags = RCS_RDWR;
1.39 xsa 383: logstr = alist = comment = elist = NULL;
1.1 deraadt 384:
1.46 xsa 385: while ((ch = rcs_getopt(argc, argv, "A:a:b::c:e::hik:Lm:MqTUV")) != -1) {
1.1 deraadt 386: switch (ch) {
387: case 'A':
1.39 xsa 388: if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
389: exit(1);
1.41 niallo 390: flags |= CO_ACLAPPEND;
1.1 deraadt 391: break;
392: case 'a':
1.28 joris 393: alist = rcs_optarg;
1.1 deraadt 394: break;
395: case 'c':
1.28 joris 396: comment = rcs_optarg;
1.1 deraadt 397: break;
398: case 'e':
1.28 joris 399: elist = rcs_optarg;
1.1 deraadt 400: break;
401: case 'h':
1.2 deraadt 402: (usage)();
1.1 deraadt 403: exit(0);
404: case 'i':
405: flags |= RCS_CREATE;
406: break;
407: case 'k':
1.28 joris 408: kflag = rcs_kflag_get(rcs_optarg);
1.1 deraadt 409: if (RCS_KWEXP_INVAL(kflag)) {
410: cvs_log(LP_ERR,
411: "invalid keyword substitution mode `%s'",
1.28 joris 412: rcs_optarg);
1.1 deraadt 413: exit(1);
414: }
415: break;
416: case 'L':
417: if (lkmode == RCS_LOCK_LOOSE)
418: cvs_log(LP_WARN, "-U overriden by -L");
419: lkmode = RCS_LOCK_STRICT;
420: break;
1.24 joris 421: case 'm':
1.28 joris 422: if ((logstr = strdup(rcs_optarg)) == NULL) {
1.24 joris 423: cvs_log(LP_ERRNO, "failed to copy logstring");
424: exit(1);
425: }
426: break;
1.1 deraadt 427: case 'M':
428: /* ignore for the moment */
429: break;
1.12 joris 430: case 'q':
431: verbose = 0;
1.46 xsa 432: break;
433: case 'T':
434: flags |= PRESERVETIME;
1.12 joris 435: break;
1.1 deraadt 436: case 'U':
437: if (lkmode == RCS_LOCK_STRICT)
438: cvs_log(LP_WARN, "-L overriden by -U");
439: lkmode = RCS_LOCK_LOOSE;
440: break;
441: case 'V':
442: printf("%s\n", rcs_version);
443: exit(0);
1.45 xsa 444: case 'x':
445: rcs_suffixes = rcs_optarg;
446: break;
1.1 deraadt 447: default:
1.2 deraadt 448: (usage)();
1.1 deraadt 449: exit(1);
450: }
451: }
452:
1.28 joris 453: argc -= rcs_optind;
454: argv += rcs_optind;
1.30 joris 455:
1.1 deraadt 456: if (argc == 0) {
457: cvs_log(LP_ERR, "no input file");
1.5 joris 458: (usage)();
1.1 deraadt 459: exit(1);
460: }
461:
462: for (i = 0; i < argc; i++) {
1.9 joris 463: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8 joris 464: continue;
1.6 joris 465:
1.35 niallo 466: if (verbose == 1)
467: printf("RCS file: %s\n", fpath);
1.6 joris 468: file = rcs_open(fpath, flags, fmode);
469: if (file == NULL)
470: continue;
1.1 deraadt 471:
1.24 joris 472: if (logstr != NULL) {
473: if ((logmsg = strchr(logstr, ':')) == NULL) {
474: cvs_log(LP_ERR, "missing log message");
475: rcs_close(file);
476: continue;
477: }
478:
479: *logmsg++ = '\0';
480: if ((logrev = rcsnum_parse(logstr)) == NULL) {
481: cvs_log(LP_ERR, "'%s' bad revision number", logstr);
482: rcs_close(file);
483: continue;
484: }
485:
486: if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
487: cvs_log(LP_ERR,
488: "failed to set logmsg for '%s' to '%s'",
489: logstr, logmsg);
490: rcs_close(file);
1.25 joris 491: rcsnum_free(logrev);
1.24 joris 492: continue;
493: }
494:
495: rcsnum_free(logrev);
1.39 xsa 496: }
497:
498: /* entries to add from <oldfile> */
1.41 niallo 499: if (flags & CO_ACLAPPEND) {
1.39 xsa 500: /* XXX */
501: if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
502: exit(1);
503:
504: TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
505: rcs_access_add(file, acp->ra_name);
506:
507: rcs_close(oldfile);
1.24 joris 508: }
509:
1.1 deraadt 510: /* entries to add to the access list */
511: if (alist != NULL) {
512: unp = alist;
513: do {
514: sp = strchr(unp, ',');
515: if (sp != NULL)
516: *(sp++) = '\0';
517:
518: rcs_access_add(file, unp);
519:
520: unp = sp;
521: } while (sp != NULL);
522: }
523:
524: if (comment != NULL)
525: rcs_comment_set(file, comment);
526:
527: if (kflag != -1)
528: rcs_kwexp_set(file, kflag);
529:
530: if (lkmode != -1)
531: rcs_lock_setmode(file, lkmode);
532:
533: rcs_close(file);
1.9 joris 534:
1.14 xsa 535: if (verbose == 1)
1.12 joris 536: printf("done\n");
1.1 deraadt 537: }
1.24 joris 538:
539: if (logstr != NULL)
540: free(logstr);
1.1 deraadt 541:
542: return (0);
543: }