Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.17
1.17 ! ray 1: /* $OpenBSD: rcsutil.c,v 1.16 2006/07/04 22:14:56 niallo 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.13 ray 46: if (fstat(file->rf_fd, &st) == -1) {
1.6 joris 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.13 ray 72: if (futimes(file->rf_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.1 xsa 265:
1.6 joris 266: return (fd);
1.1 xsa 267: }
268:
269: /*
270: * Allocate an RCSNUM and store in <rev>.
271: */
272: void
273: rcs_set_rev(const char *str, RCSNUM **rev)
274: {
275: if (str == NULL || (*rev = rcsnum_parse(str)) == NULL)
1.4 xsa 276: errx(1, "bad revision number `%s'", str);
1.1 xsa 277: }
278:
279: /*
280: * Set <str> to <new_str>. Print warning if <str> is redefined.
281: */
282: void
283: rcs_setrevstr(char **str, char *new_str)
284: {
285: if (new_str == NULL)
286: return;
287: if (*str != NULL)
288: warnx("redefinition of revision number");
289: *str = new_str;
290: }
291:
292: /*
293: * Set <str1> or <str2> to <new_str>, depending on which is not set.
294: * If both are set, error out.
295: */
296: void
297: rcs_setrevstr2(char **str1, char **str2, char *new_str)
298: {
299: if (new_str == NULL)
300: return;
301: if (*str1 == NULL)
302: *str1 = new_str;
303: else if (*str2 == NULL)
304: *str2 = new_str;
305: else
1.3 xsa 306: errx(1, "too many revision numbers");
1.1 xsa 307: }
308:
309: /*
310: * Get revision from file. The revision can be specified as a symbol or
311: * a revision number.
312: */
313: RCSNUM *
314: rcs_getrevnum(const char *rev_str, RCSFILE *file)
315: {
316: RCSNUM *rev;
317:
318: /* Search for symbol. */
319: rev = rcs_sym_getrev(file, rev_str);
320:
321: /* Search for revision number. */
322: if (rev == NULL)
323: rev = rcsnum_parse(rev_str);
324:
325: return (rev);
326: }
327:
328: /*
329: * Prompt for and store user's input in an allocated string.
330: *
331: * Returns the string's pointer.
332: */
333: char *
334: rcs_prompt(const char *prompt)
335: {
336: BUF *bp;
337: size_t len;
338: char *buf;
339:
1.5 joris 340: bp = rcs_buf_alloc(0, BUF_AUTOEXT);
1.1 xsa 341: if (isatty(STDIN_FILENO))
342: (void)fprintf(stderr, "%s", prompt);
343: if (isatty(STDIN_FILENO))
344: (void)fprintf(stderr, ">> ");
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.5 joris 350: rcs_buf_append(bp, buf, len);
1.1 xsa 351:
352: if (isatty(STDIN_FILENO))
353: (void)fprintf(stderr, ">> ");
354: }
1.5 joris 355: rcs_buf_putc(bp, '\0');
1.1 xsa 356:
1.5 joris 357: return (rcs_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.
440: */
441: void
442: rcs_set_description(RCSFILE *file, const char *in)
443: {
444: BUF *bp;
445: char *content;
446: const char *prompt =
447: "enter description, terminated with single '.' or end of file:\n"
448: "NOTE: This is NOT the log message!\n";
449:
450: /* Description is in file <in>. */
451: if (in != NULL && *in != '-') {
1.5 joris 452: bp = rcs_buf_load(in, BUF_AUTOEXT);
453: rcs_buf_putc(bp, '\0');
454: content = rcs_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);
464: xfree(content);
1.7 xsa 465: }
466:
467: /*
468: * Split the contents of a file into a list of lines.
469: */
470: struct rcs_lines *
1.15 niallo 471: rcs_splitlines(BUF *fcont)
1.7 xsa 472: {
1.15 niallo 473: u_char *c, *p;
1.7 xsa 474: struct rcs_lines *lines;
475: struct rcs_line *lp;
1.15 niallo 476: size_t i, len;
1.7 xsa 477:
1.15 niallo 478: len = rcs_buf_len(fcont);
1.7 xsa 479: lines = xmalloc(sizeof(*lines));
480: TAILQ_INIT(&(lines->l_lines));
481: lines->l_nblines = 0;
1.15 niallo 482: lines->l_data = rcs_buf_get(fcont);
1.7 xsa 483:
484: lp = xmalloc(sizeof(*lp));
485: lp->l_line = NULL;
486: lp->l_lineno = 0;
487: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
488:
489:
1.15 niallo 490: p = c = lines->l_data;
491: for (i = 0; i < rcs_buf_len(fcont); i++) {
492: if (*p == '\n') {
493: len = p - c;
494: lp = xmalloc(sizeof(*lp));
495: lp->l_line = xmalloc(len + 1);
496: memcpy(lp->l_line, c, len);
497: lp->l_line[len] = '\0';
498: lp->l_lineno = ++(lines->l_nblines);
499: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
500: c = p + 1;
501: }
502: p++;
1.7 xsa 503: }
504:
505: return (lines);
506: }
507:
508: void
509: rcs_freelines(struct rcs_lines *lines)
510: {
511: struct rcs_line *lp;
512:
513: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
514: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.16 niallo 515: if (lp->l_line != NULL)
516: xfree(lp->l_line);
1.7 xsa 517: xfree(lp);
518: }
519:
520: xfree(lines->l_data);
521: xfree(lines);
522: }
523:
524: BUF *
1.15 niallo 525: rcs_patchfile(BUF *data, BUF *patch,
1.7 xsa 526: int (*p)(struct rcs_lines *, struct rcs_lines *))
527: {
528: struct rcs_lines *dlines, *plines;
529: struct rcs_line *lp;
530: size_t len;
531: int lineno;
532: BUF *res;
533:
1.15 niallo 534: len = rcs_buf_len(data);
1.7 xsa 535:
1.17 ! ray 536: dlines = rcs_splitlines(data);
! 537: plines = rcs_splitlines(patch);
1.7 xsa 538:
539: if (p(dlines, plines) < 0) {
540: rcs_freelines(dlines);
541: rcs_freelines(plines);
542: return (NULL);
543: }
544:
545: lineno = 0;
546: res = rcs_buf_alloc(len, BUF_AUTOEXT);
547: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
548: if (lineno != 0)
549: rcs_buf_fappend(res, "%s\n", lp->l_line);
550: lineno++;
551: }
552:
553: rcs_freelines(dlines);
554: rcs_freelines(plines);
555: return (res);
556: }
557:
558: /*
559: * rcs_yesno()
560: *
561: * Read from standart input for `y' or `Y' character.
562: * Returns 0 on success, or -1 on failure.
563: */
564: int
565: rcs_yesno(void)
566: {
567: int c, ret;
568:
569: ret = 0;
570:
571: fflush(stderr);
572: fflush(stdout);
573:
574: if ((c = getchar()) != 'y' && c != 'Y')
575: ret = -1;
576: else
577: while (c != EOF && c != '\n')
578: c = getchar();
579:
580: return (ret);
581: }
582:
583: /*
584: * rcs_strsplit()
585: *
586: * Split a string <str> of <sep>-separated values and allocate
587: * an argument vector for the values found.
588: */
589: struct rcs_argvector *
1.14 ray 590: rcs_strsplit(const char *str, const char *sep)
1.7 xsa 591: {
592: struct rcs_argvector *av;
593: size_t i = 0;
594: char **nargv;
595: char *cp, *p;
596:
597: cp = xstrdup(str);
598: av = xmalloc(sizeof(*av));
599: av->str = cp;
600: av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
601:
602: while ((p = strsep(&cp, sep)) != NULL) {
603: av->argv[i++] = p;
604: nargv = xrealloc(av->argv,
605: i + 1, sizeof(*(av->argv)));
606: av->argv = nargv;
607: }
608: av->argv[i] = NULL;
609:
610: return (av);
611: }
612:
613: /*
614: * rcs_argv_destroy()
615: *
616: * Free an argument vector previously allocated by rcs_strsplit().
617: */
618: void
619: rcs_argv_destroy(struct rcs_argvector *av)
620: {
621: xfree(av->str);
622: xfree(av->argv);
623: xfree(av);
1.1 xsa 624: }