tab.c 4.0 KB

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