Annotation of src/usr.bin/cvs/rcs.c, Revision 1.56
1.56 ! joris 1: /* $OpenBSD: rcs.c,v 1.55 2005/05/31 08:58:48 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:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
1.55 xsa 30: #include <ctype.h>
31: #include <errno.h>
1.52 jfb 32: #include <pwd.h>
1.55 xsa 33: #include <stdarg.h>
1.1 jfb 34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <string.h>
1.55 xsa 37: #include <unistd.h>
1.1 jfb 38:
1.55 xsa 39: #include "log.h"
1.1 jfb 40: #include "rcs.h"
1.39 joris 41: #include "strtab.h"
1.1 jfb 42:
1.26 jfb 43: #define RCS_BUFSIZE 16384
1.18 jfb 44: #define RCS_BUFEXTSIZE 8192
1.1 jfb 45:
46:
47: /* RCS token types */
48: #define RCS_TOK_ERR -1
49: #define RCS_TOK_EOF 0
50: #define RCS_TOK_NUM 1
51: #define RCS_TOK_ID 2
52: #define RCS_TOK_STRING 3
53: #define RCS_TOK_SCOLON 4
54: #define RCS_TOK_COLON 5
55:
56:
57: #define RCS_TOK_HEAD 8
58: #define RCS_TOK_BRANCH 9
59: #define RCS_TOK_ACCESS 10
60: #define RCS_TOK_SYMBOLS 11
61: #define RCS_TOK_LOCKS 12
62: #define RCS_TOK_COMMENT 13
63: #define RCS_TOK_EXPAND 14
64: #define RCS_TOK_DATE 15
65: #define RCS_TOK_AUTHOR 16
66: #define RCS_TOK_STATE 17
67: #define RCS_TOK_NEXT 18
68: #define RCS_TOK_BRANCHES 19
69: #define RCS_TOK_DESC 20
70: #define RCS_TOK_LOG 21
71: #define RCS_TOK_TEXT 22
72: #define RCS_TOK_STRICT 23
73:
74: #define RCS_ISKEY(t) (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
75:
76:
77: #define RCS_NOSCOL 0x01 /* no terminating semi-colon */
78: #define RCS_VOPT 0x02 /* value is optional */
79:
80:
81: /* opaque parse data */
82: struct rcs_pdata {
1.18 jfb 83: u_int rp_lines;
1.1 jfb 84:
85: char *rp_buf;
86: size_t rp_blen;
1.18 jfb 87: char *rp_bufend;
1.42 jfb 88: size_t rp_tlen;
1.1 jfb 89:
90: /* pushback token buffer */
91: char rp_ptok[128];
92: int rp_pttype; /* token type, RCS_TOK_ERR if no token */
93:
94: FILE *rp_file;
95: };
96:
97:
98: struct rcs_line {
99: char *rl_line;
100: int rl_lineno;
101: TAILQ_ENTRY(rcs_line) rl_list;
102: };
1.5 vincent 103: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1 jfb 104:
105: struct rcs_foo {
106: int rl_nblines;
107: char *rl_data;
1.5 vincent 108: struct rcs_tqh rl_lines;
1.1 jfb 109: };
110:
111: #define RCS_TOKSTR(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
1.42 jfb 112: #define RCS_TOKLEN(rfp) ((struct rcs_pdata *)rfp->rf_pdata)->rp_tlen
1.1 jfb 113:
114:
1.47 jfb 115:
116: /* invalid characters in RCS symbol names */
1.49 jfb 117: static const char rcs_sym_invch[] = RCS_SYM_INVALCHAR;
1.47 jfb 118:
1.51 jfb 119:
120: /* comment leaders, depending on the file's suffix */
121: static const struct rcs_comment {
122: const char *rc_suffix;
123: const char *rc_cstr;
124: } rcs_comments[] = {
125: { "1", ".\\\" " },
126: { "2", ".\\\" " },
127: { "3", ".\\\" " },
128: { "4", ".\\\" " },
129: { "5", ".\\\" " },
130: { "6", ".\\\" " },
131: { "7", ".\\\" " },
132: { "8", ".\\\" " },
133: { "9", ".\\\" " },
134: { "a", "-- " }, /* Ada */
135: { "ada", "-- " },
136: { "adb", "-- " },
137: { "asm", ";; " }, /* assembler (MS-DOS) */
138: { "ads", "-- " }, /* Ada */
139: { "bat", ":: " }, /* batch (MS-DOS) */
140: { "body", "-- " }, /* Ada */
141: { "c", " * " }, /* C */
142: { "c++", "// " }, /* C++ */
143: { "cc", "// " },
144: { "cpp", "// " },
145: { "cxx", "// " },
146: { "m", "// " }, /* Objective-C */
147: { "cl", ";;; " }, /* Common Lisp */
148: { "cmd", ":: " }, /* command (OS/2) */
149: { "cmf", "c " }, /* CM Fortran */
150: { "csh", "# " }, /* shell */
151: { "e", "# " }, /* efl */
152: { "epsf", "% " }, /* encapsulated postscript */
153: { "epsi", "% " }, /* encapsulated postscript */
154: { "el", "; " }, /* Emacs Lisp */
155: { "f", "c " }, /* Fortran */
156: { "for", "c " },
157: { "h", " * " }, /* C-header */
158: { "hh", "// " }, /* C++ header */
159: { "hpp", "// " },
160: { "hxx", "// " },
161: { "in", "# " }, /* for Makefile.in */
162: { "l", " * " }, /* lex */
163: { "mac", ";; " }, /* macro (DEC-10, MS-DOS, PDP-11, VMS, etc) */
164: { "mak", "# " }, /* makefile, e.g. Visual C++ */
165: { "me", ".\\\" " }, /* me-macros t/nroff */
166: { "ml", "; " }, /* mocklisp */
167: { "mm", ".\\\" " }, /* mm-macros t/nroff */
168: { "ms", ".\\\" " }, /* ms-macros t/nroff */
169: { "man", ".\\\" " }, /* man-macros t/nroff */
170: { "p", " * " }, /* pascal */
171: { "pas", " * " },
1.52 jfb 172: { "pl", "# " }, /* Perl (conflict with Prolog) */
173: { "pm", "# " }, /* Perl module */
1.51 jfb 174: { "ps", "% " }, /* postscript */
175: { "psw", "% " }, /* postscript wrap */
176: { "pswm", "% " }, /* postscript wrap */
177: { "r", "# " }, /* ratfor */
178: { "rc", " * " }, /* Microsoft Windows resource file */
179: { "red", "% " }, /* psl/rlisp */
180: { "sh", "# " }, /* shell */
181: { "sl", "% " }, /* psl */
182: { "spec", "-- " }, /* Ada */
183: { "tex", "% " }, /* tex */
184: { "y", " * " }, /* yacc */
185: { "ye", " * " }, /* yacc-efl */
186: { "yr", " * " }, /* yacc-ratfor */
187: };
188:
189: #define NB_COMTYPES (sizeof(rcs_comments)/sizeof(rcs_comments[0]))
190:
1.33 jfb 191: #ifdef notyet
1.20 jfb 192: static struct rcs_kfl {
193: char rk_char;
194: int rk_val;
195: } rcs_kflags[] = {
196: { 'k', RCS_KWEXP_NAME },
197: { 'v', RCS_KWEXP_VAL },
198: { 'l', RCS_KWEXP_LKR },
199: { 'o', RCS_KWEXP_OLD },
200: { 'b', RCS_KWEXP_NONE },
201: };
1.33 jfb 202: #endif
1.20 jfb 203:
1.1 jfb 204: static struct rcs_key {
205: char rk_str[16];
206: int rk_id;
207: int rk_val;
208: int rk_flags;
209: } rcs_keys[] = {
210: { "access", RCS_TOK_ACCESS, RCS_TOK_ID, RCS_VOPT },
1.41 jfb 211: { "author", RCS_TOK_AUTHOR, RCS_TOK_ID, 0 },
1.1 jfb 212: { "branch", RCS_TOK_BRANCH, RCS_TOK_NUM, RCS_VOPT },
213: { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM, RCS_VOPT },
214: { "comment", RCS_TOK_COMMENT, RCS_TOK_STRING, RCS_VOPT },
215: { "date", RCS_TOK_DATE, RCS_TOK_NUM, 0 },
216: { "desc", RCS_TOK_DESC, RCS_TOK_STRING, RCS_NOSCOL },
217: { "expand", RCS_TOK_EXPAND, RCS_TOK_STRING, RCS_VOPT },
218: { "head", RCS_TOK_HEAD, RCS_TOK_NUM, RCS_VOPT },
219: { "locks", RCS_TOK_LOCKS, RCS_TOK_ID, 0 },
220: { "log", RCS_TOK_LOG, RCS_TOK_STRING, RCS_NOSCOL },
221: { "next", RCS_TOK_NEXT, RCS_TOK_NUM, RCS_VOPT },
1.41 jfb 222: { "state", RCS_TOK_STATE, RCS_TOK_ID, RCS_VOPT },
1.1 jfb 223: { "strict", RCS_TOK_STRICT, 0, 0, },
224: { "symbols", RCS_TOK_SYMBOLS, 0, 0 },
225: { "text", RCS_TOK_TEXT, RCS_TOK_STRING, RCS_NOSCOL },
226: };
227:
1.18 jfb 228: #define RCS_NKEYS (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1 jfb 229:
1.33 jfb 230: #ifdef notyet
231: /*
232: * Keyword expansion table
233: */
234: static struct rcs_kw {
235: char kw_str[16];
236: } rcs_expkw[] = {
237: { "Author" },
238: { "Date" },
239: { "Header" },
240: { "Id" },
241: { "Log" },
242: { "Name" },
243: { "RCSfile" },
244: { "Revision" },
245: { "Source" },
246: { "State" }
247: };
248: #endif
249:
1.32 jfb 250: static const char *rcs_errstrs[] = {
251: "No error",
252: "No such entry",
253: "Duplicate entry found",
254: "Bad RCS number",
1.48 jfb 255: "Invalid RCS symbol",
256: "Parse error",
1.32 jfb 257: };
258:
259: #define RCS_NERR (sizeof(rcs_errstrs)/sizeof(rcs_errstrs[0]))
260:
261:
262: int rcs_errno = RCS_ERR_NOERR;
263:
1.1 jfb 264:
1.27 jfb 265: static int rcs_write (RCSFILE *);
1.26 jfb 266: static int rcs_parse (RCSFILE *);
267: static int rcs_parse_admin (RCSFILE *);
268: static int rcs_parse_delta (RCSFILE *);
269: static int rcs_parse_deltatext (RCSFILE *);
270:
271: static int rcs_parse_access (RCSFILE *);
272: static int rcs_parse_symbols (RCSFILE *);
273: static int rcs_parse_locks (RCSFILE *);
274: static int rcs_parse_branches (RCSFILE *, struct rcs_delta *);
275: static void rcs_freedelta (struct rcs_delta *);
276: static void rcs_freepdata (struct rcs_pdata *);
277: static int rcs_gettok (RCSFILE *);
278: static int rcs_pushtok (RCSFILE *, const char *, int);
279: static int rcs_growbuf (RCSFILE *);
280: static int rcs_patch_lines (struct rcs_foo *, struct rcs_foo *);
1.42 jfb 281: static int rcs_strprint (const u_char *, size_t, FILE *);
1.26 jfb 282:
1.43 jfb 283: static struct rcs_delta* rcs_findrev (RCSFILE *, const RCSNUM *);
1.26 jfb 284: static struct rcs_foo* rcs_splitlines (const char *);
285: static void rcs_freefoo (struct rcs_foo *);
286:
287:
1.1 jfb 288: /*
289: * rcs_open()
290: *
291: * Open a file containing RCS-formatted information. The file's path is
1.26 jfb 292: * given in <path>, and the opening flags are given in <flags>, which is either
293: * RCS_READ, RCS_WRITE, or RCS_RDWR. If the open requests write access and
294: * the file does not exist, the RCS_CREATE flag must also be given, in which
295: * case it will be created with the mode specified in a third argument of
296: * type mode_t. If the file exists and RCS_CREATE is passed, the open will
297: * fail.
1.1 jfb 298: * Returns a handle to the opened file on success, or NULL on failure.
299: */
300: RCSFILE*
1.26 jfb 301: rcs_open(const char *path, int flags, ...)
1.1 jfb 302: {
1.26 jfb 303: int ret;
304: mode_t fmode;
1.1 jfb 305: RCSFILE *rfp;
306: struct stat st;
1.26 jfb 307: va_list vap;
308:
309: fmode = 0;
310: flags &= 0xffff; /* ditch any internal flags */
1.1 jfb 311:
1.26 jfb 312: if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
313: if (flags & RCS_CREATE) {
314: va_start(vap, flags);
315: fmode = va_arg(vap, mode_t);
316: va_end(vap);
317: } else {
1.50 jfb 318: rcs_errno = RCS_ERR_ERRNO;
1.26 jfb 319: cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
320: return (NULL);
321: }
322: } else if ((ret == 0) && (flags & RCS_CREATE)) {
323: cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1 jfb 324: return (NULL);
325: }
326:
1.26 jfb 327: if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1 jfb 328: cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
1.50 jfb 329: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 330: return (NULL);
331: }
332: memset(rfp, 0, sizeof(*rfp));
333:
1.26 jfb 334: if ((rfp->rf_path = strdup(path)) == NULL) {
1.50 jfb 335: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 336: cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26 jfb 337: free(rfp);
1.1 jfb 338: return (NULL);
339: }
340:
341: rfp->rf_ref = 1;
1.26 jfb 342: rfp->rf_flags = flags | RCS_SLOCK;
343: rfp->rf_mode = fmode;
1.1 jfb 344:
345: TAILQ_INIT(&(rfp->rf_delta));
1.29 jfb 346: TAILQ_INIT(&(rfp->rf_access));
1.1 jfb 347: TAILQ_INIT(&(rfp->rf_symbols));
348: TAILQ_INIT(&(rfp->rf_locks));
349:
1.26 jfb 350: if (rfp->rf_flags & RCS_CREATE) {
351: } else if (rcs_parse(rfp) < 0) {
1.1 jfb 352: rcs_close(rfp);
353: return (NULL);
354: }
355:
356: return (rfp);
357: }
358:
359: /*
360: * rcs_close()
361: *
362: * Close an RCS file handle.
363: */
364: void
365: rcs_close(RCSFILE *rfp)
366: {
367: struct rcs_delta *rdp;
1.13 jfb 368: struct rcs_lock *rlp;
369: struct rcs_sym *rsp;
1.1 jfb 370:
371: if (rfp->rf_ref > 1) {
372: rfp->rf_ref--;
373: return;
374: }
375:
1.26 jfb 376: if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
377: rcs_write(rfp);
378:
1.1 jfb 379: while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
380: rdp = TAILQ_FIRST(&(rfp->rf_delta));
381: TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
382: rcs_freedelta(rdp);
383: }
384:
1.13 jfb 385: while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
386: rsp = TAILQ_FIRST(&(rfp->rf_symbols));
387: TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
388: rcsnum_free(rsp->rs_num);
1.39 joris 389: cvs_strfree(rsp->rs_name);
1.13 jfb 390: free(rsp);
391: }
392:
393: while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
394: rlp = TAILQ_FIRST(&(rfp->rf_locks));
395: TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
396: rcsnum_free(rlp->rl_num);
397: free(rlp);
398: }
399:
1.1 jfb 400: if (rfp->rf_head != NULL)
401: rcsnum_free(rfp->rf_head);
1.11 joris 402: if (rfp->rf_branch != NULL)
403: rcsnum_free(rfp->rf_branch);
1.1 jfb 404:
405: if (rfp->rf_path != NULL)
406: free(rfp->rf_path);
407: if (rfp->rf_comment != NULL)
1.39 joris 408: cvs_strfree(rfp->rf_comment);
1.1 jfb 409: if (rfp->rf_expand != NULL)
1.39 joris 410: cvs_strfree(rfp->rf_expand);
1.1 jfb 411: if (rfp->rf_desc != NULL)
1.39 joris 412: cvs_strfree(rfp->rf_desc);
1.1 jfb 413: free(rfp);
414: }
415:
416: /*
417: * rcs_write()
418: *
419: * Write the contents of the RCS file handle <rfp> to disk in the file whose
420: * path is in <rf_path>.
421: * Returns 0 on success, or -1 on failure.
422: */
1.27 jfb 423: static int
1.1 jfb 424: rcs_write(RCSFILE *rfp)
425: {
426: FILE *fp;
1.42 jfb 427: char buf[1024], numbuf[64];
1.29 jfb 428: struct rcs_access *ap;
1.1 jfb 429: struct rcs_sym *symp;
1.46 jfb 430: struct rcs_branch *brp;
1.1 jfb 431: struct rcs_delta *rdp;
432:
1.28 jfb 433: if (rfp->rf_flags & RCS_SYNCED)
1.1 jfb 434: return (0);
435:
1.29 jfb 436: if ((fp = fopen(rfp->rf_path, "w")) == NULL) {
1.50 jfb 437: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 438: cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
439: rfp->rf_path);
440: return (-1);
441: }
442:
1.28 jfb 443: if (rfp->rf_head != NULL)
444: rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
445: else
446: numbuf[0] = '\0';
447:
1.1 jfb 448: fprintf(fp, "head\t%s;\n", numbuf);
1.35 jfb 449:
450: if (rfp->rf_branch != NULL) {
451: rcsnum_tostr(rfp->rf_branch, numbuf, sizeof(numbuf));
452: fprintf(fp, "branch\t%s;\n", numbuf);
453: }
454:
1.29 jfb 455: fputs("access", fp);
456: TAILQ_FOREACH(ap, &(rfp->rf_access), ra_list) {
457: fprintf(fp, "\n\t%s", ap->ra_name);
458: }
459: fputs(";\n", fp);
1.1 jfb 460:
461: fprintf(fp, "symbols\n");
462: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
463: rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
464: snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
465: fprintf(fp, "\t%s", buf);
466: if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
467: fputc('\n', fp);
468: }
469: fprintf(fp, ";\n");
470:
471: fprintf(fp, "locks;");
472:
1.26 jfb 473: if (rfp->rf_flags & RCS_SLOCK)
1.1 jfb 474: fprintf(fp, " strict;");
475: fputc('\n', fp);
476:
1.42 jfb 477: if (rfp->rf_comment != NULL) {
478: fputs("comment\t@", fp);
479: rcs_strprint(rfp->rf_comment, strlen(rfp->rf_comment), fp);
480: fputs("@;\n", fp);
481: }
1.1 jfb 482:
1.42 jfb 483: if (rfp->rf_expand != NULL) {
484: fputs("expand @", fp);
485: rcs_strprint(rfp->rf_expand, strlen(rfp->rf_expand), fp);
486: fputs("@;\n", fp);
487: }
1.1 jfb 488:
1.46 jfb 489: fputs("\n\n", fp);
1.1 jfb 490:
491: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
492: fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
493: sizeof(numbuf)));
494: fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
1.44 jfb 495: rdp->rd_date.tm_year + 1900, rdp->rd_date.tm_mon + 1,
1.1 jfb 496: rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
497: rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
498: fprintf(fp, "\tauthor %s;\tstate %s;\n",
499: rdp->rd_author, rdp->rd_state);
1.46 jfb 500: fputs("branches", fp);
501: TAILQ_FOREACH(brp, &(rdp->rd_branches), rb_list) {
502: fprintf(fp, " %s", rcsnum_tostr(brp->rb_num, numbuf,
503: sizeof(numbuf)));
504: }
505: fputs(";\n", fp);
1.1 jfb 506: fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
507: numbuf, sizeof(numbuf)));
508: }
509:
1.42 jfb 510: fputs("\ndesc\n@", fp);
511: if (rfp->rf_desc != NULL)
512: rcs_strprint(rfp->rf_desc, strlen(rfp->rf_desc), fp);
513: fputs("@\n\n", fp);
1.1 jfb 514:
515: /* deltatexts */
516: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
517: fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
518: sizeof(numbuf)));
1.42 jfb 519: fputs("log\n@", fp);
520: rcs_strprint(rdp->rd_log, strlen(rdp->rd_log), fp);
521: fputs("@\ntext\n@", fp);
522: rcs_strprint(rdp->rd_text, rdp->rd_tlen, fp);
523: fputs("@\n\n", fp);
1.1 jfb 524: }
525: fclose(fp);
526:
1.26 jfb 527: rfp->rf_flags |= RCS_SYNCED;
1.1 jfb 528:
529: return (0);
530: }
531:
532: /*
1.43 jfb 533: * rcs_head_get()
534: *
535: * Retrieve the revision number of the head revision for the RCS file <file>.
536: */
537: const RCSNUM*
538: rcs_head_get(RCSFILE *file)
539: {
540: return (file->rf_head);
541: }
542:
543: /*
544: * rcs_head_set()
545: *
546: * Set the revision number of the head revision for the RCS file <file> to
547: * <rev>, which must reference a valid revision within the file.
548: */
549: int
550: rcs_head_set(RCSFILE *file, const RCSNUM *rev)
551: {
552: struct rcs_delta *rd;
553:
554: if ((rd = rcs_findrev(file, rev)) == NULL)
555: return (-1);
556:
1.52 jfb 557: if ((file->rf_head == NULL) &&
558: ((file->rf_head = rcsnum_alloc()) == NULL))
559: return (-1);
560:
1.43 jfb 561: if (rcsnum_cpy(rev, file->rf_head, 0) < 0)
562: return (-1);
563:
564: return (0);
565: }
566:
567:
568: /*
1.35 jfb 569: * rcs_branch_get()
570: *
571: * Retrieve the default branch number for the RCS file <file>.
572: * Returns the number on success. If NULL is returned, then there is no
573: * default branch for this file.
574: */
575: const RCSNUM*
576: rcs_branch_get(RCSFILE *file)
577: {
578: return (file->rf_branch);
579: }
580:
581: /*
582: * rcs_branch_set()
583: *
584: * Set the default branch for the RCS file <file> to <bnum>.
585: * Returns 0 on success, -1 on failure.
586: */
587: int
588: rcs_branch_set(RCSFILE *file, const RCSNUM *bnum)
589: {
590: if ((file->rf_branch == NULL) &&
591: ((file->rf_branch = rcsnum_alloc()) == NULL))
592: return (-1);
593:
594: if (rcsnum_cpy(bnum, file->rf_branch, 0) < 0) {
595: rcsnum_free(file->rf_branch);
596: file->rf_branch = NULL;
597: return (-1);
598: }
599:
600: return (0);
601: }
602:
603: /*
1.29 jfb 604: * rcs_access_add()
605: *
606: * Add the login name <login> to the access list for the RCS file <file>.
607: * Returns 0 on success, or -1 on failure.
608: */
609: int
610: rcs_access_add(RCSFILE *file, const char *login)
611: {
612: struct rcs_access *ap;
613:
614: /* first look for duplication */
615: TAILQ_FOREACH(ap, &(file->rf_access), ra_list) {
616: if (strcmp(ap->ra_name, login) == 0) {
1.32 jfb 617: rcs_errno = RCS_ERR_DUPENT;
1.29 jfb 618: return (-1);
619: }
620: }
621:
622: ap = (struct rcs_access *)malloc(sizeof(*ap));
623: if (ap == NULL) {
1.50 jfb 624: rcs_errno = RCS_ERR_ERRNO;
1.29 jfb 625: cvs_log(LP_ERRNO, "failed to allocate RCS access entry");
626: return (-1);
627: }
628:
1.39 joris 629: ap->ra_name = cvs_strdup(login);
1.29 jfb 630: if (ap->ra_name == NULL) {
631: cvs_log(LP_ERRNO, "failed to duplicate user name");
632: free(ap);
633: return (-1);
634: }
635:
636: TAILQ_INSERT_TAIL(&(file->rf_access), ap, ra_list);
637:
638: /* not synced anymore */
639: file->rf_flags &= ~RCS_SYNCED;
640: return (0);
641: }
642:
643: /*
644: * rcs_access_remove()
645: *
646: * Remove an entry with login name <login> from the access list of the RCS
647: * file <file>.
648: * Returns 0 on success, or -1 on failure.
649: */
650: int
651: rcs_access_remove(RCSFILE *file, const char *login)
652: {
653: struct rcs_access *ap;
654:
655: TAILQ_FOREACH(ap, &(file->rf_access), ra_list)
656: if (strcmp(ap->ra_name, login) == 0)
657: break;
658:
659: if (ap == NULL) {
1.32 jfb 660: rcs_errno = RCS_ERR_NOENT;
1.29 jfb 661: return (-1);
662: }
663:
664: TAILQ_REMOVE(&(file->rf_access), ap, ra_list);
1.39 joris 665: cvs_strfree(ap->ra_name);
1.29 jfb 666: free(ap);
667:
668: /* not synced anymore */
669: file->rf_flags &= ~RCS_SYNCED;
670: return (0);
671: }
672:
673: /*
1.26 jfb 674: * rcs_sym_add()
1.1 jfb 675: *
676: * Add a symbol to the list of symbols for the RCS file <rfp>. The new symbol
677: * is named <sym> and is bound to the RCS revision <snum>.
678: * Returns 0 on success, or -1 on failure.
679: */
680: int
1.26 jfb 681: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1 jfb 682: {
683: struct rcs_sym *symp;
684:
1.47 jfb 685: if (!rcs_sym_check(sym)) {
686: rcs_errno = RCS_ERR_BADSYM;
687: return (NULL);
688: }
689:
1.1 jfb 690: /* first look for duplication */
691: TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
692: if (strcmp(symp->rs_name, sym) == 0) {
1.32 jfb 693: rcs_errno = RCS_ERR_DUPENT;
1.1 jfb 694: return (-1);
695: }
696: }
697:
1.50 jfb 698: if ((symp = (struct rcs_sym *)malloc(sizeof(*symp))) == NULL) {
699: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 700: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
701: return (-1);
702: }
703:
1.50 jfb 704: if ((symp->rs_name = cvs_strdup(sym)) == NULL) {
705: rcs_errno = RCS_ERR_ERRNO;
1.10 joris 706: cvs_log(LP_ERRNO, "failed to duplicate symbol");
707: free(symp);
708: return (-1);
709: }
710:
1.50 jfb 711: if ((symp->rs_num = rcsnum_alloc()) == NULL) {
1.39 joris 712: cvs_strfree(symp->rs_name);
1.11 joris 713: free(symp);
714: return (-1);
715: }
1.1 jfb 716: rcsnum_cpy(snum, symp->rs_num, 0);
717:
718: TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
719:
720: /* not synced anymore */
1.26 jfb 721: rfp->rf_flags &= ~RCS_SYNCED;
1.1 jfb 722: return (0);
723: }
724:
725: /*
1.27 jfb 726: * rcs_sym_remove()
727: *
728: * Remove the symbol with name <sym> from the symbol list for the RCS file
729: * <file>. If no such symbol is found, the call fails and returns with an
730: * error.
731: * Returns 0 on success, or -1 on failure.
732: */
733: int
734: rcs_sym_remove(RCSFILE *file, const char *sym)
735: {
736: struct rcs_sym *symp;
737:
1.47 jfb 738: if (!rcs_sym_check(sym)) {
739: rcs_errno = RCS_ERR_BADSYM;
740: return (NULL);
741: }
742:
1.27 jfb 743: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
744: if (strcmp(symp->rs_name, sym) == 0)
745: break;
746:
747: if (symp == NULL) {
1.32 jfb 748: rcs_errno = RCS_ERR_NOENT;
1.27 jfb 749: return (-1);
750: }
751:
752: TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
1.39 joris 753: cvs_strfree(symp->rs_name);
1.27 jfb 754: rcsnum_free(symp->rs_num);
755: free(symp);
756:
757: /* not synced anymore */
758: file->rf_flags &= ~RCS_SYNCED;
759: return (0);
760: }
761:
762: /*
763: * rcs_sym_getrev()
764: *
765: * Retrieve the RCS revision number associated with the symbol <sym> for the
766: * RCS file <file>. The returned value is a dynamically-allocated copy and
767: * should be freed by the caller once they are done with it.
768: * Returns the RCSNUM on success, or NULL on failure.
769: */
770: RCSNUM*
771: rcs_sym_getrev(RCSFILE *file, const char *sym)
772: {
773: RCSNUM *num;
774: struct rcs_sym *symp;
775:
1.47 jfb 776: if (!rcs_sym_check(sym)) {
777: rcs_errno = RCS_ERR_BADSYM;
778: return (NULL);
779: }
780:
1.27 jfb 781: num = NULL;
782: TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
783: if (strcmp(symp->rs_name, sym) == 0)
784: break;
785:
1.36 jfb 786: if (symp == NULL)
787: rcs_errno = RCS_ERR_NOENT;
788: else if (((num = rcsnum_alloc()) != NULL) &&
1.27 jfb 789: (rcsnum_cpy(symp->rs_num, num, 0) < 0)) {
790: rcsnum_free(num);
791: num = NULL;
792: }
793:
794: return (num);
1.47 jfb 795: }
796:
797: /*
798: * rcs_sym_check()
799: *
800: * Check the RCS symbol name <sym> for any unsupported characters.
801: * Returns 1 if the tag is correct, 0 if it isn't valid.
802: */
803: int
804: rcs_sym_check(const char *sym)
805: {
806: int ret;
807: const char *cp;
808:
809: ret = 1;
810: cp = sym;
811: if (!isalpha(*cp++))
812: return (0);
813:
814: for (; *cp != '\0'; cp++)
815: if (!isgraph(*cp) || (strchr(rcs_sym_invch, *cp) != NULL)) {
816: ret = 0;
817: break;
818: }
819:
820: return (ret);
1.30 jfb 821: }
822:
823: /*
824: * rcs_lock_getmode()
825: *
826: * Retrieve the locking mode of the RCS file <file>.
827: */
828: int
829: rcs_lock_getmode(RCSFILE *file)
830: {
831: return (file->rf_flags & RCS_SLOCK) ? RCS_LOCK_STRICT : RCS_LOCK_LOOSE;
832: }
833:
834: /*
835: * rcs_lock_setmode()
836: *
837: * Set the locking mode of the RCS file <file> to <mode>, which must either
838: * be RCS_LOCK_LOOSE or RCS_LOCK_STRICT.
839: * Returns the previous mode on success, or -1 on failure.
840: */
841: int
842: rcs_lock_setmode(RCSFILE *file, int mode)
843: {
844: int pmode;
845: pmode = rcs_lock_getmode(file);
846:
847: if (mode == RCS_LOCK_STRICT)
848: file->rf_flags |= RCS_SLOCK;
849: else if (mode == RCS_LOCK_LOOSE)
850: file->rf_flags &= ~RCS_SLOCK;
851: else {
852: cvs_log(LP_ERRNO, "invalid lock mode %d", mode);
853: return (-1);
854: }
855:
856: return (pmode);
1.27 jfb 857: }
858:
859: /*
1.40 jfb 860: * rcs_lock_add()
861: *
862: * Add an RCS lock for the user <user> on revision <rev>.
863: * Returns 0 on success, or -1 on failure.
864: */
865: int
866: rcs_lock_add(RCSFILE *file, const char *user, RCSNUM *rev)
867: {
868: struct rcs_lock *lkp;
869:
870: /* first look for duplication */
871: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
872: if (strcmp(lkp->rl_name, user) == 0) {
873: rcs_errno = RCS_ERR_DUPENT;
874: return (-1);
875: }
876: }
877:
1.50 jfb 878: if ((lkp = (struct rcs_lock *)malloc(sizeof(*lkp))) == NULL) {
879: rcs_errno = RCS_ERR_ERRNO;
1.40 jfb 880: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
881: return (-1);
882: }
883:
884: lkp->rl_name = cvs_strdup(user);
885: if (lkp->rl_name == NULL) {
886: cvs_log(LP_ERRNO, "failed to duplicate user name");
887: free(lkp);
888: return (-1);
889: }
890:
891: TAILQ_INSERT_TAIL(&(file->rf_locks), lkp, rl_list);
892:
893: /* not synced anymore */
894: file->rf_flags &= ~RCS_SYNCED;
895: return (0);
896:
897:
898: }
899:
900:
901: /*
902: * rcs_lock_remove()
903: *
904: * Remove the RCS lock on revision <rev>.
905: * Returns 0 on success, or -1 on failure.
906: */
907: int
1.56 ! joris 908: rcs_lock_remove(RCSFILE *file, const RCSNUM *rev)
1.40 jfb 909: {
910: struct rcs_lock *lkp;
911:
912: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
913: if (rcsnum_cmp(lkp->rl_num, rev, 0) == 0)
914: break;
915:
916: if (lkp == NULL) {
917: rcs_errno = RCS_ERR_NOENT;
918: return (-1);
919: }
920:
921: TAILQ_REMOVE(&(file->rf_locks), lkp, rl_list);
922: rcsnum_free(lkp->rl_num);
923: cvs_strfree(lkp->rl_name);
924: free(lkp);
925:
926: /* not synced anymore */
927: file->rf_flags &= ~RCS_SYNCED;
928: return (0);
929: }
930:
931: /*
1.27 jfb 932: * rcs_desc_get()
933: *
934: * Retrieve the description for the RCS file <file>.
935: */
936: const char*
937: rcs_desc_get(RCSFILE *file)
938: {
939: return (file->rf_desc);
940: }
941:
942: /*
943: * rcs_desc_set()
944: *
945: * Set the description for the RCS file <file>.
946: * Returns 0 on success, or -1 on failure.
947: */
948: int
949: rcs_desc_set(RCSFILE *file, const char *desc)
950: {
951: char *tmp;
952:
1.39 joris 953: if ((tmp = cvs_strdup(desc)) == NULL)
1.27 jfb 954: return (-1);
955:
956: if (file->rf_desc != NULL)
1.39 joris 957: cvs_strfree(file->rf_desc);
1.27 jfb 958: file->rf_desc = tmp;
959: file->rf_flags &= ~RCS_SYNCED;
960:
961: return (0);
1.51 jfb 962: }
963:
964: /*
965: * rcs_comment_lookup()
966: *
967: * Lookup the assumed comment leader based on a file's suffix.
968: * Returns a pointer to the string on success, or NULL on failure.
969: */
970: const char*
971: rcs_comment_lookup(const char *filename)
972: {
973: int i;
974: const char *sp;
975:
976: if ((sp = strrchr(filename, '.')) == NULL) {
977: rcs_errno = RCS_ERR_NOENT;
978: return (NULL);
979: }
980: sp++;
981:
982: for (i = 0; i < (int)NB_COMTYPES; i++)
983: if (strcmp(rcs_comments[i].rc_suffix, sp) == 0)
984: return (rcs_comments[i].rc_cstr);
985: return (NULL);
1.27 jfb 986: }
987:
1.33 jfb 988: /*
989: * rcs_comment_get()
990: *
991: * Retrieve the comment leader for the RCS file <file>.
992: */
993: const char*
994: rcs_comment_get(RCSFILE *file)
995: {
996: return (file->rf_comment);
997: }
998:
999: /*
1000: * rcs_comment_set()
1001: *
1002: * Set the comment leader for the RCS file <file>.
1003: * Returns 0 on success, or -1 on failure.
1004: */
1005: int
1006: rcs_comment_set(RCSFILE *file, const char *comment)
1007: {
1008: char *tmp;
1009:
1.39 joris 1010: if ((tmp = cvs_strdup(comment)) == NULL)
1.33 jfb 1011: return (-1);
1012:
1013: if (file->rf_comment != NULL)
1.39 joris 1014: cvs_strfree(file->rf_comment);
1.33 jfb 1015: file->rf_comment = tmp;
1016: file->rf_flags &= ~RCS_SYNCED;
1017:
1018: return (0);
1019: }
1.40 jfb 1020:
1021: /*
1022: * rcs_tag_resolve()
1023: *
1024: * Retrieve the revision number corresponding to the tag <tag> for the RCS
1025: * file <file>.
1026: */
1027: RCSNUM*
1028: rcs_tag_resolve(RCSFILE *file, const char *tag)
1029: {
1030: RCSNUM *num;
1031:
1032: if ((num = rcsnum_parse(tag)) == NULL) {
1033: num = rcs_sym_getrev(file, tag);
1034: }
1035:
1036: return (num);
1037: }
1038:
1.27 jfb 1039:
1040: /*
1.1 jfb 1041: * rcs_patch()
1042: *
1043: * Apply an RCS-format patch pointed to by <patch> to the file contents
1044: * found in <data>.
1045: * Returns 0 on success, or -1 on failure.
1046: */
1047: BUF*
1048: rcs_patch(const char *data, const char *patch)
1049: {
1.5 vincent 1050: struct rcs_foo *dlines, *plines;
1051: struct rcs_line *lp;
1.1 jfb 1052: size_t len;
1.5 vincent 1053: int lineno;
1.1 jfb 1054: BUF *res;
1055:
1056: len = strlen(data);
1057: res = cvs_buf_alloc(len, BUF_AUTOEXT);
1058: if (res == NULL)
1059: return (NULL);
1060:
1061: dlines = rcs_splitlines(data);
1.17 jfb 1062: if (dlines == NULL) {
1063: cvs_buf_free(res);
1.1 jfb 1064: return (NULL);
1.17 jfb 1065: }
1.5 vincent 1066:
1.1 jfb 1067: plines = rcs_splitlines(patch);
1.5 vincent 1068: if (plines == NULL) {
1.17 jfb 1069: cvs_buf_free(res);
1.5 vincent 1070: rcs_freefoo(dlines);
1.1 jfb 1071: return (NULL);
1.5 vincent 1072: }
1073:
1074: if (rcs_patch_lines(dlines, plines) < 0) {
1.17 jfb 1075: cvs_buf_free(res);
1.5 vincent 1076: rcs_freefoo(plines);
1077: rcs_freefoo(dlines);
1078: return (NULL);
1079: }
1080:
1081: lineno = 0;
1082: TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
1083: if (lineno != 0)
1084: cvs_buf_fappend(res, "%s\n", lp->rl_line);
1085: lineno++;
1086: }
1087:
1088: rcs_freefoo(dlines);
1089: rcs_freefoo(plines);
1090: return (res);
1091: }
1092:
1.7 jfb 1093: static int
1.5 vincent 1094: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
1095: {
1096: char op, *ep;
1097: struct rcs_line *lp, *dlp, *ndlp;
1098: int i, lineno, nbln;
1.1 jfb 1099:
1100: dlp = TAILQ_FIRST(&(dlines->rl_lines));
1101: lp = TAILQ_FIRST(&(plines->rl_lines));
1102:
1103: /* skip first bogus line */
1104: for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
1105: lp = TAILQ_NEXT(lp, rl_list)) {
1106: op = *(lp->rl_line);
1107: lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
1108: if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
1109: (*ep != ' ')) {
1110: cvs_log(LP_ERR,
1111: "invalid line specification in RCS patch");
1112: return (NULL);
1113: }
1114: ep++;
1115: nbln = (int)strtol(ep, &ep, 10);
1116: if ((nbln <= 0) || (*ep != '\0')) {
1117: cvs_log(LP_ERR,
1118: "invalid line number specification in RCS patch");
1119: return (NULL);
1120: }
1121:
1122: /* find the appropriate line */
1123: for (;;) {
1124: if (dlp == NULL)
1125: break;
1126: if (dlp->rl_lineno == lineno)
1127: break;
1128: if (dlp->rl_lineno > lineno) {
1129: dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14 deraadt 1130: } else if (dlp->rl_lineno < lineno) {
1.1 jfb 1131: ndlp = TAILQ_NEXT(dlp, rl_list);
1132: if (ndlp->rl_lineno > lineno)
1133: break;
1134: dlp = ndlp;
1135: }
1136: }
1137: if (dlp == NULL) {
1138: cvs_log(LP_ERR,
1139: "can't find referenced line in RCS patch");
1140: return (NULL);
1141: }
1142:
1143: if (op == 'd') {
1144: for (i = 0; (i < nbln) && (dlp != NULL); i++) {
1145: ndlp = TAILQ_NEXT(dlp, rl_list);
1146: TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
1147: dlp = ndlp;
1148: }
1.14 deraadt 1149: } else if (op == 'a') {
1.1 jfb 1150: for (i = 0; i < nbln; i++) {
1151: ndlp = lp;
1152: lp = TAILQ_NEXT(lp, rl_list);
1153: if (lp == NULL) {
1154: cvs_log(LP_ERR, "truncated RCS patch");
1.5 vincent 1155: return (-1);
1.1 jfb 1156: }
1157: TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
1158: TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
1159: lp, rl_list);
1160: dlp = lp;
1161:
1162: /* we don't want lookup to block on those */
1163: lp->rl_lineno = lineno;
1164:
1165: lp = ndlp;
1166: }
1.14 deraadt 1167: } else {
1.1 jfb 1168: cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5 vincent 1169: return (-1);
1.1 jfb 1170: }
1171:
1172: /* last line of the patch, done */
1173: if (lp->rl_lineno == plines->rl_nblines)
1174: break;
1175: }
1176:
1177: /* once we're done patching, rebuild the line numbers */
1.2 vincent 1178: lineno = 0;
1.5 vincent 1179: TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1 jfb 1180: lp->rl_lineno = lineno++;
1181: dlines->rl_nblines = lineno - 1;
1182:
1.5 vincent 1183: return (0);
1.1 jfb 1184: }
1185:
1186: /*
1187: * rcs_getrev()
1188: *
1189: * Get the whole contents of revision <rev> from the RCSFILE <rfp>. The
1.4 vincent 1190: * returned buffer is dynamically allocated and should be released using
1191: * cvs_buf_free() once the caller is done using it.
1.1 jfb 1192: */
1193: BUF*
1194: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
1195: {
1196: int res;
1197: size_t len;
1198: void *bp;
1199: RCSNUM *crev;
1200: BUF *rbuf;
1201: struct rcs_delta *rdp = NULL;
1202:
1.28 jfb 1203: if (rfp->rf_head == NULL)
1204: return (NULL);
1205:
1.1 jfb 1206: res = rcsnum_cmp(rfp->rf_head, rev, 0);
1207: if (res == 1) {
1.32 jfb 1208: rcs_errno = RCS_ERR_NOENT;
1.1 jfb 1209: return (NULL);
1.26 jfb 1210: }
1211:
1212: rdp = rcs_findrev(rfp, rfp->rf_head);
1213: if (rdp == NULL) {
1214: cvs_log(LP_ERR, "failed to get RCS HEAD revision");
1215: return (NULL);
1216: }
1217:
1.45 jfb 1218: len = rdp->rd_tlen;
1.26 jfb 1219: if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
1220: return (NULL);
1221:
1222: cvs_buf_append(rbuf, rdp->rd_text, len);
1223:
1224: if (res != 0) {
1225: /* Apply patches backwards to get the right version.
1226: * This will need some rework to support sub branches.
1227: */
1228: if ((crev = rcsnum_alloc()) == NULL) {
1229: cvs_buf_free(rbuf);
1.1 jfb 1230: return (NULL);
1231: }
1.26 jfb 1232: rcsnum_cpy(rfp->rf_head, crev, 0);
1233: do {
1234: crev->rn_id[crev->rn_len - 1]--;
1235: rdp = rcs_findrev(rfp, crev);
1236: if (rdp == NULL) {
1237: rcsnum_free(crev);
1238: cvs_buf_free(rbuf);
1239: return (NULL);
1240: }
1.1 jfb 1241:
1.26 jfb 1242: if (cvs_buf_putc(rbuf, '\0') < 0) {
1243: rcsnum_free(crev);
1.17 jfb 1244: cvs_buf_free(rbuf);
1.11 joris 1245: return (NULL);
1.17 jfb 1246: }
1.26 jfb 1247: bp = cvs_buf_release(rbuf);
1.45 jfb 1248: rbuf = rcs_patch((char *)bp, (char *)rdp->rd_text);
1.26 jfb 1249: if (rbuf == NULL)
1250: break;
1251: } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1 jfb 1252:
1.26 jfb 1253: rcsnum_free(crev);
1.1 jfb 1254: }
1255:
1256: return (rbuf);
1257: }
1258:
1259: /*
1.52 jfb 1260: * rcs_rev_add()
1261: *
1.53 jfb 1262: * Add a revision to the RCS file <rf>. The new revision's number can be
1263: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1264: * new revision will have a number equal to the previous head revision plus
1265: * one). The <msg> argument specifies the log message for that revision, and
1266: * <date> specifies the revision's date (a value of -1 is
1267: * equivalent to using the current time).
1.52 jfb 1268: * Returns 0 on success, or -1 on failure.
1269: */
1270: int
1.53 jfb 1271: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date)
1.52 jfb 1272: {
1273: time_t now;
1274: struct passwd *pw;
1275: struct rcs_delta *rdp;
1276:
1277: if (rev == RCS_HEAD_REV) {
1278: } else if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1279: rcs_errno = RCS_ERR_DUPENT;
1280: return (-1);
1281: }
1282:
1283: if ((pw = getpwuid(getuid())) == NULL) {
1284: rcs_errno = RCS_ERR_ERRNO;
1285: return (-1);
1286: }
1287:
1288: if ((rdp = (struct rcs_delta *)malloc(sizeof(*rdp))) == NULL) {
1289: rcs_errno = RCS_ERR_ERRNO;
1290: return (-1);
1291: }
1292: memset(rdp, 0, sizeof(*rdp));
1293:
1294: TAILQ_INIT(&(rdp->rd_branches));
1295: TAILQ_INIT(&(rdp->rd_snodes));
1296:
1297: if ((rdp->rd_num = rcsnum_alloc()) == NULL) {
1298: rcs_freedelta(rdp);
1299: return (-1);
1300: }
1301: rcsnum_cpy(rev, rdp->rd_num, 0);
1302:
1303: if ((rdp->rd_author = cvs_strdup(pw->pw_name)) == NULL) {
1304: rcs_freedelta(rdp);
1305: return (-1);
1306: }
1307:
1308: if ((rdp->rd_state = cvs_strdup(RCS_STATE_EXP)) == NULL) {
1309: rcs_freedelta(rdp);
1310: return (-1);
1311: }
1312:
1313: if ((rdp->rd_log = cvs_strdup(msg)) == NULL) {
1314: rcs_errno = RCS_ERR_ERRNO;
1315: rcs_freedelta(rdp);
1316: return (-1);
1317: }
1318:
1.53 jfb 1319: if (date != (time_t)(-1))
1320: now = date;
1321: else
1322: time(&now);
1.52 jfb 1323: gmtime_r(&now, &(rdp->rd_date));
1324:
1325: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1326: rf->rf_ndelta++;
1327:
1328: return (0);
1329: }
1330:
1331: /*
1332: * rcs_rev_remove()
1333: *
1334: * Remove the revision whose number is <rev> from the RCS file <rf>.
1335: */
1336: int
1337: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1338: {
1339: int ret;
1340: struct rcs_delta *rdp;
1341:
1342: ret = 0;
1343: if (rev == RCS_HEAD_REV)
1344: rev = rf->rf_head;
1345:
1346: /* do we actually have that revision? */
1347: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1348: rcs_errno = RCS_ERR_NOENT;
1349: ret = -1;
1350: } else {
1351: /* XXX assumes it's not a sub node */
1352: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1353: rf->rf_ndelta--;
1354: rf->rf_flags &= ~RCS_SYNCED;
1355: }
1356:
1357: return (ret);
1358:
1359: }
1360:
1361: /*
1.1 jfb 1362: * rcs_findrev()
1363: *
1364: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1365: * The revision number is given in <rev>.
1366: * Returns a pointer to the delta on success, or NULL on failure.
1367: */
1368: static struct rcs_delta*
1.43 jfb 1369: rcs_findrev(RCSFILE *rfp, const RCSNUM *rev)
1.1 jfb 1370: {
1371: u_int cmplen;
1372: struct rcs_delta *rdp;
1373: struct rcs_dlist *hp;
1.6 vincent 1374: int found;
1.26 jfb 1375:
1.1 jfb 1376: cmplen = 2;
1377: hp = &(rfp->rf_delta);
1378:
1.6 vincent 1379: do {
1380: found = 0;
1381: TAILQ_FOREACH(rdp, hp, rd_list) {
1382: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
1383: if (cmplen == rev->rn_len)
1384: return (rdp);
1.1 jfb 1385:
1.6 vincent 1386: hp = &(rdp->rd_snodes);
1387: cmplen += 2;
1388: found = 1;
1389: break;
1390: }
1.1 jfb 1391: }
1.6 vincent 1392: } while (found && cmplen < rev->rn_len);
1.1 jfb 1393:
1394: return (NULL);
1.20 jfb 1395: }
1396:
1397: /*
1.26 jfb 1398: * rcs_kwexp_set()
1399: *
1400: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1401: * Returns 0 on success, or -1 on failure.
1402: */
1403: int
1404: rcs_kwexp_set(RCSFILE *file, int mode)
1405: {
1406: int i;
1407: char *tmp, buf[8] = "";
1408:
1409: if (RCS_KWEXP_INVAL(mode))
1410: return (-1);
1411:
1412: i = 0;
1413: if (mode == RCS_KWEXP_NONE)
1414: buf[0] = 'b';
1415: else if (mode == RCS_KWEXP_OLD)
1416: buf[0] = 'o';
1417: else {
1418: if (mode & RCS_KWEXP_NAME)
1419: buf[i++] = 'k';
1420: if (mode & RCS_KWEXP_VAL)
1421: buf[i++] = 'v';
1422: if (mode & RCS_KWEXP_LKR)
1423: buf[i++] = 'l';
1424: }
1425:
1.39 joris 1426: if ((tmp = cvs_strdup(buf)) == NULL) {
1.26 jfb 1427: cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
1428: file->rf_path);
1429: return (-1);
1430: }
1431:
1.27 jfb 1432: if (file->rf_expand != NULL)
1.39 joris 1433: cvs_strfree(file->rf_expand);
1.26 jfb 1434: file->rf_expand = tmp;
1435:
1436: return (0);
1437: }
1438:
1439: /*
1440: * rcs_kwexp_get()
1441: *
1442: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1443: */
1444: int
1445: rcs_kwexp_get(RCSFILE *file)
1446: {
1447: return rcs_kflag_get(file->rf_expand);
1448: }
1449:
1450: /*
1.20 jfb 1451: * rcs_kflag_get()
1452: *
1453: * Get the keyword expansion mode from a set of character flags given in
1454: * <flags> and return the appropriate flag mask. In case of an error, the
1455: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1456: */
1457: int
1458: rcs_kflag_get(const char *flags)
1459: {
1460: int fl;
1461: size_t len;
1462: const char *fp;
1463:
1464: fl = 0;
1465: len = strlen(flags);
1466:
1467: for (fp = flags; *fp != '\0'; fp++) {
1468: if (*fp == 'k')
1469: fl |= RCS_KWEXP_NAME;
1470: else if (*fp == 'v')
1471: fl |= RCS_KWEXP_VAL;
1472: else if (*fp == 'l')
1473: fl |= RCS_KWEXP_LKR;
1474: else if (*fp == 'o') {
1475: if (len != 1)
1476: fl |= RCS_KWEXP_ERR;
1477: fl |= RCS_KWEXP_OLD;
1478: } else if (*fp == 'b') {
1479: if (len != 1)
1480: fl |= RCS_KWEXP_ERR;
1481: } else /* unknown letter */
1482: fl |= RCS_KWEXP_ERR;
1483: }
1484:
1485: return (fl);
1.32 jfb 1486: }
1487:
1488: /*
1489: * rcs_errstr()
1490: *
1491: * Get the error string matching the RCS error code <code>.
1492: */
1493: const char*
1494: rcs_errstr(int code)
1495: {
1.50 jfb 1496: const char *esp;
1497:
1498: if ((code < 0) || ((code >= (int)RCS_NERR) && (code != RCS_ERR_ERRNO)))
1499: esp = NULL;
1500: else if (code == RCS_ERR_ERRNO)
1501: esp = strerror(errno);
1502: else
1503: esp = rcs_errstrs[code];
1504: return (esp);
1.1 jfb 1505: }
1506:
1.21 jfb 1507: void
1508: rcs_kflag_usage(void)
1509: {
1510: fprintf(stderr, "Valid expansion modes include:\n"
1.22 jfb 1511: "\t-kkv\tGenerate keywords using the default form.\n"
1512: "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
1513: "\t-kk\tGenerate only keyword names in keyword strings.\n"
1514: "\t-kv\tGenerate only keyword values in keyword strings.\n"
1515: "\t-ko\tGenerate old keyword string "
1.21 jfb 1516: "(no changes from checked in file).\n"
1.22 jfb 1517: "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21 jfb 1518: }
1.1 jfb 1519:
1520: /*
1521: * rcs_parse()
1522: *
1523: * Parse the contents of file <path>, which are in the RCS format.
1524: * Returns 0 on success, or -1 on failure.
1525: */
1.26 jfb 1526: static int
1.1 jfb 1527: rcs_parse(RCSFILE *rfp)
1528: {
1529: int ret;
1530: struct rcs_pdata *pdp;
1531:
1.26 jfb 1532: if (rfp->rf_flags & RCS_PARSED)
1.1 jfb 1533: return (0);
1534:
1.26 jfb 1535: if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.50 jfb 1536: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1537: cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
1538: return (-1);
1539: }
1540: memset(pdp, 0, sizeof(*pdp));
1541:
1.18 jfb 1542: pdp->rp_lines = 0;
1.1 jfb 1543: pdp->rp_pttype = RCS_TOK_ERR;
1544:
1545: pdp->rp_file = fopen(rfp->rf_path, "r");
1546: if (pdp->rp_file == NULL) {
1.50 jfb 1547: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1548: cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
1549: rcs_freepdata(pdp);
1550: return (-1);
1551: }
1552:
1553: pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
1554: if (pdp->rp_buf == NULL) {
1.50 jfb 1555: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1556: cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
1557: rcs_freepdata(pdp);
1558: return (-1);
1559: }
1560: pdp->rp_blen = RCS_BUFSIZE;
1.18 jfb 1561: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1 jfb 1562:
1563: /* ditch the strict lock */
1.26 jfb 1564: rfp->rf_flags &= ~RCS_SLOCK;
1.1 jfb 1565: rfp->rf_pdata = pdp;
1566:
1.31 jfb 1567: if ((ret = rcs_parse_admin(rfp)) < 0) {
1.1 jfb 1568: rcs_freepdata(pdp);
1569: return (-1);
1.31 jfb 1570: } else if (ret == RCS_TOK_NUM) {
1571: for (;;) {
1572: ret = rcs_parse_delta(rfp);
1573: if (ret == 0)
1574: break;
1575: else if (ret == -1) {
1576: rcs_freepdata(pdp);
1577: return (-1);
1578: }
1.1 jfb 1579: }
1580: }
1581:
1582: ret = rcs_gettok(rfp);
1583: if (ret != RCS_TOK_DESC) {
1.50 jfb 1584: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1585: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1586: RCS_TOKSTR(rfp));
1587: rcs_freepdata(pdp);
1588: return (-1);
1589: }
1590:
1591: ret = rcs_gettok(rfp);
1592: if (ret != RCS_TOK_STRING) {
1.50 jfb 1593: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1594: cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
1595: RCS_TOKSTR(rfp));
1596: rcs_freepdata(pdp);
1597: return (-1);
1598: }
1599:
1.39 joris 1600: rfp->rf_desc = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1601: if (rfp->rf_desc == NULL) {
1602: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
1603: rcs_freepdata(pdp);
1604: return (-1);
1605: }
1.1 jfb 1606:
1607: for (;;) {
1608: ret = rcs_parse_deltatext(rfp);
1609: if (ret == 0)
1610: break;
1611: else if (ret == -1) {
1612: rcs_freepdata(pdp);
1613: return (-1);
1614: }
1615: }
1616:
1617: rcs_freepdata(pdp);
1618:
1619: rfp->rf_pdata = NULL;
1.26 jfb 1620: rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1 jfb 1621:
1622: return (0);
1623: }
1624:
1625: /*
1626: * rcs_parse_admin()
1627: *
1628: * Parse the administrative portion of an RCS file.
1.31 jfb 1629: * Returns the type of the first token found after the admin section on
1630: * success, or -1 on failure.
1.1 jfb 1631: */
1632: static int
1633: rcs_parse_admin(RCSFILE *rfp)
1634: {
1635: u_int i;
1636: int tok, ntok, hmask;
1637: struct rcs_key *rk;
1638:
1639: /* hmask is a mask of the headers already encountered */
1640: hmask = 0;
1641: for (;;) {
1642: tok = rcs_gettok(rfp);
1643: if (tok == RCS_TOK_ERR) {
1.50 jfb 1644: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1645: cvs_log(LP_ERR, "parse error in RCS admin section");
1646: return (-1);
1.31 jfb 1647: } else if ((tok == RCS_TOK_NUM) || (tok == RCS_TOK_DESC)) {
1648: /*
1649: * Assume this is the start of the first delta or
1650: * that we are dealing with an empty RCS file and
1651: * we just found the description.
1652: */
1.1 jfb 1653: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.31 jfb 1654: return (tok);
1.1 jfb 1655: }
1656:
1657: rk = NULL;
1.18 jfb 1658: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1659: if (rcs_keys[i].rk_id == tok)
1660: rk = &(rcs_keys[i]);
1661:
1662: if (hmask & (1 << tok)) {
1.50 jfb 1663: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1664: cvs_log(LP_ERR, "duplicate RCS key");
1665: return (-1);
1666: }
1667: hmask |= (1 << tok);
1668:
1669: switch (tok) {
1670: case RCS_TOK_HEAD:
1671: case RCS_TOK_BRANCH:
1672: case RCS_TOK_COMMENT:
1673: case RCS_TOK_EXPAND:
1674: ntok = rcs_gettok(rfp);
1675: if (ntok == RCS_TOK_SCOLON)
1676: break;
1677: if (ntok != rk->rk_val) {
1.50 jfb 1678: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1679: cvs_log(LP_ERR,
1680: "invalid value type for RCS key `%s'",
1681: rk->rk_str);
1682: }
1683:
1684: if (tok == RCS_TOK_HEAD) {
1.28 jfb 1685: if (rfp->rf_head == NULL) {
1686: rfp->rf_head = rcsnum_alloc();
1687: if (rfp->rf_head == NULL)
1688: return (-1);
1689: }
1.1 jfb 1690: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1691: rfp->rf_head);
1.14 deraadt 1692: } else if (tok == RCS_TOK_BRANCH) {
1.35 jfb 1693: if (rfp->rf_branch == NULL) {
1694: rfp->rf_branch = rcsnum_alloc();
1695: if (rfp->rf_branch == NULL)
1696: return (-1);
1697: }
1698: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1699: rfp->rf_branch) < 0)
1700: return (-1);
1.14 deraadt 1701: } else if (tok == RCS_TOK_COMMENT) {
1.39 joris 1702: rfp->rf_comment = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1703: if (rfp->rf_comment == NULL) {
1704: cvs_log(LP_ERRNO,
1705: "failed to duplicate rcs token");
1706: return (-1);
1707: }
1.14 deraadt 1708: } else if (tok == RCS_TOK_EXPAND) {
1.39 joris 1709: rfp->rf_expand = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1710: if (rfp->rf_expand == NULL) {
1711: cvs_log(LP_ERRNO,
1712: "failed to duplicate rcs token");
1713: return (-1);
1714: }
1.1 jfb 1715: }
1716:
1717: /* now get the expected semi-colon */
1718: ntok = rcs_gettok(rfp);
1719: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1720: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1721: cvs_log(LP_ERR,
1722: "missing semi-colon after RCS `%s' key",
1.26 jfb 1723: rk->rk_str);
1.1 jfb 1724: return (-1);
1725: }
1726: break;
1727: case RCS_TOK_ACCESS:
1.29 jfb 1728: if (rcs_parse_access(rfp) < 0)
1729: return (-1);
1.1 jfb 1730: break;
1731: case RCS_TOK_SYMBOLS:
1.29 jfb 1732: if (rcs_parse_symbols(rfp) < 0)
1733: return (-1);
1.1 jfb 1734: break;
1735: case RCS_TOK_LOCKS:
1.29 jfb 1736: if (rcs_parse_locks(rfp) < 0)
1737: return (-1);
1.1 jfb 1738: break;
1739: default:
1.50 jfb 1740: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1741: cvs_log(LP_ERR,
1742: "unexpected token `%s' in RCS admin section",
1743: RCS_TOKSTR(rfp));
1744: return (-1);
1745: }
1746: }
1747:
1748: return (0);
1749: }
1750:
1751: /*
1752: * rcs_parse_delta()
1753: *
1754: * Parse an RCS delta section and allocate the structure to store that delta's
1755: * information in the <rfp> delta list.
1756: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1757: * -1 on error.
1758: */
1759: static int
1760: rcs_parse_delta(RCSFILE *rfp)
1761: {
1762: int ret, tok, ntok, hmask;
1763: u_int i;
1764: char *tokstr;
1.3 vincent 1765: RCSNUM *datenum;
1.1 jfb 1766: struct rcs_delta *rdp;
1767: struct rcs_key *rk;
1768:
1769: rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
1770: if (rdp == NULL) {
1.50 jfb 1771: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 1772: cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
1773: return (-1);
1774: }
1775: memset(rdp, 0, sizeof(*rdp));
1776:
1777: rdp->rd_num = rcsnum_alloc();
1.11 joris 1778: if (rdp->rd_num == NULL) {
1779: rcs_freedelta(rdp);
1780: return (-1);
1781: }
1.1 jfb 1782: rdp->rd_next = rcsnum_alloc();
1.11 joris 1783: if (rdp->rd_next == NULL) {
1784: rcs_freedelta(rdp);
1785: return (-1);
1786: }
1.1 jfb 1787:
1788: TAILQ_INIT(&(rdp->rd_branches));
1.52 jfb 1789: TAILQ_INIT(&(rdp->rd_snodes));
1.1 jfb 1790:
1791: tok = rcs_gettok(rfp);
1792: if (tok != RCS_TOK_NUM) {
1.52 jfb 1793: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1794: cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
1795: RCS_TOKSTR(rfp));
1796: rcs_freedelta(rdp);
1797: return (-1);
1798: }
1799: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1800:
1801: hmask = 0;
1802: ret = 0;
1803: tokstr = NULL;
1804:
1805: for (;;) {
1806: tok = rcs_gettok(rfp);
1807: if (tok == RCS_TOK_ERR) {
1.50 jfb 1808: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1809: cvs_log(LP_ERR, "parse error in RCS delta section");
1810: rcs_freedelta(rdp);
1811: return (-1);
1.14 deraadt 1812: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15 tedu 1813: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1 jfb 1814: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1815: break;
1816: }
1817:
1818: rk = NULL;
1.18 jfb 1819: for (i = 0; i < RCS_NKEYS; i++)
1.1 jfb 1820: if (rcs_keys[i].rk_id == tok)
1821: rk = &(rcs_keys[i]);
1822:
1823: if (hmask & (1 << tok)) {
1.50 jfb 1824: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1825: cvs_log(LP_ERR, "duplicate RCS key");
1826: rcs_freedelta(rdp);
1827: return (-1);
1828: }
1829: hmask |= (1 << tok);
1830:
1831: switch (tok) {
1832: case RCS_TOK_DATE:
1833: case RCS_TOK_AUTHOR:
1834: case RCS_TOK_STATE:
1835: case RCS_TOK_NEXT:
1836: ntok = rcs_gettok(rfp);
1837: if (ntok == RCS_TOK_SCOLON) {
1838: if (rk->rk_flags & RCS_VOPT)
1839: break;
1840: else {
1.50 jfb 1841: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1842: cvs_log(LP_ERR, "missing mandatory "
1843: "value to RCS key `%s'",
1844: rk->rk_str);
1845: rcs_freedelta(rdp);
1846: return (-1);
1847: }
1848: }
1849:
1850: if (ntok != rk->rk_val) {
1.50 jfb 1851: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1852: cvs_log(LP_ERR,
1853: "invalid value type for RCS key `%s'",
1854: rk->rk_str);
1855: rcs_freedelta(rdp);
1856: return (-1);
1857: }
1858:
1859: if (tokstr != NULL)
1.41 jfb 1860: cvs_strfree(tokstr);
1.39 joris 1861: tokstr = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 1862: if (tokstr == NULL) {
1.15 tedu 1863: cvs_log(LP_ERRNO,
1.10 joris 1864: "failed to duplicate rcs token");
1865: rcs_freedelta(rdp);
1866: return (-1);
1867: }
1.1 jfb 1868:
1869: /* now get the expected semi-colon */
1870: ntok = rcs_gettok(rfp);
1871: if (ntok != RCS_TOK_SCOLON) {
1.50 jfb 1872: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1873: cvs_log(LP_ERR,
1874: "missing semi-colon after RCS `%s' key",
1.26 jfb 1875: rk->rk_str);
1.41 jfb 1876: cvs_strfree(tokstr);
1.1 jfb 1877: rcs_freedelta(rdp);
1878: return (-1);
1879: }
1880:
1881: if (tok == RCS_TOK_DATE) {
1.25 jfb 1882: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.41 jfb 1883: cvs_strfree(tokstr);
1.11 joris 1884: rcs_freedelta(rdp);
1885: return (-1);
1886: }
1.3 vincent 1887: if (datenum->rn_len != 6) {
1.50 jfb 1888: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1889: cvs_log(LP_ERR,
1890: "RCS date specification has %s "
1891: "fields",
1.3 vincent 1892: (datenum->rn_len > 6) ? "too many" :
1.1 jfb 1893: "missing");
1.41 jfb 1894: cvs_strfree(tokstr);
1.1 jfb 1895: rcs_freedelta(rdp);
1.37 tedu 1896: rcsnum_free(datenum);
1897: return (-1);
1.1 jfb 1898: }
1.3 vincent 1899: rdp->rd_date.tm_year = datenum->rn_id[0];
1.19 jfb 1900: if (rdp->rd_date.tm_year >= 1900)
1901: rdp->rd_date.tm_year -= 1900;
1.3 vincent 1902: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
1903: rdp->rd_date.tm_mday = datenum->rn_id[2];
1904: rdp->rd_date.tm_hour = datenum->rn_id[3];
1905: rdp->rd_date.tm_min = datenum->rn_id[4];
1906: rdp->rd_date.tm_sec = datenum->rn_id[5];
1907: rcsnum_free(datenum);
1.14 deraadt 1908: } else if (tok == RCS_TOK_AUTHOR) {
1.1 jfb 1909: rdp->rd_author = tokstr;
1910: tokstr = NULL;
1.14 deraadt 1911: } else if (tok == RCS_TOK_STATE) {
1.1 jfb 1912: rdp->rd_state = tokstr;
1913: tokstr = NULL;
1.14 deraadt 1914: } else if (tok == RCS_TOK_NEXT) {
1.1 jfb 1915: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1916: }
1917: break;
1918: case RCS_TOK_BRANCHES:
1.46 jfb 1919: if (rcs_parse_branches(rfp, rdp) < 0) {
1920: rcs_freedelta(rdp);
1921: return (-1);
1922: }
1.1 jfb 1923: break;
1924: default:
1.50 jfb 1925: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1926: cvs_log(LP_ERR,
1927: "unexpected token `%s' in RCS delta",
1928: RCS_TOKSTR(rfp));
1929: rcs_freedelta(rdp);
1930: return (-1);
1931: }
1932: }
1933:
1.13 jfb 1934: if (tokstr != NULL)
1.39 joris 1935: cvs_strfree(tokstr);
1.13 jfb 1936:
1.1 jfb 1937: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26 jfb 1938: rfp->rf_ndelta++;
1.1 jfb 1939:
1940: return (ret);
1941: }
1942:
1943: /*
1944: * rcs_parse_deltatext()
1945: *
1946: * Parse an RCS delta text section and fill in the log and text field of the
1947: * appropriate delta section.
1948: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1949: * -1 on error.
1950: */
1951: static int
1952: rcs_parse_deltatext(RCSFILE *rfp)
1953: {
1954: int tok;
1955: RCSNUM *tnum;
1956: struct rcs_delta *rdp;
1957:
1958: tok = rcs_gettok(rfp);
1959: if (tok == RCS_TOK_EOF)
1960: return (0);
1961:
1962: if (tok != RCS_TOK_NUM) {
1.50 jfb 1963: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1964: cvs_log(LP_ERR,
1965: "unexpected token `%s' at start of RCS delta text",
1966: RCS_TOKSTR(rfp));
1967: return (-1);
1968: }
1.13 jfb 1969:
1970: tnum = rcsnum_alloc();
1971: if (tnum == NULL)
1972: return (-1);
1.1 jfb 1973: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
1974:
1975: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1976: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
1977: break;
1978: }
1.13 jfb 1979: rcsnum_free(tnum);
1980:
1.1 jfb 1981: if (rdp == NULL) {
1982: cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
1983: RCS_TOKSTR(rfp));
1984: return (-1);
1985: }
1986:
1987: tok = rcs_gettok(rfp);
1988: if (tok != RCS_TOK_LOG) {
1.50 jfb 1989: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1990: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1991: RCS_TOKSTR(rfp));
1992: return (-1);
1993: }
1994:
1995: tok = rcs_gettok(rfp);
1996: if (tok != RCS_TOK_STRING) {
1.50 jfb 1997: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 1998: cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
1999: RCS_TOKSTR(rfp));
2000: return (-1);
2001: }
1.39 joris 2002: rdp->rd_log = cvs_strdup(RCS_TOKSTR(rfp));
1.1 jfb 2003: if (rdp->rd_log == NULL) {
2004: cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
2005: return (-1);
2006: }
2007:
2008: tok = rcs_gettok(rfp);
2009: if (tok != RCS_TOK_TEXT) {
1.50 jfb 2010: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2011: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2012: RCS_TOKSTR(rfp));
2013: return (-1);
2014: }
2015:
2016: tok = rcs_gettok(rfp);
2017: if (tok != RCS_TOK_STRING) {
1.50 jfb 2018: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2019: cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
2020: RCS_TOKSTR(rfp));
2021: return (-1);
2022: }
2023:
1.45 jfb 2024: rdp->rd_text = (u_char *)malloc(RCS_TOKLEN(rfp));
1.1 jfb 2025: if (rdp->rd_text == NULL) {
2026: cvs_log(LP_ERRNO, "failed to copy RCS delta text");
2027: return (-1);
2028: }
1.45 jfb 2029: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), RCS_TOKLEN(rfp));
1.42 jfb 2030: rdp->rd_tlen = RCS_TOKLEN(rfp);
1.1 jfb 2031:
2032: return (1);
2033: }
2034:
2035: /*
2036: * rcs_parse_access()
2037: *
2038: * Parse the access list given as value to the `access' keyword.
2039: * Returns 0 on success, or -1 on failure.
2040: */
2041: static int
2042: rcs_parse_access(RCSFILE *rfp)
2043: {
2044: int type;
2045:
2046: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2047: if (type != RCS_TOK_ID) {
1.50 jfb 2048: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2049: cvs_log(LP_ERR, "unexpected token `%s' in access list",
2050: RCS_TOKSTR(rfp));
2051: return (-1);
2052: }
1.29 jfb 2053:
2054: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2055: return (-1);
1.1 jfb 2056: }
2057:
2058: return (0);
2059: }
2060:
2061: /*
2062: * rcs_parse_symbols()
2063: *
2064: * Parse the symbol list given as value to the `symbols' keyword.
2065: * Returns 0 on success, or -1 on failure.
2066: */
2067: static int
2068: rcs_parse_symbols(RCSFILE *rfp)
2069: {
2070: int type;
2071: struct rcs_sym *symp;
2072:
2073: for (;;) {
2074: type = rcs_gettok(rfp);
2075: if (type == RCS_TOK_SCOLON)
2076: break;
2077:
1.41 jfb 2078: if (type != RCS_TOK_ID) {
1.50 jfb 2079: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2080: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2081: RCS_TOKSTR(rfp));
2082: return (-1);
2083: }
2084:
2085: symp = (struct rcs_sym *)malloc(sizeof(*symp));
2086: if (symp == NULL) {
1.50 jfb 2087: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2088: cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
2089: return (-1);
2090: }
1.39 joris 2091: symp->rs_name = cvs_strdup(RCS_TOKSTR(rfp));
1.10 joris 2092: if (symp->rs_name == NULL) {
2093: cvs_log(LP_ERRNO, "failed to duplicate rcs token");
2094: free(symp);
2095: return (-1);
2096: }
2097:
1.1 jfb 2098: symp->rs_num = rcsnum_alloc();
1.11 joris 2099: if (symp->rs_num == NULL) {
2100: cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
1.39 joris 2101: cvs_strfree(symp->rs_name);
1.11 joris 2102: free(symp);
2103: return (-1);
2104: }
1.1 jfb 2105:
2106: type = rcs_gettok(rfp);
2107: if (type != RCS_TOK_COLON) {
1.50 jfb 2108: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2109: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2110: RCS_TOKSTR(rfp));
1.11 joris 2111: rcsnum_free(symp->rs_num);
1.39 joris 2112: cvs_strfree(symp->rs_name);
1.1 jfb 2113: free(symp);
2114: return (-1);
2115: }
2116:
2117: type = rcs_gettok(rfp);
2118: if (type != RCS_TOK_NUM) {
1.50 jfb 2119: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2120: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2121: RCS_TOKSTR(rfp));
1.11 joris 2122: rcsnum_free(symp->rs_num);
1.39 joris 2123: cvs_strfree(symp->rs_name);
1.1 jfb 2124: free(symp);
2125: return (-1);
2126: }
2127:
2128: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2129: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2130: RCS_TOKSTR(rfp));
1.11 joris 2131: rcsnum_free(symp->rs_num);
1.39 joris 2132: cvs_strfree(symp->rs_name);
1.1 jfb 2133: free(symp);
2134: return (-1);
2135: }
2136:
1.43 jfb 2137: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
1.1 jfb 2138: }
2139:
2140: return (0);
2141: }
2142:
2143: /*
2144: * rcs_parse_locks()
2145: *
2146: * Parse the lock list given as value to the `locks' keyword.
2147: * Returns 0 on success, or -1 on failure.
2148: */
2149: static int
2150: rcs_parse_locks(RCSFILE *rfp)
2151: {
2152: int type;
2153: struct rcs_lock *lkp;
2154:
2155: for (;;) {
2156: type = rcs_gettok(rfp);
2157: if (type == RCS_TOK_SCOLON)
2158: break;
2159:
2160: if (type != RCS_TOK_ID) {
1.50 jfb 2161: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2162: cvs_log(LP_ERR, "unexpected token `%s' in lock list",
2163: RCS_TOKSTR(rfp));
2164: return (-1);
2165: }
2166:
2167: lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
2168: if (lkp == NULL) {
2169: cvs_log(LP_ERRNO, "failed to allocate RCS lock");
2170: return (-1);
2171: }
2172: lkp->rl_num = rcsnum_alloc();
1.11 joris 2173: if (lkp->rl_num == NULL) {
2174: free(lkp);
2175: return (-1);
2176: }
1.1 jfb 2177:
2178: type = rcs_gettok(rfp);
2179: if (type != RCS_TOK_COLON) {
1.50 jfb 2180: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2181: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2182: RCS_TOKSTR(rfp));
1.37 tedu 2183: rcsnum_free(lkp->rl_num);
1.1 jfb 2184: free(lkp);
2185: return (-1);
2186: }
2187:
2188: type = rcs_gettok(rfp);
2189: if (type != RCS_TOK_NUM) {
1.50 jfb 2190: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2191: cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
2192: RCS_TOKSTR(rfp));
1.37 tedu 2193: rcsnum_free(lkp->rl_num);
1.1 jfb 2194: free(lkp);
2195: return (-1);
2196: }
2197:
2198: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2199: cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
2200: RCS_TOKSTR(rfp));
1.37 tedu 2201: rcsnum_free(lkp->rl_num);
1.1 jfb 2202: free(lkp);
2203: return (-1);
2204: }
2205:
2206: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2207: }
2208:
2209: /* check if we have a `strict' */
2210: type = rcs_gettok(rfp);
2211: if (type != RCS_TOK_STRICT) {
2212: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14 deraadt 2213: } else {
1.26 jfb 2214: rfp->rf_flags |= RCS_SLOCK;
1.1 jfb 2215:
2216: type = rcs_gettok(rfp);
2217: if (type != RCS_TOK_SCOLON) {
1.50 jfb 2218: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2219: cvs_log(LP_ERR,
2220: "missing semi-colon after `strict' keyword");
2221: return (-1);
2222: }
2223: }
2224:
2225: return (0);
2226: }
2227:
2228: /*
2229: * rcs_parse_branches()
2230: *
2231: * Parse the list of branches following a `branches' keyword in a delta.
2232: * Returns 0 on success, or -1 on failure.
2233: */
2234: static int
2235: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2236: {
2237: int type;
2238: struct rcs_branch *brp;
2239:
2240: for (;;) {
2241: type = rcs_gettok(rfp);
2242: if (type == RCS_TOK_SCOLON)
2243: break;
2244:
2245: if (type != RCS_TOK_NUM) {
1.50 jfb 2246: rcs_errno = RCS_ERR_PARSE;
1.1 jfb 2247: cvs_log(LP_ERR,
2248: "unexpected token `%s' in list of branches",
2249: RCS_TOKSTR(rfp));
2250: return (-1);
2251: }
2252:
2253: brp = (struct rcs_branch *)malloc(sizeof(*brp));
2254: if (brp == NULL) {
1.50 jfb 2255: rcs_errno = RCS_ERR_ERRNO;
1.1 jfb 2256: cvs_log(LP_ERRNO, "failed to allocate RCS branch");
2257: return (-1);
2258: }
1.46 jfb 2259: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
1.11 joris 2260: if (brp->rb_num == NULL) {
2261: free(brp);
2262: return (-1);
2263: }
1.1 jfb 2264:
2265: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2266: }
2267:
2268: return (0);
2269: }
2270:
2271: /*
2272: * rcs_freedelta()
2273: *
2274: * Free the contents of a delta structure.
2275: */
1.18 jfb 2276: static void
1.1 jfb 2277: rcs_freedelta(struct rcs_delta *rdp)
2278: {
1.12 jfb 2279: struct rcs_branch *rb;
1.1 jfb 2280: struct rcs_delta *crdp;
2281:
1.12 jfb 2282: if (rdp->rd_num != NULL)
2283: rcsnum_free(rdp->rd_num);
2284: if (rdp->rd_next != NULL)
2285: rcsnum_free(rdp->rd_next);
2286:
1.1 jfb 2287: if (rdp->rd_author != NULL)
1.39 joris 2288: cvs_strfree(rdp->rd_author);
1.1 jfb 2289: if (rdp->rd_state != NULL)
1.39 joris 2290: cvs_strfree(rdp->rd_state);
1.1 jfb 2291: if (rdp->rd_log != NULL)
1.39 joris 2292: cvs_strfree(rdp->rd_log);
1.1 jfb 2293: if (rdp->rd_text != NULL)
1.45 jfb 2294: free(rdp->rd_text);
1.12 jfb 2295:
2296: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2297: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2298: rcsnum_free(rb->rb_num);
2299: free(rb);
2300: }
1.1 jfb 2301:
2302: while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
2303: TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
2304: rcs_freedelta(crdp);
2305: }
2306:
2307: free(rdp);
2308: }
2309:
2310: /*
2311: * rcs_freepdata()
2312: *
2313: * Free the contents of the parser data structure.
2314: */
2315: static void
2316: rcs_freepdata(struct rcs_pdata *pd)
2317: {
2318: if (pd->rp_file != NULL)
2319: (void)fclose(pd->rp_file);
2320: if (pd->rp_buf != NULL)
2321: free(pd->rp_buf);
2322: free(pd);
2323: }
2324:
2325: /*
2326: * rcs_gettok()
2327: *
2328: * Get the next RCS token from the string <str>.
2329: */
2330: static int
2331: rcs_gettok(RCSFILE *rfp)
2332: {
2333: u_int i;
2334: int ch, last, type;
1.18 jfb 2335: size_t len;
2336: char *bp;
1.1 jfb 2337: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2338:
2339: type = RCS_TOK_ERR;
2340: bp = pdp->rp_buf;
1.42 jfb 2341: pdp->rp_tlen = 0;
1.1 jfb 2342: *bp = '\0';
2343:
2344: if (pdp->rp_pttype != RCS_TOK_ERR) {
2345: type = pdp->rp_pttype;
2346: strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
2347: pdp->rp_pttype = RCS_TOK_ERR;
2348: return (type);
2349: }
2350:
2351: /* skip leading whitespace */
2352: /* XXX we must skip backspace too for compatibility, should we? */
2353: do {
2354: ch = getc(pdp->rp_file);
2355: if (ch == '\n')
1.18 jfb 2356: pdp->rp_lines++;
1.1 jfb 2357: } while (isspace(ch));
2358:
2359: if (ch == EOF) {
2360: type = RCS_TOK_EOF;
1.14 deraadt 2361: } else if (ch == ';') {
1.1 jfb 2362: type = RCS_TOK_SCOLON;
1.14 deraadt 2363: } else if (ch == ':') {
1.1 jfb 2364: type = RCS_TOK_COLON;
1.14 deraadt 2365: } else if (isalpha(ch)) {
1.31 jfb 2366: type = RCS_TOK_ID;
1.1 jfb 2367: *(bp++) = ch;
1.18 jfb 2368: for (;;) {
1.1 jfb 2369: ch = getc(pdp->rp_file);
1.11 joris 2370: if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1 jfb 2371: ungetc(ch, pdp->rp_file);
2372: break;
2373: }
2374: *(bp++) = ch;
1.42 jfb 2375: pdp->rp_tlen++;
1.18 jfb 2376: if (bp == pdp->rp_bufend - 1) {
2377: len = bp - pdp->rp_buf;
2378: if (rcs_growbuf(rfp) < 0) {
2379: type = RCS_TOK_ERR;
2380: break;
2381: }
2382: bp = pdp->rp_buf + len;
2383: }
1.1 jfb 2384: }
2385: *bp = '\0';
2386:
1.18 jfb 2387: if (type != RCS_TOK_ERR) {
2388: for (i = 0; i < RCS_NKEYS; i++) {
2389: if (strcmp(rcs_keys[i].rk_str,
2390: pdp->rp_buf) == 0) {
2391: type = rcs_keys[i].rk_id;
2392: break;
2393: }
1.1 jfb 2394: }
2395: }
1.14 deraadt 2396: } else if (ch == '@') {
1.1 jfb 2397: /* we have a string */
1.18 jfb 2398: type = RCS_TOK_STRING;
1.1 jfb 2399: for (;;) {
2400: ch = getc(pdp->rp_file);
2401: if (ch == '@') {
2402: ch = getc(pdp->rp_file);
2403: if (ch != '@') {
2404: ungetc(ch, pdp->rp_file);
2405: break;
2406: }
1.14 deraadt 2407: } else if (ch == '\n')
1.18 jfb 2408: pdp->rp_lines++;
1.1 jfb 2409:
2410: *(bp++) = ch;
1.42 jfb 2411: pdp->rp_tlen++;
1.18 jfb 2412: if (bp == pdp->rp_bufend - 1) {
2413: len = bp - pdp->rp_buf;
2414: if (rcs_growbuf(rfp) < 0) {
2415: type = RCS_TOK_ERR;
2416: break;
2417: }
2418: bp = pdp->rp_buf + len;
2419: }
1.1 jfb 2420: }
2421:
2422: *bp = '\0';
1.14 deraadt 2423: } else if (isdigit(ch)) {
1.1 jfb 2424: *(bp++) = ch;
2425: last = ch;
2426: type = RCS_TOK_NUM;
2427:
2428: for (;;) {
2429: ch = getc(pdp->rp_file);
1.18 jfb 2430: if (bp == pdp->rp_bufend)
1.1 jfb 2431: break;
2432: if (!isdigit(ch) && ch != '.') {
2433: ungetc(ch, pdp->rp_file);
2434: break;
2435: }
2436:
2437: if (last == '.' && ch == '.') {
2438: type = RCS_TOK_ERR;
2439: break;
2440: }
2441: last = ch;
2442: *(bp++) = ch;
1.42 jfb 2443: pdp->rp_tlen++;
1.1 jfb 2444: }
1.18 jfb 2445: *bp = '\0';
1.1 jfb 2446: }
2447:
2448: return (type);
2449: }
2450:
2451: /*
2452: * rcs_pushtok()
2453: *
2454: * Push a token back in the parser's token buffer.
2455: */
2456: static int
2457: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2458: {
2459: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2460:
2461: if (pdp->rp_pttype != RCS_TOK_ERR)
2462: return (-1);
2463:
2464: pdp->rp_pttype = type;
2465: strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
2466: return (0);
2467: }
2468:
2469:
2470: /*
2471: * rcs_splitlines()
2472: *
2473: * Split the contents of a file into a list of lines.
2474: */
2475: static struct rcs_foo*
2476: rcs_splitlines(const char *fcont)
2477: {
2478: char *dcp;
2479: struct rcs_foo *foo;
2480: struct rcs_line *lp;
2481:
2482: foo = (struct rcs_foo *)malloc(sizeof(*foo));
2483: if (foo == NULL) {
2484: cvs_log(LP_ERR, "failed to allocate line structure");
2485: return (NULL);
2486: }
2487: TAILQ_INIT(&(foo->rl_lines));
2488: foo->rl_nblines = 0;
2489: foo->rl_data = strdup(fcont);
2490: if (foo->rl_data == NULL) {
2491: cvs_log(LP_ERRNO, "failed to copy file contents");
2492: free(foo);
2493: return (NULL);
2494: }
2495:
2496: /*
2497: * Add a first bogus line with line number 0. This is used so we
2498: * can position the line pointer before 1 when changing the first line
2499: * in rcs_patch().
2500: */
2501: lp = (struct rcs_line *)malloc(sizeof(*lp));
1.38 joris 2502: if (lp == NULL) {
2503: rcs_freefoo(foo);
1.1 jfb 2504: return (NULL);
1.38 joris 2505: }
1.5 vincent 2506:
1.1 jfb 2507: lp->rl_line = NULL;
2508: lp->rl_lineno = 0;
2509: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2510:
2511:
2512: for (dcp = foo->rl_data; *dcp != '\0';) {
2513: lp = (struct rcs_line *)malloc(sizeof(*lp));
2514: if (lp == NULL) {
1.38 joris 2515: rcs_freefoo(foo);
1.1 jfb 2516: cvs_log(LP_ERR, "failed to allocate line entry");
2517: return (NULL);
2518: }
2519:
2520: lp->rl_line = dcp;
2521: lp->rl_lineno = ++(foo->rl_nblines);
2522: TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
2523:
2524: dcp = strchr(dcp, '\n');
2525: if (dcp == NULL) {
2526: break;
2527: }
2528: *(dcp++) = '\0';
2529: }
2530:
2531: return (foo);
1.5 vincent 2532: }
2533:
2534: static void
2535: rcs_freefoo(struct rcs_foo *fp)
2536: {
2537: struct rcs_line *lp;
2538:
2539: while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
2540: TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
2541: free(lp);
2542: }
2543: free(fp->rl_data);
2544: free(fp);
1.18 jfb 2545: }
2546:
2547: /*
2548: * rcs_growbuf()
2549: *
2550: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2551: * RCS_BUFEXTSIZE.
2552: * In case of failure, the original buffer is left unmodified.
2553: * Returns 0 on success, or -1 on failure.
2554: */
2555: static int
2556: rcs_growbuf(RCSFILE *rf)
2557: {
2558: void *tmp;
2559: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2560:
2561: tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
2562: if (tmp == NULL) {
1.50 jfb 2563: rcs_errno = RCS_ERR_ERRNO;
1.18 jfb 2564: cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
2565: return (-1);
2566: }
2567:
2568: pdp->rp_buf = (char *)tmp;
2569: pdp->rp_blen += RCS_BUFEXTSIZE;
2570: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.42 jfb 2571:
2572: return (0);
2573: }
2574:
2575: /*
2576: * rcs_strprint()
2577: *
2578: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2579: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2580: * binary data.
2581: */
2582: static int
2583: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2584: {
2585: const u_char *ap, *ep, *sp;
2586: size_t ret;
1.52 jfb 2587:
2588: if (slen == 0)
2589: return (0);
1.42 jfb 2590:
2591: ep = str + slen - 1;
2592:
2593: for (sp = str; sp <= ep;) {
2594: ap = memchr(sp, '@', ep - sp);
2595: if (ap == NULL)
2596: ap = ep;
2597: ret = fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2598:
2599: if (*ap == '@')
2600: putc('@', stream);
2601: sp = ap + 1;
2602: }
1.18 jfb 2603:
2604: return (0);
1.1 jfb 2605: }