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

Annotation of src/usr.bin/rcs/rcsutil.c, Revision 1.36

1.36    ! tobias      1: /*     $OpenBSD: rcsutil.c,v 1.35 2010/07/28 09:07:11 ray Exp $        */
1.1       xsa         2: /*
                      3:  * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
                      4:  * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
                      5:  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
                      6:  * Copyright (c) 2006 Ray Lai <ray@openbsd.org>
                      7:  * All rights reserved.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  *
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. The name of the author may not be used to endorse or promote products
                     16:  *    derived from this software without specific prior written permission.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     19:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     20:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     21:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     22:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     23:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     24:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     25:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     26:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     27:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29:
1.29      xsa        30: #include <sys/stat.h>
                     31:
                     32: #include <ctype.h>
                     33: #include <err.h>
                     34: #include <fcntl.h>
                     35: #include <stdio.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
1.1       xsa        38:
                     39: #include "rcsprog.h"
                     40:
                     41: /*
                     42:  * rcs_get_mtime()
                     43:  *
                     44:  * Get <filename> last modified time.
                     45:  * Returns last modified time on success, or -1 on failure.
                     46:  */
                     47: time_t
1.6       joris      48: rcs_get_mtime(RCSFILE *file)
1.1       xsa        49: {
                     50:        struct stat st;
                     51:        time_t mtime;
                     52:
1.13      ray        53:        if (fstat(file->rf_fd, &st) == -1) {
1.6       joris      54:                warn("%s", file->rf_path);
1.1       xsa        55:                return (-1);
                     56:        }
1.6       joris      57:
1.1       xsa        58:        mtime = (time_t)st.st_mtimespec.tv_sec;
                     59:
                     60:        return (mtime);
                     61: }
                     62:
                     63: /*
                     64:  * rcs_set_mtime()
                     65:  *
                     66:  * Set <filename> last modified time to <mtime> if it's not set to -1.
                     67:  */
                     68: void
1.6       joris      69: rcs_set_mtime(RCSFILE *file, time_t mtime)
1.1       xsa        70: {
                     71:        static struct timeval tv[2];
                     72:
                     73:        if (mtime == -1)
                     74:                return;
                     75:
                     76:        tv[0].tv_sec = mtime;
                     77:        tv[1].tv_sec = tv[0].tv_sec;
                     78:
1.13      ray        79:        if (futimes(file->rf_fd, tv) == -1)
1.3       xsa        80:                err(1, "utimes");
1.1       xsa        81: }
                     82:
                     83: int
                     84: rcs_getopt(int argc, char **argv, const char *optstr)
                     85: {
                     86:        char *a;
                     87:        const char *c;
                     88:        static int i = 1;
                     89:        int opt, hasargument, ret;
                     90:
                     91:        hasargument = 0;
                     92:        rcs_optarg = NULL;
                     93:
                     94:        if (i >= argc)
                     95:                return (-1);
                     96:
                     97:        a = argv[i++];
                     98:        if (*a++ != '-')
                     99:                return (-1);
                    100:
                    101:        ret = 0;
                    102:        opt = *a;
                    103:        for (c = optstr; *c != '\0'; c++) {
                    104:                if (*c == opt) {
                    105:                        a++;
                    106:                        ret = opt;
                    107:
                    108:                        if (*(c + 1) == ':') {
                    109:                                if (*(c + 2) == ':') {
                    110:                                        if (*a != '\0')
                    111:                                                hasargument = 1;
                    112:                                } else {
                    113:                                        if (*a != '\0') {
                    114:                                                hasargument = 1;
                    115:                                        } else {
                    116:                                                ret = 1;
                    117:                                                break;
                    118:                                        }
                    119:                                }
                    120:                        }
                    121:
                    122:                        if (hasargument == 1)
                    123:                                rcs_optarg = a;
                    124:
                    125:                        if (ret == opt)
                    126:                                rcs_optind++;
                    127:                        break;
                    128:                }
                    129:        }
                    130:
                    131:        if (ret == 0)
                    132:                warnx("unknown option -%c", opt);
                    133:        else if (ret == 1)
                    134:                warnx("missing argument for option -%c", opt);
                    135:
                    136:        return (ret);
                    137: }
                    138:
                    139: /*
                    140:  * rcs_choosefile()
                    141:  *
                    142:  * Given a relative filename, decide where the corresponding RCS file
                    143:  * should be.  Tries each extension until a file is found.  If no file
                    144:  * was found, returns a path with the first extension.
                    145:  *
1.11      ray       146:  * Opens and returns file descriptor to RCS file.
1.1       xsa       147:  */
1.6       joris     148: int
                    149: rcs_choosefile(const char *filename, char *out, size_t len)
