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

1.28    ! jfb         1: /*     $OpenBSD: rcs.c,v 1.27 2005/03/02 04:19:34 jfb 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:
                     27: #include <sys/param.h>
                     28: #include <sys/queue.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <errno.h>
                     32: #include <stdio.h>
                     33: #include <ctype.h>
                     34: #include <stdlib.h>
1.26      jfb        35: #include <stdarg.h>
1.1       jfb        36: #include <string.h>
                     37:
                     38: #include "rcs.h"
                     39: #include "log.h"
                     40:
1.26      jfb        41: #define RCS_BUFSIZE     16384
1.18      jfb        42: #define RCS_BUFEXTSIZE   8192
1.1       jfb        43:
                     44:
                     45: /* RCS token types */
                     46: #define RCS_TOK_ERR     -1
                     47: #define RCS_TOK_EOF      0
                     48: #define RCS_TOK_NUM      1
                     49: #define RCS_TOK_ID       2
                     50: #define RCS_TOK_STRING   3
                     51: #define RCS_TOK_SCOLON   4
                     52: #define RCS_TOK_COLON    5
                     53:
                     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
                     71:
                     72: #define RCS_ISKEY(t)    (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))
                     73:
                     74:
                     75: #define RCS_NOSCOL   0x01   /* no terminating semi-colon */
                     76: #define RCS_VOPT     0x02   /* value is optional */
                     77:
                     78:
                     79: /* opaque parse data */
                     80: struct rcs_pdata {
1.18      jfb        81:        u_int  rp_lines;
1.1       jfb        82:
                     83:        char  *rp_buf;
                     84:        size_t rp_blen;
1.18      jfb        85:        char  *rp_bufend;
1.1       jfb        86:
                     87:        /* pushback token buffer */
                     88:        char   rp_ptok[128];
                     89:        int    rp_pttype;       /* token type, RCS_TOK_ERR if no token */
                     90:
                     91:        FILE  *rp_file;
                     92: };
                     93:
                     94:
                     95: struct rcs_line {
                     96:        char *rl_line;
                     97:        int   rl_lineno;
                     98:        TAILQ_ENTRY(rcs_line) rl_list;
                     99: };
1.5       vincent   100: TAILQ_HEAD(rcs_tqh, rcs_line);
1.1       jfb       101:
                    102: struct rcs_foo {
                    103:        int       rl_nblines;
                    104:        char     *rl_data;
1.5       vincent   105:        struct rcs_tqh rl_lines;
1.1       jfb       106: };
                    107:
                    108: #define RCS_TOKSTR(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
                    109: #define RCS_TOKLEN(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen
                    110:
                    111:
1.20      jfb       112: static struct rcs_kfl {
                    113:        char  rk_char;
                    114:        int   rk_val;
                    115: } rcs_kflags[] = {
                    116:        { 'k',   RCS_KWEXP_NAME },
                    117:        { 'v',   RCS_KWEXP_VAL  },
                    118:        { 'l',   RCS_KWEXP_LKR  },
                    119:        { 'o',   RCS_KWEXP_OLD  },
                    120:        { 'b',   RCS_KWEXP_NONE },
                    121: };
                    122:
1.1       jfb       123: static struct rcs_key {
                    124:        char  rk_str[16];
                    125:        int   rk_id;
                    126:        int   rk_val;
                    127:        int   rk_flags;
                    128: } rcs_keys[] = {
                    129:        { "access",   RCS_TOK_ACCESS,   RCS_TOK_ID,     RCS_VOPT     },
                    130:        { "author",   RCS_TOK_AUTHOR,   RCS_TOK_STRING, 0            },
                    131:        { "branch",   RCS_TOK_BRANCH,   RCS_TOK_NUM,    RCS_VOPT     },
                    132:        { "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM,    RCS_VOPT     },
                    133:        { "comment",  RCS_TOK_COMMENT,  RCS_TOK_STRING, RCS_VOPT     },
                    134:        { "date",     RCS_TOK_DATE,     RCS_TOK_NUM,    0            },
                    135:        { "desc",     RCS_TOK_DESC,     RCS_TOK_STRING, RCS_NOSCOL   },
                    136:        { "expand",   RCS_TOK_EXPAND,   RCS_TOK_STRING, RCS_VOPT     },
                    137:        { "head",     RCS_TOK_HEAD,     RCS_TOK_NUM,    RCS_VOPT     },
                    138:        { "locks",    RCS_TOK_LOCKS,    RCS_TOK_ID,     0            },
                    139:        { "log",      RCS_TOK_LOG,      RCS_TOK_STRING, RCS_NOSCOL   },
                    140:        { "next",     RCS_TOK_NEXT,     RCS_TOK_NUM,    RCS_VOPT     },
                    141:        { "state",    RCS_TOK_STATE,    RCS_TOK_STRING, RCS_VOPT     },
                    142:        { "strict",   RCS_TOK_STRICT,   0,              0,           },
                    143:        { "symbols",  RCS_TOK_SYMBOLS,  0,              0            },
                    144:        { "text",     RCS_TOK_TEXT,     RCS_TOK_STRING, RCS_NOSCOL   },
                    145: };
                    146:
1.18      jfb       147: #define RCS_NKEYS   (sizeof(rcs_keys)/sizeof(rcs_keys[0]))
1.1       jfb       148:
                    149:
1.27      jfb       150: static int   rcs_write           (RCSFILE *);
1.26      jfb       151: static int   rcs_parse           (RCSFILE *);
                    152: static int   rcs_parse_admin     (RCSFILE *);
                    153: static int   rcs_parse_delta     (RCSFILE *);
                    154: static int   rcs_parse_deltatext (RCSFILE *);
                    155:
                    156: static int   rcs_parse_access    (RCSFILE *);
                    157: static int   rcs_parse_symbols   (RCSFILE *);
                    158: static int   rcs_parse_locks     (RCSFILE *);
                    159: static int   rcs_parse_branches  (RCSFILE *, struct rcs_delta *);
                    160: static void  rcs_freedelta       (struct rcs_delta *);
                    161: static void  rcs_freepdata       (struct rcs_pdata *);
                    162: static int   rcs_gettok          (RCSFILE *);
                    163: static int   rcs_pushtok         (RCSFILE *, const char *, int);
                    164: static int   rcs_growbuf         (RCSFILE *);
                    165: static int   rcs_patch_lines     (struct rcs_foo *, struct rcs_foo *);
                    166:
                    167: static struct rcs_delta*  rcs_findrev    (RCSFILE *, RCSNUM *);
                    168: static struct rcs_foo*    rcs_splitlines (const char *);
                    169: static void               rcs_freefoo    (struct rcs_foo *);
                    170:
                    171:
1.1       jfb       172: /*
                    173:  * rcs_open()
                    174:  *
                    175:  * Open a file containing RCS-formatted information.  The file's path is
1.26      jfb       176:  * given in <path>, and the opening flags are given in <flags>, which is either
                    177:  * RCS_READ, RCS_WRITE, or RCS_RDWR.  If the open requests write access and
                    178:  * the file does not exist, the RCS_CREATE flag must also be given, in which
                    179:  * case it will be created with the mode specified in a third argument of
                    180:  * type mode_t.  If the file exists and RCS_CREATE is passed, the open will
                    181:  * fail.
1.1       jfb       182:  * Returns a handle to the opened file on success, or NULL on failure.
                    183:  */
                    184: RCSFILE*
1.26      jfb       185: rcs_open(const char *path, int flags, ...)
1.1       jfb       186: {
1.26      jfb       187:        int ret;
                    188:        mode_t fmode;
1.1       jfb       189:        RCSFILE *rfp;
                    190:        struct stat st;
1.26      jfb       191:        va_list vap;
                    192:
                    193:        fmode = 0;
                    194:        flags &= 0xffff;        /* ditch any internal flags */
1.1       jfb       195:
1.26      jfb       196:        if (((ret = stat(path, &st)) == -1) && (errno == ENOENT)) {
                    197:                if (flags & RCS_CREATE) {
                    198:                        va_start(vap, flags);
                    199:                        fmode = va_arg(vap, mode_t);
                    200:                        va_end(vap);
                    201:                } else {
                    202:                        cvs_log(LP_ERR, "RCS file `%s' does not exist", path);
                    203:                        return (NULL);
                    204:                }
                    205:        } else if ((ret == 0) && (flags & RCS_CREATE)) {
                    206:                cvs_log(LP_ERR, "RCS file `%s' exists", path);
1.1       jfb       207:                return (NULL);
                    208:        }
                    209:
1.26      jfb       210:        if ((rfp = (RCSFILE *)malloc(sizeof(*rfp))) == NULL) {
1.1       jfb       211:                cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
                    212:                return (NULL);
                    213:        }
                    214:        memset(rfp, 0, sizeof(*rfp));
                    215:
1.26      jfb       216:        if ((rfp->rf_branch = rcsnum_alloc()) == NULL) {
                    217:                free(rfp);
1.11      joris     218:                return (NULL);
                    219:        }
                    220:
1.26      jfb       221:        if ((rfp->rf_path = strdup(path)) == NULL) {
1.1       jfb       222:                cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
1.26      jfb       223:                rcsnum_free(rfp->rf_branch);
                    224:                free(rfp);
1.1       jfb       225:                return (NULL);
                    226:        }
                    227:
                    228:        rfp->rf_ref = 1;
1.26      jfb       229:        rfp->rf_flags = flags | RCS_SLOCK;
                    230:        rfp->rf_mode = fmode;
1.1       jfb       231:
                    232:        TAILQ_INIT(&(rfp->rf_delta));
                    233:        TAILQ_INIT(&(rfp->rf_symbols));
                    234:        TAILQ_INIT(&(rfp->rf_locks));
                    235:
1.26      jfb       236:        if (rfp->rf_flags & RCS_CREATE) {
                    237:        } else if (rcs_parse(rfp) < 0) {
1.1       jfb       238:                rcs_close(rfp);
                    239:                return (NULL);
                    240:        }
                    241:
                    242:        return (rfp);
                    243: }
                    244:
                    245:
                    246: /*
                    247:  * rcs_close()
                    248:  *
                    249:  * Close an RCS file handle.
                    250:  */
                    251: void
                    252: rcs_close(RCSFILE *rfp)
                    253: {
                    254:        struct rcs_delta *rdp;
1.13      jfb       255:        struct rcs_lock *rlp;
                    256:        struct rcs_sym *rsp;
1.1       jfb       257:
                    258:        if (rfp->rf_ref > 1) {
                    259:                rfp->rf_ref--;
                    260:                return;
                    261:        }
                    262:
1.26      jfb       263:        if ((rfp->rf_flags & RCS_WRITE) && !(rfp->rf_flags & RCS_SYNCED))
                    264:                rcs_write(rfp);
                    265:
1.1       jfb       266:        while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
                    267:                rdp = TAILQ_FIRST(&(rfp->rf_delta));
                    268:                TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
                    269:                rcs_freedelta(rdp);
                    270:        }
                    271:
1.13      jfb       272:        while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
                    273:                rsp = TAILQ_FIRST(&(rfp->rf_symbols));
                    274:                TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
                    275:                rcsnum_free(rsp->rs_num);
                    276:                free(rsp->rs_name);
                    277:                free(rsp);
                    278:        }
                    279:
                    280:        while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
                    281:                rlp = TAILQ_FIRST(&(rfp->rf_locks));
                    282:                TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
                    283:                rcsnum_free(rlp->rl_num);
                    284:                free(rlp);
                    285:        }
                    286:
