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