Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.11
1.11 ! ray 1: /* $OpenBSD: rcsutil.c,v 1.10 2006/05/09 12:31:27 ray Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
4: * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
5: * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6: * Copyright (c) 2006 Ray Lai <ray@openbsd.org>
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: *
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #include "includes.h"
31:
32: #include "rcsprog.h"
33:
34: /*
35: * rcs_get_mtime()
36: *
37: * Get <filename> last modified time.
38: * Returns last modified time on success, or -1 on failure.
39: */
40: time_t
1.6 joris 41: rcs_get_mtime(RCSFILE *file)
1.1 xsa 42: {
43: struct stat st;
44: time_t mtime;
45:
1.6 joris 46: if (fstat(file->fd, &st) == -1) {
47: warn("%s", file->rf_path);
1.1 xsa 48: return (-1);
49: }
1.6 joris 50:
1.1 xsa 51: mtime = (time_t)st.st_mtimespec.tv_sec;
52:
53: return (mtime);
54: }
55:
56: /*
57: * rcs_set_mtime()
58: *
59: * Set <filename> last modified time to <mtime> if it's not set to -1.
60: */
61: void
1.6 joris 62: rcs_set_mtime(RCSFILE *file, time_t mtime)
1.1 xsa 63: {
64: static struct timeval tv[2];
65:
66: if (mtime == -1)
67: return;
68:
69: tv[0].tv_sec = mtime;
70: tv[1].tv_sec = tv[0].tv_sec;
71:
1.6 joris 72: if (futimes(file->fd, tv) == -1)
1.3 xsa 73: err(1, "utimes");
1.1 xsa 74: }
75:
76: int
77: rcs_getopt(int argc, char **argv, const char *optstr)
78: {
79: char *a;
80: const char *c;
81: static int i = 1;
82: int opt, hasargument, ret;
83:
84: hasargument = 0;
85: rcs_optarg = NULL;
86:
87: if (i >= argc)
88: return (-1);
89:
90: a = argv[i++];
91: if (*a++ != '-')
92: return (-1);
93:
94: ret = 0;
95: opt = *a;
96: for (c = optstr; *c != '\0'; c++) {
97: if (*c == opt) {
98: a++;
99: ret = opt;
100:
101: if (*(c + 1) == ':') {
102: if (*(c + 2) == ':') {
103: if (*a != '\0')
104: hasargument = 1;
105: } else {
106: if (*a != '\0') {
107: hasargument = 1;
108: } else {
109: ret = 1;
110: break;
111: }
112: }
113: }
114:
115: if (hasargument == 1)
116: rcs_optarg = a;
117:
118: if (ret == opt)
119: rcs_optind++;
120: break;
121: }
122: }
123:
124: if (ret == 0)
125: warnx("unknown option -%c", opt);
126: else if (ret == 1)
127: warnx("missing argument for option -%c", opt);
128:
129: return (ret);
130: }
131:
132: /*
133: * rcs_choosefile()
134: *
135: * Given a relative filename, decide where the corresponding RCS file
136: * should be. Tries each extension until a file is found. If no file
137: * was found, returns a path with the first extension.
138: *
1.11 ! ray 139: * Opens and returns file descriptor to RCS file.
1.1 xsa 140: */
1.6 joris 141: int
142: rcs_choosefile(const char *filename, char *out, size_t len)
1.1 xsa 143: {
1.6 joris 144: int fd;
1.1 xsa 145: struct stat sb;
146: char *p, *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
1.10 ray 147: *suffixes, rcspath[MAXPATHLEN];
1.1 xsa 148:
149: /* If -x flag was not given, use default. */
150: if (rcs_suffixes == NULL)
151: rcs_suffixes = RCS_DEFAULT_SUFFIX;
152:
1.6 joris 153: fd = -1;
154:
1.1 xsa 155: /*
156: * If `filename' contains a directory, `rcspath' contains that
157: * directory, including a trailing slash. Otherwise `rcspath'
158: * contains an empty string.
159: */
160: if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 161: errx(1, "rcs_choosefile: truncation");
162:
1.1 xsa 163: /* If `/' is found, end string after `/'. */
164: if ((ptr = strrchr(rcspath, '/')) != NULL)
165: *(++ptr) = '\0';
166: else
167: rcspath[0] = '\0';
168:
169: /* Append RCS/ to `rcspath' if it exists. */
170: if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
171: strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
1.6 joris 172: errx(1, "rcs_choosefile: truncation");
173:
1.1 xsa 174: if (stat(rcsdir, &sb) == 0 && (sb.st_mode & S_IFDIR))
1.6 joris 175: if (strlcpy(rcspath, rcsdir, sizeof(rcspath))
176: >= sizeof(rcspath) ||
1.1 xsa 177: strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 178: errx(1, "rcs_choosefile: truncation");
1.1 xsa 179:
180: /* Name of file without path. */
181: if ((ptr = strrchr(filename, '/')) == NULL) {
182: if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
1.6 joris 183: errx(1, "rcs_choosefile: truncation");
1.1 xsa 184: } else {
185: /* Skip `/'. */
186: if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
1.6 joris 187: errx(1, "rcs_choosefile: truncation");
1.1 xsa 188: }
189:
190: /* Name of RCS file without an extension. */
191: if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 192: errx(1, "rcs_choosefile: truncation");
1.1 xsa 193:
194: /*
195: * If only the empty suffix was given, use existing rcspath.
196: * This ensures that there is at least one suffix for strsep().
197: */
198: if (strcmp(rcs_suffixes, "") == 0) {
1.6 joris 199: fd = open(rcspath, O_RDONLY);
1.8 ray 200: if (strlcpy(out, rcspath, len) >= len)
201: errx(1, "rcs_choosefile: truncation");
1.6 joris 202: return (fd);
1.1 xsa 203: }
204:
205: /*
206: * Cycle through slash-separated `rcs_suffixes', appending each
207: * extension to `rcspath' and testing if the file exists. If it
208: * does, return that string. Otherwise return path with first
209: * extension.
210: */
211: suffixes = xstrdup(rcs_suffixes);
1.10 ray 212: for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
1.1 xsa 213: char fpath[MAXPATHLEN];
214:
215: if ((p = strrchr(rcspath, ',')) != NULL) {
216: if (!strcmp(p, ext)) {
1.6 joris 217: if ((fd = open(rcspath, O_RDONLY)) == -1)
218: continue;
219:
220: if (fstat(fd, &sb) == -1)
221: err(1, "%s", rcspath);
222:
223: if (strlcpy(out, rcspath, len) >= len)
224: errx(1, "rcs_choosefile; truncation");
225:
226: return (fd);
1.1 xsa 227: }
228:
229: continue;
230: }
231:
232: /* Construct RCS file path. */
233: if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
234: strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
1.6 joris 235: errx(1, "rcs_choosefile: truncation");
1.1 xsa 236:
237: /* Don't use `filename' as RCS file. */
238: if (strcmp(fpath, filename) == 0)
239: continue;
240:
1.6 joris 241: if ((fd = open(fpath, O_RDONLY)) == -1)
242: continue;
243:
244: if (fstat(fd, &sb) == -1)
245: err(1, "%s", fpath);
246:
247: if (strlcpy(out, fpath, len) >= len)
248: errx(1, "rcs_choosefile: truncation");
249:
250: return (fd);
1.1 xsa 251: }
252:
253: /*
254: * `suffixes' should now be NUL separated, so the first
255: * extension can be read just by reading `suffixes'.
256: */
1.11 ! ray 257: if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 258: errx(1, "rcs_choosefile: truncation");
1.1 xsa 259:
260: xfree(suffixes);
1.6 joris 261:
262: fd = open(rcspath, O_RDONLY);
1.8 ray 263: if (strlcpy(out, rcspath, len) >= len)
264: errx(1, "rcs_choosefile: truncation");
1.6 joris 265:
266: return (fd);
1.1 xsa 267: }
268:
269: /*
270: * Find the name of an RCS file, given a file name `fname'. If an RCS
271: * file is found, the name is copied to the `len' sized buffer `out'.
272: * Returns 0 if RCS file was found, -1 otherwise.
273: */
274: int
275: rcs_statfile(char *fname, char *out, size_t len, int flags)
276: {
1.6 joris 277: int fd;
1.1 xsa 278:
1.6 joris 279: fd = rcs_choosefile(fname, out, len);
280: if (fd == -1 && !(flags & RCS_CREATE)) {
1.1 xsa 281: if (strcmp(__progname, "rcsclean") != 0 &&
282: strcmp(__progname, "ci") != 0)
1.9 ray 283: warnx("%s", out);
1.1 xsa 284: return (-1);
285: }
286:
1.6 joris 287: return (fd);
1.1 xsa 288: }
289:
290: /*
291: * Allocate an RCSNUM and store in <rev>.
292: */
293: void
294: rcs_set_rev(const char *str, RCSNUM **rev)
295: {
296: if (str == NULL || (*rev = rcsnum_parse(str)) == NULL)
1.4 xsa 297: errx(1, "bad revision number `%s'", str);
1.1 xsa 298: }
299:
300: /*
301: * Set <str> to <new_str>. Print warning if <str> is redefined.
302: */
303: void
304: rcs_setrevstr(char **str, char *new_str)
305: {
306: if (new_str == NULL)
307: return;
308: if (*str != NULL)
309: warnx("redefinition of revision number");
310: *str = new_str;
311: }
312:
313: /*
314: * Set <str1> or <str2> to <new_str>, depending on which is not set.
315: * If both are set, error out.
316: */
317: void
318: rcs_setrevstr2(char **str1, char **str2, char *new_str)
319: {
320: if (new_str == NULL)
321: return;
322: if (*str1 == NULL)
323: *str1 = new_str;
324: else if (*str2 == NULL)
325: *str2 = new_str;
326: else
1.3 xsa 327: errx(1, "too many revision numbers");
1.1 xsa 328: }
329:
330: /*
331: * Get revision from file. The revision can be specified as a symbol or
332: * a revision number.
333: */
334: RCSNUM *
335: rcs_getrevnum(const char *rev_str, RCSFILE *file)
336: {
337: RCSNUM *rev;
338:
339: /* Search for symbol. */
340: rev = rcs_sym_getrev(file, rev_str);
341:
342: /* Search for revision number. */
343: if (rev == NULL)
344: rev = rcsnum_parse(rev_str);
345:
346: return (rev);
347: }
348:
349: /*
350: * Prompt for and store user's input in an allocated string.
351: *
352: * Returns the string's pointer.
353: */
354: char *
355: rcs_prompt(const char *prompt)
356: {
357: BUF *bp;
358: size_t len;
359: char *buf;
360:
1.5 joris 361: bp = rcs_buf_alloc(0, BUF_AUTOEXT);
1.1 xsa 362: if (isatty(STDIN_FILENO))
363: (void)fprintf(stderr, "%s", prompt);
364: if (isatty(STDIN_FILENO))
365: (void)fprintf(stderr, ">> ");
366: while ((buf = fgetln(stdin, &len)) != NULL) {
367: /* The last line may not be EOL terminated. */
368: if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
369: break;
370: else
1.5 joris 371: rcs_buf_append(bp, buf, len);
1.1 xsa 372:
373: if (isatty(STDIN_FILENO))
374: (void)fprintf(stderr, ">> ");
375: }
1.5 joris 376: rcs_buf_putc(bp, '\0');
1.1 xsa 377:
1.5 joris 378: return (rcs_buf_release(bp));
1.1 xsa 379: }
380:
381: u_int
382: rcs_rev_select(RCSFILE *file, char *range)
383: {
384: int i;
385: u_int nrev;
386: char *ep;
387: char *lstr, *rstr;
388: struct rcs_delta *rdp;
1.5 joris 389: struct rcs_argvector *revargv, *revrange;
1.1 xsa 390: RCSNUM lnum, rnum;
391:
392: nrev = 0;
393: (void)memset(&lnum, 0, sizeof(lnum));
394: (void)memset(&rnum, 0, sizeof(rnum));
395:
396: if (range == NULL) {
397: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
398: if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
399: rdp->rd_flags |= RCS_RD_SELECT;
400: return (1);
401: }
402: return (0);
403: }
404:
1.5 joris 405: revargv = rcs_strsplit(range, ",");
1.1 xsa 406: for (i = 0; revargv->argv[i] != NULL; i++) {
1.5 joris 407: revrange = rcs_strsplit(revargv->argv[i], ":");
1.1 xsa 408: if (revrange->argv[0] == NULL)
409: /* should not happen */
1.3 xsa 410: errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1 xsa 411: else if (revrange->argv[1] == NULL)
412: lstr = rstr = revrange->argv[0];
413: else {
414: if (revrange->argv[2] != NULL)
1.3 xsa 415: errx(1, "invalid revision range: %s",
416: revargv->argv[i]);
1.1 xsa 417: lstr = revrange->argv[0];
418: rstr = revrange->argv[1];
419: if (strcmp(lstr, "") == 0)
420: lstr = NULL;
421: if (strcmp(rstr, "") == 0)
422: rstr = NULL;
423: }
424:
425: if (lstr == NULL)
426: lstr = RCS_HEAD_INIT;
427: if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3 xsa 428: errx(1, "invalid revision: %s", lstr);
1.1 xsa 429:
430: if (rstr != NULL) {
431: if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3 xsa 432: errx(1, "invalid revision: %s", rstr);
1.1 xsa 433: } else
434: rcsnum_cpy(file->rf_head, &rnum, 0);
435:
1.5 joris 436: rcs_argv_destroy(revrange);
1.1 xsa 437:
438: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
439: if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
440: rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
441: !(rdp->rd_flags & RCS_RD_SELECT)) {
442: rdp->rd_flags |= RCS_RD_SELECT;
443: nrev++;
444: }
445: }
1.5 joris 446: rcs_argv_destroy(revargv);
1.1 xsa 447:
448: if (lnum.rn_id != NULL)
449: xfree(lnum.rn_id);
450: if (rnum.rn_id != NULL)
451: xfree(rnum.rn_id);
452:
453: return (nrev);
1.2 ray 454: }
455:
456: /*
457: * Load description from <in> to <file>.
458: * If <in> starts with a `-', <in> is taken as the description.
459: * Otherwise <in> is the name of the file containing the description.
460: * If <in> is NULL, the description is read from stdin.
461: */
462: void
463: rcs_set_description(RCSFILE *file, const char *in)
464: {
465: BUF *bp;
466: char *content;
467: const char *prompt =
468: "enter description, terminated with single '.' or end of file:\n"
469: "NOTE: This is NOT the log message!\n";
470:
471: /* Description is in file <in>. */
472: if (in != NULL && *in != '-') {
1.5 joris 473: bp = rcs_buf_load(in, BUF_AUTOEXT);
474: rcs_buf_putc(bp, '\0');
475: content = rcs_buf_release(bp);
1.2 ray 476: /* Description is in <in>. */
477: } else if (in != NULL)
478: /* Skip leading `-'. */
479: content = xstrdup(in + 1);
480: /* Get description from stdin. */
481: else
482: content = rcs_prompt(prompt);
483:
484: rcs_desc_set(file, content);
485: xfree(content);
1.7 xsa 486: }
487:
488: /*
489: * Split the contents of a file into a list of lines.
490: */
491: struct rcs_lines *
492: rcs_splitlines(const char *fcont)
493: {
494: char *dcp;
495: struct rcs_lines *lines;
496: struct rcs_line *lp;
497:
498: lines = xmalloc(sizeof(*lines));
499: TAILQ_INIT(&(lines->l_lines));
500: lines->l_nblines = 0;
501: lines->l_data = xstrdup(fcont);
502:
503: lp = xmalloc(sizeof(*lp));
504: lp->l_line = NULL;
505: lp->l_lineno = 0;
506: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
507:
508: for (dcp = lines->l_data; *dcp != '\0';) {
509: lp = xmalloc(sizeof(*lp));
510: lp->l_line = dcp;
511: lp->l_lineno = ++(lines->l_nblines);
512: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
513:
514: dcp = strchr(dcp, '\n');
515: if (dcp == NULL)
516: break;
517: *(dcp++) = '\0';
518: }
519:
520: return (lines);
521: }
522:
523: void
524: rcs_freelines(struct rcs_lines *lines)
525: {
526: struct rcs_line *lp;
527:
528: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
529: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
530: xfree(lp);
531: }
532:
533: xfree(lines->l_data);
534: xfree(lines);
535: }
536:
537: BUF *
538: rcs_patchfile(const char *data, const char *patch,
539: int (*p)(struct rcs_lines *, struct rcs_lines *))
540: {
541: struct rcs_lines *dlines, *plines;
542: struct rcs_line *lp;
543: size_t len;
544: int lineno;
545: BUF *res;
546:
547: len = strlen(data);
548:
549: if ((dlines = rcs_splitlines(data)) == NULL)
550: return (NULL);
551:
552: if ((plines = rcs_splitlines(patch)) == NULL)
553: return (NULL);
554:
555: if (p(dlines, plines) < 0) {
556: rcs_freelines(dlines);
557: rcs_freelines(plines);
558: return (NULL);
559: }
560:
561: lineno = 0;
562: res = rcs_buf_alloc(len, BUF_AUTOEXT);
563: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
564: if (lineno != 0)
565: rcs_buf_fappend(res, "%s\n", lp->l_line);
566: lineno++;
567: }
568:
569: rcs_freelines(dlines);
570: rcs_freelines(plines);
571: return (res);
572: }
573:
574: /*
575: * rcs_yesno()
576: *
577: * Read from standart input for `y' or `Y' character.
578: * Returns 0 on success, or -1 on failure.
579: */
580: int
581: rcs_yesno(void)
582: {
583: int c, ret;
584:
585: ret = 0;
586:
587: fflush(stderr);
588: fflush(stdout);
589:
590: if ((c = getchar()) != 'y' && c != 'Y')
591: ret = -1;
592: else
593: while (c != EOF && c != '\n')
594: c = getchar();
595:
596: return (ret);
597: }
598:
599: /*
600: * rcs_strsplit()
601: *
602: * Split a string <str> of <sep>-separated values and allocate
603: * an argument vector for the values found.
604: */
605: struct rcs_argvector *
606: rcs_strsplit(char *str, const char *sep)
607: {
608: struct rcs_argvector *av;
609: size_t i = 0;
610: char **nargv;
611: char *cp, *p;
612:
613: cp = xstrdup(str);
614: av = xmalloc(sizeof(*av));
615: av->str = cp;
616: av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
617:
618: while ((p = strsep(&cp, sep)) != NULL) {
619: av->argv[i++] = p;
620: nargv = xrealloc(av->argv,
621: i + 1, sizeof(*(av->argv)));
622: av->argv = nargv;
623: }
624: av->argv[i] = NULL;
625:
626: return (av);
627: }
628:
629: /*
630: * rcs_argv_destroy()
631: *
632: * Free an argument vector previously allocated by rcs_strsplit().
633: */
634: void
635: rcs_argv_destroy(struct rcs_argvector *av)
636: {
637: xfree(av->str);
638: xfree(av->argv);
639: xfree(av);
1.1 xsa 640: }