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