1.1       xsa       150: {
1.6       joris     151:        int fd;
1.1       xsa       152:        struct stat sb;
                    153:        char *p, *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
1.10      ray       154:            *suffixes, rcspath[MAXPATHLEN];
1.1       xsa       155:
1.6       joris     156:        fd = -1;
                    157:
1.1       xsa       158:        /*
                    159:         * If `filename' contains a directory, `rcspath' contains that
                    160:         * directory, including a trailing slash.  Otherwise `rcspath'
                    161:         * contains an empty string.
                    162:         */
                    163:        if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     164:                errx(1, "rcs_choosefile: truncation");
                    165:
1.1       xsa       166:        /* If `/' is found, end string after `/'. */
                    167:        if ((ptr = strrchr(rcspath, '/')) != NULL)
                    168:                *(++ptr) = '\0';
                    169:        else
                    170:                rcspath[0] = '\0';
                    171:
                    172:        /* Append RCS/ to `rcspath' if it exists. */
                    173:        if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
                    174:            strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
1.6       joris     175:                errx(1, "rcs_choosefile: truncation");
                    176:
1.19      otto      177:        if (stat(rcsdir, &sb) == 0 && S_ISDIR(sb.st_mode))
1.6       joris     178:                if (strlcpy(rcspath, rcsdir, sizeof(rcspath))
                    179:                    >= sizeof(rcspath) ||
1.1       xsa       180:                    strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     181:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       182:
                    183:        /* Name of file without path. */
                    184:        if ((ptr = strrchr(filename, '/')) == NULL) {
                    185:                if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
1.6       joris     186:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       187:        } else {
                    188:                /* Skip `/'. */
                    189:                if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
1.6       joris     190:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       191:        }
                    192:
                    193:        /* Name of RCS file without an extension. */
                    194:        if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     195:                errx(1, "rcs_choosefile: truncation");
1.1       xsa       196:
                    197:        /*
                    198:         * If only the empty suffix was given, use existing rcspath.
                    199:         * This ensures that there is at least one suffix for strsep().
                    200:         */
                    201:        if (strcmp(rcs_suffixes, "") == 0) {
1.8       ray       202:                if (strlcpy(out, rcspath, len) >= len)
                    203:                        errx(1, "rcs_choosefile: truncation");
1.22      niallo    204:                fd = open(rcspath, O_RDONLY);
1.6       joris     205:                return (fd);
1.1       xsa       206:        }
                    207:
                    208:        /*
                    209:         * Cycle through slash-separated `rcs_suffixes', appending each
                    210:         * extension to `rcspath' and testing if the file exists.  If it
                    211:         * does, return that string.  Otherwise return path with first
                    212:         * extension.
                    213:         */
                    214:        suffixes = xstrdup(rcs_suffixes);
1.10      ray       215:        for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
1.1       xsa       216:                char fpath[MAXPATHLEN];
                    217:
                    218:                if ((p = strrchr(rcspath, ',')) != NULL) {
                    219:                        if (!strcmp(p, ext)) {
1.6       joris     220:                                if ((fd = open(rcspath, O_RDONLY)) == -1)
                    221:                                        continue;
                    222:
                    223:                                if (fstat(fd, &sb) == -1)
                    224:                                        err(1, "%s", rcspath);
                    225:
                    226:                                if (strlcpy(out, rcspath, len) >= len)
                    227:                                        errx(1, "rcs_choosefile; truncation");
                    228:
1.33      joris     229:                                xfree(suffixes);
1.6       joris     230:                                return (fd);
1.1       xsa       231:                        }
                    232:
                    233:                        continue;
                    234:                }
                    235:
                    236:                /* Construct RCS file path. */
                    237:                if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
                    238:                    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
1.6       joris     239:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       240:
                    241:                /* Don't use `filename' as RCS file. */
                    242:                if (strcmp(fpath, filename) == 0)
                    243:                        continue;
                    244:
1.6       joris     245:                if ((fd = open(fpath, O_RDONLY)) == -1)
                    246:                        continue;
                    247:
                    248:                if (fstat(fd, &sb) == -1)
                    249:                        err(1, "%s", fpath);
                    250:
                    251:                if (strlcpy(out, fpath, len) >= len)
                    252:                        errx(1, "rcs_choosefile: truncation");
                    253:
1.33      joris     254:                xfree(suffixes);
1.6       joris     255:                return (fd);
1.1       xsa       256:        }
                    257:
                    258:        /*
                    259:         * `suffixes' should now be NUL separated, so the first
                    260:         * extension can be read just by reading `suffixes'.
                    261:         */
1.11      ray       262:        if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     263:                errx(1, "rcs_choosefile: truncation");
1.1       xsa       264:
                    265:        xfree(suffixes);
1.6       joris     266:
1.8       ray       267:        if (strlcpy(out, rcspath, len) >= len)
                    268:                errx(1, "rcs_choosefile: truncation");
1.22      niallo    269:
                    270:        fd = open(rcspath, O_RDONLY);
1.1       xsa       271:
1.6       joris     272:        return (fd);
1.1       xsa       273: }
                    274:
                    275: /*
                    276:  * Set <str> to <new_str>.  Print warning if <str> is redefined.
                    277:  */
                    278: void
                    279: rcs_setrevstr(char **str, char *new_str)
                    280: {
                    281:        if (new_str == NULL)
                    282:                return;
                    283:        if (*str != NULL)
                    284:                warnx("redefinition of revision number");
                    285:        *str = new_str;
                    286: }
                    287:
                    288: /*
                    289:  * Set <str1> or <str2> to <new_str>, depending on which is not set.
                    290:  * If both are set, error out.
                    291:  */
                    292: void
                    293: rcs_setrevstr2(char **str1, char **str2, char *new_str)
                    294: {
                    295:        if (new_str == NULL)
                    296:                return;
                    297:        if (*str1 == NULL)
                    298:                *str1 = new_str;
                    299:        else if (*str2 == NULL)
                    300:                *str2 = new_str;
                    301:        else
1.3       xsa       302:                errx(1, "too many revision numbers");
1.1       xsa       303: }
                    304:
                    305: /*
                    306:  * Get revision from file.  The revision can be specified as a symbol or
                    307:  * a revision number.
                    308:  */
                    309: RCSNUM *
                    310: rcs_getrevnum(const char *rev_str, RCSFILE *file)
                    311: {
                    312:        RCSNUM *rev;
                    313:
                    314:        /* Search for symbol. */
                    315:        rev = rcs_sym_getrev(file, rev_str);
                    316:
                    317:        /* Search for revision number. */
                    318:        if (rev == NULL)
                    319:                rev = rcsnum_parse(rev_str);
                    320:
                    321:        return (rev);
                    322: }
                    323:
                    324: /*
                    325:  * Prompt for and store user's input in an allocated string.
                    326:  *
                    327:  * Returns the string's pointer.
                    328:  */
                    329: char *
                    330: rcs_prompt(const char *prompt)
                    331: {
                    332:        BUF *bp;
                    333:        size_t len;
                    334:        char *buf;
                    335:
1.35      ray       336:        bp = buf_alloc(0);
1.1       xsa       337:        if (isatty(STDIN_FILENO))
                    338:                (void)fprintf(stderr, "%s", prompt);
                    339:        if (isatty(STDIN_FILENO))
                    340:                (void)fprintf(stderr, ">> ");
1.25      ray       341:        clearerr(stdin);
1.1       xsa       342:        while ((buf = fgetln(stdin, &len)) != NULL) {
                    343:                /* The last line may not be EOL terminated. */
                    344:                if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
                    345:                        break;
                    346:                else
1.34      ray       347:                        buf_append(bp, buf, len);
1.1       xsa       348:
                    349:                if (isatty(STDIN_FILENO))
                    350:                        (void)fprintf(stderr, ">> ");
                    351:        }
1.34      ray       352:        buf_putc(bp, '\0');
1.1       xsa       353:
1.34      ray       354:        return (buf_release(bp));
1.1       xsa       355: }
                    356:
                    357: u_int
