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