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