Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.44
1.44 ! nicm 1: /* $OpenBSD: rcsutil.c,v 1.43 2015/01/16 06:40:11 deraadt 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>
1.38 chl 31: #include <sys/time.h>
1.29 xsa 32:
33: #include <ctype.h>
34: #include <err.h>
35: #include <fcntl.h>
36: #include <stdio.h>
1.44 ! nicm 37: #include <stdlib.h>
1.29 xsa 38: #include <string.h>
39: #include <unistd.h>
1.1 xsa 40:
41: #include "rcsprog.h"
42:
43: /*
44: * rcs_get_mtime()
45: *
46: * Get <filename> last modified time.
47: * Returns last modified time on success, or -1 on failure.
48: */
49: time_t
1.6 joris 50: rcs_get_mtime(RCSFILE *file)
1.1 xsa 51: {
52: struct stat st;
53: time_t mtime;
54:
1.37 tobias 55: if (file->rf_file == NULL)
56: return (-1);
57:
58: if (fstat(fileno(file->rf_file), &st) == -1) {
1.6 joris 59: warn("%s", file->rf_path);
1.1 xsa 60: return (-1);
61: }
1.6 joris 62:
1.39 deraadt 63: mtime = st.st_mtimespec.tv_sec;
1.1 xsa 64:
65: return (mtime);
66: }
67:
68: /*
69: * rcs_set_mtime()
70: *
71: * Set <filename> last modified time to <mtime> if it's not set to -1.
72: */
73: void
1.6 joris 74: rcs_set_mtime(RCSFILE *file, time_t mtime)
1.1 xsa 75: {
76: static struct timeval tv[2];
77:
1.37 tobias 78: if (file->rf_file == NULL || mtime == -1)
1.1 xsa 79: return;
80:
81: tv[0].tv_sec = mtime;
82: tv[1].tv_sec = tv[0].tv_sec;
83:
1.37 tobias 84: if (futimes(fileno(file->rf_file), tv) == -1)
1.3 xsa 85: err(1, "utimes");
1.1 xsa 86: }
87:
88: int
89: rcs_getopt(int argc, char **argv, const char *optstr)
90: {
91: char *a;
92: const char *c;
93: static int i = 1;
94: int opt, hasargument, ret;
95:
96: hasargument = 0;
97: rcs_optarg = NULL;
98:
99: if (i >= argc)
100: return (-1);
101:
102: a = argv[i++];
103: if (*a++ != '-')
104: return (-1);
105:
106: ret = 0;
107: opt = *a;
108: for (c = optstr; *c != '\0'; c++) {
109: if (*c == opt) {
110: a++;
111: ret = opt;
112:
113: if (*(c + 1) == ':') {
114: if (*(c + 2) == ':') {
115: if (*a != '\0')
116: hasargument = 1;
117: } else {
118: if (*a != '\0') {
119: hasargument = 1;
120: } else {
121: ret = 1;
122: break;
123: }
124: }
125: }
126:
127: if (hasargument == 1)
128: rcs_optarg = a;
129:
130: if (ret == opt)
131: rcs_optind++;
132: break;
133: }
134: }
135:
136: if (ret == 0)
137: warnx("unknown option -%c", opt);
138: else if (ret == 1)
139: warnx("missing argument for option -%c", opt);
140:
141: return (ret);
142: }
143:
144: /*
145: * rcs_choosefile()
146: *
147: * Given a relative filename, decide where the corresponding RCS file
148: * should be. Tries each extension until a file is found. If no file
149: * was found, returns a path with the first extension.
150: *
1.11 ray 151: * Opens and returns file descriptor to RCS file.
1.1 xsa 152: */
1.6 joris 153: int
154: rcs_choosefile(const char *filename, char *out, size_t len)
1.1 xsa 155: {
1.6 joris 156: int fd;
1.1 xsa 157: struct stat sb;
1.43 deraadt 158: char *p, *ext, name[PATH_MAX], *next, *ptr, rcsdir[PATH_MAX],
159: *suffixes, rcspath[PATH_MAX];
1.6 joris 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.43 deraadt 219: char fpath[PATH_MAX];
1.1 xsa 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.44 ! nicm 232: free(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.44 ! nicm 257: free(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:
1.44 ! nicm 268: free(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:
1.44 ! nicm 427: free(lnum.rn_id);
! 428: free(rnum.rn_id);
1.1 xsa 429:
430: return (nrev);
1.2 ray 431: }
432:
433: /*
434: * Load description from <in> to <file>.
435: * If <in> starts with a `-', <in> is taken as the description.
436: * Otherwise <in> is the name of the file containing the description.
437: * If <in> is NULL, the description is read from stdin.
1.18 ray 438: * Returns 0 on success, -1 on failure, setting errno.
1.2 ray 439: */
1.18 ray 440: int
1.2 ray 441: rcs_set_description(RCSFILE *file, const char *in)
442: {
443: BUF *bp;
444: char *content;
445: const char *prompt =
446: "enter description, terminated with single '.' or end of file:\n"
447: "NOTE: This is NOT the log message!\n";
448:
449: /* Description is in file <in>. */
450: if (in != NULL && *in != '-') {
1.35 ray 451: if ((bp = buf_load(in)) == NULL)
1.18 ray 452: return (-1);
1.34 ray 453: buf_putc(bp, '\0');
454: content = buf_release(bp);
1.2 ray 455: /* Description is in <in>. */
456: } else if (in != NULL)
457: /* Skip leading `-'. */
458: content = xstrdup(in + 1);
459: /* Get description from stdin. */
460: else
461: content = rcs_prompt(prompt);
462:
463: rcs_desc_set(file, content);
1.44 ! nicm 464: free(content);
1.18 ray 465: return (0);
1.7 xsa 466: }
467:
468: /*
469: * Split the contents of a file into a list of lines.
470: */
471: struct rcs_lines *
1.27 xsa 472: rcs_splitlines(u_char *data, size_t len)
1.7 xsa 473: {
1.15 niallo 474: u_char *c, *p;
1.7 xsa 475: struct rcs_lines *lines;
476: struct rcs_line *lp;
1.24 niallo 477: size_t i, tlen;
1.7 xsa 478:
1.40 tedu 479: lines = xcalloc(1, sizeof(*lines));
1.7 xsa 480: TAILQ_INIT(&(lines->l_lines));
481:
1.40 tedu 482: lp = xcalloc(1, sizeof(*lp));
1.7 xsa 483: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
484:
485:
1.24 niallo 486: p = c = data;
487: for (i = 0; i < len; i++) {
488: if (*p == '\n' || (i == len - 1)) {
1.26 niallo 489: tlen = p - c + 1;
1.15 niallo 490: lp = xmalloc(sizeof(*lp));
1.24 niallo 491: lp->l_line = c;
492: lp->l_len = tlen;
1.15 niallo 493: lp->l_lineno = ++(lines->l_nblines);
494: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
495: c = p + 1;
496: }
497: p++;
1.7 xsa 498: }
499:
500: return (lines);
501: }
502:
503: void
504: rcs_freelines(struct rcs_lines *lines)
505: {
506: struct rcs_line *lp;
507:
508: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
509: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.44 ! nicm 510: free(lp);
1.7 xsa 511: }
512:
1.44 ! nicm 513: free(lines);
1.7 xsa 514: }
515:
516: BUF *
1.27 xsa 517: rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
1.7 xsa 518: int (*p)(struct rcs_lines *, struct rcs_lines *))
519: {
520: struct rcs_lines *dlines, *plines;
521: struct rcs_line *lp;
522: BUF *res;
523:
1.24 niallo 524: dlines = rcs_splitlines(data, dlen);
525: plines = rcs_splitlines(patch, plen);
1.7 xsa 526:
527: if (p(dlines, plines) < 0) {
528: rcs_freelines(dlines);
529: rcs_freelines(plines);
530: return (NULL);
531: }
532:
1.35 ray 533: res = buf_alloc(1024);
1.7 xsa 534: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.24 niallo 535: if (lp->l_line == NULL)
536: continue;
1.34 ray 537: buf_append(res, lp->l_line, lp->l_len);
1.7 xsa 538: }
539:
540: rcs_freelines(dlines);
541: rcs_freelines(plines);
542: return (res);
543: }
544:
545: /*
546: * rcs_yesno()
547: *
1.23 millert 548: * Read a char from standard input, returns defc if the
549: * user enters an equivalent to defc, else whatever char
550: * was entered. Converts input to lower case.
1.7 xsa 551: */
552: int
1.23 millert 553: rcs_yesno(int defc)
1.7 xsa 554: {
555: int c, ret;
556:
557: fflush(stderr);
558: fflush(stdout);
559:
1.25 ray 560: clearerr(stdin);
1.23 millert 561: if (isalpha(c = getchar()))
562: c = tolower(c);
563: if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
564: ret = defc;
1.7 xsa 565: else
1.23 millert 566: ret = c;
567:
568: while (c != EOF && c != '\n')
569: c = getchar();
1.7 xsa 570:
571: return (ret);
572: }
573:
574: /*
575: * rcs_strsplit()
576: *
577: * Split a string <str> of <sep>-separated values and allocate
578: * an argument vector for the values found.
579: */
580: struct rcs_argvector *
1.14 ray 581: rcs_strsplit(const char *str, const char *sep)
1.7 xsa 582: {
583: struct rcs_argvector *av;
584: size_t i = 0;
585: char *cp, *p;
586:
587: cp = xstrdup(str);
588: av = xmalloc(sizeof(*av));
589: av->str = cp;
1.31 ray 590: av->argv = xmalloc(sizeof(*(av->argv)));
1.7 xsa 591:
592: while ((p = strsep(&cp, sep)) != NULL) {
593: av->argv[i++] = p;
1.42 deraadt 594: av->argv = xreallocarray(av->argv,
1.7 xsa 595: i + 1, sizeof(*(av->argv)));
596: }
597: av->argv[i] = NULL;
598:
599: return (av);
600: }
601:
602: /*
603: * rcs_argv_destroy()
604: *
605: * Free an argument vector previously allocated by rcs_strsplit().
606: */
607: void
608: rcs_argv_destroy(struct rcs_argvector *av)
609: {
1.44 ! nicm 610: free(av->str);
! 611: free(av->argv);
! 612: free(av);
1.28 otto 613: }
614:
615: /*
616: * Strip suffix from filename.
617: */
618: void
619: rcs_strip_suffix(char *filename)
620: {
621: char *p, *suffixes, *next, *ext;
622:
623: if ((p = strrchr(filename, ',')) != NULL) {
624: suffixes = xstrdup(rcs_suffixes);
625: for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
626: if (!strcmp(p, ext)) {
627: *p = '\0';
628: break;
629: }
630: }
1.44 ! nicm 631: free(suffixes);
1.28 otto 632: }
1.1 xsa 633: }