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