Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.10
1.10 ! ray 1: /* $OpenBSD: rcsutil.c,v 1.9 2006/05/08 21:55:39 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: *
139: * Returns pointer to a char array on success, NULL on failure.
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: */
257: if (strlcat(rcspath, suffixes, sizeof(rcspath)) >=
258: sizeof(rcspath))
1.6 joris 259: errx(1, "rcs_choosefile: truncation");
1.1 xsa 260:
261: xfree(suffixes);
1.6 joris 262:
263: fd = open(rcspath, O_RDONLY);
1.8 ray 264: if (strlcpy(out, rcspath, len) >= len)
265: errx(1, "rcs_choosefile: truncation");
1.6 joris 266:
267: return (fd);
1.1 xsa 268: }
269:
270: /*
271: * Find the name of an RCS file, given a file name `fname'. If an RCS
272: * file is found, the name is copied to the `len' sized buffer `out'.
273: * Returns 0 if RCS file was found, -1 otherwise.
274: */
275: int
276: rcs_statfile(char *fname, char *out, size_t len, int flags)
277: {
1.6 joris 278: int fd;
1.1 xsa 279:
1.6 joris 280: fd = rcs_choosefile(fname, out, len);
281: if (fd == -1 && !(flags & RCS_CREATE)) {
1.1 xsa 282: if (strcmp(__progname, "rcsclean") != 0 &&
283: strcmp(__progname, "ci") != 0)
1.9 ray 284: warnx("%s", out);
1.1 xsa 285: return (-1);
286: }
287:
1.6 joris 288: return (fd);
1.1 xsa 289: }
290:
291: /*
292: * Allocate an RCSNUM and store in <rev>.
293: */
294: void
295: rcs_set_rev(const char *str, RCSNUM **rev)
296: {
297: if (str == NULL || (*rev = rcsnum_parse(str)) == NULL)
1.4 xsa 298: errx(1, "bad revision number `%s'", str);
1.1 xsa 299: }
300:
301: /*
302: * Set <str> to <new_str>. Print warning if <str> is redefined.
303: */
304: void
305: rcs_setrevstr(char **str, char *new_str)
306: {
307: if (new_str == NULL)
308: return;
309: if (*str != NULL)
310: warnx("redefinition of revision number");
311: *str = new_str;
312: }
313:
314: /*
315: * Set <str1> or <str2> to <new_str>, depending on which is not set.
316: * If both are set, error out.
317: */
318: void
319: rcs_setrevstr2(char **str1, char **str2, char *new_str)
320: {
321: if (new_str == NULL)
322: return;
323: if (*str1 == NULL)
324: *str1 = new_str;
325: else if (*str2 == NULL)
326: *str2 = new_str;
327: else
1.3 xsa 328: errx(1, "too many revision numbers");
1.1 xsa 329: }
330:
331: /*
332: * Get revision from file. The revision can be specified as a symbol or
333: * a revision number.
334: */
335: RCSNUM *
336: rcs_getrevnum(const char *rev_str, RCSFILE *file)
337: {
338: RCSNUM *rev;
339:
340: /* Search for symbol. */
341: rev = rcs_sym_getrev(file, rev_str);
342:
343: /* Search for revision number. */
344: if (rev == NULL)
345: rev = rcsnum_parse(rev_str);
346:
347: return (rev);
348: }
349:
350: /*
351: * Prompt for and store user's input in an allocated string.
352: *
353: * Returns the string's pointer.
354: */
355: char *
356: rcs_prompt(const char *prompt)
357: {
358: BUF *bp;
359: size_t len;
360: char *buf;
361:
1.5 joris 362: bp = rcs_buf_alloc(0, BUF_AUTOEXT);
1.1 xsa 363: if (isatty(STDIN_FILENO))
364: (void)fprintf(stderr, "%s", prompt);
365: if (isatty(STDIN_FILENO))
366: (void)fprintf(stderr, ">> ");
367: while ((buf = fgetln(stdin, &len)) != NULL) {
368: /* The last line may not be EOL terminated. */
369: if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
370: break;
371: else
1.5 joris 372: rcs_buf_append(bp, buf, len);
1.1 xsa 373:
374: if (isatty(STDIN_FILENO))
375: (void)fprintf(stderr, ">> ");
376: }
1.5 joris 377: rcs_buf_putc(bp, '\0');
1.1 xsa 378:
1.5 joris 379: return (rcs_buf_release(bp));
1.1 xsa 380: }
381:
382: u_int
383: rcs_rev_select(RCSFILE *file, char *range)
384: {
385: int i;
386: u_int nrev;
387: char *ep;
388: char *lstr, *rstr;
389: struct rcs_delta *rdp;
1.5 joris 390: struct rcs_argvector *revargv, *revrange;
1.1 xsa 391: RCSNUM lnum, rnum;
392:
393: nrev = 0;
394: (void)memset(&lnum, 0, sizeof(lnum));
395: (void)memset(&rnum, 0, sizeof(rnum));
396:
397: if (range == NULL) {
398: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
399: if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
400: rdp->rd_flags |= RCS_RD_SELECT;
401: return (1);
402: }
403: return (0);
404: }
405:
1.5 joris 406: revargv = rcs_strsplit(range, ",");
1.1 xsa 407: for (i = 0; revargv->argv[i] != NULL; i++) {
1.5 joris 408: revrange = rcs_strsplit(revargv->argv[i], ":");
1.1 xsa 409: if (revrange->argv[0] == NULL)
410: /* should not happen */
1.3 xsa 411: errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1 xsa 412: else if (revrange->argv[1] == NULL)
413: lstr = rstr = revrange->argv[0];
414: else {
415: if (revrange->argv[2] != NULL)
1.3 xsa 416: errx(1, "invalid revision range: %s",
417: revargv->argv[i]);
1.1 xsa 418: lstr = revrange->argv[0];
419: rstr = revrange->argv[1];
420: if (strcmp(lstr, "") == 0)
421: lstr = NULL;
422: if (strcmp(rstr, "") == 0)
423: rstr = NULL;
424: }
425:
426: if (lstr == NULL)
427: lstr = RCS_HEAD_INIT;
428: if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3 xsa 429: errx(1, "invalid revision: %s", lstr);
1.1 xsa 430:
431: if (rstr != NULL) {
432: if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3 xsa 433: errx(1, "invalid revision: %s", rstr);
1.1 xsa 434: } else
435: rcsnum_cpy(file->rf_head, &rnum, 0);
436:
1.5 joris 437: rcs_argv_destroy(revrange);
1.1 xsa 438:
439: TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
440: if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
441: rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
442: !(rdp->rd_flags & RCS_RD_SELECT)) {
443: rdp->rd_flags |= RCS_RD_SELECT;
444: nrev++;
445: }
446: }
1.5 joris 447: rcs_argv_destroy(revargv);
1.1 xsa 448:
449: if (lnum.rn_id != NULL)
450: xfree(lnum.rn_id);
451: if (rnum.rn_id != NULL)
452: xfree(rnum.rn_id);
453:
454: return (nrev);
1.2 ray 455: }
456:
457: /*
458: * Load description from <in> to <file>.
459: * If <in> starts with a `-', <in> is taken as the description.
460: * Otherwise <in> is the name of the file containing the description.
461: * If <in> is NULL, the description is read from stdin.
462: */
463: void
464: rcs_set_description(RCSFILE *file, const char *in)
465: {
466: BUF *bp;
467: char *content;
468: const char *prompt =
469: "enter description, terminated with single '.' or end of file:\n"
470: "NOTE: This is NOT the log message!\n";
471:
472: /* Description is in file <in>. */
473: if (in != NULL && *in != '-') {
1.5 joris 474: bp = rcs_buf_load(in, BUF_AUTOEXT);
475: rcs_buf_putc(bp, '\0');
476: content = rcs_buf_release(bp);
1.2 ray 477: /* Description is in <in>. */
478: } else if (in != NULL)
479: /* Skip leading `-'. */
480: content = xstrdup(in + 1);
481: /* Get description from stdin. */
482: else
483: content = rcs_prompt(prompt);
484:
485: rcs_desc_set(file, content);
486: xfree(content);
1.7 xsa 487: }
488:
489: /*
490: * Split the contents of a file into a list of lines.
491: */
492: struct rcs_lines *
493: rcs_splitlines(const char *fcont)
494: {
495: char *dcp;
496: struct rcs_lines *lines;
497: struct rcs_line *lp;
498:
499: lines = xmalloc(sizeof(*lines));
500: TAILQ_INIT(&(lines->l_lines));
501: lines->l_nblines = 0;
502: lines->l_data = xstrdup(fcont);
503:
504: lp = xmalloc(sizeof(*lp));
505: lp->l_line = NULL;
506: lp->l_lineno = 0;
507: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
508:
509: for (dcp = lines->l_data; *dcp != '\0';) {
510: lp = xmalloc(sizeof(*lp));
511: lp->l_line = dcp;
512: lp->l_lineno = ++(lines->l_nblines);
513: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
514:
515: dcp = strchr(dcp, '\n');
516: if (dcp == NULL)
517: break;
518: *(dcp++) = '\0';
519: }
520:
521: return (lines);
522: }
523:
524: void
525: rcs_freelines(struct rcs_lines *lines)
526: {
527: struct rcs_line *lp;
528:
529: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
530: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
531: xfree(lp);
532: }
533:
534: xfree(lines->l_data);
535: xfree(lines);
536: }
537:
538: BUF *
539: rcs_patchfile(const char *data, const char *patch,
540: int (*p)(struct rcs_lines *, struct rcs_lines *))
541: {
542: struct rcs_lines *dlines, *plines;
543: struct rcs_line *lp;
544: size_t len;
545: int lineno;
546: BUF *res;
547:
548: len = strlen(data);
549:
550: if ((dlines = rcs_splitlines(data)) == NULL)
551: return (NULL);
552:
553: if ((plines = rcs_splitlines(patch)) == NULL)
554: return (NULL);
555:
556: if (p(dlines, plines) < 0) {
557: rcs_freelines(dlines);
558: rcs_freelines(plines);
559: return (NULL);
560: }
561:
562: lineno = 0;
563: res = rcs_buf_alloc(len, BUF_AUTOEXT);
564: TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
565: if (lineno != 0)
566: rcs_buf_fappend(res, "%s\n", lp->l_line);
567: lineno++;
568: }
569:
570: rcs_freelines(dlines);
571: rcs_freelines(plines);
572: return (res);
573: }
574:
575: /*
576: * rcs_yesno()
577: *
578: * Read from standart input for `y' or `Y' character.
579: * Returns 0 on success, or -1 on failure.
580: */
581: int
582: rcs_yesno(void)
583: {
584: int c, ret;
585:
586: ret = 0;
587:
588: fflush(stderr);
589: fflush(stdout);
590:
591: if ((c = getchar()) != 'y' && c != 'Y')
592: ret = -1;
593: else
594: while (c != EOF && c != '\n')
595: c = getchar();
596:
597: return (ret);
598: }
599:
600: /*
601: * rcs_strsplit()
602: *
603: * Split a string <str> of <sep>-separated values and allocate
604: * an argument vector for the values found.
605: */
606: struct rcs_argvector *
607: rcs_strsplit(char *str, const char *sep)
608: {
609: struct rcs_argvector *av;
610: size_t i = 0;
611: char **nargv;
612: char *cp, *p;
613:
614: cp = xstrdup(str);
615: av = xmalloc(sizeof(*av));
616: av->str = cp;
617: av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
618:
619: while ((p = strsep(&cp, sep)) != NULL) {
620: av->argv[i++] = p;
621: nargv = xrealloc(av->argv,
622: i + 1, sizeof(*(av->argv)));
623: av->argv = nargv;
624: }
625: av->argv[i] = NULL;
626:
627: return (av);
628: }
629:
630: /*
631: * rcs_argv_destroy()
632: *
633: * Free an argument vector previously allocated by rcs_strsplit().
634: */
635: void
636: rcs_argv_destroy(struct rcs_argvector *av)
637: {
638: xfree(av->str);
639: xfree(av->argv);
640: xfree(av);
1.1 xsa 641: }