ReactOS 0.4.15-dev-7842-g558ab78
batch.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _BATCH_CONTEXT
 
struct  _FOR_CONTEXT
 

Macros

#define MSCMD_BATCH_ECHO
 
#define BATCH_BUFFSIZE   8192
 

Typedefs

typedef enum _BATCH_TYPE BATCH_TYPE
 
typedef struct _BATCH_CONTEXT BATCH_CONTEXT
 
typedef struct _BATCH_CONTEXTPBATCH_CONTEXT
 
typedef struct _FOR_CONTEXT FOR_CONTEXT
 
typedef struct _FOR_CONTEXTPFOR_CONTEXT
 

Enumerations

enum  _BATCH_TYPE { NONE , BAT_TYPE , CMD_TYPE }
 

Functions

BOOL FindArg (IN TCHAR Char, OUT PCTSTR *ArgPtr, OUT BOOL *IsParam0)
 
VOID ExitBatch (VOID)
 
VOID ExitAllBatches (VOID)
 
INT Batch (LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *)
 
BOOL BatchGetString (LPTSTR lpBuffer, INT nBufferLength)
 
LPTSTR ReadBatchLine (VOID)
 
VOID AddBatchRedirection (REDIRECTION **)
 

Variables

BATCH_TYPE BatType
 
PBATCH_CONTEXT bc
 
PFOR_CONTEXT fc
 
BOOL bBcEcho
 
BOOL bEcho
 
TCHAR textline [BATCH_BUFFSIZE]
 

Macro Definition Documentation

◆ BATCH_BUFFSIZE

#define BATCH_BUFFSIZE   8192

Definition at line 68 of file batch.h.

◆ MSCMD_BATCH_ECHO

#define MSCMD_BATCH_ECHO

Definition at line 24 of file batch.h.

Typedef Documentation

◆ BATCH_CONTEXT

◆ BATCH_TYPE

◆ FOR_CONTEXT

◆ PBATCH_CONTEXT

◆ PFOR_CONTEXT

Enumeration Type Documentation

◆ _BATCH_TYPE

Enumerator
NONE 
BAT_TYPE 
CMD_TYPE 

Definition at line 15 of file batch.h.

