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