Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

  1. /***
  2. *symtab.c - Ifdef symbol table storage module
  3. *
  4. * Copyright (c) 1988-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Store the symbols from the switches file.
  8. *
  9. *Revision History:
  10. * ??-??-88 PHG Initial version
  11. *
  12. *******************************************************************************/
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <search.h>
  17. #include <ctype.h>
  18. #include "constant.h"
  19. #include "errormes.h"
  20. #include "symtab.h"
  21. /* Internal constants */
  22. #define MAXSYMBOLS 512 /* Maximum number of symbols (switches) */
  23. #define IDENT_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_$?"
  24. /* Symbol record */
  25. struct symrec {
  26. char *name; /* name of the symbol */
  27. int type; /* type of symbol (DEFINED, UNDEFINED, IGNORE)*/
  28. };
  29. /* Internal variables */
  30. int numsyms; /* Number of symbols */
  31. struct symrec symtable[MAXSYMBOLS];
  32. /* Symbol table */
  33. /* Procedures */
  34. int compsym(const struct symrec *, const struct symrec *);
  35. /* Compare two records for alphabetical order */
  36. int compsym(rec1, rec2)
  37. const struct symrec *rec1, *rec2;
  38. {
  39. return strcmp(rec1->name, rec2->name);
  40. }
  41. /* Add symbol to symbol table */
  42. void addsym(symbol, type)
  43. char *symbol;
  44. int type;
  45. {
  46. if (lookupsym(symbol) != NOTPRESENT) {
  47. fprintf(stderr, "fatal error: symbol \"%s\" already in symbol table.\n", symbol);
  48. exit(1);
  49. }
  50. symtable[numsyms].name = _strdup(symbol);
  51. symtable[numsyms].type = type;
  52. ++numsyms;
  53. }
  54. /* Read switches from a file into symbol table */
  55. void readsyms(filename)
  56. char *filename;
  57. {
  58. FILE *f;
  59. char name[MAXNAMELEN];
  60. f = fopen(filename, "r");
  61. if (f == NULL) {
  62. fprintf(stderr, "fatal error: cannot open switch file \"%s\".\n", filename);
  63. exit(1);
  64. }
  65. numsyms = 0;
  66. do {
  67. if ( fgets(name, MAXNAMELEN, f) == NULL) {
  68. fprintf(stderr, "fatal error: unexpected EOF in switch file.\n");
  69. exit(1);
  70. }
  71. name[strlen(name) - 1] = '\0'; /* remove trailing \n */
  72. if (name[0] != '-') {
  73. addsym(name, DEFINED);
  74. }
  75. } while (name[0] != '-');
  76. do {
  77. if (fgets(name, MAXNAMELEN, f) == NULL) {
  78. fprintf(stderr, "fatal error: unexpected EOF in switch file.\n");
  79. exit(1);
  80. }
  81. name[strlen(name) - 1] = '\0'; /* remove trailing \n */
  82. if (name[0] != '-') {
  83. addsym(name, UNDEFINED);
  84. }
  85. } while (name[0] != '-');
  86. do {
  87. if (fgets(name, MAXNAMELEN, f) == NULL)
  88. break;
  89. name[strlen(name) - 1] = '\0'; /* remove trailing \n */
  90. if (name[0] != '-') {
  91. addsym(name, IGNORE);
  92. }
  93. } while (name[0] != '-');
  94. fclose(f);
  95. }
  96. /* Lookup symbol in symbol table */
  97. int lookupsym(name)
  98. char *name;
  99. {
  100. struct symrec srchrec;
  101. struct symrec *recfound;
  102. srchrec.name = name;
  103. recfound = (struct symrec *) _lfind( (const void *)&srchrec, (const void *)symtable,
  104. &numsyms, sizeof(struct symrec), compsym);
  105. if (recfound == NULL)
  106. return NOTPRESENT;
  107. else
  108. return recfound->type;
  109. }
  110. /* Check if token is identifier only (must have no whitespace) */
  111. int ident_only(token)
  112. char *token;
  113. {
  114. /* is an identifier if all characters are in IDENT_CHARS
  115. and first character is not a digit */
  116. return (strspn(token, IDENT_CHARS) == strlen(token) &&
  117. !isdigit(token[0]));
  118. }