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.

404 lines
12 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. 11/15/94 Sanofrds: rewrote and expanded - but its not quite right yet.
  10. since this took me a day to do, I'm leaving it here in case
  11. anyone has to expand or alter it later - this version is
  12. easier to maintain.
  13. Revision History:
  14. --*/
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <windef.h>
  21. char szIndex[] = "$$INDEX$$";
  22. char szForAllLPC[] = "$$FOR_ALL_LPC$$";
  23. char szForAllQLPC[] = "$$FOR_ALL_QLPC$$";
  24. char szForAll[] = "$$FOR_ALL$$";
  25. char szForAll45[] = "$$FOR_ALL45$$";
  26. char szForAllButLast[] = "$$FOR_ALL_BUT_LAST$$";
  27. char szForLast[] = "$$FOR_LAST$$";
  28. char szForAllUpper[] = "$$FOR_ALL_UPPER$$";
  29. char szForAllUpper45[] = "$$FOR_ALL_UPPER45$$";
  30. char szCheck[] = "$$";
  31. char *aKeyStrings[] = {
  32. szIndex,
  33. szForAllLPC,
  34. szForAllQLPC,
  35. szForAll,
  36. szForAll45,
  37. szForAllButLast,
  38. szForLast,
  39. szForAllUpper,
  40. szForAllUpper45,
  41. NULL
  42. };
  43. #define STRING_BUFFER_SIZE 120
  44. char ItemBuffer[STRING_BUFFER_SIZE];
  45. char ItemBufferNext[STRING_BUFFER_SIZE];
  46. char *ListName, *TemplateName;
  47. FILE *ListFile, *TemplateFile;
  48. char szENDTRANSLATE[] = "EndTranslate";
  49. char szENDTRANSLATEQLPC[] = "EndTranslateQLPC";
  50. BOOL mysubstr(
  51. char *s,
  52. char *find,
  53. char *put)
  54. {
  55. char *p;
  56. BOOL fRet = FALSE;
  57. /*
  58. * assumes strlen(put) < strlen(find) !
  59. */
  60. while (p = strstr(s, find)) {
  61. strcpy(p, put);
  62. s = p + strlen(put);
  63. strcpy(s, p + strlen(find)); // find > put!
  64. fRet = TRUE;
  65. }
  66. return(fRet);
  67. }
  68. int
  69. ProcessParameters(
  70. int argc,
  71. char *argv[]
  72. )
  73. {
  74. char c, *p;
  75. while (*++argv != NULL) {
  76. p = *argv;
  77. //
  78. // if we have a delimiter for a parameter, case throught the valid
  79. // parameter. Otherwise, the rest of the parameters are the list of
  80. // input files.
  81. //
  82. if (*p == '/' || *p == '-') {
  83. //
  84. // Switch on all the valid delimiters. If we don't get a valid
  85. // one, return with an error.
  86. //
  87. c = *++p;
  88. switch (toupper( c )) {
  89. case 'p':
  90. {
  91. char **ppsz;
  92. /*
  93. * alter key strings to use a different delimiter
  94. * so that multiple passes of listmung are possible.
  95. */
  96. ppsz = aKeyStrings;
  97. szCheck[0] = *++p;
  98. szCheck[1] = szCheck[0];
  99. while (*ppsz != NULL) {
  100. mysubstr(*ppsz++, "$$", szCheck);
  101. }
  102. }
  103. default:
  104. return 0;
  105. }
  106. } else {
  107. ListName = *argv++;
  108. TemplateName = *argv++;
  109. return (ListName && TemplateName);
  110. }
  111. }
  112. return 0;
  113. }
  114. void
  115. ProcessTemplate( void )
  116. {
  117. char *pszTemplateLine, *pszTemplateLineNext;
  118. char szBuf[STRING_BUFFER_SIZE];
  119. char szTemplateLine[STRING_BUFFER_SIZE];
  120. char szTemplateLineSave[STRING_BUFFER_SIZE];
  121. char *pchItem, *pchItemNext;
  122. char *pchLastItem;
  123. int index;
  124. BOOL fLPCZone;
  125. pszTemplateLine = fgets(szTemplateLine, STRING_BUFFER_SIZE, TemplateFile);
  126. while (pszTemplateLine ) {
  127. /*
  128. * for each line of the template...
  129. */
  130. if (strstr(pszTemplateLine, szCheck) == NULL) {
  131. /*
  132. * This line of the template has no $$ substitution strings
  133. * so just print it and go to the next.
  134. */
  135. printf(pszTemplateLine);
  136. pszTemplateLine = fgets(szTemplateLine, STRING_BUFFER_SIZE, TemplateFile);
  137. continue;
  138. }
  139. /*
  140. * remember original template line form so we can reuse it for each
  141. * item in the list.
  142. */
  143. strcpy(szTemplateLineSave, szTemplateLine);
  144. rewind(ListFile);
  145. index = 0;
  146. fLPCZone = FALSE;
  147. pchItem = pchItemNext = fgets(ItemBufferNext, STRING_BUFFER_SIZE, ListFile);
  148. while (pchItem) {
  149. /*
  150. * we look ahead by one line so we know which one is the last one.
  151. */
  152. strcpy(ItemBuffer, ItemBufferNext);
  153. pchItem = pchItemNext;
  154. pchItemNext = fgets(ItemBufferNext, STRING_BUFFER_SIZE, ListFile);
  155. /*
  156. * restore the original template line.
  157. */
  158. strcpy(szTemplateLine, szTemplateLineSave);
  159. /*
  160. * strip off the '\n'
  161. */
  162. ItemBuffer[strlen(ItemBuffer) - 1] = '\0';
  163. /*
  164. * if this line is a comment, translate and print it as is.
  165. * Don't increment the index.
  166. */
  167. if (ItemBuffer[0] == ';') {
  168. if (ItemBuffer[1] != ';') {
  169. printf("//%s\n", ItemBuffer);
  170. }
  171. continue; // on to next template item.
  172. }
  173. /*
  174. * If this template line contains a $$FOR_ALL_LPC$$ tag,
  175. * only process it if we are in the LPCZone.
  176. */
  177. if (mysubstr(pszTemplateLine, szForAllLPC, "%s")) {
  178. if (fLPCZone) {
  179. /*
  180. * replace up to 4 instances.
  181. */
  182. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  183. strcpy(pszTemplateLine, szBuf);
  184. } else {
  185. break; // skip this template line and go to the next.
  186. }
  187. }
  188. /*
  189. * If this template line contains a $$FOR_ALL_QLPC$$ tag,
  190. * only process it if we are not in the LPCZone.
  191. */
  192. if (mysubstr(pszTemplateLine, szForAllQLPC, "%s")) {
  193. if (!fLPCZone) {
  194. /*
  195. * replace up to 4 instances.
  196. */
  197. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  198. strcpy(pszTemplateLine, szBuf);
  199. } else {
  200. break; // skip this template line and go to the next.
  201. }
  202. }
  203. /*
  204. * replace up to 4 instances of $$INDEX$$ in the template
  205. * with the current index value.
  206. */
  207. if (mysubstr(pszTemplateLine, szIndex, "%d")) {
  208. sprintf(szBuf, pszTemplateLine, index, index, index);
  209. strcpy(pszTemplateLine, szBuf);
  210. }
  211. /*
  212. * Increment the index value IFF this line does not contain
  213. * an ENDTRANSLATE. (Which includes ENDTRANSLATEQLPC)
  214. */
  215. if (strnicmp(ItemBuffer, szENDTRANSLATE, sizeof(szENDTRANSLATE)-1) != 0) {
  216. index++;
  217. }
  218. /*
  219. * if this is a QLPC marker, note that we are now in the LPC zone
  220. * of the list. ie items before the QLPC marker are in the QLPC
  221. * zone, items after the QLPC marker are in the LPC zone.
  222. */
  223. if (strnicmp(ItemBuffer, szENDTRANSLATEQLPC, sizeof(szENDTRANSLATEQLPC)-1) == 0) {
  224. fLPCZone = TRUE;
  225. }
  226. /*
  227. * replace up to 4 instances of $$FOR_ALL$$ in the template
  228. * with the current list item.
  229. */
  230. if (mysubstr(pszTemplateLine, szForAll, "%s")) {
  231. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  232. strcpy(pszTemplateLine, szBuf);
  233. }
  234. /*
  235. * replace up to 4 instances of $$FOR_ALL45$$ in the template
  236. * with the current list item.
  237. */
  238. if (mysubstr(pszTemplateLine, szForAll45, "%-45s")) {
  239. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  240. strcpy(pszTemplateLine, szBuf);
  241. }
  242. if (mysubstr(pszTemplateLine, szForAllButLast, "%s")) {
  243. if (pchItemNext != NULL) {
  244. /*
  245. * replace up to 4 instances of $$FOR_ALL_BUT_LAST$$ in
  246. * the template with the current list item.
  247. */
  248. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  249. strcpy(pszTemplateLine, szBuf);
  250. } else {
  251. /*
  252. * This template line does not apply to the last item
  253. * so go on to the next template line.
  254. */
  255. break;
  256. }
  257. }
  258. if (mysubstr(pszTemplateLine, szForLast, "%s")) {
  259. if (pchItemNext == NULL) {
  260. /*
  261. * replace up to 4 instances of $$FOR_LAST$$ in
  262. * the template with the current list item.
  263. */
  264. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  265. strcpy(pszTemplateLine, szBuf);
  266. pchItem = NULL;
  267. } else {
  268. /*
  269. * This item does not apply to anything but the last line,
  270. * so don't output this line, just continue to the next
  271. * item till we find the last one.
  272. */
  273. continue;
  274. }
  275. }
  276. /*
  277. * replace up to 4 instances of $$FOR_ALL_UPPER$$ in the template
  278. * with the current list item capitalized.
  279. */
  280. if (mysubstr(pszTemplateLine, szForAllUpper, "%s")) {
  281. strupr(ItemBuffer);
  282. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  283. strcpy(pszTemplateLine, szBuf);
  284. }
  285. /*
  286. * replace up to 4 instances of $$FOR_ALL_UPPER45$$ in the template
  287. * with the current list item capitalized.
  288. */
  289. if (mysubstr(pszTemplateLine, szForAllUpper45, "%-45s")) {
  290. strupr(ItemBuffer);
  291. sprintf(szBuf, pszTemplateLine, ItemBuffer, ItemBuffer, ItemBuffer, ItemBuffer);
  292. strcpy(pszTemplateLine, szBuf);
  293. }
  294. /*
  295. * print final form.
  296. */
  297. printf("%s", pszTemplateLine);
  298. }
  299. pszTemplateLine = fgets(szTemplateLine, STRING_BUFFER_SIZE, TemplateFile);
  300. }
  301. }
  302. int
  303. _CRTAPI1 main( argc, argv )
  304. int argc;
  305. char *argv[];
  306. {
  307. char **ppsz;
  308. if (!ProcessParameters( argc, argv )) {
  309. fprintf( stderr, "usage: listmung [-p<char>] <symbol_list_file> <template>\n" );
  310. fprintf( stderr, " Converts the elements in the list file into an output file\n" );
  311. fprintf( stderr, " where the template dictates the format. Up to 4 instances of\n");
  312. fprintf( stderr, " each of the following strings are substituted apropriately:\n");
  313. ppsz = aKeyStrings;
  314. while (*ppsz != NULL) {
  315. fprintf( stderr, " %s\n", *ppsz++);
  316. }
  317. fprintf( stderr, " -p<char> substitutes <char> for $ in the previous strings.\n");
  318. fprintf( stderr, " \"EndTranslate\" marks the end of a section\n");
  319. fprintf( stderr, " \"EndTranslateQLPC\" marks the QLPC-LPC zone boundary.\n");
  320. fprintf( stderr, " ; is a list comment and will be prepended by //.\n");
  321. fprintf( stderr, " ;; is a list comment that will not be output.\n");
  322. fprintf( stderr, " output is to stdout.\n");
  323. return 1;
  324. }
  325. if ( (ListFile = fopen(ListName,"r")) == 0) {
  326. fprintf(stderr,"LISTMUNG: Unable to open list file %s.\n", ListName);
  327. return 1;
  328. }
  329. if ( (TemplateFile = fopen(TemplateName,"r")) == 0) {
  330. fprintf(stderr,"LISTMUNG: Unable to open template file %s.\n", TemplateName);
  331. return 1;
  332. }
  333. ProcessTemplate();
  334. fclose(ListFile);
  335. fclose(TemplateFile);
  336. return( 0 );
  337. }
  338.