Mercurial > hg > early-roguelike
comparison rogue4/xstr.c @ 12:9535a08ddc39
Import Rogue 5.2 from the Roguelike Restoration Project (r1490)
| author | edwarj4 |
|---|---|
| date | Sat, 24 Oct 2009 16:52:52 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 11:949d558c2162 | 12:9535a08ddc39 |
|---|---|
| 1 /* | |
| 2 * | |
| 3 * Rogue: Exploring the Dungeons of Doom | |
| 4 * Copyright (C) 1980, 1981, 1982 Michael Toy, Ken Arnold and Glenn Wichman | |
| 5 * All rights reserved. | |
| 6 * | |
| 7 * See the file LICENSE.TXT for full copyright and licensing information. | |
| 8 */ | |
| 9 | |
| 10 static char *sccsid = "@(#)xstr.c 4.1 (Berkeley) 10/1/80"; | |
| 11 #include <stdio.h> | |
| 12 #include <ctype.h> | |
| 13 #include <sys/types.h> | |
| 14 #include <signal.h> | |
| 15 | |
| 16 /* | |
| 17 * xstr - extract and hash strings in a C program | |
| 18 * | |
| 19 * Bill Joy UCB | |
| 20 * November, 1978 | |
| 21 */ | |
| 22 | |
| 23 #define ignore(a) (a) | |
| 24 | |
| 25 char *calloc(); | |
| 26 off_t tellpt; | |
| 27 off_t hashit(); | |
| 28 char *mktemp(); | |
| 29 void onintr(int); | |
| 30 char *savestr(); | |
| 31 char *strcat(); | |
| 32 char *strcpy(); | |
| 33 off_t yankstr(); | |
| 34 | |
| 35 off_t mesgpt; | |
| 36 char *strings = "strings"; | |
| 37 | |
| 38 int cflg; | |
| 39 int vflg; | |
| 40 int readstd; | |
| 41 | |
| 42 main(argc, argv) | |
| 43 int argc; | |
| 44 char *argv[]; | |
| 45 { | |
| 46 | |
| 47 argc--, argv++; | |
| 48 while (argc > 0 && argv[0][0] == '-') { | |
| 49 register char *cp = &(*argv++)[1]; | |
| 50 | |
| 51 argc--; | |
| 52 if (*cp == 0) { | |
| 53 readstd++; | |
| 54 continue; | |
| 55 } | |
| 56 do switch (*cp++) { | |
| 57 | |
| 58 case 'c': | |
| 59 cflg++; | |
| 60 continue; | |
| 61 | |
| 62 case 'v': | |
| 63 vflg++; | |
| 64 continue; | |
| 65 | |
| 66 default: | |
| 67 fprintf(stderr, "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n"); | |
| 68 } while (*cp); | |
| 69 } | |
| 70 if (signal(SIGINT, SIG_IGN) == SIG_DFL) | |
| 71 signal(SIGINT, onintr); | |
| 72 if (cflg || argc == 0 && !readstd) | |
| 73 inithash(); | |
| 74 else | |
| 75 strings = mktemp(savestr("/tmp/xstrXXXXXX")); | |
| 76 while (readstd || argc > 0) { | |
| 77 if (freopen("x.c", "w", stdout) == NULL) | |
| 78 perror("x.c"), exit(1); | |
| 79 if (!readstd && freopen(argv[0], "r", stdin) == NULL) | |
| 80 perror(argv[0]), exit(2); | |
| 81 process("x.c"); | |
| 82 if (readstd == 0) | |
| 83 argc--, argv++; | |
| 84 else | |
| 85 readstd = 0; | |
| 86 }; | |
| 87 flushsh(); | |
| 88 if (cflg == 0) | |
| 89 xsdotc(); | |
| 90 if (strings[0] == '/') | |
| 91 ignore(md_unlink(strings)); | |
| 92 exit(0); | |
| 93 } | |
| 94 | |
| 95 process(name) | |
| 96 char *name; | |
| 97 { | |
| 98 char *cp; | |
| 99 char linebuf[BUFSIZ]; | |
| 100 register int c; | |
| 101 register int incomm = 0; | |
| 102 | |
| 103 printf("extern char\txstr[];\n"); | |
| 104 for (;;) { | |
| 105 if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { | |
| 106 if (ferror(stdin)) { | |
| 107 perror(name); | |
| 108 exit(3); | |
| 109 } | |
| 110 break; | |
| 111 } | |
| 112 if (linebuf[0] == '#') { | |
| 113 if (linebuf[1] == ' ' && isdigit(linebuf[2])) | |
| 114 printf("#line%s", &linebuf[1]); | |
| 115 else | |
| 116 printf("%s", linebuf); | |
| 117 continue; | |
| 118 } | |
| 119 for (cp = linebuf; c = *cp++;) switch (c) { | |
| 120 | |
| 121 case '"': | |
| 122 if (incomm) | |
| 123 goto def; | |
| 124 printf("(&xstr[%d])", (int) yankstr(&cp)); | |
| 125 break; | |
| 126 | |
| 127 case '\'': | |
| 128 if (incomm) | |
| 129 goto def; | |
| 130 putchar(c); | |
| 131 if (*cp) | |
| 132 putchar(*cp++); | |
| 133 break; | |
| 134 | |
| 135 case '/': | |
| 136 if (incomm || *cp != '*') | |
| 137 goto def; | |
| 138 incomm = 1; | |
| 139 cp++; | |
| 140 printf("/*"); | |
| 141 continue; | |
| 142 | |
| 143 case '*': | |
| 144 if (incomm && *cp == '/') { | |
| 145 incomm = 0; | |
| 146 cp++; | |
| 147 printf("*/"); | |
| 148 continue; | |
| 149 } | |
| 150 goto def; | |
| 151 | |
| 152 def: | |
| 153 default: | |
| 154 putchar(c); | |
| 155 break; | |
| 156 } | |
| 157 } | |
| 158 if (ferror(stdout)) | |
| 159 perror("x.c"), onintr(-1); | |
| 160 } | |
| 161 | |
| 162 off_t | |
| 163 yankstr(cpp) | |
| 164 register char **cpp; | |
| 165 { | |
| 166 register char *cp = *cpp; | |
| 167 register int c, ch; | |
| 168 char dbuf[BUFSIZ]; | |
| 169 register char *dp = dbuf; | |
| 170 register char *tp; | |
| 171 | |
| 172 while (c = *cp++) { | |
| 173 switch (c) { | |
| 174 | |
| 175 case '"': | |
| 176 cp++; | |
| 177 goto out; | |
| 178 | |
| 179 case '\\': | |
| 180 c = *cp++; | |
| 181 if (c == 0) | |
| 182 break; | |
| 183 if (c == '\n') | |
| 184 continue; | |
| 185 for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; ch = *tp++; tp++) | |
