[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.232

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