1.1       jfb       287:        if (rfp->rf_head != NULL)
                    288:                rcsnum_free(rfp->rf_head);
1.11      joris     289:        if (rfp->rf_branch != NULL)
                    290:                rcsnum_free(rfp->rf_branch);
1.1       jfb       291:
                    292:        if (rfp->rf_path != NULL)
                    293:                free(rfp->rf_path);
                    294:        if (rfp->rf_comment != NULL)
                    295:                free(rfp->rf_comment);
                    296:        if (rfp->rf_expand != NULL)
                    297:                free(rfp->rf_expand);
                    298:        if (rfp->rf_desc != NULL)
                    299:                free(rfp->rf_desc);
                    300:        free(rfp);
                    301: }
                    302:
                    303:
                    304: /*
                    305:  * rcs_write()
                    306:  *
                    307:  * Write the contents of the RCS file handle <rfp> to disk in the file whose
                    308:  * path is in <rf_path>.
                    309:  * Returns 0 on success, or -1 on failure.
                    310:  */
1.27      jfb       311: static int
1.1       jfb       312: rcs_write(RCSFILE *rfp)
                    313: {
                    314:        FILE *fp;
1.7       jfb       315:        char buf[1024], numbuf[64], *cp;
                    316:        size_t rlen, len;
1.1       jfb       317:        struct rcs_sym *symp;
                    318:        struct rcs_delta *rdp;
                    319:
1.28    ! jfb       320:        if (rfp->rf_flags & RCS_SYNCED)
1.1       jfb       321:                return (0);
                    322:
                    323:        fp = fopen(rfp->rf_path, "w");
                    324:        if (fp == NULL) {
                    325:                cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
                    326:                    rfp->rf_path);
                    327:                return (-1);
                    328:        }
                    329:
1.28    ! jfb       330:        if (rfp->rf_head != NULL)
        !           331:                rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
        !           332:        else
        !           333:                numbuf[0] = '\0';
        !           334:
1.1       jfb       335:        fprintf(fp, "head\t%s;\n", numbuf);
                    336:        fprintf(fp, "access;\n");
                    337:
                    338:        fprintf(fp, "symbols\n");
                    339:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    340:                rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
                    341:                snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
                    342:                fprintf(fp, "\t%s", buf);
                    343:                if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
                    344:                        fputc('\n', fp);
                    345:        }
                    346:        fprintf(fp, ";\n");
                    347:
                    348:        fprintf(fp, "locks;");
                    349:
1.26      jfb       350:        if (rfp->rf_flags & RCS_SLOCK)
1.1       jfb       351:                fprintf(fp, " strict;");
                    352:        fputc('\n', fp);
                    353:
                    354:        if (rfp->rf_comment != NULL)
                    355:                fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);
                    356:
                    357:        if (rfp->rf_expand != NULL)
                    358:                fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);
                    359:
                    360:        fprintf(fp, "\n\n");
                    361:
                    362:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    363:                fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    364:                    sizeof(numbuf)));
                    365:                fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
                    366:                    rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
                    367:                    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    368:                    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
                    369:                fprintf(fp, "\tauthor %s;\tstate %s;\n",
                    370:                    rdp->rd_author, rdp->rd_state);
                    371:                fprintf(fp, "branches;\n");
                    372:                fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
                    373:                    numbuf, sizeof(numbuf)));
                    374:        }
                    375:
1.26      jfb       376:        fprintf(fp, "\ndesc\n@%s@\n\n",
                    377:            (rfp->rf_desc == NULL) ? "" : rfp->rf_desc);
1.1       jfb       378:
                    379:        /* deltatexts */
                    380:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                    381:                fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
                    382:                    sizeof(numbuf)));
1.7       jfb       383:                fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);
                    384:
                    385:                cp = rdp->rd_text;
                    386:                do {
                    387:                        len = sizeof(buf);
                    388:                        rlen = rcs_stresc(1, cp, buf, &len);
                    389:                        fprintf(fp, "%s", buf);
                    390:                        cp += rlen;
                    391:                } while (len != 0);
                    392:                fprintf(fp, "@\n\n");
1.1       jfb       393:        }
                    394:        fclose(fp);
                    395:
1.26      jfb       396:        rfp->rf_flags |= RCS_SYNCED;
1.1       jfb       397:
                    398:        return (0);
                    399: }
                    400:
                    401:
                    402: /*
1.26      jfb       403:  * rcs_sym_add()
1.1       jfb       404:  *
                    405:  * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
                    406:  * is named <sym> and is bound to the RCS revision <snum>.
                    407:  * Returns 0 on success, or -1 on failure.
                    408:  */
                    409: int
1.26      jfb       410: rcs_sym_add(RCSFILE *rfp, const char *sym, RCSNUM *snum)
1.1       jfb       411: {
                    412:        struct rcs_sym *symp;
                    413:
                    414:        /* first look for duplication */
                    415:        TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
                    416:                if (strcmp(symp->rs_name, sym) == 0) {
1.17      jfb       417:                        cvs_log(LP_ERR, "attempt to add duplicate symbol `%s'",
                    418:                            sym);
1.1       jfb       419:                        return (-1);
                    420:                }
                    421:        }
                    422:
                    423:        symp = (struct rcs_sym *)malloc(sizeof(*symp));
                    424:        if (symp == NULL) {
                    425:                cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                    426:                return (-1);
                    427:        }
                    428:
                    429:        symp->rs_name = strdup(sym);
1.10      joris     430:        if (symp->rs_name == NULL) {
                    431:                cvs_log(LP_ERRNO, "failed to duplicate symbol");
                    432:                free(symp);
                    433:                return (-1);
                    434:        }
                    435:
1.1       jfb       436:        symp->rs_num = rcsnum_alloc();
1.11      joris     437:        if (symp->rs_num == NULL) {
1.17      jfb       438:                free(symp->rs_name);
1.11      joris     439:                free(symp);
                    440:                return (-1);
                    441:        }
1.1       jfb       442:        rcsnum_cpy(snum, symp->rs_num, 0);
                    443:
                    444:        TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                    445:
                    446:        /* not synced anymore */
