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

1.32    ! chl         1: /*     $OpenBSD: rcsutil.c,v 1.31 2007/07/19 06:37:58 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:
                    156:        /* If -x flag was not given, use default. */
                    157:        if (rcs_suffixes == NULL)
                    158:                rcs_suffixes = RCS_DEFAULT_SUFFIX;
                    159:
1.6       joris     160:        fd = -1;
                    161:
1.1       xsa       162:        /*
                    163:         * If `filename' contains a directory, `rcspath' contains that
                    164:         * directory, including a trailing slash.  Otherwise `rcspath'
                    165:         * contains an empty string.
                    166:         */
                    167:        if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     168:                errx(1, "rcs_choosefile: truncation");
                    169:
1.1       xsa       170:        /* If `/' is found, end string after `/'. */
                    171:        if ((ptr = strrchr(rcspath, '/')) != NULL)
                    172:                *(++ptr) = '\0';
                    173:        else
                    174:                rcspath[0] = '\0';
                    175:
                    176:        /* Append RCS/ to `rcspath' if it exists. */
                    177:        if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
                    178:            strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
1.6       joris     179:                errx(1, "rcs_choosefile: truncation");
                    180:
1.19      otto      181:        if (stat(rcsdir, &sb) == 0 && S_ISDIR(sb.st_mode))
1.6       joris     182:                if (strlcpy(rcspath, rcsdir, sizeof(rcspath))
                    183:                    >= sizeof(rcspath) ||
1.1       xsa       184:                    strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     185:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       186:
                    187:        /* Name of file without path. */
                    188:        if ((ptr = strrchr(filename, '/')) == NULL) {
                    189:                if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
1.6       joris     190:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       191:        } else {
                    192:                /* Skip `/'. */
                    193:                if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
1.6       joris     194:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       195:        }
                    196:
                    197:        /* Name of RCS file without an extension. */
                    198:        if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     199:                errx(1, "rcs_choosefile: truncation");
1.1       xsa       200:
                    201:        /*
                    202:         * If only the empty suffix was given, use existing rcspath.
                    203:         * This ensures that there is at least one suffix for strsep().
                    204:         */
                    205:        if (strcmp(rcs_suffixes, "") == 0) {
1.8       ray       206:                if (strlcpy(out, rcspath, len) >= len)
                    207:                        errx(1, "rcs_choosefile: truncation");
1.22      niallo    208:                fd = open(rcspath, O_RDONLY);
1.6       joris     209:                return (fd);
1.1       xsa       210:        }
                    211:
                    212:        /*
                    213:         * Cycle through slash-separated `rcs_suffixes', appending each
                    214:         * extension to `rcspath' and testing if the file exists.  If it
                    215:         * does, return that string.  Otherwise return path with first
                    216:         * extension.
                    217:         */
                    218:        suffixes = xstrdup(rcs_suffixes);
1.10      ray       219:        for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
1.1       xsa       220:                char fpath[MAXPATHLEN];
                    221:
                    222:                if ((p = strrchr(rcspath, ',')) != NULL) {
                    223:                        if (!strcmp(p, ext)) {
1.6       joris     224:                                if ((fd = open(rcspath, O_RDONLY)) == -1)
                    225:                                        continue;
                    226:
                    227:                                if (fstat(fd, &sb) == -1)
                    228:                                        err(1, "%s", rcspath);
                    229:
                    230:                                if (strlcpy(out, rcspath, len) >= len)
                    231:                                        errx(1, "rcs_choosefile; truncation");
                    232:
                    233:                                return (fd);
1.1       xsa       234:                        }
                    235:
                    236:                        continue;
                    237:                }
                    238:
                    239:                /* Construct RCS file path. */
                    240:                if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
                    241:                    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
1.6       joris     242:                        errx(1, "rcs_choosefile: truncation");
1.1       xsa       243:
                    244:                /* Don't use `filename' as RCS file. */
                    245:                if (strcmp(fpath, filename) == 0)
                    246:                        continue;
                    247:
1.6       joris     248:                if ((fd = open(fpath, O_RDONLY)) == -1)
                    249:                        continue;
                    250:
                    251:                if (fstat(fd, &sb) == -1)
                    252:                        err(1, "%s", fpath);
                    253:
                    254:                if (strlcpy(out, fpath, len) >= len)
                    255:                        errx(1, "rcs_choosefile: truncation");
                    256:
                    257:                return (fd);
1.1       xsa       258:        }
                    259:
                    260:        /*
                    261:         * `suffixes' should now be NUL separated, so the first
                    262:         * extension can be read just by reading `suffixes'.
                    263:         */
1.11      ray       264:        if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath))
1.6       joris     265:                errx(1, "rcs_choosefile: truncation");
1.1       xsa       266:
                    267:        xfree(suffixes);
