Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.123
1.123 ! joris 1: /* $OpenBSD: rcsprog.c,v 1.122 2006/04/29 05:31:28 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.76 ray 155: /* NOTREACHED */
1.1 deraadt 156: }
157:
158:
159: void
160: rcs_usage(void)
161: {
162: fprintf(stderr,
1.120 jmc 163: "usage: rcs [-eIiLqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92 ray 164: " [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
1.121 ray 165: " [-orev] [-sstate[:rev]] [-tstr] [-u[rev]]\n"
1.67 xsa 166: " [-xsuffixes] file ...\n");
1.1 deraadt 167: }
168:
169: /*
170: * rcs_main()
171: *
172: * Handler for the `rcs' program.
173: * Returns 0 on success, or >0 on error.
174: */
175: int
176: rcs_main(int argc, char **argv)
177: {
1.118 joris 178: int fd, ofd;
1.112 xsa 179: int i, j, ch, flags, kflag, lkmode;
1.39 xsa 180: char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56 joris 181: char *logstr, *logmsg, *nflag, *descfile;
1.101 joris 182: char *alist, *comment, *elist, *lrev, *urev, *orange;
1.1 deraadt 183: mode_t fmode;
1.39 xsa 184: RCSFILE *file, *oldfile;
1.24 joris 185: RCSNUM *logrev;
1.39 xsa 186: struct rcs_access *acp;
1.48 xsa 187: time_t rcs_mtime = -1;
1.1 deraadt 188:
1.95 xsa 189: kflag = RCS_KWEXP_ERR;
190: lkmode = -1;
1.89 niallo 191: fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.58 niallo 192: flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92 ray 193: lrev = urev = descfile = nflag = NULL;
1.101 joris 194: logstr = alist = comment = elist = orange = NULL;
1.111 xsa 195:
196: /* match GNU */
197: if (1 < argc && argv[1][0] != '-')
198: warnx("warning: No options were given; "
199: "this usage is obsolescent.");
1.1 deraadt 200:
1.118 joris 201: ofd = -1;
1.51 xsa 202: while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1 deraadt 203: switch (ch) {
204: case 'A':
1.118 joris 205: ofd = rcs_statfile(rcs_optarg, ofpath,
206: sizeof(ofpath), flags);
207: if (ofd < 0)
1.39 xsa 208: exit(1);
1.56 joris 209: rcsflags |= CO_ACLAPPEND;
1.1 deraadt 210: break;
211: case 'a':
1.28 joris 212: alist = rcs_optarg;
1.1 deraadt 213: break;
214: case 'c':
1.28 joris 215: comment = rcs_optarg;
1.1 deraadt 216: break;
217: case 'e':
1.28 joris 218: elist = rcs_optarg;
1.87 ray 219: rcsflags |= RCSPROG_EFLAG;
1.1 deraadt 220: break;
221: case 'i':
222: flags |= RCS_CREATE;
223: break;
224: case 'k':
1.28 joris 225: kflag = rcs_kflag_get(rcs_optarg);
1.1 deraadt 226: if (RCS_KWEXP_INVAL(kflag)) {
1.114 xsa 227: warnx("invalid RCS keyword substitution mode");
228: (usage)();
1.1 deraadt 229: exit(1);
230: }
231: break;
232: case 'L':
233: if (lkmode == RCS_LOCK_LOOSE)
1.110 xsa 234: warnx("-U overriden by -L");
1.1 deraadt 235: lkmode = RCS_LOCK_STRICT;
236: break;
1.92 ray 237: case 'l':
238: /* XXX - Check with -u flag. */
239: lrev = rcs_optarg;
240: rcsflags |= RCSPROG_LFLAG;
241: break;
1.24 joris 242: case 'm':
1.53 joris 243: logstr = xstrdup(rcs_optarg);
1.24 joris 244: break;
1.1 deraadt 245: case 'M':
246: /* ignore for the moment */
247: break;
1.56 joris 248: case 'n':
249: nflag = xstrdup(rcs_optarg);
250: break;
251: case 'N':
252: nflag = xstrdup(rcs_optarg);
1.87 ray 253: rcsflags |= RCSPROG_NFLAG;
1.56 joris 254: break;
1.101 joris 255: case 'o':
256: orange = xstrdup(rcs_optarg);
257: break;
1.12 joris 258: case 'q':
1.108 xsa 259: rcsflags |= QUIET;
1.46 xsa 260: break;
1.56 joris 261: case 't':
262: descfile = rcs_optarg;
1.113 ray 263: rcsflags |= DESCRIPTION;
1.56 joris 264: break;
1.46 xsa 265: case 'T':
1.56 joris 266: rcsflags |= PRESERVETIME;
1.12 joris 267: break;
1.1 deraadt 268: case 'U':
269: if (lkmode == RCS_LOCK_STRICT)
1.110 xsa 270: warnx("-L overriden by -U");
1.1 deraadt 271: lkmode = RCS_LOCK_LOOSE;
272: break;
1.92 ray 273: case 'u':
274: /* XXX - Check with -l flag. */
275: urev = rcs_optarg;
276: rcsflags |= RCSPROG_UFLAG;
277: break;
1.1 deraadt 278: case 'V':
279: printf("%s\n", rcs_version);
280: exit(0);
1.76 ray 281: /* NOTREACHED */
1.45 xsa 282: case 'x':
1.85 ray 283: /* Use blank extension if none given. */
284: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51 xsa 285: break;
286: case 'z':
287: /*
288: * kept for compatibility
289: */
1.45 xsa 290: break;
1.1 deraadt 291: default:
1.2 deraadt 292: (usage)();
1.1 deraadt 293: exit(1);
294: }
295: }
296:
1.28 joris 297: argc -= rcs_optind;
298: argv += rcs_optind;
1.30 joris 299:
1.1 deraadt 300: if (argc == 0) {
1.110 xsa 301: warnx("no input file");
1.5 joris 302: (usage)();
1.1 deraadt 303: exit(1);
304: }
305:
306: for (i = 0; i < argc; i++) {
1.118 joris 307: fd = rcs_statfile(argv[i], fpath, sizeof(fpath), flags);
308: if (fd < 0 && !(flags & RCS_CREATE))
1.8 joris 309: continue;
1.6 joris 310:
1.108 xsa 311: if (!(rcsflags & QUIET))
1.35 niallo 312: printf("RCS file: %s\n", fpath);
1.48 xsa 313:
1.118 joris 314: if ((file = rcs_open(fpath, fd, flags, fmode)) == NULL)
1.6 joris 315: continue;
1.1 deraadt 316:
1.113 ray 317: if (rcsflags & DESCRIPTION)
1.87 ray 318: rcs_set_description(file, descfile);
319: else if (flags & RCS_CREATE)
1.56 joris 320: rcs_set_description(file, NULL);
321:
322: if (rcsflags & PRESERVETIME)
1.118 joris 323: rcs_mtime = rcs_get_mtime(file);
1.48 xsa 324:
1.56 joris 325: if (nflag != NULL)
326: rcs_attach_symbol(file, nflag);
327:
1.24 joris 328: if (logstr != NULL) {
329: if ((logmsg = strchr(logstr, ':')) == NULL) {
1.110 xsa 330: warnx("missing log message");
1.24 joris 331: rcs_close(file);
332: continue;
333: }
334:
335: *logmsg++ = '\0';
336: if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.116 xsa 337: warnx("`%s' bad revision number", logstr);
1.24 joris 338: rcs_close(file);
339: continue;
340: }
341:
342: if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
1.116 xsa 343: warnx("failed to set logmsg for `%s' to `%s'",
1.24 joris 344: logstr, logmsg);
345: rcs_close(file);
1.25 joris 346: rcsnum_free(logrev);
1.24 joris 347: continue;
348: }
349:
350: rcsnum_free(logrev);
1.39 xsa 351: }
352:
353: /* entries to add from <oldfile> */
1.56 joris 354: if (rcsflags & CO_ACLAPPEND) {
1.39 xsa 355: /* XXX */
1.118 joris 356: if ((oldfile = rcs_open(ofpath, ofd, RCS_READ)) == NULL)
1.39 xsa 357: exit(1);
358:
359: TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
360: rcs_access_add(file, acp->ra_name);
361:
362: rcs_close(oldfile);
1.118 joris 363: ofd = -1;
1.24 joris 364: }
365:
1.1 deraadt 366: /* entries to add to the access list */
367: if (alist != NULL) {
1.117 joris 368: struct rcs_argvector *aargv;
1.1 deraadt 369:
1.117 joris 370: aargv = rcs_strsplit(alist, ",");
1.86 pat 371: for (j = 0; aargv->argv[j] != NULL; j++)
372: rcs_access_add(file, aargv->argv[j]);
1.1 deraadt 373:
1.117 joris 374: rcs_argv_destroy(aargv);
1.1 deraadt 375: }
376:
377: if (comment != NULL)
378: rcs_comment_set(file, comment);
1.82 xsa 379:
380: if (elist != NULL) {
1.117 joris 381: struct rcs_argvector *eargv;
1.82 xsa 382:
1.117 joris 383: eargv = rcs_strsplit(elist, ",");
1.86 pat 384: for (j = 0; eargv->argv[j] != NULL; j++)
385: rcs_access_remove(file, eargv->argv[j]);
1.82 xsa 386:
1.117 joris 387: rcs_argv_destroy(eargv);
1.87 ray 388: } else if (rcsflags & RCSPROG_EFLAG) {
1.82 xsa 389: struct rcs_access *rap;
390:
391: /* XXX rcs_access_remove(file, NULL); ?? */
392: while (!TAILQ_EMPTY(&(file->rf_access))) {
393: rap = TAILQ_FIRST(&(file->rf_access));
394: TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
395: xfree(rap->ra_name);
396: xfree(rap);
397: }
398: /* not synced anymore */
399: file->rf_flags &= ~RCS_SYNCED;
400: }
1.1 deraadt 401:
1.95 xsa 402: rcs_kwexp_set(file, kflag);
1.1 deraadt 403:
404: if (lkmode != -1)
1.84 xsa 405: (void)rcs_lock_setmode(file, lkmode);
1.92 ray 406:
407: if (rcsflags & RCSPROG_LFLAG) {
408: RCSNUM *rev;
409: const char *username;
1.98 ray 410: char rev_str[16];
1.92 ray 411:
412: if ((username = getlogin()) == NULL)
1.115 xsa 413: err(1, "getlogin");
1.92 ray 414: if (lrev == NULL) {
415: rev = rcsnum_alloc();
416: rcsnum_cpy(file->rf_head, rev, 0);
417: } else if ((rev = rcsnum_parse(lrev)) == NULL) {
1.110 xsa 418: warnx("unable to unlock file");
1.92 ray 419: rcs_close(file);
420: continue;
421: }
1.98 ray 422: rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92 ray 423: /* Make sure revision exists. */
424: if (rcs_findrev(file, rev) == NULL)
1.115 xsa 425: errx(1, "%s: cannot lock nonexisting "
426: "revision %s", fpath, rev_str);
1.98 ray 427: if (rcs_lock_add(file, username, rev) != -1 &&
1.108 xsa 428: !(rcsflags & QUIET))
1.98 ray 429: printf("%s locked\n", rev_str);
1.92 ray 430: rcsnum_free(rev);
431: }
432:
433: if (rcsflags & RCSPROG_UFLAG) {
434: RCSNUM *rev;
435: const char *username;
1.98 ray 436: char rev_str[16];
1.92 ray 437:
438: if ((username = getlogin()) == NULL)
1.115 xsa 439: err(1, "getlogin");
1.92 ray 440: if (urev == NULL) {
441: rev = rcsnum_alloc();
442: rcsnum_cpy(file->rf_head, rev, 0);
443: } else if ((rev = rcsnum_parse(urev)) == NULL) {
1.110 xsa 444: warnx("unable to unlock file");
1.92 ray 445: rcs_close(file);
446: continue;
447: }
1.98 ray 448: rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92 ray 449: /* Make sure revision exists. */
450: if (rcs_findrev(file, rev) == NULL)
1.115 xsa 451: errx(1, "%s: cannot unlock nonexisting "
452: "revision %s", fpath, rev_str);
1.98 ray 453: if (rcs_lock_remove(file, username, rev) == -1 &&
1.108 xsa 454: !(rcsflags & QUIET))
1.110 xsa 455: warnx("%s: warning: No locks are set.", fpath);
1.92 ray 456: rcsnum_free(rev);
457: }
1.1 deraadt 458:
1.101 joris 459: if (orange != NULL) {
1.123 ! joris 460: struct rcs_delta *rdp, *nrdp;
1.101 joris 461: char b[16];
462:
463: rcs_rev_select(file, orange);
1.123 ! joris 464: for (rdp = TAILQ_FIRST(&(file->rf_delta));
! 465: rdp != NULL; rdp = nrdp) {
! 466: nrdp = TAILQ_NEXT(rdp, rd_list);
! 467:
1.101 joris 468: /*
469: * Delete selected revisions.
470: */
471: if (rdp->rd_flags & RCS_RD_SELECT) {
472: rcsnum_tostr(rdp->rd_num, b, sizeof(b));
473: printf("deleting revision %s\n", b);
474: (void)rcs_rev_remove(file, rdp->rd_num);
475: }
476: }
477: }
478:
1.118 joris 479: rcs_write(file);
1.48 xsa 480:
1.56 joris 481: if (rcsflags & PRESERVETIME)
1.118 joris 482: rcs_set_mtime(file, rcs_mtime);
483:
484: rcs_close(file);
1.9 joris 485:
1.108 xsa 486: if (!(rcsflags & QUIET))
1.12 joris 487: printf("done\n");
1.1 deraadt 488: }
1.24 joris 489:
490: if (logstr != NULL)
1.53 joris 491: xfree(logstr);
1.1 deraadt 492:
1.56 joris 493: if (nflag != NULL)
494: xfree(nflag);
1.105 xsa 495:
496: if (orange != NULL)
497: xfree(orange);
1.118 joris 498:
499: if (ofd != -1)
500: (void)close(ofd);
1.56 joris 501:
1.1 deraadt 502: return (0);
1.56 joris 503: }
504:
505: static void
506: rcs_attach_symbol(RCSFILE *file, const char *symname)
507: {
508: char *rnum;
509: RCSNUM *rev;
510: char rbuf[16];
511: int rm;
512:
513: rm = 0;
514: rev = NULL;
515: if ((rnum = strrchr(symname, ':')) != NULL) {
516: if (rnum[1] == '\0')
517: rev = file->rf_head;
518: *(rnum++) = '\0';
519: } else {
520: rm = 1;
521: }
522:
523: if (rev == NULL && rm != 1) {
524: if ((rev = rcsnum_parse(rnum)) == NULL)
1.115 xsa 525: errx(1, "bad revision %s", rnum);
1.56 joris 526: }
527:
1.87 ray 528: if (rcsflags & RCSPROG_NFLAG)
1.56 joris 529: rm = 1;
530:
531: if (rm == 1) {
532: if (rcs_sym_remove(file, symname) < 0) {
1.102 deraadt 533: if (rcs_errno == RCS_ERR_NOENT &&
1.87 ray 534: !(rcsflags & RCSPROG_NFLAG))
1.115 xsa 535: warnx("cannot delete nonexisting symbol %s",
1.110 xsa 536: symname);
1.56 joris 537: } else {
1.87 ray 538: if (rcsflags & RCSPROG_NFLAG)
1.56 joris 539: rm = 0;
540: }
541: }
542:
543: if (rm == 0) {
1.102 deraadt 544: if (rcs_sym_add(file, symname, rev) < 0 &&
545: rcs_errno == RCS_ERR_DUPENT) {
1.56 joris 546: rcsnum_tostr(rcs_sym_getrev(file, symname),
547: rbuf, sizeof(rbuf));
1.115 xsa 548: errx(1, "symbolic name %s already bound to %s",
1.56 joris 549: symname, rbuf);
550: }
551: }
1.1 deraadt 552: }