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