Annotation of src/usr.bin/rcs/rcs.c, Revision 1.10
1.10 ! ray 1: /* $OpenBSD: rcs.c,v 1.9 2006/05/11 07:34:26 xsa Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include "includes.h"
28:
29: #include "diff.h"
30: #include "rcs.h"
31: #include "rcsprog.h"
1.4 xsa 32: #include "rcsutil.h"
1.1 joris 33: #include "xmalloc.h"
34:
35: #define RCS_BUFSIZE 16384
36: #define RCS_BUFEXTSIZE 8192
37: #define RCS_KWEXP_SIZE 1024
38:
39: /* RCS token types */
40: #define RCS_TOK_ERR -1
41: #define RCS_TOK_EOF 0
42: #define RCS_TOK_NUM 1
43: #define RCS_TOK_ID 2
44: #define RCS_TOK_STRING 3
45: #define RCS_TOK_SCOLON 4
46: #define RCS_TOK_COLON 5
47:
48: #define RCS_TOK_HEAD 8
49: #define RCS_TOK_BRANCH 9
50: #define RCS_TOK_ACCESS 10
51: #define RCS_TOK_SYMBOLS 11
52: #define RCS_TOK_LOCKS 12
53: #define RCS_TOK_COMMENT 13
54: #define RCS_TOK_EXPAND 14
55: #define RCS_TOK_DATE 15
56: #define RCS_TOK_AUTHOR 16
57: #define RCS_TOK_STATE 17
58: #define RCS_TOK_NEXT 18
59: #define RCS_TOK_BRANCHES 19
60: #define RCS_TOK_DESC 20
61: #define RCS_TOK_LOG 21
62: #define RCS_TOK_TEXT 22
63: #define RCS_TOK_STRICT 23
64:
65: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
66:
67: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
68: #define RCS_VOPT 0x02 /* value is optional */
69:
70: /* opaque parse data */
71: struct rcs_pdata {
72: u_int rp_lines;
73:
74: char *rp_buf;
75: size_t rp_blen;
76: char *rp_bufend;
77: size_t rp_tlen;
78:
79: /* pushback token buffer */
80: char rp_ptok[128];
81: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
82:
83: FILE *rp_file;
84: };
85:
86: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
87: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
88:
89: /* invalid characters in RCS symbol names */
90: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
91:
92: /* comment leaders, depending on the file's suffix */
93: static const struct rcs_comment {
94: const char *rc_suffix;
95: const char *rc_cstr;
96: } rcs_comments[] = {
97: { "1", ".\\\" " },
98: { "2", ".\\\" " },
99: { "3", ".\\\" " },
100: { "4", ".\\\" " },
101: { "5", ".\\\" " },
102: { "6", ".\\\" " },
103: { "7", ".\\\" " },
104: { "8", ".\\\" " },
105: { "9", ".\\\" " },
106: { "a", "-- " }, /* Ada */
107: { "ada", "-- " },
108: { "adb", "-- " },
109: { "asm", ";; " }, /* assembler (MS-DOS) */
110: { "ads", "-- " }, /* Ada */
111: { "bat", ":: " }, /* batch (MS-DOS) */
112: { "body", "-- " }, /* Ada */
113: { "c", " * " }, /* C */
114: { "c++", "// " }, /* C++ */
115: { "cc", "// " },
116: { "cpp", "// " },
117: { "cxx", "// " },
118: { "m", "// " }, /* Objective-C */
119: { "cl", ";;; " }, /* Common Lisp */
120: { "cmd", ":: " }, /* command (OS/2) */
121: { "cmf", "c " }, /* CM Fortran */
122: { "csh", "# " }, /* shell */
123: { "e", "# " }, /* efl */
124: { "epsf", "% " }, /* encapsulated postscript */
125: { "epsi", "% " }, /* encapsulated postscript */
126: { "el", "; " }, /* Emacs Lisp */
127: { "f", "c " }, /* Fortran */
128: { "for", "c " },
129: { "h", " * " }, /* C-header */
130: { "hh", "// " }, /* C++ header */
131: { "hpp", "// " },
132: { "hxx", "// " },
133: { "in", "# " }, /* for Makefile.in */
134: { "l", " * " }, /* lex */
135: { "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
136: { "mak", "# " }, /* makefile, e.g. Visual C++ */
137: { "me", ".\\\" " }, /* me-macros t/nroff */
138: { "ml", "; " }, /* mocklisp */
139: { "mm", ".\\\" " }, /* mm-macros t/nroff */
140: { "ms", ".\\\" " }, /* ms-macros t/nroff */
141: { "man", ".\\\" " }, /* man-macros t/nroff */
142: { "p", " * " }, /* pascal */
143: { "pas", " * " },
144: { "pl", "# " }, /* Perl (conflict with Prolog) */
145: { "pm", "# " }, /* Perl module */
146: { "ps", "% " }, /* postscript */
147: { "psw", "% " }, /* postscript wrap */
148: { "pswm", "% " }, /* postscript wrap */
149: { "r", "# " }, /* ratfor */
150: { "rc", " * " }, /* Microsoft Windows resource file */
151: { "red", "% " }, /* psl/rlisp */
152: { "sh", "# " }, /* shell */
153: { "sl", "% " }, /* psl */
154: { "spec", "-- " }, /* Ada */
155: { "tex", "% " }, /* tex */
156: { "y", " * " }, /* yacc */
157: { "ye", " * " }, /* yacc-efl */
158: { "yr", " * " }, /* yacc-ratfor */
159: };
160:
161: struct rcs_kw rcs_expkw[] = {
162: { "Author", RCS_KW_AUTHOR },
163: { "Date", RCS_KW_DATE },
164: { "Header", RCS_KW_HEADER },
165: { "Id", RCS_KW_ID },
166: { "Log", RCS_KW_LOG },
167: { "Name", RCS_KW_NAME },
168: { "RCSfile", RCS_KW_RCSFILE },
169: { "Revision", RCS_KW_REVISION },
170: { "Source", RCS_KW_SOURCE },
171: { "State", RCS_KW_STATE },
172: };
173:
174: #define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
175:
176: static struct rcs_key {
177: char rk_str[16];
178: int rk_id;
179: int rk_val;
180: int rk_flags;
181: } rcs_keys[] = {
182: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
183: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
184: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
185: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
186: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
187: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
188: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
189: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
190: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
191: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
192: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
193: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
194: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
195: { "strict", RCS_TOK_STRICT, 0, 0, },
196: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
197: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
198: };
199:
200: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
201:
202: static const char *rcs_errstrs[] = {
203: "No error",
204: "No such entry",
205: "Duplicate entry found",
206: "Bad RCS number",
207: "Invalid RCS symbol",
208: "Parse error",
209: };
210:
211: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
212:
213: int rcs_errno = RCS_ERR_NOERR;
214: char *timezone_flag = NULL;
215:
216: int rcs_patch_lines(struct rcs_lines *, struct rcs_lines *);
1.9 xsa 217: static int rcs_movefile(char *, char *, mode_t, u_int);
1.1 joris 218: static void rcs_parse_init(RCSFILE *);
219: static int rcs_parse_admin(RCSFILE *);
220: static int rcs_parse_delta(RCSFILE *);
221: static void rcs_parse_deltas(RCSFILE *, RCSNUM *);
222: static int rcs_parse_deltatext(RCSFILE *);
223: static void rcs_parse_deltatexts(RCSFILE *, RCSNUM *);
224: static void rcs_parse_desc(RCSFILE *, RCSNUM *);
225:
226: static int rcs_parse_access(RCSFILE *);
227: static int rcs_parse_symbols(RCSFILE *);
228: static int rcs_parse_locks(RCSFILE *);
229: static int rcs_parse_branches(RCSFILE *, struct rcs_delta *);
230: static void rcs_freedelta(struct rcs_delta *);
231: static void rcs_freepdata(struct rcs_pdata *);
232: static int rcs_gettok(RCSFILE *);
233: static int rcs_pushtok(RCSFILE *, const char *, int);
234: static void rcs_growbuf(RCSFILE *);
235: static void rcs_strprint(const u_char *, size_t, FILE *);
236:
237: static char* rcs_expand_keywords(char *, struct rcs_delta *, char *,
238: size_t, int);
239:
240: RCSFILE *
1.3 joris 241: rcs_open(const char *path, int fd, int flags, ...)
1.1 joris 242: {
1.3 joris 243: int mode;
1.1 joris 244: mode_t fmode;
245: RCSFILE *rfp;
246: va_list vap;
247: struct rcs_delta *rdp;
248: struct rcs_lock *lkr;
249:
250: fmode = S_IRUSR|S_IRGRP|S_IROTH;
251: flags &= 0xffff; /* ditch any internal flags */
252:
1.3 joris 253: if (flags & RCS_CREATE) {
254: va_start(vap, flags);
255: mode = va_arg(vap, int);
256: va_end(vap);
257: fmode = (mode_t)mode;
1.1 joris 258: }
259:
260: rfp = xcalloc(1, sizeof(*rfp));
261:
262: rfp->rf_path = xstrdup(path);
263: rfp->rf_flags = flags | RCS_SLOCK | RCS_SYNCED;
264: rfp->rf_mode = fmode;
1.10 ! ray 265: rfp->rf_fd = fd;
1.1 joris 266:
267: TAILQ_INIT(&(rfp->rf_delta));
268: TAILQ_INIT(&(rfp->rf_access));
269: TAILQ_INIT(&(rfp->rf_symbols));
270: TAILQ_INIT(&(rfp->rf_locks));
271:
272: if (!(rfp->rf_flags & RCS_CREATE))
273: rcs_parse_init(rfp);
274:
275: /* fill in rd_locker */
276: TAILQ_FOREACH(lkr, &(rfp->rf_locks), rl_list) {
277: if ((rdp = rcs_findrev(rfp, lkr->rl_num)) == NULL) {
278: rcs_close(rfp);
279: return (NULL);
280: }
281:
282: rdp->rd_locker = xstrdup(lkr->rl_name);
283: }
284:
285: return (rfp);
286: }
287:
288: /*
289: * rcs_close()
290: *
291: * Close an RCS file handle.
292: */
293: void
294: rcs_close(RCSFILE *rfp)
295: {
296: struct rcs_delta *rdp;
297: struct rcs_access *rap;
298: struct rcs_lock *rlp;
299: struct rcs_sym *rsp;
300:
301: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
302: rcs_write(rfp);
303:
304: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
305: rdp = TAILQ_FIRST(&(rfp->rf_delta));
306: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
307: rcs_freedelta(rdp);
308: }
309:
310: while (!TAILQ_EMPTY(&(rfp->rf_access))) {
311: rap = TAILQ_FIRST(&(rfp->rf_access));
312: TAILQ_REMOVE(&(rfp->rf_access), rap, ra_list);
313: xfree(rap->ra_name);
314: xfree(rap);
315: }
316:
317: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
318: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
319: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
320: rcsnum_free(rsp->rs_num);
321: xfree(rsp->rs_name);
322: xfree(rsp);
323: }
324:
325: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
326: rlp = TAILQ_FIRST(&(rfp->rf_locks));
327: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
328: rcsnum_free(rlp->rl_num);
329: xfree(rlp->rl_name);
330: xfree(rlp);
331: }
332:
333: if (rfp->rf_head != NULL)
334: rcsnum_free(rfp->rf_head);
335: if (rfp->rf_branch != NULL)
336: rcsnum_free(rfp->rf_branch);
337:
338: if (rfp->rf_path != NULL)
339: xfree(rfp->rf_path);
340: if (rfp->rf_comment != NULL)
341: xfree(rfp->rf_comment);
342: if (rfp->rf_expand != NULL)
343: xfree(rfp->rf_expand);
344: if (rfp->rf_desc != NULL)
345: xfree(rfp->rf_desc);
346: if (rfp->rf_pdata != NULL)
347: rcs_freepdata(rfp->rf_pdata);
348: xfree(rfp);
349: }
350:
351: /*
352: * rcs_write()
353: *
354: * Write the contents of the RCS file handle <rfp> to disk in the file whose
355: * path is in <rf_path>.
356: */
1.9 xsa 357: void
1.1 joris 358: rcs_write(RCSFILE *rfp)
359: {
360: FILE *fp;
1.7 xsa 361: char buf[1024], numbuf[64], *fn;
1.1 joris 362: struct rcs_access *ap;
363: struct rcs_sym *symp;
364: struct rcs_branch *brp;
365: struct rcs_delta *rdp;
366: struct rcs_lock *lkp;
367: size_t len;
1.9 xsa 368: int fd, from_fd, to_fd;
1.1 joris 369:
370: from_fd = to_fd = fd = -1;
371:
372: if (rfp->rf_flags & RCS_SYNCED)
1.9 xsa 373: return;
1.1 joris 374:
375: /* Write operations need the whole file parsed */
376: rcs_parse_deltatexts(rfp, NULL);
377:
1.7 xsa 378: (void)xasprintf(&fn, "%s/rcs.XXXXXXXXXX", rcs_tmpdir);
379:
1.1 joris 380: if ((fd = mkstemp(fn)) == -1)
1.2 xsa 381: err(1, "%s", fn);
1.1 joris 382:
383: if ((fp = fdopen(fd, "w+")) == NULL) {
1.5 ray 384: int saved_errno;
385:
386: saved_errno = errno;
1.2 xsa 387: (void)unlink(fn);
1.5 ray 388: errno = saved_errno;
1.2 xsa 389: err(1, "%s", fn);
1.1 joris 390: }
391:
392: if (rfp->rf_head != NULL)
393: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
394: else
395: numbuf[0] = '\0';
396:
397: fprintf(fp, "head\t%s;\n", numbuf);
398:
399: if (rfp->rf_branch != NULL) {
400: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
401: fprintf(fp, "branch\t%s;\n", numbuf);
402: }
403:
404: fputs("access", fp);
405: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
406: fprintf(fp, "\n\t%s", ap->ra_name);
407: }
408: fputs(";\n", fp);
409:
410: fprintf(fp, "symbols");
411: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
412: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
1.5 ray 413: if (strlcpy(buf, symp->rs_name, sizeof(buf)) >= sizeof(buf) ||
414: strlcat(buf, ":", sizeof(buf)) >= sizeof(buf) ||
415: strlcat(buf, numbuf, sizeof(buf)) >= sizeof(buf))
416: errx(1, "rcs_write: string overflow");
1.1 joris 417: fprintf(fp, "\n\t%s", buf);
418: }
419: fprintf(fp, ";\n");
420:
421: fprintf(fp, "locks");
422: TAILQ_FOREACH(lkp, &(rfp->rf_locks), rl_list) {
423: rcsnum_tostr(lkp->rl_num, numbuf, sizeof(numbuf));
424: fprintf(fp, "\n\t%s:%s", lkp->rl_name, numbuf);
425: }
426:
427: fprintf(fp, ";");
428:
429: if (rfp->rf_flags & RCS_SLOCK)
430: fprintf(fp, " strict;");
431: fputc('\n', fp);
432:
433: fputs("comment\t@", fp);
434: if (rfp->rf_comment != NULL) {
435: rcs_strprint((const u_char *)rfp->rf_comment,
436: strlen(rfp->rf_comment), fp);
437: fputs("@;\n", fp);
438: } else
439: fputs("# @;\n", fp);
440:
441: if (rfp->rf_expand != NULL) {
442: fputs("expand @", fp);
443: rcs_strprint((const u_char *)rfp->rf_expand,
444: strlen(rfp->rf_expand), fp);
445: fputs("@;\n", fp);
446: }
447:
448: fputs("\n\n", fp);
449:
450: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
451: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
452: sizeof(numbuf)));
453: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
454: rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
455: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
456: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
457: fprintf(fp, "\tauthor %s;\tstate %s;\n",
458: rdp->rd_author, rdp->rd_state);
459: fputs("branches", fp);
460: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
461: fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
462: sizeof(numbuf)));
463: }
464: fputs(";\n", fp);
465: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
466: numbuf, sizeof(numbuf)));
467: }
468:
469: fputs("\ndesc\n@", fp);
470: if (rfp->rf_desc != NULL && (len = strlen(rfp->rf_desc)) > 0) {
471: rcs_strprint((const u_char *)rfp->rf_desc, len, fp);
472: if (rfp->rf_desc[len-1] != '\n')
473: fputc('\n', fp);
474: }
475: fputs("@\n", fp);
476:
477: /* deltatexts */
478: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
479: fprintf(fp, "\n\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
480: sizeof(numbuf)));
481: fputs("log\n@", fp);
482: if (rdp->rd_log != NULL) {
483: len = strlen(rdp->rd_log);
484: rcs_strprint((const u_char *)rdp->rd_log, len, fp);
485: if (rdp->rd_log[len-1] != '\n')
486: fputc('\n', fp);
487: }
488: fputs("@\ntext\n@", fp);
489: if (rdp->rd_text != NULL) {
490: rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
491:
492: if (rdp->rd_tlen != 0) {
493: if (rdp->rd_text[rdp->rd_tlen-1] != '\n')
494: fputc('\n', fp);
495: }
496: }
497: fputs("@\n", fp);
498: }
1.9 xsa 499: (void)fclose(fp);
1.1 joris 500:
1.9 xsa 501: if (rcs_movefile(fn, rfp->rf_path, rfp->rf_mode, rfp->rf_flags) == -1) {
502: (void)unlink(fn);
503: errx(1, "rcs_movefile failed");
504: }
1.1 joris 505:
1.9 xsa 506: rfp->rf_flags |= RCS_SYNCED;
1.1 joris 507:
1.9 xsa 508: if (fn != NULL)
509: xfree(fn);
510: }
1.1 joris 511:
1.9 xsa 512: /*
513: * rcs_movefile()
514: *
515: * Move a file using rename(2) if possible and copying if not.
516: * Returns 0 on success, -1 on failure.
517: */
518: static int
519: rcs_movefile(char *from, char *to, mode_t perm, u_int to_flags)
520: {
521: FILE *src, *dst;
522: size_t nread, nwritten;
523: char *buf;
524: int ret;
1.1 joris 525:
1.9 xsa 526: ret = -1;
1.1 joris 527:
1.9 xsa 528: if (rename(from, to) == 0) {
529: if (chmod(to, perm) == -1) {
530: warn("%s", to);
531: return (-1);
1.1 joris 532: }
1.9 xsa 533: return (0);
534: } else if (errno != EXDEV) {
535: warn("failed to access temp RCS output file");
536: return (-1);
1.1 joris 537: }
538:
1.9 xsa 539: if ((chmod(to, S_IWUSR) == -1) && !(to_flags & RCS_CREATE)) {
540: warnx("chmod(%s, 0%o) failed", to, S_IWUSR);
541: return (-1);
1.1 joris 542: }
543:
1.9 xsa 544: /* different filesystem, have to copy the file */
545: if ((src = fopen(from, "r")) == NULL) {
546: warn("%s", from);
547: return (-1);
548: }
549: if ((dst = fopen(to, "w")) == NULL) {
550: warn("%s", to);
551: return (-1);
552: }
553: if (fchmod(fileno(dst), perm)) {
554: warn("%s", to);
555: (void)unlink(to);
556: return (-1);
557: }
558:
559: buf = xmalloc(MAXBSIZE);
560: while ((nread = fread(buf, sizeof(char), MAXBSIZE, src)) != 0) {
561: if (ferror(src)) {
562: warnx("failed to read `%s'", from);
563: (void)unlink(to);
564: goto out;
565: }
566: nwritten = fwrite(buf, sizeof(char), nread, dst);
567: if (nwritten != nread) {
568: warnx("failed to write `%s'", to);
569: (void)unlink(to);
570: goto out;
571: }
572: }
1.1 joris 573:
1.7 xsa 574: ret = 0;
1.9 xsa 575:
576: (void)fclose(src);
577: (void)fclose(dst);
578: (void)unlink(from);
579:
1.7 xsa 580: out:
1.9 xsa 581: xfree(buf);
1.7 xsa 582:
583: return (ret);
1.1 joris 584: }
585:
586: /*
587: * rcs_head_get()
588: *
589: * Retrieve the revision number of the head revision for the RCS file <file>.
590: */
591: const RCSNUM *
592: rcs_head_get(RCSFILE *file)
593: {
594: return (file->rf_head);
595: }
596:
597: /*
598: * rcs_head_set()
599: *
600: * Set the revision number of the head revision for the RCS file <file> to
601: * <rev>, which must reference a valid revision within the file.
602: */
603: int
604: rcs_head_set(RCSFILE *file, RCSNUM *rev)
605: {
606: if (rcs_findrev(file, rev) == NULL)
607: return (-1);
608:
609: if (file->rf_head == NULL)
610: file->rf_head = rcsnum_alloc();
611:
612: rcsnum_cpy(rev, file->rf_head, 0);
613: file->rf_flags &= ~RCS_SYNCED;
614: return (0);
615: }
616:
617:
618: /*
619: * rcs_branch_get()
620: *
621: * Retrieve the default branch number for the RCS file <file>.
622: * Returns the number on success. If NULL is returned, then there is no
623: * default branch for this file.
624: */
625: const RCSNUM *
626: rcs_branch_get(RCSFILE *file)
627: {
628: return (file->rf_branch);
629: }
630:
631: /*
632: * rcs_branch_set()
633: *
634: * Set the default branch for the RCS file <file> to <bnum>.
635: * Returns 0 on success, -1 on failure.
636: */
637: int
638: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
639: {
640: if (file->rf_branch == NULL)
641: file->rf_branch = rcsnum_alloc();
642:
643: rcsnum_cpy(bnum, file->rf_branch, 0);
644: file->rf_flags &= ~RCS_SYNCED;
645: return (0);
646: }
647:
648: /*
649: * rcs_access_add()
650: *
651: * Add the login name <login> to the access list for the RCS file <file>.
652: * Returns 0 on success, or -1 on failure.
653: */
654: int
655: rcs_access_add(RCSFILE *file, const char *login)
656: {
657: struct rcs_access *ap;
658:
659: /* first look for duplication */
660: TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
661: if (strcmp(ap->ra_name, login) == 0) {
662: rcs_errno = RCS_ERR_DUPENT;
663: return (-1);
664: }
665: }
666:
667: ap = xmalloc(sizeof(*ap));
668: ap->ra_name = xstrdup(login);
669: TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
670:
671: /* not synced anymore */
672: file->rf_flags &= ~RCS_SYNCED;
673: return (0);
674: }
675:
676: /*
677: * rcs_access_remove()
678: *
679: * Remove an entry with login name <login> from the access list of the RCS
680: * file <file>.
681: * Returns 0 on success, or -1 on failure.
682: */
683: int
684: rcs_access_remove(RCSFILE *file, const char *login)
685: {
686: struct rcs_access *ap;
687:
688: TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
689: if (strcmp(ap->ra_name, login) == 0)
690: break;
691:
692: if (ap == NULL) {
693: rcs_errno = RCS_ERR_NOENT;
694: return (-1);
695: }
696:
697: TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
698: xfree(ap->ra_name);
699: xfree(ap);
700:
701: /* not synced anymore */
702: file->rf_flags &= ~RCS_SYNCED;
703: return (0);
704: }
705:
706: /*
707: * rcs_sym_add()
708: *
709: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
710: * is named <sym> and is bound to the RCS revision <snum>.
711: * Returns 0 on success, or -1 on failure.
712: */
713: int
714: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
715: {
716: struct rcs_sym *symp;
717:
718: if (!rcs_sym_check(sym)) {
719: rcs_errno = RCS_ERR_BADSYM;
720: return (-1);
721: }
722:
723: /* first look for duplication */
724: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
725: if (strcmp(symp->rs_name, sym) == 0) {
726: rcs_errno = RCS_ERR_DUPENT;
727: return (-1);
728: }
729: }
730:
731: symp = xmalloc(sizeof(*symp));
732: symp->rs_name = xstrdup(sym);
733: symp->rs_num = rcsnum_alloc();
734: rcsnum_cpy(snum, symp->rs_num, 0);
735:
736: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
737:
738: /* not synced anymore */
739: rfp->rf_flags &= ~RCS_SYNCED;
740: return (0);
741: }
742:
743: /*
744: * rcs_sym_remove()
745: *
746: * Remove the symbol with name <sym> from the symbol list for the RCS file
747: * <file>. If no such symbol is found, the call fails and returns with an
748: * error.
749: * Returns 0 on success, or -1 on failure.
750: */
751: int
752: rcs_sym_remove(RCSFILE *file, const char *sym)
753: {
754: struct rcs_sym *symp;
755:
756: if (!rcs_sym_check(sym)) {
757: rcs_errno = RCS_ERR_BADSYM;
758: return (-1);
759: }
760:
761: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
762: if (strcmp(symp->rs_name, sym) == 0)
763: break;
764:
765: if (symp == NULL) {
766: rcs_errno = RCS_ERR_NOENT;
767: return (-1);
768: }
769:
770: TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
771: xfree(symp->rs_name);
772: rcsnum_free(symp->rs_num);
773: xfree(symp);
774:
775: /* not synced anymore */
776: file->rf_flags &= ~RCS_SYNCED;
777: return (0);
778: }
779:
780: /*
781: * rcs_sym_getrev()
782: *
783: * Retrieve the RCS revision number associated with the symbol <sym> for the
784: * RCS file <file>. The returned value is a dynamically-allocated copy and
785: * should be freed by the caller once they are done with it.
786: * Returns the RCSNUM on success, or NULL on failure.
787: */
788: RCSNUM *
789: rcs_sym_getrev(RCSFILE *file, const char *sym)
790: {
791: RCSNUM *num;
792: struct rcs_sym *symp;
793:
794: if (!rcs_sym_check(sym)) {
795: rcs_errno = RCS_ERR_BADSYM;
796: return (NULL);
797: }
798:
799: num = NULL;
800: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
801: if (strcmp(symp->rs_name, sym) == 0)
802: break;
803:
804: if (symp == NULL) {
805: rcs_errno = RCS_ERR_NOENT;
806: } else {
807: num = rcsnum_alloc();
808: rcsnum_cpy(symp->rs_num, num, 0);
809: }
810:
811: return (num);
812: }
813:
814: /*
815: * rcs_sym_check()
816: *
817: * Check the RCS symbol name <sym> for any unsupported characters.
818: * Returns 1 if the tag is correct, 0 if it isn't valid.
819: */
820: int
821: rcs_sym_check(const char *sym)
822: {
823: int ret;
824: const char *cp;
825:
826: ret = 1;
827: cp = sym;
828: if (!isalpha(*cp++))
829: return (0);
830:
831: for (; *cp != '\0'; cp++)
832: if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
833: ret = 0;
834: break;
835: }
836:
837: return (ret);
838: }
839:
840: /*
841: * rcs_lock_getmode()
842: *
843: * Retrieve the locking mode of the RCS file <file>.
844: */
845: int
846: rcs_lock_getmode(RCSFILE *file)
847: {
848: return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
849: }
850:
851: /*
852: * rcs_lock_setmode()
853: *
854: * Set the locking mode of the RCS file <file> to <mode>, which must either
855: * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
856: * Returns the previous mode on success, or -1 on failure.
857: */
858: int
859: rcs_lock_setmode(RCSFILE *file, int mode)
860: {
861: int pmode;
862: pmode = rcs_lock_getmode(file);
863:
864: if (mode == RCS_LOCK_STRICT)
865: file->rf_flags |= RCS_SLOCK;
866: else if (mode == RCS_LOCK_LOOSE)
867: file->rf_flags &= ~RCS_SLOCK;
868: else
869: errx(1, "rcs_lock_setmode: invalid mode `%d'", mode);
870:
871: file->rf_flags &= ~RCS_SYNCED;
872: return (pmode);
873: }
874:
875: /*
876: * rcs_lock_add()
877: *
878: * Add an RCS lock for the user <user> on revision <rev>.
879: * Returns 0 on success, or -1 on failure.
880: */
881: int
882: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
883: {
884: struct rcs_lock *lkp;
885:
886: /* first look for duplication */
887: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
888: if (strcmp(lkp->rl_name, user) == 0 &&
889: rcsnum_cmp(rev, lkp->rl_num, 0) == 0) {
890: rcs_errno = RCS_ERR_DUPENT;
891: return (-1);
892: }
893: }
894:
895: lkp = xmalloc(sizeof(*lkp));
896: lkp->rl_name = xstrdup(user);
897: lkp->rl_num = rcsnum_alloc();
898: rcsnum_cpy(rev, lkp->rl_num, 0);
899:
900: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
901:
902: /* not synced anymore */
903: file->rf_flags &= ~RCS_SYNCED;
904: return (0);
905: }
906:
907:
908: /*
909: * rcs_lock_remove()
910: *
911: * Remove the RCS lock on revision <rev>.
912: * Returns 0 on success, or -1 on failure.
913: */
914: int
915: rcs_lock_remove(RCSFILE *file, const char *user, RCSNUM *rev)
916: {
917: struct rcs_lock *lkp;
918:
919: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
920: if (strcmp(lkp->rl_name, user) == 0 &&
921: rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
922: break;
923: }
924:
925: if (lkp == NULL) {
926: rcs_errno = RCS_ERR_NOENT;
927: return (-1);
928: }
929:
930: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
931: rcsnum_free(lkp->rl_num);
932: xfree(lkp->rl_name);
933: xfree(lkp);
934:
935: /* not synced anymore */
936: file->rf_flags &= ~RCS_SYNCED;
937: return (0);
938: }
939:
940: /*
941: * rcs_desc_get()
942: *
943: * Retrieve the description for the RCS file <file>.
944: */
945: const char *
946: rcs_desc_get(RCSFILE *file)
947: {
948: return (file->rf_desc);
949: }
950:
951: /*
952: * rcs_desc_set()
953: *
954: * Set the description for the RCS file <file>.
955: */
956: void
957: rcs_desc_set(RCSFILE *file, const char *desc)
958: {
959: char *tmp;
960:
961: tmp = xstrdup(desc);
962: if (file->rf_desc != NULL)
963: xfree(file->rf_desc);
964: file->rf_desc = tmp;
965: file->rf_flags &= ~RCS_SYNCED;
966: }
967:
968: /*
969: * rcs_comment_lookup()
970: *
971: * Lookup the assumed comment leader based on a file's suffix.
972: * Returns a pointer to the string on success, or NULL on failure.
973: */
974: const char *
975: rcs_comment_lookup(const char *filename)
976: {
977: int i;
978: const char *sp;
979:
980: if ((sp = strrchr(filename, '.')) == NULL) {
981: rcs_errno = RCS_ERR_NOENT;
982: return (NULL);
983: }
984: sp++;
985:
986: for (i = 0; i < (int)NB_COMTYPES; i++)
987: if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
988: return (rcs_comments[i].rc_cstr);
989: return (NULL);
990: }
991:
992: /*
993: * rcs_comment_get()
994: *
995: * Retrieve the comment leader for the RCS file <file>.
996: */
997: const char *
998: rcs_comment_get(RCSFILE *file)
999: {
1000: return (file->rf_comment);
1001: }
1002:
1003: /*
1004: * rcs_comment_set()
1005: *
1006: * Set the comment leader for the RCS file <file>.
1007: */
1008: void
1009: rcs_comment_set(RCSFILE *file, const char *comment)
1010: {
1011: char *tmp;
1012:
1013: tmp = xstrdup(comment);
1014: if (file->rf_comment != NULL)
1015: xfree(file->rf_comment);
1016: file->rf_comment = tmp;
1017: file->rf_flags &= ~RCS_SYNCED;
1018: }
1019:
1020: /*
1021: * rcs_tag_resolve()
1022: *
1023: * Retrieve the revision number corresponding to the tag <tag> for the RCS
1024: * file <file>.
1025: */
1026: RCSNUM *
1027: rcs_tag_resolve(RCSFILE *file, const char *tag)
1028: {
1029: RCSNUM *num;
1030:
1031: if ((num = rcsnum_parse(tag)) == NULL) {
1032: num = rcs_sym_getrev(file, tag);
1033: }
1034:
1035: return (num);
1036: }
1037:
1038: int
1039: rcs_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
1040: {
1041: char op, *ep;
1042: struct rcs_line *lp, *dlp, *ndlp;
1043: int i, lineno, nbln;
1044:
1045: dlp = TAILQ_FIRST(&(dlines->l_lines));
1046: lp = TAILQ_FIRST(&(plines->l_lines));
1047:
1048: /* skip first bogus line */
1049: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
1050: lp = TAILQ_NEXT(lp, l_list)) {
1051: op = *(lp->l_line);
1052: lineno = (int)strtol((lp->l_line + 1), &ep, 10);
1053: if (lineno > dlines->l_nblines || lineno < 0 ||
1054: *ep != ' ')
1055: errx(1, "invalid line specification in RCS patch");
1056: ep++;
1057: nbln = (int)strtol(ep, &ep, 10);
1058: if (nbln < 0 || *ep != '\0')
1059: errx(1,
1060: "invalid line number specification in RCS patch");
1061:
1062: /* find the appropriate line */
1063: for (;;) {
1064: if (dlp == NULL)
1065: break;
1066: if (dlp->l_lineno == lineno)
1067: break;
1068: if (dlp->l_lineno > lineno) {
1069: dlp = TAILQ_PREV(dlp, rcs_tqh, l_list);
1070: } else if (dlp->l_lineno < lineno) {
1071: if (((ndlp = TAILQ_NEXT(dlp, l_list)) == NULL) ||
1072: ndlp->l_lineno > lineno)
1073: break;
1074: dlp = ndlp;
1075: }
1076: }
1077: if (dlp == NULL)
1078: errx(1, "can't find referenced line in RCS patch");
1079:
1080: if (op == 'd') {
1081: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1082: ndlp = TAILQ_NEXT(dlp, l_list);
1083: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
1084: dlp = ndlp;
1085: /* last line is gone - reset dlp */
1086: if (dlp == NULL) {
1087: ndlp = TAILQ_LAST(&(dlines->l_lines),
1088: rcs_tqh);
1089: dlp = ndlp;
1090: }
1091: }
1092: } else if (op == 'a') {
1093: for (i = 0; i < nbln; i++) {
1094: ndlp = lp;
1095: lp = TAILQ_NEXT(lp, l_list);
1096: if (lp == NULL)
1097: errx(1, "truncated RCS patch");
1098: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
1099: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
1100: lp, l_list);
1101: dlp = lp;
1102:
1103: /* we don't want lookup to block on those */
1104: lp->l_lineno = lineno;
1105:
1106: lp = ndlp;
1107: }
1108: } else
1109: errx(1, "unknown RCS patch operation `%c'", op);
1110:
1111: /* last line of the patch, done */
1112: if (lp->l_lineno == plines->l_nblines)
1113: break;
1114: }
1115:
1116: /* once we're done patching, rebuild the line numbers */
1117: lineno = 0;
1118: TAILQ_FOREACH(lp, &(dlines->l_lines), l_list)
1119: lp->l_lineno = lineno++;
1120: dlines->l_nblines = lineno - 1;
1121:
1122: return (0);
1123: }
1124:
1125: /*
1126: * rcs_getrev()
1127: *
1128: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1129: * returned buffer is dynamically allocated and should be released using
1130: * rcs_buf_free() once the caller is done using it.
1131: */
1132: BUF*
1133: rcs_getrev(RCSFILE *rfp, RCSNUM *frev)
1134: {
1135: u_int i, numlen;
1.6 niallo 1136: int isbranch, lookonbranch, found;
1.1 joris 1137: size_t len;
1138: void *bp;
1139: RCSNUM *crev, *rev, *brev;
1140: BUF *rbuf;
1141: struct rcs_delta *rdp = NULL;
1142: struct rcs_branch *rb;
1143:
1144: if (rfp->rf_head == NULL)
1145: return (NULL);
1146:
1147: if (frev == RCS_HEAD_REV)
1148: rev = rfp->rf_head;
1149: else
1150: rev = frev;
1151:
1152: /* XXX rcsnum_cmp() */
1153: for (i = 0; i < rfp->rf_head->rn_len; i++) {
1154: if (rfp->rf_head->rn_id[i] < rev->rn_id[i]) {
1155: rcs_errno = RCS_ERR_NOENT;
1156: return (NULL);
1157: }
1158: }
1159:
1160: /* No matter what, we're going to need up the the description parsed */
1161: rcs_parse_desc(rfp, NULL);
1162:
1163: rdp = rcs_findrev(rfp, rfp->rf_head);
1164: if (rdp == NULL) {
1165: warnx("failed to get RCS HEAD revision");
1166: return (NULL);
1167: }
1168:
1169: if (rdp->rd_tlen == 0)
1170: rcs_parse_deltatexts(rfp, rfp->rf_head);
1171:
1172: len = rdp->rd_tlen;
1173: if (len == 0) {
1174: rbuf = rcs_buf_alloc(1, 0);
1175: rcs_buf_empty(rbuf);
1176: return (rbuf);
1177: }
1178:
1179: rbuf = rcs_buf_alloc(len, BUF_AUTOEXT);
1180: rcs_buf_append(rbuf, rdp->rd_text, len);
1181:
1182: isbranch = 0;
1183: brev = NULL;
1184:
1185: /*
1186: * If a branch was passed, get the latest revision on it.
1187: */
1188: if (RCSNUM_ISBRANCH(rev)) {
1189: brev = rev;
1190: rdp = rcs_findrev(rfp, rev);
1191: if (rdp == NULL)
1192: return (NULL);
1193:
1194: rev = rdp->rd_num;
1195: } else {
1196: if (RCSNUM_ISBRANCHREV(rev)) {
1197: brev = rcsnum_revtobr(rev);
1198: isbranch = 1;
1199: }
1200: }
1201:
1202: lookonbranch = 0;
1203: crev = NULL;
1204:
1205: /* Apply patches backwards to get the right version.
1206: */
1207: do {
1.6 niallo 1208: found = 0;
1209:
1.1 joris 1210: if (rcsnum_cmp(rfp->rf_head, rev, 0) == 0)
1211: break;
1212:
1213: if (isbranch == 1 && rdp->rd_num->rn_len < rev->rn_len &&
1214: !TAILQ_EMPTY(&(rdp->rd_branches)))
1215: lookonbranch = 1;
1216:
1217: if (isbranch && lookonbranch == 1) {
1218: lookonbranch = 0;
1219: TAILQ_FOREACH(rb, &(rdp->rd_branches), rb_list) {
1220: /* XXX rcsnum_cmp() is totally broken for
1221: * this purpose.
1222: */
1223: numlen = MIN(brev->rn_len, rb->rb_num->rn_len);
1224: for (i = 0; i < numlen; i++) {
1225: if (rb->rb_num->rn_id[i] !=
1226: brev->rn_id[i])
1227: break;
1228: }
1229:
1230: if (i == numlen) {
1231: crev = rb->rb_num;
1.6 niallo 1232: found = 1;
1.1 joris 1233: break;
1234: }
1235: }
1.6 niallo 1236: if (found == 0)
1237: crev = rdp->rd_next;
1.1 joris 1238: } else {
1239: crev = rdp->rd_next;
1240: }
1241:
1242: rdp = rcs_findrev(rfp, crev);
1243: if (rdp == NULL) {
1244: rcs_buf_free(rbuf);
1245: return (NULL);
1246: }
1247:
1248: rcs_buf_putc(rbuf, '\0');
1249:
1250: /* check if we have parsed this rev's deltatext */
1251: if (rdp->rd_tlen == 0)
1252: rcs_parse_deltatexts(rfp, rdp->rd_num);
1253:
1254: bp = rcs_buf_release(rbuf);
1255: rbuf = rcs_patchfile((char *)bp, (char *)rdp->rd_text,
1256: rcs_patch_lines);
1257: xfree(bp);
1258:
1259: if (rbuf == NULL)
1260: break;
1261: } while (rcsnum_cmp(crev, rev, 0) != 0);
1262:
1263: if (rcs_buf_getc(rbuf, rcs_buf_len(rbuf)-1) != '\n' &&
1264: rbuf != NULL)
1265: rcs_buf_putc(rbuf, '\n');
1266:
1267: return (rbuf);
1268: }
1269:
1270: /*
1271: * rcs_rev_add()
1272: *
1273: * Add a revision to the RCS file <rf>. The new revision's number can be
1274: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1275: * new revision will have a number equal to the previous head revision plus
1276: * one). The <msg> argument specifies the log message for that revision, and
1277: * <date> specifies the revision's date (a value of -1 is
1278: * equivalent to using the current time).
1279: * If <username> is NULL, set the author for this revision to the current user.
1280: * Otherwise, set it to <username>.
1281: * Returns 0 on success, or -1 on failure.
1282: */
1283: int
1284: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1285: const char *username)
1286: {
1287: time_t now;
1288: struct passwd *pw;
1289: struct rcs_delta *ordp, *rdp;
1290:
1291: if (rev == RCS_HEAD_REV) {
1292: if (rf->rf_flags & RCS_CREATE) {
1293: if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
1294: return (-1);
1295: rf->rf_head = rcsnum_alloc();
1296: rcsnum_cpy(rev, rf->rf_head, 0);
1297: } else {
1298: rev = rcsnum_inc(rf->rf_head);
1299: }
1300: } else {
1301: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1302: rcs_errno = RCS_ERR_DUPENT;
1303: return (-1);
1304: }
1305: }
1306:
1307: if ((pw = getpwuid(getuid())) == NULL)
1308: errx(1, "getpwuid failed");
1309:
1310: rdp = xcalloc(1, sizeof(*rdp));
1311:
1312: TAILQ_INIT(&(rdp->rd_branches));
1313:
1314: rdp->rd_num = rcsnum_alloc();
1315: rcsnum_cpy(rev, rdp->rd_num, 0);
1316:
1317: rdp->rd_next = rcsnum_alloc();
1318:
1319: if (!(rf->rf_flags & RCS_CREATE)) {
1320: /* next should point to the previous HEAD */
1321: ordp = TAILQ_FIRST(&(rf->rf_delta));
1322: rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
1323: }
1324:
1325:
1326: if (username == NULL)
1327: username = pw->pw_name;
1328:
1329: rdp->rd_author = xstrdup(username);
1330: rdp->rd_state = xstrdup(RCS_STATE_EXP);
1331: rdp->rd_log = xstrdup(msg);
1332:
1333: if (date != (time_t)(-1))
1334: now = date;
1335: else
1336: time(&now);
1337: gmtime_r(&now, &(rdp->rd_date));
1338:
1339: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1340: rf->rf_ndelta++;
1341:
1342: /* not synced anymore */
1343: rf->rf_flags &= ~RCS_SYNCED;
1344:
1345: return (0);
1346: }
1347:
1348: /*
1349: * rcs_rev_remove()
1350: *
1351: * Remove the revision whose number is <rev> from the RCS file <rf>.
1352: */
1353: int
1354: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1355: {
1.7 xsa 1356: char *newdeltatext, *path_tmp1, *path_tmp2;
1.1 joris 1357: struct rcs_delta *rdp, *prevrdp, *nextrdp;
1358: BUF *nextbuf, *prevbuf, *newdiff;
1359:
1360: if (rev == RCS_HEAD_REV)
1361: rev = rf->rf_head;
1362:
1363: /* do we actually have that revision? */
1364: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1365: rcs_errno = RCS_ERR_NOENT;
1366: return (-1);
1367: }
1368:
1369: /*
1370: * This is confusing, the previous delta is next in the TAILQ list.
1371: * the next delta is the previous one in the TAILQ list.
1372: *
1373: * When the HEAD revision got specified, nextrdp will be NULL.
1374: * When the first revision got specified, prevrdp will be NULL.
1375: */
1376: prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
1377: nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, rcs_tqh, rd_list);
1378:
1379: newdeltatext = NULL;
1380: prevbuf = nextbuf = NULL;
1381:
1382: if (prevrdp != NULL) {
1383: if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
1384: errx(1, "error getting revision");
1385: }
1386:
1387: if (prevrdp != NULL && nextrdp != NULL) {
1388: if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
1389: errx(1, "error getting revision");
1390:
1391: newdiff = rcs_buf_alloc(64, BUF_AUTOEXT);
1392:
1393: /* calculate new diff */
1.7 xsa 1394: (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.1 joris 1395: rcs_buf_write_stmp(nextbuf, path_tmp1, 0600);
1396: rcs_buf_free(nextbuf);
1397:
1.7 xsa 1398: (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.1 joris 1399: rcs_buf_write_stmp(prevbuf, path_tmp2, 0600);
1400: rcs_buf_free(prevbuf);
1401:
1402: diff_format = D_RCSDIFF;
1.8 xsa 1403: if (rcs_diffreg(path_tmp1, path_tmp2, newdiff) == D_ERROR)
1404: errx(1, "rcs_diffreg failed");
1.1 joris 1405:
1406: newdeltatext = rcs_buf_release(newdiff);
1407: } else if (nextrdp == NULL && prevrdp != NULL) {
1408: newdeltatext = rcs_buf_release(prevbuf);
1409: }
1410:
1411: if (newdeltatext != NULL) {
1412: if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
1413: errx(1, "error setting new deltatext");
1414: }
1415:
1416: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1417:
1418: /* update pointers */
1419: if (prevrdp != NULL && nextrdp != NULL) {
1420: rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
1421: } else if (prevrdp != NULL) {
1422: if (rcs_head_set(rf, prevrdp->rd_num) < 0)
1423: errx(1, "rcs_head_set failed");
1424: } else if (nextrdp != NULL) {
1425: rcsnum_free(nextrdp->rd_next);
1426: nextrdp->rd_next = rcsnum_alloc();
1427: } else {
1428: rcsnum_free(rf->rf_head);
1429: rf->rf_head = NULL;
1430: }
1431:
1432: rf->rf_ndelta--;
1433: rf->rf_flags &= ~RCS_SYNCED;
1434:
1435: rcs_freedelta(rdp);
1436:
1437: if (newdeltatext != NULL)
1438: xfree(newdeltatext);
1.7 xsa 1439:
1440: if (path_tmp1 != NULL)
1441: xfree(path_tmp1);
1442: if (path_tmp2 != NULL)
1443: xfree(path_tmp2);
1.1 joris 1444:
1445: return (0);
1446: }
1447:
1448: /*
1449: * rcs_findrev()
1450: *
1451: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1452: * The revision number is given in <rev>.
1453: *
1454: * If the given revision is a branch number, we translate it into the latest
1455: * revision on the branch.
1456: *
1457: * Returns a pointer to the delta on success, or NULL on failure.
1458: */
1459: struct rcs_delta *
1460: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1461: {
1462: u_int cmplen;
1463: struct rcs_delta *rdp;
1464: RCSNUM *brev, *frev;
1465:
1466: /*
1467: * We need to do more parsing if the last revision in the linked list
1468: * is greater than the requested revision.
1469: */
1470: rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1471: if (rdp == NULL ||
1472: rcsnum_cmp(rdp->rd_num, rev, 0) == -1) {
1473: rcs_parse_deltas(rfp, rev);
1474: }
1475:
1476: /*
1477: * Translate a branch into the latest revision on the branch itself.
1478: */
1479: if (RCSNUM_ISBRANCH(rev)) {
1480: brev = rcsnum_brtorev(rev);
1481: frev = brev;
1482: for (;;) {
1483: rdp = rcs_findrev(rfp, frev);
1484: if (rdp == NULL)
1485: return (NULL);
1486:
1487: if (rdp->rd_next->rn_len == 0)
1488: break;
1489:
1490: frev = rdp->rd_next;
1491: }
1492:
1493: rcsnum_free(brev);
1494: return (rdp);
1495: }
1496:
1497: cmplen = rev->rn_len;
1498:
1499: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1500: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0)
1501: return (rdp);
1502: }
1503:
1504: return (NULL);
1505: }
1506:
1507: /*
1508: * rcs_kwexp_set()
1509: *
1510: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1511: */
1512: void
1513: rcs_kwexp_set(RCSFILE *file, int mode)
1514: {
1515: int i;
1516: char *tmp, buf[8] = "";
1517:
1518: if (RCS_KWEXP_INVAL(mode))
1519: return;
1520:
1521: i = 0;
1522: if (mode == RCS_KWEXP_NONE)
1523: buf[0] = 'b';
1524: else if (mode == RCS_KWEXP_OLD)
1525: buf[0] = 'o';
1526: else {
1527: if (mode & RCS_KWEXP_NAME)
1528: buf[i++] = 'k';
1529: if (mode & RCS_KWEXP_VAL)
1530: buf[i++] = 'v';
1531: if (mode & RCS_KWEXP_LKR)
1532: buf[i++] = 'l';
1533: }
1534:
1535: tmp = xstrdup(buf);
1536: if (file->rf_expand != NULL)
1537: xfree(file->rf_expand);
1538: file->rf_expand = tmp;
1539: /* not synced anymore */
1540: file->rf_flags &= ~RCS_SYNCED;
1541: }
1542:
1543: /*
1544: * rcs_kwexp_get()
1545: *
1546: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1547: */
1548: int
1549: rcs_kwexp_get(RCSFILE *file)
1550: {
1551: return rcs_kflag_get(file->rf_expand);
1552: }
1553:
1554: /*
1555: * rcs_kflag_get()
1556: *
1557: * Get the keyword expansion mode from a set of character flags given in
1558: * <flags> and return the appropriate flag mask. In case of an error, the
1559: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1560: */
1561: int
1562: rcs_kflag_get(const char *flags)
1563: {
1564: int fl;
1565: size_t len;
1566: const char *fp;
1567:
1568: fl = 0;
1569: len = strlen(flags);
1570:
1571: for (fp = flags; *fp != '\0'; fp++) {
1572: if (*fp == 'k')
1573: fl |= RCS_KWEXP_NAME;
1574: else if (*fp == 'v')
1575: fl |= RCS_KWEXP_VAL;
1576: else if (*fp == 'l')
1577: fl |= RCS_KWEXP_LKR;
1578: else if (*fp == 'o') {
1579: if (len != 1)
1580: fl |= RCS_KWEXP_ERR;
1581: fl |= RCS_KWEXP_OLD;
1582: } else if (*fp == 'b') {
1583: if (len != 1)
1584: fl |= RCS_KWEXP_ERR;
1585: } else /* unknown letter */
1586: fl |= RCS_KWEXP_ERR;
1587: }
1588:
1589: return (fl);
1590: }
1591:
1592: /*
1593: * rcs_errstr()
1594: *
1595: * Get the error string matching the RCS error code <code>.
1596: */
1597: const char *
1598: rcs_errstr(int code)
1599: {
1600: const char *esp;
1601:
1602: if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
1603: esp = NULL;
1604: else if (code == RCS_ERR_ERRNO)
1605: esp = strerror(errno);
1606: else
1607: esp = rcs_errstrs[code];
1608: return (esp);
1609: }
1610:
1611: /* rcs_parse_deltas()
1612: *
1613: * Parse deltas. If <rev> is not NULL, parse only as far as that
1614: * revision. If <rev> is NULL, parse all deltas.
1615: */
1616: static void
1617: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1618: {
1619: int ret;
1620: struct rcs_delta *enddelta;
1621:
1622: if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
1623: return;
1624:
1625: for (;;) {
1626: ret = rcs_parse_delta(rfp);
1627: if (rev != NULL) {
1628: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1629: if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1630: break;
1631: }
1632: if (ret == 0) {
1633: rfp->rf_flags |= PARSED_DELTAS;
1634: break;
1635: }
1636: else if (ret == -1)
1637: errx(1, "error parsing deltas");
1638: }
1639: }
1640:
1641: /* rcs_parse_deltatexts()
1642: *
1643: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1644: * revision. If <rev> is NULL, parse everything.
1645: */
1646: static void
1647: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1648: {
1649: int ret;
1650: struct rcs_delta *rdp;
1651:
1652: if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
1653: (rfp->rf_flags & RCS_CREATE))
1654: return;
1655:
1656: if (!(rfp->rf_flags & PARSED_DESC))
1657: rcs_parse_desc(rfp, rev);
1658: for (;;) {
1659: if (rev != NULL) {
1660: rdp = rcs_findrev(rfp, rev);
1661: if (rdp->rd_text != NULL)
1662: break;
1663: else
1664: ret = rcs_parse_deltatext(rfp);
1665: } else
1666: ret = rcs_parse_deltatext(rfp);
1667: if (ret == 0) {
1668: rfp->rf_flags |= PARSED_DELTATEXTS;
1669: break;
1670: }
1671: else if (ret == -1)
1672: errx(1, "problem parsing deltatexts");
1673: }
1674: }
1675:
1676: /* rcs_parse_desc()
1677: *
1678: * Parse RCS description.
1679: */
1680: static void
1681: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1682: {
1683: int ret = 0;
1684:
1685: if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
1686: return;
1687: if (!(rfp->rf_flags & PARSED_DELTAS))
1688: rcs_parse_deltas(rfp, rev);
1689: /* do parsing */
1690: ret = rcs_gettok(rfp);
1691: if (ret != RCS_TOK_DESC)
1692: errx(1, "token `%s' found where RCS desc expected",
1693: RCS_TOKSTR(rfp));
1694:
1695: ret = rcs_gettok(rfp);
1696: if (ret != RCS_TOK_STRING)
1697: errx(1, "token `%s' found where RCS desc expected",
1698: RCS_TOKSTR(rfp));
1699:
1700: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1701: rfp->rf_flags |= PARSED_DESC;
1702: }
1703:
1704: /*
1705: * rcs_parse_init()
1706: *
1707: * Initial parsing of file <path>, which are in the RCS format.
1708: * Just does admin section.
1709: */
1710: static void
1711: rcs_parse_init(RCSFILE *rfp)
1712: {
1713: struct rcs_pdata *pdp;
1714:
1715: if (rfp->rf_flags & RCS_PARSED)
1716: return;
1717:
1718: pdp = xcalloc(1, sizeof(*pdp));
1719:
1720: pdp->rp_lines = 0;
1721: pdp->rp_pttype = RCS_TOK_ERR;
1722:
1.10 ! ray 1723: if ((pdp->rp_file = fdopen(rfp->rf_fd, "r")) == NULL)
1.3 joris 1724: err(1, "fopen: `%s'", rfp->rf_path);
1.1 joris 1725:
1726: pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1727: pdp->rp_blen = RCS_BUFSIZE;
1728: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1729:
1730: /* ditch the strict lock */
1731: rfp->rf_flags &= ~RCS_SLOCK;
1732: rfp->rf_pdata = pdp;
1733:
1734: if (rcs_parse_admin(rfp) < 0) {
1735: rcs_freepdata(pdp);
1736: errx(1, "could not parse admin data");
1737: }
1738:
1739: if (rfp->rf_flags & RCS_PARSE_FULLY)
1740: rcs_parse_deltatexts(rfp, NULL);
1741:
1742: rfp->rf_flags |= RCS_SYNCED;
1743: }
1744:
1745: /*
1746: * rcs_parse_admin()
1747: *
1748: * Parse the administrative portion of an RCS file.
1749: * Returns the type of the first token found after the admin section on
1750: * success, or -1 on failure.
1751: */
1752: static int
1753: rcs_parse_admin(RCSFILE *rfp)
1754: {
1755: u_int i;
1756: int tok, ntok, hmask;
1757: struct rcs_key *rk;
1758:
1759: /* hmask is a mask of the headers already encountered */
1760: hmask = 0;
1761: for (;;) {
1762: tok = rcs_gettok(rfp);
1763: if (tok == RCS_TOK_ERR) {
1764: rcs_errno = RCS_ERR_PARSE;
1765: warnx("parse error in RCS admin section");
1766: goto fail;
1767: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1768: /*
1769: * Assume this is the start of the first delta or
1770: * that we are dealing with an empty RCS file and
1771: * we just found the description.
1772: */
1773: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1774: return (tok);
1775: }
1776:
1777: rk = NULL;
1778: for (i = 0; i < RCS_NKEYS; i++)
1779: if (rcs_keys[i].rk_id == tok)
1780: rk = &(rcs_keys[i]);
1781:
1782: if (hmask & (1 << tok)) {
1783: rcs_errno = RCS_ERR_PARSE;
1784: warnx("duplicate RCS key");
1785: goto fail;
1786: }
1787: hmask |= (1 << tok);
1788:
1789: switch (tok) {
1790: case RCS_TOK_HEAD:
1791: case RCS_TOK_BRANCH:
1792: case RCS_TOK_COMMENT:
1793: case RCS_TOK_EXPAND:
1794: ntok = rcs_gettok(rfp);
1795: if (ntok == RCS_TOK_SCOLON)
1796: break;
1797: if (ntok != rk->rk_val) {
1798: rcs_errno = RCS_ERR_PARSE;
1799: warnx("invalid value type for RCS key `%s'",
1800: rk->rk_str);
1801: }
1802:
1803: if (tok == RCS_TOK_HEAD) {
1804: if (rfp->rf_head == NULL)
1805: rfp->rf_head = rcsnum_alloc();
1806: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1807: rfp->rf_head);
1808: } else if (tok == RCS_TOK_BRANCH) {
1809: if (rfp->rf_branch == NULL)
1810: rfp->rf_branch = rcsnum_alloc();
1811: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1812: rfp->rf_branch) < 0)
1813: goto fail;
1814: } else if (tok == RCS_TOK_COMMENT) {
1815: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1816: } else if (tok == RCS_TOK_EXPAND) {
1817: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1818: }
1819:
1820: /* now get the expected semi-colon */
1821: ntok = rcs_gettok(rfp);
1822: if (ntok != RCS_TOK_SCOLON) {
1823: rcs_errno = RCS_ERR_PARSE;
1824: warnx("missing semi-colon after RCS `%s' key",
1825: rk->rk_str);
1826: goto fail;
1827: }
1828: break;
1829: case RCS_TOK_ACCESS:
1830: if (rcs_parse_access(rfp) < 0)
1831: goto fail;
1832: break;
1833: case RCS_TOK_SYMBOLS:
1834: if (rcs_parse_symbols(rfp) < 0)
1835: goto fail;
1836: break;
1837: case RCS_TOK_LOCKS:
1838: if (rcs_parse_locks(rfp) < 0)
1839: goto fail;
1840: break;
1841: default:
1842: rcs_errno = RCS_ERR_PARSE;
1843: warnx("unexpected token `%s' in RCS admin section",
1844: RCS_TOKSTR(rfp));
1845: goto fail;
1846: }
1847: }
1848:
1849: fail:
1850: return (-1);
1851: }
1852:
1853: /*
1854: * rcs_parse_delta()
1855: *
1856: * Parse an RCS delta section and allocate the structure to store that delta's
1857: * information in the <rfp> delta list.
1858: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1859: * -1 on error.
1860: */
1861: static int
1862: rcs_parse_delta(RCSFILE *rfp)
1863: {
1864: int ret, tok, ntok, hmask;
1865: u_int i;
1866: char *tokstr;
1867: RCSNUM *datenum;
1868: struct rcs_delta *rdp;
1869: struct rcs_key *rk;
1870:
1871: rdp = xcalloc(1, sizeof(*rdp));
1872:
1873: rdp->rd_num = rcsnum_alloc();
1874: rdp->rd_next = rcsnum_alloc();
1875:
1876: TAILQ_INIT(&(rdp->rd_branches));
1877:
1878: tok = rcs_gettok(rfp);
1879: if (tok == RCS_TOK_DESC) {
1880: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1881: return (0);
1882: } else if (tok != RCS_TOK_NUM) {
1883: rcs_errno = RCS_ERR_PARSE;
1884: warnx("unexpected token `%s' at start of delta",
1885: RCS_TOKSTR(rfp));
1886: rcs_freedelta(rdp);
1887: return (-1);
1888: }
1889: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1890:
1891: hmask = 0;
1892: ret = 0;
1893: tokstr = NULL;
1894:
1895: for (;;) {
1896: tok = rcs_gettok(rfp);
1897: if (tok == RCS_TOK_ERR) {
1898: rcs_errno = RCS_ERR_PARSE;
1899: warnx("parse error in RCS delta section");
1900: rcs_freedelta(rdp);
1901: return (-1);
1902: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1903: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1904: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1905: break;
1906: }
1907:
1908: rk = NULL;
1909: for (i = 0; i < RCS_NKEYS; i++)
1910: if (rcs_keys[i].rk_id == tok)
1911: rk = &(rcs_keys[i]);
1912:
1913: if (hmask & (1 << tok)) {
1914: rcs_errno = RCS_ERR_PARSE;
1915: warnx("duplicate RCS key");
1916: rcs_freedelta(rdp);
1917: return (-1);
1918: }
1919: hmask |= (1 << tok);
1920:
1921: switch (tok) {
1922: case RCS_TOK_DATE:
1923: case RCS_TOK_AUTHOR:
1924: case RCS_TOK_STATE:
1925: case RCS_TOK_NEXT:
1926: ntok = rcs_gettok(rfp);
1927: if (ntok == RCS_TOK_SCOLON) {
1928: if (rk->rk_flags & RCS_VOPT)
1929: break;
1930: else {
1931: rcs_errno = RCS_ERR_PARSE;
1932: warnx("missing mandatory "
1933: "value to RCS key `%s'",
1934: rk->rk_str);
1935: rcs_freedelta(rdp);
1936: return (-1);
1937: }
1938: }
1939:
1940: if (ntok != rk->rk_val) {
1941: rcs_errno = RCS_ERR_PARSE;
1942: warnx("invalid value type for RCS key `%s'",
1943: rk->rk_str);
1944: rcs_freedelta(rdp);
1945: return (-1);
1946: }
1947:
1948: if (tokstr != NULL)
1949: xfree(tokstr);
1950: tokstr = xstrdup(RCS_TOKSTR(rfp));
1951: /* now get the expected semi-colon */
1952: ntok = rcs_gettok(rfp);
1953: if (ntok != RCS_TOK_SCOLON) {
1954: rcs_errno = RCS_ERR_PARSE;
1955: warnx("missing semi-colon after RCS `%s' key",
1956: rk->rk_str);
1957: xfree(tokstr);
1958: rcs_freedelta(rdp);
1959: return (-1);
1960: }
1961:
1962: if (tok == RCS_TOK_DATE) {
1963: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1964: xfree(tokstr);
1965: rcs_freedelta(rdp);
1966: return (-1);
1967: }
1968: if (datenum->rn_len != 6) {
1969: rcs_errno = RCS_ERR_PARSE;
1970: warnx("RCS date specification has %s "
1971: "fields",
1972: (datenum->rn_len > 6) ? "too many" :
1973: "missing");
1974: xfree(tokstr);
1975: rcs_freedelta(rdp);
1976: rcsnum_free(datenum);
1977: return (-1);
1978: }
1979: rdp->rd_date.tm_year = datenum->rn_id[0];
1980: if (rdp->rd_date.tm_year >= 1900)
1981: rdp->rd_date.tm_year -= 1900;
1982: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1983: rdp->rd_date.tm_mday = datenum->rn_id[2];
1984: rdp->rd_date.tm_hour = datenum->rn_id[3];
1985: rdp->rd_date.tm_min = datenum->rn_id[4];
1986: rdp->rd_date.tm_sec = datenum->rn_id[5];
1987: rcsnum_free(datenum);
1988: } else if (tok == RCS_TOK_AUTHOR) {
1989: rdp->rd_author = tokstr;
1990: tokstr = NULL;
1991: } else if (tok == RCS_TOK_STATE) {
1992: rdp->rd_state = tokstr;
1993: tokstr = NULL;
1994: } else if (tok == RCS_TOK_NEXT) {
1995: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1996: }
1997: break;
1998: case RCS_TOK_BRANCHES:
1999: if (rcs_parse_branches(rfp, rdp) < 0) {
2000: rcs_freedelta(rdp);
2001: return (-1);
2002: }
2003: break;
2004: default:
2005: rcs_errno = RCS_ERR_PARSE;
2006: warnx("unexpected token `%s' in RCS delta",
2007: RCS_TOKSTR(rfp));
2008: rcs_freedelta(rdp);
2009: return (-1);
2010: }
2011: }
2012:
2013: if (tokstr != NULL)
2014: xfree(tokstr);
2015:
2016: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
2017: rfp->rf_ndelta++;
2018:
2019: return (ret);
2020: }
2021:
2022: /*
2023: * rcs_parse_deltatext()
2024: *
2025: * Parse an RCS delta text section and fill in the log and text field of the
2026: * appropriate delta section.
2027: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2028: * -1 on error.
2029: */
2030: static int
2031: rcs_parse_deltatext(RCSFILE *rfp)
2032: {
2033: int tok;
2034: RCSNUM *tnum;
2035: struct rcs_delta *rdp;
2036:
2037: tok = rcs_gettok(rfp);
2038: if (tok == RCS_TOK_EOF)
2039: return (0);
2040:
2041: if (tok != RCS_TOK_NUM) {
2042: rcs_errno = RCS_ERR_PARSE;
2043: warnx("unexpected token `%s' at start of RCS delta text",
2044: RCS_TOKSTR(rfp));
2045: return (-1);
2046: }
2047:
2048: tnum = rcsnum_alloc();
2049: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2050:
2051: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2052: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2053: break;
2054: }
2055: rcsnum_free(tnum);
2056:
2057: if (rdp == NULL) {
2058: warnx("RCS delta text `%s' has no matching delta",
2059: RCS_TOKSTR(rfp));
2060: return (-1);
2061: }
2062:
2063: tok = rcs_gettok(rfp);
2064: if (tok != RCS_TOK_LOG) {
2065: rcs_errno = RCS_ERR_PARSE;
2066: warnx("unexpected token `%s' where RCS log expected",
2067: RCS_TOKSTR(rfp));
2068: return (-1);
2069: }
2070:
2071: tok = rcs_gettok(rfp);
2072: if (tok != RCS_TOK_STRING) {
2073: rcs_errno = RCS_ERR_PARSE;
2074: warnx("unexpected token `%s' where RCS log expected",
2075: RCS_TOKSTR(rfp));
2076: return (-1);
2077: }
2078: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
2079: tok = rcs_gettok(rfp);
2080: if (tok != RCS_TOK_TEXT) {
2081: rcs_errno = RCS_ERR_PARSE;
2082: warnx("unexpected token `%s' where RCS text expected",
2083: RCS_TOKSTR(rfp));
2084: return (-1);
2085: }
2086:
2087: tok = rcs_gettok(rfp);
2088: if (tok != RCS_TOK_STRING) {
2089: rcs_errno = RCS_ERR_PARSE;
2090: warnx("unexpected token `%s' where RCS text expected",
2091: RCS_TOKSTR(rfp));
2092: return (-1);
2093: }
2094:
2095: rdp->rd_text = xmalloc(RCS_TOKLEN(rfp) + 1);
1.5 ray 2096: if (strlcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp) + 1)) >=
2097: RCS_TOKLEN(rfp) + 1)
2098: errx(1, "rcs_parse_deltatext: strlcpy");
1.1 joris 2099: rdp->rd_tlen = RCS_TOKLEN(rfp);
2100:
2101: return (1);
2102: }
2103:
2104: /*
2105: * rcs_parse_access()
2106: *
2107: * Parse the access list given as value to the `access' keyword.
2108: * Returns 0 on success, or -1 on failure.
2109: */
2110: static int
2111: rcs_parse_access(RCSFILE *rfp)
2112: {
2113: int type;
2114:
2115: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2116: if (type != RCS_TOK_ID) {
2117: rcs_errno = RCS_ERR_PARSE;
2118: warnx("unexpected token `%s' in access list",
2119: RCS_TOKSTR(rfp));
2120: return (-1);
2121: }
2122:
2123: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2124: return (-1);
2125: }
2126:
2127: return (0);
2128: }
2129:
2130: /*
2131: * rcs_parse_symbols()
2132: *
2133: * Parse the symbol list given as value to the `symbols' keyword.
2134: * Returns 0 on success, or -1 on failure.
2135: */
2136: static int
2137: rcs_parse_symbols(RCSFILE *rfp)
2138: {
2139: int type;
2140: struct rcs_sym *symp;
2141:
2142: for (;;) {
2143: type = rcs_gettok(rfp);
2144: if (type == RCS_TOK_SCOLON)
2145: break;
2146:
2147: if (type != RCS_TOK_ID) {
2148: rcs_errno = RCS_ERR_PARSE;
2149: warnx("unexpected token `%s' in symbol list",
2150: RCS_TOKSTR(rfp));
2151: return (-1);
2152: }
2153:
2154: symp = xmalloc(sizeof(*symp));
2155: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
2156: symp->rs_num = rcsnum_alloc();
2157:
2158: type = rcs_gettok(rfp);
2159: if (type != RCS_TOK_COLON) {
2160: rcs_errno = RCS_ERR_PARSE;
2161: warnx("unexpected token `%s' in symbol list",
2162: RCS_TOKSTR(rfp));
2163: rcsnum_free(symp->rs_num);
2164: xfree(symp->rs_name);
2165: xfree(symp);
2166: return (-1);
2167: }
2168:
2169: type = rcs_gettok(rfp);
2170: if (type != RCS_TOK_NUM) {
2171: rcs_errno = RCS_ERR_PARSE;
2172: warnx("unexpected token `%s' in symbol list",
2173: RCS_TOKSTR(rfp));
2174: rcsnum_free(symp->rs_num);
2175: xfree(symp->rs_name);
2176: xfree(symp);
2177: return (-1);
2178: }
2179:
2180: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2181: warnx("failed to parse RCS NUM `%s'",
2182: RCS_TOKSTR(rfp));
2183: rcsnum_free(symp->rs_num);
2184: xfree(symp->rs_name);
2185: xfree(symp);
2186: return (-1);
2187: }
2188:
2189: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
2190: }
2191:
2192: return (0);
2193: }
2194:
2195: /*
2196: * rcs_parse_locks()
2197: *
2198: * Parse the lock list given as value to the `locks' keyword.
2199: * Returns 0 on success, or -1 on failure.
2200: */
2201: static int
2202: rcs_parse_locks(RCSFILE *rfp)
2203: {
2204: int type;
2205: struct rcs_lock *lkp;
2206:
2207: for (;;) {
2208: type = rcs_gettok(rfp);
2209: if (type == RCS_TOK_SCOLON)
2210: break;
2211:
2212: if (type != RCS_TOK_ID) {
2213: rcs_errno = RCS_ERR_PARSE;
2214: warnx("unexpected token `%s' in lock list",
2215: RCS_TOKSTR(rfp));
2216: return (-1);
2217: }
2218:
2219: lkp = xmalloc(sizeof(*lkp));
2220: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
2221: lkp->rl_num = rcsnum_alloc();
2222:
2223: type = rcs_gettok(rfp);
2224: if (type != RCS_TOK_COLON) {
2225: rcs_errno = RCS_ERR_PARSE;
2226: warnx("unexpected token `%s' in symbol list",
2227: RCS_TOKSTR(rfp));
2228: rcsnum_free(lkp->rl_num);
2229: xfree(lkp->rl_name);
2230: xfree(lkp);
2231: return (-1);
2232: }
2233:
2234: type = rcs_gettok(rfp);
2235: if (type != RCS_TOK_NUM) {
2236: rcs_errno = RCS_ERR_PARSE;
2237: warnx("unexpected token `%s' in symbol list",
2238: RCS_TOKSTR(rfp));
2239: rcsnum_free(lkp->rl_num);
2240: xfree(lkp->rl_name);
2241: xfree(lkp);
2242: return (-1);
2243: }
2244:
2245: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2246: warnx("failed to parse RCS NUM `%s'",
2247: RCS_TOKSTR(rfp));
2248: rcsnum_free(lkp->rl_num);
2249: xfree(lkp->rl_name);
2250: xfree(lkp);
2251: return (-1);
2252: }
2253:
2254: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2255: }
2256:
2257: /* check if we have a `strict' */
2258: type = rcs_gettok(rfp);
2259: if (type != RCS_TOK_STRICT) {
2260: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
2261: } else {
2262: rfp->rf_flags |= RCS_SLOCK;
2263:
2264: type = rcs_gettok(rfp);
2265: if (type != RCS_TOK_SCOLON) {
2266: rcs_errno = RCS_ERR_PARSE;
2267: warnx("missing semi-colon after `strict' keyword");
2268: return (-1);
2269: }
2270: }
2271:
2272: return (0);
2273: }
2274:
2275: /*
2276: * rcs_parse_branches()
2277: *
2278: * Parse the list of branches following a `branches' keyword in a delta.
2279: * Returns 0 on success, or -1 on failure.
2280: */
2281: static int
2282: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2283: {
2284: int type;
2285: struct rcs_branch *brp;
2286:
2287: for (;;) {
2288: type = rcs_gettok(rfp);
2289: if (type == RCS_TOK_SCOLON)
2290: break;
2291:
2292: if (type != RCS_TOK_NUM) {
2293: rcs_errno = RCS_ERR_PARSE;
2294: warnx("unexpected token `%s' in list of branches",
2295: RCS_TOKSTR(rfp));
2296: return (-1);
2297: }
2298:
2299: brp = xmalloc(sizeof(*brp));
2300: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
2301: if (brp->rb_num == NULL) {
2302: xfree(brp);
2303: return (-1);
2304: }
2305:
2306: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2307: }
2308:
2309: return (0);
2310: }
2311:
2312: /*
2313: * rcs_freedelta()
2314: *
2315: * Free the contents of a delta structure.
2316: */
2317: static void
2318: rcs_freedelta(struct rcs_delta *rdp)
2319: {
2320: struct rcs_branch *rb;
2321:
2322: if (rdp->rd_num != NULL)
2323: rcsnum_free(rdp->rd_num);
2324: if (rdp->rd_next != NULL)
2325: rcsnum_free(rdp->rd_next);
2326:
2327: if (rdp->rd_author != NULL)
2328: xfree(rdp->rd_author);
2329: if (rdp->rd_locker != NULL)
2330: xfree(rdp->rd_locker);
2331: if (rdp->rd_state != NULL)
2332: xfree(rdp->rd_state);
2333: if (rdp->rd_log != NULL)
2334: xfree(rdp->rd_log);
2335: if (rdp->rd_text != NULL)
2336: xfree(rdp->rd_text);
2337:
2338: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2339: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2340: rcsnum_free(rb->rb_num);
2341: xfree(rb);
2342: }
2343:
2344: xfree(rdp);
2345: }
2346:
2347: /*
2348: * rcs_freepdata()
2349: *
2350: * Free the contents of the parser data structure.
2351: */
2352: static void
2353: rcs_freepdata(struct rcs_pdata *pd)
2354: {
2355: if (pd->rp_file != NULL)
2356: (void)fclose(pd->rp_file);
2357: if (pd->rp_buf != NULL)
2358: xfree(pd->rp_buf);
2359: xfree(pd);
2360: }
2361:
2362: /*
2363: * rcs_gettok()
2364: *
2365: * Get the next RCS token from the string <str>.
2366: */
2367: static int
2368: rcs_gettok(RCSFILE *rfp)
2369: {
2370: u_int i;
2371: int ch, last, type;
2372: size_t len;
2373: char *bp;
2374: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2375:
2376: type = RCS_TOK_ERR;
2377: bp = pdp->rp_buf;
2378: pdp->rp_tlen = 0;
2379: *bp = '\0';
2380:
2381: if (pdp->rp_pttype != RCS_TOK_ERR) {
2382: type = pdp->rp_pttype;
1.5 ray 2383: if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
2384: pdp->rp_blen)
2385: errx(1, "rcs_gettok: strlcpy");
1.1 joris 2386: pdp->rp_pttype = RCS_TOK_ERR;
2387: return (type);
2388: }
2389:
2390: /* skip leading whitespace */
2391: /* XXX we must skip backspace too for compatibility, should we? */
2392: do {
2393: ch = getc(pdp->rp_file);
2394: if (ch == '\n')
2395: pdp->rp_lines++;
2396: } while (isspace(ch));
2397:
2398: if (ch == EOF) {
2399: type = RCS_TOK_EOF;
2400: } else if (ch == ';') {
2401: type = RCS_TOK_SCOLON;
2402: } else if (ch == ':') {
2403: type = RCS_TOK_COLON;
2404: } else if (isalpha(ch)) {
2405: type = RCS_TOK_ID;
2406: *(bp++) = ch;
2407: for (;;) {
2408: ch = getc(pdp->rp_file);
2409: if (!isalnum(ch) && ch != '_' && ch != '-' &&
2410: ch != '/') {
2411: ungetc(ch, pdp->rp_file);
2412: break;
2413: }
2414: *(bp++) = ch;
2415: pdp->rp_tlen++;
2416: if (bp == pdp->rp_bufend - 1) {
2417: len = bp - pdp->rp_buf;
2418: rcs_growbuf(rfp);
2419: bp = pdp->rp_buf + len;
2420: }
2421: }
2422: *bp = '\0';
2423:
2424: if (type != RCS_TOK_ERR) {
2425: for (i = 0; i < RCS_NKEYS; i++) {
2426: if (strcmp(rcs_keys[i].rk_str,
2427: pdp->rp_buf) == 0) {
2428: type = rcs_keys[i].rk_id;
2429: break;
2430: }
2431: }
2432: }
2433: } else if (ch == '@') {
2434: /* we have a string */
2435: type = RCS_TOK_STRING;
2436: for (;;) {
2437: ch = getc(pdp->rp_file);
2438: if (ch == '@') {
2439: ch = getc(pdp->rp_file);
2440: if (ch != '@') {
2441: ungetc(ch, pdp->rp_file);
2442: break;
2443: }
2444: } else if (ch == '\n')
2445: pdp->rp_lines++;
2446:
2447: *(bp++) = ch;
2448: pdp->rp_tlen++;
2449: if (bp == pdp->rp_bufend - 1) {
2450: len = bp - pdp->rp_buf;
2451: rcs_growbuf(rfp);
2452: bp = pdp->rp_buf + len;
2453: }
2454: }
2455:
2456: *bp = '\0';
2457: } else if (isdigit(ch)) {
2458: *(bp++) = ch;
2459: last = ch;
2460: type = RCS_TOK_NUM;
2461:
2462: for (;;) {
2463: ch = getc(pdp->rp_file);
2464: if (bp == pdp->rp_bufend)
2465: break;
2466: if (!isdigit(ch) && ch != '.') {
2467: ungetc(ch, pdp->rp_file);
2468: break;
2469: }
2470:
2471: if (last == '.' && ch == '.') {
2472: type = RCS_TOK_ERR;
2473: break;
2474: }
2475: last = ch;
2476: *(bp++) = ch;
2477: pdp->rp_tlen++;
2478: }
2479: *bp = '\0';
2480: }
2481:
2482: return (type);
2483: }
2484:
2485: /*
2486: * rcs_pushtok()
2487: *
2488: * Push a token back in the parser's token buffer.
2489: */
2490: static int
2491: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2492: {
2493: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2494:
2495: if (pdp->rp_pttype != RCS_TOK_ERR)
2496: return (-1);
2497:
2498: pdp->rp_pttype = type;
1.5 ray 2499: if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
2500: sizeof(pdp->rp_ptok))
2501: errx(1, "rcs_pushtok: strlcpy");
1.1 joris 2502: return (0);
2503: }
2504:
2505:
2506: /*
2507: * rcs_growbuf()
2508: *
2509: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2510: * RCS_BUFEXTSIZE.
2511: * In case of failure, the original buffer is left unmodified.
2512: */
2513: static void
2514: rcs_growbuf(RCSFILE *rf)
2515: {
2516: void *tmp;
2517: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2518:
2519: tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
2520: pdp->rp_buf = tmp;
2521: pdp->rp_blen += RCS_BUFEXTSIZE;
2522: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
2523: }
2524:
2525: /*
2526: * rcs_strprint()
2527: *
2528: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2529: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2530: * binary data.
2531: */
2532: static void
2533: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2534: {
2535: const u_char *ap, *ep, *sp;
2536:
2537: if (slen == 0)
2538: return;
2539:
2540: ep = str + slen - 1;
2541:
2542: for (sp = str; sp <= ep;) {
2543: ap = memchr(sp, '@', ep - sp);
2544: if (ap == NULL)
2545: ap = ep;
2546: (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2547:
2548: if (*ap == '@')
2549: putc('@', stream);
2550: sp = ap + 1;
2551: }
2552: }
2553:
2554: /*
2555: * rcs_expand_keywords()
2556: *
2557: * Return expansion any RCS keywords in <data>
2558: *
2559: * On error, return NULL.
2560: */
2561: static char *
2562: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
2563: size_t len, int mode)
2564: {
2565: ptrdiff_t c_offset, sizdiff, start_offset;
2566: size_t i;
2567: int kwtype;
2568: u_int j, found;
2569: char *c, *kwstr, *start, *end, *tbuf;
2570: char expbuf[256], buf[256];
2571: struct tm tb;
2572: char *fmt;
2573:
2574: kwtype = 0;
2575: kwstr = NULL;
2576: i = 0;
2577:
2578: /*
2579: * -z support for RCS
2580: */
2581: tb = rdp->rd_date;
2582: if (timezone_flag != NULL)
2583: rcs_set_tz(timezone_flag, rdp, &tb);
2584:
2585: /*
2586: * Keyword formats:
2587: * $Keyword$
2588: * $Keyword: value$
2589: */
2590: for (c = data; *c != '\0' && i < len; c++) {
2591: if (*c == '$') {
2592: /* remember start of this possible keyword */
2593: start = c;
2594: start_offset = start - data;
2595:
2596: /* first following character has to be alphanumeric */
2597: c++;
2598: if (!isalpha(*c)) {
2599: c = start;
2600: continue;
2601: }
2602:
2603: /* look for any matching keywords */
2604: found = 0;
2605: for (j = 0; j < RCS_NKWORDS; j++) {
2606: if (!strncmp(c, rcs_expkw[j].kw_str,
2607: strlen(rcs_expkw[j].kw_str))) {
2608: found = 1;
2609: kwstr = rcs_expkw[j].kw_str;
2610: kwtype = rcs_expkw[j].kw_type;
2611: break;
2612: }
2613: }
2614:
2615: /* unknown keyword, continue looking */
2616: if (found == 0) {
2617: c = start;
2618: continue;
2619: }
2620:
2621: /* next character has to be ':' or '$' */
2622: c += strlen(kwstr);
2623: if (*c != ':' && *c != '$') {
2624: c = start;
2625: continue;
2626: }
2627:
2628: /*
2629: * if the next character was ':' we need to look for
2630: * an '$' before the end of the line to be sure it is
2631: * in fact a keyword.
2632: */
2633: if (*c == ':') {
2634: while (*c++) {
2635: if (*c == '$' || *c == '\n')
2636: break;
2637: }
2638:
2639: if (*c != '$') {
2640: c = start;
2641: continue;
2642: }
2643: }
2644: c_offset = c - data;
2645: end = c + 1;
2646:
2647: /* start constructing the expansion */
2648: expbuf[0] = '\0';
2649:
2650: if (mode & RCS_KWEXP_NAME) {
1.5 ray 2651: if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf) ||
2652: strlcat(expbuf, kwstr, sizeof(expbuf)) >= sizeof(expbuf))
2653: errx(1, "rcs_expand_keywords: string truncated");
2654: if ((mode & RCS_KWEXP_VAL) &&
2655: strlcat(expbuf, ": ", sizeof(expbuf)) >= sizeof(expbuf))
2656: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2657: }
2658:
2659: /*
2660: * order matters because of RCS_KW_ID and
2661: * RCS_KW_HEADER here
2662: */
2663: if (mode & RCS_KWEXP_VAL) {
2664: if (kwtype & RCS_KW_RCSFILE) {
2665: if (!(kwtype & RCS_KW_FULLPATH))
1.5 ray 2666: (void)strlcat(expbuf, basename(rcsfile), sizeof(expbuf));
1.1 joris 2667: else
1.5 ray 2668: (void)strlcat(expbuf, rcsfile, sizeof(expbuf));
2669: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2670: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2671: }
2672:
2673: if (kwtype & RCS_KW_REVISION) {
1.5 ray 2674: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
2675: if (strlcat(buf, " ", sizeof(buf)) >= sizeof(buf) ||
2676: strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(buf))
2677: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2678: }
2679:
2680: if (kwtype & RCS_KW_DATE) {
2681: if (timezone_flag != NULL)
2682: fmt = "%Y/%m/%d %H:%M:%S%z ";
2683: else
2684: fmt = "%Y/%m/%d %H:%M:%S ";
2685:
2686: strftime(buf, sizeof(buf), fmt, &tb);
1.5 ray 2687: if (strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(expbuf))
2688: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2689: }
2690:
2691: if (kwtype & RCS_KW_AUTHOR) {
1.5 ray 2692: if (strlcat(expbuf, rdp->rd_author, sizeof(expbuf)) >= sizeof(expbuf) ||
2693: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2694: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2695: }
2696:
2697: if (kwtype & RCS_KW_STATE) {
1.5 ray 2698: if (strlcat(expbuf, rdp->rd_state, sizeof(expbuf)) >= sizeof(expbuf) ||
2699: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2700: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2701: }
2702:
2703: /* order does not matter anymore below */
2704: if (kwtype & RCS_KW_LOG)
1.5 ray 2705: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2706: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2707:
2708: if (kwtype & RCS_KW_SOURCE) {
1.5 ray 2709: if (strlcat(expbuf, rcsfile, sizeof(expbuf)) >= sizeof(expbuf) ||
2710: strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2711: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2712: }
2713:
2714: if (kwtype & RCS_KW_NAME)
1.5 ray 2715: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2716: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2717: }
2718:
2719: /* end the expansion */
2720: if (mode & RCS_KWEXP_NAME)
1.5 ray 2721: if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf))
2722: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2723:
2724: sizdiff = strlen(expbuf) - (end - start);
2725: tbuf = xstrdup(end);
2726: /* only realloc if we have to */
2727: if (sizdiff > 0) {
2728: char *newdata;
2729:
2730: len += sizdiff;
2731: newdata = xrealloc(data, 1, len);
2732: data = newdata;
2733: /*
2734: * ensure string pointers are not invalidated
2735: * after realloc()
2736: */
2737: start = data + start_offset;
2738: c = data + c_offset;
2739: }
1.5 ray 2740: if (strlcpy(start, expbuf, len) >= len ||
2741: strlcat(data, tbuf, len) >= len)
2742: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2743: xfree(tbuf);
2744: i += strlen(expbuf);
2745: }
2746: }
2747:
2748: return (data);
2749: }
2750:
2751: /*
2752: * rcs_deltatext_set()
2753: *
2754: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
2755: * Returns -1 on error, 0 on success.
2756: */
2757: int
2758: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, const char *dtext)
2759: {
2760: size_t len;
2761: struct rcs_delta *rdp;
2762:
2763: /* Write operations require full parsing */
2764: rcs_parse_deltatexts(rfp, NULL);
2765:
2766: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2767: return (-1);
2768:
2769: if (rdp->rd_text != NULL)
2770: xfree(rdp->rd_text);
2771:
2772: len = strlen(dtext);
2773: if (len != 0) {
2774: /* XXX - use xstrdup() if rd_text changes to char *. */
2775: rdp->rd_text = xmalloc(len + 1);
2776: rdp->rd_tlen = len;
2777: (void)memcpy(rdp->rd_text, dtext, len + 1);
2778: } else {
2779: rdp->rd_text = NULL;
2780: rdp->rd_tlen = 0;
2781: }
2782:
2783: return (0);
2784: }
2785:
2786: /*
2787: * rcs_rev_setlog()
2788: *
2789: * Sets the log message of revision <rev> to <logtext>
2790: */
2791: int
2792: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2793: {
2794: struct rcs_delta *rdp;
2795: char buf[16];
2796:
2797: rcsnum_tostr(rev, buf, sizeof(buf));
2798:
2799: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2800: return (-1);
2801:
2802: if (rdp->rd_log != NULL)
2803: xfree(rdp->rd_log);
2804:
2805: rdp->rd_log = xstrdup(logtext);
2806: rfp->rf_flags &= ~RCS_SYNCED;
2807: return (0);
2808: }
2809: /*
2810: * rcs_rev_getdate()
2811: *
2812: * Get the date corresponding to a given revision.
2813: * Returns the date on success, -1 on failure.
2814: */
2815: time_t
2816: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2817: {
2818: struct rcs_delta *rdp;
2819:
2820: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2821: return (-1);
2822:
2823: return (mktime(&rdp->rd_date));
2824: }
2825:
2826: /*
2827: * rcs_state_set()
2828: *
2829: * Sets the state of revision <rev> to <state>
2830: * NOTE: default state is 'Exp'. States may not contain spaces.
2831: *
2832: * Returns -1 on failure, 0 on success.
2833: */
2834: int
2835: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2836: {
2837: struct rcs_delta *rdp;
2838:
2839: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2840: return (-1);
2841:
2842: if (rdp->rd_state != NULL)
2843: xfree(rdp->rd_state);
2844:
2845: rdp->rd_state = xstrdup(state);
2846:
2847: rfp->rf_flags &= ~RCS_SYNCED;
2848:
2849: return (0);
2850: }
2851:
2852: /*
2853: * rcs_state_check()
2854: *
2855: * Check if string <state> is valid.
2856: *
2857: * Returns 0 if the string is valid, -1 otherwise.
2858: */
2859: int
2860: rcs_state_check(const char *state)
2861: {
2862: if (strchr(state, ' ') != NULL)
2863: return (-1);
2864:
2865: return (0);
2866: }
2867:
2868: /*
2869: * rcs_state_get()
2870: *
2871: * Get the state for a given revision of a specified RCSFILE.
2872: *
2873: * Returns NULL on failure.
2874: */
2875: const char *
2876: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2877: {
2878: struct rcs_delta *rdp;
2879:
2880: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2881: return (NULL);
2882:
2883: return (rdp->rd_state);
2884: }
2885:
2886: /*
2887: * rcs_kwexp_buf()
2888: *
2889: * Do keyword expansion on a buffer if necessary
2890: *
2891: */
2892: BUF *
2893: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
2894: {
2895: struct rcs_delta *rdp;
2896: char *expanded, *tbuf;
2897: int expmode;
2898: size_t len;
2899:
2900: /*
2901: * Do keyword expansion if required.
2902: */
2903: if (rf->rf_expand != NULL)
2904: expmode = rcs_kwexp_get(rf);
2905: else
2906: expmode = RCS_KWEXP_DEFAULT;
2907:
2908: if (!(expmode & RCS_KWEXP_NONE)) {
2909: if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.5 ray 2910: errx(1, "could not fetch revision");
1.1 joris 2911: rcs_buf_putc(bp, '\0');
2912: len = rcs_buf_len(bp);
2913: tbuf = rcs_buf_release(bp);
2914: expanded = rcs_expand_keywords(rf->rf_path, rdp,
2915: tbuf, len, expmode);
2916: bp = rcs_buf_alloc(len, BUF_AUTOEXT);
2917: rcs_buf_set(bp, expanded, strlen(expanded), 0);
2918: xfree(expanded);
2919: }
2920: return (bp);
2921: }