prid.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* Print IDentifiers occurring in C programs outside comment, strings
  7. and character constants.
  8. Flags:
  9. -l<num> : print identifiers of <num> or more characters
  10. (default <num> = 8)
  11. Author: Erik Baalbergen
  12. Date: Oct 23, 1985
  13. */
  14. #include <stdio.h>
  15. extern char *ProgName;
  16. #ifndef DEF_LENGTH
  17. #define DEF_LENGTH 8
  18. #endif
  19. int maxlen = DEF_LENGTH;
  20. BeginOfProgram() {}
  21. DoOption(str)
  22. char *str;
  23. {
  24. switch (str[1]) {
  25. case 'l':
  26. if ((maxlen = atoi(&str[2])) <= 0) {
  27. fprintf(stderr, "%s: option \"-l%s\" ignored\n",
  28. ProgName, &str[2]);
  29. maxlen = DEF_LENGTH;
  30. }
  31. break;
  32. default:
  33. fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
  34. break;
  35. }
  36. }
  37. CheckId(id, len)
  38. char *id;
  39. {
  40. if (len >= maxlen) {
  41. InsertId(id);
  42. }
  43. }
  44. #define HASHSIZE 257
  45. struct idf {
  46. char *id_name;
  47. struct idf *id_next;
  48. };
  49. struct idf *hash_tab[HASHSIZE];
  50. char *Malloc(), *Salloc();
  51. InsertId(id)
  52. char *id;
  53. {
  54. int hash_val = EnHash(id);
  55. register struct idf *idp = hash_tab[hash_val];
  56. register struct idf *p = 0;
  57. while (idp && strcmp(idp->id_name, id)) {
  58. p = idp;
  59. idp = idp->id_next;
  60. }
  61. if (idp == 0) {
  62. idp = (struct idf *) Malloc(sizeof(struct idf));
  63. idp->id_next = 0;
  64. if (!p) hash_tab[hash_val] = idp;
  65. else p->id_next = idp;
  66. idp->id_name = Salloc(id);
  67. }
  68. }
  69. char *
  70. Malloc(n)
  71. unsigned n;
  72. {
  73. char *mem, *malloc();
  74. if ((mem = malloc(n)) == 0) {
  75. fprintf(stderr, "%s: out of memory\n", ProgName);
  76. exit(1);
  77. }
  78. return mem;
  79. }
  80. char *
  81. Salloc(str)
  82. char *str;
  83. {
  84. char *strcpy();
  85. if (str == 0)
  86. str = "";
  87. return strcpy(Malloc((unsigned)strlen(str) + 1), str);
  88. }
  89. EnHash(id)
  90. char *id;
  91. {
  92. register unsigned hash_val = 0;
  93. register n = maxlen;
  94. while (n-- && *id)
  95. hash_val = 31 * hash_val + *id++;
  96. return hash_val % (unsigned) HASHSIZE;
  97. }
  98. EndOfProgram()
  99. {
  100. register struct idf *idp;
  101. register int i;
  102. for (i = 0; i < HASHSIZE; i++) {
  103. for (idp = hash_tab[i]; idp; idp = idp->id_next) {
  104. printf("%s\n", idp->id_name);
  105. }
  106. }
  107. }