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.

449 lines
14 KiB

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #define TRUE (~0)
  5. #define FALSE 0
  6. #define TABLESIZE 1100
  7. struct entry {
  8. struct entry *t_link;
  9. char *t_name;
  10. char *t_lex;
  11. unsigned short t_id;
  12. } *table[TABLESIZE];
  13. FILE *infile, *outfile;
  14. unsigned short nsym = 0;
  15. unsigned short hashstore = TRUE;
  16. unsigned short f386 = TRUE;
  17. extern char *_strdup();
  18. extern char *malloc();
  19. extern unsigned short hash();
  20. extern unsigned short atoi();
  21. static tfree(); /* defined below */
  22. static struct entry *talloc(); /* defined below */
  23. /* Local functions */
  24. void s_entries ( struct entry *, char * );
  25. void enter ( char *, char *, unsigned short );
  26. __cdecl main( ac, av )
  27. int ac;
  28. char **av;
  29. {
  30. char ent[30], name[30], lex[30], ts[30], tc[30];
  31. unsigned short size, count;
  32. register unsigned short i;
  33. register struct entry *p;
  34. struct entry *q;
  35. char *a;
  36. double n, j, ssq, sum;
  37. ac--;
  38. av++;
  39. while (**av == '-') {
  40. ac--;
  41. a = *av++;
  42. while (*++a){
  43. if (_stricmp(a, "dnoV386") == 0){
  44. f386 = FALSE;
  45. break;
  46. }
  47. else
  48. if (*a == 'h')
  49. hashstore = ~hashstore;
  50. else {
  51. fprintf( stderr, "usage: genkey [-dnoV386] [-h] [infile] [outfile]\n" );
  52. exit( 1 );
  53. }
  54. }
  55. }
  56. if (ac < 1)
  57. infile = stdin;
  58. else {
  59. ac--;
  60. if ((infile = fopen( *av++, "r" )) == NULL) {
  61. fprintf( stderr, "Cannot open input file %s\n", *--av );
  62. exit( 1 );
  63. }
  64. }
  65. if (ac < 1)
  66. outfile = stdout;
  67. else if ((outfile = fopen( *av, "w" )) == NULL) {
  68. fprintf( stderr, "Cannot open output file %s\n", *av );
  69. exit( 1 );
  70. }
  71. #ifdef DEBUG
  72. setbuf( outfile, NULL );
  73. #endif
  74. /* copy first line of file */
  75. do {
  76. i = getc( infile );
  77. putc( i, outfile );
  78. } while (i != '\n');
  79. while (fscanf( infile, " %s %s %s\n", tc, ts, name) == 3) {
  80. count = atoi( tc );
  81. size = atoi( ts );
  82. #ifdef DEBUG
  83. printf( "DEBUG: name=%s, size=%u, count=%u\n", name, size, count );
  84. #endif
  85. for (i = 0; i < TABLESIZE; table[i++] = NULL)
  86. ;
  87. for (i = 0; i < count; i++)
  88. if (fscanf( infile, " %s %s\n", ent, lex ) == 2) {
  89. #ifdef DEBUG
  90. printf( "DEBUG: ent=%s, lex=%s\n", ent, lex );
  91. #endif
  92. enter( ent, lex, size );
  93. }
  94. else {
  95. fprintf( stderr, "Error in input file\n" );
  96. exit( 1 );
  97. }
  98. #ifdef DEBUG
  99. printf( "DEBUG: finished input loop\n" );
  100. #endif
  101. print_table( size );
  102. #ifdef DEBUG
  103. printf( "DEBUG: finished print_table()\n" );
  104. #endif
  105. print_struct( name, size );
  106. #ifdef DEBUG
  107. printf("DEBUG: finished print_struct()\n" );
  108. #endif
  109. n = sum = ssq = 0.0;
  110. for (i = 0; i < TABLESIZE; i++) {
  111. j = 0.0;
  112. if (p = table[i]) {
  113. n += 1.0;
  114. do {
  115. q = p->t_link;
  116. tfree( p );
  117. j += 1.0;
  118. } while (p = q);
  119. sum += j;
  120. ssq += j * j;
  121. }
  122. }
  123. #ifdef DEBUG
  124. printf( "DEBUG: finished statistics loop\n" );
  125. #endif
  126. printf( "%6s: Size = %3u, Buckets = %3u, Avg = %.2f, Std = %.2f\n",
  127. name, (unsigned short)sum, (unsigned short)n, sum / n,
  128. sqrt( (ssq - sum * sum / n) / (n - 1.0) )
  129. );
  130. #ifdef DEBUG
  131. printf( "DEBUG: finished this table; looking for more\n" );
  132. #endif
  133. }
  134. exit( 0 );
  135. }
  136. /****************************************************************/
  137. /* */
  138. /* enter : make an ent into the symbol table. */
  139. /* */
  140. /****************************************************************/
  141. void enter ( ent, lex, size )
  142. char *ent;
  143. char *lex;
  144. unsigned short size;
  145. {
  146. register unsigned short hashval;
  147. register struct entry *p;
  148. int cb;
  149. int fIs386Only;
  150. cb = strlen(ent);
  151. fIs386Only = !strcmp(ent + strlen(ent) - 4, ".386");
  152. if (!f386 && fIs386Only)
  153. return;
  154. if (fIs386Only)
  155. *(ent + cb - 4) = '\0';
  156. p = talloc();
  157. p->t_id = nsym++;
  158. hashval = hash( ent ) % size;
  159. p->t_link = table[hashval];
  160. table[hashval] = p;
  161. if ((p->t_name = _strdup( ent )) == NULL
  162. || (p->t_lex = _strdup( lex )) == NULL)
  163. memerror();
  164. }
  165. /****************************************************************/
  166. /* */
  167. /* print_table : output the table we have built. */
  168. /* */
  169. /****************************************************************/
  170. print_table ( size )
  171. unsigned short size;
  172. {
  173. register unsigned short i;
  174. register struct entry *p;
  175. fprintf( outfile, "/***\n" );
  176. for (i = 0; i < size; i++) {
  177. fprintf( outfile, " *\t[%u]\n", i );
  178. for (p = table[i]; p; p = p->t_link)
  179. fprintf( outfile, " *\t\t%s,\t%s\n", p->t_name,
  180. p->t_lex );
  181. }
  182. fprintf( outfile, " */\n" );
  183. }
  184. /****************************************************************/
  185. /* */
  186. /* print_struct : print the initialization structures. */
  187. /* */
  188. /****************************************************************/
  189. print_struct ( name, size )
  190. char *name;
  191. unsigned short size;
  192. {
  193. register unsigned short i;
  194. for (i = 0; i < size; i++)
  195. s_entries( table[i], name );
  196. s_symbols( name, size );
  197. s_header( name, size );
  198. }
  199. /****************************************************************/
  200. /* */
  201. /* s_entries : print the symbol names and defs. */
  202. /* */
  203. /****************************************************************/
  204. void s_entries ( p, name )
  205. register struct entry *p;
  206. char *name;
  207. {
  208. struct reverse {
  209. struct entry *actual;
  210. struct reverse *next;
  211. } *head = NULL;
  212. register struct reverse *q;
  213. if (!p)
  214. return;
  215. while (p) {
  216. /*
  217. ** all definitions must be reversed so that output will be that a
  218. ** unit will be defined before it is used.
  219. */
  220. if ((q = (struct reverse *)malloc( sizeof(struct reverse) ))
  221. == NULL)
  222. memerror();
  223. q->actual = p;
  224. q->next = head;
  225. head = q;
  226. p = p->t_link;
  227. }
  228. for (q = head; q; q = q->next) {
  229. fprintf( outfile, "static KEYSYM\t%s%u\t= {", name,
  230. q->actual->t_id );
  231. if (hashstore)
  232. if (q->actual->t_link)
  233. fprintf( outfile, "&%s%u,\"%s\",%u,%s", name,
  234. q->actual->t_link->t_id,
  235. q->actual->t_name,
  236. hash( q->actual->t_name ),
  237. q->actual->t_lex
  238. );
  239. else
  240. fprintf( outfile, "0,\"%s\",%u,%s",
  241. q->actual->t_name,
  242. hash( q->actual->t_name ),
  243. q->actual->t_lex
  244. );
  245. else if (q->actual->t_link)
  246. fprintf( outfile, "&%s%u,\"%s\",%s", name,
  247. q->actual->t_link->t_id, q->actual->t_name,
  248. q->actual->t_lex
  249. );
  250. else
  251. fprintf( outfile, "0,\"%s\",%s", q->actual->t_name,
  252. q->actual->t_lex
  253. );
  254. fprintf( outfile, "};\n" );
  255. }
  256. for (q = head; q; head = q) {
  257. q = q->next;
  258. free( head );
  259. }
  260. }
  261. /****************************************************************/
  262. /* */
  263. /* s_symbols : output the structure defining the */
  264. /* symbol table. */
  265. /* */
  266. /****************************************************************/
  267. s_symbols ( name, size )
  268. char *name;
  269. unsigned short size;
  270. {
  271. register unsigned short i;
  272. fprintf( outfile, "\nstatic KEYSYM FARSYM *%s_words[%u] = {\n", name,
  273. size );
  274. for (i = 0; i < size; i++)
  275. if (table[i])
  276. fprintf( outfile, "\t&%s%u%c\n", name, table[i]->t_id,
  277. ((i < (size - 1)) ? ',' : ' ') );
  278. else
  279. fprintf( outfile, "\t0%c\n",
  280. ((i < (size - 1)) ? ',' : ' ') );
  281. fprintf( outfile, "\t};\n" );
  282. }
  283. /****************************************************************/
  284. /* */
  285. /* s_header : output the header for the symbol table. */
  286. /* */
  287. /****************************************************************/
  288. s_header ( name, size )
  289. char *name;
  290. unsigned short size;
  291. {
  292. fprintf( outfile, "\nKEYWORDS %s_table = {%s_words,%u};\n\n\n",
  293. name, name, size );
  294. }
  295. static struct entry *head = NULL;
  296. /****************************************************************/
  297. /* */
  298. /* talloc -- allocate space for a table entry */
  299. /* */
  300. /****************************************************************/
  301. static struct entry *
  302. talloc ()
  303. {
  304. register struct entry *p;
  305. if (p = head) {
  306. head = head->t_link;
  307. return( p );
  308. }
  309. if ((p = (struct entry *)malloc( sizeof(struct entry) )))
  310. return( p );
  311. memerror();
  312. }
  313. /****************************************************************/
  314. /* */
  315. /* tfree -- free space for a table entry */
  316. /* */
  317. /****************************************************************/
  318. static
  319. tfree ( p )
  320. struct entry *p;
  321. {
  322. free( p->t_name );
  323. free( p->t_lex );
  324. p->t_link = head;
  325. head = p;
  326. }
  327. /****************************************************************/
  328. /* */
  329. /* memerr -- ran out of heap space; die */
  330. /* */
  331. /****************************************************************/
  332. memerror ()
  333. {
  334. fprintf( stderr, "Out of heap space\n" );
  335. exit( 1 );
  336. }
  337. #ifdef XENIX
  338. int _stricmp ( first, last )
  339. register char *first;
  340. register char *last;
  341. {
  342. register f;
  343. register l;
  344. do {
  345. if ((f = *first++) >= 'A' && f <= 'Z')
  346. f += 'a' - 'A';
  347. if ((l = *last++) >= 'A' && l <= 'Z')
  348. l += 'a' - 'A';
  349. } while (f && f == l);
  350. return( f - l );
  351. }
  352. #endif /* XENIX */