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.

592 lines
14 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. mapmsg.c
  5. Abstract:
  6. This utility will create an input file for MC from specially
  7. formatted include files. This is used to create DLL's which can be
  8. used by the message utilities to get message text to display.
  9. The format of the header files is:
  10. :
  11. :
  12. #define <basename> <basenumber>
  13. :
  14. :
  15. #define <errornum> <basenumber> + <number> /* text of message */
  16. /*
  17. Example:
  18. #define NETBASE 1000
  19. #define NerrFOO NETBASE+1 /* A FOO has been encountered at %1 * /
  20. /*
  21. The mapping tries to be generous about whitespace and parenthesis.
  22. It will also handle comments across several lines. Some important points:
  23. - all continuations must begin with [WS]'*'
  24. any whitespace at the beginning of a message is removed
  25. unless the -p command line option is specified.
  26. - #define .....
  27. /*
  28. * FOO
  29. */
  30. /* is handled correctly.
  31. The command line to MAPMSG is:
  32. mapmsg [-p] [-a appendfile] <system name> <basename> <inputfile>
  33. Example:
  34. mapmsg NET NERRBASE neterr.h > neterr.mc
  35. The <system name> is the 3 character name required by the mkmsg
  36. input. The output is written to stdout. If the append file
  37. is given, the output is appropriately appended to an existing
  38. mkmsgf source file.
  39. An optional @X, X: {E, W, I, P} can be the 1st non-WS chars of the
  40. comment field. The letter (E, W, I, or P) will be the message type.
  41. See MKMSGF documentation for an explaination of the message types.
  42. The default type is E.
  43. The @X must appear on the same line as the #define.
  44. Examples:
  45. #define NerrFOO NETBASE+1 /* @I A FOO has been encountered * /
  46. /*
  47. #define NERR_Foo NETBASE + 2 /* @P
  48. The prompt text: %0 */
  49. /*
  50. The resulting entry in the message file input file will be
  51. NETnnnnI: A FOO has been encountered
  52. Use the DOS message file source convention of XXXnnnn?: for
  53. placeholder messages.
  54. Author:
  55. This was ported from the Lanman utility that was used to create input
  56. files for mkmsgf by:
  57. Dan Hinsley (danhi) 29-Jul-1991
  58. Revision History:
  59. Ronald Meijer (ronaldm) 17-Mar-1993
  60. Added -p option to preserve leading white space characters
  61. --*/
  62. #include <windows.h>
  63. #include <stdio.h>
  64. #include <string.h>
  65. #include <ctype.h>
  66. #include <stdlib.h>
  67. #include "mapmsg.h"
  68. #define USAGE "syntax: mapmsg [-p] [-a appendfile] <system name> <basename> <inputfile>\n"
  69. int Append = FALSE; /* was the -a switch specified */
  70. int Preserve = FALSE; /* TRUE if the -p switch is set */
  71. int
  72. __cdecl main(
  73. int argc,
  74. PCHAR * argv
  75. )
  76. {
  77. int Base;
  78. // Check for -p[reserve whitespace] option
  79. if (argc > 1)
  80. {
  81. if (_stricmp(argv[1], "-p") == 0)
  82. {
  83. ++argv;
  84. --argc;
  85. Preserve = TRUE;
  86. }
  87. }
  88. if (argc == 6)
  89. {
  90. if (_stricmp(argv[1], "-a") != 0)
  91. {
  92. fprintf(stderr, USAGE);
  93. return(1);
  94. }
  95. if (freopen(argv[2], "r+", stdout) == NULL)
  96. {
  97. fprintf(stderr, "Cannot open '%s'\n", argv[2]);
  98. return(1);
  99. }
  100. argv += 2;
  101. argc -= 2;
  102. Append = TRUE;
  103. }
  104. /* check for valid command line */
  105. if (argc != 4)
  106. {
  107. fprintf(stderr, USAGE);
  108. return(1);
  109. }
  110. if (freopen(argv[3], "r", stdin) == NULL)
  111. {
  112. fprintf(stderr, "Cannot open '%s'\n", argv[3]);
  113. return(1);
  114. }
  115. if (GetBase(argv[2], &Base))
  116. {
  117. fprintf(stderr, "Cannot locate definition of <basename> in '%s'\n", argv[3]);
  118. return(1);
  119. }
  120. /* now process the rest of the file and map it */
  121. MapMessage(Base, argv[2]);
  122. return(0);
  123. }
  124. int
  125. GetBase(
  126. PCHAR String,
  127. int * pBase
  128. )
  129. /*++
  130. Routine Description:
  131. GetBase - find the line defining the value of the base number.
  132. Arguments:
  133. String is the string to match.
  134. pBase is a pointer of where to put the value.
  135. Return Value:
  136. Return 0 if string found, 1 if not.
  137. Notes:
  138. The global variable, chBuff is used w/in this routine.
  139. The pattern to look for is:
  140. [WS] #define [WS] <string> [WS | '('] <number> .....
  141. --*/
  142. {
  143. PCHAR p;
  144. size_t len;
  145. len = strlen(String);
  146. while(fgets(chBuff, sizeof(chBuff), stdin))
  147. {
  148. p = chBuff;
  149. SKIPWHITE(p);
  150. if (strncmp(p, "#define", 7) == 0)
  151. {
  152. p += 7;
  153. SKIPWHITE(p);
  154. if (strncmp(String, p, len) == 0 && strcspn(p, " \t") == len)
  155. {
  156. /* found the definition ... skip to number */
  157. p += len;
  158. SKIP_W_P(p);
  159. if ( !isdigit(*p))
  160. {
  161. ReportError(chBuff, "Bad <base> definition");
  162. }
  163. *pBase = atoi(p);
  164. return(0);
  165. }
  166. }
  167. }
  168. return(1);
  169. }
  170. VOID
  171. MapMessage(
  172. int Base,
  173. PCHAR BaseName
  174. )
  175. /*++
  176. Routine Description:
  177. MapMessage - map the definition lines.
  178. Arguments:
  179. Base is the base number
  180. BaseName is the text form of base
  181. Return Value:
  182. None
  183. Notes:
  184. The global variable, chBuff is used w/in this routine.
  185. Make sure that the numbers are strictly increasing.
  186. --*/
  187. {
  188. CHAR auxbuff[BUFSIZ];
  189. int num;
  190. int first = TRUE;
  191. int next;
  192. PCHAR text;
  193. CHAR define[41];
  194. PCHAR p;
  195. CHAR type;
  196. /* Make certain the buffer is always null-terminated */
  197. define[sizeof(define)-1] = '\0';
  198. /* print the header */
  199. if (!Append)
  200. {
  201. printf(";//\n");
  202. printf(";// Net error file for basename %s = %d\n", BaseName, Base);
  203. printf(";//\n");
  204. }
  205. else
  206. {
  207. /* get last number and position to end of file */
  208. first = FALSE;
  209. next = 0;
  210. if (fseek(stdout, 0L, SEEK_END) == -1) {
  211. return;
  212. }
  213. }
  214. /* for each line of the proper format */
  215. while (GetNextLine(BaseName, chBuff, define, &num, &text, &type))
  216. {
  217. num += Base;
  218. if (first)
  219. {
  220. first = FALSE;
  221. next = num;
  222. }
  223. /* make sure that the numbers are monotonically increasing */
  224. if (num > next)
  225. {
  226. if (next == num - 1)
  227. {
  228. fprintf(stderr, "(warning) Missing error number %d\n", next);
  229. }
  230. else
  231. {
  232. fprintf(stderr, "(warning) Missing error numbers %d - %d\n",
  233. next, num-1);
  234. }
  235. next = num;
  236. }
  237. else if (num < next)
  238. {
  239. ReportError(chBuff, "Error numbers not strictly increasing");
  240. }
  241. /* rule out comment start alone on def line */
  242. if (text && *text == 0)
  243. {
  244. ReportError(chBuff, "Bad comment format");
  245. }
  246. /*
  247. * catch the cases where there is no open comment
  248. * or the open comment just contains a @X
  249. */
  250. if (text == NULL)
  251. {
  252. text = fgets(auxbuff, sizeof(auxbuff), stdin);
  253. if (!text) {
  254. ReportError(chBuff, "Bad comment format");
  255. }
  256. SKIPWHITE(text);
  257. if ((type == '\0') && (strncmp(text, "/*", 2) == 0))
  258. {
  259. if (text[2] == 0)
  260. {
  261. if (!fgets(auxbuff, sizeof(auxbuff), stdin)) {
  262. ReportError(chBuff, "Bad comment format");
  263. }
  264. }
  265. else
  266. {
  267. text += 1;
  268. }
  269. strncpy(chBuff, text, (sizeof(chBuff)/sizeof(chBuff[0]))-1);
  270. text = chBuff;
  271. SKIPWHITE(text);
  272. if (*text++ != '*')
  273. {
  274. ReportError(chBuff, "Comment continuation requires '*'");
  275. }
  276. }
  277. else if ((type) && (*text == '*'))
  278. {
  279. if (text[1] == 0)
  280. {
  281. if (!fgets(auxbuff, sizeof(auxbuff), stdin)) {
  282. ReportError(chBuff, "Bad comment format");
  283. }
  284. }
  285. strncpy(chBuff, text, (sizeof(chBuff)/sizeof(chBuff[0]))-1);
  286. text = chBuff;
  287. SKIPWHITE(text);
  288. if (*text++ != '*')
  289. {
  290. ReportError(chBuff, "Comment continuation requires '*'");
  291. }
  292. }
  293. else
  294. {
  295. ReportError(chBuff, "Bad comment format");
  296. }
  297. }
  298. /* Strip off trailing trailing close comment */
  299. while (strstr(text, "*/") == NULL)
  300. {
  301. /* multi-line message ... comment MUST
  302. * be continued with '*'
  303. */
  304. p = fgets(auxbuff, sizeof(auxbuff), stdin);
  305. if (!p) {
  306. ReportError( chBuff, "invalid comment\n");
  307. }
  308. SKIPWHITE(p);
  309. if (*p != '*')
  310. {
  311. ReportError(auxbuff, "Comment continuation requires '*'");
  312. }
  313. if (*++p == '/')
  314. {
  315. break;
  316. }
  317. // abort if the current text length + add text + "\n" is > the max
  318. if (strlen(text) + strlen(p) + 1 > MAXMSGTEXTLEN)
  319. {
  320. ReportError(text, "\nMessage text length too long");
  321. }
  322. strcat(text, "\n");
  323. //
  324. // Get rid of leading spaces on continuation line,
  325. // unless -p specified
  326. //
  327. if (!Preserve)
  328. {
  329. SKIPWHITE(p);
  330. }
  331. strcat(text, p);
  332. }
  333. if ((p=strstr(text, "*/")) != NULL)
  334. {
  335. *p = 0;
  336. }
  337. TrimTrailingSpaces(text);
  338. //
  339. // Get rid of leading spaces on first line, unless -p specified
  340. //
  341. p = text;
  342. if (!Preserve) {
  343. SKIPWHITE(p);
  344. if (!p) {
  345. p = text;
  346. }
  347. }
  348. printf("MessageId=%04d SymbolicName=%s\nLanguage=English\n"
  349. "%s\n.\n", num, define, p);
  350. ++next;
  351. }
  352. }
  353. int
  354. GetNextLine(
  355. PCHAR BaseName,
  356. PCHAR pInputBuffer,
  357. PCHAR pDefineName,
  358. int * pNumber,
  359. PCHAR * pText,
  360. PCHAR pType
  361. )
  362. /*++
  363. Routine Description:
  364. GetNextLine - get the next line of the proper format, and parse out
  365. the error number.
  366. The format is assumed to be:
  367. [WS] #define [WS] <name> [WS | '('] <basename> [WS | ')'] \
  368. '+' [WS | '('] <number> [WS | ')'] '/*' [WS] [@X] [WS] <text>
  369. Arguments:
  370. BaseName is the basename.
  371. pInputBuffer is a pointer to an input buffer
  372. pDefineName is a pointer to where the manifest constant name pointer goes
  373. pNumber is a pointer to where the <number> goes.
  374. pText is a pointer to where the text pointer goes.
  375. pType is a pointer to the message type (set to 0 if no @X on line).
  376. Return Value:
  377. Returns 0 at end of file, non-zero otherwise.
  378. --*/
  379. {
  380. size_t len = strlen(BaseName);
  381. PCHAR savep = pInputBuffer;
  382. PCHAR startdefine;
  383. while (gets(savep))
  384. {
  385. pInputBuffer = savep;
  386. SKIPWHITE(pInputBuffer);
  387. if (strncmp(pInputBuffer, "#define", 7) == 0)
  388. {
  389. pInputBuffer += 7;
  390. SKIPWHITE(pInputBuffer);
  391. /* get manifest constant name */
  392. startdefine = pInputBuffer;
  393. pInputBuffer += strcspn(pInputBuffer, " \t");
  394. *pInputBuffer = '\0';
  395. pInputBuffer++;
  396. strncpy(pDefineName, startdefine, 40);
  397. SKIP_W_P(pInputBuffer);
  398. /* match <basename?> */
  399. if (strncmp(BaseName, pInputBuffer, len) == 0 &&
  400. strcspn(pInputBuffer, " \t)+") == len)
  401. {
  402. pInputBuffer += len;
  403. SKIP_W_P(pInputBuffer);
  404. if (*pInputBuffer == '+')
  405. {
  406. ++pInputBuffer;
  407. SKIP_W_P(pInputBuffer);
  408. /* the number !! */
  409. if (!isdigit(*pInputBuffer))
  410. {
  411. ReportError(savep, "Bad error file format");
  412. }
  413. *pNumber = atoi(pInputBuffer);
  414. SKIP_NOT_W_P(pInputBuffer);
  415. SKIP_W_P(pInputBuffer);
  416. if (strncmp(pInputBuffer, "/*", 2))
  417. {
  418. *pText = NULL;
  419. *pType = '\0';
  420. return(1);
  421. }
  422. pInputBuffer += 2;
  423. SKIPWHITE(pInputBuffer);
  424. if (*pInputBuffer == '@')
  425. {
  426. *pType = *(pInputBuffer+1);
  427. pInputBuffer += 2;
  428. SKIPWHITE(pInputBuffer);
  429. }
  430. else
  431. {
  432. *pType = '\0';
  433. }
  434. if (*pInputBuffer)
  435. {
  436. *pText = pInputBuffer;
  437. }
  438. else
  439. {
  440. *pText = NULL;
  441. }
  442. return(1);
  443. }
  444. }
  445. }
  446. }
  447. return(0);
  448. }
  449. void
  450. ReportError(
  451. PCHAR pLineNumber,
  452. PCHAR Message
  453. )
  454. /*++
  455. Routine Description:
  456. ReportError - report a fatal error.
  457. Arguments:
  458. pLineNumber is the offending input line.
  459. Message is a description of what is wrong.
  460. Return Value:
  461. None
  462. --*/
  463. {
  464. fprintf(stderr, "\a%s:%s\n", Message, pLineNumber);
  465. exit(1);
  466. }
  467. void
  468. TrimTrailingSpaces(
  469. PCHAR Text
  470. )
  471. /*++
  472. Routine Description:
  473. TrimTrailingSpaces - strip off the end spaces.
  474. Arguments:
  475. Text - the text to remove spaces from
  476. Return Value:
  477. None
  478. --*/
  479. {
  480. PCHAR p;
  481. /* strip off trailing space */
  482. while (((p=strrchr(Text, ' ')) && p[1] == 0) ||
  483. ((p=strrchr(Text, '\t')) && p[1] == 0))
  484. {
  485. *p = 0;
  486. }
  487. }