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