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

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