1.26      jfb       447:        rfp->rf_flags &= ~RCS_SYNCED;
1.1       jfb       448:
                    449:        return (0);
                    450: }
                    451:
                    452:
                    453: /*
1.27      jfb       454:  * rcs_sym_remove()
                    455:  *
                    456:  * Remove the symbol with name <sym> from the symbol list for the RCS file
                    457:  * <file>.  If no such symbol is found, the call fails and returns with an
                    458:  * error.
                    459:  * Returns 0 on success, or -1 on failure.
                    460:  */
                    461: int
                    462: rcs_sym_remove(RCSFILE *file, const char *sym)
                    463: {
                    464:        struct rcs_sym *symp;
                    465:
                    466:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    467:                if (strcmp(symp->rs_name, sym) == 0)
                    468:                        break;
                    469:
                    470:        if (symp == NULL) {
                    471:                cvs_log(LP_ERR, "%s: no such symbol `%s'", file->rf_path, sym);
                    472:                return (-1);
                    473:        }
                    474:
                    475:        TAILQ_REMOVE(&(file->rf_symbols), symp, rs_list);
                    476:        free(symp->rs_name);
                    477:        rcsnum_free(symp->rs_num);
                    478:        free(symp);
                    479:
                    480:        /* not synced anymore */
                    481:        file->rf_flags &= ~RCS_SYNCED;
                    482:
                    483:        return (0);
                    484: }
                    485:
                    486:
                    487: /*
                    488:  * rcs_sym_getrev()
                    489:  *
                    490:  * Retrieve the RCS revision number associated with the symbol <sym> for the
                    491:  * RCS file <file>.  The returned value is a dynamically-allocated copy and
                    492:  * should be freed by the caller once they are done with it.
                    493:  * Returns the RCSNUM on success, or NULL on failure.
                    494:  */
                    495: RCSNUM*
                    496: rcs_sym_getrev(RCSFILE *file, const char *sym)
                    497: {
                    498:        RCSNUM *num;
                    499:        struct rcs_sym *symp;
                    500:
                    501:        num = NULL;
                    502:
                    503:        TAILQ_FOREACH(symp, &(file->rf_symbols), rs_list)
                    504:                if (strcmp(symp->rs_name, sym) == 0)
                    505:                        break;
                    506:
                    507:        if (symp == NULL) {
                    508:                /* XXX set error */
                    509:        } else if (((num = rcsnum_alloc()) != NULL) &&
                    510:            (rcsnum_cpy(symp->rs_num, num, 0) < 0)) {
                    511:                rcsnum_free(num);
                    512:                num = NULL;
                    513:        }
                    514:
                    515:        return (num);
                    516: }
                    517:
                    518:
                    519: /*
                    520:  * rcs_desc_get()
                    521:  *
                    522:  * Retrieve the description for the RCS file <file>.
                    523:  */
                    524: const char*
                    525: rcs_desc_get(RCSFILE *file)
                    526: {
                    527:        return (file->rf_desc);
                    528: }
                    529:
                    530:
                    531: /*
                    532:  * rcs_desc_set()
                    533:  *
                    534:  * Set the description for the RCS file <file>.
                    535:  * Returns 0 on success, or -1 on failure.
                    536:  */
                    537: int
                    538: rcs_desc_set(RCSFILE *file, const char *desc)
                    539: {
                    540:        char *tmp;
                    541:
                    542:        if ((tmp = strdup(desc)) == NULL)
                    543:                return (-1);
                    544:
                    545:        if (file->rf_desc != NULL)
                    546:                free(file->rf_desc);
                    547:        file->rf_desc = tmp;
                    548:        file->rf_flags &= ~RCS_SYNCED;
                    549:
                    550:        return (0);
                    551: }
                    552:
                    553:
                    554: /*
1.1       jfb       555:  * rcs_patch()
                    556:  *
                    557:  * Apply an RCS-format patch pointed to by <patch> to the file contents
                    558:  * found in <data>.
                    559:  * Returns 0 on success, or -1 on failure.
                    560:  */
                    561: BUF*
                    562: rcs_patch(const char *data, const char *patch)
                    563: {
1.5       vincent   564:        struct rcs_foo *dlines, *plines;
                    565:        struct rcs_line *lp;
1.1       jfb       566:        size_t len;
1.5       vincent   567:        int lineno;
1.1       jfb       568:        BUF *res;
                    569:
                    570:        len = strlen(data);
                    571:        res = cvs_buf_alloc(len, BUF_AUTOEXT);
                    572:        if (res == NULL)
                    573:                return (NULL);
                    574:
                    575:        dlines = rcs_splitlines(data);
1.17      jfb       576:        if (dlines == NULL) {
                    577:                cvs_buf_free(res);
1.1       jfb       578:                return (NULL);
1.17      jfb       579:        }
1.5       vincent   580:
1.1       jfb       581:        plines = rcs_splitlines(patch);
1.5       vincent   582:        if (plines == NULL) {
1.17      jfb       583:                cvs_buf_free(res);
1.5       vincent   584:                rcs_freefoo(dlines);
1.1       jfb       585:                return (NULL);
1.5       vincent   586:        }
                    587:
                    588:        if (rcs_patch_lines(dlines, plines) < 0) {
1.17      jfb       589:                cvs_buf_free(res);
1.5       vincent   590:                rcs_freefoo(plines);
                    591:                rcs_freefoo(dlines);
                    592:                return (NULL);
                    593:        }
                    594:
                    595:        lineno = 0;
                    596:        TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
                    597:                if (lineno != 0)
                    598:                        cvs_buf_fappend(res, "%s\n", lp->rl_line);
                    599:                lineno++;
                    600:        }
                    601:
                    602:        rcs_freefoo(dlines);
                    603:        rcs_freefoo(plines);
                    604:        return (res);
                    605: }
                    606:
1.7       jfb       607: static int
1.5       vincent   608: rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
                    609: {
                    610:        char op, *ep;
                    611:        struct rcs_line *lp, *dlp, *ndlp;
                    612:        int i, lineno, nbln;
1.1       jfb       613:
                    614:        dlp = TAILQ_FIRST(&(dlines->rl_lines));
                    615:        lp = TAILQ_FIRST(&(plines->rl_lines));
                    616:
                    617:        /* skip first bogus line */
                    618:        for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
                    619:            lp = TAILQ_NEXT(lp, rl_list)) {
                    620:                op = *(lp->rl_line);
                    621:                lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
                    622:                if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
                    623:                    (*ep != ' ')) {
                    624:                        cvs_log(LP_ERR,
                    625:                            "invalid line specification in RCS patch");
                    626:                        return (NULL);
                    627:                }
                    628:                ep++;
                    629:                nbln = (int)strtol(ep, &ep, 10);
                    630:                if ((nbln <= 0) || (*ep != '\0')) {
                    631:                        cvs_log(LP_ERR,
                    632:                            "invalid line number specification in RCS patch");
                    633:                        return (NULL);
                    634:                }
                    635:
                    636:                /* find the appropriate line */
                    637:                for (;;) {
                    638:                        if (dlp == NULL)
                    639:                                break;
                    640:                        if (dlp->rl_lineno == lineno)
                    641:                                break;
                    642:                        if (dlp->rl_lineno > lineno) {
                    643:                                dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
1.14      deraadt   644:                        } else if (dlp->rl_lineno < lineno) {
1.1       jfb       645:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    646:                                if (ndlp->rl_lineno > lineno)
                    647:                                        break;
                    648:                                dlp = ndlp;
                    649:                        }
                    650:                }
                    651:                if (dlp == NULL) {
                    652:                        cvs_log(LP_ERR,
                    653:                            "can't find referenced line in RCS patch");
                    654:                        return (NULL);
                    655:                }
                    656:
                    657:                if (op == 'd') {
                    658:                        for (i = 0; (i < nbln) && (dlp != NULL); i++) {
                    659:                                ndlp = TAILQ_NEXT(dlp, rl_list);
                    660:                                TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
                    661:                                dlp = ndlp;
                    662:                        }
1.14      deraadt   663:                } else if (op == 'a') {
1.1       jfb       664:                        for (i = 0; i < nbln; i++) {
                    665:                                ndlp = lp;
                    666:                                lp = TAILQ_NEXT(lp, rl_list);
                    667:                                if (lp == NULL) {
                    668:                                        cvs_log(LP_ERR, "truncated RCS patch");
1.5       vincent   669:                                        return (-1);
1.1       jfb       670:                                }
                    671:                                TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
                    672:                                TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
                    673:                                    lp, rl_list);
                    674:                                dlp = lp;
                    675:
                    676:                                /* we don't want lookup to block on those */
                    677:                                lp->rl_lineno = lineno;
                    678:
                    679:                                lp = ndlp;
                    680:                        }
1.14      deraadt   681:                } else {
1.1       jfb       682:                        cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
1.5       vincent   683:                        return (-1);
1.1       jfb       684:                }
                    685:
                    686:                /* last line of the patch, done */
                    687:                if (lp->rl_lineno == plines->rl_nblines)
                    688:                        break;
                    689:        }
                    690:
                    691:        /* once we're done patching, rebuild the line numbers */
1.2       vincent   692:        lineno = 0;
1.5       vincent   693:        TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
1.1       jfb       694:                lp->rl_lineno = lineno++;
                    695:        dlines->rl_nblines = lineno - 1;
                    696:
