ReactOS  0.4.14-dev-390-g34947ad
setlocal.c
Go to the documentation of this file.
1 /*
2  * SETLOCAL.C - setlocal and endlocal internal batch commands.
3  *
4  * History:
5  *
6  * 1 Feb 2008 (Christoph von Wittich)
7  * started.
8 */
9 
10 #include "precomp.h"
11 
12 typedef struct _SETLOCAL
13 {
14  struct _SETLOCAL *Prev;
18 } SETLOCAL;
19 
20 /* Create a copy of the current environment */
21 LPTSTR
23 {
24  LPTSTR Environ = GetEnvironmentStrings();
25  LPTSTR End, EnvironCopy;
26 
27  if (!Environ) return NULL;
28 
29  for (End = Environ; *End; End += _tcslen(End) + 1) ;
30  EnvironCopy = cmd_alloc((End + 1 - Environ) * sizeof(TCHAR));
31 
32  if (EnvironCopy)
33  memcpy(EnvironCopy, Environ, (End + 1 - Environ) * sizeof(TCHAR));
34 
35  FreeEnvironmentStrings(Environ);
36  return EnvironCopy;
37 }
38 
40 {
41  SETLOCAL *Saved;
42  LPTSTR *arg;
43  INT argc, i;
44 
45  /* SETLOCAL only works inside a batch file */
46  if (!bc)
47  return 0;
48 
49  Saved = cmd_alloc(sizeof(SETLOCAL));
50  if (!Saved)
51  {
52  WARN("Cannot allocate memory for Saved!\n");
54  return 1;
55  }
56  Saved->Prev = bc->setlocal;
60  if (!Saved->Environment)
61  {
63  cmd_free(Saved);
64  return 1;
65  }
66  bc->setlocal = Saved;
67 
68  nErrorLevel = 0;
69 
70  arg = splitspace(param, &argc);
71  for (i = 0; i < argc; i++)
72  {
73  if (!_tcsicmp(arg[i], _T("enableextensions")))
74  /* FIXME: not implemented! */
76  else if (!_tcsicmp(arg[i], _T("disableextensions")))
77  /* FIXME: not implemented! */
79  else if (!_tcsicmp(arg[i], _T("enabledelayedexpansion")))
81  else if (!_tcsicmp(arg[i], _T("disabledelayedexpansion")))
83  else
84  {
86  break;
87  }
88  }
89  freep(arg);
90 
91  return nErrorLevel;
92 }
93 
94 /* endlocal doesn't take any params */
96 {
97  LPTSTR Environ, Name, Value;
98  SETLOCAL *Saved;
99 
100  /* Pop a SETLOCAL struct off of this batch file's stack */
101  if (!bc || !(Saved = bc->setlocal))
102  return 0;
103  bc->setlocal = Saved->Prev;
104 
107 
108  /* First, clear out the environment. Since making any changes to the
109  * environment invalidates pointers obtained from GetEnvironmentStrings(),
110  * we must make a copy of it and get the variable names from that */
111  Environ = DuplicateEnvironment();
112  if (Environ)
113  {
114  for (Name = Environ; *Name; Name += _tcslen(Name) + 1)
115  {
116  if (!(Value = _tcschr(Name + 1, _T('='))))
117  continue;
118  *Value++ = _T('\0');
120  Name = Value;
121  }
122  cmd_free(Environ);
123  }
124 
125  /* Now, restore variables from the copy saved by cmd_setlocal */
126  for (Name = Saved->Environment; *Name; Name += _tcslen(Name) + 1)
127  {
128  if (!(Value = _tcschr(Name + 1, _T('='))))
129  continue;
130  *Value++ = _T('\0');
132  Name = Value;
133  }
134 
135  cmd_free(Saved->Environment);
136  cmd_free(Saved);
137  return 0;
138 }
INT nErrorLevel
Definition: cmd.c:157
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
static int argc
Definition: ServiceArgs.c:12
INT cmd_setlocal(LPTSTR param)
Definition: setlocal.c:39
#define TRUE
Definition: types.h:120
#define _tcsicmp
Definition: xmlstorage.h:205
LPBATCH_CONTEXT bc
Definition: batch.c:66
#define WARN(fmt,...)
Definition: debug.h:111
LPTSTR * splitspace(LPTSTR, LPINT)
Definition: misc.c:382
void * arg
Definition: msvc.h:12
int32_t INT
Definition: typedefs.h:56
CHAR * LPTSTR
Definition: xmlstorage.h:192
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
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL EnableExtensions
Definition: setlocal.c:15
struct NameRec_ * Name
Definition: cdprocs.h:464
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
static VOID freep(LPSTR *p)
Definition: cmdcons.c:98
smooth NULL
Definition: ftsmooth.c:416
VOID error_out_of_memory(VOID)
Definition: error.c:136
VOID error_invalid_parameter_format(LPTSTR)
Definition: error.c:143
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
struct _SETLOCAL * setlocal
Definition: batch.h:21
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
GLfloat param
Definition: glext.h:5796
BOOL bEnableExtensions
Definition: cmd.c:160
struct _SETLOCAL * Prev
Definition: setlocal.c:14
BOOL DelayedExpansion
Definition: setlocal.c:16
LPTSTR DuplicateEnvironment(VOID)
Definition: setlocal.c:22
LPTSTR Environment
Definition: setlocal.c:17
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define SetEnvironmentVariable
Definition: winbase.h:3732
#define cmd_alloc(size)
Definition: cmddbg.h:29
#define cmd_free(ptr)
Definition: cmddbg.h:31
INT cmd_endlocal(LPTSTR param)
Definition: setlocal.c:95
BOOL bDelayedExpansion
Definition: cmd.c:161
LPSTR WINAPI GetEnvironmentStrings(void)
#define FreeEnvironmentStrings
Definition: winbase.h:3620
struct _SETLOCAL SETLOCAL