Annotation of src/usr.bin/rcs/rcs.c, Revision 1.47
1.47 ! tobias 1: /* $OpenBSD: rcs.c,v 1.46 2008/02/02 16:21:38 xsa 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: }
1.47 ! tobias 1325:
! 1326: rcs_freelines(plines);
1.46 xsa 1327:
1328: *ladded = added;
1329: *lremoved = removed;
1330: }
1331:
1.1 joris 1332:
1333: /*
1334: * rcs_rev_add()
1335: *
1336: * Add a revision to the RCS file <rf>. The new revision's number can be
1337: * specified in <rev> (which can also be RCS_HEAD_REV, in which case the
1338: * new revision will have a number equal to the previous head revision plus
1339: * one). The <msg> argument specifies the log message for that revision, and
1340: * <date> specifies the revision's date (a value of -1 is
1341: * equivalent to using the current time).
1342: * If <username> is NULL, set the author for this revision to the current user.
1343: * Otherwise, set it to <username>.
1344: * Returns 0 on success, or -1 on failure.
1345: */
1346: int
1347: rcs_rev_add(RCSFILE *rf, RCSNUM *rev, const char *msg, time_t date,
1348: const char *username)
1349: {
1350: time_t now;
1351: struct passwd *pw;
1352: struct rcs_delta *ordp, *rdp;
1353:
1354: if (rev == RCS_HEAD_REV) {
1355: if (rf->rf_flags & RCS_CREATE) {
1356: if ((rev = rcsnum_parse(RCS_HEAD_INIT)) == NULL)
1357: return (-1);
1358: rf->rf_head = rcsnum_alloc();
1359: rcsnum_cpy(rev, rf->rf_head, 0);
1360: } else {
1361: rev = rcsnum_inc(rf->rf_head);
1362: }
1363: } else {
1364: if ((rdp = rcs_findrev(rf, rev)) != NULL) {
1365: rcs_errno = RCS_ERR_DUPENT;
1366: return (-1);
1367: }
1368: }
1369:
1370: if ((pw = getpwuid(getuid())) == NULL)
1371: errx(1, "getpwuid failed");
1372:
1373: rdp = xcalloc(1, sizeof(*rdp));
1374:
1375: TAILQ_INIT(&(rdp->rd_branches));
1376:
1377: rdp->rd_num = rcsnum_alloc();
1378: rcsnum_cpy(rev, rdp->rd_num, 0);
1379:
1380: rdp->rd_next = rcsnum_alloc();
1381:
1382: if (!(rf->rf_flags & RCS_CREATE)) {
1383: /* next should point to the previous HEAD */
1384: ordp = TAILQ_FIRST(&(rf->rf_delta));
1385: rcsnum_cpy(ordp->rd_num, rdp->rd_next, 0);
1386: }
1387:
1388:
1389: if (username == NULL)
1390: username = pw->pw_name;
1391:
1392: rdp->rd_author = xstrdup(username);
1393: rdp->rd_state = xstrdup(RCS_STATE_EXP);
1394: rdp->rd_log = xstrdup(msg);
1395:
1396: if (date != (time_t)(-1))
1397: now = date;
1398: else
1399: time(&now);
1400: gmtime_r(&now, &(rdp->rd_date));
1401:
1402: TAILQ_INSERT_HEAD(&(rf->rf_delta), rdp, rd_list);
1403: rf->rf_ndelta++;
1404:
1405: /* not synced anymore */
1406: rf->rf_flags &= ~RCS_SYNCED;
1407:
1408: return (0);
1409: }
1410:
1411: /*
1412: * rcs_rev_remove()
1413: *
1414: * Remove the revision whose number is <rev> from the RCS file <rf>.
1415: */
1416: int
1417: rcs_rev_remove(RCSFILE *rf, RCSNUM *rev)
1418: {
1.12 niallo 1419: char *path_tmp1, *path_tmp2;
1.1 joris 1420: struct rcs_delta *rdp, *prevrdp, *nextrdp;
1.12 niallo 1421: BUF *newdeltatext, *nextbuf, *prevbuf, *newdiff;
1422:
1423: nextrdp = prevrdp = NULL;
1.17 niallo 1424: path_tmp1 = path_tmp2 = NULL;
1.1 joris 1425:
1426: if (rev == RCS_HEAD_REV)
1427: rev = rf->rf_head;
1428:
1429: /* do we actually have that revision? */
1430: if ((rdp = rcs_findrev(rf, rev)) == NULL) {
1431: rcs_errno = RCS_ERR_NOENT;
1432: return (-1);
1433: }
1434:
1435: /*
1436: * This is confusing, the previous delta is next in the TAILQ list.
1437: * the next delta is the previous one in the TAILQ list.
1438: *
1439: * When the HEAD revision got specified, nextrdp will be NULL.
1440: * When the first revision got specified, prevrdp will be NULL.
1441: */
1442: prevrdp = (struct rcs_delta *)TAILQ_NEXT(rdp, rd_list);
1443: nextrdp = (struct rcs_delta *)TAILQ_PREV(rdp, rcs_tqh, rd_list);
1444:
1.12 niallo 1445: newdeltatext = prevbuf = nextbuf = NULL;
1.1 joris 1446:
1447: if (prevrdp != NULL) {
1448: if ((prevbuf = rcs_getrev(rf, prevrdp->rd_num)) == NULL)
1449: errx(1, "error getting revision");
1450: }
1451:
1452: if (prevrdp != NULL && nextrdp != NULL) {
1453: if ((nextbuf = rcs_getrev(rf, nextrdp->rd_num)) == NULL)
1454: errx(1, "error getting revision");
1455:
1456: newdiff = rcs_buf_alloc(64, BUF_AUTOEXT);
1457:
1458: /* calculate new diff */
1.7 xsa 1459: (void)xasprintf(&path_tmp1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.22 ray 1460: rcs_buf_write_stmp(nextbuf, path_tmp1);
1.1 joris 1461: rcs_buf_free(nextbuf);
1462:
1.7 xsa 1463: (void)xasprintf(&path_tmp2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.22 ray 1464: rcs_buf_write_stmp(prevbuf, path_tmp2);
1.1 joris 1465: rcs_buf_free(prevbuf);
1466:
1467: diff_format = D_RCSDIFF;
1.41 ray 1468: if (diffreg(path_tmp1, path_tmp2, newdiff, 0) == D_ERROR)
1469: errx(1, "diffreg failed");
1.1 joris 1470:
1.12 niallo 1471: newdeltatext = newdiff;
1.1 joris 1472: } else if (nextrdp == NULL && prevrdp != NULL) {
1.12 niallo 1473: newdeltatext = prevbuf;
1.1 joris 1474: }
1475:
1476: if (newdeltatext != NULL) {
1477: if (rcs_deltatext_set(rf, prevrdp->rd_num, newdeltatext) < 0)
1478: errx(1, "error setting new deltatext");
1479: }
1480:
1481: TAILQ_REMOVE(&(rf->rf_delta), rdp, rd_list);
1482:
1483: /* update pointers */
1484: if (prevrdp != NULL && nextrdp != NULL) {
1485: rcsnum_cpy(prevrdp->rd_num, nextrdp->rd_next, 0);
1486: } else if (prevrdp != NULL) {
1487: if (rcs_head_set(rf, prevrdp->rd_num) < 0)
1488: errx(1, "rcs_head_set failed");
1489: } else if (nextrdp != NULL) {
1490: rcsnum_free(nextrdp->rd_next);
1491: nextrdp->rd_next = rcsnum_alloc();
1492: } else {
1493: rcsnum_free(rf->rf_head);
1494: rf->rf_head = NULL;
1495: }
1496:
1497: rf->rf_ndelta--;
1498: rf->rf_flags &= ~RCS_SYNCED;
1499:
1500: rcs_freedelta(rdp);
1501:
1.7 xsa 1502: if (path_tmp1 != NULL)
1503: xfree(path_tmp1);
1504: if (path_tmp2 != NULL)
1505: xfree(path_tmp2);
1.1 joris 1506:
1507: return (0);
1508: }
1509:
1510: /*
1511: * rcs_findrev()
1512: *
1513: * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
1514: * The revision number is given in <rev>.
1515: *
1516: * If the given revision is a branch number, we translate it into the latest
1517: * revision on the branch.
1518: *
1519: * Returns a pointer to the delta on success, or NULL on failure.
1520: */
1521: struct rcs_delta *
1522: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
1523: {
1524: u_int cmplen;
1525: struct rcs_delta *rdp;
1526: RCSNUM *brev, *frev;
1527:
1528: /*
1529: * We need to do more parsing if the last revision in the linked list
1530: * is greater than the requested revision.
1531: */
1532: rdp = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1533: if (rdp == NULL ||
1534: rcsnum_cmp(rdp->rd_num, rev, 0) == -1) {
1535: rcs_parse_deltas(rfp, rev);
1536: }
1537:
1538: /*
1539: * Translate a branch into the latest revision on the branch itself.
1540: */
1541: if (RCSNUM_ISBRANCH(rev)) {
1542: brev = rcsnum_brtorev(rev);
1543: frev = brev;
1544: for (;;) {
1545: rdp = rcs_findrev(rfp, frev);
1546: if (rdp == NULL)
1547: return (NULL);
1548:
1549: if (rdp->rd_next->rn_len == 0)
1550: break;
1551:
1552: frev = rdp->rd_next;
1553: }
1554:
1555: rcsnum_free(brev);
1556: return (rdp);
1557: }
1558:
1559: cmplen = rev->rn_len;
1560:
1561: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
1562: if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0)
1563: return (rdp);
1564: }
1565:
1566: return (NULL);
1567: }
1568:
1569: /*
1570: * rcs_kwexp_set()
1571: *
1572: * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
1573: */
1574: void
1575: rcs_kwexp_set(RCSFILE *file, int mode)
1576: {
1577: int i;
1578: char *tmp, buf[8] = "";
1579:
1580: if (RCS_KWEXP_INVAL(mode))
1581: return;
1582:
1583: i = 0;
1584: if (mode == RCS_KWEXP_NONE)
1585: buf[0] = 'b';
1586: else if (mode == RCS_KWEXP_OLD)
1587: buf[0] = 'o';
1588: else {
1589: if (mode & RCS_KWEXP_NAME)
1590: buf[i++] = 'k';
1591: if (mode & RCS_KWEXP_VAL)
1592: buf[i++] = 'v';
1593: if (mode & RCS_KWEXP_LKR)
1594: buf[i++] = 'l';
1595: }
1596:
1597: tmp = xstrdup(buf);
1598: if (file->rf_expand != NULL)
1599: xfree(file->rf_expand);
1600: file->rf_expand = tmp;
1601: /* not synced anymore */
1602: file->rf_flags &= ~RCS_SYNCED;
1603: }
1604:
1605: /*
1606: * rcs_kwexp_get()
1607: *
1608: * Retrieve the keyword expansion mode to be used for the RCS file <file>.
1609: */
1610: int
1611: rcs_kwexp_get(RCSFILE *file)
1612: {
1613: return rcs_kflag_get(file->rf_expand);
1614: }
1615:
1616: /*
1617: * rcs_kflag_get()
1618: *
1619: * Get the keyword expansion mode from a set of character flags given in
1620: * <flags> and return the appropriate flag mask. In case of an error, the
1621: * returned mask will have the RCS_KWEXP_ERR bit set to 1.
1622: */
1623: int
1624: rcs_kflag_get(const char *flags)
1625: {
1626: int fl;
1627: size_t len;
1628: const char *fp;
1629:
1630: fl = 0;
1631: len = strlen(flags);
1632:
1633: for (fp = flags; *fp != '\0'; fp++) {
1634: if (*fp == 'k')
1635: fl |= RCS_KWEXP_NAME;
1636: else if (*fp == 'v')
1637: fl |= RCS_KWEXP_VAL;
1638: else if (*fp == 'l')
1639: fl |= RCS_KWEXP_LKR;
1640: else if (*fp == 'o') {
1641: if (len != 1)
1642: fl |= RCS_KWEXP_ERR;
1643: fl |= RCS_KWEXP_OLD;
1644: } else if (*fp == 'b') {
1645: if (len != 1)
1646: fl |= RCS_KWEXP_ERR;
1647: } else /* unknown letter */
1648: fl |= RCS_KWEXP_ERR;
1649: }
1650:
1651: return (fl);
1652: }
1653:
1654: /*
1655: * rcs_errstr()
1656: *
1657: * Get the error string matching the RCS error code <code>.
1658: */
1659: const char *
1660: rcs_errstr(int code)
1661: {
1662: const char *esp;
1663:
1664: if (code < 0 || (code >= (int)RCS_NERR && code != RCS_ERR_ERRNO))
1665: esp = NULL;
1666: else if (code == RCS_ERR_ERRNO)
1667: esp = strerror(errno);
1668: else
1669: esp = rcs_errstrs[code];
1670: return (esp);
1671: }
1672:
1673: /* rcs_parse_deltas()
1674: *
1675: * Parse deltas. If <rev> is not NULL, parse only as far as that
1676: * revision. If <rev> is NULL, parse all deltas.
1677: */
1678: static void
1679: rcs_parse_deltas(RCSFILE *rfp, RCSNUM *rev)
1680: {
1681: int ret;
1682: struct rcs_delta *enddelta;
1683:
1684: if ((rfp->rf_flags & PARSED_DELTAS) || (rfp->rf_flags & RCS_CREATE))
1685: return;
1686:
1687: for (;;) {
1688: ret = rcs_parse_delta(rfp);
1689: if (rev != NULL) {
1690: enddelta = TAILQ_LAST(&(rfp->rf_delta), rcs_dlist);
1691: if (rcsnum_cmp(enddelta->rd_num, rev, 0) == 0)
1692: break;
1693: }
1694: if (ret == 0) {
1695: rfp->rf_flags |= PARSED_DELTAS;
1696: break;
1697: }
1698: else if (ret == -1)
1699: errx(1, "error parsing deltas");
1700: }
1701: }
1702:
1703: /* rcs_parse_deltatexts()
1704: *
1705: * Parse deltatexts. If <rev> is not NULL, parse only as far as that
1706: * revision. If <rev> is NULL, parse everything.
1707: */
1708: static void
1709: rcs_parse_deltatexts(RCSFILE *rfp, RCSNUM *rev)
1710: {
1711: int ret;
1712: struct rcs_delta *rdp;
1713:
1714: if ((rfp->rf_flags & PARSED_DELTATEXTS) ||
1715: (rfp->rf_flags & RCS_CREATE))
1716: return;
1717:
1718: if (!(rfp->rf_flags & PARSED_DESC))
1719: rcs_parse_desc(rfp, rev);
1720: for (;;) {
1721: if (rev != NULL) {
1722: rdp = rcs_findrev(rfp, rev);
1723: if (rdp->rd_text != NULL)
1724: break;
1725: else
1726: ret = rcs_parse_deltatext(rfp);
1727: } else
1728: ret = rcs_parse_deltatext(rfp);
1729: if (ret == 0) {
1730: rfp->rf_flags |= PARSED_DELTATEXTS;
1731: break;
1732: }
1733: else if (ret == -1)
1734: errx(1, "problem parsing deltatexts");
1735: }
1736: }
1737:
1738: /* rcs_parse_desc()
1739: *
1740: * Parse RCS description.
1741: */
1742: static void
1743: rcs_parse_desc(RCSFILE *rfp, RCSNUM *rev)
1744: {
1745: int ret = 0;
1746:
1747: if ((rfp->rf_flags & PARSED_DESC) || (rfp->rf_flags & RCS_CREATE))
1748: return;
1749: if (!(rfp->rf_flags & PARSED_DELTAS))
1750: rcs_parse_deltas(rfp, rev);
1751: /* do parsing */
1752: ret = rcs_gettok(rfp);
1753: if (ret != RCS_TOK_DESC)
1754: errx(1, "token `%s' found where RCS desc expected",
1755: RCS_TOKSTR(rfp));
1756:
1757: ret = rcs_gettok(rfp);
1758: if (ret != RCS_TOK_STRING)
1759: errx(1, "token `%s' found where RCS desc expected",
1760: RCS_TOKSTR(rfp));
1761:
1762: rfp->rf_desc = xstrdup(RCS_TOKSTR(rfp));
1763: rfp->rf_flags |= PARSED_DESC;
1764: }
1765:
1766: /*
1767: * rcs_parse_init()
1768: *
1769: * Initial parsing of file <path>, which are in the RCS format.
1770: * Just does admin section.
1771: */
1772: static void
1773: rcs_parse_init(RCSFILE *rfp)
1774: {
1775: struct rcs_pdata *pdp;
1776:
1777: if (rfp->rf_flags & RCS_PARSED)
1778: return;
1779:
1780: pdp = xcalloc(1, sizeof(*pdp));
1781:
1782: pdp->rp_lines = 0;
1783: pdp->rp_pttype = RCS_TOK_ERR;
1784:
1.10 ray 1785: if ((pdp->rp_file = fdopen(rfp->rf_fd, "r")) == NULL)
1.42 xsa 1786: err(1, "fdopen: `%s'", rfp->rf_path);
1.1 joris 1787:
1788: pdp->rp_buf = xmalloc((size_t)RCS_BUFSIZE);
1789: pdp->rp_blen = RCS_BUFSIZE;
1790: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1791:
1792: /* ditch the strict lock */
1793: rfp->rf_flags &= ~RCS_SLOCK;
1794: rfp->rf_pdata = pdp;
1795:
1796: if (rcs_parse_admin(rfp) < 0) {
1797: rcs_freepdata(pdp);
1798: errx(1, "could not parse admin data");
1799: }
1800:
1801: if (rfp->rf_flags & RCS_PARSE_FULLY)
1802: rcs_parse_deltatexts(rfp, NULL);
1803:
1804: rfp->rf_flags |= RCS_SYNCED;
1805: }
1806:
1807: /*
1808: * rcs_parse_admin()
1809: *
1810: * Parse the administrative portion of an RCS file.
1811: * Returns the type of the first token found after the admin section on
1812: * success, or -1 on failure.
1813: */
1814: static int
1815: rcs_parse_admin(RCSFILE *rfp)
1816: {
1817: u_int i;
1818: int tok, ntok, hmask;
1819: struct rcs_key *rk;
1820:
1821: /* hmask is a mask of the headers already encountered */
1822: hmask = 0;
1823: for (;;) {
1824: tok = rcs_gettok(rfp);
1825: if (tok == RCS_TOK_ERR) {
1826: rcs_errno = RCS_ERR_PARSE;
1827: warnx("parse error in RCS admin section");
1828: goto fail;
1829: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1830: /*
1831: * Assume this is the start of the first delta or
1832: * that we are dealing with an empty RCS file and
1833: * we just found the description.
1834: */
1835: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1836: return (tok);
1837: }
1838:
1839: rk = NULL;
1840: for (i = 0; i < RCS_NKEYS; i++)
1841: if (rcs_keys[i].rk_id == tok)
1842: rk = &(rcs_keys[i]);
1843:
1844: if (hmask & (1 << tok)) {
1845: rcs_errno = RCS_ERR_PARSE;
1846: warnx("duplicate RCS key");
1847: goto fail;
1848: }
1849: hmask |= (1 << tok);
1850:
1851: switch (tok) {
1852: case RCS_TOK_HEAD:
1853: case RCS_TOK_BRANCH:
1854: case RCS_TOK_COMMENT:
1855: case RCS_TOK_EXPAND:
1856: ntok = rcs_gettok(rfp);
1857: if (ntok == RCS_TOK_SCOLON)
1858: break;
1859: if (ntok != rk->rk_val) {
1860: rcs_errno = RCS_ERR_PARSE;
1861: warnx("invalid value type for RCS key `%s'",
1862: rk->rk_str);
1863: }
1864:
1865: if (tok == RCS_TOK_HEAD) {
1866: if (rfp->rf_head == NULL)
1867: rfp->rf_head = rcsnum_alloc();
1868: rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1869: rfp->rf_head);
1870: } else if (tok == RCS_TOK_BRANCH) {
1871: if (rfp->rf_branch == NULL)
1872: rfp->rf_branch = rcsnum_alloc();
1873: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL,
1874: rfp->rf_branch) < 0)
1875: goto fail;
1876: } else if (tok == RCS_TOK_COMMENT) {
1877: rfp->rf_comment = xstrdup(RCS_TOKSTR(rfp));
1878: } else if (tok == RCS_TOK_EXPAND) {
1879: rfp->rf_expand = xstrdup(RCS_TOKSTR(rfp));
1880: }
1881:
1882: /* now get the expected semi-colon */
1883: ntok = rcs_gettok(rfp);
1884: if (ntok != RCS_TOK_SCOLON) {
1885: rcs_errno = RCS_ERR_PARSE;
1886: warnx("missing semi-colon after RCS `%s' key",
1887: rk->rk_str);
1888: goto fail;
1889: }
1890: break;
1891: case RCS_TOK_ACCESS:
1892: if (rcs_parse_access(rfp) < 0)
1893: goto fail;
1894: break;
1895: case RCS_TOK_SYMBOLS:
1896: if (rcs_parse_symbols(rfp) < 0)
1897: goto fail;
1898: break;
1899: case RCS_TOK_LOCKS:
1900: if (rcs_parse_locks(rfp) < 0)
1901: goto fail;
1902: break;
1903: default:
1904: rcs_errno = RCS_ERR_PARSE;
1905: warnx("unexpected token `%s' in RCS admin section",
1906: RCS_TOKSTR(rfp));
1907: goto fail;
1908: }
1909: }
1910:
1911: fail:
1912: return (-1);
1913: }
1914:
1915: /*
1916: * rcs_parse_delta()
1917: *
1918: * Parse an RCS delta section and allocate the structure to store that delta's
1919: * information in the <rfp> delta list.
1920: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
1921: * -1 on error.
1922: */
1923: static int
1924: rcs_parse_delta(RCSFILE *rfp)
1925: {
1926: int ret, tok, ntok, hmask;
1927: u_int i;
1928: char *tokstr;
1929: RCSNUM *datenum;
1930: struct rcs_delta *rdp;
1931: struct rcs_key *rk;
1932:
1933: tok = rcs_gettok(rfp);
1934: if (tok == RCS_TOK_DESC) {
1935: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1936: return (0);
1937: } else if (tok != RCS_TOK_NUM) {
1938: rcs_errno = RCS_ERR_PARSE;
1939: warnx("unexpected token `%s' at start of delta",
1940: RCS_TOKSTR(rfp));
1941: return (-1);
1942: }
1.34 niallo 1943:
1944: rdp = xcalloc(1, sizeof(*rdp));
1945:
1946: rdp->rd_num = rcsnum_alloc();
1947: rdp->rd_next = rcsnum_alloc();
1948:
1949: TAILQ_INIT(&(rdp->rd_branches));
1950:
1.1 joris 1951: rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
1952:
1953: hmask = 0;
1954: ret = 0;
1955: tokstr = NULL;
1956:
1957: for (;;) {
1958: tok = rcs_gettok(rfp);
1959: if (tok == RCS_TOK_ERR) {
1960: rcs_errno = RCS_ERR_PARSE;
1961: warnx("parse error in RCS delta section");
1962: rcs_freedelta(rdp);
1963: return (-1);
1964: } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1965: rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1966: ret = (tok == RCS_TOK_NUM ? 1 : 0);
1967: break;
1968: }
1969:
1970: rk = NULL;
1971: for (i = 0; i < RCS_NKEYS; i++)
1972: if (rcs_keys[i].rk_id == tok)
1973: rk = &(rcs_keys[i]);
1974:
1975: if (hmask & (1 << tok)) {
1976: rcs_errno = RCS_ERR_PARSE;
1977: warnx("duplicate RCS key");
1978: rcs_freedelta(rdp);
1979: return (-1);
1980: }
1981: hmask |= (1 << tok);
1982:
1983: switch (tok) {
1984: case RCS_TOK_DATE:
1985: case RCS_TOK_AUTHOR:
1986: case RCS_TOK_STATE:
1987: case RCS_TOK_NEXT:
1.43 sturm 1988: case RCS_TOK_COMMITID:
1.1 joris 1989: ntok = rcs_gettok(rfp);
1990: if (ntok == RCS_TOK_SCOLON) {
1991: if (rk->rk_flags & RCS_VOPT)
1992: break;
1993: else {
1994: rcs_errno = RCS_ERR_PARSE;
1995: warnx("missing mandatory "
1996: "value to RCS key `%s'",
1997: rk->rk_str);
1998: rcs_freedelta(rdp);
1999: return (-1);
2000: }
2001: }
2002:
2003: if (ntok != rk->rk_val) {
2004: rcs_errno = RCS_ERR_PARSE;
2005: warnx("invalid value type for RCS key `%s'",
2006: rk->rk_str);
2007: rcs_freedelta(rdp);
2008: return (-1);
2009: }
2010:
2011: if (tokstr != NULL)
2012: xfree(tokstr);
2013: tokstr = xstrdup(RCS_TOKSTR(rfp));
2014: /* now get the expected semi-colon */
2015: ntok = rcs_gettok(rfp);
2016: if (ntok != RCS_TOK_SCOLON) {
2017: rcs_errno = RCS_ERR_PARSE;
2018: warnx("missing semi-colon after RCS `%s' key",
2019: rk->rk_str);
2020: xfree(tokstr);
2021: rcs_freedelta(rdp);
2022: return (-1);
2023: }
2024:
2025: if (tok == RCS_TOK_DATE) {
2026: if ((datenum = rcsnum_parse(tokstr)) == NULL) {
2027: xfree(tokstr);
2028: rcs_freedelta(rdp);
2029: return (-1);
2030: }
2031: if (datenum->rn_len != 6) {
2032: rcs_errno = RCS_ERR_PARSE;
2033: warnx("RCS date specification has %s "
2034: "fields",
2035: (datenum->rn_len > 6) ? "too many" :
2036: "missing");
2037: xfree(tokstr);
2038: rcs_freedelta(rdp);
2039: rcsnum_free(datenum);
2040: return (-1);
2041: }
2042: rdp->rd_date.tm_year = datenum->rn_id[0];
2043: if (rdp->rd_date.tm_year >= 1900)
2044: rdp->rd_date.tm_year -= 1900;
2045: rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
2046: rdp->rd_date.tm_mday = datenum->rn_id[2];
2047: rdp->rd_date.tm_hour = datenum->rn_id[3];
2048: rdp->rd_date.tm_min = datenum->rn_id[4];
2049: rdp->rd_date.tm_sec = datenum->rn_id[5];
2050: rcsnum_free(datenum);
2051: } else if (tok == RCS_TOK_AUTHOR) {
2052: rdp->rd_author = tokstr;
2053: tokstr = NULL;
2054: } else if (tok == RCS_TOK_STATE) {
2055: rdp->rd_state = tokstr;
2056: tokstr = NULL;
2057: } else if (tok == RCS_TOK_NEXT) {
2058: rcsnum_aton(tokstr, NULL, rdp->rd_next);
1.43 sturm 2059: } else if (tok == RCS_TOK_COMMITID) {
2060: /* XXX just parse it, no action yet */
1.1 joris 2061: }
2062: break;
2063: case RCS_TOK_BRANCHES:
2064: if (rcs_parse_branches(rfp, rdp) < 0) {
2065: rcs_freedelta(rdp);
2066: return (-1);
2067: }
2068: break;
2069: default:
2070: rcs_errno = RCS_ERR_PARSE;
2071: warnx("unexpected token `%s' in RCS delta",
2072: RCS_TOKSTR(rfp));
2073: rcs_freedelta(rdp);
2074: return (-1);
2075: }
2076: }
2077:
2078: if (tokstr != NULL)
2079: xfree(tokstr);
2080:
2081: TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
2082: rfp->rf_ndelta++;
2083:
2084: return (ret);
2085: }
2086:
2087: /*
2088: * rcs_parse_deltatext()
2089: *
2090: * Parse an RCS delta text section and fill in the log and text field of the
2091: * appropriate delta section.
2092: * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
2093: * -1 on error.
2094: */
2095: static int
2096: rcs_parse_deltatext(RCSFILE *rfp)
2097: {
2098: int tok;
2099: RCSNUM *tnum;
2100: struct rcs_delta *rdp;
2101:
2102: tok = rcs_gettok(rfp);
2103: if (tok == RCS_TOK_EOF)
2104: return (0);
2105:
2106: if (tok != RCS_TOK_NUM) {
2107: rcs_errno = RCS_ERR_PARSE;
2108: warnx("unexpected token `%s' at start of RCS delta text",
2109: RCS_TOKSTR(rfp));
2110: return (-1);
2111: }
2112:
2113: tnum = rcsnum_alloc();
2114: rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
2115:
2116: TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
2117: if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
2118: break;
2119: }
2120: rcsnum_free(tnum);
2121:
2122: if (rdp == NULL) {
2123: warnx("RCS delta text `%s' has no matching delta",
2124: RCS_TOKSTR(rfp));
2125: return (-1);
2126: }
2127:
2128: tok = rcs_gettok(rfp);
2129: if (tok != RCS_TOK_LOG) {
2130: rcs_errno = RCS_ERR_PARSE;
2131: warnx("unexpected token `%s' where RCS log expected",
2132: RCS_TOKSTR(rfp));
2133: return (-1);
2134: }
2135:
2136: tok = rcs_gettok(rfp);
2137: if (tok != RCS_TOK_STRING) {
2138: rcs_errno = RCS_ERR_PARSE;
2139: warnx("unexpected token `%s' where RCS log expected",
2140: RCS_TOKSTR(rfp));
2141: return (-1);
2142: }
2143: rdp->rd_log = xstrdup(RCS_TOKSTR(rfp));
2144: tok = rcs_gettok(rfp);
2145: if (tok != RCS_TOK_TEXT) {
2146: rcs_errno = RCS_ERR_PARSE;
2147: warnx("unexpected token `%s' where RCS text expected",
2148: RCS_TOKSTR(rfp));
2149: return (-1);
2150: }
2151:
2152: tok = rcs_gettok(rfp);
2153: if (tok != RCS_TOK_STRING) {
2154: rcs_errno = RCS_ERR_PARSE;
2155: warnx("unexpected token `%s' where RCS text expected",
2156: RCS_TOKSTR(rfp));
2157: return (-1);
2158: }
2159:
1.12 niallo 2160: if (RCS_TOKLEN(rfp) == 0) {
2161: rdp->rd_text = xmalloc(1);
2162: rdp->rd_text[0] = '\0';
2163: rdp->rd_tlen = 0;
2164: } else {
2165: rdp->rd_text = xmalloc(RCS_TOKLEN(rfp));
2166: memcpy(rdp->rd_text, RCS_TOKSTR(rfp), (RCS_TOKLEN(rfp)));
2167: rdp->rd_tlen = RCS_TOKLEN(rfp);
2168: }
1.1 joris 2169:
2170: return (1);
2171: }
2172:
2173: /*
2174: * rcs_parse_access()
2175: *
2176: * Parse the access list given as value to the `access' keyword.
2177: * Returns 0 on success, or -1 on failure.
2178: */
2179: static int
2180: rcs_parse_access(RCSFILE *rfp)
2181: {
2182: int type;
2183:
2184: while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
2185: if (type != RCS_TOK_ID) {
2186: rcs_errno = RCS_ERR_PARSE;
2187: warnx("unexpected token `%s' in access list",
2188: RCS_TOKSTR(rfp));
2189: return (-1);
2190: }
2191:
2192: if (rcs_access_add(rfp, RCS_TOKSTR(rfp)) < 0)
2193: return (-1);
2194: }
2195:
2196: return (0);
2197: }
2198:
2199: /*
2200: * rcs_parse_symbols()
2201: *
2202: * Parse the symbol list given as value to the `symbols' keyword.
2203: * Returns 0 on success, or -1 on failure.
2204: */
2205: static int
2206: rcs_parse_symbols(RCSFILE *rfp)
2207: {
2208: int type;
2209: struct rcs_sym *symp;
2210:
2211: for (;;) {
2212: type = rcs_gettok(rfp);
2213: if (type == RCS_TOK_SCOLON)
2214: break;
2215:
2216: if (type != RCS_TOK_ID) {
2217: rcs_errno = RCS_ERR_PARSE;
2218: warnx("unexpected token `%s' in symbol list",
2219: RCS_TOKSTR(rfp));
2220: return (-1);
2221: }
2222:
2223: symp = xmalloc(sizeof(*symp));
2224: symp->rs_name = xstrdup(RCS_TOKSTR(rfp));
2225: symp->rs_num = rcsnum_alloc();
2226:
2227: type = rcs_gettok(rfp);
2228: if (type != RCS_TOK_COLON) {
2229: rcs_errno = RCS_ERR_PARSE;
2230: warnx("unexpected token `%s' in symbol list",
2231: RCS_TOKSTR(rfp));
2232: rcsnum_free(symp->rs_num);
2233: xfree(symp->rs_name);
2234: xfree(symp);
2235: return (-1);
2236: }
2237:
2238: type = rcs_gettok(rfp);
2239: if (type != RCS_TOK_NUM) {
2240: rcs_errno = RCS_ERR_PARSE;
2241: warnx("unexpected token `%s' in symbol list",
2242: RCS_TOKSTR(rfp));
2243: rcsnum_free(symp->rs_num);
2244: xfree(symp->rs_name);
2245: xfree(symp);
2246: return (-1);
2247: }
2248:
2249: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
2250: warnx("failed to parse RCS NUM `%s'",
2251: RCS_TOKSTR(rfp));
2252: rcsnum_free(symp->rs_num);
2253: xfree(symp->rs_name);
2254: xfree(symp);
2255: return (-1);
2256: }
2257:
2258: TAILQ_INSERT_TAIL(&(rfp->rf_symbols), symp, rs_list);
2259: }
2260:
2261: return (0);
2262: }
2263:
2264: /*
2265: * rcs_parse_locks()
2266: *
2267: * Parse the lock list given as value to the `locks' keyword.
2268: * Returns 0 on success, or -1 on failure.
2269: */
2270: static int
2271: rcs_parse_locks(RCSFILE *rfp)
2272: {
2273: int type;
2274: struct rcs_lock *lkp;
2275:
2276: for (;;) {
2277: type = rcs_gettok(rfp);
2278: if (type == RCS_TOK_SCOLON)
2279: break;
2280:
2281: if (type != RCS_TOK_ID) {
2282: rcs_errno = RCS_ERR_PARSE;
2283: warnx("unexpected token `%s' in lock list",
2284: RCS_TOKSTR(rfp));
2285: return (-1);
2286: }
2287:
2288: lkp = xmalloc(sizeof(*lkp));
2289: lkp->rl_name = xstrdup(RCS_TOKSTR(rfp));
2290: lkp->rl_num = rcsnum_alloc();
2291:
2292: type = rcs_gettok(rfp);
2293: if (type != RCS_TOK_COLON) {
2294: rcs_errno = RCS_ERR_PARSE;
2295: warnx("unexpected token `%s' in symbol list",
2296: RCS_TOKSTR(rfp));
2297: rcsnum_free(lkp->rl_num);
2298: xfree(lkp->rl_name);
2299: xfree(lkp);
2300: return (-1);
2301: }
2302:
2303: type = rcs_gettok(rfp);
2304: if (type != RCS_TOK_NUM) {
2305: rcs_errno = RCS_ERR_PARSE;
2306: warnx("unexpected token `%s' in symbol list",
2307: RCS_TOKSTR(rfp));
2308: rcsnum_free(lkp->rl_num);
2309: xfree(lkp->rl_name);
2310: xfree(lkp);
2311: return (-1);
2312: }
2313:
2314: if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
2315: warnx("failed to parse RCS NUM `%s'",
2316: RCS_TOKSTR(rfp));
2317: rcsnum_free(lkp->rl_num);
2318: xfree(lkp->rl_name);
2319: xfree(lkp);
2320: return (-1);
2321: }
2322:
2323: TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
2324: }
2325:
2326: /* check if we have a `strict' */
2327: type = rcs_gettok(rfp);
2328: if (type != RCS_TOK_STRICT) {
2329: rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
2330: } else {
2331: rfp->rf_flags |= RCS_SLOCK;
2332:
2333: type = rcs_gettok(rfp);
2334: if (type != RCS_TOK_SCOLON) {
2335: rcs_errno = RCS_ERR_PARSE;
2336: warnx("missing semi-colon after `strict' keyword");
2337: return (-1);
2338: }
2339: }
2340:
2341: return (0);
2342: }
2343:
2344: /*
2345: * rcs_parse_branches()
2346: *
2347: * Parse the list of branches following a `branches' keyword in a delta.
2348: * Returns 0 on success, or -1 on failure.
2349: */
2350: static int
2351: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
2352: {
2353: int type;
2354: struct rcs_branch *brp;
2355:
2356: for (;;) {
2357: type = rcs_gettok(rfp);
2358: if (type == RCS_TOK_SCOLON)
2359: break;
2360:
2361: if (type != RCS_TOK_NUM) {
2362: rcs_errno = RCS_ERR_PARSE;
2363: warnx("unexpected token `%s' in list of branches",
2364: RCS_TOKSTR(rfp));
2365: return (-1);
2366: }
2367:
2368: brp = xmalloc(sizeof(*brp));
2369: brp->rb_num = rcsnum_parse(RCS_TOKSTR(rfp));
2370: if (brp->rb_num == NULL) {
2371: xfree(brp);
2372: return (-1);
2373: }
2374:
2375: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
2376: }
2377:
2378: return (0);
2379: }
2380:
2381: /*
2382: * rcs_freedelta()
2383: *
2384: * Free the contents of a delta structure.
2385: */
2386: static void
2387: rcs_freedelta(struct rcs_delta *rdp)
2388: {
2389: struct rcs_branch *rb;
2390:
2391: if (rdp->rd_num != NULL)
2392: rcsnum_free(rdp->rd_num);
2393: if (rdp->rd_next != NULL)
2394: rcsnum_free(rdp->rd_next);
2395:
2396: if (rdp->rd_author != NULL)
2397: xfree(rdp->rd_author);
2398: if (rdp->rd_locker != NULL)
2399: xfree(rdp->rd_locker);
2400: if (rdp->rd_state != NULL)
2401: xfree(rdp->rd_state);
2402: if (rdp->rd_log != NULL)
2403: xfree(rdp->rd_log);
2404: if (rdp->rd_text != NULL)
2405: xfree(rdp->rd_text);
2406:
2407: while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
2408: TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
2409: rcsnum_free(rb->rb_num);
2410: xfree(rb);
2411: }
2412:
2413: xfree(rdp);
2414: }
2415:
2416: /*
2417: * rcs_freepdata()
2418: *
2419: * Free the contents of the parser data structure.
2420: */
2421: static void
2422: rcs_freepdata(struct rcs_pdata *pd)
2423: {
2424: if (pd->rp_file != NULL)
2425: (void)fclose(pd->rp_file);
2426: if (pd->rp_buf != NULL)
2427: xfree(pd->rp_buf);
2428: xfree(pd);
2429: }
2430:
2431: /*
2432: * rcs_gettok()
2433: *
2434: * Get the next RCS token from the string <str>.
2435: */
2436: static int
2437: rcs_gettok(RCSFILE *rfp)
2438: {
2439: u_int i;
2440: int ch, last, type;
2441: size_t len;
2442: char *bp;
2443: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2444:
2445: type = RCS_TOK_ERR;
2446: bp = pdp->rp_buf;
2447: pdp->rp_tlen = 0;
2448: *bp = '\0';
2449:
2450: if (pdp->rp_pttype != RCS_TOK_ERR) {
2451: type = pdp->rp_pttype;
1.5 ray 2452: if (strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen) >=
2453: pdp->rp_blen)
2454: errx(1, "rcs_gettok: strlcpy");
1.1 joris 2455: pdp->rp_pttype = RCS_TOK_ERR;
2456: return (type);
2457: }
2458:
2459: /* skip leading whitespace */
2460: /* XXX we must skip backspace too for compatibility, should we? */
2461: do {
2462: ch = getc(pdp->rp_file);
2463: if (ch == '\n')
2464: pdp->rp_lines++;
2465: } while (isspace(ch));
2466:
2467: if (ch == EOF) {
2468: type = RCS_TOK_EOF;
2469: } else if (ch == ';') {
2470: type = RCS_TOK_SCOLON;
2471: } else if (ch == ':') {
2472: type = RCS_TOK_COLON;
1.39 niallo 2473: } else if (isalpha(ch) || ch == '_' || ch == '$' || ch == '.' || ch == '-') {
1.1 joris 2474: type = RCS_TOK_ID;
2475: *(bp++) = ch;
2476: for (;;) {
2477: ch = getc(pdp->rp_file);
1.32 millert 2478: if (ch == EOF) {
2479: type = RCS_TOK_EOF;
2480: break;
2481: } else if (!isalnum(ch) && ch != '_' && ch != '-' &&
1.39 niallo 2482: ch != '/' && ch != '.' && ch != '$') {
1.1 joris 2483: ungetc(ch, pdp->rp_file);
2484: break;
2485: }
2486: *(bp++) = ch;
2487: pdp->rp_tlen++;
2488: if (bp == pdp->rp_bufend - 1) {
2489: len = bp - pdp->rp_buf;
2490: rcs_growbuf(rfp);
2491: bp = pdp->rp_buf + len;
2492: }
2493: }
2494: *bp = '\0';
2495:
2496: if (type != RCS_TOK_ERR) {
2497: for (i = 0; i < RCS_NKEYS; i++) {
2498: if (strcmp(rcs_keys[i].rk_str,
2499: pdp->rp_buf) == 0) {
2500: type = rcs_keys[i].rk_id;
2501: break;
2502: }
2503: }
2504: }
2505: } else if (ch == '@') {
2506: /* we have a string */
2507: type = RCS_TOK_STRING;
2508: for (;;) {
2509: ch = getc(pdp->rp_file);
1.32 millert 2510: if (ch == EOF) {
2511: type = RCS_TOK_EOF;
2512: break;
2513: } else if (ch == '@') {
1.1 joris 2514: ch = getc(pdp->rp_file);
2515: if (ch != '@') {
2516: ungetc(ch, pdp->rp_file);
2517: break;
2518: }
2519: } else if (ch == '\n')
2520: pdp->rp_lines++;
2521:
2522: *(bp++) = ch;
2523: pdp->rp_tlen++;
2524: if (bp == pdp->rp_bufend - 1) {
2525: len = bp - pdp->rp_buf;
2526: rcs_growbuf(rfp);
2527: bp = pdp->rp_buf + len;
2528: }
2529: }
2530:
2531: *bp = '\0';
2532: } else if (isdigit(ch)) {
2533: *(bp++) = ch;
2534: last = ch;
2535: type = RCS_TOK_NUM;
2536:
2537: for (;;) {
2538: ch = getc(pdp->rp_file);
1.32 millert 2539: if (ch == EOF) {
2540: type = RCS_TOK_EOF;
2541: break;
2542: }
1.1 joris 2543: if (bp == pdp->rp_bufend)
2544: break;
1.43 sturm 2545: if (isalpha(ch) && ch != '.') {
2546: type = RCS_TOK_ID;
2547: } else if (!isdigit(ch) && ch != '.') {
1.1 joris 2548: ungetc(ch, pdp->rp_file);
2549: break;
2550: }
2551:
2552: if (last == '.' && ch == '.') {
2553: type = RCS_TOK_ERR;
2554: break;
2555: }
2556: last = ch;
2557: *(bp++) = ch;
2558: pdp->rp_tlen++;
2559: }
2560: *bp = '\0';
2561: }
2562:
2563: return (type);
2564: }
2565:
2566: /*
2567: * rcs_pushtok()
2568: *
2569: * Push a token back in the parser's token buffer.
2570: */
2571: static int
2572: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
2573: {
2574: struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
2575:
2576: if (pdp->rp_pttype != RCS_TOK_ERR)
2577: return (-1);
2578:
2579: pdp->rp_pttype = type;
1.5 ray 2580: if (strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok)) >=
2581: sizeof(pdp->rp_ptok))
2582: errx(1, "rcs_pushtok: strlcpy");
1.1 joris 2583: return (0);
2584: }
2585:
2586:
2587: /*
2588: * rcs_growbuf()
2589: *
2590: * Attempt to grow the internal parse buffer for the RCS file <rf> by
2591: * RCS_BUFEXTSIZE.
2592: * In case of failure, the original buffer is left unmodified.
2593: */
2594: static void
2595: rcs_growbuf(RCSFILE *rf)
2596: {
2597: struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
2598:
1.38 ray 2599: pdp->rp_buf = xrealloc(pdp->rp_buf, 1,
2600: pdp->rp_blen + RCS_BUFEXTSIZE);
1.1 joris 2601: pdp->rp_blen += RCS_BUFEXTSIZE;
2602: pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
2603: }
2604:
2605: /*
2606: * rcs_strprint()
2607: *
2608: * Output an RCS string <str> of size <slen> to the stream <stream>. Any
2609: * '@' characters are escaped. Otherwise, the string can contain arbitrary
2610: * binary data.
2611: */
2612: static void
2613: rcs_strprint(const u_char *str, size_t slen, FILE *stream)
2614: {
2615: const u_char *ap, *ep, *sp;
2616:
2617: if (slen == 0)
2618: return;
2619:
2620: ep = str + slen - 1;
2621:
2622: for (sp = str; sp <= ep;) {
2623: ap = memchr(sp, '@', ep - sp);
2624: if (ap == NULL)
2625: ap = ep;
2626: (void)fwrite(sp, sizeof(u_char), ap - sp + 1, stream);
2627:
2628: if (*ap == '@')
2629: putc('@', stream);
2630: sp = ap + 1;
2631: }
2632: }
2633:
2634: /*
2635: * rcs_expand_keywords()
2636: *
2637: * Return expansion any RCS keywords in <data>
2638: *
2639: * On error, return NULL.
2640: */
1.12 niallo 2641: static BUF *
2642: rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, BUF *bp, int mode)
1.1 joris 2643: {
1.28 ray 2644: BUF *newbuf;
1.1 joris 2645: int kwtype;
2646: u_int j, found;
1.28 ray 2647: u_char *c, *kwstr, *start, *end, *fin;
1.1 joris 2648: char expbuf[256], buf[256];
2649: struct tm tb;
2650: char *fmt;
1.28 ray 2651: size_t len;
1.1 joris 2652:
2653: kwtype = 0;
2654: kwstr = NULL;
2655:
2656: /*
2657: * -z support for RCS
2658: */
2659: tb = rdp->rd_date;
2660: if (timezone_flag != NULL)
2661: rcs_set_tz(timezone_flag, rdp, &tb);
2662:
1.12 niallo 2663: len = rcs_buf_len(bp);
2664:
2665: c = rcs_buf_get(bp);
2666: found = 0;
1.28 ray 2667: /* Final character in buffer. */
2668: fin = c + len - 1;
1.12 niallo 2669:
1.28 ray 2670: /* If no keywords are found, return original buffer. */
2671: newbuf = bp;
1.18 niallo 2672:
1.1 joris 2673: /*
2674: * Keyword formats:
2675: * $Keyword$
2676: * $Keyword: value$
2677: */
1.18 niallo 2678: for (; c < fin; c++) {
1.1 joris 2679: if (*c == '$') {
1.28 ray 2680: BUF *tmpbuf;
2681: size_t clen;
2682:
1.1 joris 2683: /* remember start of this possible keyword */
2684: start = c;
2685:
2686: /* first following character has to be alphanumeric */
2687: c++;
2688: if (!isalpha(*c)) {
2689: c = start;
2690: continue;
2691: }
2692:
1.28 ray 2693: /* Number of characters between c and fin, inclusive. */
2694: clen = fin - c + 1;
2695:
1.1 joris 2696: /* look for any matching keywords */
2697: found = 0;
2698: for (j = 0; j < RCS_NKWORDS; j++) {
1.28 ray 2699: size_t kwlen;
2700:
2701: kwlen = strlen(rcs_expkw[j].kw_str);
2702: /*
2703: * kwlen must be less than clen since clen
2704: * includes either a terminating `$' or a `:'.
2705: */
2706: if (kwlen < clen &&
2707: memcmp(c, rcs_expkw[j].kw_str, kwlen) == 0 &&
2708: (c[kwlen] == '$' || c[kwlen] == ':')) {
1.1 joris 2709: found = 1;
2710: kwstr = rcs_expkw[j].kw_str;
2711: kwtype = rcs_expkw[j].kw_type;
1.28 ray 2712: c += kwlen;
1.1 joris 2713: break;
2714: }
2715: }
2716:
2717: /* unknown keyword, continue looking */
2718: if (found == 0) {
2719: c = start;
2720: continue;
2721: }
2722:
2723: /*
2724: * if the next character was ':' we need to look for
2725: * an '$' before the end of the line to be sure it is
2726: * in fact a keyword.
2727: */
2728: if (*c == ':') {
1.28 ray 2729: for (; c <= fin; ++c) {
1.1 joris 2730: if (*c == '$' || *c == '\n')
2731: break;
2732: }
2733:
2734: if (*c != '$') {
2735: c = start;
2736: continue;
2737: }
2738: }
2739: end = c + 1;
2740:
2741: /* start constructing the expansion */
2742: expbuf[0] = '\0';
2743:
2744: if (mode & RCS_KWEXP_NAME) {
1.31 ray 2745: char *tmp;
2746:
2747: (void)xasprintf(&tmp, "$%s%s", kwstr,
2748: (mode & RCS_KWEXP_VAL) ? ": " : "");
2749: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(expbuf))
1.5 ray 2750: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2751: xfree(tmp);
1.1 joris 2752: }
2753:
2754: /*
2755: * order matters because of RCS_KW_ID and
2756: * RCS_KW_HEADER here
2757: */
2758: if (mode & RCS_KWEXP_VAL) {
2759: if (kwtype & RCS_KW_RCSFILE) {
1.31 ray 2760: char *tmp;
2761:
2762: (void)xasprintf(&tmp, "%s ",
2763: (kwtype & RCS_KW_FULLPATH) ? rcsfile : basename(rcsfile));
2764: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(expbuf))
1.5 ray 2765: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2766: xfree(tmp);
1.1 joris 2767: }
2768:
2769: if (kwtype & RCS_KW_REVISION) {
1.31 ray 2770: char *tmp;
2771:
1.5 ray 2772: rcsnum_tostr(rdp->rd_num, buf, sizeof(buf));
1.31 ray 2773: (void)xasprintf(&tmp, "%s ", buf);
2774: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(buf))
1.5 ray 2775: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2776: xfree(tmp);
1.1 joris 2777: }
2778:
2779: if (kwtype & RCS_KW_DATE) {
2780: if (timezone_flag != NULL)
2781: fmt = "%Y/%m/%d %H:%M:%S%z ";
2782: else
2783: fmt = "%Y/%m/%d %H:%M:%S ";
2784:
2785: strftime(buf, sizeof(buf), fmt, &tb);
1.5 ray 2786: if (strlcat(expbuf, buf, sizeof(expbuf)) >= sizeof(expbuf))
2787: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2788: }
2789:
2790: if (kwtype & RCS_KW_AUTHOR) {
1.31 ray 2791: char *tmp;
2792:
2793: (void)xasprintf(&tmp, "%s ", rdp->rd_author);
2794: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(expbuf))
1.5 ray 2795: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2796: xfree(tmp);
1.1 joris 2797: }
2798:
2799: if (kwtype & RCS_KW_STATE) {
1.31 ray 2800: char *tmp;
2801:
2802: (void)xasprintf(&tmp, "%s ", rdp->rd_state);
2803: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(expbuf))
1.5 ray 2804: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2805: xfree(tmp);
1.1 joris 2806: }
2807:
2808: /* order does not matter anymore below */
2809: if (kwtype & RCS_KW_LOG)
1.5 ray 2810: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2811: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2812:
2813: if (kwtype & RCS_KW_SOURCE) {
1.31 ray 2814: char *tmp;
2815:
2816: (void)xasprintf(&tmp, "%s ", rcsfile);
2817: if (strlcat(expbuf, tmp, sizeof(expbuf)) >= sizeof(expbuf))
1.5 ray 2818: errx(1, "rcs_expand_keywords: string truncated");
1.31 ray 2819: xfree(tmp);
1.1 joris 2820: }
2821:
2822: if (kwtype & RCS_KW_NAME)
1.5 ray 2823: if (strlcat(expbuf, " ", sizeof(expbuf)) >= sizeof(expbuf))
2824: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2825: }
2826:
2827: /* end the expansion */
2828: if (mode & RCS_KWEXP_NAME)
1.5 ray 2829: if (strlcat(expbuf, "$", sizeof(expbuf)) >= sizeof(expbuf))
2830: errx(1, "rcs_expand_keywords: string truncated");
1.1 joris 2831:
1.28 ray 2832: /* Concatenate everything together. */
2833: tmpbuf = rcs_buf_alloc(len + strlen(expbuf), BUF_AUTOEXT);
2834: /* Append everything before keyword. */
2835: rcs_buf_append(tmpbuf, rcs_buf_get(newbuf),
2836: start - (unsigned char *)rcs_buf_get(newbuf));
2837: /* Append keyword. */
2838: rcs_buf_append(tmpbuf, expbuf, strlen(expbuf));
2839: /* Point c to end of keyword. */
2840: c = rcs_buf_get(tmpbuf) + rcs_buf_len(tmpbuf) - 1;
2841: /* Append everything after keyword. */
2842: rcs_buf_append(tmpbuf, end,
2843: ((unsigned char *)rcs_buf_get(newbuf) + rcs_buf_len(newbuf)) - end);
2844: /* Point fin to end of data. */
2845: fin = rcs_buf_get(tmpbuf) + rcs_buf_len(tmpbuf) - 1;
1.29 ray 2846: /* Recalculate new length. */
2847: len = rcs_buf_len(tmpbuf);
1.28 ray 2848:
2849: /* tmpbuf is now ready, free old newbuf if allocated here. */
2850: if (newbuf != bp)
2851: rcs_buf_free(newbuf);
2852: newbuf = tmpbuf;
1.1 joris 2853: }
2854: }
1.18 niallo 2855:
1.28 ray 2856: return (newbuf);
1.1 joris 2857: }
2858:
2859: /*
2860: * rcs_deltatext_set()
2861: *
2862: * Set deltatext for <rev> in RCS file <rfp> to <dtext>
2863: * Returns -1 on error, 0 on success.
2864: */
2865: int
1.12 niallo 2866: rcs_deltatext_set(RCSFILE *rfp, RCSNUM *rev, BUF *bp)
1.1 joris 2867: {
2868: size_t len;
1.12 niallo 2869: u_char *dtext;
1.1 joris 2870: struct rcs_delta *rdp;
2871:
2872: /* Write operations require full parsing */
2873: rcs_parse_deltatexts(rfp, NULL);
2874:
2875: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2876: return (-1);
2877:
2878: if (rdp->rd_text != NULL)
2879: xfree(rdp->rd_text);
2880:
1.12 niallo 2881: len = rcs_buf_len(bp);
2882: dtext = rcs_buf_release(bp);
2883: bp = NULL;
1.1 joris 2884: if (len != 0) {
1.12 niallo 2885: rdp->rd_text = xmalloc(len);
1.1 joris 2886: rdp->rd_tlen = len;
1.12 niallo 2887: (void)memcpy(rdp->rd_text, dtext, len);
2888: xfree(dtext);
1.1 joris 2889: } else {
2890: rdp->rd_text = NULL;
2891: rdp->rd_tlen = 0;
2892: }
2893:
2894: return (0);
2895: }
2896:
2897: /*
2898: * rcs_rev_setlog()
2899: *
2900: * Sets the log message of revision <rev> to <logtext>
2901: */
2902: int
2903: rcs_rev_setlog(RCSFILE *rfp, RCSNUM *rev, const char *logtext)
2904: {
2905: struct rcs_delta *rdp;
1.40 xsa 2906: char buf[RCS_REV_BUFSZ];
1.1 joris 2907:
2908: rcsnum_tostr(rev, buf, sizeof(buf));
2909:
2910: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2911: return (-1);
2912:
2913: if (rdp->rd_log != NULL)
2914: xfree(rdp->rd_log);
2915:
2916: rdp->rd_log = xstrdup(logtext);
2917: rfp->rf_flags &= ~RCS_SYNCED;
2918: return (0);
2919: }
2920: /*
2921: * rcs_rev_getdate()
2922: *
2923: * Get the date corresponding to a given revision.
2924: * Returns the date on success, -1 on failure.
2925: */
2926: time_t
2927: rcs_rev_getdate(RCSFILE *rfp, RCSNUM *rev)
2928: {
2929: struct rcs_delta *rdp;
2930:
2931: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2932: return (-1);
2933:
2934: return (mktime(&rdp->rd_date));
2935: }
2936:
2937: /*
2938: * rcs_state_set()
2939: *
2940: * Sets the state of revision <rev> to <state>
2941: * NOTE: default state is 'Exp'. States may not contain spaces.
2942: *
2943: * Returns -1 on failure, 0 on success.
2944: */
2945: int
2946: rcs_state_set(RCSFILE *rfp, RCSNUM *rev, const char *state)
2947: {
2948: struct rcs_delta *rdp;
2949:
2950: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2951: return (-1);
2952:
2953: if (rdp->rd_state != NULL)
2954: xfree(rdp->rd_state);
2955:
2956: rdp->rd_state = xstrdup(state);
2957:
2958: rfp->rf_flags &= ~RCS_SYNCED;
2959:
2960: return (0);
2961: }
2962:
2963: /*
2964: * rcs_state_check()
2965: *
2966: * Check if string <state> is valid.
2967: *
2968: * Returns 0 if the string is valid, -1 otherwise.
2969: */
2970: int
2971: rcs_state_check(const char *state)
2972: {
2973: if (strchr(state, ' ') != NULL)
2974: return (-1);
2975:
2976: return (0);
2977: }
2978:
2979: /*
2980: * rcs_state_get()
2981: *
2982: * Get the state for a given revision of a specified RCSFILE.
2983: *
2984: * Returns NULL on failure.
2985: */
2986: const char *
2987: rcs_state_get(RCSFILE *rfp, RCSNUM *rev)
2988: {
2989: struct rcs_delta *rdp;
2990:
2991: if ((rdp = rcs_findrev(rfp, rev)) == NULL)
2992: return (NULL);
2993:
2994: return (rdp->rd_state);
2995: }
2996:
2997: /*
2998: * rcs_kwexp_buf()
2999: *
3000: * Do keyword expansion on a buffer if necessary
3001: *
3002: */
3003: BUF *
3004: rcs_kwexp_buf(BUF *bp, RCSFILE *rf, RCSNUM *rev)
3005: {
3006: struct rcs_delta *rdp;
3007: int expmode;
3008:
3009: /*
3010: * Do keyword expansion if required.
3011: */
3012: if (rf->rf_expand != NULL)
3013: expmode = rcs_kwexp_get(rf);
3014: else
3015: expmode = RCS_KWEXP_DEFAULT;
3016:
3017: if (!(expmode & RCS_KWEXP_NONE)) {
3018: if ((rdp = rcs_findrev(rf, rev)) == NULL)
1.5 ray 3019: errx(1, "could not fetch revision");
1.12 niallo 3020: return (rcs_expand_keywords(rf->rf_path, rdp, bp, expmode));
1.1 joris 3021: }
3022: return (bp);
3023: }