1.5       vincent   697:        return (0);
1.1       jfb       698: }
                    699:
                    700:
                    701: /*
                    702:  * rcs_getrev()
                    703:  *
                    704:  * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
1.4       vincent   705:  * returned buffer is dynamically allocated and should be released using
                    706:  * cvs_buf_free() once the caller is done using it.
1.1       jfb       707:  */
                    708: BUF*
                    709: rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
                    710: {
                    711:        int res;
                    712:        size_t len;
                    713:        void *bp;
                    714:        RCSNUM *crev;
                    715:        BUF *rbuf;
                    716:        struct rcs_delta *rdp = NULL;
                    717:
1.28    ! jfb       718:        if (rfp->rf_head == NULL)
        !           719:                return (NULL);
        !           720:
1.1       jfb       721:        res = rcsnum_cmp(rfp->rf_head, rev, 0);
                    722:        if (res == 1) {
                    723:                cvs_log(LP_ERR, "sorry, can't travel in the future yet");
                    724:                return (NULL);
1.26      jfb       725:        }
                    726:
                    727:        rdp = rcs_findrev(rfp, rfp->rf_head);
                    728:        if (rdp == NULL) {
                    729:                cvs_log(LP_ERR, "failed to get RCS HEAD revision");
                    730:                return (NULL);
                    731:        }
                    732:
                    733:        len = strlen(rdp->rd_text);
                    734:        if ((rbuf = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL)
                    735:                return (NULL);
                    736:
                    737:        cvs_buf_append(rbuf, rdp->rd_text, len);
                    738:
                    739:        if (res != 0) {
                    740:                /* Apply patches backwards to get the right version.
                    741:                 * This will need some rework to support sub branches.
                    742:                 */
                    743:                if ((crev = rcsnum_alloc()) == NULL) {
                    744:                        cvs_buf_free(rbuf);
1.1       jfb       745:                        return (NULL);
                    746:                }
1.26      jfb       747:                rcsnum_cpy(rfp->rf_head, crev, 0);
                    748:                do {
                    749:                        crev->rn_id[crev->rn_len - 1]--;
                    750:                        rdp = rcs_findrev(rfp, crev);
                    751:                        if (rdp == NULL) {
                    752:                                rcsnum_free(crev);
                    753:                                cvs_buf_free(rbuf);
                    754:                                return (NULL);
                    755:                        }
1.1       jfb       756:
1.26      jfb       757:                        if (cvs_buf_putc(rbuf, '\0') < 0) {
                    758:                                rcsnum_free(crev);
1.17      jfb       759:                                cvs_buf_free(rbuf);
1.11      joris     760:                                return (NULL);
1.17      jfb       761:                        }
1.26      jfb       762:                        bp = cvs_buf_release(rbuf);
                    763:                        rbuf = rcs_patch((char *)bp, rdp->rd_text);
                    764:                        if (rbuf == NULL)
                    765:                                break;
                    766:                } while (rcsnum_cmp(crev, rev, 0) != 0);
1.1       jfb       767:
1.26      jfb       768:                rcsnum_free(crev);
1.1       jfb       769:        }
                    770:
                    771:        return (rbuf);
1.16      jfb       772: }
                    773:
                    774:
                    775: /*
                    776:  * rcs_gethead()
                    777:  *
                    778:  * Get the head revision for the RCS file <rf>.
                    779:  */
                    780: BUF*
                    781: rcs_gethead(RCSFILE *rf)
                    782: {
                    783:        return rcs_getrev(rf, rf->rf_head);
1.1       jfb       784: }
                    785:
                    786:
                    787: /*
                    788:  * rcs_getrevbydate()
                    789:  *
                    790:  * Get an RCS revision by a specific date.
                    791:  */
                    792: RCSNUM*
                    793: rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
                    794: {
                    795:        return (NULL);
                    796: }
                    797:
                    798:
                    799: /*
                    800:  * rcs_findrev()
                    801:  *
                    802:  * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
                    803:  * The revision number is given in <rev>.
                    804:  * Returns a pointer to the delta on success, or NULL on failure.
                    805:  */
                    806: static struct rcs_delta*
                    807: rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
                    808: {
                    809:        u_int cmplen;
                    810:        struct rcs_delta *rdp;
                    811:        struct rcs_dlist *hp;
1.6       vincent   812:        int found;
1.26      jfb       813:
1.1       jfb       814:        cmplen = 2;
                    815:        hp = &(rfp->rf_delta);
                    816:
1.6       vincent   817:        do {
                    818:                found = 0;
                    819:                TAILQ_FOREACH(rdp, hp, rd_list) {
                    820:                        if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
                    821:                                if (cmplen == rev->rn_len)
                    822:                                        return (rdp);
1.1       jfb       823:
1.6       vincent   824:                                hp = &(rdp->rd_snodes);
                    825:                                cmplen += 2;
                    826:                                found = 1;
                    827:                                break;
                    828:                        }
1.1       jfb       829:                }
1.6       vincent   830:        } while (found && cmplen < rev->rn_len);
1.1       jfb       831:
                    832:        return (NULL);
1.20      jfb       833: }
                    834:
                    835:
                    836: /*
1.26      jfb       837:  * rcs_kwexp_set()
                    838:  *
                    839:  * Set the keyword expansion mode to use on the RCS file <file> to <mode>.
                    840:  * Returns 0 on success, or -1 on failure.
                    841:  */
                    842: int
                    843: rcs_kwexp_set(RCSFILE *file, int mode)
                    844: {
                    845:        int i;
                    846:        char *tmp, buf[8] = "";
                    847:
                    848:        if (RCS_KWEXP_INVAL(mode))
                    849:                return (-1);
                    850:
                    851:        i = 0;
                    852:        if (mode == RCS_KWEXP_NONE)
                    853:                buf[0] = 'b';
                    854:        else if (mode == RCS_KWEXP_OLD)
                    855:                buf[0] = 'o';
                    856:        else {
                    857:                if (mode & RCS_KWEXP_NAME)
                    858:                        buf[i++] = 'k';
                    859:                if (mode & RCS_KWEXP_VAL)
                    860:                        buf[i++] = 'v';
                    861:                if (mode & RCS_KWEXP_LKR)
                    862:                        buf[i++] = 'l';
                    863:        }
                    864:
                    865:        if ((tmp = strdup(buf)) == NULL) {
                    866:                cvs_log(LP_ERRNO, "%s: failed to copy expansion mode",
                    867:                    file->rf_path);
                    868:                return (-1);
                    869:        }
                    870:
1.27      jfb       871:        if (file->rf_expand != NULL)
                    872:                free(file->rf_expand);
1.26      jfb       873:        file->rf_expand = tmp;
                    874:
                    875:        return (0);
                    876: }
                    877:
                    878: /*
                    879:  * rcs_kwexp_get()
                    880:  *
                    881:  * Retrieve the keyword expansion mode to be used for the RCS file <file>.
                    882:  */
                    883: int
                    884: rcs_kwexp_get(RCSFILE *file)
                    885: {
                    886:        return rcs_kflag_get(file->rf_expand);
                    887: }
                    888:
                    889: /*
1.20      jfb       890:  * rcs_kflag_get()
                    891:  *
                    892:  * Get the keyword expansion mode from a set of character flags given in
                    893:  * <flags> and return the appropriate flag mask.  In case of an error, the
                    894:  * returned mask will have the RCS_KWEXP_ERR bit set to 1.
                    895:  */
                    896: int
                    897: rcs_kflag_get(const char *flags)
                    898: {
                    899:        int fl;
                    900:        size_t len;
                    901:        const char *fp;
                    902:
                    903:        fl = 0;
                    904:        len = strlen(flags);
                    905:
                    906:        for (fp = flags; *fp != '\0'; fp++) {
                    907:                if (*fp == 'k')
                    908:                        fl |= RCS_KWEXP_NAME;
                    909:                else if (*fp == 'v')
                    910:                        fl |= RCS_KWEXP_VAL;
                    911:                else if (*fp == 'l')
                    912:                        fl |= RCS_KWEXP_LKR;
                    913:                else if (*fp == 'o') {
                    914:                        if (len != 1)
                    915:                                fl |= RCS_KWEXP_ERR;
                    916:                        fl |= RCS_KWEXP_OLD;
                    917:                } else if (*fp == 'b') {
                    918:                        if (len != 1)
                    919:                                fl |= RCS_KWEXP_ERR;
                    920:                } else  /* unknown letter */
                    921:                        fl |= RCS_KWEXP_ERR;
                    922:        }
                    923:
                    924:        return (fl);
1.1       jfb       925: }
                    926:
1.21      jfb       927: void
                    928: rcs_kflag_usage(void)
                    929: {
                    930:        fprintf(stderr, "Valid expansion modes include:\n"
1.22      jfb       931:            "\t-kkv\tGenerate keywords using the default form.\n"
                    932:            "\t-kkvl\tLike -kkv, except locker's name inserted.\n"
                    933:            "\t-kk\tGenerate only keyword names in keyword strings.\n"
                    934:            "\t-kv\tGenerate only keyword values in keyword strings.\n"
                    935:            "\t-ko\tGenerate old keyword string "
1.21      jfb       936:            "(no changes from checked in file).\n"
1.22      jfb       937:            "\t-kb\tGenerate binary file unmodified (merges not allowed).\n");
1.21      jfb       938: }
1.1       jfb       939:
1.26      jfb       940:
1.1       jfb       941: /*
                    942:  * rcs_parse()
                    943:  *
                    944:  * Parse the contents of file <path>, which are in the RCS format.
                    945:  * Returns 0 on success, or -1 on failure.
                    946:  */
1.26      jfb       947: static int
1.1       jfb       948: rcs_parse(RCSFILE *rfp)
                    949: {
                    950:        int ret;
                    951:        struct rcs_pdata *pdp;
                    952:
1.26      jfb       953:        if (rfp->rf_flags & RCS_PARSED)
1.1       jfb       954:                return (0);
                    955:
1.26      jfb       956:        if ((pdp = (struct rcs_pdata *)malloc(sizeof(*pdp))) == NULL) {
1.1       jfb       957:                cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
                    958:                return (-1);
                    959:        }
                    960:        memset(pdp, 0, sizeof(*pdp));
                    961:
1.18      jfb       962:        pdp->rp_lines = 0;
1.1       jfb       963:        pdp->rp_pttype = RCS_TOK_ERR;
                    964:
                    965:        pdp->rp_file = fopen(rfp->rf_path, "r");
                    966:        if (pdp->rp_file == NULL) {
                    967:                cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
                    968:                rcs_freepdata(pdp);
                    969:                return (-1);
                    970:        }
                    971:
                    972:        pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
                    973:        if (pdp->rp_buf == NULL) {
                    974:                cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
                    975:                rcs_freepdata(pdp);
                    976:                return (-1);
                    977:        }
                    978:        pdp->rp_blen = RCS_BUFSIZE;
1.18      jfb       979:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
1.1       jfb       980:
                    981:        /* ditch the strict lock */
1.26      jfb       982:        rfp->rf_flags &= ~RCS_SLOCK;
1.1       jfb       983:        rfp->rf_pdata = pdp;
                    984:
                    985:        if (rcs_parse_admin(rfp) < 0) {
                    986:                rcs_freepdata(pdp);
                    987:                return (-1);
                    988:        }
                    989:
                    990:        for (;;) {
                    991:                ret = rcs_parse_delta(rfp);
                    992:                if (ret == 0)
                    993:                        break;
                    994:                else if (ret == -1) {
                    995:                        rcs_freepdata(pdp);
                    996:                        return (-1);
                    997:                }
                    998:        }
                    999:
                   1000:        ret = rcs_gettok(rfp);
                   1001:        if (ret != RCS_TOK_DESC) {
                   1002:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1003:                    RCS_TOKSTR(rfp));
                   1004:                rcs_freepdata(pdp);
                   1005:                return (-1);
                   1006:        }
                   1007:
                   1008:        ret = rcs_gettok(rfp);
                   1009:        if (ret != RCS_TOK_STRING) {
                   1010:                cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
                   1011:                    RCS_TOKSTR(rfp));
                   1012:                rcs_freepdata(pdp);
                   1013:                return (-1);
                   1014:        }
                   1015:
                   1016:        rfp->rf_desc = strdup(RCS_TOKSTR(rfp));
