tab.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. /* @cc tab.c -o $INSTALLDIR/tab@
  7. tab - table generator
  8. Author: Erik Baalbergen (..tjalk!erikb)
  9. */
  10. #include <stdio.h>
  11. #define MAXTAB 10000
  12. #define MAXBUF 10000
  13. #define COMCOM '-'
  14. #define FILECOM '%'
  15. int InputForm = 'c';
  16. char OutputForm[MAXBUF] = "%s,\n";
  17. int TabSize = 257;
  18. char *Table[MAXTAB];
  19. char *ProgCall;
  20. main(argc, argv)
  21. char *argv[];
  22. {
  23. ProgCall = *argv++;
  24. argc--;
  25. while (argc-- > 0) {
  26. if (**argv == COMCOM) {
  27. option(*argv++);
  28. }
  29. else {
  30. process(*argv++, InputForm);
  31. }
  32. }
  33. exit(0);
  34. }
  35. char *
  36. Salloc(s)
  37. char *s;
  38. {
  39. extern char *malloc(), *strcpy();
  40. char *ns = malloc((unsigned int)strlen(s) + 1);
  41. if (ns) {
  42. strcpy(ns, s);
  43. }
  44. return ns;
  45. }
  46. option(str)
  47. char *str;
  48. {
  49. /* note that *str indicates the source of the option:
  50. either COMCOM (from command line) or FILECOM (from a file).
  51. */
  52. extern char *sprintf();
  53. switch (*++str) {
  54. case ' ': /* command */
  55. case '\t':
  56. case '\0':
  57. break;
  58. case 'I':
  59. InputForm = *++str;
  60. break;
  61. case 'f':
  62. if (*++str == '\0') {
  63. fprintf(stderr, "%s: -f: name expected\n", ProgCall);
  64. exit(1);
  65. }
  66. DoFile(str);
  67. break;
  68. case 'F':
  69. sprintf(OutputForm, "%s\n", ++str);
  70. break;
  71. case 'T':
  72. printf("%s\n", ++str);
  73. break;
  74. case 'p':
  75. PrintTable();
  76. break;
  77. case 'C':
  78. ClearTable();
  79. break;
  80. case 'S':
  81. {
  82. register i = stoi(++str);
  83. if (i <= 0 || i > MAXTAB) {
  84. fprintf(stderr, "%s: size would exceed maximum\n",
  85. ProgCall);
  86. }
  87. else {
  88. TabSize = i;
  89. }
  90. break;
  91. }
  92. default:
  93. fprintf(stderr, "%s: bad option -%s\n", ProgCall, str);
  94. }
  95. }
  96. ClearTable()
  97. {
  98. register i;
  99. for (i = 0; i < MAXTAB; i++) {
  100. Table[i] = 0;
  101. }
  102. }
  103. PrintTable()
  104. {
  105. register i;
  106. for (i = 0; i < TabSize; i++) {
  107. if (Table[i]) {
  108. printf(OutputForm, Table[i]);
  109. }
  110. else {
  111. printf(OutputForm, "0");
  112. }
  113. }
  114. }
  115. process(str, format)
  116. char *str;
  117. {
  118. char *cstr = str;
  119. char *Name = cstr; /* overwrite original string! */
  120. /* strip of the entry name
  121. */
  122. while (*str && *str != ':') {
  123. if (*str == '\\') {
  124. ++str;
  125. }
  126. *cstr++ = *str++;
  127. }
  128. if (*str != ':') {
  129. fprintf(stderr, "%s: bad specification: \"%s\", ignored\n",
  130. ProgCall, Name);
  131. return 0;
  132. }
  133. *cstr = '\0';
  134. str++;
  135. switch (format) {
  136. case 'c':
  137. return c_proc(str, Name);
  138. default:
  139. fprintf(stderr, "%s: bad input format\n", ProgCall);
  140. }
  141. return 0;
  142. }
  143. c_proc(str, Name)
  144. char *str;
  145. char *Name;
  146. {
  147. int ch, ch2;
  148. int quoted();
  149. while (*str) {
  150. if (*str == '\\') {
  151. ch = quoted(&str);
  152. }
  153. else {
  154. ch = *str++;
  155. }
  156. if (*str == '-') {
  157. if (*++str == '\\') {
  158. ch2 = quoted(&str);
  159. }
  160. else {
  161. if (ch2 = *str++);
  162. else str--;
  163. }
  164. if (ch > ch2) {
  165. fprintf(stderr, "%s: bad range\n", ProgCall);
  166. return 0;
  167. }
  168. if (ch >= 0 && ch2 <= 255)
  169. while (ch <= ch2)
  170. Table[ch++] = Salloc(Name);
  171. }
  172. else {
  173. if (ch >= 0 && ch <= 255)
  174. Table[ch] = Salloc(Name);
  175. }
  176. }
  177. return 1;
  178. }
  179. int
  180. quoted(pstr)
  181. char **pstr;
  182. {
  183. register int ch;
  184. register int i;
  185. register char *str = *pstr;
  186. if ((*++str >= '0') && (*str <= '9')) {
  187. ch = 0;
  188. for (i = 0; i < 3; i++) {
  189. ch = 8 * ch + *str - '0';
  190. if (*++str < '0' || *str > '9')
  191. break;
  192. }
  193. }
  194. else {
  195. switch (*str++) {
  196. case 'n':
  197. ch = '\n';
  198. break;
  199. case 't':
  200. ch = '\t';
  201. break;
  202. case 'b':
  203. ch = '\b';
  204. break;
  205. case 'r':
  206. ch = '\r';
  207. break;
  208. case 'f':
  209. ch = '\f';
  210. break;
  211. default :
  212. ch = *str;
  213. }
  214. }
  215. *pstr = str;
  216. return ch & 0377;
  217. }
  218. int
  219. stoi(str)
  220. char *str;
  221. {
  222. register i = 0;
  223. while (*str >= '0' && *str <= '9') {
  224. i = i * 10 + *str++ - '0';
  225. }
  226. return i;
  227. }
  228. char *
  229. getline(s, n, fp)
  230. char *s;
  231. FILE *fp;
  232. {
  233. register c = getc(fp);
  234. char *str = s;
  235. while (n--) {
  236. if (c == EOF) {
  237. return NULL;
  238. }
  239. else
  240. if (c == '\n') {
  241. *str++ = '\0';
  242. return s;
  243. }
  244. *str++ = c;
  245. c = getc(fp);
  246. }
  247. s[n - 1] = '\0';
  248. return s;
  249. }
  250. #define BUFSIZE 1024
  251. DoFile(name)
  252. char *name;
  253. {
  254. char text[BUFSIZE];
  255. FILE *fp;
  256. if ((fp = fopen(name, "r")) == NULL) {
  257. fprintf(stderr, "%s: cannot read file %s\n", ProgCall, name);
  258. exit(1);
  259. }
  260. while (getline(text, BUFSIZE, fp) != NULL) {
  261. if (text[0] == FILECOM) {
  262. option(text);
  263. }
  264. else {
  265. process(text, InputForm);
  266. }
  267. }
  268. }