Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.22
1.22 ! joris 1: /* $OpenBSD: rcsprog.c,v 1.21 2005/10/06 12:56:49 joris 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 <signal.h>
36: #include <string.h>
1.1 deraadt 37: #include <stdio.h>
38: #include <stdlib.h>
39: #include <unistd.h>
40:
41: #include "log.h"
42: #include "rcs.h"
1.9 joris 43: #include "rcsprog.h"
1.1 deraadt 44: #include "strtab.h"
45:
46: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12 joris 47: int verbose = 1;
1.1 deraadt 48:
49: struct rcs_prog {
50: char *prog_name;
51: int (*prog_hdlr)(int, char **);
1.2 deraadt 52: void (*prog_usage)(void);
1.1 deraadt 53: } programs[] = {
1.2 deraadt 54: { "rcs", rcs_main, rcs_usage },
1.17 joris 55: { "ci", checkin_main, checkin_usage },
1.11 joris 56: { "co", checkout_main, checkout_usage },
1.20 joris 57: { "rcsclean", rcsclean_main, rcsclean_usage },
1.18 joris 58: { "rcsdiff", rcsdiff_main, rcsdiff_usage },
1.21 joris 59: { "rlog", rlog_main, rlog_usage },
1.22 ! joris 60: { "ident", ident_main, ident_usage },
1.1 deraadt 61: };
62:
1.9 joris 63: int
64: rcs_statfile(char *fname, char *out, size_t len)
65: {
66: int l;
67: char *s;
68: char filev[MAXPATHLEN], fpath[MAXPATHLEN];
69: struct stat st;
70:
71: l = snprintf(filev, sizeof(filev), "%s%s", fname, RCS_FILE_EXT);
72: if (l == -1 || l >= (int)sizeof(filev))
73: return (-1);
74:
1.16 joris 75: if ((stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
1.9 joris 76: l = snprintf(fpath, sizeof(fpath), "%s/%s", RCSDIR, filev);
77: if (l == -1 || l >= (int)sizeof(fpath))
78: return (-1);
79: } else {
80: strlcpy(fpath, filev, sizeof(fpath));
81: }
82:
83: if (stat(fpath, &st) == -1) {
1.20 joris 84: if (strcmp(__progname, "rcsclean"))
85: cvs_log(LP_ERRNO, "%s", fpath);
1.9 joris 86: return (-1);
87: }
88:
89: strlcpy(out, fpath, len);
1.20 joris 90: if (verbose == 1 && strcmp(__progname, "rcsclean")) {
1.12 joris 91: if (!strcmp(__progname, "co")) {
1.19 joris 92: printf("%s --> ", fpath);
1.12 joris 93: if ((s = strrchr(filev, ',')) != NULL) {
94: *s = '\0';
95: printf("%s\n", fname);
96: }
97: } else {
98: printf("RCS file: %s\n", fpath);
1.9 joris 99: }
100: }
101:
102: return (0);
103: }
1.1 deraadt 104:
105: int
106: main(int argc, char **argv)
107: {
108: u_int i;
109: int ret;
110:
111: ret = -1;
112: cvs_strtab_init();
1.9 joris 113: cvs_log_init(LD_STD, 0);
1.1 deraadt 114:
115: for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2 deraadt 116: if (strcmp(__progname, programs[i].prog_name) == 0) {
117: usage = programs[i].prog_usage;
1.1 deraadt 118: ret = programs[i].prog_hdlr(argc, argv);
1.2 deraadt 119: break;
120: }
1.1 deraadt 121:
122: cvs_strtab_cleanup();
123:
124: return (ret);
125: }
126:
127:
128: void
129: rcs_usage(void)
130: {
131: fprintf(stderr,
1.2 deraadt 132: "usage: %s [-hiLMUV] [-a users] [-b [rev]] [-c string] "
133: "[-e users] [-k opt] file ...\n", __progname);
1.1 deraadt 134: }
135:
136: /*
137: * rcs_main()
138: *
139: * Handler for the `rcs' program.
140: * Returns 0 on success, or >0 on error.
141: */
142: int
143: rcs_main(int argc, char **argv)
144: {
145: int i, ch, flags, kflag, lkmode;
1.10 joris 146: char fpath[MAXPATHLEN];
1.1 deraadt 147: char *oldfile, *alist, *comment, *elist, *unp, *sp;
148: mode_t fmode;
149: RCSFILE *file;
150:
151: kflag = lkmode = -1;
152: fmode = 0;
153: flags = RCS_RDWR;
154: oldfile = alist = comment = elist = NULL;
155:
1.12 joris 156: while ((ch = getopt(argc, argv, "A:a:b::c:e::hik:LMqUV")) != -1) {
1.1 deraadt 157: switch (ch) {
158: case 'A':
159: oldfile = optarg;
160: break;
161: case 'a':
162: alist = optarg;
163: break;
164: case 'c':
165: comment = optarg;
166: break;
167: case 'e':
168: elist = optarg;
169: break;
170: case 'h':
1.2 deraadt 171: (usage)();
1.1 deraadt 172: exit(0);
173: case 'i':
174: flags |= RCS_CREATE;
175: break;
176: case 'k':
177: kflag = rcs_kflag_get(optarg);
178: if (RCS_KWEXP_INVAL(kflag)) {
179: cvs_log(LP_ERR,
180: "invalid keyword substitution mode `%s'",
181: optarg);
182: exit(1);
183: }
184: break;
185: case 'L':
186: if (lkmode == RCS_LOCK_LOOSE)
187: cvs_log(LP_WARN, "-U overriden by -L");
188: lkmode = RCS_LOCK_STRICT;
189: break;
190: case 'M':
191: /* ignore for the moment */
192: break;
1.12 joris 193: case 'q':
194: verbose = 0;
195: break;
1.1 deraadt 196: case 'U':
197: if (lkmode == RCS_LOCK_STRICT)
198: cvs_log(LP_WARN, "-L overriden by -U");
199: lkmode = RCS_LOCK_LOOSE;
200: break;
201: case 'V':
202: printf("%s\n", rcs_version);
203: exit(0);
204: default:
1.2 deraadt 205: (usage)();
1.1 deraadt 206: exit(1);
207: }
208: }
209:
210: argc -= optind;
211: argv += optind;
212: if (argc == 0) {
213: cvs_log(LP_ERR, "no input file");
1.5 joris 214: (usage)();
1.1 deraadt 215: exit(1);
216: }
217:
218: for (i = 0; i < argc; i++) {
1.9 joris 219: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8 joris 220: continue;
1.6 joris 221:
222: file = rcs_open(fpath, flags, fmode);
223: if (file == NULL)
224: continue;
1.1 deraadt 225:
226: /* entries to add to the access list */
227: if (alist != NULL) {
228: unp = alist;
229: do {
230: sp = strchr(unp, ',');
231: if (sp != NULL)
232: *(sp++) = '\0';
233:
234: rcs_access_add(file, unp);
235:
236: unp = sp;
237: } while (sp != NULL);
238: }
239:
240: if (comment != NULL)
241: rcs_comment_set(file, comment);
242:
243: if (kflag != -1)
244: rcs_kwexp_set(file, kflag);
245:
246: if (lkmode != -1)
247: rcs_lock_setmode(file, lkmode);
248:
249: rcs_close(file);
1.9 joris 250:
1.14 xsa 251: if (verbose == 1)
1.12 joris 252: printf("done\n");
1.1 deraadt 253: }
254:
255: return (0);
256: }