1.10      joris    1017:        if (rfp->rf_desc == NULL) {
                   1018:                cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1019:                rcs_freepdata(pdp);
                   1020:                return (-1);
                   1021:        }
1.1       jfb      1022:
                   1023:        for (;;) {
                   1024:                ret = rcs_parse_deltatext(rfp);
                   1025:                if (ret == 0)
                   1026:                        break;
                   1027:                else if (ret == -1) {
                   1028:                        rcs_freepdata(pdp);
                   1029:                        return (-1);
                   1030:                }
                   1031:        }
                   1032:
                   1033:        cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
1.18      jfb      1034:            pdp->rp_lines);
1.1       jfb      1035:
                   1036:        rcs_freepdata(pdp);
                   1037:
                   1038:        rfp->rf_pdata = NULL;
1.26      jfb      1039:        rfp->rf_flags |= RCS_PARSED | RCS_SYNCED;
1.1       jfb      1040:
                   1041:        return (0);
                   1042: }
                   1043:
                   1044:
                   1045: /*
                   1046:  * rcs_parse_admin()
                   1047:  *
                   1048:  * Parse the administrative portion of an RCS file.
                   1049:  * Returns 0 on success, or -1 on failure.
                   1050:  */
                   1051: static int
                   1052: rcs_parse_admin(RCSFILE *rfp)
                   1053: {
                   1054:        u_int i;
                   1055:        int tok, ntok, hmask;
                   1056:        struct rcs_key *rk;
                   1057:
                   1058:        /* hmask is a mask of the headers already encountered */
                   1059:        hmask = 0;
                   1060:        for (;;) {
                   1061:                tok = rcs_gettok(rfp);
                   1062:                if (tok == RCS_TOK_ERR) {
                   1063:                        cvs_log(LP_ERR, "parse error in RCS admin section");
                   1064:                        return (-1);
1.14      deraadt  1065:                } else if (tok == RCS_TOK_NUM) {
1.1       jfb      1066:                        /* assume this is the start of the first delta */
                   1067:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
                   1068:                        return (0);
                   1069:                }
                   1070:
                   1071:                rk = NULL;
1.18      jfb      1072:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1073:                        if (rcs_keys[i].rk_id == tok)
                   1074:                                rk = &(rcs_keys[i]);
                   1075:
                   1076:                if (hmask & (1 << tok)) {
                   1077:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1078:                        return (-1);
                   1079:                }
                   1080:                hmask |= (1 << tok);
                   1081:
                   1082:                switch (tok) {
                   1083:                case RCS_TOK_HEAD:
                   1084:                case RCS_TOK_BRANCH:
                   1085:                case RCS_TOK_COMMENT:
                   1086:                case RCS_TOK_EXPAND:
                   1087:                        ntok = rcs_gettok(rfp);
                   1088:                        if (ntok == RCS_TOK_SCOLON)
                   1089:                                break;
                   1090:                        if (ntok != rk->rk_val) {
                   1091:                                cvs_log(LP_ERR,
                   1092:                                    "invalid value type for RCS key `%s'",
                   1093:                                    rk->rk_str);
                   1094:                        }
                   1095:
                   1096:                        if (tok == RCS_TOK_HEAD) {
1.28    ! jfb      1097:                                if (rfp->rf_head == NULL) {
        !          1098:                                        rfp->rf_head = rcsnum_alloc();
        !          1099:                                        if (rfp->rf_head == NULL)
        !          1100:                                                return (-1);
        !          1101:                                }
1.1       jfb      1102:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1103:                                    rfp->rf_head);
1.14      deraadt  1104:                        } else if (tok == RCS_TOK_BRANCH) {
1.1       jfb      1105:                                rcsnum_aton(RCS_TOKSTR(rfp), NULL,
                   1106:                                    rfp->rf_branch);
1.14      deraadt  1107:                        } else if (tok == RCS_TOK_COMMENT) {
1.1       jfb      1108:                                rfp->rf_comment = strdup(RCS_TOKSTR(rfp));
1.10      joris    1109:                                if (rfp->rf_comment == NULL) {
                   1110:                                        cvs_log(LP_ERRNO,
                   1111:                                            "failed to duplicate rcs token");
                   1112:                                        return (-1);
                   1113:                                }
1.14      deraadt  1114:                        } else if (tok == RCS_TOK_EXPAND) {
1.1       jfb      1115:                                rfp->rf_expand = strdup(RCS_TOKSTR(rfp));
1.10      joris    1116:                                if (rfp->rf_expand == NULL) {
                   1117:                                        cvs_log(LP_ERRNO,
                   1118:                                            "failed to duplicate rcs token");
                   1119:                                        return (-1);
                   1120:                                }
1.1       jfb      1121:                        }
                   1122:
                   1123:                        /* now get the expected semi-colon */
                   1124:                        ntok = rcs_gettok(rfp);
                   1125:                        if (ntok != RCS_TOK_SCOLON) {
                   1126:                                cvs_log(LP_ERR,
                   1127:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1128:                                    rk->rk_str);
1.1       jfb      1129:                                return (-1);
                   1130:                        }
                   1131:                        break;
                   1132:                case RCS_TOK_ACCESS:
                   1133:                        rcs_parse_access(rfp);
                   1134:                        break;
                   1135:                case RCS_TOK_SYMBOLS:
                   1136:                        rcs_parse_symbols(rfp);
                   1137:                        break;
                   1138:                case RCS_TOK_LOCKS:
                   1139:                        rcs_parse_locks(rfp);
                   1140:                        break;
                   1141:                default:
                   1142:                        cvs_log(LP_ERR,
                   1143:                            "unexpected token `%s' in RCS admin section",
                   1144:                            RCS_TOKSTR(rfp));
                   1145:                        return (-1);
                   1146:                }
                   1147:        }
                   1148:
                   1149:        return (0);
                   1150: }
                   1151:
                   1152:
                   1153: /*
                   1154:  * rcs_parse_delta()
                   1155:  *
                   1156:  * Parse an RCS delta section and allocate the structure to store that delta's
                   1157:  * information in the <rfp> delta list.
                   1158:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1159:  * -1 on error.
                   1160:  */
                   1161: static int
                   1162: rcs_parse_delta(RCSFILE *rfp)
                   1163: {
                   1164:        int ret, tok, ntok, hmask;
                   1165:        u_int i;
                   1166:        char *tokstr;
1.3       vincent  1167:        RCSNUM *datenum;
1.1       jfb      1168:        struct rcs_delta *rdp;
                   1169:        struct rcs_key *rk;
                   1170:
                   1171:        rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
                   1172:        if (rdp == NULL) {
                   1173:                cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
                   1174:                return (-1);
                   1175:        }
                   1176:        memset(rdp, 0, sizeof(*rdp));
                   1177:
                   1178:        rdp->rd_num = rcsnum_alloc();
1.11      joris    1179:        if (rdp->rd_num == NULL) {
                   1180:                rcs_freedelta(rdp);
                   1181:                return (-1);
                   1182:        }
1.1       jfb      1183:        rdp->rd_next = rcsnum_alloc();
1.11      joris    1184:        if (rdp->rd_next == NULL) {
                   1185:                rcs_freedelta(rdp);
                   1186:                return (-1);
                   1187:        }
1.1       jfb      1188:
                   1189:        TAILQ_INIT(&(rdp->rd_branches));
                   1190:
                   1191:        tok = rcs_gettok(rfp);
                   1192:        if (tok != RCS_TOK_NUM) {
                   1193:                cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
                   1194:                    RCS_TOKSTR(rfp));
                   1195:                rcs_freedelta(rdp);
                   1196:                return (-1);
                   1197:        }
                   1198:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);
                   1199:
                   1200:        hmask = 0;
                   1201:        ret = 0;
                   1202:        tokstr = NULL;
                   1203:
                   1204:        for (;;) {
                   1205:                tok = rcs_gettok(rfp);
                   1206:                if (tok == RCS_TOK_ERR) {
                   1207:                        cvs_log(LP_ERR, "parse error in RCS delta section");
                   1208:                        rcs_freedelta(rdp);
                   1209:                        return (-1);
1.14      deraadt  1210:                } else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
1.15      tedu     1211:                        rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
1.1       jfb      1212:                        ret = (tok == RCS_TOK_NUM ? 1 : 0);
                   1213:                        break;
                   1214:                }
                   1215:
                   1216:                rk = NULL;