1.6       joris     268:
1.8       ray       269:        if (strlcpy(out, rcspath, len) >= len)
                    270:                errx(1, "rcs_choosefile: truncation");
1.22      niallo    271:
                    272:        fd = open(rcspath, O_RDONLY);
1.1       xsa       273:
1.6       joris     274:        return (fd);
1.1       xsa       275: }
                    276:
                    277: /*
                    278:  * Set <str> to <new_str>.  Print warning if <str> is redefined.
                    279:  */
                    280: void
                    281: rcs_setrevstr(char **str, char *new_str)
                    282: {
                    283:        if (new_str == NULL)
                    284:                return;
                    285:        if (*str != NULL)
                    286:                warnx("redefinition of revision number");
                    287:        *str = new_str;
                    288: }
                    289:
                    290: /*
                    291:  * Set <str1> or <str2> to <new_str>, depending on which is not set.
                    292:  * If both are set, error out.
                    293:  */
                    294: void
                    295: rcs_setrevstr2(char **str1, char **str2, char *new_str)
                    296: {
                    297:        if (new_str == NULL)
                    298:                return;
                    299:        if (*str1 == NULL)
                    300:                *str1 = new_str;
                    301:        else if (*str2 == NULL)
                    302:                *str2 = new_str;
                    303:        else
1.3       xsa       304:                errx(1, "too many revision numbers");
1.1       xsa       305: }
                    306:
                    307: /*
                    308:  * Get revision from file.  The revision can be specified as a symbol or
                    309:  * a revision number.
                    310:  */
                    311: RCSNUM *
                    312: rcs_getrevnum(const char *rev_str, RCSFILE *file)
                    313: {
                    314:        RCSNUM *rev;
                    315:
                    316:        /* Search for symbol. */
                    317:        rev = rcs_sym_getrev(file, rev_str);
                    318:
                    319:        /* Search for revision number. */
                    320:        if (rev == NULL)
                    321:                rev = rcsnum_parse(rev_str);
                    322:
                    323:        return (rev);
                    324: }
                    325:
                    326: /*
                    327:  * Prompt for and store user's input in an allocated string.
                    328:  *
                    329:  * Returns the string's pointer.
                    330:  */
                    331: char *
                    332: rcs_prompt(const char *prompt)
                    333: {
                    334:        BUF *bp;
                    335:        size_t len;
                    336:        char *buf;
                    337:
1.5       joris     338:        bp = rcs_buf_alloc(0, BUF_AUTOEXT);
1.1       xsa       339:        if (isatty(STDIN_FILENO))
                    340:                (void)fprintf(stderr, "%s", prompt);
                    341:        if (isatty(STDIN_FILENO))
                    342:                (void)fprintf(stderr, ">> ");
1.25      ray       343:        clearerr(stdin);
1.1       xsa       344:        while ((buf = fgetln(stdin, &len)) != NULL) {
                    345:                /* The last line may not be EOL terminated. */
                    346:                if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
                    347:                        break;
                    348:                else
1.5       joris     349:                        rcs_buf_append(bp, buf, len);
1.1       xsa       350:
                    351:                if (isatty(STDIN_FILENO))
                    352:                        (void)fprintf(stderr, ">> ");
                    353:        }
1.5       joris     354:        rcs_buf_putc(bp, '\0');
1.1       xsa       355:
1.5       joris     356:        return (rcs_buf_release(bp));
1.1       xsa       357: }
                    358:
                    359: u_int
1.14      ray       360: rcs_rev_select(RCSFILE *file, const char *range)
1.1       xsa       361: {
                    362:        int i;
                    363:        u_int nrev;
                    364:        char *ep;
                    365:        char *lstr, *rstr;
                    366:        struct rcs_delta *rdp;
1.5       joris     367:        struct rcs_argvector *revargv, *revrange;
1.1       xsa       368:        RCSNUM lnum, rnum;
                    369:
                    370:        nrev = 0;
                    371:        (void)memset(&lnum, 0, sizeof(lnum));
                    372:        (void)memset(&rnum, 0, sizeof(rnum));
                    373:
                    374:        if (range == NULL) {
                    375:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    376:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    377:                                rdp->rd_flags |= RCS_RD_SELECT;
                    378:                                return (1);
                    379:                        }
                    380:                return (0);
                    381:        }
                    382:
