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