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