1.5       joris     383:        revargv = rcs_strsplit(range, ",");
1.1       xsa       384:        for (i = 0; revargv->argv[i] != NULL; i++) {
1.5       joris     385:                revrange = rcs_strsplit(revargv->argv[i], ":");
1.1       xsa       386:                if (revrange->argv[0] == NULL)
                    387:                        /* should not happen */
1.3       xsa       388:                        errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1       xsa       389:                else if (revrange->argv[1] == NULL)
                    390:                        lstr = rstr = revrange->argv[0];
                    391:                else {
                    392:                        if (revrange->argv[2] != NULL)
1.3       xsa       393:                                errx(1, "invalid revision range: %s",
                    394:                                    revargv->argv[i]);
1.1       xsa       395:                        lstr = revrange->argv[0];
                    396:                        rstr = revrange->argv[1];
                    397:                        if (strcmp(lstr, "") == 0)
                    398:                                lstr = NULL;
                    399:                        if (strcmp(rstr, "") == 0)
                    400:                                rstr = NULL;
                    401:                }
                    402:
                    403:                if (lstr == NULL)
                    404:                        lstr = RCS_HEAD_INIT;
                    405:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3       xsa       406:                        errx(1, "invalid revision: %s", lstr);
1.1       xsa       407:
                    408:                if (rstr != NULL) {
                    409:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3       xsa       410:                                errx(1, "invalid revision: %s", rstr);
1.1       xsa       411:                } else
                    412:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    413:
1.5       joris     414:                rcs_argv_destroy(revrange);
1.1       xsa       415:
                    416:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    417:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    418:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
                    419:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    420:                                rdp->rd_flags |= RCS_RD_SELECT;
                    421:                                nrev++;
                    422:                        }
                    423:        }
1.5       joris     424:        rcs_argv_destroy(revargv);
1.1       xsa       425:
                    426:        if (lnum.rn_id != NULL)
                    427:                xfree(lnum.rn_id);
                    428:        if (rnum.rn_id != NULL)
                    429:                xfree(rnum.rn_id);
                    430:
                    431:        return (nrev);
1.2       ray       432: }
                    433:
                    434: /*
                    435:  * Load description from <in> to <file>.
                    436:  * If <in> starts with a `-', <in> is taken as the description.
                    437:  * Otherwise <in> is the name of the file containing the description.
                    438:  * If <in> is NULL, the description is read from stdin.
1.18      ray       439:  * Returns 0 on success, -1 on failure, setting errno.
1.2       ray       440:  */
1.18      ray       441: int
1.2       ray       442: rcs_set_description(RCSFILE *file, const char *in)
                    443: {
                    444:        BUF *bp;
                    445:        char *content;
                    446:        const char *prompt =
                    447:            "enter description, terminated with single '.' or end of file:\n"
                    448:            "NOTE: This is NOT the log message!\n";
                    449:
                    450:        /* Description is in file <in>. */
                    451:        if (in != NULL && *in != '-') {
1.18      ray       452:                if ((bp = rcs_buf_load(in, BUF_AUTOEXT)) == NULL)
                    453:                        return (-1);
1.5       joris     454:                rcs_buf_putc(bp, '\0');
                    455:                content = rcs_buf_release(bp);
1.2       ray       456:        /* Description is in <in>. */
                    457:        } else if (in != NULL)
                    458:                /* Skip leading `-'. */
                    459:                content = xstrdup(in + 1);
                    460:        /* Get description from stdin. */
                    461:        else
                    462:                content = rcs_prompt(prompt);
                    463:
                    464:        rcs_desc_set(file, content);
                    465:        xfree(content);
1.18      ray       466:        return (0);
1.7       xsa       467: }
                    468:
                    469: /*
                    470:  * Split the contents of a file into a list of lines.
                    471:  */
                    472: struct rcs_lines *
1.27      xsa       473: rcs_splitlines(u_char *data, size_t len)
1.7       xsa       474: {
1.15      niallo    475:        u_char *c, *p;
1.7       xsa       476:        struct rcs_lines *lines;
                    477:        struct rcs_line *lp;
1.24      niallo    478:        size_t i, tlen;
1.7       xsa       479:
                    480:        lines = xmalloc(sizeof(*lines));
1.24      niallo    481:        memset(lines, 0, sizeof(*lines));
1.7       xsa       482:        TAILQ_INIT(&(lines->l_lines));
                    483:
                    484:        lp = xmalloc(sizeof(*lp));
1.24      niallo    485:        memset(lp, 0, sizeof(*lp));
1.7       xsa       486:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    487:
                    488:
1.24      niallo    489:        p = c = data;
                    490:        for (i = 0; i < len; i++) {
                    491:                if (*p == '\n' || (i == len - 1)) {
1.26      niallo    492:                        tlen = p - c + 1;
1.15      niallo    493:                        lp = xmalloc(sizeof(*lp));
1.24      niallo    494:                        lp->l_line = c;
                    495:                        lp->l_len = tlen;
1.15      niallo    496:                        lp->l_lineno = ++(lines->l_nblines);
                    497:                        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    498:                        c = p + 1;
                    499:                }
                    500:                p++;
1.7       xsa       501:        }
                    502:
                    503:        return (lines);
                    504: }
                    505:
                    506: void
                    507: rcs_freelines(struct rcs_lines *lines)
                    508: {
                    509:        struct rcs_line *lp;
                    510:
                    511:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    512:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
                    513:                xfree(lp);
                    514:        }
                    515:
                    516:        xfree(lines);
                    517: }
                    518:
                    519: BUF *
