Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.36
1.36 ! xsa 1: /* $OpenBSD: rcsprog.c,v 1.35 2005/10/19 11:37:11 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.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: #include "strtab.h"
44:
1.29 joris 45: #define RCS_CMD_MAXARG 128
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.36 ! xsa 53: char *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28 joris 54:
1.1 deraadt 55: struct rcs_prog {
1.26 deraadt 56: char *prog_name;
57: int (*prog_hdlr)(int, char **);
58: void (*prog_usage)(void);
1.1 deraadt 59: } programs[] = {
1.2 deraadt 60: { "rcs", rcs_main, rcs_usage },
1.17 joris 61: { "ci", checkin_main, checkin_usage },
1.11 joris 62: { "co", checkout_main, checkout_usage },
1.20 joris 63: { "rcsclean", rcsclean_main, rcsclean_usage },
1.18 joris 64: { "rcsdiff", rcsdiff_main, rcsdiff_usage },
1.33 xsa 65: { "rcsmerge", rcsmerge_main, rcsmerge_usage },
1.21 joris 66: { "rlog", rlog_main, rlog_usage },
1.22 joris 67: { "ident", ident_main, ident_usage },
1.1 deraadt 68: };
1.31 joris 69:
70: void
71: rcs_set_rev(const char *str, RCSNUM **rev)
72: {
1.32 joris 73: if (str == NULL)
74: return;
75:
1.31 joris 76: if (*rev != RCS_HEAD_REV)
77: cvs_log(LP_WARN, "redefinition of revision number");
78:
79: if ((*rev = rcsnum_parse(str)) == NULL) {
80: cvs_log(LP_ERR, "bad revision number '%s'", str);
81: exit (1);
82: }
83: }
1.1 deraadt 84:
1.9 joris 85: int
1.29 joris 86: rcs_init(char *envstr, char **argv, int argvlen)
87: {
88: u_int i;
89: int argc, error;
90: char linebuf[256], *lp, *cp;
91:
92: strlcpy(linebuf, envstr, sizeof(linebuf));
93: memset(argv, 0, argvlen * sizeof(char *));
94:
95: error = argc = 0;
96: for (lp = linebuf; lp != NULL;) {
97: cp = strsep(&lp, " \t\b\f\n\r\t\v");;
98: if (cp == NULL)
99: break;
100: else if (*cp == '\0')
101: continue;
102:
103: if (argc == argvlen) {
104: error++;
105: break;
106: }
107:
108: argv[argc] = strdup(cp);
109: if (argv[argc] == NULL) {
110: cvs_log(LP_ERRNO, "failed to copy argument");
111: error++;
112: break;
113: }
114:
115: argc++;
116: }
117:
118: if (error != 0) {
119: for (i = 0; i < (u_int)argc; i++)
120: free(argv[i]);
121: argc = -1;
122: }
123:
124: return (argc);
125: }
126:
127: int
1.28 joris 128: rcs_getopt(int argc, char **argv, const char *optstr)
129: {
130: char *a;
131: const char *c;
132: static int i = 1;
133: int opt, hasargument, ret;
134:
135: hasargument = 0;
136: rcs_optarg = NULL;
137:
138: if (i >= argc)
139: return (-1);
140:
141: a = argv[i++];
142: if (*a++ != '-')
143: return (-1);
144:
145: ret = 0;
146: opt = *a;
147: for (c = optstr; *c != '\0'; c++) {
148: if (*c == opt) {
149: a++;
150: ret = opt;
151:
152: if (*(c + 1) == ':') {
153: if (*(c + 2) == ':') {
154: if (*a != '\0')
155: hasargument = 1;
156: } else {
157: if (*a != '\0') {
158: hasargument = 1;
159: } else {
160: ret = 1;
161: break;
162: }
163: }
164: }
165:
166: if (hasargument == 1)
167: rcs_optarg = a;
168:
169: if (ret == opt)
170: rcs_optind++;
171: break;
172: }
173: }
174:
175: if (ret == 0)
176: cvs_log(LP_ERR, "unknown option -%c", opt);
177: else if (ret == 1)
178: cvs_log(LP_ERR, "missing argument for option -%c", opt);
179:
180: return (ret);
181: }
182:
183: int
1.9 joris 184: rcs_statfile(char *fname, char *out, size_t len)
185: {
186: int l;
187: char filev[MAXPATHLEN], fpath[MAXPATHLEN];
188: struct stat st;
189:
190: l = snprintf(filev, sizeof(filev), "%s%s", fname, RCS_FILE_EXT);
191: if (l == -1 || l >= (int)sizeof(filev))
192: return (-1);
193:
1.16 joris 194: if ((stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
1.9 joris 195: l = snprintf(fpath, sizeof(fpath), "%s/%s", RCSDIR, filev);
196: if (l == -1 || l >= (int)sizeof(fpath))
197: return (-1);
198: } else {
199: strlcpy(fpath, filev, sizeof(fpath));
200: }
201:
202: if (stat(fpath, &st) == -1) {
1.20 joris 203: if (strcmp(__progname, "rcsclean"))
204: cvs_log(LP_ERRNO, "%s", fpath);
1.9 joris 205: return (-1);
206: }
207:
208: strlcpy(out, fpath, len);
209:
210: return (0);
211: }
1.1 deraadt 212:
213: int
214: main(int argc, char **argv)
215: {
216: u_int i;
1.29 joris 217: char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
218: int ret, cmd_argc;
1.1 deraadt 219:
220: ret = -1;
1.28 joris 221: rcs_optind = 1;
1.1 deraadt 222: cvs_strtab_init();
1.9 joris 223: cvs_log_init(LD_STD, 0);
1.1 deraadt 224:
1.29 joris 225: cmd_argc = 0;
1.30 joris 226: cmd_argv[cmd_argc++] = argv[0];
1.29 joris 227: if ((rcsinit = getenv("RCSINIT")) != NULL) {
228: ret = rcs_init(rcsinit, cmd_argv + 1,
229: RCS_CMD_MAXARG - 1);
230: if (ret < 0) {
231: cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
232: exit (1);
233: }
234:
235: cmd_argc += ret;
236: }
1.36 ! xsa 237:
! 238: if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
! 239: rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29 joris 240:
241: for (ret = 1; ret < argc; ret++)
242: cmd_argv[cmd_argc++] = argv[ret];
243:
1.1 deraadt 244: for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2 deraadt 245: if (strcmp(__progname, programs[i].prog_name) == 0) {
246: usage = programs[i].prog_usage;
1.29 joris 247: ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2 deraadt 248: break;
249: }
1.1 deraadt 250:
251: cvs_strtab_cleanup();
252:
1.23 niallo 253: exit(ret);
1.1 deraadt 254: }
255:
256:
257: void
258: rcs_usage(void)
259: {
260: fprintf(stderr,
1.26 deraadt 261: "usage: rcs [-hiLMUV] [-a users] [-b [rev]] [-c string] [-e users]\n"
262: " [-k opt] [-m rev:log] file ...\n");
1.1 deraadt 263: }
264:
265: /*
266: * rcs_main()
267: *
268: * Handler for the `rcs' program.
269: * Returns 0 on success, or >0 on error.
270: */
271: int
272: rcs_main(int argc, char **argv)
273: {
274: int i, ch, flags, kflag, lkmode;
1.10 joris 275: char fpath[MAXPATHLEN];
1.24 joris 276: char *logstr, *logmsg;
1.1 deraadt 277: char *oldfile, *alist, *comment, *elist, *unp, *sp;
278: mode_t fmode;
279: RCSFILE *file;
1.24 joris 280: RCSNUM *logrev;
1.1 deraadt 281:
282: kflag = lkmode = -1;
283: fmode = 0;
284: flags = RCS_RDWR;
1.24 joris 285: logstr = oldfile = alist = comment = elist = NULL;
1.1 deraadt 286:
1.28 joris 287: while ((ch = rcs_getopt(argc, argv, "A:a:b::c:e::hik:Lm:MqUV")) != -1) {
1.1 deraadt 288: switch (ch) {
289: case 'A':
1.28 joris 290: oldfile = rcs_optarg;
1.1 deraadt 291: break;
292: case 'a':
1.28 joris 293: alist = rcs_optarg;
1.1 deraadt 294: break;
295: case 'c':
1.28 joris 296: comment = rcs_optarg;
1.1 deraadt 297: break;
298: case 'e':
1.28 joris 299: elist = rcs_optarg;
1.1 deraadt 300: break;
301: case 'h':
1.2 deraadt 302: (usage)();
1.1 deraadt 303: exit(0);
304: case 'i':
305: flags |= RCS_CREATE;
306: break;
307: case 'k':
1.28 joris 308: kflag = rcs_kflag_get(rcs_optarg);
1.1 deraadt 309: if (RCS_KWEXP_INVAL(kflag)) {
310: cvs_log(LP_ERR,
311: "invalid keyword substitution mode `%s'",
1.28 joris 312: rcs_optarg);
1.1 deraadt 313: exit(1);
314: }
315: break;
316: case 'L':
317: if (lkmode == RCS_LOCK_LOOSE)
318: cvs_log(LP_WARN, "-U overriden by -L");
319: lkmode = RCS_LOCK_STRICT;
320: break;
1.24 joris 321: case 'm':
1.28 joris 322: if ((logstr = strdup(rcs_optarg)) == NULL) {
1.24 joris 323: cvs_log(LP_ERRNO, "failed to copy logstring");
324: exit(1);
325: }
326: break;
1.1 deraadt 327: case 'M':
328: /* ignore for the moment */
329: break;
1.12 joris 330: case 'q':
331: verbose = 0;
332: break;
1.1 deraadt 333: case 'U':
334: if (lkmode == RCS_LOCK_STRICT)
335: cvs_log(LP_WARN, "-L overriden by -U");
336: lkmode = RCS_LOCK_LOOSE;
337: break;
338: case 'V':
339: printf("%s\n", rcs_version);
340: exit(0);
341: default:
1.2 deraadt 342: (usage)();
1.1 deraadt 343: exit(1);
344: }
345: }
346:
1.28 joris 347: argc -= rcs_optind;
348: argv += rcs_optind;
1.30 joris 349:
1.1 deraadt 350: if (argc == 0) {
351: cvs_log(LP_ERR, "no input file");
1.5 joris 352: (usage)();
1.1 deraadt 353: exit(1);
354: }
355:
356: for (i = 0; i < argc; i++) {
1.9 joris 357: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8 joris 358: continue;
1.6 joris 359:
1.35 niallo 360: if (verbose == 1)
361: printf("RCS file: %s\n", fpath);
1.6 joris 362: file = rcs_open(fpath, flags, fmode);
363: if (file == NULL)
364: continue;
1.1 deraadt 365:
1.24 joris 366: if (logstr != NULL) {
367: if ((logmsg = strchr(logstr, ':')) == NULL) {
368: cvs_log(LP_ERR, "missing log message");
369: rcs_close(file);
370: continue;
371: }
372:
373: *logmsg++ = '\0';
374: if ((logrev = rcsnum_parse(logstr)) == NULL) {
375: cvs_log(LP_ERR, "'%s' bad revision number", logstr);
376: rcs_close(file);
377: continue;
378: }
379:
380: if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
381: cvs_log(LP_ERR,
382: "failed to set logmsg for '%s' to '%s'",
383: logstr, logmsg);
384: rcs_close(file);
1.25 joris 385: rcsnum_free(logrev);
1.24 joris 386: continue;
387: }
388:
389: rcsnum_free(logrev);
390: }
391:
1.1 deraadt 392: /* entries to add to the access list */
393: if (alist != NULL) {
394: unp = alist;
395: do {
396: sp = strchr(unp, ',');
397: if (sp != NULL)
398: *(sp++) = '\0';
399:
400: rcs_access_add(file, unp);
401:
402: unp = sp;
403: } while (sp != NULL);
404: }
405:
406: if (comment != NULL)
407: rcs_comment_set(file, comment);
408:
409: if (kflag != -1)
410: rcs_kwexp_set(file, kflag);
411:
412: if (lkmode != -1)
413: rcs_lock_setmode(file, lkmode);
414:
415: rcs_close(file);
1.9 joris 416:
1.14 xsa 417: if (verbose == 1)
1.12 joris 418: printf("done\n");
1.1 deraadt 419: }
1.24 joris 420:
421: if (logstr != NULL)
422: free(logstr);
1.1 deraadt 423:
424: return (0);
425: }