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

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