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

1.25    ! ray         1: /*     $OpenBSD: rcsutil.c,v 1.24 2007/01/02 16:43:45 niallo 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, ">> ");
1.25    ! ray       346:        clearerr(stdin);
1.1       xsa       347:        while ((buf = fgetln(stdin, &len)) != NULL) {
                    348:                /* The last line may not be EOL terminated. */
                    349:                if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
                    350:                        break;
                    351:                else
1.5       joris     352:                        rcs_buf_append(bp, buf, len);
1.1       xsa       353:
                    354:                if (isatty(STDIN_FILENO))
                    355:                        (void)fprintf(stderr, ">> ");
                    356:        }
1.5       joris     357:        rcs_buf_putc(bp, '\0');
1.1       xsa       358:
1.5       joris     359:        return (rcs_buf_release(bp));
1.1       xsa       360: }
                    361:
                    362: u_int
1.14      ray       363: rcs_rev_select(RCSFILE *file, const char *range)
1.1       xsa       364: {
                    365:        int i;
                    366:        u_int nrev;
                    367:        char *ep;
                    368:        char *lstr, *rstr;
                    369:        struct rcs_delta *rdp;
1.5       joris     370:        struct rcs_argvector *revargv, *revrange;
1.1       xsa       371:        RCSNUM lnum, rnum;
                    372:
                    373:        nrev = 0;
                    374:        (void)memset(&lnum, 0, sizeof(lnum));
                    375:        (void)memset(&rnum, 0, sizeof(rnum));
                    376:
                    377:        if (range == NULL) {
                    378:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    379:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    380:                                rdp->rd_flags |= RCS_RD_SELECT;
                    381:                                return (1);
                    382:                        }
                    383:                return (0);
                    384:        }
                    385:
1.5       joris     386:        revargv = rcs_strsplit(range, ",");
1.1       xsa       387:        for (i = 0; revargv->argv[i] != NULL; i++) {
1.5       joris     388:                revrange = rcs_strsplit(revargv->argv[i], ":");
1.1       xsa       389:                if (revrange->argv[0] == NULL)
                    390:                        /* should not happen */
1.3       xsa       391:                        errx(1, "invalid revision range: %s", revargv->argv[i]);
1.1       xsa       392:                else if (revrange->argv[1] == NULL)
                    393:                        lstr = rstr = revrange->argv[0];
                    394:                else {
                    395:                        if (revrange->argv[2] != NULL)
1.3       xsa       396:                                errx(1, "invalid revision range: %s",
                    397:                                    revargv->argv[i]);
1.1       xsa       398:                        lstr = revrange->argv[0];
                    399:                        rstr = revrange->argv[1];
                    400:                        if (strcmp(lstr, "") == 0)
                    401:                                lstr = NULL;
                    402:                        if (strcmp(rstr, "") == 0)
                    403:                                rstr = NULL;
                    404:                }
                    405:
                    406:                if (lstr == NULL)
                    407:                        lstr = RCS_HEAD_INIT;
                    408:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
1.3       xsa       409:                        errx(1, "invalid revision: %s", lstr);
1.1       xsa       410:
                    411:                if (rstr != NULL) {
                    412:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
1.3       xsa       413:                                errx(1, "invalid revision: %s", rstr);
1.1       xsa       414:                } else
                    415:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    416:
1.5       joris     417:                rcs_argv_destroy(revrange);
1.1       xsa       418:
                    419:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    420:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    421:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
                    422:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    423:                                rdp->rd_flags |= RCS_RD_SELECT;
                    424:                                nrev++;
                    425:                        }
                    426:        }
1.5       joris     427:        rcs_argv_destroy(revargv);
1.1       xsa       428:
                    429:        if (lnum.rn_id != NULL)
                    430:                xfree(lnum.rn_id);
                    431:        if (rnum.rn_id != NULL)
                    432:                xfree(rnum.rn_id);
                    433:
                    434:        return (nrev);
1.2       ray       435: }
                    436:
                    437: /*
                    438:  * Load description from <in> to <file>.
                    439:  * If <in> starts with a `-', <in> is taken as the description.
                    440:  * Otherwise <in> is the name of the file containing the description.
                    441:  * If <in> is NULL, the description is read from stdin.
1.18      ray       442:  * Returns 0 on success, -1 on failure, setting errno.
1.2       ray       443:  */
1.18      ray       444: int
1.2       ray       445: rcs_set_description(RCSFILE *file, const char *in)
                    446: {
                    447:        BUF *bp;
                    448:        char *content;
                    449:        const char *prompt =
                    450:            "enter description, terminated with single '.' or end of file:\n"
                    451:            "NOTE: This is NOT the log message!\n";
                    452:
                    453:        /* Description is in file <in>. */
                    454:        if (in != NULL && *in != '-') {
1.18      ray       455:                if ((bp = rcs_buf_load(in, BUF_AUTOEXT)) == NULL)
                    456:                        return (-1);
1.5       joris     457:                rcs_buf_putc(bp, '\0');
                    458:                content = rcs_buf_release(bp);
1.2       ray       459:        /* Description is in <in>. */
                    460:        } else if (in != NULL)
                    461:                /* Skip leading `-'. */
                    462:                content = xstrdup(in + 1);
                    463:        /* Get description from stdin. */
                    464:        else
                    465:                content = rcs_prompt(prompt);
                    466:
                    467:        rcs_desc_set(file, content);
                    468:        xfree(content);
1.18      ray       469:        return (0);
1.7       xsa       470: }
                    471:
                    472: /*
                    473:  * Split the contents of a file into a list of lines.
                    474:  */
                    475: struct rcs_lines *
