Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.37
1.37 ! tobias 1: /* $OpenBSD: rcsutil.c,v 1.36 2010/09/08 15:15:50 tobias 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:
1.29 xsa 30: #include <sys/stat.h>
31:
32: #include <ctype.h>
33: #include <err.h>
34: #include <fcntl.h>
35: #include <stdio.h>
36: #include <string.h>
37: #include <unistd.h>
1.1 xsa 38:
39: #include "rcsprog.h"
40:
41: /*
42: * rcs_get_mtime()
43: *
44: * Get <filename> last modified time.
45: * Returns last modified time on success, or -1 on failure.
46: */
47: time_t
1.6 joris 48: rcs_get_mtime(RCSFILE *file)
1.1 xsa 49: {
50: struct stat st;
51: time_t mtime;
52:
1.37 ! tobias 53: if (file->rf_file == NULL)
! 54: return (-1);
! 55:
! 56: if (fstat(fileno(file->rf_file), &st) == -1) {
1.6 joris 57: warn("%s", file->rf_path);
1.1 xsa 58: return (-1);
59: }
1.6 joris 60:
1.1 xsa 61: mtime = (time_t)st.st_mtimespec.tv_sec;
62:
63: return (mtime);
64: }
65:
66: /*
67: * rcs_set_mtime()
68: *
69: * Set <filename> last modified time to <mtime> if it's not set to -1.
70: */
71: void
1.6 joris 72: rcs_set_mtime(RCSFILE *file, time_t mtime)
1.1 xsa 73: {
74: static struct timeval tv[2];
75:
1.37 ! tobias 76: if (file->rf_file == NULL || mtime == -1)
1.1 xsa 77: return;
78:
79: tv[0].tv_sec = mtime;
80: tv[1].tv_sec = tv[0].tv_sec;
81:
1.37 ! tobias 82: if (futimes(fileno(file->rf_file), tv) == -1)
1.3 xsa 83: err(1, "utimes");
1.1 xsa 84: }
85:
86: int
87: rcs_getopt(int argc, char **argv, const char *optstr)
88: {
89: char *a;
90: const char *c;
91: static int i = 1;
92: int opt, hasargument, ret;
93:
94: hasargument = 0;
95: rcs_optarg = NULL;
96:
97: if (i >= argc)
98: return (-1);
99:
100: a = argv[i++];
101: if (*a++ != '-')
102: return (-1);
103:
104: ret = 0;
105: opt = *a;
106: for (c = optstr; *c != '\0'; c++) {
107: if (*c == opt) {
108: a++;
109: ret = opt;
110:
111: if (*(c + 1) == ':') {
112: if (*(c + 2) == ':') {
113: if (*a != '\0')
114: hasargument = 1;
115: } else {
116: if (*a != '\0') {
117: hasargument = 1;
118: } else {
119: ret = 1;
120: break;
121: }
122: }
123: }
124:
125: if (hasargument == 1)
126: rcs_optarg = a;
127:
128: if (ret == opt)
129: rcs_optind++;
130: break;
131: }
132: }
133:
134: if (ret == 0)
135: warnx("unknown option -%c", opt);
136: else if (ret == 1)
137: warnx("missing argument for option -%c", opt);
138:
139: return (ret);
140: }
141:
142: /*
143: * rcs_choosefile()
144: *
145: * Given a relative filename, decide where the corresponding RCS file
146: * should be. Tries each extension until a file is found. If no file
147: * was found, returns a path with the first extension.
148: *
1.11 ray 149: * Opens and returns file descriptor to RCS file.
1.1 xsa 150: */
1.6 joris 151: int
152: rcs_choosefile(const char *filename, char *out, size_t len)
1.1 xsa 153: {
1.6 joris 154: int fd;
1.1 xsa 155: struct stat sb;
156: char *p, *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
1.10 ray 157: *suffixes, rcspath[MAXPATHLEN];
1.1 xsa 158:
1.6 joris 159: fd = -1;
160:
1.1 xsa 161: /*
162: * If `filename' contains a directory, `rcspath' contains that
163: * directory, including a trailing slash. Otherwise `rcspath'
164: * contains an empty string.
165: */
166: if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 167: errx(1, "rcs_choosefile: truncation");
168:
1.1 xsa 169: /* If `/' is found, end string after `/'. */
170: if ((ptr = strrchr(rcspath, '/')) != NULL)
171: *(++ptr) = '\0';
172: else
173: rcspath[0] = '\0';
174:
175: /* Append RCS/ to `rcspath' if it exists. */
176: if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
177: strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
1.6 joris 178: errx(1, "rcs_choosefile: truncation");
179:
1.19 otto 180: if (stat(rcsdir, &sb) == 0 && S_ISDIR(sb.st_mode))
1.6 joris 181: if (strlcpy(rcspath, rcsdir, sizeof(rcspath))
182: >= sizeof(rcspath) ||
1.1 xsa 183: strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 184: errx(1, "rcs_choosefile: truncation");
1.1 xsa 185:
186: /* Name of file without path. */
187: if ((ptr = strrchr(filename, '/')) == NULL) {
188: if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
1.6 joris 189: errx(1, "rcs_choosefile: truncation");
1.1 xsa 190: } else {
191: /* Skip `/'. */
192: if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
1.6 joris 193: errx(1, "rcs_choosefile: truncation");
1.1 xsa 194: }
195:
196: /* Name of RCS file without an extension. */
197: if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 198: errx(1, "rcs_choosefile: truncation");
1.1 xsa 199:
200: /*
201: * If only the empty suffix was given, use existing rcspath.
202: * This ensures that there is at least one suffix for strsep().
203: */
204: if (strcmp(rcs_suffixes, "") == 0) {
1.8 ray 205: if (strlcpy(out, rcspath, len) >= len)
206: errx(1, "rcs_choosefile: truncation");
1.22 niallo 207: fd = open(rcspath, O_RDONLY);
1.6 joris 208: return (fd);
1.1 xsa 209: }
210:
211: /*
212: * Cycle through slash-separated `rcs_suffixes', appending each
213: * extension to `rcspath' and testing if the file exists. If it
214: * does, return that string. Otherwise return path with first
215: * extension.
216: */
217: suffixes = xstrdup(rcs_suffixes);
1.10 ray 218: for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
1.1 xsa 219: char fpath[MAXPATHLEN];
220:
221: if ((p = strrchr(rcspath, ',')) != NULL) {
222: if (!strcmp(p, ext)) {
1.6 joris 223: if ((fd = open(rcspath, O_RDONLY)) == -1)
224: continue;
225:
226: if (fstat(fd, &sb) == -1)
227: err(1, "%s", rcspath);
228:
229: if (strlcpy(out, rcspath, len) >= len)
230: errx(1, "rcs_choosefile; truncation");
231:
1.33 joris 232: xfree(suffixes);
1.6 joris 233: return (fd);
1.1 xsa 234: }
235:
236: continue;
237: }
238:
239: /* Construct RCS file path. */
240: if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
241: strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
1.6 joris 242: errx(1, "rcs_choosefile: truncation");
1.1 xsa 243:
244: /* Don't use `filename' as RCS file. */
245: if (strcmp(fpath, filename) == 0)
246: continue;
247:
1.6 joris 248: if ((fd = open(fpath, O_RDONLY)) == -1)
249: continue;
250:
251: if (fstat(fd, &sb) == -1)
252: err(1, "%s", fpath);
253:
254: if (strlcpy(out, fpath, len) >= len)
255: errx(1, "rcs_choosefile: truncation");
256:
1.33 joris 257: xfree(suffixes);
1.6 joris 258: return (fd);
1.1 xsa 259: }
260:
261: /*
262: * `suffixes' should now be NUL separated, so the first
263: * extension can be read just by reading `suffixes'.
264: */
1.11 ray 265: if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath))
1.6 joris 266: errx(1, "rcs_choosefile: truncation");
1.1 xsa 267:
268: xfree(suffixes);
1.6 joris 269:
1.8 ray 270: if (strlcpy(out, rcspath, len) >= len)
271: errx(1, "rcs_choosefile: truncation");
1.22 niallo 272:
273: fd = open(rcspath, O_RDONLY);
1.1 xsa 274:
1.6 joris 275: return (fd);
1.1 xsa 276: }
277:
278: /*
279: * Set <str> to <new_str>. Print warning if <str> is redefined.
280: */
281: void
282: rcs_setrevstr(char **str, char *new_str)
283: {
284: if (new_str == NULL)
285: return;
286: if (*str != NULL)
287: warnx("redefinition of revision number");
288: *str = new_str;
289: }
290:
291: /*
292: * Set <str1> or <str2> to <new_str>, depending on which is not set.
293: * If both are set, error out.
294: */
295: void
296: rcs_setrevstr2(char **str1, char **str2, char *new_str)
297: {
298: if (new_str == NULL)
299: return;
300: if (*str1 == NULL)
301: *str1 = new_str;
302: else if (*str2 == NULL)
303: *str2 = new_str;
304: else
1.3 xsa 305: errx(1, "too many revision numbers");
1.1 xsa 306: }
307:
308: /*
309: * Get revision from file. The revision can be specified as a symbol or
310: * a revision number.
311: */
312: RCSNUM *
313: rcs_getrevnum(const char *rev_str, RCSFILE *file)
314: {
315: RCSNUM *rev;
316:
317: /* Search for symbol. */
318: rev = rcs_sym_getrev(file, rev_str);
319:
320: /* Search for revision number. */
321: if (rev == NULL)
322: rev = rcsnum_parse(rev_str);
323:
324: return (rev);
325: }
326:
327: /*
328: * Prompt for and store user's input in an allocated string.
329: *
330: * Returns the string's pointer.
331: */
332: char *
333: rcs_prompt(const char *prompt)
334: {
335: BUF *bp;
336: size_t len;
337: char *buf;
338:
1.35 ray 339: bp = buf_alloc(0);
1.1 xsa 340: if (isatty(STDIN_FILENO))
341: (void)fprintf(stderr, "%s", prompt);
342: if (isatty(STDIN_FILENO))
343: (void)fprintf(stderr, ">> ");
1.25 ray 344: clearerr(stdin);
1.1 xsa 345: while ((buf = fgetln(stdin, &len)) != NULL) {
346: /* The last line may not be EOL terminated. */
347: if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
348: break;
349: else
1.34 ray 350: buf_append(bp, buf, len);
1.1 xsa 351:
352: if (isatty(STDIN_FILENO))
353: (void)fprintf(stderr, ">> ");
354: }
1.34 ray 355: buf_putc(bp, '\0');
1.1 xsa 356:
1.34 ray 357: return (buf_release(bp));
1.1 xsa 358: }
359:
360: u_int
1.14 ray 361: rcs_rev_select(RCSFILE *file, const char *range)
1.1 xsa 362: {
363: int i;
364: u_int nrev;
365: char *ep;
366: char *lstr, *rstr;
367: struct rcs_delta *rdp;
1.5 joris 368: struct rcs_argvector *revargv, *revrange;
1.1 xsa 369: RCSNUM lnum, rnum;
370:
371: nrev = 0;
372: (void)memset(&lnum, 0, sizeof(lnum));
373: (void)memset(&rnum, 0, sizeof(rnum));
374:
375: if (range == NULL) {
376: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
377: if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
378: rdp->rd_flags |= RCS_RD_SELECT;
379: return (1);
380: }
381: return (0);
382: }
383:
1.5 joris 384: revargv = rcs_strsplit(range, ",");
1.1 xsa 385: for (i = 0; revargv->argv[i] != NULL; i++) {
1.5 joris 386: revrange = rcs_strsplit(revargv->argv[i], ":");
1.1 xsa 387: if (revrange->argv[0] == NULL)
388: /* should not happen */
1.3 xsa 389: errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1 xsa 390: else if (revrange->argv[1] == NULL)
391: lstr = rstr = revrange->argv[0];
392: else {
393: if (revrange->argv[2] != NULL)
1.3 xsa 394: errx(1, "invalid revision range: %s",
395: revargv->argv[i]);
1.1 xsa 396: lstr = revrange->argv[0];
397: rstr = revrange->argv[1];
398: if (strcmp(lstr, "") == 0)
399: lstr = NULL;
400: if (strcmp(rstr, "") == 0)
401: rstr = NULL;
402: }
403:
404: if (lstr == NULL)
405: lstr = RCS_HEAD_INIT;
406: if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3 xsa 407: errx(1, "invalid revision: %s", lstr);
1.1 xsa 408:
409: if (rstr != NULL) {
410: if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3 xsa 411: errx(1, "invalid revision: %s", rstr);
1.1 xsa 412: } else
413: rcsnum_cpy(file->rf_head, &rnum, 0);
414:
1.5 joris 415: rcs_argv_destroy(revrange);
1.1 xsa 416:
417: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
418: if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
419: rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
420: !(rdp->rd_flags & RCS_RD_SELECT)) {
421: rdp->rd_flags |= RCS_RD_SELECT;
422: nrev++;
423: }
424: }
1.5 joris 425: rcs_argv_destroy(revargv);
1.1 xsa 426:
427: if (lnum.rn_id != NULL)
428: xfree(lnum.rn_id);
429: if (rnum.rn_id != NULL)
430: xfree(rnum.rn_id);
431:
432: return (nrev);
1.2 ray 433: }
434:
435: /*
436: * Load description from <in> to <file>.
437: * If <in> starts with a `-', <in> is taken as the description.
438: * Otherwise <in> is the name of the file containing the description.
439: * If <in> is NULL, the description is read from stdin.
1.18 ray 440: * Returns 0 on success, -1 on failure, setting errno.
1.2 ray 441: */
1.18 ray 442: int
1.2 ray 443: rcs_set_description(RCSFILE *file, const char *in)
444: {
445: BUF *bp;
446: char *content;
447: const char *prompt =
448: "enter description, terminated with single '.' or end of file:\n"
449: "NOTE: This is NOT the log message!\n";
450:
451: /* Description is in file <in>. */
452: if (in != NULL && *in != '-') {
1.35 ray 453: if ((bp = buf_load(in)) == NULL)
1.18 ray 454: return (-1);
1.34 ray 455: buf_putc(bp, '\0');
456: content = buf_release(bp);
1.2 ray 457: /* Description is in <in>. */
458: } else if (in != NULL)
459: /* Skip leading `-'. */
460: content = xstrdup(in + 1);
461: /* Get description from stdin. */
462: else
463: content = rcs_prompt(prompt);
464:
465: rcs_desc_set(file, content);
466: xfree(content);
1.18 ray 467: return (0);
1.7 xsa 468: }
469:
470: /*
471: * Split the contents of a file into a list of lines.
472: */
473: struct rcs_lines *
1.27 xsa 474: rcs_splitlines(u_char *data, size_t len)
1.7 xsa 475: {
1.15 niallo 476: u_char *c, *p;
1.7 xsa 477: struct rcs_lines *lines;
478: struct rcs_line *lp;
1.24 niallo 479: size_t i, tlen;
1.7 xsa 480:
481: lines = xmalloc(sizeof(*lines));
1.24 niallo 482: memset(lines, 0, sizeof(*lines));
1.7 xsa 483: TAILQ_INIT(&(lines->l_lines));
484:
485: lp = xmalloc(sizeof(*lp));
1.24 niallo 486: memset(lp, 0, sizeof(*lp));
1.7 xsa 487: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
488:
489:
1.24 niallo 490: p = c = data;
491: for (i = 0; i < len; i++) {
492: if (*p == '\n' || (i == len - 1)) {
1.26 niallo 493: tlen = p - c + 1;
1.15 niallo 494: lp = xmalloc(sizeof(*lp));
1.24 niallo 495: lp->l_line = c;
496: lp->l_len = tlen;
1.15 niallo 497: lp->l_lineno = ++(lines->l_nblines);
498: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
499: c = p + 1;
500: }
501: p++;
1.7 xsa 502: }
503:
504: return (lines);
505: }
506:
507: void
508: rcs_freelines(struct rcs_lines *lines)
509: {
510: struct rcs_line *lp;
511:
512: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
513: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
514: xfree(lp);
515: }
516:
517: xfree(lines);
518: }
519:
520: BUF *
1.27 xsa 521: rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
1.7 xsa 522: int (*p)(struct rcs_lines *, struct rcs_lines *))
523: {
524: struct rcs_lines *dlines, *plines;
525: struct rcs_line *lp;
526: BUF *res;
527:
1.24 niallo 528: dlines = rcs_splitlines(data, dlen);
529: plines = rcs_splitlines(patch, plen);
1.7 xsa 530:
531: if (p(dlines, plines) < 0) {
532: rcs_freelines(dlines);
533: rcs_freelines(plines);
534: return (NULL);
535: }
536:
1.35 ray 537: res = buf_alloc(1024);
1.7 xsa 538: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.24 niallo 539: if (lp->l_line == NULL)
540: continue;
1.34 ray 541: buf_append(res, lp->l_line, lp->l_len);
1.7 xsa 542: }
543:
544: rcs_freelines(dlines);
545: rcs_freelines(plines);
546: return (res);
547: }
548:
549: /*
550: * rcs_yesno()
551: *
1.23 millert 552: * Read a char from standard input, returns defc if the
553: * user enters an equivalent to defc, else whatever char
554: * was entered. Converts input to lower case.
1.7 xsa 555: */
556: int
1.23 millert 557: rcs_yesno(int defc)
1.7 xsa 558: {
559: int c, ret;
560:
561: fflush(stderr);
562: fflush(stdout);
563:
1.25 ray 564: clearerr(stdin);
1.23 millert 565: if (isalpha(c = getchar()))
566: c = tolower(c);
567: if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
568: ret = defc;
1.7 xsa 569: else
1.23 millert 570: ret = c;
571:
572: while (c != EOF && c != '\n')
573: c = getchar();
1.7 xsa 574:
575: return (ret);
576: }
577:
578: /*
579: * rcs_strsplit()
580: *
581: * Split a string <str> of <sep>-separated values and allocate
582: * an argument vector for the values found.
583: */
584: struct rcs_argvector *
1.14 ray 585: rcs_strsplit(const char *str, const char *sep)
1.7 xsa 586: {
587: struct rcs_argvector *av;
588: size_t i = 0;
589: char *cp, *p;
590:
591: cp = xstrdup(str);
592: av = xmalloc(sizeof(*av));
593: av->str = cp;
1.31 ray 594: av->argv = xmalloc(sizeof(*(av->argv)));
1.7 xsa 595:
596: while ((p = strsep(&cp, sep)) != NULL) {
597: av->argv[i++] = p;
1.30 ray 598: av->argv = xrealloc(av->argv,
1.7 xsa 599: i + 1, sizeof(*(av->argv)));
600: }
601: av->argv[i] = NULL;
602:
603: return (av);
604: }
605:
606: /*
607: * rcs_argv_destroy()
608: *
609: * Free an argument vector previously allocated by rcs_strsplit().
610: */
611: void
612: rcs_argv_destroy(struct rcs_argvector *av)
613: {
614: xfree(av->str);
615: xfree(av->argv);
616: xfree(av);
1.28 otto 617: }
618:
619: /*
620: * Strip suffix from filename.
621: */
622: void
623: rcs_strip_suffix(char *filename)
624: {
625: char *p, *suffixes, *next, *ext;
626:
627: if ((p = strrchr(filename, ',')) != NULL) {
628: suffixes = xstrdup(rcs_suffixes);
629: for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
630: if (!strcmp(p, ext)) {
631: *p = '\0';
632: break;
633: }
634: }
635: xfree(suffixes);
636: }
1.1 xsa 637: }