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.

2205 lines
74 KiB

  1. /*++
  2. Copyright (c) 1988-1999 Microsoft Corporation
  3. Module Name:
  4. clex.c
  5. Abstract:
  6. Lexical processing support
  7. --*/
  8. #include "cmd.h"
  9. extern unsigned int DosErr;
  10. extern jmp_buf CmdJBuf2; /* Used for error handling */
  11. extern TCHAR DTNums[];
  12. extern TCHAR MsgBuf[];
  13. extern unsigned msglen; /* @@@@@@@@ */
  14. int Necho = 0; /* No echo option */
  15. extern BOOLEAN fDumpTokens;
  16. extern int KeysFlag; /* @@5 */
  17. unsigned DataFlag; /* Tells FillBuf where to get its input */
  18. UINT_PTR DataPtr; /* File handle/string ptr FillBuf... */
  19. int Ctrlc = 0; /* flag - if set print a ctrl/c before next prompt */
  20. int ExtCtrlc = 0; /* @@4 flag, if set print msg */
  21. int AtIsToken; /* @@4 flag, true if @ is a token */
  22. /***
  23. * The lex buffer is called LexBuf. It holds characters as they are
  24. * retrieved one by one by GetByte. With the advent of double byte
  25. * characters, UnGetByte may be sometimes called upon to put back
  26. * up to two characters. To facilitate this, LexBuf is really an
  27. * alias for &LexBuffer[1]. This gives an extra byte in front of the
  28. * buffer for character push back. Every time fillbuf is called, it
  29. * copies the last character of the previous buffer into the byte
  30. * preceeding the normal buffer. Thus, when UnGetByte does a
  31. * LexBufPtr-- the pointer will correctly point at the preceeding character.
  32. */
  33. TCHAR LexBuffer[LBUFLEN+3]; /* @@4 */
  34. /* ...reads from Lexer input buffer *M011*/
  35. /* LBUFLEN characters + newline + null + */
  36. /* an extra byte for UnGetByte */
  37. #define LexBuf (&LexBuffer[1])
  38. TCHAR *LexBufPtr; /* Ptr to next byte in Lex's input buffer */
  39. TCHAR *PrevLexPtr; /* M013 - New previous token pointer */
  40. TCHAR FrsBuf[LBUFLEN+1];
  41. extern CHAR AnsiBuf[];
  42. extern TCHAR Fmt27[];
  43. extern int NulNode;
  44. extern TCHAR Fmt19[];
  45. extern TCHAR DBkSpc[];
  46. #if defined(FE_SB) // DDBkSpc[]
  47. extern TCHAR DDBkSpc[];
  48. #endif // defined(FE_SB)
  49. extern unsigned global_dfvalue; /* @@4 */
  50. extern int EchoFlag;
  51. extern TCHAR PromptStr[], CurDrvDir[], Delimiters[];
  52. extern unsigned flgwd;
  53. extern BOOL CtrlCSeen;
  54. VOID SetCtrlC();
  55. VOID ResetCtrlC();
  56. //
  57. // Prompt string special characters and associated print character/flag.
  58. //
  59. // These are the flags which may be placed in the flag field of the
  60. // prompt_table structure to control PrintPrompt
  61. //
  62. #define PNULLFLAG 0
  63. #define PTIMFLAG 1
  64. #define PDATFLAG 2
  65. #define PPATFLAG 3
  66. #define PVERFLAG 4
  67. #define PBAKFLAG 5 // destructive backspace flag
  68. #define PNLNFLAG 6 // newline prompt flag
  69. #define PDRVFLAG 7
  70. #define PLITFLAG 8 // Print character in SpecialChar field
  71. #define PDPTFLAG 9 // Print depth of pushd stack
  72. #define PNETFLAG 10 // Print \\server\share or local for current drive
  73. //
  74. // Esc character used to mark a special prompt char. in prompt string
  75. //
  76. #define PROMPTESC DOLLAR
  77. //
  78. // Table of prompts for user.
  79. //
  80. typedef struct {
  81. TCHAR Char; // Used to match esc. char. in user prompt
  82. TCHAR Format; // Used to print some string that has to be computed
  83. TCHAR Literal; // When Format == PLITFLAG this is printed in prompt
  84. } PROMPT_ENTRY;
  85. PROMPT_ENTRY PromptTable[] = {
  86. { TEXT('P'),PPATFLAG, NULLC },
  87. { TEXT('E'),PLITFLAG,'\033' },
  88. { TEXT('D'),PDATFLAG, NULLC },
  89. { TEXT('T'),PTIMFLAG, NULLC },
  90. { TEXT('B'),PLITFLAG, PIPOP },
  91. { TEXT('G'),PLITFLAG, OUTOP },
  92. { TEXT('H'),PBAKFLAG, NULLC },
  93. { TEXT('L'),PLITFLAG, INOP },
  94. { TEXT('N'),PDRVFLAG, NULLC },
  95. { TEXT('S'),PLITFLAG, SPACE },
  96. { TEXT('Q'),PLITFLAG, EQ },
  97. { TEXT('V'),PVERFLAG, NULLC },
  98. { TEXT('_'),PNLNFLAG, NULLC },
  99. { DOLLAR, PLITFLAG, DOLLAR },
  100. { TEXT('A'),PLITFLAG, ANDOP },
  101. { TEXT('C'),PLITFLAG, LPOP },
  102. { TEXT('F'),PLITFLAG, RPOP },
  103. { TEXT('+'),PDPTFLAG, NULLC },
  104. { TEXT('M'),PNETFLAG, NULLC },
  105. { NULLC,PNULLFLAG, NULLC}};
  106. /*** InitLex - initialize the lexer's global variables
  107. *
  108. * Purpose:
  109. * Initialize DataFlag, DataPtr, LexBuf, and LexBufPtr.
  110. *
  111. * InitLex(unsigned dfvalue, INT_PTR dpvalue)
  112. *
  113. * Args:
  114. * dfvalue - the value to be assigned to DataFlag
  115. * dpvalue - the value to be assigned to DataPtr
  116. *
  117. */
  118. void InitLex(dfvalue, dpvalue)
  119. unsigned dfvalue;
  120. INT_PTR dpvalue;
  121. {
  122. DataFlag = dfvalue;
  123. DataPtr = dpvalue;
  124. *LexBuf = NULLC;
  125. PrevLexPtr = LexBufPtr = LexBuf; /* M013 - Init new ptr */
  126. DEBUG((PAGRP, LXLVL, "INITLEX: Dataflag = %04x DataPtr = %04x", DataFlag, DataPtr));
  127. }
  128. /*** Lex - controls data input and token lexing
  129. *
  130. * Purpose:
  131. * Read in the next token or argstring and put it in tokbuf.
  132. *
  133. * unsigned Lex(TCHAR *tokbuf, unsigned lflag)
  134. *
  135. * Args:
  136. * tokbuf - buffer used by lex to store the next token or
  137. * - M013 if zero, indicates unget last token.
  138. * lflag - bit 0 on if lex is to return an argument string, ie white space
  139. * other than NLN is not considered a token delimiter
  140. *
  141. * Returns:
  142. * If the token is an operator, EOS, or NLN ret the 1st byte of the token.
  143. * If the token is a command, REM arg or argstring, return TEXTOKEN.
  144. * If the token is longer than MAXTOKLEN or the token is illegal, LEXERROR
  145. * is returned.
  146. *
  147. * Notes:
  148. * The parser depends on the fact that the only values returned by
  149. * Lex that are greater than 0xff are TEXTOKEN and LEXERROR.
  150. *
  151. */
  152. unsigned Lex(tokbuf, lflag)
  153. TCHAR *tokbuf;
  154. unsigned lflag;
  155. {
  156. int i; /* Length of text token */
  157. TCHAR c, /* Current character */
  158. *tbcpy; /* Copy of tokbuf */
  159. if(setjmp(CmdJBuf2)) { /* M026 - Now msg printed prior */
  160. return((unsigned)LEXERROR); /* ...to arrival here */
  161. }
  162. /* M013 - This code detects request to unget last token and if so, performs
  163. that function. If not, it sets the previous token pointer to
  164. to equal the current token pointer.
  165. */
  166. if (tokbuf == LX_UNGET) { /* Unget last token? */
  167. DEBUG((PAGRP, LXLVL, "LEX: Ungetting last token."));
  168. if (fDumpTokens)
  169. cmd_printf( TEXT("Ungetting: '%s'\n"), PrevLexPtr);
  170. LexBufPtr = PrevLexPtr; /* If so, reset ptr... */
  171. return(LX_UNGET); /* ...and return */
  172. } else { /* If not, set previous... */
  173. PrevLexPtr = LexBufPtr; /* ...ptr to current... */
  174. DEBUG((PAGRP, LXLVL, "LEX: lflag = %d", lflag));
  175. } /* ...ptr and continue */
  176. /* M013 ends */
  177. tbcpy = tokbuf;
  178. /* M005 - Altered conditional below to also fail if the LX_REM bit
  179. * is set making it "If !(arg | rem), eat whtspc & delims".
  180. */
  181. if (!(lflag & (LX_ARG|LX_REM))) {
  182. DEBUG((PAGRP, LXLVL, "LEX: Trashing white space."));
  183. while (TRUE) {
  184. c = GetByte();
  185. if (((_istspace(c) && c != NLN)
  186. || (mystrchr(((lflag & LX_EQOK) ? &Delimiters[1] : Delimiters), c) && c)))
  187. ;
  188. else
  189. break;
  190. }
  191. UnGetByte();
  192. }
  193. /* As of M016, operators of more than 2 characters can be lexed. For now,
  194. * these are assumed to be specific-handle redirection operators of the form
  195. * 'n>>' or 'n<<' and always begin with a digit. TextCheck will not return
  196. * a digit as an operator unless it is preceeded by whitespace and followed
  197. * by '>' or '<'. To simplify matters, handle substitution (ie., '...&n')
  198. * is now lexed as part of a special five character operator, instead of
  199. * looking at the '&n' as an argument. ASCII filename arguments, however,
  200. * are still lexed as separate tokens via another call to Lex.
  201. */
  202. if (TextCheck(&c, &lflag) == LX_DELOP) {
  203. *tokbuf++ = c; /* The token is an operator */
  204. if (_istdigit(c)) { /* Next is '<' or '>'... */
  205. DEBUG((PAGRP, LXLVL, "LEX: Found digit operator."));
  206. c = GetByte(); /* ...by definition or ... */
  207. *tokbuf++ = c; /* ...we wouldn't be here */
  208. }
  209. if (c == PIPOP || c == ANDOP || c == OUTOP || c == INOP) {
  210. if ((c = GetByte()) == *(tokbuf-1)) {
  211. *tokbuf++ = c;
  212. c = GetByte();
  213. }
  214. if (*(tokbuf-1) == OUTOP || *(tokbuf-1) == INOP) {
  215. DEBUG((PAGRP,LXLVL, "LEX: Found redir."));
  216. if (c == CSOP) {
  217. DEBUG((PAGRP,LXLVL, "LEX: Found >&"));
  218. *tokbuf++ = c;
  219. do {
  220. c = GetByte();
  221. } while (_istspace(c) ||
  222. mystrchr(Delimiters,c));
  223. if (_istdigit(c)) {
  224. *tokbuf++ = c;
  225. c = GetByte();
  226. }
  227. }
  228. /* M016 ends */
  229. }
  230. UnGetByte();
  231. }
  232. *tokbuf = NULLC;
  233. DEBUG((PAGRP, LXLVL, "LEX: Returning op = `%ws'", tbcpy));
  234. return(*tbcpy);
  235. }
  236. DEBUG((PAGRP, LXLVL, "LEX: Found text token %04x, Getting more.", c));
  237. *tokbuf++ = c; /* Found text token, read the rest */
  238. lflag |= LX_DBLOK;
  239. AtIsToken = 0; /* @@4, treat @ as text now */
  240. for (i = (int)(tokbuf-tbcpy); TextCheck(&c, &lflag) != LX_DELOP && i < MAXTOKLEN; i++)
  241. *tokbuf++ = c;
  242. lflag &= ~LX_DBLOK;
  243. *tokbuf = NULLC;
  244. if (i < MAXTOKLEN)
  245. UnGetByte();
  246. if (i >= MAXTOKLEN && c != (TCHAR) -1) { /* Token too long, complain */
  247. /* M025 */ PutStdErr(MSG_TOKEN_TOO_LONG, ONEARG, tbcpy );
  248. return((unsigned)LEXERROR);
  249. }
  250. DEBUG((PAGRP, LXLVL, "LEX: Return text = `%ws' type = %04x", tbcpy, TEXTOKEN));
  251. return(TEXTOKEN);
  252. }
  253. /*** TextCheck - get the next character and determine its type
  254. *
  255. * Purpose:
  256. * Store the next character in LexBuf in *c. If that character is a
  257. * valid text token character, return it. Otherwise return LX_DELOP.
  258. *
  259. * int TextCheck(TCHAR *c, unsigned &lflag)
  260. *
  261. * Args:
  262. * c - the next character in the lexer input buffer is stored here.
  263. * lflag - Bit 0 = On if lex is to return an argument string, ie.,
  264. * white space other than NLN is not a token delimiter.
  265. * Bit 1 = On if a quoted string is being read, ie., only NLN
  266. * or a closing quote are delimiters.
  267. * Bit 2 = On if equalsigns are NOT to be considered delimiters.
  268. * Bit 3 = On if left parens are to be considered operators.
  269. * Bit 4 = On if right parens are to be considered operators.
  270. * Bit 5 = On if only NLN is to be a delimiter.
  271. * Bit 6 = On iff the caller is willing to accept the second
  272. * half of a double byte character
  273. *
  274. * Returns:
  275. * Next character or LX_DELOP if a delimeter/operator character is found.
  276. *
  277. */
  278. int TextCheck(c, lflag)
  279. TCHAR *c;
  280. unsigned *lflag;
  281. {
  282. TCHAR i; /* M016 - Temp byte holder */
  283. static int saw_dbcs_lead = 0; /* remember if we're in the middle
  284. of a double byte character */
  285. *c = GetByte();
  286. if (saw_dbcs_lead) {
  287. saw_dbcs_lead = 0;
  288. if (*lflag & LX_DBLOK) /* If second half of double byte is */
  289. return(*c); /* ok, return it, otherwise. . . */
  290. else
  291. *c = GetByte(); /* go on to next character */
  292. }
  293. DEBUG((PAGRP, BYLVL, "TXTCHK: c = %04x lflag = %04x", *c, *lflag));
  294. switch (*c) {
  295. case SILOP: /* M017 - New unary operator */
  296. /* ...binds like left paren */
  297. if ((*lflag & (LX_QUOTE|LX_REM))) /* M005 */
  298. break;
  299. if( !AtIsToken ) /* If @ is not to be treated */
  300. { /* as token, then indicate */
  301. return( *c ); /* such @@4 */
  302. }
  303. case LPOP: /* M002 - Moved these two cases */
  304. if ((*lflag & (LX_QUOTE|LX_REM))) /* M005 */
  305. break;
  306. if(!(*lflag & GT_LPOP)) /* ...up and break if */
  307. break; /* ...they are not to */
  308. case RPOP: /* ...be treated as ops */
  309. if ((*lflag & (LX_QUOTE|LX_REM))) /* M005 */
  310. break;
  311. if((!(*lflag & GT_RPOP)) && *c == RPOP)
  312. break; /* M002 ends */
  313. case NLN: /* M005 - NLN turns off QUOTE/REM flags */
  314. case EOS: /* @@5a - treat like NLN */
  315. *lflag &= (~LX_QUOTE & ~LX_REM); /* M005 */
  316. case CSOP:
  317. case INOP: /* M005 - Note below that LX_DELOP... */
  318. case PIPOP: /* ...QUOTE mode or REM mode is in... */
  319. case OUTOP: /* ...in effect at the time */
  320. if (!(*lflag & (LX_QUOTE|LX_REM))) /* M005 */
  321. return(LX_DELOP);
  322. }
  323. /* M003 - If the character is '^', and the QUOTE mode flag is off,
  324. * discard the current character, get the next one and return
  325. * it as text.
  326. * M005 - Extended this conditional to insure that both QUOTE and
  327. * REM flags must be off for "escape" to occur.
  328. */
  329. if (*c == ESCHAR && !(*lflag & (LX_QUOTE|LX_REM))) {
  330. *c = GetByte();
  331. if (*c == NLN) {
  332. *c = GetByte( );
  333. //
  334. // If we are at the end of the string, meaning that the
  335. // command ended with ^ and there is an empty next command
  336. // (most likely from a batch file), then we just treat
  337. // this as a standard delimeter.
  338. //
  339. if (*c == EOS) {
  340. return(LX_DELOP);
  341. }
  342. }
  343. return(*c);
  344. }
  345. /* M003/M005 end */
  346. if (*c == QUOTE) /* Flip quote mode flag bit */
  347. *lflag ^= LX_QUOTE;
  348. /* M005 - Altered conditional below to also insure that REM flag was
  349. * off before checking for any delimiters
  350. */
  351. if (!(*lflag & (LX_ARG|LX_QUOTE|LX_REM)) &&
  352. (_istspace(*c) ||
  353. mystrchr(((*lflag & LX_EQOK) ? &Delimiters[1] : Delimiters), *c)))
  354. return(LX_DELOP);
  355. /* As of M016, before accepting this character as text, it is now tested
  356. * for being a digit followed by one of the redirection operators and;
  357. * 1) is the first character on a line, 2) is preceeded by whitespace or
  358. * 3) is preceeded by a delimiter (including Command's operators). If it
  359. * meets these conditions, it is a special, specific-handle redirection
  360. * operator and TextCheck must return LX_DELOP so that Lex can build the
  361. * remainder. NOTE: LexBufPtr is advanced during GetByte, so that the
  362. * current byte is *(LexBufPtr-1).
  363. */
  364. if (_istdigit(*c)) {
  365. DEBUG((PAGRP,BYLVL,"TXTCHK: Found digit character."));
  366. if ((LexBufPtr-LexBuf) < 2 ||
  367. _istspace(i = *(LexBufPtr-2)) ||
  368. mystrchr(TEXT("()|&=,;\""), i)) {
  369. DEBUG((PAGRP,BYLVL,"TXTCHK: Digit follows delim."));
  370. if (*LexBufPtr == INOP || *LexBufPtr == OUTOP) {
  371. DEBUG((PAGRP,BYLVL,"TXTCHK: Found hdl redir"));
  372. if (!(*lflag & (LX_QUOTE|LX_REM))) /* M005 */
  373. return(LX_DELOP);
  374. }
  375. }
  376. }
  377. /* M016 ends */
  378. return(*c);
  379. }
  380. /*** GetByte - return the next byte in the buffer
  381. *
  382. * Purpose:
  383. * Get the next byte from the lexer's input buffer. If the buffer is
  384. * empty, fill it first. Update the buffer pointer.
  385. *
  386. * TCHAR GetByte()
  387. *
  388. * Return:
  389. * The next character in the buffer or EOF.
  390. *
  391. * Notes:
  392. * All three types of input STDIN, FILE and STRING are treated
  393. * the same now when it comes to dealing with CR/LF combinations.
  394. * Keyboard input is massaged to look like file input.
  395. * Invalid double byte characters are thrown away and not returned
  396. * to the caller.
  397. *
  398. */
  399. TCHAR GetByte()
  400. {
  401. static int saw_dbcs_lead = 0; /* remember if we're in the middle
  402. of a double byte character */
  403. TCHAR lead; /* variables for remember parts of
  404. double byte characters */
  405. if (!*LexBufPtr)
  406. FillBuf();
  407. DEBUG((PAGRP, BYLVL, "GTTCHAR: byte = %04x", *LexBufPtr));
  408. if (*LexBufPtr == CR && !saw_dbcs_lead) {
  409. /* M000 - removed file-only test */
  410. LexBufPtr++;
  411. return(GetByte());
  412. }
  413. /* if this is a double byte character, look ahead to the next character
  414. and check for legality */
  415. if (saw_dbcs_lead) {
  416. saw_dbcs_lead = 0;
  417. return(*LexBufPtr++);
  418. }
  419. else {
  420. lead = *LexBufPtr++;
  421. return(lead);
  422. }
  423. }
  424. /*** UnGetByte - rewind the lexer buffer pointer 1 byte
  425. *
  426. * Purpose:
  427. * Back up the lexer's buffer pointer. If the pointer already points
  428. * to the beginning of the buffer, do nothing.
  429. *
  430. * UnGetByte()
  431. *
  432. * Return:
  433. * Nothing.
  434. *
  435. */
  436. void UnGetByte()
  437. {
  438. if (!(LexBufPtr == LexBuffer))
  439. LexBufPtr--;
  440. }
  441. int
  442. EditLine(
  443. CRTHANDLE CrtHandle,
  444. TCHAR *Buffer, /* the buffer to be filled */
  445. int MaxLength, /* the Maximum length of the buffer, */
  446. /* including <CR> */
  447. int *ReturnLength /* the number of characters in the buffer, not */
  448. /* including the <CR> */
  449. )
  450. {
  451. BOOL flag;
  452. flag = ReadBufFromInput( CRTTONT(CrtHandle), (TCHAR*)Buffer, MaxLength, ReturnLength);
  453. if (flag == 0 || *ReturnLength == 0) {
  454. return(GetLastError());
  455. } else {
  456. return(0);
  457. }
  458. }
  459. /*** FillBuf - read data to fill the lexer's buffer
  460. *
  461. * Purpose:
  462. * To fill the lexer's buffer with data from whatever source is indicated
  463. * by the global variables DataFlag and DataPtr. If reading from
  464. * stdin, prompt for data.
  465. *
  466. * FillBuf()
  467. *
  468. * Notes:
  469. * - Algorithm after M021 is as follows:
  470. * copy last char of current buffer into LexBuffer[0] (which preceeds
  471. * LexBuf by one byte) so the UnGetByte can unget two bytes
  472. * If READSTDIN or READFILE
  473. * If input is STDIN
  474. * Print correct prompt
  475. * Use DOSREAD to fill primary buffer
  476. * Copy to Lexer buffer so that primary buffer is usable by F3 key.
  477. * Null terminate total input.
  478. * Scan buffer for NLN || ^Z
  479. * If none found
  480. * Error out
  481. * Else
  482. * Terminate statement at NLN or ^Z (exclude ^Z iteself)
  483. * If read was from file
  484. * Rewind to end of first statement
  485. * If file handle is STDIN
  486. * Echo statement to STDOUT
  487. * Else
  488. * Read first statement from string and reset pointer
  489. * Reset Lexer Buffer Pointer to start of buffer
  490. * Substitute for batch and environment variables (M026)
  491. * Reset Previous Lexer Buffer Pointer to start of buffer
  492. *
  493. */
  494. BOOL ReadFromStdInOkay = FALSE;
  495. void FillBuf()
  496. {
  497. long l; /* M004 - Data count in buffer */
  498. TCHAR *sptr; /* Copy of DataPtr */
  499. size_t i; /* Work variable */
  500. DWORD cnt; /* Count of input bytes */
  501. BOOL flag;
  502. //
  503. // clear this flag in case it was hit from previous command
  504. // if it is true we would not execute the next command
  505. //
  506. ResetCtrlC();
  507. LexBuffer[0] = *(LexBufPtr - 1);
  508. switch (DataFlag & FIRSTIME) {
  509. case READFILE:
  510. case READSTDIN:
  511. if ((DataFlag & FIRSTIME) == READSTDIN ||
  512. DataPtr == STDIN) {
  513. if (DataFlag & NOTFIRSTIME) {
  514. /* M025 */ PutStdOut(MSG_MS_MORE, NOARGS);
  515. } else {
  516. PrintPrompt();
  517. DataFlag |= NOTFIRSTIME;
  518. DEBUG((PAGRP, LFLVL, "FLBF: Reading stdin"));
  519. }
  520. }
  521. //
  522. // clear in case ^c seen while printing prompt
  523. //
  524. ResetCtrlC();
  525. DEBUG((PAGRP, LFLVL, "FLBF: Reading handle %d", DataPtr));
  526. //
  527. // If input is STDIN and piped or input is from a
  528. // device but not console input (flgwd == 1)
  529. //
  530. if ( ( DataPtr == STDIN ) && ( FileIsPipe( STDIN ) ||
  531. ( FileIsDevice( STDIN ) && (!(flgwd & 1)) ) ) ) {
  532. cnt = 0;
  533. while (
  534. ( cnt < LBUFLEN) && /* ##1 */
  535. ( (ReadBufFromFile(CRTTONT((CRTHANDLE)DataPtr),
  536. &FrsBuf[cnt], 1, (LPDWORD)&i)) != 0 ||
  537. GetLastError() == ERROR_MORE_DATA) &&
  538. ( i != 0 )
  539. ) {
  540. cnt++;
  541. if ( FrsBuf[cnt-1] == NLN ){
  542. break;
  543. } /* endif */
  544. }
  545. } else if ( ( DataPtr == STDIN ) &&
  546. FileIsDevice( STDIN ) &&
  547. (flgwd & 1) ) {
  548. //
  549. // Are reading from stdin and it is a device
  550. // (not a file) and it is console input
  551. //
  552. if ( KeysFlag ) {
  553. i = EditLine( (CRTHANDLE)DataPtr, FrsBuf, LBUFLEN, &cnt );
  554. }
  555. else {
  556. ResetCtrlC();
  557. if (ReadBufFromConsole(
  558. CRTTONT((CRTHANDLE)DataPtr),
  559. FrsBuf,
  560. LBUFLEN,
  561. &cnt) ) {
  562. //
  563. // Check that ^c's on the current line.
  564. // Could be the case where ^c thread
  565. // came in from a previous line
  566. //
  567. //
  568. // also if cnt is 0 then outpt crlf to
  569. // prevent two prompts on command line
  570. //
  571. if (cnt == 0) {
  572. if (GetLastError() == ERROR_OPERATION_ABORTED) {
  573. cmd_printf(CrLf);
  574. longjmp(CmdJBuf2, -1);
  575. }
  576. cmd_printf(CrLf);
  577. }
  578. i = 0;
  579. DEBUG((PAGRP, LFLVL, "FLBF: ReadFile %d bytes", cnt));
  580. } else {
  581. cnt = 0;
  582. i = GetLastError();
  583. DEBUG((PAGRP, LFLVL, "FLBF: ReadFile %d bytes and error %d", cnt, i));
  584. }
  585. }
  586. }
  587. else {
  588. flag = ReadBufFromFile(
  589. CRTTONT((CRTHANDLE)DataPtr),
  590. FrsBuf, LBUFLEN, (LPDWORD)&cnt);
  591. DEBUG((PAGRP, LFLVL, "FLBF: Read %d bytes", cnt));
  592. if (CtrlCSeen) {
  593. ResetCtrlC();
  594. longjmp(CmdJBuf2, -1);
  595. // Abort();
  596. }
  597. if (flag == 0 || (int)cnt <= 0) {
  598. cnt = 0;
  599. i = GetLastError();
  600. }
  601. else {
  602. i = 0;
  603. }
  604. }
  605. DEBUG((PAGRP, LFLVL, "FLBF: I made it here alive"));
  606. if (!cnt && DataPtr == STDIN) {
  607. DEBUG((PAGRP,LFLVL,"FLBF: ^Z from STDIN!"));
  608. DEBUG((PAGRP,LFLVL," READFILE retd %d",i));
  609. if (FileIsDevice(STDIN) && ReadFromStdInOkay) {
  610. DEBUG((PAGRP,LFLVL,"FLBF: Is device, fixing up buffer"));
  611. FrsBuf[0] = NLN;
  612. ++cnt;
  613. } else {
  614. DEBUG((PAGRP,LFLVL,"FLBF: Is file, aborting!!!"));
  615. ExitAbort(EXIT_EOF);
  616. }
  617. } else if (!ReadFromStdInOkay && cnt && DataPtr == STDIN) {
  618. ReadFromStdInOkay = TRUE;
  619. }
  620. cnt = LexCopy(LexBuf, FrsBuf, cnt);
  621. DEBUG((PAGRP, LFLVL, "FLBF: Received %d characters.", cnt));
  622. *(LexBuf+cnt) = NULLC; /* Term with NULL */
  623. /* Have total bytes read. Now scan for NLN or ^Z. Either means end of
  624. * input statement, neither in 128 bytes means buffer overflow error.
  625. */
  626. if((i = mystrcspn(LexBuf, TEXT("\n\032"))) < mystrlen(LexBuf)
  627. || cnt == 0) { /*M029*/
  628. DEBUG((PAGRP, LFLVL, "FLBF: Scan found %04x", *(LexBuf+i)));
  629. DEBUG((PAGRP, LFLVL, "FLBF: At position %d", i));
  630. sptr = LexBuf+i; /* Set pointer */
  631. if(*sptr == CTRLZ) {
  632. *sptr = NLN;
  633. }
  634. if(*sptr == NLN) { /* If \n, inc... */
  635. ++sptr; /* ...ptr & sub */
  636. l = cnt - ++i; /* ....whats used */
  637. /* M014 ends */ i = FILE_CURRENT;
  638. } else { /* If ^Z, go EOF */
  639. l = 0;
  640. i = FILE_END;
  641. }
  642. DEBUG((PAGRP,LFLVL,"FLBF: Changing %04x to NULLC",*(sptr-1)));
  643. *sptr = NULLC; /* Term valid input */
  644. if (!FileIsDevice((CRTHANDLE)DataPtr)) {
  645. SetFilePointer(CRTTONT((CRTHANDLE)DataPtr), -l, NULL, i);
  646. DEBUG((PAGRP, LFLVL, "FLBF: Rewound %ld", l));
  647. if ((DataPtr == STDIN) && (!Necho)) {
  648. CmdPutString( LexBuf );
  649. }
  650. }
  651. } else if(i >= LBUFLEN) { /*M029*/
  652. /* @@4 */ if ( global_dfvalue == READFILE )
  653. /* @@4 */ {
  654. /* @@4 */ if ( EchoFlag == E_ON )
  655. /* @@4 */ {
  656. /* @@4 */ DEBUG((PAGRP, LXLVL,
  657. /* @@4 */ "BLOOP: Displaying Statement."));
  658. /* @@4 */
  659. /* @@4 */ PrintPrompt();
  660. /* @@4 */ cmd_printf(&LexBuffer[1]);
  661. /* @@4 */ cmd_printf(CrLf);
  662. /* @@4 */ }
  663. /* @@4 */ PutStdErr(MSG_LINES_TOO_LONG, NOARGS);
  664. /* @@4 */ Abort();
  665. /* @@4 */ }
  666. PutStdErr(MSG_LINES_TOO_LONG, NOARGS);
  667. /* M028 */ if(DataPtr == STDIN && FileIsDevice((CRTHANDLE)DataPtr))
  668. FlushKB();
  669. longjmp(CmdJBuf2,-1);
  670. }
  671. break;
  672. case READSTRING:
  673. DEBUG((PAGRP, LFLVL, "FLBF: Reading string."));
  674. *(LexBuf+LBUFLEN) = NULLC; /* Term max string */
  675. _tcsncpy(LexBuf, (TCHAR *) DataPtr, LBUFLEN);
  676. DataPtr += mystrlen(LexBuf)*sizeof(TCHAR); /* Update DataPtr */
  677. DEBUG((PAGRP, LFLVL, "FLBF: New DataPtr = %ws", DataPtr));
  678. break;
  679. }
  680. LexBufPtr = LexBuf; /* M004 - Reset pointer */
  681. SubVar(); /* Sub env & batch vars (M026) */
  682. DEBUG((PAGRP, LFLVL, "FLBF: Buffer contents: `%ws'", LexBufPtr));
  683. /* Insure that when buffer is refilled, the previous token pointer is
  684. * reset to the start of the lexer buffer
  685. */
  686. PrevLexPtr = LexBufPtr;
  687. }
  688. /*** LexCopy - copy the lex buffer
  689. *
  690. * Purpose:
  691. * To copy the contents read into the dos buffer into LexBuf,
  692. * translating double byte spaces into regular spaces in the
  693. * process.
  694. * Input:
  695. * A to and from pointer and a byte count.
  696. * Returned:
  697. * A new byte count which might be smaller than that passed in.
  698. */
  699. int LexCopy(to, from, count)
  700. TCHAR *to, *from;
  701. int count;
  702. {
  703. _tcsncpy( to, from, count );
  704. return count;
  705. }
  706. BOOLEAN PromptValid;
  707. TCHAR PromptVariableBuffer[ 512 ];
  708. TCHAR PromptBuffer[ 1024 ];
  709. void
  710. PrintPrompt()
  711. /*++
  712. Routine Description:
  713. To print Command's main input prompt and to interpret the special
  714. characters in it (see MSDOS manual for list of special prompt
  715. characters).
  716. An array of PROMPT_ENTRY structures called PromptTable is searched for
  717. the special characters. If a match is found , then either print out
  718. the special character if the format field is PLITFLAG or do some
  719. sort of special processing to print out the prompt string such as
  720. get time of day etc.
  721. Arguments:
  722. Return Value:
  723. --*/
  724. {
  725. TCHAR *pszPrompt;
  726. TCHAR *s;
  727. int nLeft, nUsed;
  728. ULONG idx;
  729. #if defined(FE_SB)
  730. // This local variable is used for determine the last
  731. // character is full width character (=DBCS) or not.
  732. TCHAR chLast = NULLC;
  733. #endif
  734. if (CtrlCSeen) {
  735. PutStdOut(MSG_C, NOARGS);
  736. ResetCtrlC();
  737. // Abort();
  738. }
  739. //
  740. // The newline which must preceed the prompt is tied to prompt rather than to
  741. // command completion in Dispatch.
  742. //
  743. // Also return without newline or prompt if echo status is "echo off".
  744. //
  745. if (EchoFlag == E_OFF) {
  746. return;
  747. }
  748. if (!NulNode) {
  749. cmd_printf(CrLf);
  750. }
  751. if ( PromptValid ) {
  752. pszPrompt = PromptVariableBuffer;
  753. }
  754. else {
  755. //
  756. // Fetch user prompt string from environment (should be PROMPT)
  757. //
  758. pszPrompt = GetEnvVar(PromptStr);
  759. if ( pszPrompt ) {
  760. mystrcpy( PromptVariableBuffer, pszPrompt);
  761. pszPrompt = PromptVariableBuffer;
  762. PromptValid = TRUE;
  763. }
  764. }
  765. //
  766. // refetch the current directory, since we may have lost the
  767. // drive letter due to net disconnect
  768. //
  769. GetDir(CurDrvDir, GD_DEFAULT);
  770. DEBUG((PAGRP, LFLVL, "PRINTPROMPT: pszPrompt = `%ws'", pszPrompt));
  771. s = PromptBuffer;
  772. *s = NULLC;
  773. nLeft = sizeof(PromptBuffer) / sizeof(TCHAR);
  774. //
  775. // Check if there was a prompt string.
  776. // If there is not prompt string then just print current drive
  777. //
  778. if (!pszPrompt || !*pszPrompt) {
  779. nUsed = _sntprintf( s, nLeft, Fmt27, CurDrvDir);
  780. s += nUsed;
  781. nLeft -= nUsed;
  782. } else {
  783. //
  784. // Loop through interpreting prompt string
  785. //
  786. for (; *pszPrompt; pszPrompt++) {
  787. //
  788. // Look for the escape character in prompt for special
  789. // processing
  790. //
  791. if (*pszPrompt != PROMPTESC) {
  792. nUsed = _sntprintf( s, nLeft, Fmt19, *pszPrompt);
  793. s += nUsed;
  794. nLeft -= nUsed;
  795. #if defined(FE_SB)
  796. // If character is full width character, mark it.
  797. if (IsDBCSCodePage() &&
  798. IsFullWidth(*pszPrompt))
  799. chLast = *pszPrompt;
  800. else
  801. chLast = NULLC;
  802. #endif
  803. } else {
  804. //
  805. // There is an escape character in prompt string.
  806. // Try to find a match for next character after escape
  807. // character from prompt table.
  808. //
  809. pszPrompt++;
  810. for (idx = 0; PromptTable[idx].Char != NULLC; idx++)
  811. if (_totupper(*pszPrompt) == PromptTable[idx].Char) {
  812. break;
  813. }
  814. if (PromptTable[idx].Char == NULLC) {
  815. //
  816. // Could find no match for escape. Exit loop to print
  817. // what we have so far
  818. //
  819. break;
  820. } else {
  821. if (PromptTable[idx].Format == PLITFLAG) {
  822. nUsed = _sntprintf( s, nLeft, Fmt19, PromptTable[idx].Literal);
  823. s += nUsed;
  824. nLeft -= nUsed;
  825. } else {
  826. switch (PromptTable[idx].Format) {
  827. case PTIMFLAG:
  828. nUsed = PrintTime(NULL, PT_TIME, s, nLeft);
  829. s += nUsed;
  830. nLeft -= nUsed;
  831. break;
  832. case PDATFLAG:
  833. nUsed = PrintDate(NULL, PD_DATE, s, nLeft);
  834. s += nUsed;
  835. nLeft -= nUsed;
  836. break;
  837. case PPATFLAG:
  838. nUsed = _sntprintf( s, nLeft, TEXT( "%s" ), CurDrvDir);
  839. s += nUsed;
  840. nLeft -= nUsed;
  841. break;
  842. case PVERFLAG:
  843. {
  844. TCHAR VersionFormat[32];
  845. PTCHAR Message;
  846. GetVersionString(
  847. VersionFormat,
  848. sizeof( VersionFormat ) / sizeof( VersionFormat[0] ));
  849. Message = GetMsg( MSG_MS_DOS_VERSION, VersionFormat );
  850. nUsed = _sntprintf( s, nLeft, TEXT( "%s" ), Message );
  851. s += nUsed;
  852. nLeft -= nUsed;
  853. LocalFree( Message );
  854. }
  855. break;
  856. case PBAKFLAG:
  857. #if defined(FE_SB) // PrintPrompt()
  858. // if the last character is full width character.
  859. // we should delete 2 bytes.
  860. if (chLast != NULLC)
  861. nUsed = _sntprintf( s, nLeft, DDBkSpc);
  862. else
  863. nUsed = _sntprintf( s, nLeft, DBkSpc);
  864. #else
  865. nUsed = _sntprintf( s, nLeft, DBkSpc);
  866. #endif
  867. s += nUsed;
  868. nLeft -= nUsed;
  869. break;
  870. case PNLNFLAG:
  871. nUsed = _sntprintf( s, nLeft, CrLf);
  872. s += nUsed;
  873. nLeft -= nUsed;
  874. break;
  875. case PDPTFLAG:
  876. //
  877. // $+ generates from zero to N plus characters, depending upon
  878. // the depth of the PUSHD directory stack.
  879. //
  880. if (fEnableExtensions) {
  881. int Depth = GetDirStackDepth( );
  882. while (nLeft && Depth--) {
  883. *s++ = TEXT( '+' );
  884. nLeft--;
  885. }
  886. }
  887. break;
  888. case PNETFLAG:
  889. //
  890. // $m generates the empty string if the current drive is not a
  891. // network drive. If it is, then $m generates the \\server\share
  892. // name with a trailing space.
  893. //
  894. if (!fEnableExtensions) {
  895. break;
  896. }
  897. {
  898. TCHAR CurDrive[4];
  899. TCHAR NetPath[MAX_PATH];
  900. DWORD n, Err;
  901. _tcsncpy( CurDrive, CurDrvDir, 2 );
  902. CurDrive[2] = BSLASH;
  903. CurDrive[3] = NULLC;
  904. if (GetDriveType( CurDrive ) != DRIVE_REMOTE) {
  905. break;
  906. }
  907. CurDrive[2] = NULLC;
  908. n = MAX_PATH;
  909. try {
  910. Err = WNetGetConnection( CurDrive, NetPath, &n );
  911. } except( Err = GetExceptionCode( ), EXCEPTION_EXECUTE_HANDLER) {
  912. }
  913. switch (Err) {
  914. case NO_ERROR:
  915. nUsed = _sntprintf( s, nLeft, TEXT("%s "), NetPath);
  916. s += nUsed;
  917. nLeft -= nUsed;
  918. break;
  919. case ERROR_NOT_CONNECTED:
  920. break;
  921. default:
  922. nUsed = _sntprintf( s, nLeft, TEXT("Unknown"));
  923. s += nUsed;
  924. nLeft -= nUsed;
  925. break;
  926. }
  927. }
  928. break;
  929. default:
  930. nUsed = _sntprintf( s, nLeft, Fmt19, CurDrvDir[0]);
  931. s += nUsed;
  932. nLeft -= nUsed;
  933. }
  934. }
  935. }
  936. }
  937. } // for
  938. } // else
  939. *s = NULLC;
  940. CmdPutString( PromptBuffer );
  941. //
  942. // If ^c seen while printing prompt blow it away
  943. //
  944. if (CtrlCSeen) {
  945. ResetCtrlC();
  946. }
  947. }
  948. /*** IsData - check the input buffer
  949. *
  950. * Purpose:
  951. * Check the lexer's input buffer to see if there is data in it.
  952. *
  953. * int IsData()
  954. *
  955. * Returns:
  956. * TRUE if the buffer has data in it.
  957. * FALSE if the buffer is empty.
  958. *
  959. */
  960. int IsData()
  961. {
  962. DEBUG((PAGRP, LXLVL, "ISDATA: *LexBufPtr = %04x", *LexBufPtr));
  963. if (*LexBufPtr)
  964. return(TRUE);
  965. return(FALSE);
  966. }
  967. /*** SubVar - Substitute for environment variables. (M004)
  968. *
  969. * Purpose:
  970. * This function scans the lexer input buffer looking for percent
  971. * signs and substituting batch variables and environment variables
  972. * as they are found.
  973. *
  974. * void SubVar()
  975. *
  976. * NOTES:
  977. * - This function does not return if expansion causes length to exceed
  978. * maximum line length (LBUFLEN).
  979. * - M026 caused complete rewrite to perform batch variable substitution
  980. * at the lexer stage rather than in batch processing. Note that the
  981. * printing of error messages can now be either line too long or token
  982. * too long, so error printing occurs before the longjmp() call.
  983. */
  984. void SubVar()
  985. {
  986. TCHAR lxtmp[LBUFLEN+1]; /* Temporary holding buffer */
  987. int dlen; /* Temps & counters */
  988. int j, slen;
  989. TCHAR *srcp = lxtmp; /* Src byte pointer */
  990. TCHAR *substr = NULL; /* Possible Env Var pointer */
  991. TCHAR c; /* Temp byte holder */
  992. mystrcpy(srcp,LexBufPtr); /* Make a copy of the input */
  993. DEBUG((PAGRP, LXLVL, "SBENV: Copy = %ws", srcp));
  994. dlen = j = slen = 0; /* Zero the counters */
  995. while ((c = *srcp++) && dlen <= LBUFLEN + 1) {
  996. if (c != PERCENT) {
  997. *LexBufPtr++ = c;
  998. ++dlen;
  999. if (c == NLN) /* Stop subst. if statement end */
  1000. break;
  1001. continue;
  1002. }
  1003. DEBUG((PAGRP,LXLVL,"SBVAR: Found `%%' in input"));
  1004. DEBUG((PAGRP,LXLVL,"SBVAR: Current pair is `%c%c'",c,*srcp));
  1005. if (CurrentBatchFile && *srcp == PERCENT) {
  1006. DEBUG((PAGRP,LXLVL,"SBVAR: Found `%%%%' in batch file"));
  1007. *LexBufPtr++ = *srcp++;
  1008. ++dlen;
  1009. continue;
  1010. }
  1011. //
  1012. // If inside a command script and extensions are enabled,
  1013. // expand %* into all the arguments (%1 through %n).
  1014. //
  1015. if (CurrentBatchFile && fEnableExtensions && *srcp == STAR) {
  1016. ++srcp; /* Kick past star */
  1017. slen = mystrlen(CurrentBatchFile->orgargs);
  1018. substr = CurrentBatchFile->orgargs;
  1019. DEBUG((PAGRP,LXLVL,"SBVAR: Found batch var %*"));
  1020. DEBUG((PAGRP,LXLVL,"SBVAR: - len = %d", slen));
  1021. DEBUG((PAGRP,LXLVL,"SBVAR: - var = %ws", substr));
  1022. if (slen > 0) {
  1023. if (dlen+slen > MAXTOKLEN) {
  1024. DEBUG((PAGRP,LXLVL,"SBVAR: Too Long!"));
  1025. _tcsncpy(LexBufPtr,substr,MAXTOKLEN - dlen);
  1026. LexBuf[MAXTOKLEN] = NULLC;
  1027. PutStdErr(MSG_TOKEN_TOO_LONG, ONEARG,LexBuf);
  1028. longjmp(CmdJBuf2,-1);
  1029. }
  1030. mystrcpy(LexBufPtr, substr);
  1031. dlen += slen;
  1032. LexBufPtr += slen;
  1033. DEBUG((PAGRP,LXLVL,"SBVAR: Subst complete; dest = `%ws'", LexBuf));
  1034. } else {
  1035. DEBUG((PAGRP,LXLVL,"SBVAR: Var %* undefined"));
  1036. }
  1037. continue;
  1038. }
  1039. //
  1040. // If inside a command script attempt to expand variable references
  1041. // of the form %n where n is a digit from 0 to 9
  1042. //
  1043. // If not in a command script or not a variable reference see if
  1044. // this is an environment variable expansion request.
  1045. //
  1046. if ((CurrentBatchFile != NULL
  1047. && (substr = MSCmdVar( &CmdJBuf2, srcp, &j, TEXT("0123456789"), CurrentBatchFile->aptrs )))
  1048. || (substr = MSEnvVar( &CmdJBuf2, srcp, &j, PERCENT )) != NULL ) {
  1049. DEBUG((PAGRP,LXLVL,"SBVAR: Found var %ws", substr));
  1050. //
  1051. // Either variable reference or environment variable reference.
  1052. // Copy the result to the input buffer
  1053. //
  1054. slen = mystrlen( substr );
  1055. dlen += slen;
  1056. if (dlen > LBUFLEN + 1) {
  1057. PutStdErr( MSG_LINES_TOO_LONG, NOARGS );
  1058. longjmp( CmdJBuf2, -1 );
  1059. }
  1060. mystrcpy( LexBufPtr, substr );
  1061. LexBufPtr += slen;
  1062. srcp += j;
  1063. } else {
  1064. DEBUG((PAGRP,LXLVL,"SBVAR: No var found"));
  1065. //
  1066. // Variable not found. If inside of command script, toss
  1067. // the variable reference in the bit bucket. If not in a
  1068. // command script pass the characters that make up the reference
  1069. // into the input buffer. User will see their mistake shortly.
  1070. //
  1071. if (CurrentBatchFile) {
  1072. srcp += j;
  1073. } else {
  1074. *LexBufPtr++ = c;
  1075. dlen++;
  1076. }
  1077. }
  1078. }
  1079. *LexBufPtr = NULLC; /* Terminate Statement */
  1080. LexBufPtr = LexBuf; /* Reset Pointer to start */
  1081. if (dlen > LBUFLEN+1) { /* Statement too long?? */
  1082. *LexBufPtr = NULLC; /* If so, kill line, print err */
  1083. PutStdErr(MSG_LINES_TOO_LONG, NOARGS);
  1084. longjmp(CmdJBuf2,-1);
  1085. }
  1086. }
  1087. /*** MSEnvVar - Does environment variable substitution
  1088. *
  1089. * Purpose:
  1090. * When percent signs are found in the newly filled lexer buffer,
  1091. * this function is called to determine if there is an environment
  1092. * variable substitution possible.
  1093. *
  1094. * TCHAR *MSEnvVar(TCHAR *str, int *supdate, TCHAR delim)
  1095. *
  1096. * Args:
  1097. * errjmp - optional pointer to jmp_buf for errors
  1098. * str - pointer to a possible environment variable name
  1099. * supdate - location to place env variable name length in
  1100. * delim - delimiter character to look for (e.g. PERCENT)
  1101. *
  1102. * Returns:
  1103. * If there is no ending delim,
  1104. * set supdate to 0
  1105. * return NULL
  1106. * else
  1107. * set supdate to the enclosed string length
  1108. * if the string is not an environment variable
  1109. * return NULL
  1110. * else
  1111. * return a pointer to the replacement string
  1112. *
  1113. * Notes:
  1114. * - M026 changed the way this function works so that supdate will
  1115. * contain the string length if any string was found. This allows
  1116. * the string to be deleted if within a batch file.
  1117. *
  1118. */
  1119. TCHAR *MSEnvVar(errjmp, str, supdate, delim)
  1120. jmp_buf *errjmp;
  1121. TCHAR *str;
  1122. int *supdate;
  1123. const TCHAR delim;
  1124. {
  1125. TCHAR TempBuf[LBUFLEN];
  1126. TCHAR *w0; /* Points to ending delim */
  1127. TCHAR *w1; /* Will hold ptr to env var value */
  1128. TCHAR *w2;
  1129. TCHAR *SearchString;
  1130. TCHAR *SearchStringEnd;
  1131. int SearchLength;
  1132. TCHAR *ReplaceString;
  1133. TCHAR *ReplaceStringEnd;
  1134. int ReplaceLength;
  1135. TCHAR TerminatingChar;
  1136. int noff, nlen;
  1137. BOOL PrefixMatch;
  1138. //
  1139. // Search for character that terminates the environment variable.
  1140. // This can be either the passed-in delimiter or a COLON
  1141. // (when extensions are enabled) as long as the colon isn't
  1142. // immediately followed by the delimiter. In this case, we
  1143. // treat the colon as part of the environment variable name.
  1144. //
  1145. w0 = str;
  1146. while (
  1147. // Stop at end of string
  1148. *w0 != NULLC &&
  1149. // Stop at delimiter
  1150. *w0 != delim &&
  1151. // Stop if extensions and colon not followed by delimiter
  1152. (!fEnableExtensions || w0[0] != COLON || w0[1] == delim)) {
  1153. w0++;
  1154. }
  1155. DEBUG((PAGRP, LFLVL, "MSENVVAR: *w0 = %04x", *w0));
  1156. //
  1157. // If there is no further text or if there are two delimiters
  1158. // in a row then the env var is not well formed and we terminate
  1159. //
  1160. if ( *w0 == NULLC || w0 == str) {
  1161. *supdate = 0;
  1162. return NULL;
  1163. }
  1164. //
  1165. // We have pointers to the start and end of the environment variable name.
  1166. // Terminate the name, look it up in the environment and restore the
  1167. // original name
  1168. //
  1169. TerminatingChar = *w0;
  1170. *w0 = NULLC;
  1171. DEBUG((PAGRP, LFLVL, "MSENVVAR: Possible env var = `%ws'", str));
  1172. w1 = GetEnvVar(str);
  1173. //
  1174. // w1 points to the environment variable value in a buffer that we
  1175. // can use. This static buffer is good until the next GetEnvVar call.
  1176. //
  1177. *w0++ = TerminatingChar;
  1178. //
  1179. // If Command Extensions are enabled, then we support munging the
  1180. // output of environment variable expansion. Here is the supported
  1181. // syntax, all keyed off a trailing COLON character at the end of
  1182. // the environment variable name. Note, that %FOO:% is treated
  1183. // as it was before.
  1184. //
  1185. // Environment variable substitution has been enhanced as follows:
  1186. //
  1187. // %PATH:str1=str2%
  1188. //
  1189. // would expand the PATH environment variable, substituting each
  1190. // occurrence of "str1" in the expanded result with "str2". "str2" can
  1191. // be the empty string to effectively delete all occurrences of "str1"
  1192. // from the expanded output. Additionally:
  1193. //
  1194. // %PATH:~10,5%
  1195. //
  1196. // would expand the PATH environment variable, and then use only the 5
  1197. // characters that begin at the 11th character of the expanded result.
  1198. // If the ,5 is left off, it will take the entire remainder of the
  1199. // expanded result.
  1200. //
  1201. //
  1202. // If we aren't doing extensions or we have a simple replacement
  1203. // or the named environment variable has no value then we're done.
  1204. //
  1205. // Note that we must do this for non-existent environment variables
  1206. // since the following will generate big problems:
  1207. // for %i in (c:foo) do ...
  1208. // The % will introduce an environment variable and the : will indicate
  1209. // a substitution. If we were to delete the above text, things break.
  1210. //
  1211. if (!fEnableExtensions
  1212. || TerminatingChar != COLON
  1213. || w1 == NULL) {
  1214. *supdate = (int)(w0 - str);
  1215. return w1;
  1216. }
  1217. if (*w0 == EQI) {
  1218. //
  1219. // We are extracting a substring %PATH:~10,5%
  1220. //
  1221. w0 += 1;
  1222. //
  1223. // Parse the offset within the environment string. A
  1224. // negative number indicates an offset from the right end of
  1225. // the string.
  1226. //
  1227. noff = _tcstol( w0, &w0, 0 );
  1228. if (noff < 0) {
  1229. noff += _tcslen( w1 );
  1230. }
  1231. noff = max( 0, min( noff, (int)_tcslen( w1 )));
  1232. //
  1233. // If a comma is present, what follows is the maximum length
  1234. // to be copied. Parse off this number, negative turns into
  1235. // an offset from the right of the string
  1236. //
  1237. if (*w0 == COMMA) {
  1238. //
  1239. // The length is specified; extract it and bound it by the
  1240. // length of the entire string
  1241. //
  1242. w0 += 1;
  1243. nlen = _tcstol( w0, &w0, 0 );
  1244. if (nlen < 0) {
  1245. nlen += _tcslen( w1 + noff );
  1246. }
  1247. nlen = max( 0, nlen );
  1248. nlen = min( nlen, (int)_tcslen( w1 + noff ));
  1249. } else {
  1250. nlen = _tcslen( w1 + noff );
  1251. }
  1252. //
  1253. // Copy from the string up to the requested length or the
  1254. // terminating NUL
  1255. //
  1256. _tcsncpy( w1, w1+noff, nlen );
  1257. //
  1258. // Terminate the string. We need to be extra careful here since the string
  1259. // may be the null string above which is in READ/ONLY memory.
  1260. //
  1261. if (w1[nlen] != NULLC) {
  1262. w1[nlen] = NULLC;
  1263. }
  1264. //
  1265. // We must have a terminating delimiter to make this a valid
  1266. // substitution.
  1267. //
  1268. if (*w0++ != delim) {
  1269. *supdate = 0;
  1270. return NULL;
  1271. }
  1272. *supdate = (int)(w0 - str);
  1273. } else {
  1274. //
  1275. // Not extracting a string, so must be search and replacing
  1276. //
  1277. // %PATH:str1=str2%
  1278. //
  1279. // Test for asterisk and skip forward looking for = sign or
  1280. // end of string
  1281. //
  1282. if (*w0 == STAR) {
  1283. w0++;
  1284. PrefixMatch = TRUE;
  1285. } else {
  1286. PrefixMatch = FALSE;
  1287. }
  1288. SearchString = w0;
  1289. while (*w0 != NULLC && *w0 != EQ) {
  1290. w0 += 1;
  1291. }
  1292. //
  1293. // No equal sign is no substitution
  1294. //
  1295. if (*w0 == NULLC) {
  1296. *supdate = 0;
  1297. return NULL;
  1298. }
  1299. SearchStringEnd = w0;
  1300. SearchLength = (int)(SearchStringEnd - SearchString);
  1301. //
  1302. // An empty search string is a syntax error
  1303. //
  1304. if (SearchLength == 0) {
  1305. if (errjmp != NULL) {
  1306. PutStdErr( MSG_SYNERR_GENL, ONEARG, SearchStringEnd );
  1307. longjmp( *errjmp, -1 );
  1308. }
  1309. *supdate = 0;
  1310. return NULL;
  1311. }
  1312. //
  1313. // Find end of replacement string
  1314. //
  1315. w0++;
  1316. ReplaceString = w0;
  1317. while (*w0 && *w0 != delim) {
  1318. w0 += 1;
  1319. }
  1320. //
  1321. // No terminating delimiter is no substitution
  1322. //
  1323. if (*w0 == NULLC) {
  1324. *supdate = 0;
  1325. return NULL;
  1326. }
  1327. ReplaceStringEnd = w0++;
  1328. ReplaceLength = (int)(ReplaceStringEnd - ReplaceString);
  1329. //
  1330. // Everything is well-formed now. Set the correct number
  1331. // of source chars to be skipped.
  1332. //
  1333. *supdate = (int)(w0 - str);
  1334. //
  1335. // If there's actually a string to replace
  1336. //
  1337. if (*w1 != TEXT( '\0' )) {
  1338. //
  1339. // Copy env var into temp buffer so that we can
  1340. // perform a replacement copy back to original buffer
  1341. //
  1342. _tcscpy( TempBuf, w1 );
  1343. w2 = TempBuf;
  1344. w0 = w1;
  1345. //
  1346. // Walk forward through w2 trying to find instances of
  1347. // the search string and replacing them into w2
  1348. //
  1349. while (TRUE) {
  1350. if (!_tcsnicmp( w2, SearchString, SearchLength )) {
  1351. //
  1352. // A prefix match means replacing everything from the
  1353. // beginning of the string through the end of the search
  1354. // match with the replacement string and then bailing
  1355. //
  1356. if (PrefixMatch) {
  1357. _tcsncpy( w1, ReplaceString, ReplaceLength );
  1358. _tcscpy( w1 + ReplaceLength, w2 + SearchLength );
  1359. break;
  1360. }
  1361. //
  1362. // Otherwise, we copy in the replacement string and
  1363. // skip over the search string
  1364. //
  1365. _tcsncpy( w0, ReplaceString, ReplaceLength );
  1366. w0 += ReplaceLength;
  1367. w2 += SearchLength;
  1368. } else {
  1369. //
  1370. // Not a match, copy this one character
  1371. //
  1372. *w0++ = *w2++;
  1373. if (w0[-1] == NULLC) {
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. }
  1379. }
  1380. return w1;
  1381. }
  1382. struct {
  1383. ULONG dwAttributeFlag;
  1384. TCHAR chAttributeChar;
  1385. } rgAttrToCharMap[] = {
  1386. {FILE_ATTRIBUTE_DIRECTORY , TEXT('d')},
  1387. {FILE_ATTRIBUTE_READONLY , TEXT('r')},
  1388. {FILE_ATTRIBUTE_ARCHIVE , TEXT('a')},
  1389. {FILE_ATTRIBUTE_HIDDEN , TEXT('h')},
  1390. {FILE_ATTRIBUTE_SYSTEM , TEXT('s')},
  1391. {FILE_ATTRIBUTE_COMPRESSED, TEXT('c')},
  1392. {FILE_ATTRIBUTE_OFFLINE , TEXT('o')},
  1393. {FILE_ATTRIBUTE_TEMPORARY , TEXT('t')},
  1394. {FILE_ATTRIBUTE_REPARSE_POINT, TEXT('l')},
  1395. {0, NULLC}
  1396. };
  1397. /*** MSCmdVar - Does command variable substitution
  1398. *
  1399. * Purpose:
  1400. * When percent signs are found in the newly filled lexer buffer,
  1401. * this function is called to determine if there is a command processor
  1402. * variable substitution possible.
  1403. *
  1404. * TCHAR *MSCmdVar(TCHAR *srcp, int *supdate, TCHAR *vars, TCHAR *subs[])
  1405. *
  1406. * Args:
  1407. * errjmp - optional pointer to jmp_buf for errors
  1408. * srcp - pointer to a possible variable name
  1409. * supdate - location to place variable name length in
  1410. * vars - array of character variable names to look for
  1411. * subs - array of substitution strings for each variable name.
  1412. *
  1413. * Returns:
  1414. * If there is no ending delimiter
  1415. * set supdate to 0
  1416. * return NULL
  1417. * else
  1418. * set supdate to the enclosed string length
  1419. * if the string is not a variable
  1420. * return NULL
  1421. * else
  1422. * return a pointer to the replacement string
  1423. */
  1424. TCHAR *MSCmdVar(errjmp, srcp, supdate, vars, subs)
  1425. jmp_buf *errjmp;
  1426. TCHAR *srcp;
  1427. int *supdate;
  1428. TCHAR *vars;
  1429. TCHAR *subs[];
  1430. {
  1431. static TCHAR ValidModifiers[] = TEXT( "fdpnxsatz" );
  1432. TCHAR *substr;
  1433. TCHAR *s1;
  1434. substr = NULL;
  1435. *supdate = 0;
  1436. //
  1437. // If extensions are enabled, we support the following syntax for expanding
  1438. // variable values:
  1439. // %~fi - expands %i to a fully qualified path name
  1440. // %~di - expands %i to a drive letter only
  1441. // %~pi - expands %i to a path only
  1442. // %~ni - expands %i to a file name only
  1443. // %~xi - expands %i to a file extension only
  1444. // %~si - changes the meaning of n and x options to
  1445. // reference the short name instead
  1446. // %~$PATH:i - searches the directories listed in the PATH
  1447. // environment variable and expands %i to the
  1448. // fully qualified name of the first one found.
  1449. // If the environment variable name is not
  1450. // defined or the file is not found by the
  1451. // search, then this modifier expands to the
  1452. // empty string
  1453. //
  1454. // The modifiers can be combined to get compound results:
  1455. //
  1456. // %~dpi - expands %i to a drive letter and path only
  1457. // %~nxi - expands %i to a file name and extension only
  1458. // %~dp$PATH:i - searches the directories listed in the PATH
  1459. // environment variable for %i and expands to the
  1460. // drive letter and path of the first one found.
  1461. //
  1462. //
  1463. // See if new syntax is being specified
  1464. //
  1465. if (fEnableExtensions && *srcp == EQI) {
  1466. #define PATHMODIFIER 0x8000
  1467. #define ATTRMODIFIER 0x4000
  1468. #define WANTFULLPATH (0x0001)
  1469. #define WANTDRIVE (0x0002)
  1470. #define WANTPATH (0x0004)
  1471. #define WANTNAME (0x0008)
  1472. #define WANTEXTENSION (0x0010)
  1473. #define WANTSHORTNAME (0x0020)
  1474. #define PATHBITS (0x00FF)
  1475. #define WANTATTRIB (0x0100)
  1476. #define WANTTIMESTAMP (0x0200)
  1477. #define WANTSIZE (0x0400)
  1478. #define ATTRBITS (0x0F00)
  1479. ULONG Modifiers = 0;
  1480. ULONG LastModifierValue = 0;
  1481. TCHAR *LastVariablePosition;
  1482. TCHAR FullPath[ 2 * MAX_PATH], NullExt;
  1483. TCHAR *FilePart, *Extension, *StartPath, *VarName, *StartBuf;
  1484. const TCHAR *SearchVar;
  1485. DWORD FullPathLength;
  1486. FullPathLength = 0;
  1487. SearchVar = NULL;
  1488. StartBuf = srcp-1;
  1489. s1 = NULL;
  1490. srcp++;
  1491. LastVariablePosition = srcp;
  1492. //
  1493. // Walk forward through the string, remembering where we see variables
  1494. // and saving the position and status of the modifiers at the most
  1495. // recent variable. We stop when we encounter a char that cannot be a
  1496. // modifier. If that char is a variable, we use it. Otherwise, we
  1497. // reset the pointer to the most recently found variable.
  1498. //
  1499. while (*srcp != TEXT( '\0' ) &&
  1500. *srcp != TEXT( '$' ) &&
  1501. _tcsrchr( ValidModifiers, _totlower( *srcp )) != NULL) {
  1502. //
  1503. // The character we are looking at is a valid modifier. If it is
  1504. // a variable, save this location
  1505. //
  1506. if ( _tcsrchr( vars, *srcp ) != NULL) {
  1507. LastVariablePosition = srcp;
  1508. LastModifierValue = Modifiers;
  1509. }
  1510. //
  1511. // Add up the functions that this modifier demands
  1512. //
  1513. switch (_totlower( *srcp )) {
  1514. case TEXT('f'): Modifiers |= PATHMODIFIER | WANTFULLPATH; break;
  1515. case TEXT('d'): Modifiers |= PATHMODIFIER | WANTDRIVE; break;
  1516. case TEXT('p'): Modifiers |= PATHMODIFIER | WANTPATH; break;
  1517. case TEXT('n'): Modifiers |= PATHMODIFIER | WANTNAME; break;
  1518. case TEXT('x'): Modifiers |= PATHMODIFIER | WANTEXTENSION; break;
  1519. case TEXT('s'): Modifiers |= PATHMODIFIER | WANTSHORTNAME; break;
  1520. case TEXT('a'): Modifiers |= ATTRMODIFIER | WANTATTRIB; break;
  1521. case TEXT('t'): Modifiers |= ATTRMODIFIER | WANTTIMESTAMP; break;
  1522. case TEXT('z'): Modifiers |= ATTRMODIFIER | WANTSIZE; break;
  1523. default:
  1524. //
  1525. // This had better not occur
  1526. //
  1527. if (errjmp != NULL) {
  1528. PutStdErr(MSG_PATH_OPERATOR_INVALID, ONEARG, StartBuf);
  1529. longjmp(*errjmp,-1);
  1530. } else {
  1531. return NULL;
  1532. }
  1533. }
  1534. srcp++;
  1535. }
  1536. //
  1537. // *srcp is no longer pointing at a valid modifier.
  1538. // It may be:
  1539. // EOS - we back up to the previously saved variable position
  1540. // $ - perform special environment variable scanning
  1541. // some other char -
  1542. // if this is not a variable, we back up to the prevously
  1543. // saved variable position
  1544. //
  1545. if (*srcp == TEXT( '\0' )) {
  1546. //
  1547. // Restore bak to last found variable
  1548. //
  1549. srcp = LastVariablePosition;
  1550. Modifiers = LastModifierValue;
  1551. } else if (*srcp == TEXT( '$' )) {
  1552. //
  1553. // Save beginning of environment variable
  1554. //
  1555. VarName = ++srcp;
  1556. //
  1557. // Look for : If not present, then error
  1558. //
  1559. srcp = _tcschr( srcp, COLON );
  1560. if (srcp == NULL) {
  1561. if (errjmp != NULL) {
  1562. PutStdErr(MSG_PATH_OPERATOR_INVALID, ONEARG, StartBuf);
  1563. longjmp(*errjmp,-1);
  1564. } else {
  1565. return NULL;
  1566. }
  1567. }
  1568. //
  1569. // Look up environment variable to search
  1570. //
  1571. *srcp = NULLC;
  1572. SearchVar = MyGetEnvVarPtr( VarName );
  1573. if (SearchVar == NULL) {
  1574. SearchVar = (TCHAR *)-1;
  1575. }
  1576. *srcp++ = COLON;
  1577. Modifiers |= PATHMODIFIER;
  1578. } else if (_tcsrchr( vars, *srcp) == NULL) {
  1579. //
  1580. // Restore back to last found variable
  1581. //
  1582. srcp = LastVariablePosition;
  1583. Modifiers = LastModifierValue;
  1584. }
  1585. //
  1586. // If we didn't find a variable, bail out.
  1587. //
  1588. s1 = _tcsrchr( vars, *srcp );
  1589. if (s1 == NULL) {
  1590. if (errjmp != NULL) {
  1591. PutStdErr(MSG_PATH_OPERATOR_INVALID, ONEARG, StartBuf);
  1592. longjmp(*errjmp,-1);
  1593. } else {
  1594. return NULL;
  1595. }
  1596. }
  1597. //
  1598. // Get current value of variable (strip surrounding quotes)
  1599. //
  1600. substr = subs[s1 - vars];
  1601. if (substr != NULL) {
  1602. if (*substr == QUOTE) {
  1603. substr = dupstr( substr + 1 );
  1604. if (substr == NULL) {
  1605. if (errjmp != NULL) {
  1606. PutStdErr( ERROR_NOT_ENOUGH_MEMORY, NOARGS );
  1607. longjmp( *errjmp, -1 );
  1608. } else {
  1609. return NULL;
  1610. }
  1611. }
  1612. s1 = lastc( substr );
  1613. if (*s1 == QUOTE)
  1614. *s1 = NULLC;
  1615. } else if (*srcp == TEXT('0') &&
  1616. CurrentBatchFile != NULL &&
  1617. CurrentBatchFile->orgaptr0 == substr &&
  1618. SearchVar == NULL &&
  1619. (Modifiers & (PATHMODIFIER | ATTRMODIFIER)) != 0
  1620. ) {
  1621. substr = CurrentBatchFile->filespec;
  1622. }
  1623. }
  1624. //
  1625. // Skip past the variable name letter and tell caller how much of the
  1626. // source string we consumed.
  1627. //
  1628. ++srcp;
  1629. *supdate = (int)(srcp - StartBuf) - 1;
  1630. //
  1631. // If the variable has a value, then apply the modifiers to the
  1632. // value.
  1633. //
  1634. if (substr != NULL && *substr != TEXT( '\0' )) {
  1635. //
  1636. // If no path or attribute modifiers request then all done.
  1637. //
  1638. if ((Modifiers & (PATHMODIFIER | ATTRMODIFIER)) == 0) {
  1639. substr = dupstr( substr );
  1640. goto alldone;
  1641. }
  1642. //
  1643. // If requested searching an environment variable path, do that.
  1644. //
  1645. FullPath[0] = NULLC;
  1646. if (SearchVar != NULL) {
  1647. if (SearchVar != (TCHAR *)-1) {
  1648. FullPathLength = SearchPath( SearchVar,
  1649. substr,
  1650. NULL,
  1651. sizeof( FullPath ) / sizeof( FullPath[0] ),
  1652. FullPath,
  1653. &FilePart);
  1654. if (FullPathLength == 0) {
  1655. SearchVar = (TCHAR *)-1;
  1656. } else if ((Modifiers & PATHBITS) == 0) {
  1657. Modifiers |= PATHMODIFIER | WANTFULLPATH;
  1658. }
  1659. }
  1660. }
  1661. if (SearchVar == NULL) {
  1662. //
  1663. // If not searching environment variable path, start with full path.
  1664. //
  1665. FullPathLength = GetFullPathName( substr,
  1666. sizeof( FullPath ) / sizeof( FullPath[0] ),
  1667. FullPath,
  1668. &FilePart);
  1669. } else if (SearchVar == (TCHAR *)-1) {
  1670. //
  1671. // If search of environment variable path failed, result is empty string
  1672. //
  1673. substr = NULL;
  1674. }
  1675. if (FilePart == NULL)
  1676. FilePart = _tcschr( FullPath, NULLC );
  1677. //
  1678. // Fixup the path to have same case as on disk, substituting short
  1679. // names if requested.
  1680. //
  1681. FixupPath( FullPath, (Modifiers & WANTSHORTNAME) );
  1682. //
  1683. // If we have a full path, the result gets the portions requested by
  1684. // the user, unless they wanted the full path, in which case there is
  1685. // nothing more to do.
  1686. //
  1687. if (FullPathLength != 0) {
  1688. TCHAR Buffer[ 2 * MAX_PATH ];
  1689. TCHAR *s;
  1690. s = Buffer;
  1691. if ((Modifiers & ATTRMODIFIER) != 0) {
  1692. struct tm FileTime;
  1693. LARGE_INTEGER FileSize;
  1694. WIN32_FILE_ATTRIBUTE_DATA FileInfo;
  1695. int i;
  1696. BOOL bResult;
  1697. #ifdef WIN95_CMD
  1698. WIN32_FIND_DATA Win95FileInfo;
  1699. HANDLE hFind;
  1700. hFind = FindFirstFile( FullPath, &Win95FileInfo );
  1701. if (hFind != INVALID_HANDLE_VALUE) {
  1702. FileInfo.dwFileAttributes = Win95FileInfo.dwFileAttributes;
  1703. FileInfo.ftCreationTime = Win95FileInfo.ftCreationTime;
  1704. FileInfo.ftLastAccessTime = Win95FileInfo.ftLastAccessTime;
  1705. FileInfo.ftLastWriteTime = Win95FileInfo.ftLastWriteTime;
  1706. FileInfo.nFileSizeHigh = Win95FileInfo.nFileSizeHigh;
  1707. FileInfo.nFileSizeLow = Win95FileInfo.nFileSizeLow;
  1708. FindClose( hFind );
  1709. bResult = TRUE;
  1710. } else {
  1711. bResult = FALSE;
  1712. }
  1713. #else
  1714. bResult = GetFileAttributesEx( FullPath,
  1715. GetFileExInfoStandard,
  1716. &FileInfo
  1717. );
  1718. #endif
  1719. if (bResult) {
  1720. if ((Modifiers & WANTATTRIB) != 0) {
  1721. i = 0;
  1722. while (rgAttrToCharMap[i].chAttributeChar) {
  1723. if (FileInfo.dwFileAttributes & rgAttrToCharMap[i].dwAttributeFlag)
  1724. *s++ = rgAttrToCharMap[i].chAttributeChar;
  1725. else
  1726. *s++ = TEXT('-');
  1727. i += 1;
  1728. }
  1729. }
  1730. if ((Modifiers & WANTTIMESTAMP) != 0) {
  1731. ConvertFILETIMETotm( &FileInfo.ftLastWriteTime, &FileTime );
  1732. if (s != Buffer)
  1733. *s++ = SPACE;
  1734. s += PrintDate( &FileTime, PD_DIR, s, MAX_PATH );
  1735. *s++ = SPACE;
  1736. s += PrintTime( &FileTime, PT_DIR, s, MAX_PATH );
  1737. }
  1738. if ((Modifiers & WANTSIZE) != 0) {
  1739. if (s != Buffer)
  1740. *s++ = SPACE;
  1741. FileSize.LowPart = FileInfo.nFileSizeLow;
  1742. FileSize.HighPart = FileInfo.nFileSizeHigh;
  1743. // THOUSANDSEPSWITCH
  1744. s += FormatFileSize( 0, &FileSize, 0, s );
  1745. }
  1746. }
  1747. }
  1748. if ((Modifiers & PATHMODIFIER) != 0 ||
  1749. (Modifiers & ATTRMODIFIER) == 0) {
  1750. if (s != Buffer)
  1751. *s++ = SPACE;
  1752. if ((Modifiers & WANTFULLPATH) == 0 &&
  1753. ((Modifiers & WANTSHORTNAME) == 0 ||
  1754. (Modifiers & (WANTDRIVE | WANTPATH | WANTEXTENSION | WANTNAME)) != 0
  1755. )
  1756. ) {
  1757. StartPath = FullPath + 2;
  1758. if ((Modifiers & WANTDRIVE) == 0) {
  1759. StartPath = _tcscpy(FullPath, StartPath);
  1760. FilePart -= 2;
  1761. }
  1762. if ((Modifiers & WANTPATH) == 0)
  1763. FilePart = _tcscpy(StartPath, FilePart);
  1764. Extension = _tcsrchr(FilePart, DOT);
  1765. if (Extension == NULL) {
  1766. NullExt = NULLC;
  1767. Extension = &NullExt;
  1768. }
  1769. if ((Modifiers & WANTEXTENSION) == 0)
  1770. *Extension = NULLC;
  1771. if ((Modifiers & WANTNAME) == 0)
  1772. _tcscpy(FilePart, Extension);
  1773. }
  1774. _tcscpy(s, FullPath);
  1775. s += _tcslen(s);
  1776. }
  1777. *s = NULLC;
  1778. substr = dupstr( Buffer );
  1779. }
  1780. }
  1781. } else if (*srcp && (s1 = _tcsrchr(vars, *srcp))) {
  1782. //
  1783. // Old syntax. Result is value of variable
  1784. //
  1785. substr = subs[s1 - vars]; /* Found variable*/
  1786. //
  1787. // Skip past the variable name letter and tell caller how much of the
  1788. // source string we consumed.
  1789. //
  1790. ++srcp; /* Kick past name*/
  1791. *supdate += 1;
  1792. }
  1793. alldone:
  1794. //
  1795. // If result was empty, then return the null string. Otherwise return the result
  1796. //
  1797. if (!substr && *supdate != 0)
  1798. return TEXT("");
  1799. else
  1800. return substr;
  1801. }