1.14      ray       358: rcs_rev_select(RCSFILE *file, const char *range)
1.1       xsa       359: {
                    360:        int i;
                    361:        u_int nrev;
                    362:        char *ep;
                    363:        char *lstr, *rstr;
                    364:        struct rcs_delta *rdp;
1.5       joris     365:        struct rcs_argvector *revargv, *revrange;
1.1       xsa       366:        RCSNUM lnum, rnum;
                    367:
                    368:        nrev = 0;
                    369:        (void)memset(&lnum, 0, sizeof(lnum));
                    370:        (void)memset(&rnum, 0, sizeof(rnum));
                    371:
                    372:        if (range == NULL) {
                    373:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    374:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    375:                                rdp->rd_flags |= RCS_RD_SELECT;
                    376:                                return (1);
                    377:                        }
                    378:                return (0);
                    379:        }
                    380:
1.5       joris     381:        revargv = rcs_strsplit(range, ",");
1.1       xsa       382:        for (i = 0; revargv->argv[i] != NULL; i++) {
1.5       joris     383:                revrange = rcs_strsplit(revargv->argv[i], ":");
1.1       xsa       384:                if (revrange->argv[0] == NULL)
                    385:                        /* should not happen */
1.3       xsa       386:                        errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1       xsa       387:                else if (revrange->argv[1] == NULL)
                    388:                        lstr = rstr = revrange->argv[0];
                    389:                else {
                    390:                        if (revrange->argv[2] != NULL)
1.3       xsa       391:                                errx(1, "invalid revision range: %s",
                    392:                                    revargv->argv[i]);
1.1       xsa       393:                        lstr = revrange->argv[0];
                    394:                        rstr = revrange->argv[1];
                    395:                        if (strcmp(lstr, "") == 0)
                    396:                                lstr = NULL;
                    397:                        if (strcmp(rstr, "") == 0)
                    398:                                rstr = NULL;
                    399:                }
                    400:
                    401:                if (lstr == NULL)
                    402:                        lstr = RCS_HEAD_INIT;
                    403:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3       xsa       404:                        errx(1, "invalid revision: %s", lstr);
1.1       xsa       405:
                    406:                if (rstr != NULL) {
                    407:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3       xsa       408:                                errx(1, "invalid revision: %s", rstr);
1.1       xsa       409:                } else
                    410:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    411:
1.5       joris     412:                rcs_argv_destroy(revrange);
1.1       xsa       413:
                    414:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    415:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    416:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
                    417:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    418:                                rdp->rd_flags |= RCS_RD_SELECT;
                    419:                                nrev++;
                    420:                        }
                    421:        }
