1818RunScript (
1919 _In_ LPCWSTR filename )
2020{
21- FILE * script ;
2221 WCHAR tmp_string [MAX_STRING_SIZE ];
22+ FILE * script ;
23+ DWORD dwError = ERROR_SUCCESS ;
2324
2425 /* Open the file for processing */
2526 script = _wfopen (filename , L"r" );
@@ -32,19 +33,51 @@ RunScript(
3233 /* Read and process the script */
3334 while (fgetws (tmp_string , MAX_STRING_SIZE , script ) != NULL )
3435 {
35- if (InterpretLine (tmp_string ) == FALSE)
36- {
37- fclose (script );
38- return ERROR_SUCCESS ; /* FIXME */
39- }
36+ dwError = InterpretLine (tmp_string );
37+ if (dwError != ERROR_SUCCESS )
38+ break ;
4039 }
4140
4241 /* Close the file */
4342 fclose (script );
4443
45- return ERROR_SUCCESS ;
44+ return dwError ;
4645}
4746
47+
48+ LPWSTR
49+ MergeStrings (
50+ _In_ LPWSTR pszStringArray [],
51+ _In_ INT nCount )
52+ {
53+ LPWSTR pszOutString = NULL ;
54+ INT i , nLength ;
55+
56+ if ((pszStringArray == NULL ) || (nCount == 0 ))
57+ return NULL ;
58+
59+ nLength = 0 ;
60+ for (i = 0 ; i < nCount ; i ++ )
61+ nLength += wcslen (pszStringArray [i ]);
62+
63+ if (nLength > 0 )
64+ nLength += nCount ; /* Space characters and terminating zero */
65+
66+ pszOutString = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY , nLength * sizeof (WCHAR ));
67+ if (pszOutString == NULL )
68+ return NULL ;
69+
70+ for (i = 0 ; i < nCount ; i ++ )
71+ {
72+ if (i != 0 )
73+ wcscat (pszOutString , L" " );
74+ wcscat (pszOutString , pszStringArray [i ]);
75+ }
76+
77+ return pszOutString ;
78+ }
79+
80+
4881/*
4982 * wmain():
5083 * Main entry point of the application.
@@ -54,12 +87,11 @@ wmain(
5487 _In_ int argc ,
5588 _In_ const LPWSTR argv [])
5689{
57- LPCWSTR tmpBuffer = NULL ;
5890 LPCWSTR pszFileName = NULL ;
5991 LPCWSTR pszContext = NULL ;
92+ LPWSTR pszCommand = NULL ;
6093 int index ;
61- int result = EXIT_SUCCESS ;
62- BOOL bDone = FALSE;
94+ DWORD dwError = ERROR_SUCCESS ;
6395
6496 DPRINT ("wmain(%S)\n" , GetCommandLineW ());
6597
@@ -74,34 +106,17 @@ wmain(
74106 /* Process the command arguments */
75107 for (index = 1 ; index < argc ; index ++ )
76108 {
77- if ((argv [index ][0 ] == '/' )||
78- (argv [index ][0 ] == '-' ))
79- {
80- tmpBuffer = argv [index ] + 1 ;
81- }
82- else
83- {
84- if (pszFileName != NULL )
85- {
86- ConResPuts (StdOut , IDS_APP_USAGE );
87- result = EXIT_FAILURE ;
88- goto done ;
89- }
90-
91- /* Run a command from the command line */
92- if (InterpretCommand ((LPWSTR * )& argv [index ], argc - index , & bDone ) == FALSE)
93- result = EXIT_FAILURE ;
94- goto done ;
95- }
96-
97- if (_wcsicmp (tmpBuffer , L"?" ) == 0 )
109+ if ((_wcsicmp (argv [index ], L"-?" ) == 0 ) ||
110+ (_wcsicmp (argv [index ], L"/?" ) == 0 ) ||
111+ (_wcsicmp (argv [index ], L"?" ) == 0 ))
98112 {
99113 /* Help option */
100114 ConResPuts (StdOut , IDS_APP_USAGE );
101- result = EXIT_SUCCESS ;
115+ dwError = ERROR_SUCCESS ;
102116 goto done ;
103117 }
104- else if (_wcsicmp (tmpBuffer , L"a" ) == 0 )
118+ else if ((_wcsicmp (argv [index ], L"-a" ) == 0 ) ||
119+ (_wcsicmp (argv [index ], L"/a" ) == 0 ))
105120 {
106121 /* Aliasfile option */
107122 if ((index + 1 ) < argc )
@@ -113,10 +128,12 @@ wmain(
113128 else
114129 {
115130 ConResPuts (StdOut , IDS_APP_USAGE );
116- result = EXIT_FAILURE ;
131+ dwError = ERROR_INVALID_SYNTAX ;
132+ goto done ;
117133 }
118134 }
119- else if (_wcsicmp (tmpBuffer , L"c" ) == 0 )
135+ else if ((_wcsicmp (argv [index ], L"-c" ) == 0 ) ||
136+ (_wcsicmp (argv [index ], L"/c" ) == 0 ))
120137 {
121138 /* Context option */
122139 if ((index + 1 ) < argc )
@@ -127,10 +144,12 @@ wmain(
127144 else
128145 {
129146 ConResPuts (StdOut , IDS_APP_USAGE );
130- result = EXIT_FAILURE ;
147+ dwError = ERROR_INVALID_SYNTAX ;
148+ goto done ;
131149 }
132150 }
133- else if (_wcsicmp (tmpBuffer , L"f" ) == 0 )
151+ else if ((_wcsicmp (argv [index ], L"-f" ) == 0 ) ||
152+ (_wcsicmp (argv [index ], L"/f" ) == 0 ))
134153 {
135154 /* File option */
136155 if ((index + 1 ) < argc )
@@ -141,10 +160,12 @@ wmain(
141160 else
142161 {
143162 ConResPuts (StdOut , IDS_APP_USAGE );
144- result = EXIT_FAILURE ;
163+ dwError = ERROR_INVALID_SYNTAX ;
164+ goto done ;
145165 }
146166 }
147- else if (_wcsicmp (tmpBuffer , L"r" ) == 0 )
167+ else if ((_wcsicmp (argv [index ], L"-r" ) == 0 ) ||
168+ (_wcsicmp (argv [index ], L"/r" ) == 0 ))
148169 {
149170 /* Remote option */
150171 if ((index + 1 ) < argc )
@@ -156,33 +177,43 @@ wmain(
156177 else
157178 {
158179 ConResPuts (StdOut , IDS_APP_USAGE );
159- result = EXIT_FAILURE ;
180+ dwError = ERROR_INVALID_SYNTAX ;
181+ goto done ;
160182 }
161183 }
162184 else
163185 {
164- /* Invalid command */
165- ConResPrintf (StdOut , IDS_INVALID_COMMAND , argv [index ]);
166- result = EXIT_FAILURE ;
167- goto done ;
186+ if (pszFileName != NULL )
187+ {
188+ ConResPuts (StdOut , IDS_APP_USAGE );
189+ dwError = ERROR_INVALID_SYNTAX ;
190+ goto done ;
191+ }
192+ else if (pszCommand == NULL )
193+ {
194+ pszCommand = MergeStrings ((LPWSTR * )& argv [index ], argc - index );
195+ if (pszCommand )
196+ break ;
197+ }
168198 }
169199 }
170200
171201 /* Set a context */
172202 if (pszContext )
173203 {
174- if (InterpretLine ((LPWSTR )pszContext ) == FALSE)
175- {
176- result = EXIT_FAILURE ;
204+ dwError = InterpretLine ((LPWSTR )pszContext );
205+ if (dwError != ERROR_SUCCESS )
177206 goto done ;
178- }
179207 }
180208
181- /* Run a script or the interactive interpeter */
209+ /* Run a script, the command line instruction or the interactive interpeter */
182210 if (pszFileName != NULL )
183211 {
184- if (RunScript (pszFileName ) == FALSE)
185- result = EXIT_FAILURE ;
212+ dwError = RunScript (pszFileName );
213+ }
214+ else if (pszCommand != NULL )
215+ {
216+ dwError = InterpretLine (pszCommand );
186217 }
187218 else
188219 {
@@ -191,10 +222,12 @@ wmain(
191222
192223done :
193224 /* FIXME: Cleanup code goes here */
225+ if (pszCommand != NULL )
226+ HeapFree (GetProcessHeap (), 0 , pszCommand );
194227 CleanupContext ();
195228 UnloadHelpers ();
196229
197- return result ;
230+ return ( dwError == ERROR_SUCCESS ) ? EXIT_SUCCESS : EXIT_FAILURE ;
198231}
199232
200233VOID
0 commit comments