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