cid.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* Change IDentifiers occurring in C programs outside comment, strings
  7. and character constants.
  8. -Dname=text : replace all occerences of name by text
  9. -Dname : same as -Dname=
  10. -Ffile : read sentences of the from name=text from file
  11. Author: Erik Baalbergen
  12. Date: Oct 23, 1985
  13. */
  14. #include <stdio.h>
  15. #ifndef DEF_LENGTH
  16. #define DEF_LENGTH 8
  17. #endif
  18. #define LINE_LEN 1024
  19. #define HASHSIZE 257
  20. struct idf {
  21. struct idf *id_next;
  22. char *id_name;
  23. char *id_text;
  24. };
  25. struct idf *hash_tab[HASHSIZE];
  26. char *Malloc(), *Salloc();
  27. struct idf *FindId();
  28. extern char *ProgName;
  29. DoOption(str)
  30. char *str;
  31. {
  32. switch (str[1]) {
  33. case 'D':
  34. DoMacro(&str[2]);
  35. break;
  36. case 'F':
  37. GetMacros(&str[2]);
  38. break;
  39. default:
  40. fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
  41. break;
  42. }
  43. }
  44. /*ARGSUSED*/
  45. CheckId(id, len)
  46. char *id;
  47. {
  48. struct idf *idp = FindId(id);
  49. if (idp) {
  50. printf("%s", idp->id_text);
  51. }
  52. else {
  53. printf("%s", id);
  54. }
  55. }
  56. DoMacro(str)
  57. char *str;
  58. {
  59. char *id, *text;
  60. id = str++;
  61. while (*str != '\0' && *str != '=') {
  62. str++;
  63. }
  64. if (*str == '=') {
  65. *str++ = '\0';
  66. text = str;
  67. }
  68. else {
  69. text = "";
  70. }
  71. InsertMacro(id, text);
  72. }
  73. GetMacros(fn)
  74. char *fn;
  75. {
  76. FILE *fp;
  77. register c;
  78. char buf[LINE_LEN];
  79. char *bufp = &buf[0];
  80. if ((fp = fopen(fn, "r")) == NULL) {
  81. fprintf(stderr, "%s: cannot read file \"%s\"\n", ProgName, fn);
  82. return;
  83. }
  84. while ((c = getc(fp)) != EOF) {
  85. if (c == '\n' && bufp != &buf[0]) {
  86. *bufp = '\0';
  87. DoMacro(&buf[0]);
  88. bufp = &buf[0];
  89. }
  90. else {
  91. *bufp++ = c;
  92. }
  93. }
  94. fclose(fp);
  95. }
  96. InsertMacro(id, text)
  97. char *id, *text;
  98. {
  99. int hash_val = EnHash(id);
  100. struct idf *idp = hash_tab[hash_val];
  101. while (idp) {
  102. if (strcmp(idp->id_name, id) == 0) {
  103. fprintf(stderr, "%s: (warning) redefinition of %s\n",
  104. ProgName, id);
  105. break;
  106. }
  107. idp = idp->id_next;
  108. }
  109. if (idp == 0) {
  110. idp = (struct idf *) Malloc(sizeof(struct idf));
  111. }
  112. idp->id_next = hash_tab[hash_val];
  113. idp->id_name = Salloc(id);
  114. idp->id_text = Salloc(text);
  115. hash_tab[hash_val] = idp;
  116. }
  117. char *
  118. Malloc(n)
  119. unsigned n;
  120. {
  121. char *mem, *malloc();
  122. if ((mem = malloc(n)) == 0) {
  123. fprintf(stderr, "%s: out of memory\n", ProgName);
  124. exit(1);
  125. }
  126. return mem;
  127. }
  128. char *
  129. Salloc(str)
  130. char *str;
  131. {
  132. char *strcpy();
  133. if (str == 0) {
  134. str = "";
  135. }
  136. return strcpy(Malloc((unsigned)strlen(str) + 1), str);
  137. }
  138. struct idf *
  139. FindId(id)
  140. char *id;
  141. {
  142. register hash_val = EnHash(id);
  143. register struct idf *idp = hash_tab[hash_val];
  144. while (idp) {
  145. if (strcmp(idp->id_name, id) == 0) {
  146. return idp;
  147. }
  148. idp = idp->id_next;
  149. }
  150. return 0;
  151. }
  152. EnHash(id)
  153. char *id;
  154. {
  155. register unsigned hash_val = 0;
  156. while (*id) {
  157. hash_val = 31 * hash_val + *id++;
  158. }
  159. return hash_val % (unsigned) HASHSIZE;
  160. }
  161. extern int GCcopy;
  162. BeginOfProgram()
  163. {
  164. GCcopy = 1;
  165. }
  166. EndOfProgram()
  167. {
  168. }