1.18      jfb      1217:                for (i = 0; i < RCS_NKEYS; i++)
1.1       jfb      1218:                        if (rcs_keys[i].rk_id == tok)
                   1219:                                rk = &(rcs_keys[i]);
                   1220:
                   1221:                if (hmask & (1 << tok)) {
                   1222:                        cvs_log(LP_ERR, "duplicate RCS key");
                   1223:                        rcs_freedelta(rdp);
                   1224:                        return (-1);
                   1225:                }
                   1226:                hmask |= (1 << tok);
                   1227:
                   1228:                switch (tok) {
                   1229:                case RCS_TOK_DATE:
                   1230:                case RCS_TOK_AUTHOR:
                   1231:                case RCS_TOK_STATE:
                   1232:                case RCS_TOK_NEXT:
                   1233:                        ntok = rcs_gettok(rfp);
                   1234:                        if (ntok == RCS_TOK_SCOLON) {
                   1235:                                if (rk->rk_flags & RCS_VOPT)
                   1236:                                        break;
                   1237:                                else {
                   1238:                                        cvs_log(LP_ERR, "missing mandatory "
                   1239:                                            "value to RCS key `%s'",
                   1240:                                            rk->rk_str);
                   1241:                                        rcs_freedelta(rdp);
                   1242:                                        return (-1);
                   1243:                                }
                   1244:                        }
                   1245:
                   1246:                        if (ntok != rk->rk_val) {
                   1247:                                cvs_log(LP_ERR,
                   1248:                                    "invalid value type for RCS key `%s'",
                   1249:                                    rk->rk_str);
                   1250:                                rcs_freedelta(rdp);
                   1251:                                return (-1);
                   1252:                        }
                   1253:
                   1254:                        if (tokstr != NULL)
                   1255:                                free(tokstr);
                   1256:                        tokstr = strdup(RCS_TOKSTR(rfp));
1.10      joris    1257:                        if (tokstr == NULL) {
1.15      tedu     1258:                                cvs_log(LP_ERRNO,
1.10      joris    1259:                                    "failed to duplicate rcs token");
                   1260:                                rcs_freedelta(rdp);
                   1261:                                return (-1);
                   1262:                        }
1.1       jfb      1263:
                   1264:                        /* now get the expected semi-colon */
                   1265:                        ntok = rcs_gettok(rfp);
                   1266:                        if (ntok != RCS_TOK_SCOLON) {
                   1267:                                cvs_log(LP_ERR,
                   1268:                                    "missing semi-colon after RCS `%s' key",
1.26      jfb      1269:                                    rk->rk_str);
1.1       jfb      1270:                                rcs_freedelta(rdp);
                   1271:                                return (-1);
                   1272:                        }
                   1273:
                   1274:                        if (tok == RCS_TOK_DATE) {
1.25      jfb      1275:                                if ((datenum = rcsnum_parse(tokstr)) == NULL) {
1.11      joris    1276:                                        rcs_freedelta(rdp);
                   1277:                                        return (-1);
                   1278:                                }
1.3       vincent  1279:                                if (datenum->rn_len != 6) {
1.1       jfb      1280:                                        cvs_log(LP_ERR,
                   1281:                                            "RCS date specification has %s "
                   1282:                                            "fields",
1.3       vincent  1283:                                            (datenum->rn_len > 6) ? "too many" :
1.1       jfb      1284:                                            "missing");
                   1285:                                        rcs_freedelta(rdp);
                   1286:                                }
1.3       vincent  1287:                                rdp->rd_date.tm_year = datenum->rn_id[0];
1.19      jfb      1288:                                if (rdp->rd_date.tm_year >= 1900)
                   1289:                                        rdp->rd_date.tm_year -= 1900;
1.3       vincent  1290:                                rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
                   1291:                                rdp->rd_date.tm_mday = datenum->rn_id[2];
                   1292:                                rdp->rd_date.tm_hour = datenum->rn_id[3];
                   1293:                                rdp->rd_date.tm_min = datenum->rn_id[4];
                   1294:                                rdp->rd_date.tm_sec = datenum->rn_id[5];
                   1295:                                rcsnum_free(datenum);
1.14      deraadt  1296:                        } else if (tok == RCS_TOK_AUTHOR) {
1.1       jfb      1297:                                rdp->rd_author = tokstr;
                   1298:                                tokstr = NULL;
1.14      deraadt  1299:                        } else if (tok == RCS_TOK_STATE) {
1.1       jfb      1300:                                rdp->rd_state = tokstr;
                   1301:                                tokstr = NULL;
1.14      deraadt  1302:                        } else if (tok == RCS_TOK_NEXT) {
1.1       jfb      1303:                                rcsnum_aton(tokstr, NULL, rdp->rd_next);
                   1304:                        }
                   1305:                        break;
                   1306:                case RCS_TOK_BRANCHES:
                   1307:                        rcs_parse_branches(rfp, rdp);
                   1308:                        break;
                   1309:                default:
                   1310:                        cvs_log(LP_ERR,
                   1311:                            "unexpected token `%s' in RCS delta",
                   1312:                            RCS_TOKSTR(rfp));
                   1313:                        rcs_freedelta(rdp);
                   1314:                        return (-1);
                   1315:                }
                   1316:        }
                   1317:
1.13      jfb      1318:        if (tokstr != NULL)
                   1319:                free(tokstr);
                   1320:
1.1       jfb      1321:        TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);
1.26      jfb      1322:        rfp->rf_ndelta++;
1.1       jfb      1323:
                   1324:        return (ret);
                   1325: }
                   1326:
                   1327:
                   1328: /*
                   1329:  * rcs_parse_deltatext()
                   1330:  *
                   1331:  * Parse an RCS delta text section and fill in the log and text field of the
                   1332:  * appropriate delta section.
                   1333:  * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
                   1334:  * -1 on error.
                   1335:  */
                   1336: static int
                   1337: rcs_parse_deltatext(RCSFILE *rfp)
                   1338: {
                   1339:        int tok;
                   1340:        RCSNUM *tnum;
                   1341:        struct rcs_delta *rdp;
                   1342:
                   1343:        tok = rcs_gettok(rfp);
                   1344:        if (tok == RCS_TOK_EOF)
                   1345:                return (0);
                   1346:
                   1347:        if (tok != RCS_TOK_NUM) {
                   1348:                cvs_log(LP_ERR,
                   1349:                    "unexpected token `%s' at start of RCS delta text",
                   1350:                    RCS_TOKSTR(rfp));
                   1351:                return (-1);
                   1352:        }
1.13      jfb      1353:
                   1354:        tnum = rcsnum_alloc();
                   1355:        if (tnum == NULL)
                   1356:                return (-1);
1.1       jfb      1357:        rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);
                   1358:
                   1359:        TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
                   1360:                if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
                   1361:                        break;
                   1362:        }
1.13      jfb      1363:        rcsnum_free(tnum);
                   1364:
1.1       jfb      1365:        if (rdp == NULL) {
                   1366:                cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
                   1367:                    RCS_TOKSTR(rfp));
                   1368:                return (-1);
                   1369:        }
                   1370:
                   1371:        tok = rcs_gettok(rfp);
                   1372:        if (tok != RCS_TOK_LOG) {
                   1373:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1374:                    RCS_TOKSTR(rfp));
                   1375:                return (-1);
                   1376:        }
                   1377:
                   1378:        tok = rcs_gettok(rfp);
                   1379:        if (tok != RCS_TOK_STRING) {
                   1380:                cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
                   1381:                    RCS_TOKSTR(rfp));
                   1382:                return (-1);
                   1383:        }
                   1384:        rdp->rd_log = strdup(RCS_TOKSTR(rfp));
                   1385:        if (rdp->rd_log == NULL) {
                   1386:                cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
                   1387:                return (-1);
                   1388:        }
                   1389:
                   1390:        tok = rcs_gettok(rfp);
                   1391:        if (tok != RCS_TOK_TEXT) {
                   1392:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1393:                    RCS_TOKSTR(rfp));
                   1394:                return (-1);
                   1395:        }
                   1396:
                   1397:        tok = rcs_gettok(rfp);
                   1398:        if (tok != RCS_TOK_STRING) {
                   1399:                cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
                   1400:                    RCS_TOKSTR(rfp));
                   1401:                return (-1);
                   1402:        }
                   1403:
                   1404:        rdp->rd_text = strdup(RCS_TOKSTR(rfp));
                   1405:        if (rdp->rd_text == NULL) {
                   1406:                cvs_log(LP_ERRNO, "failed to copy RCS delta text");
                   1407:                return (-1);
                   1408:        }
                   1409:
                   1410:        return (1);
                   1411: }
                   1412:
                   1413:
                   1414: /*
                   1415:  * rcs_parse_access()
                   1416:  *
                   1417:  * Parse the access list given as value to the `access' keyword.
                   1418:  * Returns 0 on success, or -1 on failure.
                   1419:  */
                   1420: static int
                   1421: rcs_parse_access(RCSFILE *rfp)
                   1422: {
                   1423:        int type;
                   1424:
                   1425:        while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
                   1426:                if (type != RCS_TOK_ID) {
                   1427:                        cvs_log(LP_ERR, "unexpected token `%s' in access list",
                   1428:                            RCS_TOKSTR(rfp));
                   1429:                        return (-1);
                   1430:                }
                   1431:        }
                   1432:
                   1433:        return (0);
                   1434: }
                   1435:
                   1436:
                   1437: /*
                   1438:  * rcs_parse_symbols()
                   1439:  *
                   1440:  * Parse the symbol list given as value to the `symbols' keyword.
                   1441:  * Returns 0 on success, or -1 on failure.
                   1442:  */
                   1443: static int
                   1444: rcs_parse_symbols(RCSFILE *rfp)
                   1445: {
                   1446:        int type;
                   1447:        struct rcs_sym *symp;
                   1448:
                   1449:        for (;;) {
                   1450:                type = rcs_gettok(rfp);
                   1451:                if (type == RCS_TOK_SCOLON)
                   1452:                        break;
                   1453:
                   1454:                if (type != RCS_TOK_STRING) {
                   1455:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1456:                            RCS_TOKSTR(rfp));
                   1457:                        return (-1);
                   1458:                }
                   1459:
                   1460:                symp = (struct rcs_sym *)malloc(sizeof(*symp));
                   1461:                if (symp == NULL) {
                   1462:                        cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
                   1463:                        return (-1);
                   1464:                }
                   1465:                symp->rs_name = strdup(RCS_TOKSTR(rfp));
1.10      joris    1466:                if (symp->rs_name == NULL) {
                   1467:                        cvs_log(LP_ERRNO, "failed to duplicate rcs token");
                   1468:                        free(symp);
                   1469:                        return (-1);
                   1470:                }
                   1471:
1.1       jfb      1472:                symp->rs_num = rcsnum_alloc();
1.11      joris    1473:                if (symp->rs_num == NULL) {
                   1474:                        cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
                   1475:                        free(symp);
                   1476:                        return (-1);
                   1477:                }
1.1       jfb      1478:
                   1479:                type = rcs_gettok(rfp);
                   1480:                if (type != RCS_TOK_COLON) {
                   1481:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1482:                            RCS_TOKSTR(rfp));
1.11      joris    1483:                        rcsnum_free(symp->rs_num);
1.1       jfb      1484:                        free(symp->rs_name);
                   1485:                        free(symp);
                   1486:                        return (-1);
                   1487:                }
                   1488:
                   1489:                type = rcs_gettok(rfp);
                   1490:                if (type != RCS_TOK_NUM) {
                   1491:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1492:                            RCS_TOKSTR(rfp));
