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