chtab.c 4.6 KB

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