Source code of Windows XP (NT5)
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.

626 lines
15 KiB

  1. /*++
  2. Copyright (c) 1991-1999, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. nlstrans.c
  5. Abstract:
  6. This file contains the main function, the main parsing functions, and
  7. all helper functions used by the various modules.
  8. External Routines in this file:
  9. MAIN
  10. GetSize
  11. Revision History:
  12. 06-14-91 JulieB Created.
  13. --*/
  14. //
  15. // Include Files.
  16. //
  17. #include <stdio.h>
  18. #include "nlstrans.h"
  19. //
  20. // Global Variables.
  21. //
  22. FILE *pInputFile; // pointer to Input File
  23. BOOL Verbose = 0; // verbose flag
  24. BOOL NLSPlus = 0; // To indicate the file should be generated in NLS+ format.
  25. WCHAR FileExtension[32]; // The file extension for generated files. It can be ".nls" or ".nlp"
  26. //
  27. // Forward Declarations.
  28. //
  29. int
  30. ParseCmdLine(
  31. int argc,
  32. char *argv[]);
  33. void InitGlobals();
  34. int
  35. ParseInputFile(void);
  36. int
  37. GetKeyParam(
  38. PSZ pszParam);
  39. //-------------------------------------------------------------------------//
  40. // MAIN ROUTINE //
  41. //-------------------------------------------------------------------------//
  42. ////////////////////////////////////////////////////////////////////////////
  43. //
  44. // MAIN
  45. //
  46. // Main Routine.
  47. //
  48. // 07-30-91 JulieB Created.
  49. // 12-10-91 JulieB Modified for new table format.
  50. ////////////////////////////////////////////////////////////////////////////
  51. int __cdecl main(
  52. int argc,
  53. char *argv[])
  54. {
  55. //
  56. // Parse the command line.
  57. // Open input file.
  58. //
  59. if (ParseCmdLine(argc, argv))
  60. {
  61. return (1);
  62. }
  63. InitGlobals();
  64. //
  65. // Parse the input file.
  66. // Close the input file.
  67. //
  68. if (ParseInputFile())
  69. {
  70. fclose(pInputFile);
  71. return (1);
  72. }
  73. fclose(pInputFile);
  74. //
  75. // Return success.
  76. //
  77. return (0);
  78. }
  79. //-------------------------------------------------------------------------//
  80. // INTERNAL ROUTINES //
  81. //-------------------------------------------------------------------------//
  82. ////////////////////////////////////////////////////////////////////////////
  83. //
  84. // ParseCmdLine
  85. //
  86. // This routine parses the command line.
  87. //
  88. // 07-30-91 JulieB Created.
  89. // 12-10-91 JulieB Modified for new table format.
  90. ////////////////////////////////////////////////////////////////////////////
  91. int ParseCmdLine(
  92. int argc,
  93. char *argv[])
  94. {
  95. int Pos = 1; // position of argument
  96. //
  97. // Check for correct number of arguments.
  98. //
  99. if (argc < 2)
  100. {
  101. printf("Usage: nlstrans [-v] [-nlp] <inputfile>\n");
  102. printf(" -v: Verbose output.\n");
  103. printf(" -nlp: NLS+ format. This will only affect sortkey and sorttabl generation.\n");
  104. return (1);
  105. }
  106. //
  107. // Parse switches.
  108. //
  109. for (Pos = 1; Pos < argc - 1; Pos++)
  110. {
  111. if (argv[Pos][0] == '-' || argv[Pos][0] == '/')
  112. {
  113. //
  114. // Check for verbose switch.
  115. //
  116. if (_stricmp(argv[Pos]+1, "v") == 0)
  117. {
  118. Verbose = 1;
  119. Pos++;
  120. } else if (_stricmp(argv[Pos]+1, "nlp") == 0)
  121. {
  122. NLSPlus = 1;
  123. Pos++;
  124. } else
  125. {
  126. printf("Invalid switch.\n");
  127. return (1);
  128. }
  129. } else
  130. {
  131. printf("Invalid switch.\n");
  132. return (1);
  133. }
  134. }
  135. //
  136. // The last argument is the file name.
  137. // Check input file exists and can be open as read only.
  138. //
  139. if ((pInputFile = fopen(argv[argc-1], "r")) == 0)
  140. {
  141. printf("Error opening input file.\n");
  142. return (1);
  143. }
  144. //
  145. // Return success.
  146. //
  147. return (0);
  148. }
  149. ////////////////////////////////////////////////////////////////////////////
  150. //
  151. // InitGlobals
  152. //
  153. // Initialize global variables.
  154. //
  155. ////////////////////////////////////////////////////////////////////////////
  156. void InitGlobals()
  157. {
  158. if (NLSPlus)
  159. {
  160. wcscpy(FileExtension, NLP_FILE_SUFFIX_W);
  161. } else
  162. {
  163. wcscpy(FileExtension, DATA_FILE_SUFFIX_W);
  164. }
  165. }
  166. ////////////////////////////////////////////////////////////////////////////
  167. //
  168. // ParseInputFile
  169. //
  170. // This routine parses the input file.
  171. //
  172. // 07-30-91 JulieB Created.
  173. // 12-10-91 JulieB Modified for new table format.
  174. ////////////////////////////////////////////////////////////////////////////
  175. int ParseInputFile()
  176. {
  177. char pszKeyWord[MAX]; // input token
  178. char pszParam[MAX]; // parameter for keyword
  179. CODEPAGE CP; // codepage structure
  180. LANGUAGE Lang; // language structure
  181. LANG_EXCEPT LangExcept; // language exception structure
  182. LOCALE_HEADER LocHdr; // header locale structure
  183. LOCALE_STATIC LocStat; // static length locale structure
  184. LOCALE_VARIABLE LocVar; // variable length locale structure
  185. UNICODE Unic; // unicode structure
  186. CTYPES CTypes; // ctypes structure
  187. SORTKEY Sortkey; // sortkey structure - sorting
  188. SORT_TABLES SortTbls; // sort tables structure - sorting
  189. IDEOGRAPH_EXCEPT IdeographExcept; // ideograph exception structure - sorting
  190. char SortKeyFileName[MAX_PATH]; // The output file name for sortkey table.
  191. char SortTblsFileName[MAX_PATH]; // The output file name for sorttbls table.
  192. while (fscanf(pInputFile, "%s", pszKeyWord) == 1)
  193. {
  194. if (_stricmp(pszKeyWord, "CODEPAGE") == 0)
  195. {
  196. if (Verbose)
  197. printf("\n\nFound CODEPAGE keyword.\n");
  198. //
  199. // Initialize CodePage structure.
  200. //
  201. memset(&CP, 0, sizeof(CODEPAGE));
  202. memset(pszParam, 0, MAX * sizeof(char));
  203. CP.pszName = pszParam;
  204. //
  205. // Get CODEPAGE parameter string.
  206. //
  207. if (GetKeyParam(pszParam))
  208. {
  209. return (1);
  210. }
  211. //
  212. // Get the valid keywords for CODEPAGE.
  213. //
  214. if (ParseCodePage(&CP, pszKeyWord))
  215. {
  216. return (1);
  217. }
  218. //
  219. // Write the CODEPAGE tables to an output file.
  220. //
  221. if (WriteCodePage(&CP))
  222. {
  223. return (1);
  224. }
  225. }
  226. else if (_stricmp(pszKeyWord, "LANGUAGE") == 0)
  227. {
  228. if (Verbose)
  229. printf("\n\nFound LANGUAGE keyword.\n");
  230. //
  231. // Initialize Language structure.
  232. //
  233. memset(&Lang, 0, sizeof(LANGUAGE));
  234. //
  235. // Get LANGUAGE parameter string.
  236. //
  237. if (GetKeyParam(pszParam))
  238. {
  239. return (1);
  240. }
  241. //
  242. // Get the valid keywords for LANGUAGE.
  243. //
  244. if (ParseLanguage(&Lang, pszKeyWord))
  245. {
  246. return (1);
  247. }
  248. //
  249. // Write the LANGUAGE tables to an output file.
  250. //
  251. if (WriteLanguage(&Lang))
  252. {
  253. return (1);
  254. }
  255. }
  256. else if (_stricmp(pszKeyWord, "LANGUAGE_EXCEPTION") == 0)
  257. {
  258. if (Verbose)
  259. printf("\n\nFound LANGUAGE_EXCEPTION keyword.\n");
  260. //
  261. // Initialize Language structure.
  262. //
  263. memset(&LangExcept, 0, sizeof(LANG_EXCEPT));
  264. //
  265. // Get the valid keywords for LANGUAGE_EXCEPTION.
  266. //
  267. if (ParseLangException(&LangExcept, pszKeyWord))
  268. {
  269. return (1);
  270. }
  271. //
  272. // Write the LANGUAGE_EXCEPTION tables to an output file.
  273. //
  274. if (WriteLangException(&LangExcept))
  275. {
  276. return (1);
  277. }
  278. }
  279. else if (_stricmp(pszKeyWord, "LOCALE") == 0)
  280. {
  281. if (Verbose)
  282. printf("\n\nFound LOCALE keyword.\n");
  283. //
  284. // Get the valid keywords for LOCALE.
  285. // Write the LOCALE information to an output file.
  286. //
  287. if (ParseWriteLocale( &LocHdr,
  288. &LocStat,
  289. &LocVar,
  290. pszKeyWord ))
  291. {
  292. return (1);
  293. }
  294. }
  295. else if (_stricmp(pszKeyWord, "UNICODE") == 0)
  296. {
  297. if (Verbose)
  298. printf("\n\nFound UNICODE keyword.\n");
  299. //
  300. // Initialize Unicode structure.
  301. //
  302. memset(&Unic, 0, sizeof(UNICODE));
  303. //
  304. // Get the valid keywords for UNICODE.
  305. //
  306. if (ParseUnicode(&Unic, pszKeyWord))
  307. {
  308. return (1);
  309. }
  310. //
  311. // Write the UNICODE tables to an output file.
  312. //
  313. if (WriteUnicode(&Unic))
  314. {
  315. return (1);
  316. }
  317. }
  318. else if (_stricmp(pszKeyWord, "GEO") == 0)
  319. {
  320. if (Verbose)
  321. printf("\n\nFound GEO keyword.\n");
  322. //
  323. // Get the valid keywords for GEO.
  324. // Write the GEO information to an output file.
  325. //
  326. if (ParseWriteGEO(pszKeyWord))
  327. {
  328. return (1);
  329. }
  330. }
  331. else if (_stricmp(pszKeyWord, "CTYPES") == 0)
  332. {
  333. if (Verbose)
  334. printf("\n\nFound CTYPES keyword.\n");
  335. //
  336. // Initialize CTypes structure.
  337. //
  338. memset(&CTypes, 0, sizeof(CTYPES));
  339. //
  340. // Get the valid keywords for CTYPES.
  341. //
  342. if (ParseCTypes(&CTypes))
  343. {
  344. return (1);
  345. }
  346. //
  347. // Write the CTYPES tables to different output files.
  348. //
  349. if (WriteCTypes(&CTypes))
  350. {
  351. return (1);
  352. }
  353. }
  354. else if (_stricmp(pszKeyWord, "SORTKEY") == 0)
  355. {
  356. if (Verbose)
  357. printf("\n\nFound SORTKEY keyword.\n");
  358. if (NLSPlus)
  359. {
  360. strcpy(SortKeyFileName, SORTKEY_NLP_FILE);
  361. } else
  362. {
  363. strcpy(SortKeyFileName, SORTKEY_FILE);
  364. }
  365. //
  366. // Initialize Sortkey structure.
  367. //
  368. memset(&Sortkey, 0, sizeof(SORTKEY));
  369. //
  370. // Get the valid keywords for SORTKEY.
  371. //
  372. if (ParseSortkey(&Sortkey, pszKeyWord))
  373. {
  374. return (1);
  375. }
  376. //
  377. // Write the SORTKEY tables to an output file.
  378. //
  379. if (WriteSortkey(&Sortkey, SortKeyFileName))
  380. {
  381. return (1);
  382. }
  383. }
  384. else if (_stricmp(pszKeyWord, "SORTTABLES") == 0)
  385. {
  386. if (Verbose)
  387. printf("\n\nFound SORTTABLES keyword.\n");
  388. if (NLSPlus)
  389. {
  390. strcpy(SortTblsFileName, SORTTBLS_NLP_FILE);
  391. } else
  392. {
  393. strcpy(SortTblsFileName, SORTTBLS_FILE);
  394. }
  395. //
  396. // Initialize Sort Tables structure.
  397. //
  398. memset(&SortTbls, 0, sizeof(SORT_TABLES));
  399. //
  400. // Get the valid keywords for SORTTABLES.
  401. //
  402. if (ParseSortTables(&SortTbls, pszKeyWord))
  403. {
  404. return (1);
  405. }
  406. //
  407. // Write the Sort Tables to an output file.
  408. //
  409. if (WriteSortTables(&SortTbls, SortTblsFileName))
  410. {
  411. return (1);
  412. }
  413. }
  414. else if (_stricmp(pszKeyWord, "IDEOGRAPH_EXCEPTION") == 0)
  415. {
  416. if (Verbose)
  417. printf("\n\nFound IDEOGRAPH_EXCEPTION keyword.\n");
  418. //
  419. // Initialize Ideograph Exception structure.
  420. //
  421. memset(&IdeographExcept, 0, sizeof(IDEOGRAPH_EXCEPT));
  422. //
  423. // Get the valid keywords for IDEOGRAPH_EXCEPTION.
  424. //
  425. if (ParseIdeographExceptions(&IdeographExcept))
  426. {
  427. return (1);
  428. }
  429. //
  430. // Write the Ideograph Exceptions to the given output file.
  431. //
  432. if (WriteIdeographExceptions(&IdeographExcept))
  433. {
  434. return (1);
  435. }
  436. }
  437. else
  438. {
  439. printf("Parse Error: Invalid Instruction '%s'.\n", pszKeyWord);
  440. return (1);
  441. }
  442. }
  443. //
  444. // Return success.
  445. //
  446. return (0);
  447. }
  448. ////////////////////////////////////////////////////////////////////////////
  449. //
  450. // GetKeyParam
  451. //
  452. // This routine gets the parameter for the keyword from the input file. If
  453. // the parameter is not there, then an error is returned.
  454. //
  455. // 12-10-91 JulieB Created.
  456. ////////////////////////////////////////////////////////////////////////////
  457. int GetKeyParam(
  458. PSZ pszParam)
  459. {
  460. //
  461. // Read the parameter from the input file.
  462. //
  463. if (fscanf( pInputFile,
  464. "%s ;%*[^\n]",
  465. pszParam ) != 1)
  466. {
  467. printf("Parse Error: Error reading parameter value.\n");
  468. return (1);
  469. }
  470. if (Verbose)
  471. printf(" PARAMETER = %s\n\n", pszParam);
  472. //
  473. // Return success.
  474. //
  475. return (0);
  476. }
  477. //-------------------------------------------------------------------------//
  478. // EXTERNAL ROUTINES //
  479. //-------------------------------------------------------------------------//
  480. ////////////////////////////////////////////////////////////////////////////
  481. //
  482. // GetSize
  483. //
  484. // This routine gets the size of the table from the input file. If the
  485. // size is not there, then an error is returned.
  486. //
  487. // 07-30-91 JulieB Created.
  488. ////////////////////////////////////////////////////////////////////////////
  489. int GetSize(
  490. int *pSize)
  491. {
  492. int NumItems; // number of items returned from fscanf
  493. //
  494. // Read the size from the input file.
  495. //
  496. NumItems = fscanf( pInputFile,
  497. "%d ;%*[^\n]",
  498. pSize );
  499. if (NumItems != 1)
  500. {
  501. printf("Parse Error: Error reading size value.\n");
  502. return (1);
  503. }
  504. if (*pSize < 0)
  505. {
  506. printf("Parse Error: Invalid size value %d\n", *pSize);
  507. return (1);
  508. }
  509. if (Verbose)
  510. printf(" SIZE = %d\n\n", *pSize);
  511. //
  512. // Return success.
  513. //
  514. return (0);
  515. }