1.5       joris     422:        rcs_argv_destroy(revargv);
1.1       xsa       423:
                    424:        if (lnum.rn_id != NULL)
                    425:                xfree(lnum.rn_id);
                    426:        if (rnum.rn_id != NULL)
                    427:                xfree(rnum.rn_id);
                    428:
                    429:        return (nrev);
1.2       ray       430: }
                    431:
                    432: /*
                    433:  * Load description from <in> to <file>.
                    434:  * If <in> starts with a `-', <in> is taken as the description.
                    435:  * Otherwise <in> is the name of the file containing the description.
                    436:  * If <in> is NULL, the description is read from stdin.
1.18      ray       437:  * Returns 0 on success, -1 on failure, setting errno.
1.2       ray       438:  */
1.18      ray       439: int
1.2       ray       440: rcs_set_description(RCSFILE *file, const char *in)
                    441: {
                    442:        BUF *bp;
                    443:        char *content;
                    444:        const char *prompt =
                    445:            "enter description, terminated with single '.' or end of file:\n"
                    446:            "NOTE: This is NOT the log message!\n";
                    447:
                    448:        /* Description is in file <in>. */
                    449:        if (in != NULL && *in != '-') {
1.35      ray       450:                if ((bp = buf_load(in)) == NULL)
1.18      ray       451:                        return (-1);
1.34      ray       452:                buf_putc(bp, '\0');
                    453:                content = buf_release(bp);
1.2       ray       454:        /* Description is in <in>. */
                    455:        } else if (in != NULL)
                    456:                /* Skip leading `-'. */
                    457:                content = xstrdup(in + 1);
                    458:        /* Get description from stdin. */
                    459:        else
                    460:                content = rcs_prompt(prompt);
                    461:
                    462:        rcs_desc_set(file, content);
                    463:        xfree(content);
1.18      ray       464:        return (0);
1.7       xsa       465: }
                    466:
                    467: /*
                    468:  * Split the contents of a file into a list of lines.
                    469:  */
                    470: struct rcs_lines *