16{
17 NONE,
18 BAT_TYPE, /* Old-style DOS batch file */
19 CMD_TYPE /* New-style NT OS/2 batch file */
@ BAT_TYPE
Definition: batch.h:18
@ CMD_TYPE
Definition: batch.h:19
@ NONE
Definition: batch.h:17
enum _BATCH_TYPE BATCH_TYPE

Function Documentation

◆ AddBatchRedirection()

VOID AddBatchRedirection ( REDIRECTION **  RedirList)

Definition at line 507 of file batch.c.

508{
510
511 /* Prepend the list to the batch context's list */
512 ListEnd = RedirList;
513 while (*ListEnd)
514 ListEnd = &(*ListEnd)->Next;
515 *ListEnd = bc->RedirList;
516 bc->RedirList = *RedirList;
517
518 /* Null out the pointer so that the list will not be cleared prematurely.
519 * These redirections should persist until the batch file exits. */
520 *RedirList = NULL;
521}
PBATCH_CONTEXT bc
Definition: batch.c:67
#define NULL
Definition: types.h:112
REDIRECTION * RedirList
Definition: batch.h:40
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY ListEnd
Definition: exfuncs.h:1224

Referenced by Batch().

◆ Batch()

INT Batch ( LPTSTR  fullname,
LPTSTR  firstword,
LPTSTR  param,
PARSED_COMMAND Cmd 
)

Definition at line 299 of file batch.c.

300{
301 INT ret = 0;
302 INT i;
303 HANDLE hFile = NULL;
304 BOOLEAN bSameFn = FALSE;
305 BOOLEAN bTopLevel;
306 BATCH_CONTEXT new;
307 PFOR_CONTEXT saved_fc;
308
309 SetLastError(0);
310 if (bc && bc->mem)
311 {
312 TCHAR fpname[MAX_PATH];
313 GetFullPathName(fullname, ARRAYSIZE(fpname), fpname, NULL);
314 if (_tcsicmp(bc->BatchFilePath, fpname) == 0)
315 bSameFn = TRUE;
316 }
317 TRACE("Batch(\'%s\', \'%s\', \'%s\') bSameFn = %d\n",
318 debugstr_aw(fullname), debugstr_aw(firstword), debugstr_aw(param), bSameFn);
319
320 if (!bSameFn)
321 {
325
327 {
329 return 1;
330 }
331 }
332
333 /*
334 * Remember whether this is a top-level batch context, i.e. if there is
335 * no batch context existing prior (bc == NULL originally), and we are
336 * going to create one below.
337 */
338 bTopLevel = !bc;
339
340 if (bc != NULL && Cmd == bc->current)
341 {
342 /* Then we are transferring to another batch */
343 ClearBatch();
344 AddBatchRedirection(&Cmd->Redirections);
345 }
346 else
347 {
348 struct _SETLOCAL *setlocal = NULL;
349
350 if (Cmd == NULL)
351 {
352 /* This is a CALL. CALL will set errorlevel to our return value, so
353 * in order to keep the value of errorlevel unchanged in the case
354 * of calling an empty batch file, we must return that same value. */
356 }
357 else if (bc)
358 {
359 /* If a batch file runs another batch file as part of a compound command
360 * (e.g. "x.bat & somethingelse") then the first file gets terminated. */
361
362 /* Get its SETLOCAL stack so it can be migrated to the new context */
363 setlocal = bc->setlocal;
364 bc->setlocal = NULL;
365 ExitBatch();
366 }
367
368 /* Create a new context. This function will not
369 * return until this context has been exited */
370 new.prev = bc;
371 /* copy some fields in the new structure if it is the same file */
372 if (bSameFn)
373 {
374 new.mem = bc->mem;
375 new.memsize = bc->memsize;
376 new.mempos = 0;
377 new.memfree = FALSE; /* don't free this, being used before this */
378 }
379 bc = &new;
380 bc->RedirList = NULL;
381 bc->setlocal = setlocal;
382 }
383
385
386 /* If a new batch file, load it into memory and close the file */
387 if (!bSameFn)
388 {
391 }
392
393 bc->mempos = 0; /* Go to the beginning of the batch file */
394#ifndef MSCMD_BATCH_ECHO
395 bc->bEcho = bEcho; /* Preserve echo across batch calls */
396#endif
397 for (i = 0; i < 10; i++)
398 bc->shiftlevel[i] = i;
399
400 /* Parse the batch parameters */
401 if (!BatchParams(firstword, param, &bc->raw_params, &bc->params))
402 return 1;
403
404 /* If we are calling from inside a FOR, hide the FOR variables */
405 saved_fc = fc;
406 fc = NULL;
407
408 /* Perform top-level batch initialization */
409 if (bTopLevel)
410 {
411 TCHAR *dot;
412
413 /* Default the top-level batch context type to .BAT */
415
416 /* If this is a .CMD file, adjust the type */
417 dot = _tcsrchr(bc->BatchFilePath, _T('.'));
418 if (dot && (!_tcsicmp(dot, _T(".cmd"))))
419 {
421 }
422
423#ifdef MSCMD_BATCH_ECHO
424 bBcEcho = bEcho;
425#endif
426 }
427
428 /* If this is a "CALL :label args ...", call a subroutine of
429 * the current batch file, only if extensions are enabled. */
430 if (bEnableExtensions && (*firstword == _T(':')))
431 {
432 LPTSTR expLabel;
433
434 /* Position at the place of the parent file (which is the same as the caller) */
435 bc->mempos = (bc->prev ? bc->prev->mempos : 0);
436
437 /*
438 * Jump to the label. Strip the label's colon; as a side-effect
439 * this will forbid "CALL :EOF"; however "CALL ::EOF" will work!
440 */
441 bc->current = Cmd;
442 ++firstword;
443
444 /* Expand the label only! (simulate a GOTO command as in Windows' CMD) */
445 expLabel = DoDelayedExpansion(firstword);
446 ret = cmd_goto(expLabel ? expLabel : firstword);
447 if (expLabel)
448 cmd_free(expLabel);
449 }
450
451 /* If we have created a new context, don't return
452 * until this batch file has completed. */
453 while (bc == &new && !bExit)
454 {
456 if (!Cmd)
457 {
458 if (!bParseError)
459 continue;
460
461 /* Echo the pre-parsed batch file line on error */
462 if (bEcho && !bDisableBatchEcho)
463 {
464 if (!bIgnoreEcho)
465 ConOutChar(_T('\n'));
466 PrintPrompt();
468 ConOutChar(_T('\n'));
469 }
470 /* Stop all execution */
472 ret = 1;
473 break;
474 }
475
476 /* JPP 19980807 */
477 /* Echo the command and execute it */
478 bc->current = Cmd;
481 }
482 if (bExit)
483 {
484 /* Stop all execution */
486 }
487
488 /* Perform top-level batch cleanup */
489 if (!bc || bTopLevel)
490 {
491 /* Reset the top-level batch context type */
492 BatType = NONE;
493
494#ifdef MSCMD_BATCH_ECHO
495 bEcho = bBcEcho;
496#endif
497 }
498
499 /* Restore the FOR variables */
500 fc = saved_fc;
501
502 /* Always return the last command's return code */
503 TRACE("Batch: returns %d\n", ret);
504 return ret;
505}
unsigned char BOOLEAN
VOID AddBatchRedirection(REDIRECTION **RedirList)
Definition: batch.c:507
static void BatchFile2Mem(HANDLE hBatchFile)
Definition: batch.c:270
BOOL bBcEcho
Definition: batch.c:70
BATCH_TYPE BatType
Definition: batch.c:66
VOID ExitBatch(VOID)
Definition: batch.c:222
BOOL bEcho
Definition: batch.c:73
static VOID ClearBatch(VOID)
Definition: batch.c:198
static BOOL BatchParams(IN PCTSTR Arg0, IN PCTSTR Args, OUT PTSTR *RawParams, OUT PTSTR *ParamList)
Definition: batch.c:119
VOID ExitAllBatches(VOID)
Definition: batch.c:261
INT nErrorLevel
Definition: cmd.c:158
BOOL bDisableBatchEcho
Definition: cmd.c:160
BOOL bExit
Definition: cmd.c:152
INT ExecuteCommandWithEcho(IN PARSED_COMMAND *Cmd)
Definition: cmd.c:868
PTSTR DoDelayedExpansion(IN PCTSTR Line)
Definition: cmd.c:1640
BOOL bIgnoreEcho
Definition: cmd.c:155
PARSED_COMMAND * ParseCommand(IN PCTSTR Line)
Definition: parser.c:1461
BOOL bParseError
Definition: parser.c:90
VOID PrintPrompt(VOID)
Definition: prompt.c:109
TCHAR ParseLine[CMDLINE_LENGTH]
Definition: parser.c:92
INT cmd_goto(LPTSTR)
Definition: goto.c:36
VOID FreeCommand(IN OUT PARSED_COMMAND *Cmd)
Definition: parser.c:527
VOID ConOutChar(TCHAR c)
Definition: console.c:123
#define ConErrResPuts(uID)
Definition: console.h:38
#define ConOutPuts(szStr)
Definition: console.h:29
#define debugstr_aw
Definition: precomp.h:43
#define STRING_BATCH_ERROR
Definition: resource.h:23
PFOR_CONTEXT fc
Definition: for.c:57
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
GLfloat param
Definition: glext.h:5796
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
if(dx< 0)
Definition: linetemp.h:194
#define _tcsrchr
Definition: utility.h:116
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
const char * fullname
Definition: shader.c:1766
BOOL bEnableExtensions
Definition: more.c:53
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define NONE
Definition: ntddpar.h:94
@ Cmd
Definition: sacdrv.h:278
#define TRACE(s)
Definition: solgame.cpp:4
DWORD memsize
Definition: batch.h:30
char * mem
Definition: batch.h:29
struct _BATCH_CONTEXT * prev
Definition: batch.h:28
struct _SETLOCAL * setlocal
Definition: batch.h:42
LPTSTR params
Definition: batch.h:34
TCHAR BatchFilePath[MAX_PATH]
Definition: batch.h:33
LPTSTR raw_params
Definition: batch.h:35
INT shiftlevel[10]
Definition: batch.h:36
PARSED_COMMAND * current
Definition: batch.h:41
DWORD mempos
Definition: batch.h:31
int32_t INT
Definition: typedefs.h:58
#define _T(x)
Definition: vfdio.h:22
int ret
#define CreateFile
Definition: winbase.h:3684
#define GetFullPathName
Definition: winbase.h:3756
char TCHAR
Definition: xmlstorage.h:189
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define _tcsicmp
Definition: xmlstorage.h:205

◆ BatchGetString()

BOOL BatchGetString ( LPTSTR  lpBuffer,
INT  nBufferLength 
)

Definition at line 527 of file batch.c.

528{
529 INT len = 0;
530
531 /* read all chars from memory until a '\n' is encountered */
532 if (bc->mem)
533 {
534 for (; ((bc->mempos + len) < bc->memsize && len < (nBufferLength-1)); len++)
535 {
536#ifndef _UNICODE
537 lpBuffer[len] = bc->mem[bc->mempos + len];
538#endif
539 if (bc->mem[bc->mempos + len] == '\n')
540 {
541 len++;
542 break;
543 }
544 }
545#ifdef _UNICODE
547 lpBuffer[nBufferLength] = L'\0';
548 lpBuffer[len] = '\0';
549#endif
550 bc->mempos += len;
551 }
552
553 return len != 0;
554}
UINT OutputCodePage
Definition: console.c:26
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define MultiByteToWideChar
Definition: compat.h:110
GLenum GLsizei len
Definition: glext.h:6722
#define L(x)
Definition: ntvdm.h:50
_In_ LPCSTR _In_opt_ LPCSTR _In_ DWORD nBufferLength
Definition: winbase.h:3073

Referenced by cmd_goto(), and ReadBatchLine().

◆ ExitAllBatches()

VOID ExitAllBatches ( VOID  )

Definition at line 261 of file batch.c.

262{
263 while (bc)
264 ExitBatch();
265}

Referenced by Batch(), and ReadBatchLine().

◆ ExitBatch()

VOID ExitBatch ( VOID  )

Definition at line 222 of file batch.c.

223{
224 ClearBatch();
225
226 TRACE("ExitBatch\n");
227
230
231#ifndef MSCMD_BATCH_ECHO
232 /* Preserve echo state across batch calls */
233 bEcho = bc->bEcho;
234#endif
235
236 while (bc->setlocal)
237 cmd_endlocal(_T(""));
238
239 bc = bc->prev;
240
241#if 0
242 /* Do not process any more parts of a compound command */
243 bc->current = NULL;
244#endif
245
246 /* If there is no more batch contexts, notify the signal handler */
247 if (!bc)
248 {
250 BatType = NONE;
251
252#ifdef MSCMD_BATCH_ECHO
253 bEcho = bBcEcho;
254#endif
255 }
256}
BOOL CheckCtrlBreak(INT)
Definition: misc.c:132
INT cmd_endlocal(LPTSTR)
Definition: setlocal.c:110
#define BREAK_OUTOFBATCH
Definition: cmd.h:35
VOID FreeRedirection(REDIRECTION *)
Definition: redir.c:153
VOID UndoRedirection(REDIRECTION *, REDIRECTION *End)
Definition: redir.c:142

Referenced by Batch(), cmd_goto(), ExitAllBatches(), and ReadBatchLine().

◆ FindArg()

BOOL FindArg ( IN TCHAR  Char,
OUT PCTSTR ArgPtr,
OUT BOOL IsParam0 
)

Definition at line 84 of file batch.c.

88{
89 PCTSTR pp;
90 INT n = Char - _T('0');
91
92 TRACE("FindArg: (%d)\n", n);
93
94 *ArgPtr = NULL;
95
96 if (n < 0 || n > 9)
97 return FALSE;
98
99 n = bc->shiftlevel[n];
100 *IsParam0 = (n == 0);
101 pp = bc->params;
102
103 /* Step up the strings till we reach
104 * the end or the one we want. */
105 while (*pp && n--)
106 pp += _tcslen(pp) + 1;
107
108 *ArgPtr = pp;
109 return TRUE;
110}
GLdouble n
Definition: glext.h:7729
LPCSTR PCTSTR
Definition: ntbasedef.h:488
#define _tcslen
Definition: xmlstorage.h:198

Referenced by GetBatchVar().

◆ ReadBatchLine()

LPTSTR ReadBatchLine ( VOID  )

Definition at line 564 of file batch.c.

565{
566 TRACE("ReadBatchLine()\n");
567
568 /* User halt */
570 {
572 return NULL;
573 }
574
576 {
577 TRACE("ReadBatchLine(): Reached EOF!\n");
578 /* End of file */
579 ExitBatch();
580 return NULL;
581 }
582
583 TRACE("ReadBatchLine(): textline: \'%s\'\n", debugstr_aw(textline));
584
585#if 1
586 //
587 // FIXME: This is redundant, but keep it for the moment until we correctly
588 // hande the end-of-file situation here, in ReadLine() and in the parser.
589 // (In an EOF, the previous BatchGetString() call will return FALSE but
590 // we want not to run the ExitBatch() at first, but wait later to do it.)
591 //
592 if (textline[_tcslen(textline) - 1] != _T('\n'))
593 _tcscat(textline, _T("\n"));
594#endif
595
596 return textline;
597}
TCHAR textline[BATCH_BUFFSIZE]
Definition: batch.c:76
BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
Definition: batch.c:527
#define BREAK_BATCHFILE
Definition: cmd.h:34
#define _tcscat
Definition: tchar.h:622

Referenced by ReadLine().

Variable Documentation

◆ BatType

BATCH_TYPE BatType
extern

Definition at line 66 of file batch.c.

Referenced by Batch(), cmd_path(), cmd_prompt(), cmd_set(), CommandAssoc(), and ExitBatch().

◆ bBcEcho

BOOL bBcEcho
extern

Definition at line 70 of file batch.c.

Referenced by Batch(), and ExitBatch().

◆ bc

◆ bEcho

BOOL bEcho
extern

Definition at line 73 of file batch.c.

Referenced by Batch(), CommandEcho(), ExecuteCommandWithEcho(), ExitBatch(), ReadFromConsole(), and ReadLine().

◆ fc

◆ textline

TCHAR textline[BATCH_BUFFSIZE]
extern

Definition at line 76 of file batch.c.

Referenced by cmd_goto(), and ReadBatchLine().