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

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