[BACK]Return to rcs.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/rcs.c, Revision 1.50

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