Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensetlocal.c
Go to the documentation of this file.
00001 /* 00002 * SETLOCAL.C - setlocal and endlocal internal batch commands. 00003 * 00004 * History: 00005 * 00006 * 1 Feb 2008 (Christoph von Wittich) 00007 * started. 00008 */ 00009 00010 #include <precomp.h> 00011 00012 typedef struct _SETLOCAL { 00013 struct _SETLOCAL *Prev; 00014 BOOL DelayedExpansion; 00015 LPTSTR Environment; 00016 } SETLOCAL; 00017 00018 /* Create a copy of the current environment */ 00019 LPTSTR 00020 DuplicateEnvironment(VOID) 00021 { 00022 LPTSTR Environ = GetEnvironmentStrings(); 00023 LPTSTR End, EnvironCopy; 00024 if (!Environ) 00025 return NULL; 00026 for (End = Environ; *End; End += _tcslen(End) + 1) 00027 ; 00028 EnvironCopy = cmd_alloc((End + 1 - Environ) * sizeof(TCHAR)); 00029 if (EnvironCopy) 00030 memcpy(EnvironCopy, Environ, (End + 1 - Environ) * sizeof(TCHAR)); 00031 FreeEnvironmentStrings(Environ); 00032 return EnvironCopy; 00033 } 00034 00035 INT cmd_setlocal(LPTSTR param) 00036 { 00037 SETLOCAL *Saved; 00038 LPTSTR *arg; 00039 INT argc, i; 00040 00041 /* SETLOCAL only works inside a batch file */ 00042 if (!bc) 00043 return 0; 00044 00045 Saved = cmd_alloc(sizeof(SETLOCAL)); 00046 if (!Saved) 00047 { 00048 error_out_of_memory(); 00049 return 1; 00050 } 00051 Saved->Prev = bc->setlocal; 00052 Saved->DelayedExpansion = bDelayedExpansion; 00053 Saved->Environment = DuplicateEnvironment(); 00054 if (!Saved->Environment) 00055 { 00056 error_out_of_memory(); 00057 cmd_free(Saved); 00058 return 1; 00059 } 00060 bc->setlocal = Saved; 00061 00062 nErrorLevel = 0; 00063 00064 arg = splitspace(param, &argc); 00065 for (i = 0; i < argc; i++) 00066 { 00067 if (!_tcsicmp(arg[i], _T("enableextensions"))) 00068 /* not implemented, ignore */; 00069 else if (!_tcsicmp(arg[i], _T("disableextensions"))) 00070 /* not implemented, ignore */; 00071 else if (!_tcsicmp(arg[i], _T("enabledelayedexpansion"))) 00072 bDelayedExpansion = TRUE; 00073 else if (!_tcsicmp(arg[i], _T("disabledelayedexpansion"))) 00074 bDelayedExpansion = FALSE; 00075 else 00076 { 00077 error_invalid_parameter_format(arg[i]); 00078 break; 00079 } 00080 } 00081 freep(arg); 00082 00083 return nErrorLevel; 00084 } 00085 00086 /* endlocal doesn't take any params */ 00087 INT cmd_endlocal(LPTSTR param) 00088 { 00089 LPTSTR Environ, Name, Value; 00090 SETLOCAL *Saved; 00091 00092 /* Pop a SETLOCAL struct off of this batch file's stack */ 00093 if (!bc || !(Saved = bc->setlocal)) 00094 return 0; 00095 bc->setlocal = Saved->Prev; 00096 00097 bDelayedExpansion = Saved->DelayedExpansion; 00098 00099 /* First, clear out the environment. Since making any changes to the 00100 * environment invalidates pointers obtained from GetEnvironmentStrings(), 00101 * we must make a copy of it and get the variable names from that */ 00102 Environ = DuplicateEnvironment(); 00103 if (Environ) 00104 { 00105 for (Name = Environ; *Name; Name += _tcslen(Name) + 1) 00106 { 00107 if (!(Value = _tcschr(Name + 1, _T('=')))) 00108 continue; 00109 *Value++ = _T('\0'); 00110 SetEnvironmentVariable(Name, NULL); 00111 Name = Value; 00112 } 00113 cmd_free(Environ); 00114 } 00115 00116 /* Now, restore variables from the copy saved by cmd_setlocal */ 00117 for (Name = Saved->Environment; *Name; Name += _tcslen(Name) + 1) 00118 { 00119 if (!(Value = _tcschr(Name + 1, _T('=')))) 00120 continue; 00121 *Value++ = _T('\0'); 00122 SetEnvironmentVariable(Name, Value); 00123 Name = Value; 00124 } 00125 00126 cmd_free(Saved->Environment); 00127 cmd_free(Saved); 00128 return 0; 00129 } 00130 Generated on Sun May 27 2012 04:18:15 for ReactOS by
1.7.6.1
|