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