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.

337 lines
9.9 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. listmung.c
  5. Abstract:
  6. This is the main module for a stubfile generation utility
  7. Author:
  8. Sanford Staab (sanfords) 22-Apr-1992
  9. Revision History:
  10. --*/
  11. #include <assert.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <windef.h>
  17. #define STRING_BUFFER_SIZE 120
  18. char StringBuffer[STRING_BUFFER_SIZE];
  19. char ItemBuffer[STRING_BUFFER_SIZE];
  20. char ItemBuffer2[STRING_BUFFER_SIZE];
  21. char *ListName, *TemplateName;
  22. FILE *ListFile, *TemplateFile;
  23. char szBEGINTRANSLATE[] = "BeginTranslate";
  24. char szENDTRANSLATE[] = "EndTranslate";
  25. char szENDTRANSLATEQLPC[] = "EndTranslateQLPC";
  26. BOOL IsTranslateTag(char * pBuffer)
  27. {
  28. return (_strnicmp(pBuffer, szBEGINTRANSLATE, sizeof(szBEGINTRANSLATE)-1) == 0)
  29. || (_strnicmp(pBuffer, szENDTRANSLATE, sizeof(szENDTRANSLATE)-1) == 0);
  30. }
  31. BOOL IsCommentOrTag(char * pBuffer)
  32. {
  33. return ((*pBuffer == ';') || IsTranslateTag(pBuffer));
  34. }
  35. int
  36. ProcessParameters(
  37. int argc,
  38. char *argv[]
  39. )
  40. {
  41. char c, *p;
  42. while (*++argv != NULL) {
  43. p = *argv;
  44. //
  45. // if we have a delimiter for a parameter, case throught the valid
  46. // parameter. Otherwise, the rest of the parameters are the list of
  47. // input files.
  48. //
  49. if (*p == '/' || *p == '-') {
  50. //
  51. // Switch on all the valid delimiters. If we don't get a valid
  52. // one, return with an error.
  53. //
  54. c = *++p;
  55. switch (toupper( c )) {
  56. default:
  57. return 0;
  58. }
  59. } else {
  60. ListName = *argv++;
  61. TemplateName = *argv++;
  62. return (ListName && TemplateName);
  63. }
  64. }
  65. return 0;
  66. }
  67. BOOL mysubstr(
  68. char *s,
  69. char *find,
  70. char *put)
  71. {
  72. char *p;
  73. if (p = strstr(s, find)) {
  74. strcpy(p, put);
  75. strcpy(p + strlen(put), p + strlen(find)); // find > put!
  76. return(TRUE);
  77. }
  78. return(FALSE);
  79. }
  80. VOID myprint(
  81. char *s,
  82. char *item,
  83. int index)
  84. {
  85. if (strstr(s, "%d") || mysubstr(s, "%%INDEX%%", "%d")) {
  86. printf(s, item, index);
  87. } else {
  88. printf(s, item);
  89. }
  90. }
  91. //+---------------------------------------------------------------------------
  92. //
  93. // Function: myfgets
  94. //
  95. // Synopsis: Calls fgets to read a string from a file.
  96. // It ignores empty lines or lines starting with a blank character
  97. //
  98. // This is needed when the input file was generated by the
  99. // compiler preprocessor. Using the preprocessor allows us
  100. // to use #idfdef, #inlcude, etc in the original list file.
  101. //
  102. // Arguments: [pszBuff] -- Buffer to store the string
  103. // [iBuffSize] -- Buffer size
  104. // [pFile] -- Pointer to the file to read
  105. //
  106. // Return [pszRet] -- Pointer to pszBuff if succesful read. NULL otherwise.
  107. //----------------------------------------------------------------------------
  108. char * myfgets(
  109. char * pszBuff,
  110. int iBuffSize,
  111. FILE * pFile)
  112. {
  113. char *pszRet;
  114. while (pszRet = fgets(pszBuff, iBuffSize, pFile)) {
  115. if ((*pszRet == '\n') || (*pszRet == ' ')) {
  116. continue;
  117. } else {
  118. break;
  119. }
  120. }
  121. return pszRet;
  122. }
  123. //+---------------------------------------------------------------------------
  124. //
  125. // Function: SkipCommentsAndTags
  126. //
  127. // Synopsis: Calls myfgets to read a string from a file.
  128. // It ignores lines starting with ; (ie, comments) and lines
  129. // containing the Begin/EndTranslate tags.
  130. //
  131. // Arguments: [pszBuff] -- Buffer to store the string
  132. // [iBuffSize] -- Buffer size
  133. // [pFile] -- Pointer to the file to read
  134. //
  135. // Return [pszRet] -- Pointer to pszBuff if succesful read. NULL otherwise.
  136. //----------------------------------------------------------------------------
  137. char * SkipCommentsAndTags(
  138. char * pszBuff,
  139. int iBuffSize,
  140. FILE * pFile)
  141. {
  142. char *pszRet;
  143. while (pszRet = myfgets(pszBuff, iBuffSize, pFile)) {
  144. if (IsCommentOrTag(pszRet)) {
  145. continue;
  146. } else {
  147. break;
  148. }
  149. }
  150. return pszRet;
  151. }
  152. void
  153. ProcessTemplate( void )
  154. {
  155. char *s;
  156. char *pchItem;
  157. char *pchLastItem;
  158. int index;
  159. s = fgets(StringBuffer,STRING_BUFFER_SIZE,TemplateFile);
  160. while ( s ) {
  161. if (mysubstr(s, "%%FOR_ALL_UPPER%%", "%-45s")) {
  162. rewind(ListFile);
  163. index = 0;
  164. while (pchItem = myfgets(ItemBuffer, STRING_BUFFER_SIZE, ListFile)) {
  165. if (ItemBuffer[0] != ';') {
  166. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  167. pchItem = _strupr(pchItem);
  168. if (IsTranslateTag(ItemBuffer)) {
  169. myprint(s, pchItem, index);
  170. } else {
  171. myprint(s, pchItem, index++);
  172. }
  173. } else {
  174. printf("// %s", ItemBuffer);
  175. }
  176. }
  177. } else if (mysubstr(s, "%%FOR_ALL%%", "%s")) {
  178. rewind(ListFile);
  179. index = 0;
  180. while (pchItem = myfgets(ItemBuffer, STRING_BUFFER_SIZE, ListFile)) {
  181. if (ItemBuffer[0] != ';') {
  182. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  183. myprint(s, pchItem, index++);
  184. }
  185. }
  186. } else if (mysubstr(s, "%%FOR_ALL_QLPC%%", "%s")) {
  187. rewind(ListFile);
  188. index = 0;
  189. while (pchItem = myfgets(ItemBuffer, STRING_BUFFER_SIZE, ListFile)) {
  190. if (ItemBuffer[0] != ';') {
  191. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  192. if (_strnicmp(ItemBuffer, szENDTRANSLATEQLPC, sizeof(szENDTRANSLATEQLPC)-1) == 0)
  193. break;
  194. myprint(s, pchItem, index++);
  195. }
  196. }
  197. } else if (mysubstr(s, "%%FOR_ALL_LPC%%", "%s")) {
  198. rewind(ListFile);
  199. index = 0;
  200. while (pchItem = myfgets(ItemBuffer, STRING_BUFFER_SIZE, ListFile)) {
  201. if (ItemBuffer[0] != ';') {
  202. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  203. if (_strnicmp(ItemBuffer, szENDTRANSLATEQLPC, sizeof(szENDTRANSLATEQLPC)-1) == 0)
  204. break;
  205. }
  206. }
  207. while (pchItem = myfgets(ItemBuffer, STRING_BUFFER_SIZE, ListFile)) {
  208. if (ItemBuffer[0] != ';') {
  209. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  210. myprint(s, pchItem, index++);
  211. }
  212. }
  213. } else if (mysubstr(s, "%%FOR_ALL_BUT_LAST%%", "%s")) {
  214. rewind(ListFile);
  215. index = 0;
  216. pchLastItem = SkipCommentsAndTags(ItemBuffer, STRING_BUFFER_SIZE, ListFile);
  217. if (pchLastItem != NULL) {
  218. pchLastItem[strlen(pchLastItem) - 1] = '\0'; // strip off \n
  219. while (pchItem = myfgets(ItemBuffer2, STRING_BUFFER_SIZE, ListFile)) {
  220. if (!IsCommentOrTag(pchItem)) {
  221. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  222. myprint(s, pchLastItem, index++); // Write previous line
  223. strcpy(pchLastItem, pchItem); // Save current line
  224. }
  225. }
  226. } else {
  227. fprintf(stderr,"LISTMUNG: FOR_ALL_BUT_LAST: no lines found\n");
  228. }
  229. } else if (mysubstr(s, "%%FOR_LAST%%", "%s")) {
  230. rewind(ListFile);
  231. index = 0;
  232. pchLastItem = SkipCommentsAndTags(ItemBuffer, STRING_BUFFER_SIZE, ListFile);
  233. if (pchLastItem != NULL) {
  234. pchLastItem[strlen(pchLastItem) - 1] = '\0'; // strip off \n
  235. while (pchItem = myfgets(ItemBuffer2, STRING_BUFFER_SIZE, ListFile)) {
  236. if (!IsCommentOrTag(pchItem)) {
  237. pchItem[strlen(pchItem) - 1] = '\0'; // strip off \n
  238. strcpy(pchLastItem, pchItem); // Save current line
  239. index++;
  240. }
  241. }
  242. myprint(s, pchLastItem, index); // Write Last line.
  243. } else {
  244. fprintf(stderr,"LISTMUNG: FOR_LAST: no lines found\n");
  245. }
  246. } else {
  247. printf("%s", s);
  248. }
  249. s = fgets(StringBuffer,STRING_BUFFER_SIZE,TemplateFile);
  250. }
  251. }
  252. int
  253. __cdecl main( argc, argv )
  254. int argc;
  255. char *argv[];
  256. {
  257. if (!ProcessParameters( argc, argv )) {
  258. fprintf( stderr, "Stub File Generation Utility. Version: 1.1\n" );
  259. fprintf( stderr, "usage: listmung <symbol_list_file> <template>\n" );
  260. fprintf( stderr, " Converts the elements in the list file into an output file\n" );
  261. fprintf( stderr, " where the template dictates the format. The following strings\n");
  262. fprintf( stderr, " are substituted apropriately:\n");
  263. fprintf( stderr, " %%FOR_ALL%%\n");
  264. fprintf( stderr, " %%FOR_ALL_UPPER%%\n");
  265. fprintf( stderr, " %%FOR_ALL_BUT_LAST%%\n");
  266. fprintf( stderr, " %%FOR_LAST%%\n");
  267. fprintf( stderr, " %%INDEX%%\n");
  268. fprintf( stderr, " output is to stdout.\n");
  269. return 1;
  270. }
  271. if ( (ListFile = fopen(ListName,"r")) == 0) {
  272. fprintf(stderr,"LISTMUNG: Unable to open list file.\n");
  273. return 1;
  274. }
  275. if ( (TemplateFile = fopen(TemplateName,"r")) == 0) {
  276. fprintf(stderr,"LISTMUNG: Unable to open template file.\n");
  277. return 1;
  278. }
  279. ProcessTemplate();
  280. fclose(ListFile);
  281. fclose(TemplateFile);
  282. return( 0 );
  283. }