1.11      joris    1493:                        rcsnum_free(symp->rs_num);
1.1       jfb      1494:                        free(symp->rs_name);
                   1495:                        free(symp);
                   1496:                        return (-1);
                   1497:                }
                   1498:
                   1499:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
                   1500:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1501:                            RCS_TOKSTR(rfp));
1.11      joris    1502:                        rcsnum_free(symp->rs_num);
1.1       jfb      1503:                        free(symp->rs_name);
                   1504:                        free(symp);
                   1505:                        return (-1);
                   1506:                }
                   1507:
                   1508:                TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
                   1509:        }
                   1510:
                   1511:        return (0);
                   1512: }
                   1513:
                   1514:
                   1515: /*
                   1516:  * rcs_parse_locks()
                   1517:  *
                   1518:  * Parse the lock list given as value to the `locks' keyword.
                   1519:  * Returns 0 on success, or -1 on failure.
                   1520:  */
                   1521: static int
                   1522: rcs_parse_locks(RCSFILE *rfp)
                   1523: {
                   1524:        int type;
                   1525:        struct rcs_lock *lkp;
                   1526:
                   1527:        for (;;) {
                   1528:                type = rcs_gettok(rfp);
                   1529:                if (type == RCS_TOK_SCOLON)
                   1530:                        break;
                   1531:
                   1532:                if (type != RCS_TOK_ID) {
                   1533:                        cvs_log(LP_ERR, "unexpected token `%s' in lock list",
                   1534:                            RCS_TOKSTR(rfp));
                   1535:                        return (-1);
                   1536:                }
                   1537:
                   1538:                lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
                   1539:                if (lkp == NULL) {
                   1540:                        cvs_log(LP_ERRNO, "failed to allocate RCS lock");
                   1541:                        return (-1);
                   1542:                }
                   1543:                lkp->rl_num = rcsnum_alloc();
1.11      joris    1544:                if (lkp->rl_num == NULL) {
                   1545:                        free(lkp);
                   1546:                        return (-1);
                   1547:                }
1.1       jfb      1548:
                   1549:                type = rcs_gettok(rfp);
                   1550:                if (type != RCS_TOK_COLON) {
                   1551:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1552:                            RCS_TOKSTR(rfp));
                   1553:                        free(lkp);
                   1554:                        return (-1);
                   1555:                }
                   1556:
                   1557:                type = rcs_gettok(rfp);
                   1558:                if (type != RCS_TOK_NUM) {
                   1559:                        cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
                   1560:                            RCS_TOKSTR(rfp));
                   1561:                        free(lkp);
                   1562:                        return (-1);
                   1563:                }
                   1564:
                   1565:                if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
                   1566:                        cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
                   1567:                            RCS_TOKSTR(rfp));
                   1568:                        free(lkp);
                   1569:                        return (-1);
                   1570:                }
                   1571:
                   1572:                TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
                   1573:        }
                   1574:
                   1575:        /* check if we have a `strict' */
                   1576:        type = rcs_gettok(rfp);
                   1577:        if (type != RCS_TOK_STRICT) {
                   1578:                rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
1.14      deraadt  1579:        } else {
1.26      jfb      1580:                rfp->rf_flags |= RCS_SLOCK;
1.1       jfb      1581:
                   1582:                type = rcs_gettok(rfp);
                   1583:                if (type != RCS_TOK_SCOLON) {
                   1584:                        cvs_log(LP_ERR,
                   1585:                            "missing semi-colon after `strict' keyword");
                   1586:                        return (-1);
                   1587:                }
                   1588:        }
                   1589:
                   1590:        return (0);
                   1591: }
                   1592:
                   1593: /*
                   1594:  * rcs_parse_branches()
                   1595:  *
                   1596:  * Parse the list of branches following a `branches' keyword in a delta.
                   1597:  * Returns 0 on success, or -1 on failure.
                   1598:  */
                   1599: static int
                   1600: rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
                   1601: {
                   1602:        int type;
                   1603:        struct rcs_branch *brp;
                   1604:
                   1605:        for (;;) {
                   1606:                type = rcs_gettok(rfp);
                   1607:                if (type == RCS_TOK_SCOLON)
                   1608:                        break;
                   1609:
                   1610:                if (type != RCS_TOK_NUM) {
                   1611:                        cvs_log(LP_ERR,
                   1612:                            "unexpected token `%s' in list of branches",
                   1613:                            RCS_TOKSTR(rfp));
                   1614:                        return (-1);
                   1615:                }
                   1616:
                   1617:                brp = (struct rcs_branch *)malloc(sizeof(*brp));
                   1618:                if (brp == NULL) {
                   1619:                        cvs_log(LP_ERRNO, "failed to allocate RCS branch");
                   1620:                        return (-1);
                   1621:                }
                   1622:                brp->rb_num = rcsnum_alloc();
1.11      joris    1623:                if (brp->rb_num == NULL) {
                   1624:                        free(brp);
                   1625:                        return (-1);
                   1626:                }
                   1627:
1.1       jfb      1628:                rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);
                   1629:
                   1630:                TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
                   1631:        }
                   1632:
                   1633:        return (0);
                   1634: }
                   1635:
                   1636:
                   1637: /*
                   1638:  * rcs_freedelta()
                   1639:  *
                   1640:  * Free the contents of a delta structure.
                   1641:  */
1.18      jfb      1642: static void
1.1       jfb      1643: rcs_freedelta(struct rcs_delta *rdp)
                   1644: {
1.12      jfb      1645:        struct rcs_branch *rb;
1.1       jfb      1646:        struct rcs_delta *crdp;
                   1647:
1.12      jfb      1648:        if (rdp->rd_num != NULL)
                   1649:                rcsnum_free(rdp->rd_num);
                   1650:        if (rdp->rd_next != NULL)
                   1651:                rcsnum_free(rdp->rd_next);
                   1652:
1.1       jfb      1653:        if (rdp->rd_author != NULL)
                   1654:                free(rdp->rd_author);
                   1655:        if (rdp->rd_state != NULL)
                   1656:                free(rdp->rd_state);
                   1657:        if (rdp->rd_log != NULL)
                   1658:                free(rdp->rd_log);
                   1659:        if (rdp->rd_text != NULL)
                   1660:                free(rdp->rd_text);
1.12      jfb      1661:
                   1662:        while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
                   1663:                TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
                   1664:                rcsnum_free(rb->rb_num);
                   1665:                free(rb);
                   1666:        }