1.27      xsa       471: rcs_splitlines(u_char *data, size_t len)
1.7       xsa       472: {
1.15      niallo    473:        u_char *c, *p;
1.7       xsa       474:        struct rcs_lines *lines;
                    475:        struct rcs_line *lp;
1.24      niallo    476:        size_t i, tlen;
1.7       xsa       477:
                    478:        lines = xmalloc(sizeof(*lines));
1.24      niallo    479:        memset(lines, 0, sizeof(*lines));
1.7       xsa       480:        TAILQ_INIT(&(lines->l_lines));
                    481:
                    482:        lp = xmalloc(sizeof(*lp));
1.24      niallo    483:        memset(lp, 0, sizeof(*lp));
1.7       xsa       484:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    485:
                    486:
1.24      niallo    487:        p = c = data;
                    488:        for (i = 0; i < len; i++) {
                    489:                if (*p == '\n' || (i == len - 1)) {
1.26      niallo    490:                        tlen = p - c + 1;
1.15      niallo    491:                        lp = xmalloc(sizeof(*lp));
1.24      niallo    492:                        lp->l_line = c;
                    493:                        lp->l_len = tlen;
1.15      niallo    494:                        lp->l_lineno = ++(lines->l_nblines);
                    495:                        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    496:                        c = p + 1;
                    497:                }
                    498:                p++;
1.7       xsa       499:        }
                    500:
                    501:        return (lines);
                    502: }
                    503:
                    504: void
                    505: rcs_freelines(struct rcs_lines *lines)
                    506: {
                    507:        struct rcs_line *lp;
                    508:
                    509:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    510:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
                    511:                xfree(lp);
                    512:        }
                    513:
                    514:        xfree(lines);
                    515: }
                    516:
                    517: BUF *
