Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.61
1.61 ! xsa 1: /* $OpenBSD: rcsprog.c,v 1.60 2006/01/05 10:28:24 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.59 xsa 27: #include "includes.h"
1.1 deraadt 28:
1.9 joris 29: #include "rcsprog.h"
1.1 deraadt 30:
1.29 joris 31: #define RCS_CMD_MAXARG 128
1.42 xsa 32: #define RCS_DEFAULT_SUFFIX ",v/"
1.56 joris 33: #define RCSPROG_OPTSTRING "A:a:b::c:e::hik:Lm:Mn:N:qt::TUVx:z:"
34:
35: #define DESC_PROMPT "enter description, terminated with single '.' " \
36: "or end of file:\nNOTE: This is NOT the log message!" \
37: "\n>> "
1.29 joris 38:
1.1 deraadt 39: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12 joris 40: int verbose = 1;
1.34 joris 41: int pipeout = 0;
1.1 deraadt 42:
1.56 joris 43: #define RCS_NFLAG 1
44: #define RCS_TFLAG 2
45: static int rcsflags = 0;
46:
1.36 xsa 47: int rcs_optind;
1.28 joris 48: char *rcs_optarg;
1.42 xsa 49: char *rcs_suffixes;
1.36 xsa 50: char *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28 joris 51:
1.1 deraadt 52: struct rcs_prog {
1.26 deraadt 53: char *prog_name;
54: int (*prog_hdlr)(int, char **);
55: void (*prog_usage)(void);
1.1 deraadt 56: } programs[] = {
1.2 deraadt 57: { "rcs", rcs_main, rcs_usage },
1.17 joris 58: { "ci", checkin_main, checkin_usage },
1.11 joris 59: { "co", checkout_main, checkout_usage },
1.20 joris 60: { "rcsclean", rcsclean_main, rcsclean_usage },
1.18 joris 61: { "rcsdiff", rcsdiff_main, rcsdiff_usage },
1.33 xsa 62: { "rcsmerge", rcsmerge_main, rcsmerge_usage },
1.21 joris 63: { "rlog", rlog_main, rlog_usage },
1.22 joris 64: { "ident", ident_main, ident_usage },
1.1 deraadt 65: };
1.31 joris 66:
1.56 joris 67: static void rcs_set_description(RCSFILE *, const char *);
68: static void rcs_attach_symbol(RCSFILE *, const char *);
69:
1.31 joris 70: void
71: rcs_set_rev(const char *str, RCSNUM **rev)
72: {
1.32 joris 73: if (str == NULL)
74: return;
75:
1.52 joris 76: if ((*rev != NULL) && (*rev != RCS_HEAD_REV))
1.31 joris 77: cvs_log(LP_WARN, "redefinition of revision number");
78:
1.55 xsa 79: if ((*rev = rcsnum_parse(str)) == NULL)
80: fatal("bad revision number '%s'", str);
1.47 xsa 81: }
82:
83: /*
84: * rcs_get_mtime()
85: *
86: * Get <filename> last modified time.
87: * Returns last modified time on success, or -1 on failure.
88: */
89: time_t
90: rcs_get_mtime(const char *filename)
91: {
92: struct stat st;
93: time_t mtime;
94:
95: if (stat(filename, &st) == -1) {
96: cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
97: return (-1);
98: }
99: mtime = (time_t)st.st_mtimespec.tv_sec;
100:
101: return (mtime);
102: }
103:
104: /*
105: * rcs_set_mtime()
106: *
107: * Set <filename> last modified time to <mtime> if it's not set to -1.
108: * Returns 0 on success, or -1 on failure.
109: */
110: int
111: rcs_set_mtime(const char *filename, time_t mtime)
112: {
113: static struct timeval tv[2];
114:
115: if (mtime == -1)
116: return (0);
117:
118: tv[0].tv_sec = mtime;
119: tv[1].tv_sec = tv[0].tv_sec;
120:
121: if (utimes(filename, tv) == -1) {
122: cvs_log(LP_ERRNO, "error setting utimes");
123: return (-1);
124: }
125:
126: return (0);
1.31 joris 127: }
1.1 deraadt 128:
1.9 joris 129: int
1.29 joris 130: rcs_init(char *envstr, char **argv, int argvlen)
131: {
132: u_int i;
133: int argc, error;
134: char linebuf[256], *lp, *cp;
135:
136: strlcpy(linebuf, envstr, sizeof(linebuf));
137: memset(argv, 0, argvlen * sizeof(char *));
138:
139: error = argc = 0;
140: for (lp = linebuf; lp != NULL;) {
1.61 ! xsa 141: cp = strsep(&lp, " \t\b\f\n\r\t\v");
1.29 joris 142: if (cp == NULL)
143: break;
144: else if (*cp == '\0')
145: continue;
146:
147: if (argc == argvlen) {
148: error++;
149: break;
150: }
151:
1.53 joris 152: argv[argc] = xstrdup(cp);
1.29 joris 153: argc++;
154: }
155:
156: if (error != 0) {
157: for (i = 0; i < (u_int)argc; i++)
1.53 joris 158: xfree(argv[i]);
1.29 joris 159: argc = -1;
160: }
161:
162: return (argc);
163: }
164:
165: int
1.28 joris 166: rcs_getopt(int argc, char **argv, const char *optstr)
167: {
168: char *a;
169: const char *c;
170: static int i = 1;
171: int opt, hasargument, ret;
172:
173: hasargument = 0;
174: rcs_optarg = NULL;
175:
176: if (i >= argc)
177: return (-1);
178:
179: a = argv[i++];
180: if (*a++ != '-')
181: return (-1);
182:
183: ret = 0;
184: opt = *a;
185: for (c = optstr; *c != '\0'; c++) {
186: if (*c == opt) {
187: a++;
188: ret = opt;
189:
190: if (*(c + 1) == ':') {
191: if (*(c + 2) == ':') {
192: if (*a != '\0')
193: hasargument = 1;
194: } else {
195: if (*a != '\0') {
196: hasargument = 1;
197: } else {
198: ret = 1;
199: break;
200: }
201: }
202: }
203:
204: if (hasargument == 1)
205: rcs_optarg = a;
206:
207: if (ret == opt)
208: rcs_optind++;
209: break;
210: }
211: }
212:
213: if (ret == 0)
214: cvs_log(LP_ERR, "unknown option -%c", opt);
215: else if (ret == 1)
216: cvs_log(LP_ERR, "missing argument for option -%c", opt);
217:
218: return (ret);
219: }
220:
221: int
1.9 joris 222: rcs_statfile(char *fname, char *out, size_t len)
223: {
1.56 joris 224: size_t len1;
1.42 xsa 225: int l, found, strdir;
226: char defaultsuffix[] = RCS_DEFAULT_SUFFIX;
1.9 joris 227: char filev[MAXPATHLEN], fpath[MAXPATHLEN];
1.42 xsa 228: char *ext, *slash;
1.9 joris 229: struct stat st;
230:
1.42 xsa 231: strdir = found = 0;
232:
233: /* we might have gotten a RCS file as argument */
234: if ((ext = strchr(fname, ',')) != NULL)
235: *ext = '\0';
236:
237: /* we might have gotten the RCS/ dir in the argument string */
238: if (strstr(fname, RCSDIR) != NULL)
239: strdir = 1;
240:
241: if (rcs_suffixes != NULL)
242: ext = rcs_suffixes;
243: else
244: ext = defaultsuffix;
1.43 xsa 245:
1.42 xsa 246: for (;;) {
247: /*
248: * GNU documentation says -x,v/ specifies two suffixes,
249: * namely the ,v one and an empty one (which matches
250: * everything).
251: * The problem is that they don't follow this rule at
252: * all, and their documentation seems flawed.
253: * We try to be compatible, so let's do so.
254: */
255: if (*ext == '\0')
256: break;
257:
258: if ((slash = strchr(ext, '/')) != NULL)
259: *slash = '\0';
1.9 joris 260:
1.42 xsa 261: l = snprintf(filev, sizeof(filev), "%s%s", fname, ext);
1.55 xsa 262: if (l == -1 || l >= (int)sizeof(filev))
263: fatal("rcs_statfile: path truncation");
1.42 xsa 264:
265: if ((strdir == 0) &&
266: (stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
267: l = snprintf(fpath, sizeof(fpath), "%s/%s",
268: RCSDIR, filev);
1.55 xsa 269: if (l == -1 || l >= (int)sizeof(fpath))
270: fatal("rcs_statfile: path truncation");
1.42 xsa 271: } else {
1.56 joris 272: len1 = strlcpy(fpath, filev, sizeof(fpath));
273: if (len1 >= sizeof(fpath))
1.57 xsa 274: fatal("rcs_statfile: path truncation");
1.42 xsa 275: }
276:
1.56 joris 277: if ((stat(fpath, &st) != -1) || (rcsflags & RCS_CREATE)) {
1.42 xsa 278: found++;
279: break;
280: }
281:
282: if (slash == NULL)
283: break;
284:
285: *slash++ = '/';
286: ext = slash;
1.9 joris 287: }
288:
1.42 xsa 289: if (found != 1) {
1.44 niallo 290: if ((strcmp(__progname, "rcsclean") != 0)
291: && (strcmp(__progname, "ci") != 0))
1.20 joris 292: cvs_log(LP_ERRNO, "%s", fpath);
1.9 joris 293: return (-1);
294: }
295:
1.56 joris 296: len1 = strlcpy(out, fpath, len);
297: if (len1 >= len)
1.57 xsa 298: fatal("rcs_statfile: path truncation");
1.9 joris 299:
300: return (0);
301: }
1.1 deraadt 302:
303: int
304: main(int argc, char **argv)
305: {
306: u_int i;
1.29 joris 307: char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
308: int ret, cmd_argc;
1.1 deraadt 309:
310: ret = -1;
1.28 joris 311: rcs_optind = 1;
1.9 joris 312: cvs_log_init(LD_STD, 0);
1.1 deraadt 313:
1.29 joris 314: cmd_argc = 0;
1.30 joris 315: cmd_argv[cmd_argc++] = argv[0];
1.29 joris 316: if ((rcsinit = getenv("RCSINIT")) != NULL) {
317: ret = rcs_init(rcsinit, cmd_argv + 1,
318: RCS_CMD_MAXARG - 1);
319: if (ret < 0) {
320: cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
321: exit (1);
322: }
323:
324: cmd_argc += ret;
325: }
1.36 xsa 326:
327: if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
328: rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29 joris 329:
330: for (ret = 1; ret < argc; ret++)
331: cmd_argv[cmd_argc++] = argv[ret];
332:
1.1 deraadt 333: for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2 deraadt 334: if (strcmp(__progname, programs[i].prog_name) == 0) {
335: usage = programs[i].prog_usage;
1.29 joris 336: ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2 deraadt 337: break;
338: }
1.1 deraadt 339:
1.23 niallo 340: exit(ret);
1.1 deraadt 341: }
342:
343:
344: void
345: rcs_usage(void)
346: {
347: fprintf(stderr,
1.50 xsa 348: "usage: rcs [-hiLMTUV] [-Aoldfile] [-ausers] [-b[rev]] [-cstring]\n"
1.54 xsa 349: " [-eusers] [-kmode] [-mrev:msg] [-xsuffixes] [-ztz] file ...\n");
1.1 deraadt 350: }
351:
352: /*
353: * rcs_main()
354: *
355: * Handler for the `rcs' program.
356: * Returns 0 on success, or >0 on error.
357: */
358: int
359: rcs_main(int argc, char **argv)
360: {
361: int i, ch, flags, kflag, lkmode;
1.39 xsa 362: char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56 joris 363: char *logstr, *logmsg, *nflag, *descfile;
1.39 xsa 364: char *alist, *comment, *elist, *unp, *sp;
1.1 deraadt 365: mode_t fmode;
1.39 xsa 366: RCSFILE *file, *oldfile;
1.24 joris 367: RCSNUM *logrev;
1.39 xsa 368: struct rcs_access *acp;
1.48 xsa 369: time_t rcs_mtime = -1;
1.1 deraadt 370:
371: kflag = lkmode = -1;
372: fmode = 0;
1.58 niallo 373: flags = RCS_RDWR|RCS_PARSE_FULLY;
1.56 joris 374: descfile = nflag = NULL;
1.39 xsa 375: logstr = alist = comment = elist = NULL;
1.1 deraadt 376:
1.51 xsa 377: while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1 deraadt 378: switch (ch) {
379: case 'A':
1.39 xsa 380: if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
381: exit(1);
1.56 joris 382: rcsflags |= CO_ACLAPPEND;
1.1 deraadt 383: break;
384: case 'a':
1.28 joris 385: alist = rcs_optarg;
1.1 deraadt 386: break;
387: case 'c':
1.28 joris 388: comment = rcs_optarg;
1.1 deraadt 389: break;
390: case 'e':
1.28 joris 391: elist = rcs_optarg;
1.1 deraadt 392: break;
393: case 'h':
1.2 deraadt 394: (usage)();
1.1 deraadt 395: exit(0);
396: case 'i':
397: flags |= RCS_CREATE;
1.56 joris 398: rcsflags |= RCS_CREATE;
1.1 deraadt 399: break;
400: case 'k':
1.28 joris 401: kflag = rcs_kflag_get(rcs_optarg);
1.1 deraadt 402: if (RCS_KWEXP_INVAL(kflag)) {
403: cvs_log(LP_ERR,
404: "invalid keyword substitution mode `%s'",
1.28 joris 405: rcs_optarg);
1.1 deraadt 406: exit(1);
407: }
408: break;
409: case 'L':
410: if (lkmode == RCS_LOCK_LOOSE)
411: cvs_log(LP_WARN, "-U overriden by -L");
412: lkmode = RCS_LOCK_STRICT;
413: break;
1.24 joris 414: case 'm':
1.53 joris 415: logstr = xstrdup(rcs_optarg);
1.24 joris 416: break;
1.1 deraadt 417: case 'M':
418: /* ignore for the moment */
419: break;
1.56 joris 420: case 'n':
421: nflag = xstrdup(rcs_optarg);
422: break;
423: case 'N':
424: nflag = xstrdup(rcs_optarg);
425: rcsflags |= RCS_NFLAG;
426: break;
1.12 joris 427: case 'q':
428: verbose = 0;
1.46 xsa 429: break;
1.56 joris 430: case 't':
431: descfile = rcs_optarg;
432: rcsflags |= RCS_TFLAG;
433: break;
1.46 xsa 434: case 'T':
1.56 joris 435: rcsflags |= PRESERVETIME;
1.12 joris 436: break;
1.1 deraadt 437: case 'U':
438: if (lkmode == RCS_LOCK_STRICT)
439: cvs_log(LP_WARN, "-L overriden by -U");
440: lkmode = RCS_LOCK_LOOSE;
441: break;
442: case 'V':
443: printf("%s\n", rcs_version);
444: exit(0);
1.45 xsa 445: case 'x':
446: rcs_suffixes = rcs_optarg;
1.51 xsa 447: break;
448: case 'z':
449: /*
450: * kept for compatibility
451: */
1.45 xsa 452: break;
1.1 deraadt 453: default:
1.2 deraadt 454: (usage)();
1.1 deraadt 455: exit(1);
456: }
457: }
458:
1.28 joris 459: argc -= rcs_optind;
460: argv += rcs_optind;
1.30 joris 461:
1.1 deraadt 462: if (argc == 0) {
463: cvs_log(LP_ERR, "no input file");
1.5 joris 464: (usage)();
1.1 deraadt 465: exit(1);
466: }
467:
468: for (i = 0; i < argc; i++) {
1.9 joris 469: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8 joris 470: continue;
1.6 joris 471:
1.35 niallo 472: if (verbose == 1)
473: printf("RCS file: %s\n", fpath);
1.48 xsa 474:
1.49 niallo 475: if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6 joris 476: continue;
1.1 deraadt 477:
1.56 joris 478: if (rcsflags & RCS_CREATE)
479: rcs_set_description(file, NULL);
480:
481: if (rcsflags & RCS_TFLAG)
482: rcs_set_description(file, descfile);
483:
484: if (rcsflags & PRESERVETIME)
1.48 xsa 485: rcs_mtime = rcs_get_mtime(file->rf_path);
486:
1.56 joris 487: if (nflag != NULL)
488: rcs_attach_symbol(file, nflag);
489:
1.24 joris 490: if (logstr != NULL) {
491: if ((logmsg = strchr(logstr, ':')) == NULL) {
492: cvs_log(LP_ERR, "missing log message");
493: rcs_close(file);
494: continue;
495: }
496:
497: *logmsg++ = '\0';
498: if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48 xsa 499: cvs_log(LP_ERR,
500: "'%s' bad revision number", logstr);
1.24 joris 501: rcs_close(file);
502: continue;
503: }
504:
505: if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
506: cvs_log(LP_ERR,
507: "failed to set logmsg for '%s' to '%s'",
508: logstr, logmsg);
509: rcs_close(file);
1.25 joris 510: rcsnum_free(logrev);
1.24 joris 511: continue;
512: }
513:
514: rcsnum_free(logrev);
1.39 xsa 515: }
516:
517: /* entries to add from <oldfile> */
1.56 joris 518: if (rcsflags & CO_ACLAPPEND) {
1.39 xsa 519: /* XXX */
520: if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
521: exit(1);
522:
523: TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
524: rcs_access_add(file, acp->ra_name);
525:
526: rcs_close(oldfile);
1.24 joris 527: }
528:
1.1 deraadt 529: /* entries to add to the access list */
530: if (alist != NULL) {
531: unp = alist;
532: do {
533: sp = strchr(unp, ',');
534: if (sp != NULL)
535: *(sp++) = '\0';
536:
537: rcs_access_add(file, unp);
538:
539: unp = sp;
540: } while (sp != NULL);
541: }
542:
543: if (comment != NULL)
544: rcs_comment_set(file, comment);
545:
546: if (kflag != -1)
547: rcs_kwexp_set(file, kflag);
548:
549: if (lkmode != -1)
550: rcs_lock_setmode(file, lkmode);
551:
552: rcs_close(file);
1.48 xsa 553:
1.56 joris 554: if (rcsflags & PRESERVETIME)
1.48 xsa 555: rcs_set_mtime(fpath, rcs_mtime);
1.9 joris 556:
1.14 xsa 557: if (verbose == 1)
1.12 joris 558: printf("done\n");
1.1 deraadt 559: }
1.24 joris 560:
561: if (logstr != NULL)
1.53 joris 562: xfree(logstr);
1.1 deraadt 563:
1.56 joris 564: if (nflag != NULL)
565: xfree(nflag);
566:
1.1 deraadt 567: return (0);
1.56 joris 568: }
569:
570: static void
571: rcs_attach_symbol(RCSFILE *file, const char *symname)
572: {
573: char *rnum;
574: RCSNUM *rev;
575: char rbuf[16];
576: int rm;
577:
578: rm = 0;
579: rev = NULL;
580: if ((rnum = strrchr(symname, ':')) != NULL) {
581: if (rnum[1] == '\0')
582: rev = file->rf_head;
583: *(rnum++) = '\0';
584: } else {
585: rm = 1;
586: }
587:
588: if (rev == NULL && rm != 1) {
589: if ((rev = rcsnum_parse(rnum)) == NULL)
590: fatal("bad revision %s", rnum);
591: }
592:
593: if (rcsflags & RCS_NFLAG)
594: rm = 1;
595:
596: if (rm == 1) {
597: if (rcs_sym_remove(file, symname) < 0) {
598: if ((rcs_errno == RCS_ERR_NOENT) &&
599: !(rcsflags & RCS_NFLAG))
600: cvs_log(LP_WARN,
601: "can't delete nonexisting symbol %s", symname);
602: } else {
603: if (rcsflags & RCS_NFLAG)
604: rm = 0;
605: }
606: }
607:
608: if (rm == 0) {
609: if ((rcs_sym_add(file, symname, rev) < 0) &&
610: (rcs_errno == RCS_ERR_DUPENT)) {
611: rcsnum_tostr(rcs_sym_getrev(file, symname),
612: rbuf, sizeof(rbuf));
613: fatal("symbolic name %s already bound to %s",
614: symname, rbuf);
615: }
616: }
617: }
618:
619: static void
620: rcs_set_description(RCSFILE *file, const char *in)
621: {
622: BUF *bp;
623: char *content, buf[128];
624:
625: content = NULL;
626: if (in != NULL) {
627: bp = cvs_buf_load(in, BUF_AUTOEXT);
628: } else {
629: bp = cvs_buf_alloc(64, BUF_AUTOEXT);
630:
631: printf(DESC_PROMPT);
632: for (;;) {
633: fgets(buf, sizeof(buf), stdin);
634: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
635: break;
636: cvs_buf_append(bp, buf, strlen(buf));
637: printf(">> ");
638: }
639: }
640:
641: cvs_buf_putc(bp, '\0');
642: content = cvs_buf_release(bp);
643:
644: rcs_desc_set(file, content);
1.1 deraadt 645: }