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