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