1.1       jfb      1667:
                   1668:        while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
                   1669:                TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
                   1670:                rcs_freedelta(crdp);
                   1671:        }
                   1672:
                   1673:        free(rdp);
                   1674: }
                   1675:
                   1676:
                   1677: /*
                   1678:  * rcs_freepdata()
                   1679:  *
                   1680:  * Free the contents of the parser data structure.
                   1681:  */
                   1682: static void
                   1683: rcs_freepdata(struct rcs_pdata *pd)
                   1684: {
                   1685:        if (pd->rp_file != NULL)
                   1686:                (void)fclose(pd->rp_file);
                   1687:        if (pd->rp_buf != NULL)
                   1688:                free(pd->rp_buf);
                   1689:        free(pd);
                   1690: }
                   1691:
                   1692:
                   1693: /*
                   1694:  * rcs_gettok()
                   1695:  *
                   1696:  * Get the next RCS token from the string <str>.
                   1697:  */
                   1698: static int
                   1699: rcs_gettok(RCSFILE *rfp)
                   1700: {
                   1701:        u_int i;
                   1702:        int ch, last, type;
1.18      jfb      1703:        size_t len;
                   1704:        char *bp;
1.1       jfb      1705:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1706:
                   1707:        type = RCS_TOK_ERR;
                   1708:        bp = pdp->rp_buf;
                   1709:        *bp = '\0';
                   1710:
                   1711:        if (pdp->rp_pttype != RCS_TOK_ERR) {
                   1712:                type = pdp->rp_pttype;
                   1713:                strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
                   1714:                pdp->rp_pttype = RCS_TOK_ERR;
                   1715:                return (type);
                   1716:        }
                   1717:
                   1718:        /* skip leading whitespace */
                   1719:        /* XXX we must skip backspace too for compatibility, should we? */
                   1720:        do {
                   1721:                ch = getc(pdp->rp_file);
                   1722:                if (ch == '\n')
1.18      jfb      1723:                        pdp->rp_lines++;
1.1       jfb      1724:        } while (isspace(ch));
                   1725:
                   1726:        if (ch == EOF) {
                   1727:                type = RCS_TOK_EOF;
1.14      deraadt  1728:        } else if (ch == ';') {
1.1       jfb      1729:                type = RCS_TOK_SCOLON;
1.14      deraadt  1730:        } else if (ch == ':') {
1.1       jfb      1731:                type = RCS_TOK_COLON;
1.14      deraadt  1732:        } else if (isalpha(ch)) {
1.18      jfb      1733:                type = RCS_TOK_STRING;
1.1       jfb      1734:                *(bp++) = ch;
1.18      jfb      1735:                for (;;) {
1.1       jfb      1736:                        ch = getc(pdp->rp_file);
1.11      joris    1737:                        if (!isalnum(ch) && ch != '_' && ch != '-') {
1.1       jfb      1738:                                ungetc(ch, pdp->rp_file);
                   1739:                                break;
                   1740:                        }
                   1741:                        *(bp++) = ch;
1.18      jfb      1742:                        if (bp == pdp->rp_bufend - 1) {
                   1743:                                len = bp - pdp->rp_buf;
                   1744:                                if (rcs_growbuf(rfp) < 0) {
                   1745:                                        type = RCS_TOK_ERR;
                   1746:                                        break;
                   1747:                                }
                   1748:                                bp = pdp->rp_buf + len;
                   1749:                        }
1.1       jfb      1750:                }
                   1751:                *bp = '\0';
                   1752:
1.18      jfb      1753:                if (type != RCS_TOK_ERR) {
                   1754:                        for (i = 0; i < RCS_NKEYS; i++) {
                   1755:                                if (strcmp(rcs_keys[i].rk_str,
                   1756:                                    pdp->rp_buf) == 0) {
                   1757:                                        type = rcs_keys[i].rk_id;
                   1758:                                        break;
                   1759:                                }
1.1       jfb      1760:                        }
                   1761:                }
1.14      deraadt  1762:        } else if (ch == '@') {
1.1       jfb      1763:                /* we have a string */
1.18      jfb      1764:                type = RCS_TOK_STRING;
1.1       jfb      1765:                for (;;) {
                   1766:                        ch = getc(pdp->rp_file);
                   1767:                        if (ch == '@') {
                   1768:                                ch = getc(pdp->rp_file);
                   1769:                                if (ch != '@') {
                   1770:                                        ungetc(ch, pdp->rp_file);
                   1771:                                        break;
                   1772:                                }
1.14      deraadt  1773:                        } else if (ch == '\n')
1.18      jfb      1774:                                pdp->rp_lines++;
1.1       jfb      1775:
                   1776:                        *(bp++) = ch;
1.18      jfb      1777:                        if (bp == pdp->rp_bufend - 1) {
                   1778:                                len = bp - pdp->rp_buf;
                   1779:                                if (rcs_growbuf(rfp) < 0) {
                   1780:                                        type = RCS_TOK_ERR;
                   1781:                                        break;
                   1782:                                }
                   1783:                                bp = pdp->rp_buf + len;
                   1784:                        }
1.1       jfb      1785:                }
                   1786:
                   1787:                *bp = '\0';
1.14      deraadt  1788:        } else if (isdigit(ch)) {
1.1       jfb      1789:                *(bp++) = ch;
                   1790:                last = ch;
                   1791:                type = RCS_TOK_NUM;
                   1792:
                   1793:                for (;;) {
                   1794:                        ch = getc(pdp->rp_file);
1.18      jfb      1795:                        if (bp == pdp->rp_bufend)
1.1       jfb      1796:                                break;
                   1797:                        if (!isdigit(ch) && ch != '.') {
                   1798:                                ungetc(ch, pdp->rp_file);
                   1799:                                break;
                   1800:                        }
                   1801:
                   1802:                        if (last == '.' && ch == '.') {
                   1803:                                type = RCS_TOK_ERR;
                   1804:                                break;
                   1805:                        }
                   1806:                        last = ch;
                   1807:                        *(bp++) = ch;
                   1808:                }
1.18      jfb      1809:                *bp = '\0';
1.1       jfb      1810:        }
                   1811:
                   1812:        return (type);
                   1813: }
                   1814:
                   1815:
                   1816: /*
                   1817:  * rcs_pushtok()
                   1818:  *
                   1819:  * Push a token back in the parser's token buffer.
                   1820:  */
                   1821: static int
                   1822: rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
                   1823: {
                   1824:        struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;
                   1825:
                   1826:        if (pdp->rp_pttype != RCS_TOK_ERR)
                   1827:                return (-1);
                   1828:
                   1829:        pdp->rp_pttype = type;
                   1830:        strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
                   1831:        return (0);
                   1832: }
                   1833:
                   1834:
                   1835: /*
                   1836:  * rcs_stresc()
                   1837:  *
                   1838:  * Performs either escaping or unescaping of the string stored in <str>.
                   1839:  * The operation is to escape special RCS characters if the <esc> argument
                   1840:  * is 1, or unescape otherwise.  The result is stored in the <buf> destination
                   1841:  * buffer, and <blen> must originally point to the size of <buf>.
                   1842:  * Returns the number of bytes which have been read from the source <str> and
                   1843:  * operated on.  The <blen> parameter will contain the number of bytes
                   1844:  * actually copied in <buf>.
                   1845:  */
                   1846: size_t
                   1847: rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
                   1848: {
                   1849:        size_t rlen;
                   1850:        const char *sp;
                   1851:        char *bp, *bep;
                   1852:
                   1853:        rlen = 0;
                   1854:        bp = buf;
                   1855:        bep = buf + *blen - 1;
                   1856:
                   1857:        for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
                   1858:                if (*sp == '@') {
                   1859:                        if (esc) {
                   1860:                                if (bp > (bep - 2))
                   1861:                                        break;
                   1862:                                *(bp++) = '@';
1.14      deraadt  1863:                        } else {
1.1       jfb      1864:                                sp++;
                   1865:                                if (*sp != '@') {
                   1866:                                        cvs_log(LP_WARN,
                   1867:                                            "unknown escape character `%c' in "
                   1868:                                            "RCS file", *sp);
                   1869:                                        if (*sp == '\0')
                   1870:                                                break;
                   1871:                                }
                   1872:                        }
                   1873:                }
                   1874:
                   1875:                *(bp++) = *sp;
                   1876:        }
                   1877:
                   1878:        *bp = '\0';
                   1879:        *blen = (bp - buf);
                   1880:        return (sp - str);
                   1881: }
                   1882:
                   1883:
                   1884: /*
                   1885:  * rcs_splitlines()
                   1886:  *
                   1887:  * Split the contents of a file into a list of lines.
                   1888:  */
                   1889: static struct rcs_foo*
                   1890: rcs_splitlines(const char *fcont)
                   1891: {
                   1892:        char *dcp;
                   1893:        struct rcs_foo *foo;
                   1894:        struct rcs_line *lp;
                   1895:
                   1896:        foo = (struct rcs_foo *)malloc(sizeof(*foo));
                   1897:        if (foo == NULL) {
                   1898:                cvs_log(LP_ERR, "failed to allocate line structure");
                   1899:                return (NULL);
                   1900:        }
                   1901:        TAILQ_INIT(&(foo->rl_lines));
                   1902:        foo->rl_nblines = 0;
                   1903:        foo->rl_data = strdup(fcont);
                   1904:        if (foo->rl_data == NULL) {
                   1905:                cvs_log(LP_ERRNO, "failed to copy file contents");
                   1906:                free(foo);
                   1907:                return (NULL);
                   1908:        }
                   1909:
                   1910:        /*
                   1911:         * Add a first bogus line with line number 0.  This is used so we
                   1912:         * can position the line pointer before 1 when changing the first line
                   1913:         * in rcs_patch().
                   1914:         */
                   1915:        lp = (struct rcs_line *)malloc(sizeof(*lp));
1.5       vincent  1916:        if (lp == NULL)
1.1       jfb      1917:                return (NULL);
1.5       vincent  1918:
1.1       jfb      1919:        lp->rl_line = NULL;
                   1920:        lp->rl_lineno = 0;
                   1921:        TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   1922:
                   1923:
                   1924:        for (dcp = foo->rl_data; *dcp != '\0';) {
                   1925:                lp = (struct rcs_line *)malloc(sizeof(*lp));
                   1926:                if (lp == NULL) {
                   1927:                        cvs_log(LP_ERR, "failed to allocate line entry");
                   1928:                        return (NULL);
                   1929:                }
                   1930:
                   1931:                lp->rl_line = dcp;
                   1932:                lp->rl_lineno = ++(foo->rl_nblines);
                   1933:                TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);
                   1934:
                   1935:                dcp = strchr(dcp, '\n');
                   1936:                if (dcp == NULL) {
                   1937:                        break;
                   1938:                }
                   1939:                *(dcp++) = '\0';
                   1940:        }
                   1941:
                   1942:        return (foo);
1.5       vincent  1943: }
                   1944:
                   1945: static void
                   1946: rcs_freefoo(struct rcs_foo *fp)
                   1947: {
                   1948:        struct rcs_line *lp;
                   1949:
                   1950:        while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
                   1951:                TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
                   1952:                free(lp);
                   1953:        }
                   1954:        free(fp->rl_data);
                   1955:        free(fp);
1.18      jfb      1956: }
                   1957:
                   1958: /*
                   1959:  * rcs_growbuf()
                   1960:  *
                   1961:  * Attempt to grow the internal parse buffer for the RCS file <rf> by
                   1962:  * RCS_BUFEXTSIZE.
                   1963:  * In case of failure, the original buffer is left unmodified.
                   1964:  * Returns 0 on success, or -1 on failure.
                   1965:  */
                   1966: static int
                   1967: rcs_growbuf(RCSFILE *rf)
                   1968: {
                   1969:        void *tmp;
                   1970:        struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
                   1971:
                   1972:        tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
                   1973:        if (tmp == NULL) {
                   1974:                cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
                   1975:                return (-1);
                   1976:        }
                   1977:
                   1978:        pdp->rp_buf = (char *)tmp;
                   1979:        pdp->rp_blen += RCS_BUFEXTSIZE;
                   1980:        pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
                   1981:
                   1982:        return (0);
1.1       jfb      1983: }