diff options
author | 2014-07-09 19:04:03 +0000 | |
---|---|---|
committer | 2014-07-09 19:04:03 +0000 | |
commit | 6169bbdff3016567dd748c85a8359ebeccdd4b64 (patch) | |
tree | acdc76601ca4ebd8a1af42f262a260bf5882b4d8 | |
parent | "mkstr was intended for the limited architecture of the PDP 11 family." (diff) | |
download | wireguard-openbsd-6169bbdff3016567dd748c85a8359ebeccdd4b64.tar.xz wireguard-openbsd-6169bbdff3016567dd748c85a8359ebeccdd4b64.zip |
Do you know what horrors lie beyond that wall?
No.
Then you go first.
-rw-r--r-- | usr.bin/mkstr/Makefile | 5 | ||||
-rw-r--r-- | usr.bin/mkstr/mkstr.1 | 146 | ||||
-rw-r--r-- | usr.bin/mkstr/mkstr.c | 319 | ||||
-rw-r--r-- | usr.bin/xstr/Makefile | 5 | ||||
-rw-r--r-- | usr.bin/xstr/pathnames.h | 35 | ||||
-rw-r--r-- | usr.bin/xstr/xstr.1 | 164 | ||||
-rw-r--r-- | usr.bin/xstr/xstr.c | 493 |
7 files changed, 0 insertions, 1167 deletions
diff --git a/usr.bin/mkstr/Makefile b/usr.bin/mkstr/Makefile deleted file mode 100644 index 9145f04492e..00000000000 --- a/usr.bin/mkstr/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -# $OpenBSD: Makefile,v 1.3 1997/09/21 11:50:06 deraadt Exp $ - -PROG= mkstr - -.include <bsd.prog.mk> diff --git a/usr.bin/mkstr/mkstr.1 b/usr.bin/mkstr/mkstr.1 deleted file mode 100644 index ce431dfc816..00000000000 --- a/usr.bin/mkstr/mkstr.1 +++ /dev/null @@ -1,146 +0,0 @@ -.\" $OpenBSD: mkstr.1,v 1.13 2014/07/09 13:32:26 tedu Exp $ -.\" $NetBSD: mkstr.1,v 1.3 1995/09/28 06:22:19 tls Exp $ -.\" -.\" Copyright (c) 1980, 1990, 1993 -.\" The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. -.\" -.\" @(#)mkstr.1 8.1 (Berkeley) 6/6/93 -.\" -.Dd $Mdocdate: July 9 2014 $ -.Dt MKSTR 1 -.Os -.Sh NAME -.Nm mkstr -.Nd create an error message file by massaging C source -.Sh SYNOPSIS -.Nm mkstr -.Op Fl -.Ar messagefile -.Ar prefix -.Ar -.Sh DESCRIPTION -.Nm mkstr -creates files containing error messages extracted from C source, -and restructures the same C source, to utilize the created error message -file. -The intent of -.Nm mkstr -was to reduce the size of large programs and -reduce swapping (see -.Sx BUGS -section below). -.Pp -.Nm mkstr -processes each of the specified -.Ar files , -placing a restructured version of the input in a file whose name -consists of the specified -.Ar prefix -and the original name. -A typical usage of -.Nm mkstr -is -.Bd -literal -offset indent -$ mkstr pistrings xx *.c -.Ed -.Pp -This command causes all the error messages from the C source -files in the current directory to be placed in the file -.Dq pistrings -and restructured copies of the sources to be placed in -files whose names are prefixed with -.Dq \&xx . -.Pp -The options are as follows: -.Bl -tag -width Ds -.It Fl -Error messages are placed at the end of the specified -.Ar messagefile -for recompiling part of a large -.Nm mkstr Ns ed -program. -.El -.Pp -.Nm mkstr -finds error messages in the source by -searching for the string -.Li \&`error("' -in the input stream. -Each time it occurs, the C string starting at the -.Sq \&"\& -is stored -in the message file followed by a null character and a newline character. -The new source is restructured with -.Xr lseek 2 -pointers into the error message file for retrieval. -.Bd -literal -offset indent -char efilname = "/usr/lib/pi_strings"; -int efil = -1; -void -error(int a1, int a2, int a3, int a4) -{ - char buf[256]; - - if (efil < 0) { - efil = open(efilname, 0); - if (efil < 0) { -oops: - perror(efilname); - exit(1); - } - } - if (lseek(efil, (long) a1, 0) <= 0 || - read(efil, buf, 256) <= 0) - goto oops; - printf(buf, a2, a3, a4); -} -.Ed -.Sh SEE ALSO -.Xr xstr 1 , -.Xr lseek 2 -.Sh HISTORY -The -.Nm -utility first appeared in -.Bx 1 . -.Sh AUTHORS -.An -nosplit -.An Bill Joy -and -.An Chuck Haley , -1977. -.Sh BUGS -.Nm mkstr -was intended for the limited architecture of the PDP 11 family. -Very few programs actually use it. -The pascal interpreter, -.Xr pi , -and the editor, -.Xr ex 1 , -are two programs that are built this way. -It is not an efficient method; the error messages -should be stored in the program text. diff --git a/usr.bin/mkstr/mkstr.c b/usr.bin/mkstr/mkstr.c deleted file mode 100644 index 70c33e47dab..00000000000 --- a/usr.bin/mkstr/mkstr.c +++ /dev/null @@ -1,319 +0,0 @@ -/* $OpenBSD: mkstr.c,v 1.11 2009/10/27 23:59:40 deraadt Exp $ */ -/* $NetBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls Exp $ */ - -/* - * Copyright (c) 1980, 1993 - * The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 <sys/param.h> -#include <sys/stat.h> - -#include <err.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#define ungetchar(c) ungetc(c, stdin) - -/* - * mkstr - create a string error message file by massaging C source - * - * Bill Joy UCB August 1977 - * - * Modified March 1978 to hash old messages to be able to recompile - * without addding messages to the message file (usually) - * - * Based on an earlier program conceived by Bill Joy and Chuck Haley - * - * Program to create a string error message file - * from a group of C programs. Arguments are the name - * of the file where the strings are to be placed, the - * prefix of the new files where the processed source text - * is to be placed, and the files to be processed. - * - * The program looks for 'error("' in the source stream. - * Whenever it finds this, the following characters from the '"' - * to a '"' are replaced by 'seekpt' where seekpt is a - * pointer into the error message file. - * If the '(' is not immediately followed by a '"' no change occurs. - * - * The optional '-' causes strings to be added at the end of the - * existing error message file for recompilation of single routines. - */ - - -FILE *mesgread, *mesgwrite; -char name[MAXPATHLEN], *np; - -void inithash(void); -void process(void); -int match(char *); -void copystr(void); -int octdigit(char); -unsigned int hashit(char *, char, unsigned int); -int fgetNUL(char *, int, FILE *); -__dead void usage(void); - -int -main(int argc, char **argv) -{ - size_t n; - char addon = 0; - - argc--, argv++; - if (argc > 1 && argv[0][0] == '-') - addon++, argc--, argv++; - if (argc < 3) - usage(); - mesgwrite = fopen(argv[0], addon ? "a" : "w"); - if (mesgwrite == NULL) - perror(argv[0]), exit(1); - mesgread = fopen(argv[0], "r"); - if (mesgread == NULL) - perror(argv[0]), exit(1); - inithash(); - argc--, argv++; - if ((n = strlcpy(name, argv[0], sizeof(name))) >= sizeof(name)) - errx(1, "%s too long", argv[0]); - np = name + n; - argc--, argv++; - do { - if (strlcpy(np, argv[0], sizeof(name) - n) >= - sizeof(name) - n) - errx(1, "%s too long", argv[0]); - if (freopen(name, "w", stdout) == NULL) - perror(name), exit(1); - if (freopen(argv[0], "r", stdin) == NULL) - perror(argv[0]), exit(1); - process(); - argc--, argv++; - } while (argc > 0); - exit (0); -} - -void -process(void) -{ - int c; - - for (;;) { - c = getchar(); - if (c == EOF) - return; - if (c != 'e') { - putchar(c); - continue; - } - if (match("error(")) { - printf("error("); - c = getchar(); - if (c != '"') - putchar(c); - else - copystr(); - } - } -} - -int -match(char *ocp) -{ - char *cp; - int c; - - for (cp = ocp + 1; *cp; cp++) { - c = getchar(); - if (c != *cp) { - while (ocp < cp) - putchar(*ocp++); - ungetchar(c); - return (0); - } - } - return (1); -} - -void -copystr(void) -{ - int c, ch; - char buf[512]; - char *cp = buf; - - for (;;) { - c = getchar(); - if (c == EOF) - break; - switch (c) { - - case '"': - *cp++ = 0; - goto out; - case '\\': - c = getchar(); - switch (c) { - - case 'b': - c = '\b'; - break; - case 't': - c = '\t'; - break; - case 'r': - c = '\r'; - break; - case 'n': - c = '\n'; - break; - case '\n': - continue; - case 'f': - c = '\f'; - break; - case '0': - c = 0; - break; - case '\\': - break; - default: - if (!octdigit(c)) - break; - c -= '0'; - ch = getchar(); - if (!octdigit(ch)) - break; - c <<= 7, c += ch - '0'; - ch = getchar(); - if (!octdigit(ch)) - break; - c <<= 3, c+= ch - '0', ch = -1; - break; - } - } - *cp++ = c; - } -out: - *cp = 0; - printf("%d", hashit(buf, 1, 0)); -} - -int -octdigit(char c) -{ - - return (c >= '0' && c <= '7'); -} - -void -inithash(void) -{ - char buf[512]; - int mesgpt = 0; - - rewind(mesgread); - while (fgetNUL(buf, sizeof buf, mesgread) != 0) { - hashit(buf, 0, mesgpt); - mesgpt += strlen(buf) + 2; - } -} - -#define NBUCKETS 511 - -struct hash { - long hval; - unsigned int hpt; - struct hash *hnext; -} *bucket[NBUCKETS]; - -unsigned int -hashit(char *str, char really, unsigned int fakept) -{ - int i; - struct hash *hp; - char buf[512]; - long hashval = 0; - char *cp; - - if (really) - fflush(mesgwrite); - for (cp = str; *cp;) - hashval = (hashval << 1) + *cp++; - i = hashval % NBUCKETS; - if (i < 0) - i += NBUCKETS; - if (really != 0) - for (hp = bucket[i]; hp != 0; hp = hp->hnext) - if (hp->hval == hashval) { - fseek(mesgread, (long) hp->hpt, SEEK_SET); - fgetNUL(buf, sizeof buf, mesgread); -#ifdef DEBUG - fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf); -#endif - if (strcmp(buf, str) == 0) - break; - } - if (!really || hp == 0) { - if ((hp = (struct hash *) calloc(1, sizeof *hp)) == NULL) - err(1, "calloc"); - hp->hnext = bucket[i]; - hp->hval = hashval; - hp->hpt = really ? ftell(mesgwrite) : fakept; - if (really) { - fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite); - fwrite("\n", sizeof (char), 1, mesgwrite); - } - bucket[i] = hp; - } -#ifdef DEBUG - fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt); -#endif - return (hp->hpt); -} - -int -fgetNUL(char *obuf, int rmdr, FILE *file) -{ - int c; - char *buf = obuf; - - while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF) - *buf++ = c; - *buf++ = 0; - getc(file); - return ((feof(file) || ferror(file)) ? 0 : 1); -} - -__dead void -usage(void) -{ - extern char *__progname; - - fprintf(stderr, "usage: %s [-] messagefile prefix file ...\n", - __progname); - exit(1); -} diff --git a/usr.bin/xstr/Makefile b/usr.bin/xstr/Makefile deleted file mode 100644 index 1a171a85793..00000000000 --- a/usr.bin/xstr/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -# $OpenBSD: Makefile,v 1.3 1997/09/21 11:52:15 deraadt Exp $ - -PROG= xstr - -.include <bsd.prog.mk> diff --git a/usr.bin/xstr/pathnames.h b/usr.bin/xstr/pathnames.h deleted file mode 100644 index 584b5cc64d0..00000000000 --- a/usr.bin/xstr/pathnames.h +++ /dev/null @@ -1,35 +0,0 @@ -/* $OpenBSD: pathnames.h,v 1.5 2003/07/02 00:21:17 avsm Exp $ */ -/* $NetBSD: pathnames.h,v 1.3 1994/11/14 04:56:22 jtc Exp $ */ - -/* - * Copyright (c) 1989, 1993 - * The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. - * - * @(#)pathnames.h 8.1 (Berkeley) 6/9/93 - */ - -#define _PATH_TMPFILE "/tmp/xstrXXXXXXXXXX" diff --git a/usr.bin/xstr/xstr.1 b/usr.bin/xstr/xstr.1 deleted file mode 100644 index 6eebd2cb58a..00000000000 --- a/usr.bin/xstr/xstr.1 +++ /dev/null @@ -1,164 +0,0 @@ -.\" $OpenBSD: xstr.1,v 1.15 2013/08/14 06:32:31 jmc Exp $ -.\" $NetBSD: xstr.1,v 1.4 1994/11/26 09:25:22 jtc Exp $ -.\" -.\" Copyright (c) 1980, 1993 -.\" The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. -.\" -.\" @(#)xstr.1 8.2 (Berkeley) 12/30/93 -.\" -.Dd $Mdocdate: August 14 2013 $ -.Dt XSTR 1 -.Os -.Sh NAME -.Nm xstr -.Nd extract strings from C programs to implement shared strings -.Sh SYNOPSIS -.Nm xstr -.Op Fl cv -.Op Fl l Ar array -.Op Fl -.Op Ar -.Sh DESCRIPTION -.Nm -maintains a file -.Pa strings -into which strings in component parts of a large program are hashed. -These strings are replaced with references to this common area. -This serves to implement shared constant strings, most useful if they -are also read-only. -.Pp -The options are as follows: -.Bl -tag -width "-l arrayXX" -.It Fl -Cause -.Nm -to read from the standard input. -.It Fl c -.Nm -will extract the strings from the C source -.Ar file -or the standard input -.Pq Fl , -replacing -string references by expressions of the form (&xstr[number]) -for some number. -An appropriate declaration of -.Nm -is prepended to the file. -The resulting C text is placed in the file -.Pa x.c , -to then be compiled. -The strings from this file are placed in the -.Pa strings -database if they are not there already. -Repeated strings and strings which are suffixes of existing strings -do not cause changes to the database. -.It Fl l Ar array -Specify the named array in program references to abstracted strings. -The default array name is -.Dq xstr . -.It Fl v -Be verbose. -.El -.Pp -After all components of a large program have been compiled, a file -.Pa xs.c -declaring the common -.Nm -space can be created by a command of the form: -.Pp -.Dl $ xstr -.Pp -The file -.Pa xs.c -should then be compiled and loaded with the rest -of the program. -If possible, the array can be made read-only (shared) saving -space and swap overhead. -.Pp -.Nm -can also be used on a single file. -The following command creates files -.Pa x.c -and -.Pa xs.c -as before, without using or affecting any -.Pa strings -file in the same directory: -.Pp -.Dl $ xstr name -.Pp -It may be useful to run -.Nm -after the C preprocessor if any macro definitions yield strings -or if there is conditional code which contains strings -which may not, in fact, be needed. -An appropriate command sequence for running -.Nm -after the C preprocessor is: -.Bd -literal -offset indent -$ cc -E name.c | xstr -c - -$ cc -c x.c -$ mv x.o name.o -.Ed -.Pp -.Nm -does not touch the file -.Pa strings -unless new items are added, so that -.Xr make 1 -can avoid remaking -.Pa xs.o -unless truly necessary. -.Sh FILES -.Bl -tag -width /tmp/xsxx* -compact -.It Pa strings -database of strings -.It Pa x.c -massaged C source -.It Pa xs.c -C source for definition of array -.Dq xstr -.It Pa /tmp/xs* -temporary file when -.Dq xstr name -doesn't touch -.Pa strings -.El -.Sh SEE ALSO -.Xr mkstr 1 -.Sh HISTORY -The -.Nm -command appeared in -.Bx 3.0 . -.Sh BUGS -If a string is a suffix of another string in the database, -but the shorter string is seen first by -.Nm -both strings will be placed in the database, when just -placing the longer one there will do. diff --git a/usr.bin/xstr/xstr.c b/usr.bin/xstr/xstr.c deleted file mode 100644 index 28dd99cf7d5..00000000000 --- a/usr.bin/xstr/xstr.c +++ /dev/null @@ -1,493 +0,0 @@ -/* $OpenBSD: xstr.c,v 1.18 2013/11/26 21:08:09 deraadt Exp $ */ -/* $NetBSD: xstr.c,v 1.5 1994/12/24 16:57:59 cgd Exp $ */ - -/* - * Copyright (c) 1980, 1993 - * The Regents of the University of California. 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. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 <sys/types.h> -#include <signal.h> -#include <errno.h> -#include <unistd.h> -#include <stdio.h> -#include <ctype.h> -#include <string.h> -#include <stdlib.h> -#include "pathnames.h" - -/* - * xstr - extract and hash strings in a C program - * - * Bill Joy UCB - * November, 1978 - */ - -#define BUCKETS 128 - -off_t tellpt; -off_t mesgpt; -char *strings = "strings"; -char *array = 0; - -int cflg; -int vflg; -int readstd; - -struct hash { - off_t hpt; - char *hstr; - struct hash *hnext; - short hnew; -} bucket[BUCKETS]; - -void process(char *); -off_t yankstr(char **); -int octdigit(char); -void inithash(void); -int fgetNUL(char *, int, FILE *); -int xgetc(FILE *); -off_t hashit(char *, int); -void flushsh(void); -void found(int, off_t, char *); -void prstr(char *); -void xsdotc(void); -char lastchr(char *); -int istail(char *, char *); -void onintr(void); - -int -main(int argc, char *argv[]) -{ - int c; - int fdesc; - - while ((c = getopt(argc, argv, "cvl:-")) != -1) - switch (c) { - case '-': - readstd++; - break; - case 'c': - cflg++; - break; - case 'v': - vflg++; - break; - case 'l': - array = optarg; - break; - default: - fprintf(stderr, - "usage: xstr [-cv] [-l array] [-] [file ...]\n"); - exit(1); - } - argc -= optind; - argv += optind; - - if (array == 0) - array = "xstr"; - - if (signal(SIGINT, SIG_IGN) == SIG_DFL) - signal(SIGINT, (void(*)(int))onintr); - if (cflg || (argc == 0 && !readstd)) - inithash(); - else { - strings = strdup (_PATH_TMPFILE); - if (strings == NULL) { - fprintf(stderr, "Unable to allocate memory: %s", - strerror (errno)); - exit(1); - } - fdesc = mkstemp (strings); - if (fdesc < 0) { - fprintf(stderr, "Unable to create temporary file.\n"); - exit(1); - } - close (fdesc); - } - - while (readstd || argc > 0) { - if (freopen("x.c", "w", stdout) == NULL) { - perror("x.c"); - exit(1); - } - if (!readstd && freopen(argv[0], "r", stdin) == NULL) { - perror(argv[0]); - exit(2); - } - process("x.c"); - if (readstd == 0) - argc--, argv++; - else - readstd = 0; - } - flushsh(); - if (cflg == 0) - xsdotc(); - if (strings[0] == '/') - unlink(strings); - exit(0); -} - -char linebuf[BUFSIZ]; - -void -process(char *name) -{ - char *cp; - int c; - int incomm = 0; - int ret; - - printf("extern char\t%s[];\n", array); - for (;;) { - if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { - if (ferror(stdin)) { - perror(name); - exit(3); - } - break; - } - if (linebuf[0] == '#') { - if (linebuf[1] == ' ' && - isdigit((unsigned char)linebuf[2])) - printf("#line%s", &linebuf[1]); - else - printf("%s", linebuf); - continue; - } - for (cp = linebuf; (c = *cp++); ) - switch (c) { - case '"': - if (incomm) - goto def; - if ((ret = (int) yankstr(&cp)) == -1) - goto out; - printf("(&%s[%d])", array, ret); - break; - case '\'': - if (incomm) - goto def; - putchar(c); - if (*cp) - putchar(*cp++); - break; - case '/': - if (incomm || *cp != '*') - goto def; - incomm = 1; - cp++; - printf("/*"); - continue; - case '*': - if (incomm && *cp == '/') { - incomm = 0; - cp++; - printf("*/"); - continue; - } - goto def; - def: - default: - putchar(c); - break; - } - } -out: - if (ferror(stdout)) - perror("x.c"), onintr(); -} - -off_t -yankstr(char **cpp) -{ - char *cp = *cpp; - int c, ch; - char dbuf[BUFSIZ]; - char *dp = dbuf; - char *tp; - - while ((c = *cp++)) { - switch (c) { - case '"': - cp++; - goto out; - case '\\': - c = *cp++; - if (c == 0) - break; - if (c == '\n') { - if (fgets(linebuf, sizeof linebuf, stdin) - == NULL) { - if (ferror(stdin)) { - perror("x.c"); - exit(3); - } - return(-1); - } - cp = linebuf; - continue; - } - for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; (ch = *tp++); tp++) - if (c == ch) { - c = *tp; - goto gotc; - } - if (!octdigit(c)) { - *dp++ = '\\'; - break; - } - c -= '0'; - if (!octdigit(*cp)) - break; - c <<= 3, c += *cp++ - '0'; - if (!octdigit(*cp)) - break; - c <<= 3, c += *cp++ - '0'; - break; - } -gotc: - *dp++ = c; - } -out: - *cpp = --cp; - *dp = 0; - return (hashit(dbuf, 1)); -} - -int -octdigit(char c) -{ - - return (isdigit((unsigned char)c) && c != '8' && c != '9'); -} - -void -inithash(void) -{ - char buf[BUFSIZ]; - FILE *mesgread = fopen(strings, "r"); - - if (mesgread == NULL) - return; - for (;;) { - mesgpt = tellpt; - if (fgetNUL(buf, sizeof buf, mesgread) == 0) - break; - hashit(buf, 0); - } - fclose(mesgread); -} - -int -fgetNUL(char *obuf, int rmdr, FILE *file) -{ - int c; - char *buf = obuf; - - while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) - *buf++ = c; - *buf++ = 0; - return ((feof(file) || ferror(file)) ? 0 : 1); -} - -int -xgetc(FILE *file) -{ - - tellpt++; - return (getc(file)); -} - - -off_t -hashit(char *str, int new) -{ - int i; - struct hash *hp, *hp0; - - hp = hp0 = &bucket[lastchr(str) & 0177]; - while (hp->hnext) { - hp = hp->hnext; - i = istail(str, hp->hstr); - if (i >= 0) - return (hp->hpt + i); - } - if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) { - perror("xstr"); - exit(8); - } - hp->hpt = mesgpt; - if (!(hp->hstr = strdup(str))) { - (void)fprintf(stderr, "xstr: %s\n", strerror(errno)); - exit(1); - } - mesgpt += strlen(hp->hstr) + 1; - hp->hnext = hp0->hnext; - hp->hnew = new; - hp0->hnext = hp; - return (hp->hpt); -} - -void -flushsh(void) -{ - int i; - struct hash *hp; - FILE *mesgwrit; - int old = 0, new = 0; - - for (i = 0; i < BUCKETS; i++) - for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) - if (hp->hnew) - new++; - else - old++; - if (new == 0 && old != 0) - return; - mesgwrit = fopen(strings, old ? "r+" : "w"); - if (mesgwrit == NULL) { - perror(strings); - exit(4); - } - for (i = 0; i < BUCKETS; i++) - for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { - found(hp->hnew, hp->hpt, hp->hstr); - if (hp->hnew) { - fseek(mesgwrit, hp->hpt, SEEK_SET); - fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, - mesgwrit); - if (ferror(mesgwrit)) { - perror(strings); - exit(4); - } - } - } - if (fclose(mesgwrit) == EOF) { - perror(strings); - exit(4); - } -} - -void -found(int new, off_t off, char *str) -{ - if (vflg == 0) - return; - if (!new) - fprintf(stderr, "found at %d:", (int) off); - else - fprintf(stderr, "new at %d:", (int) off); - prstr(str); - fprintf(stderr, "\n"); -} - -void -prstr(char *cp) -{ - int c; - - while ((c = (*cp++ & 0377))) - if (c < ' ') - fprintf(stderr, "^%c", c + '`'); - else if (c == 0177) - fprintf(stderr, "^?"); - else if (c > 0200) - fprintf(stderr, "\\%03o", c); - else - fprintf(stderr, "%c", c); -} - -void -xsdotc(void) -{ - FILE *strf = fopen(strings, "r"); - FILE *xdotcf; - - if (strf == NULL) { - perror(strings); - exit(5); - } - xdotcf = fopen("xs.c", "w"); - if (xdotcf == NULL) { - perror("xs.c"); - exit(6); - } - fprintf(xdotcf, "char\t%s[] = {\n", array); - for (;;) { - int i, c; - - for (i = 0; i < 8; i++) { - c = getc(strf); - if (ferror(strf)) { - perror(strings); - onintr(); - } - if (feof(strf)) { - fprintf(xdotcf, "\n"); - goto out; - } - fprintf(xdotcf, "0x%02x,", c); - } - fprintf(xdotcf, "\n"); - } -out: - fprintf(xdotcf, "};\n"); - fclose(xdotcf); - fclose(strf); -} - -char -lastchr(char *cp) -{ - - while (cp[0] && cp[1]) - cp++; - return (*cp); -} - -int -istail(char *str, char *of) -{ - int d = strlen(of) - strlen(str); - - if (d < 0 || strcmp(&of[d], str) != 0) - return (-1); - return (d); -} - -void -onintr(void) -{ - - signal(SIGINT, SIG_IGN); - if (strings[0] == '/') - unlink(strings); - unlink("x.c"); - unlink("xs.c"); - _exit(7); -} |