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

Annotation of src/usr.bin/cvs/rcs.c, Revision 1.227

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