1.27      xsa       518: rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
1.7       xsa       519:     int (*p)(struct rcs_lines *, struct rcs_lines *))
                    520: {
                    521:        struct rcs_lines *dlines, *plines;
                    522:        struct rcs_line *lp;
                    523:        BUF *res;
                    524:
1.24      niallo    525:        dlines = rcs_splitlines(data, dlen);
                    526:        plines = rcs_splitlines(patch, plen);
1.7       xsa       527:
                    528:        if (p(dlines, plines) < 0) {
                    529:                rcs_freelines(dlines);
                    530:                rcs_freelines(plines);
                    531:                return (NULL);
                    532:        }
                    533:
1.35      ray       534:        res = buf_alloc(1024);
1.7       xsa       535:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.24      niallo    536:                if (lp->l_line == NULL)
                    537:                        continue;
1.34      ray       538:                buf_append(res, lp->l_line, lp->l_len);
1.7       xsa       539:        }
                    540:
                    541:        rcs_freelines(dlines);
                    542:        rcs_freelines(plines);
                    543:        return (res);
                    544: }
                    545:
                    546: /*
                    547:  * rcs_yesno()
                    548:  *
1.23      millert   549:  * Read a char from standard input, returns defc if the
                    550:  * user enters an equivalent to defc, else whatever char
                    551:  * was entered.  Converts input to lower case.
1.7       xsa       552:  */
                    553: int
1.23      millert   554: rcs_yesno(int defc)
1.7       xsa       555: {
                    556:        int c, ret;
                    557:
                    558:        fflush(stderr);
                    559:        fflush(stdout);
                    560:
1.25      ray       561:        clearerr(stdin);
1.23      millert   562:        if (isalpha(c = getchar()))
                    563:                c = tolower(c);
                    564:        if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
                    565:                ret = defc;
1.7       xsa       566:        else
1.23      millert   567:                ret = c;
                    568:
                    569:        while (c != EOF && c != '\n')
                    570:                c = getchar();
1.7       xsa       571:
                    572:        return (ret);
                    573: }
                    574:
                    575: /*
                    576:  * rcs_strsplit()
                    577:  *
                    578:  * Split a string <str> of <sep>-separated values and allocate
                    579:  * an argument vector for the values found.
                    580:  */
                    581: struct rcs_argvector *
1.14      ray       582: rcs_strsplit(const char *str, const char *sep)
1.7       xsa       583: {
                    584:        struct rcs_argvector *av;
                    585:        size_t i = 0;
                    586:        char *cp, *p;
                    587:
                    588:        cp = xstrdup(str);
                    589:        av = xmalloc(sizeof(*av));
                    590:        av->str = cp;
1.31      ray       591:        av->argv = xmalloc(sizeof(*(av->argv)));
1.7       xsa       592:
                    593:        while ((p = strsep(&cp, sep)) != NULL) {
                    594:                av->argv[i++] = p;
1.30      ray       595:                av->argv = xrealloc(av->argv,
1.7       xsa       596:                    i + 1, sizeof(*(av->argv)));
                    597:        }
                    598:        av->argv[i] = NULL;
                    599:
                    600:        return (av);
                    601: }
                    602:
                    603: /*
                    604:  * rcs_argv_destroy()
                    605:  *
                    606:  * Free an argument vector previously allocated by rcs_strsplit().
                    607:  */
                    608: void
                    609: rcs_argv_destroy(struct rcs_argvector *av)
                    610: {
                    611:        xfree(av->str);
                    612:        xfree(av->argv);
                    613:        xfree(av);
1.28      otto      614: }
                    615:
                    616: /*
                    617:  * Strip suffix from filename.
                    618:  */
                    619: void
                    620: rcs_strip_suffix(char *filename)
                    621: {
                    622:        char *p, *suffixes, *next, *ext;
                    623:
                    624:        if ((p = strrchr(filename, ',')) != NULL) {
                    625:                suffixes = xstrdup(rcs_suffixes);
                    626:                for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
                    627:                        if (!strcmp(p, ext)) {
                    628:                                *p = '\0';
                    629:                                break;
                    630:                        }
                    631:                }
                    632:                xfree(suffixes);
                    633:        }
1.1       xsa       634: }