1.24      niallo    476: rcs_splitlines(const u_char *data, size_t len)
1.7       xsa       477: {
1.15      niallo    478:        u_char *c, *p;
1.7       xsa       479:        struct rcs_lines *lines;
                    480:        struct rcs_line *lp;
1.24      niallo    481:        size_t i, tlen;
1.7       xsa       482:
                    483:        lines = xmalloc(sizeof(*lines));
1.24      niallo    484:        memset(lines, 0, sizeof(*lines));
1.7       xsa       485:        TAILQ_INIT(&(lines->l_lines));
                    486:
                    487:        lp = xmalloc(sizeof(*lp));
1.24      niallo    488:        memset(lp, 0, sizeof(*lp));
1.7       xsa       489:        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    490:
                    491:
1.24      niallo    492:        p = c = data;
                    493:        for (i = 0; i < len; i++) {
                    494:                if (*p == '\n' || (i == len - 1)) {
                    495:                        tlen = p - c;
                    496:                        if (*p == '\n')
                    497:                                tlen++;
1.15      niallo    498:                        lp = xmalloc(sizeof(*lp));
1.24      niallo    499:                        lp->l_line = c;
                    500:                        lp->l_len = tlen;
1.15      niallo    501:                        lp->l_lineno = ++(lines->l_nblines);
                    502:                        TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
                    503:                        c = p + 1;
                    504:                }
                    505:                p++;
1.7       xsa       506:        }
                    507:
                    508:        return (lines);
                    509: }
                    510:
                    511: void
                    512: rcs_freelines(struct rcs_lines *lines)
                    513: {
                    514:        struct rcs_line *lp;
                    515:
                    516:        while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
                    517:                TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
                    518:                xfree(lp);
                    519:        }
                    520:
                    521:        xfree(lines);
                    522: }
                    523:
                    524: BUF *
1.24      niallo    525: rcs_patchfile(const u_char *data, size_t dlen, const u_char *patch, size_t plen,
1.7       xsa       526:     int (*p)(struct rcs_lines *, struct rcs_lines *))
                    527: {
                    528:        struct rcs_lines *dlines, *plines;
                    529:        struct rcs_line *lp;
                    530:        BUF *res;
                    531:
1.24      niallo    532:        dlines = rcs_splitlines(data, dlen);
                    533:        plines = rcs_splitlines(patch, plen);
1.7       xsa       534:
                    535:        if (p(dlines, plines) < 0) {
                    536:                rcs_freelines(dlines);
                    537:                rcs_freelines(plines);
                    538:                return (NULL);
                    539:        }
                    540:
1.24      niallo    541:        res = rcs_buf_alloc(1024, BUF_AUTOEXT);
1.7       xsa       542:        TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
1.24      niallo    543:                if (lp->l_line == NULL)
                    544:                        continue;
                    545:                rcs_buf_append(res, lp->l_line, lp->l_len);
1.7       xsa       546:        }
                    547:
                    548:        rcs_freelines(dlines);
                    549:        rcs_freelines(plines);
                    550:        return (res);
                    551: }
                    552:
                    553: /*
                    554:  * rcs_yesno()
                    555:  *
1.23      millert   556:  * Read a char from standard input, returns defc if the
                    557:  * user enters an equivalent to defc, else whatever char
                    558:  * was entered.  Converts input to lower case.
1.7       xsa       559:  */
                    560: int
1.23      millert   561: rcs_yesno(int defc)
1.7       xsa       562: {
                    563:        int c, ret;
                    564:
                    565:        fflush(stderr);
                    566:        fflush(stdout);
                    567:
1.25    ! ray       568:        clearerr(stdin);
1.23      millert   569:        if (isalpha(c = getchar()))
                    570:                c = tolower(c);
                    571:        if (c == defc || c == '\n' || (c == EOF && feof(stdin)))
                    572:                ret = defc;
1.7       xsa       573:        else
1.23      millert   574:                ret = c;
                    575:
                    576:        while (c != EOF && c != '\n')
                    577:                c = getchar();
1.7       xsa       578:
                    579:        return (ret);
                    580: }
                    581:
                    582: /*
                    583:  * rcs_strsplit()
                    584:  *
                    585:  * Split a string <str> of <sep>-separated values and allocate
                    586:  * an argument vector for the values found.
                    587:  */
                    588: struct rcs_argvector *
1.14      ray       589: rcs_strsplit(const char *str, const char *sep)
1.7       xsa       590: {
                    591:        struct rcs_argvector *av;
                    592:        size_t i = 0;
                    593:        char **nargv;
                    594:        char *cp, *p;
                    595:
                    596:        cp = xstrdup(str);
                    597:        av = xmalloc(sizeof(*av));
                    598:        av->str = cp;
                    599:        av->argv = xcalloc(i + 1, sizeof(*(av->argv)));
                    600:
                    601:        while ((p = strsep(&cp, sep)) != NULL) {
                    602:                av->argv[i++] = p;
                    603:                nargv = xrealloc(av->argv,
                    604:                    i + 1, sizeof(*(av->argv)));
                    605:                av->argv = nargv;
                    606:        }
                    607:        av->argv[i] = NULL;
                    608:
                    609:        return (av);
                    610: }
                    611:
                    612: /*
                    613:  * rcs_argv_destroy()
                    614:  *
                    615:  * Free an argument vector previously allocated by rcs_strsplit().
                    616:  */
                    617: void
                    618: rcs_argv_destroy(struct rcs_argvector *av)
                    619: {
                    620:        xfree(av->str);
                    621:        xfree(av->argv);
                    622:        xfree(av);
1.1       xsa       623: }