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