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