diff options
author | Xavier Santolaria <xsa@cvs.openbsd.org> | 2006-04-27 07:59:34 +0000 |
---|---|---|
committer | Xavier Santolaria <xsa@cvs.openbsd.org> | 2006-04-27 07:59:34 +0000 |
commit | d194ea38c556fd7a9e0f7b252a725e2f625f86c4 (patch) | |
tree | 7e5fc93c795be7fd2bac8525d91a5aab8aeebf0d /usr.bin | |
parent | a68878a8f1fbd3499517fb209bc575f9dea579f1 (diff) |
zap util.[ch] and move the content into rcsutil.[ch].
discussed with joris@.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/rcs/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/rcs/diff3.c | 6 | ||||
-rw-r--r-- | usr.bin/rcs/rcs.c | 4 | ||||
-rw-r--r-- | usr.bin/rcs/rcsprog.h | 3 | ||||
-rw-r--r-- | usr.bin/rcs/rcsutil.c | 156 | ||||
-rw-r--r-- | usr.bin/rcs/rcsutil.h | 52 | ||||
-rw-r--r-- | usr.bin/rcs/util.c | 187 | ||||
-rw-r--r-- | usr.bin/rcs/util.h | 58 |
8 files changed, 202 insertions, 268 deletions
diff --git a/usr.bin/rcs/Makefile b/usr.bin/rcs/Makefile index 16bf1423b3a..76df15f7071 100644 --- a/usr.bin/rcs/Makefile +++ b/usr.bin/rcs/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.33 2006/04/26 07:37:59 xsa Exp $ +# $OpenBSD: Makefile,v 1.34 2006/04/27 07:59:33 xsa Exp $ .PATH: ${.CURDIR}/.. @@ -7,7 +7,7 @@ MAN= ci.1 co.1 ident.1 rcs.1 rcsclean.1 rcsdiff.1 rcsmerge.1 rlog.1 SRCS= ci.c co.c ident.c rcsclean.c rcsdiff.c rcsmerge.c rcsprog.c rlog.c \ rcsutil.c buf.c date.y diff.c diff3.c rcs.c rcsnum.c \ - rcstime.c util.c worklist.c xmalloc.c + rcstime.c worklist.c xmalloc.c LINKS= ${BINDIR}/rcs ${BINDIR}/ci ${BINDIR}/rcs ${BINDIR}/co \ ${BINDIR}/rcs ${BINDIR}/rcsclean ${BINDIR}/rcs ${BINDIR}/rcsdiff \ diff --git a/usr.bin/rcs/diff3.c b/usr.bin/rcs/diff3.c index 687759ea4a9..bd04b9efff9 100644 --- a/usr.bin/rcs/diff3.c +++ b/usr.bin/rcs/diff3.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff3.c,v 1.1 2006/04/26 02:55:13 joris Exp $ */ +/* $OpenBSD: diff3.c,v 1.2 2006/04/27 07:59:33 xsa Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -72,13 +72,13 @@ static const char copyright[] = #ifndef lint static const char rcsid[] = - "$OpenBSD: diff3.c,v 1.1 2006/04/26 02:55:13 joris Exp $"; + "$OpenBSD: diff3.c,v 1.2 2006/04/27 07:59:33 xsa Exp $"; #endif /* not lint */ #include "includes.h" #include "diff.h" -#include "util.h" +#include "rcsutil.h" #include "xmalloc.h" /* diff3 - 3-way differential file comparison */ diff --git a/usr.bin/rcs/rcs.c b/usr.bin/rcs/rcs.c index 28c98d197ed..b1f3f5de11b 100644 --- a/usr.bin/rcs/rcs.c +++ b/usr.bin/rcs/rcs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcs.c,v 1.3 2006/04/26 21:55:22 joris Exp $ */ +/* $OpenBSD: rcs.c,v 1.4 2006/04/27 07:59:33 xsa Exp $ */ /* * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org> * All rights reserved. @@ -27,9 +27,9 @@ #include "includes.h" #include "diff.h" -#include "util.h" #include "rcs.h" #include "rcsprog.h" +#include "rcsutil.h" #include "xmalloc.h" #define RCS_BUFSIZE 16384 diff --git a/usr.bin/rcs/rcsprog.h b/usr.bin/rcs/rcsprog.h index 2f8fd5d3234..9d00e5c1f7a 100644 --- a/usr.bin/rcs/rcsprog.h +++ b/usr.bin/rcs/rcsprog.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsprog.h,v 1.56 2006/04/26 07:47:49 xsa Exp $ */ +/* $OpenBSD: rcsprog.h,v 1.57 2006/04/27 07:59:33 xsa Exp $ */ /* * Copyright (c) 2005 Joris Vink <joris@openbsd.org> * All rights reserved. @@ -30,7 +30,6 @@ #include "rcs.h" #include "rcsutil.h" #include "worklist.h" -#include "util.h" #include "xmalloc.h" #define RCS_DEFAULT_SUFFIX ",v/" diff --git a/usr.bin/rcs/rcsutil.c b/usr.bin/rcs/rcsutil.c index 8c732a0e944..31119174e33 100644 --- a/usr.bin/rcs/rcsutil.c +++ b/usr.bin/rcs/rcsutil.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsutil.c,v 1.6 2006/04/26 21:55:22 joris Exp $ */ +/* $OpenBSD: rcsutil.c,v 1.7 2006/04/27 07:59:33 xsa Exp $ */ /* * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org> * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org> @@ -486,3 +486,157 @@ rcs_set_description(RCSFILE *file, const char *in) rcs_desc_set(file, content); xfree(content); } + +/* + * Split the contents of a file into a list of lines. + */ +struct rcs_lines * +rcs_splitlines(const char *fcont) +{ + char *dcp; + struct rcs_lines *lines; + struct rcs_line *lp; + + lines = xmalloc(sizeof(*lines)); + TAILQ_INIT(&(lines->l_lines)); + lines->l_nblines = 0; + lines->l_data = xstrdup(fcont); + + lp = xmalloc(sizeof(*lp)); + lp->l_line = NULL; + lp->l_lineno = 0; + TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list); + + for (dcp = lines->l_data; *dcp != '\0';) { + lp = xmalloc(sizeof(*lp)); + lp->l_line = dcp; + lp->l_lineno = ++(lines->l_nblines); + TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list); + + dcp = strchr(dcp, '\n'); + if (dcp == NULL) + break; + *(dcp++) = '\0'; + } + + return (lines); +} + +void +rcs_freelines(struct rcs_lines *lines) +{ + struct rcs_line *lp; + + while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) { + TAILQ_REMOVE(&(lines->l_lines), lp, l_list); + xfree(lp); + } + + xfree(lines->l_data); + xfree(lines); +} + +BUF * +rcs_patchfile(const char *data, const char *patch, + int (*p)(struct rcs_lines *, struct rcs_lines *)) +{ + struct rcs_lines *dlines, *plines; + struct rcs_line *lp; + size_t len; + int lineno; + BUF *res; + + len = strlen(data); + + if ((dlines = rcs_splitlines(data)) == NULL) + return (NULL); + + if ((plines = rcs_splitlines(patch)) == NULL) + return (NULL); + + if (p(dlines, plines) < 0) { + rcs_freelines(dlines); + rcs_freelines(plines); + return (NULL); + } + + lineno = 0; + res = rcs_buf_alloc(len, BUF_AUTOEXT); + TAILQ_FOREACH(lp, &dlines->l_lines, l_list) { + if (lineno != 0) + rcs_buf_fappend(res, "%s\n", lp->l_line); + lineno++; + } + + rcs_freelines(dlines); + rcs_freelines(plines); + return (res); +} + +/* + * rcs_yesno() + * + * Read from standart input for `y' or `Y' character. + * Returns 0 on success, or -1 on failure. + */ +int +rcs_yesno(void) +{ + int c, ret; + + ret = 0; + + fflush(stderr); + fflush(stdout); + + if ((c = getchar()) != 'y' && c != 'Y') + ret = -1; + else + while (c != EOF && c != '\n') + c = getchar(); + + return (ret); +} + +/* + * rcs_strsplit() + * + * Split a string <str> of <sep>-separated values and allocate + * an argument vector for the values found. + */ +struct rcs_argvector * +rcs_strsplit(char *str, const char *sep) +{ + struct rcs_argvector *av; + size_t i = 0; + char **nargv; + char *cp, *p; + + cp = xstrdup(str); + av = xmalloc(sizeof(*av)); + av->str = cp; + av->argv = xcalloc(i + 1, sizeof(*(av->argv))); + + while ((p = strsep(&cp, sep)) != NULL) { + av->argv[i++] = p; + nargv = xrealloc(av->argv, + i + 1, sizeof(*(av->argv))); + av->argv = nargv; + } + av->argv[i] = NULL; + + return (av); +} + +/* + * rcs_argv_destroy() + * + * Free an argument vector previously allocated by rcs_strsplit(). + */ +void +rcs_argv_destroy(struct rcs_argvector *av) +{ + xfree(av->str); + xfree(av->argv); + xfree(av); +} diff --git a/usr.bin/rcs/rcsutil.h b/usr.bin/rcs/rcsutil.h index 68dc5d655eb..977e370431a 100644 --- a/usr.bin/rcs/rcsutil.h +++ b/usr.bin/rcs/rcsutil.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsutil.h,v 1.3 2006/04/26 21:55:22 joris Exp $ */ +/* $OpenBSD: rcsutil.h,v 1.4 2006/04/27 07:59:33 xsa Exp $ */ /* * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org> * All rights reserved. @@ -29,18 +29,44 @@ #include "rcs.h" +struct rcs_line { + char *l_line; + int l_lineno; + TAILQ_ENTRY(rcs_line) l_list; +}; + +TAILQ_HEAD(rcs_tqh, rcs_line); + +struct rcs_lines { + int l_nblines; + char *l_data; + struct rcs_tqh l_lines; +}; + +struct rcs_argvector { + char *str; + char **argv; +}; + /* rcsutil.c */ -int rcs_getopt(int, char **, const char *); -void rcs_set_mtime(RCSFILE *, time_t); -int rcs_choosefile(const char *, char *, size_t); -int rcs_statfile(char *, char *, size_t, int); -time_t rcs_get_mtime(RCSFILE *); -RCSNUM *rcs_getrevnum(const char *, RCSFILE *); -char *rcs_prompt(const char *); -u_int rcs_rev_select(RCSFILE *, char *); -void rcs_set_description(RCSFILE *, const char *); -void rcs_set_rev(const char *, RCSNUM **); -void rcs_setrevstr(char **, char *); -void rcs_setrevstr2(char **, char **, char *); +int rcs_getopt(int, char **, const char *); +void rcs_set_mtime(RCSFILE *, time_t); +int rcs_choosefile(const char *, char *, size_t); +int rcs_statfile(char *, char *, size_t, int); +time_t rcs_get_mtime(RCSFILE *); +RCSNUM *rcs_getrevnum(const char *, RCSFILE *); +char *rcs_prompt(const char *); +u_int rcs_rev_select(RCSFILE *, char *); +void rcs_set_description(RCSFILE *, const char *); +void rcs_set_rev(const char *, RCSNUM **); +void rcs_setrevstr(char **, char *); +void rcs_setrevstr2(char **, char **, char *); +BUF *rcs_patchfile(const char *, const char *, + int (*p)(struct rcs_lines *, struct rcs_lines *)); +struct rcs_lines *rcs_splitlines(const char *); +void rcs_freelines(struct rcs_lines *); +int rcs_yesno(void); +struct rcs_argvector *rcs_strsplit(char *, const char *); +void rcs_argv_destroy(struct rcs_argvector *); #endif /* RCSUTIL_H */ diff --git a/usr.bin/rcs/util.c b/usr.bin/rcs/util.c deleted file mode 100644 index fbbef666b5f..00000000000 --- a/usr.bin/rcs/util.c +++ /dev/null @@ -1,187 +0,0 @@ -/* $OpenBSD: util.c,v 1.1 2006/04/26 02:55:13 joris Exp $ */ -/* - * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org> - * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org> - * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "includes.h" - -#include "buf.h" -#include "util.h" -#include "xmalloc.h" - -/* - * Split the contents of a file into a list of lines. - */ -struct rcs_lines * -rcs_splitlines(const char *fcont) -{ - char *dcp; - struct rcs_lines *lines; - struct rcs_line *lp; - - lines = xmalloc(sizeof(*lines)); - TAILQ_INIT(&(lines->l_lines)); - lines->l_nblines = 0; - lines->l_data = xstrdup(fcont); - - lp = xmalloc(sizeof(*lp)); - lp->l_line = NULL; - lp->l_lineno = 0; - TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list); - - for (dcp = lines->l_data; *dcp != '\0';) { - lp = xmalloc(sizeof(*lp)); - lp->l_line = dcp; - lp->l_lineno = ++(lines->l_nblines); - TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list); - - dcp = strchr(dcp, '\n'); - if (dcp == NULL) - break; - *(dcp++) = '\0'; - } - - return (lines); -} - -void -rcs_freelines(struct rcs_lines *lines) -{ - struct rcs_line *lp; - - while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) { - TAILQ_REMOVE(&(lines->l_lines), lp, l_list); - xfree(lp); - } - - xfree(lines->l_data); - xfree(lines); -} - -BUF * -rcs_patchfile(const char *data, const char *patch, - int (*p)(struct rcs_lines *, struct rcs_lines *)) -{ - struct rcs_lines *dlines, *plines; - struct rcs_line *lp; - size_t len; - int lineno; - BUF *res; - - len = strlen(data); - - if ((dlines = rcs_splitlines(data)) == NULL) - return (NULL); - - if ((plines = rcs_splitlines(patch)) == NULL) - return (NULL); - - if (p(dlines, plines) < 0) { - rcs_freelines(dlines); - rcs_freelines(plines); - return (NULL); - } - - lineno = 0; - res = rcs_buf_alloc(len, BUF_AUTOEXT); - TAILQ_FOREACH(lp, &dlines->l_lines, l_list) { - if (lineno != 0) - rcs_buf_fappend(res, "%s\n", lp->l_line); - lineno++; - } - - rcs_freelines(dlines); - rcs_freelines(plines); - return (res); -} - -/* - * rcs_yesno() - * - * Read from standart input for `y' or `Y' character. - * Returns 0 on success, or -1 on failure. - */ -int -rcs_yesno(void) -{ - int c, ret; - - ret = 0; - - fflush(stderr); - fflush(stdout); - - if ((c = getchar()) != 'y' && c != 'Y') - ret = -1; - else - while (c != EOF && c != '\n') - c = getchar(); - - return (ret); -} - -/* - * rcs_strsplit() - * - * Split a string <str> of <sep>-separated values and allocate - * an argument vector for the values found. - */ -struct rcs_argvector * -rcs_strsplit(char *str, const char *sep) -{ - struct rcs_argvector *av; - size_t i = 0; - char **nargv; - char *cp, *p; - - cp = xstrdup(str); - av = xmalloc(sizeof(*av)); - av->str = cp; - av->argv = xcalloc(i + 1, sizeof(*(av->argv))); - - while ((p = strsep(&cp, sep)) != NULL) { - av->argv[i++] = p; - nargv = xrealloc(av->argv, - i + 1, sizeof(*(av->argv))); - av->argv = nargv; - } - av->argv[i] = NULL; - - return (av); -} - -/* - * rcs_argv_destroy() - * - * Free an argument vector previously allocated by rcs_strsplit(). - */ -void -rcs_argv_destroy(struct rcs_argvector *av) -{ - xfree(av->str); - xfree(av->argv); - xfree(av); -} diff --git a/usr.bin/rcs/util.h b/usr.bin/rcs/util.h deleted file mode 100644 index 3439c602a98..00000000000 --- a/usr.bin/rcs/util.h +++ /dev/null @@ -1,58 +0,0 @@ -/* $OpenBSD: util.h,v 1.1 2006/04/26 02:55:13 joris Exp $ */ -/* - * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef UTIL_H -#define UTIL_H - -struct rcs_line { - char *l_line; - int l_lineno; - TAILQ_ENTRY(rcs_line) l_list; -}; - -TAILQ_HEAD(rcs_tqh, rcs_line); - -struct rcs_lines { - int l_nblines; - char *l_data; - struct rcs_tqh l_lines; -}; - -struct rcs_argvector { - char *str; - char **argv; -}; - -BUF *rcs_patchfile(const char *, const char *, - int (*p)(struct rcs_lines *, struct rcs_lines *)); -struct rcs_lines *rcs_splitlines(const char *); -void rcs_freelines(struct rcs_lines *); -int rcs_yesno(void); -struct rcs_argvector *rcs_strsplit(char *, const char *); - -void rcs_argv_destroy(struct rcs_argvector *); - -#endif /* UTIL_H */ |