cid.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* $Header$ */
  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. CheckId(id, len)
  45. char *id;
  46. {
  47. struct idf *idp = FindId(id);
  48. if (idp) {
  49. printf("%s", idp->id_text);
  50. }
  51. else {
  52. printf("%s", id);
  53. }
  54. }
  55. DoMacro(str)
  56. char *str;
  57. {
  58. char *id, *text;
  59. id = str++;
  60. while (*str != '\0' && *str != '=') {
  61. str++;
  62. }
  63. if (*str == '=') {
  64. *str++ = '\0';
  65. text = str;
  66. }
  67. else {
  68. text = "";
  69. }
  70. InsertMacro(id, text);
  71. }
  72. GetMacros(fn)
  73. char *fn;
  74. {
  75. FILE *fp;
  76. register c;
  77. char buf[LINE_LEN];
  78. char *bufp = &buf[0];
  79. if ((fp = fopen(fn, "r")) == NULL) {
  80. fprintf(stderr, "%s: cannot read file \"%s\"\n", ProgName, fn);
  81. return 0;
  82. }
  83. while ((c = getc(fp)) != EOF) {
  84. if (c == '\n' && bufp != &buf[0]) {
  85. *bufp = '\0';
  86. DoMacro(&buf[0]);
  87. bufp = &buf[0];
  88. }
  89. else {
  90. *bufp++ = c;
  91. }
  92. }
  93. fclose(fp);
  94. }
  95. InsertMacro(id, text)
  96. char *id, *text;
  97. {
  98. int hash_val = EnHash(id);
  99. struct idf *idp = hash_tab[hash_val];
  100. while (idp) {
  101. if (strcmp(idp->id_name, id) == 0) {
  102. fprintf(stderr, "%s: (warning) redefinition of %s\n",
  103. ProgName, id);
  104. break;
  105. }
  106. idp = idp->id_next;
  107. }
  108. if (idp == 0) {
  109. idp = (struct idf *) Malloc(sizeof(struct idf));
  110. }
  111. idp->id_next = hash_tab[hash_val];
  112. idp->id_name = Salloc(id);
  113. idp->id_text = Salloc(text);
  114. hash_tab[hash_val] = idp;
  115. }
  116. char *
  117. Malloc(n)
  118. unsigned n;
  119. {
  120. char *mem, *malloc();
  121. if ((mem = malloc(n)) == 0) {
  122. fprintf(stderr, "%s: out of memory\n", ProgName);
  123. exit(1);
  124. }
  125. return mem;
  126. }
  127. char *
  128. Salloc(str)
  129. char *str;
  130. {
  131. char *strcpy();
  132. if (str == 0) {
  133. str = "";
  134. }
  135. return strcpy(Malloc(strlen(str) + 1), str);
  136. }
  137. struct idf *
  138. FindId(id)
  139. char *id;
  140. {
  141. register hash_val = EnHash(id);
  142. register struct idf *idp = hash_tab[hash_val];
  143. while (idp) {
  144. if (strcmp(idp->id_name, id) == 0) {
  145. return idp;
  146. }
  147. idp = idp->id_next;
  148. }
  149. return 0;
  150. }
  151. EnHash(id)
  152. char *id;
  153. {
  154. register unsigned hash_val = 0;
  155. while (*id) {
  156. hash_val = 31 * hash_val + *id++;
  157. }
  158. return hash_val % (unsigned) HASHSIZE;
  159. }
  160. extern int GCcopy;
  161. BeginOfProgram()
  162. {
  163. GCcopy = 1;
  164. }
  165. EndOfProgram()
  166. {
  167. }