Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.125
1.125 ! xsa 1: /* $OpenBSD: rcsprog.c,v 1.124 2006/05/09 12:33:42 ray 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.106 ray 32: #define RCSPROG_OPTSTRING "A:a:b::c:e::ik:Ll::m:Mn:N:o:qt::TUu::Vx::z::"
1.56 joris 33:
1.1 deraadt 34: const char rcs_version[] = "OpenCVS RCS version 3.6";
35:
1.91 ray 36: int rcsflags;
1.36 xsa 37: int rcs_optind;
1.28 joris 38: char *rcs_optarg;
1.42 xsa 39: char *rcs_suffixes;
1.36 xsa 40: char *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28 joris 41:
1.1 deraadt 42: struct rcs_prog {
1.26 deraadt 43: char *prog_name;
44: int (*prog_hdlr)(int, char **);
45: void (*prog_usage)(void);
1.1 deraadt 46: } programs[] = {
1.2 deraadt 47: { "rcs", rcs_main, rcs_usage },
1.17 joris 48: { "ci", checkin_main, checkin_usage },
1.11 joris 49: { "co", checkout_main, checkout_usage },
1.20 joris 50: { "rcsclean", rcsclean_main, rcsclean_usage },
1.18 joris 51: { "rcsdiff", rcsdiff_main, rcsdiff_usage },
1.33 xsa 52: { "rcsmerge", rcsmerge_main, rcsmerge_usage },
1.21 joris 53: { "rlog", rlog_main, rlog_usage },
1.22 joris 54: { "ident", ident_main, ident_usage },
1.1 deraadt 55: };
1.31 joris 56:
1.117 joris 57: struct rcs_wklhead rcs_temp_files;
1.68 joris 58:
59: void sighdlr(int);
1.101 joris 60: static void rcs_attach_symbol(RCSFILE *, const char *);
1.56 joris 61:
1.78 ray 62: /* ARGSUSED */
1.31 joris 63: void
1.68 joris 64: sighdlr(int sig)
65: {
1.117 joris 66: rcs_worklist_clean(&rcs_temp_files, rcs_worklist_unlink);
1.68 joris 67: _exit(1);
68: }
69:
1.9 joris 70: int
1.29 joris 71: rcs_init(char *envstr, char **argv, int argvlen)
72: {
73: u_int i;
74: int argc, error;
75: char linebuf[256], *lp, *cp;
76:
1.122 ray 77: if (strlcpy(linebuf, envstr, sizeof(linebuf)) >= sizeof(linebuf))
78: errx(1, "rcs_init: string truncation");
79: (void)memset(argv, 0, argvlen * sizeof(char *));
1.29 joris 80:
81: error = argc = 0;
82: for (lp = linebuf; lp != NULL;) {
1.61 xsa 83: cp = strsep(&lp, " \t\b\f\n\r\t\v");
1.29 joris 84: if (cp == NULL)
85: break;
86: else if (*cp == '\0')
87: continue;
88:
89: if (argc == argvlen) {
90: error++;
91: break;
92: }
93:
1.53 joris 94: argv[argc] = xstrdup(cp);
1.29 joris 95: argc++;
96: }
97:
98: if (error != 0) {
99: for (i = 0; i < (u_int)argc; i++)
1.53 joris 100: xfree(argv[i]);
1.29 joris 101: argc = -1;
102: }
103:
104: return (argc);
105: }
106:
107: int
1.1 deraadt 108: main(int argc, char **argv)
109: {
110: u_int i;
1.29 joris 111: char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
112: int ret, cmd_argc;
1.1 deraadt 113:
114: ret = -1;
1.28 joris 115: rcs_optind = 1;
1.68 joris 116: SLIST_INIT(&rcs_temp_files);
1.1 deraadt 117:
1.29 joris 118: cmd_argc = 0;
1.30 joris 119: cmd_argv[cmd_argc++] = argv[0];
1.29 joris 120: if ((rcsinit = getenv("RCSINIT")) != NULL) {
121: ret = rcs_init(rcsinit, cmd_argv + 1,
122: RCS_CMD_MAXARG - 1);
123: if (ret < 0) {
1.110 xsa 124: warnx("failed to prepend RCSINIT options");
1.29 joris 125: exit (1);
126: }
127:
128: cmd_argc += ret;
129: }
1.36 xsa 130:
131: if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
132: rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29 joris 133:
134: for (ret = 1; ret < argc; ret++)
135: cmd_argv[cmd_argc++] = argv[ret];
1.68 joris 136:
137: signal(SIGHUP, sighdlr);
138: signal(SIGINT, sighdlr);
139: signal(SIGQUIT, sighdlr);
140: signal(SIGABRT, sighdlr);
141: signal(SIGALRM, sighdlr);
142: signal(SIGTERM, sighdlr);
1.29 joris 143:
1.1 deraadt 144: for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2 deraadt 145: if (strcmp(__progname, programs[i].prog_name) == 0) {
146: usage = programs[i].prog_usage;
1.29 joris 147: ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2 deraadt 148: break;
149: }
1.100 joris 150:
151: /* clean up temporary files */
1.117 joris 152: rcs_worklist_run(&rcs_temp_files, rcs_worklist_unlink);
1.1 deraadt 153:
1.23 niallo 154: exit(ret);
1.1 deraadt 155: }
156:
157:
158: void
159: rcs_usage(void)
160: {
161: fprintf(stderr,
1.120 jmc 162: "usage: rcs [-eIiLqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92 ray 163: " [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
1.121 ray 164: " [-orev] [-sstate[:rev]] [-tstr] [-u[rev]]\n"
1.67 xsa 165: " [-xsuffixes] file ...\n");
1.1 deraadt 166: }
167:
168: /*
169: * rcs_main()
170: *
171: * Handler for the `rcs' program.
172: * Returns 0 on success, or >0 on error.
173: */
174: int
175: rcs_main(int argc, char **argv)
176: {
1.118 joris 177: int fd, ofd;
1.112 xsa 178: int i, j, ch, flags, kflag, lkmode;
1.39 xsa 179: char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56 joris 180: char *logstr, *logmsg, *nflag, *descfile;
1.101 joris 181: char *alist, *comment, *elist, *lrev, *urev, *orange;
1.1 deraadt 182: mode_t fmode;
1.39 xsa 183: RCSFILE *file, *oldfile;
1.24 joris 184: RCSNUM *logrev;
1.39 xsa 185: struct rcs_access *acp;
1.48 xsa 186: time_t rcs_mtime = -1;
1.1 deraadt 187:
1.95 xsa 188: kflag = RCS_KWEXP_ERR;
189: lkmode = -1;
1.89 niallo 190: fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.58 niallo 191: flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92 ray 192: lrev = urev = descfile = nflag = NULL;
1.101 joris 193: logstr = alist = comment = elist = orange = NULL;
1.111 xsa 194:
195: /* match GNU */
196: if (1 < argc && argv[1][0] != '-')
197: warnx("warning: No options were given; "
198: "this usage is obsolescent.");
1.1 deraadt 199:
1.118 joris 200: ofd = -1;
1.51 xsa 201: while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1 deraadt 202: switch (ch) {
203: case 'A':
1.118 joris 204: ofd = rcs_statfile(rcs_optarg, ofpath,
205: sizeof(ofpath), flags);
206: if (ofd < 0)
1.39 xsa 207: exit(1);
1.56 joris 208: rcsflags |= CO_ACLAPPEND;
1.1 deraadt 209: break;
210: case 'a':
1.28 joris 211: alist = rcs_optarg;
1.1 deraadt 212: break;
213: case 'c':
1.28 joris 214: comment = rcs_optarg;
1.1 deraadt 215: break;
216: case 'e':
1.28 joris 217: elist = rcs_optarg;
1.87 ray 218: rcsflags |= RCSPROG_EFLAG;
1.1 deraadt 219: break;
220: case 'i':
221: flags |= RCS_CREATE;
222: break;
223: case 'k':
1.28 joris 224: kflag = rcs_kflag_get(rcs_optarg);
1.1 deraadt 225: if (RCS_KWEXP_INVAL(kflag)) {
1.114 xsa 226: warnx("invalid RCS keyword substitution mode");
227: (usage)();
1.1 deraadt 228: exit(1);
229: }
230: break;
231: case 'L':
232: if (lkmode == RCS_LOCK_LOOSE)
1.110 xsa 233: warnx("-U overriden by -L");
1.1 deraadt 234: lkmode = RCS_LOCK_STRICT;
235: break;
1.92 ray 236: case 'l':
237: /* XXX - Check with -u flag. */
238: lrev = rcs_optarg;
239: rcsflags |= RCSPROG_LFLAG;
240: break;
1.24 joris 241: case 'm':
1.53 joris 242: logstr = xstrdup(rcs_optarg);
1.24 joris 243: break;
1.1 deraadt 244: case 'M':
245: /* ignore for the moment */
246: break;
1.56 joris 247: case 'n':
248: nflag = xstrdup(rcs_optarg);
249: break;
250: case 'N':
251: nflag = xstrdup(rcs_optarg);
1.87 ray 252: rcsflags |= RCSPROG_NFLAG;
1.56 joris 253: break;
1.101 joris 254: case 'o':
255: orange = xstrdup(rcs_optarg);
256: break;
1.12 joris 257: case 'q':
1.108 xsa 258: rcsflags |= QUIET;
1.46 xsa 259: break;
1.56 joris 260: case 't':
261: descfile = rcs_optarg;
1.113 ray 262: rcsflags |= DESCRIPTION;
1.56 joris 263: break;
1.46 xsa 264: case 'T':
1.56 joris 265: rcsflags |= PRESERVETIME;
1.12 joris 266: break;
1.1 deraadt 267: case 'U':
268: if (lkmode == RCS_LOCK_STRICT)
1.110 xsa 269: warnx("-L overriden by -U");
1.1 deraadt 270: lkmode = RCS_LOCK_LOOSE;
271: break;
1.92 ray 272: case 'u':
273: /* XXX - Check with -l flag. */
274: urev = rcs_optarg;
275: rcsflags |= RCSPROG_UFLAG;
276: break;
1.1 deraadt 277: case 'V':
278: printf("%s\n", rcs_version);
279: exit(0);
1.45 xsa 280: case 'x':
1.85 ray 281: /* Use blank extension if none given. */
282: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51 xsa 283: break;
284: case 'z':
285: /*
286: * kept for compatibility
287: */
1.45 xsa 288: break;
1.1 deraadt 289: default:
1.2 deraadt 290: (usage)();
1.1 deraadt 291: exit(1);
292: }
293: }
294:
1.28 joris 295: argc -= rcs_optind;
296: argv += rcs_optind;
1.30 joris 297:
1.1 deraadt 298: if (argc == 0) {
1.110 xsa 299: warnx("no input file");
1.5 joris 300: (usage)();
1.1 deraadt 301: exit(1);
302: }
303:
304: for (i = 0; i < argc; i++) {
1.118 joris 305: fd = rcs_statfile(argv[i], fpath, sizeof(fpath), flags);
306: if (fd < 0 && !(flags & RCS_CREATE))
1.8 joris 307: continue;
1.6 joris 308:
1.108 xsa 309: if (!(rcsflags & QUIET))
1.125 ! xsa 310: (void)fprintf(stderr, "RCS file: %s\n", fpath);
1.48 xsa 311:
1.118 joris 312: if ((file = rcs_open(fpath, fd, flags, fmode)) == NULL)
1.6 joris 313: continue;
1.1 deraadt 314:
1.113 ray 315: if (rcsflags & DESCRIPTION)
1.87 ray 316: rcs_set_description(file, descfile);
317: else if (flags & RCS_CREATE)
1.56 joris 318: rcs_set_description(file, NULL);
319:
320: if (rcsflags & PRESERVETIME)
1.118 joris 321: rcs_mtime = rcs_get_mtime(file);
1.48 xsa 322:
1.56 joris 323: if (nflag != NULL)
324: rcs_attach_symbol(file, nflag);
325:
1.24 joris 326: if (logstr != NULL) {
327: if ((logmsg = strchr(logstr, ':')) == NULL) {
1.110 xsa 328: warnx("missing log message");
1.24 joris 329: rcs_close(file);
330: continue;
331: }
332:
333: *logmsg++ = '\0';
334: if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.116 xsa 335: warnx("`%s' bad revision number", logstr);
1.24 joris 336: rcs_close(file);
337: continue;
338: }
339:
340: if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
1.116 xsa 341: warnx("failed to set logmsg for `%s' to `%s'",
1.24 joris 342: logstr, logmsg);
343: rcs_close(file);
1.25 joris 344: rcsnum_free(logrev);
1.24 joris 345: continue;
346: }
347:
348: rcsnum_free(logrev);
1.39 xsa 349: }
350:
351: /* entries to add from <oldfile> */
1.56 joris 352: if (rcsflags & CO_ACLAPPEND) {
1.39 xsa 353: /* XXX */
1.118 joris 354: if ((oldfile = rcs_open(ofpath, ofd, RCS_READ)) == NULL)
1.39 xsa 355: exit(1);
356:
357: TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
358: rcs_access_add(file, acp->ra_name);
359:
360: rcs_close(oldfile);
1.118 joris 361: ofd = -1;
1.24 joris 362: }
363:
1.1 deraadt 364: /* entries to add to the access list */
365: if (alist != NULL) {
1.117 joris 366: struct rcs_argvector *aargv;
1.1 deraadt 367:
1.117 joris 368: aargv = rcs_strsplit(alist, ",");
1.86 pat 369: for (j = 0; aargv->argv[j] != NULL; j++)
370: rcs_access_add(file, aargv->argv[j]);
1.1 deraadt 371:
1.117 joris 372: rcs_argv_destroy(aargv);
1.1 deraadt 373: }
374:
375: if (comment != NULL)
376: rcs_comment_set(file, comment);
1.82 xsa 377:
378: if (elist != NULL) {
1.117 joris 379: struct rcs_argvector *eargv;
1.82 xsa 380:
1.117 joris 381: eargv = rcs_strsplit(elist, ",");
1.86 pat 382: for (j = 0; eargv->argv[j] != NULL; j++)
383: rcs_access_remove(file, eargv->argv[j]);
1.82 xsa 384:
1.117 joris 385: rcs_argv_destroy(eargv);
1.87 ray 386: } else if (rcsflags & RCSPROG_EFLAG) {
1.82 xsa 387: struct rcs_access *rap;
388:
389: /* XXX rcs_access_remove(file, NULL); ?? */
390: while (!TAILQ_EMPTY(&(file->rf_access))) {
391: rap = TAILQ_FIRST(&(file->rf_access));
392: TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
393: xfree(rap->ra_name);
394: xfree(rap);
395: }
396: /* not synced anymore */
397: file->rf_flags &= ~RCS_SYNCED;
398: }
1.1 deraadt 399:
1.95 xsa 400: rcs_kwexp_set(file, kflag);
1.1 deraadt 401:
402: if (lkmode != -1)
1.84 xsa 403: (void)rcs_lock_setmode(file, lkmode);
1.92 ray 404:
405: if (rcsflags & RCSPROG_LFLAG) {
406: RCSNUM *rev;
407: const char *username;
1.98 ray 408: char rev_str[16];
1.92 ray 409:
410: if ((username = getlogin()) == NULL)
1.115 xsa 411: err(1, "getlogin");
1.92 ray 412: if (lrev == NULL) {
413: rev = rcsnum_alloc();
414: rcsnum_cpy(file->rf_head, rev, 0);
415: } else if ((rev = rcsnum_parse(lrev)) == NULL) {
1.110 xsa 416: warnx("unable to unlock file");
1.92 ray 417: rcs_close(file);
418: continue;
419: }
1.98 ray 420: rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92 ray 421: /* Make sure revision exists. */
422: if (rcs_findrev(file, rev) == NULL)
1.115 xsa 423: errx(1, "%s: cannot lock nonexisting "
424: "revision %s", fpath, rev_str);
1.98 ray 425: if (rcs_lock_add(file, username, rev) != -1 &&
1.108 xsa 426: !(rcsflags & QUIET))
1.98 ray 427: printf("%s locked\n", rev_str);
1.92 ray 428: rcsnum_free(rev);
429: }
430:
431: if (rcsflags & RCSPROG_UFLAG) {
432: RCSNUM *rev;
433: const char *username;
1.98 ray 434: char rev_str[16];
1.92 ray 435:
436: if ((username = getlogin()) == NULL)
1.115 xsa 437: err(1, "getlogin");
1.92 ray 438: if (urev == NULL) {
439: rev = rcsnum_alloc();
440: rcsnum_cpy(file->rf_head, rev, 0);
441: } else if ((rev = rcsnum_parse(urev)) == NULL) {
1.110 xsa 442: warnx("unable to unlock file");
1.92 ray 443: rcs_close(file);
444: continue;
445: }
1.98 ray 446: rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92 ray 447: /* Make sure revision exists. */
448: if (rcs_findrev(file, rev) == NULL)
1.115 xsa 449: errx(1, "%s: cannot unlock nonexisting "
450: "revision %s", fpath, rev_str);
1.98 ray 451: if (rcs_lock_remove(file, username, rev) == -1 &&
1.108 xsa 452: !(rcsflags & QUIET))
1.110 xsa 453: warnx("%s: warning: No locks are set.", fpath);
1.92 ray 454: rcsnum_free(rev);
455: }
1.1 deraadt 456:
1.101 joris 457: if (orange != NULL) {
1.123 joris 458: struct rcs_delta *rdp, *nrdp;
1.101 joris 459: char b[16];
460:
461: rcs_rev_select(file, orange);
1.123 joris 462: for (rdp = TAILQ_FIRST(&(file->rf_delta));
463: rdp != NULL; rdp = nrdp) {
464: nrdp = TAILQ_NEXT(rdp, rd_list);
465:
1.101 joris 466: /*
467: * Delete selected revisions.
468: */
469: if (rdp->rd_flags & RCS_RD_SELECT) {
470: rcsnum_tostr(rdp->rd_num, b, sizeof(b));
1.125 ! xsa 471: if (!(rcsflags & QUIET)) {
! 472: (void)fprintf(stderr, "deleting"
! 473: " revision %s\n", b);
! 474: }
1.101 joris 475: (void)rcs_rev_remove(file, rdp->rd_num);
476: }
477: }
478: }
479:
1.118 joris 480: rcs_write(file);
1.48 xsa 481:
1.56 joris 482: if (rcsflags & PRESERVETIME)
1.118 joris 483: rcs_set_mtime(file, rcs_mtime);
484:
485: rcs_close(file);
1.9 joris 486:
1.108 xsa 487: if (!(rcsflags & QUIET))
1.125 ! xsa 488: (void)fprintf(stderr, "done\n");
1.1 deraadt 489: }
1.24 joris 490:
491: if (logstr != NULL)
1.53 joris 492: xfree(logstr);
1.1 deraadt 493:
1.56 joris 494: if (nflag != NULL)
495: xfree(nflag);
1.105 xsa 496:
497: if (orange != NULL)
498: xfree(orange);
1.118 joris 499:
500: if (ofd != -1)
501: (void)close(ofd);
1.56 joris 502:
1.1 deraadt 503: return (0);
1.56 joris 504: }
505:
506: static void
507: rcs_attach_symbol(RCSFILE *file, const char *symname)
508: {
509: char *rnum;
510: RCSNUM *rev;
511: char rbuf[16];
512: int rm;
513:
514: rm = 0;
515: rev = NULL;
516: if ((rnum = strrchr(symname, ':')) != NULL) {
517: if (rnum[1] == '\0')
518: rev = file->rf_head;
519: *(rnum++) = '\0';
520: } else {
521: rm = 1;
522: }
523:
524: if (rev == NULL && rm != 1) {
525: if ((rev = rcsnum_parse(rnum)) == NULL)
1.115 xsa 526: errx(1, "bad revision %s", rnum);
1.56 joris 527: }
528:
1.87 ray 529: if (rcsflags & RCSPROG_NFLAG)
1.56 joris 530: rm = 1;
531:
532: if (rm == 1) {
533: if (rcs_sym_remove(file, symname) < 0) {
1.102 deraadt 534: if (rcs_errno == RCS_ERR_NOENT &&
1.87 ray 535: !(rcsflags & RCSPROG_NFLAG))
1.115 xsa 536: warnx("cannot delete nonexisting symbol %s",
1.110 xsa 537: symname);
1.56 joris 538: } else {
1.87 ray 539: if (rcsflags & RCSPROG_NFLAG)
1.56 joris 540: rm = 0;
541: }
542: }
543:
544: if (rm == 0) {
1.102 deraadt 545: if (rcs_sym_add(file, symname, rev) < 0 &&
546: rcs_errno == RCS_ERR_DUPENT) {
1.56 joris 547: rcsnum_tostr(rcs_sym_getrev(file, symname),
548: rbuf, sizeof(rbuf));
1.115 xsa 549: errx(1, "symbolic name %s already bound to %s",
1.56 joris 550: symname, rbuf);
551: }
552: }
1.1 deraadt 553: }