1.27      xsa       520: rcs_patchfile(u_char *data, size_t dlen, u_char *patch, size_t plen,
1.7       xsa       521:     int (*p)(struct rcs_lines *, struct rcs_lines *))
                    522: {
                    523:        struct rcs_lines *dlines, *plines;
                    524:        struct rcs_line *lp;
                    525:        BUF *res;
                    526:
1.24      niallo    527:        dlines = rcs_splitlines(data, dlen);
                    528:        plines = rcs_splitlines(patch, plen);
1.7       xsa       529:
                    530:        if (p(dlines, plines) < 0) {
                    531:                rcs_freelines(dlines);
                    532:                rcs_freelines(plines);
                    533:                return (NULL);
                    534:        }
                    535:
1.24      niallo    536:        res = rcs_buf_alloc(1024, BUF_AUTOEXT);
1.7       xsa       537:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.24      niallo    538:                if (lp->l_line == NULL)
                    539:                        continue;
                    540:                rcs_buf_append(res, lp->l_line, lp->l_len);
1.7       xsa       541:        }
                    542:
                    543:        rcs_freelines(dlines);
                    544:        rcs_freelines(plines);
                    545:        return (res);
                    546: }
                    547:
                    548: /*
                    549:  * rcs_yesno()
                    550:  *
1.23      millert   551:  * Read a char from standard input, returns defc if the
                    552:  * user enters an equivalent to defc, else whatever char
                    553:  * was entered.  Converts input to lower case.
1.7       xsa       554:  */
                    555: int
1.23      millert   556: rcs_yesno(int defc)
1.7       xsa       557: {
                    558:        int c, ret;
                    559:
                    560:        fflush(stderr);
                    561:        fflush(stdout);
                    562:
1.25      ray       563:        clearerr(stdin);
1.23      millert   564:        if (isalpha(c = getchar()))
                    565:                c = tolower(c);
                    566:        if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
                    567:                ret = defc;
1.7       xsa       568:        else
1.23      millert   569:                ret = c;
                    570:
                    571:        while (c != EOF && c != '\n')
                    572:                c = getchar();
1.7       xsa       573:
                    574:        return (ret);
                    575: }
                    576:
                    577: /*
                    578:  * rcs_strsplit()
                    579:  *
                    580:  * Split a string <str> of <sep>-separated values and allocate
                    581:  * an argument vector for the values found.
                    582:  */
                    583: struct rcs_argvector *
1.14      ray       584: rcs_strsplit(const char *str, const char *sep)
1.7       xsa       585: {
                    586:        struct rcs_argvector *av;
                    587:        size_t i = 0;
                    588:        char *cp, *p;
                    589:
                    590:        cp = xstrdup(str);
                    591:        av = xmalloc(sizeof(*av));
                    592:        av->str = cp;
1.31      ray       593:        av->argv = xmalloc(sizeof(*(av->argv)));
1.7       xsa       594:
                    595:        while ((p = strsep(&cp, sep)) != NULL) {
                    596:                av->argv[i++] = p;
1.30      ray       597:                av->argv = xrealloc(av->argv,
1.7       xsa       598:                    i + 1, sizeof(*(av->argv)));
                    599:        }
                    600:        av->argv[i] = NULL;
                    601:
                    602:        return (av);
                    603: }
                    604:
                    605: /*
                    606:  * rcs_argv_destroy()
                    607:  *
                    608:  * Free an argument vector previously allocated by rcs_strsplit().
                    609:  */
                    610: void
                    611: rcs_argv_destroy(struct rcs_argvector *av)
                    612: {
                    613:        xfree(av->str);
                    614:        xfree(av->argv);
                    615:        xfree(av);
1.28      otto      616: }
                    617:
                    618: /*
                    619:  * Strip suffix from filename.
                    620:  */
                    621: void
                    622: rcs_strip_suffix(char *filename)
                    623: {
                    624:        char *p, *suffixes, *next, *ext;
                    625:
                    626:        if ((p = strrchr(filename, ',')) != NULL) {
                    627:                suffixes = xstrdup(rcs_suffixes);
                    628:                for (next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
                    629:                        if (!strcmp(p, ext)) {
                    630:                                *p = '\0';
                    631:                                break;
                    632:                        }
                    633:                }
                    634:                xfree(suffixes);
                    635:        }
1.1       xsa       636: }