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.

590 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. cue.c
  5. Abstract:
  6. Generates command prompt aliases
  7. Author:
  8. Jim Schmidt (jimschm) 13-Feb-1998
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. HANDLE g_hHeap;
  14. HINSTANCE g_hInst;
  15. VOID
  16. HelpAndExit (
  17. VOID
  18. )
  19. {
  20. printf ("Command line syntax:\n\n"
  21. "cue [-c:cmd] [-a:alias] [root]\n"
  22. "\n"
  23. "-c Specifies command, default is cd /d %%d\\$1\n"
  24. "-a Specifies alias format string, default is %%n\n"
  25. "[root] Specifies root directory to scan\n"
  26. "\n"
  27. "The -c and -a options can have the following symbols:\n"
  28. "\n"
  29. " %%d Specifies full directory path\n"
  30. " %%n Specifies directory name\n"
  31. "\n"
  32. "The output can be copied to cue.pri in the razzle environment.\n"
  33. );
  34. exit(0);
  35. }
  36. GROWLIST g_VarNames = GROWLIST_INIT;
  37. GROWBUFFER g_VarVals = GROWBUF_INIT;
  38. VOID
  39. pInitEnvVars (
  40. VOID
  41. )
  42. {
  43. PCSTR Env;
  44. PCSTR p;
  45. CHAR VarName[512];
  46. PSTR VarVal;
  47. Env = GetEnvironmentStrings();
  48. Env = GetEndOfStringA (Env) + 1;
  49. Env = GetEndOfStringA (Env) + 1;
  50. p = Env;
  51. while (*p) {
  52. _mbscpy (VarName, p);
  53. p = GetEndOfStringA (p) + 1;
  54. if (*VarName != '_') {
  55. continue;
  56. }
  57. VarVal = _mbschr (VarName, '=');
  58. if (VarVal) {
  59. *VarVal = 0;
  60. VarVal++;
  61. GrowListAppendString (&g_VarNames, VarName);
  62. MultiSzAppend (&g_VarVals, VarVal);
  63. }
  64. }
  65. }
  66. VOID
  67. pFreeEnvVars (
  68. VOID
  69. )
  70. {
  71. FreeGrowList (&g_VarNames);
  72. FreeGrowBuffer (&g_VarVals);
  73. }
  74. VOID
  75. pUseEnvVars (
  76. IN OUT PSTR String
  77. )
  78. {
  79. PSTR p;
  80. UINT u;
  81. UINT Best;
  82. UINT Length;
  83. UINT BestLength;
  84. PCSTR q;
  85. CHAR NewString[1024];
  86. PSTR d;
  87. p = String;
  88. d = NewString;
  89. do {
  90. q = (PCSTR) g_VarVals.Buf;
  91. BestLength = 0;
  92. u = 0;
  93. Best = 0xffffffff;
  94. while (*q) {
  95. Length = _mbslen (q);
  96. if (Length > 3) {
  97. if (!_mbsnicmp (p, q, Length)) {
  98. if (Length > BestLength) {
  99. Best = u;
  100. BestLength = Length;
  101. }
  102. }
  103. }
  104. q = GetEndOfStringA (q) + 1;
  105. u++;
  106. }
  107. if (Best != 0xffffffff) {
  108. q = GrowListGetString (&g_VarNames, Best);
  109. *d++ = '%';
  110. StringCopyA (d, q);
  111. d = GetEndOfStringA (d);
  112. *d++ = '%';
  113. p += BestLength;
  114. } else {
  115. *d++ = *p++;
  116. }
  117. } while (*p);
  118. *d = 0;
  119. StringCopyA (String, NewString);
  120. }
  121. VOID
  122. pGenerateString (
  123. IN PCSTR Format,
  124. OUT PSTR String,
  125. IN PCSTR Directory,
  126. IN PCSTR FullPath
  127. )
  128. {
  129. PSTR p;
  130. PCSTR q;
  131. q = Format;
  132. p = String;
  133. while (*q) {
  134. if (*q == '%') {
  135. q++;
  136. switch (tolower (*q)) {
  137. case 'n':
  138. StringCopyA (p, Directory);
  139. p = GetEndOfStringA (p);
  140. break;
  141. case 'd':
  142. StringCopyA (p, FullPath);
  143. p = GetEndOfStringA (p);
  144. break;
  145. default:
  146. if (*q != 0) {
  147. *p++ = *q;
  148. } else {
  149. q--;
  150. }
  151. break;
  152. }
  153. q++;
  154. } else {
  155. *p++ = *q++;
  156. }
  157. }
  158. *p = 0;
  159. }
  160. VOID
  161. AddString (
  162. PCSTR String
  163. )
  164. {
  165. MemDbSetValue (String, 0);
  166. }
  167. BOOL
  168. FindString (
  169. PCSTR String
  170. )
  171. {
  172. return MemDbGetValue (String, NULL);
  173. }
  174. BOOL
  175. pIsAliasInUse (
  176. IN PCSTR Alias,
  177. IN OUT PGROWLIST NewAliases
  178. )
  179. {
  180. LONG rc;
  181. CHAR CmdLine[512];
  182. UINT Size;
  183. UINT u;
  184. PCSTR p;
  185. UINT Len;
  186. PSTR DontCare;
  187. //
  188. // Scan new aliases first. Two or more identical aliases are ambiguous,
  189. // so we find them and delete them.
  190. //
  191. Size = GrowListGetSize (NewAliases);
  192. wsprintf (CmdLine, "%s ", Alias);
  193. Len = _mbslen (CmdLine);
  194. for (u = 0 ; u < Size ; u++) {
  195. p = GrowListGetString (NewAliases, u);
  196. if (!_mbsnicmp (p, CmdLine, Len)) {
  197. GrowListDeleteItem (NewAliases, u);
  198. return TRUE;
  199. }
  200. }
  201. //
  202. // Now see if the alias was already defined
  203. //
  204. if (FindString (Alias)) {
  205. return TRUE;
  206. }
  207. //
  208. // Finally verify the alias is not an EXE in the path
  209. //
  210. wsprintf (CmdLine, "%s.exe", Alias);
  211. if (SearchPath (NULL, CmdLine, NULL, 512, CmdLine, &DontCare)) {
  212. return TRUE;
  213. }
  214. wsprintf (CmdLine, "%s.bat", Alias);
  215. if (SearchPath (NULL, CmdLine, NULL, 512, CmdLine, &DontCare)) {
  216. return TRUE;
  217. }
  218. wsprintf (CmdLine, "%s.cmd", Alias);
  219. if (SearchPath (NULL, CmdLine, NULL, 512, CmdLine, &DontCare)) {
  220. return TRUE;
  221. }
  222. return FALSE;
  223. }
  224. VOID
  225. pAddAlias (
  226. IN OUT PGROWLIST NewAliases,
  227. IN PCSTR AliasStr,
  228. IN PCSTR CommandStr
  229. )
  230. {
  231. CHAR CmdLine[512];
  232. AddString (AliasStr);
  233. wsprintf (CmdLine, "%s %s", AliasStr, CommandStr);
  234. GrowListAppendString (NewAliases, CmdLine);
  235. }
  236. VOID
  237. pProcessCueFile (
  238. IN PCSTR Path,
  239. IN PCSTR FileName
  240. )
  241. {
  242. HANDLE File;
  243. CHAR CmdLine[512];
  244. PSTR p;
  245. PCSTR q;
  246. DWORD Read;
  247. DWORD Size;
  248. DWORD Pos;
  249. CHAR FullPath[MAX_PATH];
  250. wsprintf (FullPath, "%s\\%s", Path, FileName);
  251. File = CreateFile (
  252. FullPath,
  253. GENERIC_READ,
  254. 0,
  255. NULL,
  256. OPEN_EXISTING,
  257. FILE_ATTRIBUTE_NORMAL,
  258. NULL
  259. );
  260. if (File != INVALID_HANDLE_VALUE) {
  261. Size = GetFileSize (File, NULL);
  262. for (Pos = 0 ; Pos < Size ; Pos += Read) {
  263. SetFilePointer (File, Pos, NULL, FILE_BEGIN);
  264. if (!ReadFile (File, CmdLine, 256, &Read, NULL)) {
  265. break;
  266. }
  267. //
  268. // Find end of line
  269. //
  270. CmdLine[Read] = 0;
  271. p = _mbschr (CmdLine, '\n');
  272. if (p) {
  273. *p = 0;
  274. Read = p - CmdLine + 1;
  275. } else {
  276. p = _mbschr (CmdLine, '\r');
  277. if (p) {
  278. *p = 0;
  279. Read = p - CmdLine + 1;
  280. } else {
  281. p = GetEndOfStringA (CmdLine);
  282. Read = p - CmdLine;
  283. }
  284. }
  285. //
  286. // Add alias
  287. //
  288. q = CmdLine;
  289. while (isspace (*q)) {
  290. q++;
  291. }
  292. p = (PSTR) q;
  293. while (*p && !isspace (*p)) {
  294. p++;
  295. }
  296. if (*p) {
  297. *p = 0;
  298. AddString (q);
  299. }
  300. }
  301. CloseHandle (File);
  302. }
  303. }
  304. BOOL
  305. MemDb_Entry (
  306. HINSTANCE hInst,
  307. DWORD Reason,
  308. PVOID DontCare
  309. );
  310. BOOL
  311. MigUtil_Entry (
  312. HINSTANCE hInst,
  313. DWORD Reason,
  314. PVOID DontCare
  315. );
  316. INT
  317. __cdecl
  318. main (
  319. INT argc,
  320. CHAR *argv[]
  321. )
  322. {
  323. TREE_ENUM e;
  324. GROWLIST NewAliases = GROWLIST_INIT;
  325. PCSTR RootPath;
  326. INT i;
  327. UINT u;
  328. UINT Size;
  329. PCSTR Alias = "%n";
  330. PCSTR Command = "cd /d %d\\$1";
  331. CHAR AliasStr[256];
  332. CHAR CommandStr[256];
  333. CHAR PubPath[MAX_PATH];
  334. CHAR PriPath[MAX_PATH];
  335. PSTR p;
  336. CHAR CurDir[MAX_PATH];
  337. GetCurrentDirectory (MAX_PATH, CurDir);
  338. RootPath = NULL;
  339. g_hHeap = GetProcessHeap();
  340. g_hInst = GetModuleHandle (NULL);
  341. MigUtil_Entry (g_hInst, DLL_PROCESS_ATTACH, NULL);
  342. MemDb_Entry (g_hInst, DLL_PROCESS_ATTACH, NULL);
  343. for (i = 1 ; i < argc ; i++) {
  344. if (argv[i][0] == '-' || argv[i][0] == '/') {
  345. switch (tolower (argv[i][1])) {
  346. case 'c':
  347. if (argv[i][2] != ':') {
  348. i++;
  349. if (i == argc) {
  350. HelpAndExit();
  351. }
  352. Command = argv[i];
  353. } else {
  354. Command = &argv[i][3];
  355. }
  356. break;
  357. case 'a':
  358. if (argv[i][2] != ':') {
  359. i++;
  360. if (i == argc) {
  361. HelpAndExit();
  362. }
  363. Alias = argv[i];
  364. } else {
  365. Alias = &argv[i][3];
  366. }
  367. break;
  368. default:
  369. HelpAndExit();
  370. }
  371. }
  372. else if (RootPath) {
  373. HelpAndExit();
  374. }
  375. else {
  376. RootPath = argv[i];
  377. }
  378. }
  379. if (!RootPath) {
  380. RootPath = CurDir;
  381. }
  382. //
  383. // Parse the ntcue.pub, cue.pub and cue.pri files
  384. //
  385. if (!GetEnvironmentVariable ("INIT", PriPath, MAX_PATH)) {
  386. printf ("Must be in razzle environment to run this tool\n");
  387. return -1;
  388. }
  389. StringCopyA (PubPath, PriPath);
  390. p = _mbsrchr (PubPath, '\\');
  391. if (!p) {
  392. printf ("Must be in razzle environment to run this tool\n");
  393. return -1;
  394. }
  395. *p = 0;
  396. pProcessCueFile (PubPath, "ntcue.pub");
  397. pProcessCueFile (PubPath, "cue.pub");
  398. pProcessCueFile (PriPath, "cue.pri");
  399. //
  400. // Add commands
  401. //
  402. AddString ("cd");
  403. AddString ("dir");
  404. AddString ("copy");
  405. AddString ("type");
  406. AddString ("del");
  407. AddString ("erase");
  408. AddString ("color");
  409. AddString ("md");
  410. AddString ("chdir");
  411. AddString ("mkdir");
  412. AddString ("prompt");
  413. AddString ("pushd");
  414. AddString ("popd");
  415. AddString ("set");
  416. AddString ("setlocal");
  417. AddString ("endlocal");
  418. AddString ("if");
  419. AddString ("for");
  420. AddString ("call");
  421. AddString ("shift");
  422. AddString ("goto");
  423. AddString ("start");
  424. AddString ("assoc");
  425. AddString ("ftype");
  426. AddString ("exit");
  427. AddString ("ren");
  428. AddString ("rename");
  429. AddString ("move");
  430. pInitEnvVars();
  431. //
  432. // Scan the specified directory
  433. //
  434. if (EnumFirstFileInTree (&e, RootPath, NULL, TRUE)) {
  435. do {
  436. if (!e.Directory) {
  437. continue;
  438. }
  439. pGenerateString (Alias, AliasStr, e.Name, e.FullPath);
  440. if (pIsAliasInUse (AliasStr, &NewAliases)) {
  441. continue;
  442. }
  443. pGenerateString (Command, CommandStr, e.Name, e.FullPath);
  444. pUseEnvVars (CommandStr);
  445. pAddAlias (&NewAliases, AliasStr, CommandStr);
  446. } while (EnumNextFileInTree (&e));
  447. }
  448. Size = GrowListGetSize (&NewAliases);
  449. for (u = 0 ; u < Size ; u++) {
  450. p = (PSTR) GrowListGetString (&NewAliases, u);
  451. printf ("%s\n", p);
  452. }
  453. FreeGrowList (&NewAliases);
  454. pFreeEnvVars();
  455. return 0;
  456. }