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