Debian Codegraph @

Source/makenssi.cpp

raw
1 /*
2 * makenssi.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support by Jim Park -- 08/09/2007
17 */
18
19 #include "Platform.h"
20 #include <stdio.h>
21 #include <signal.h>
22 #ifdef _WIN32
23 # include <direct.h>
24 #else
25 # include <unistd.h>
26 #endif
27 #include "tstring.h"
28
29 #include "build.h"
30 #include "util.h"
31 #include "utf.h"
32 #include "winchar.h" // assert(sizeof(WINWCHAR)...)
33
34 #include <nsis-version.h>
35 #define NSIS_COPYYEARS _T("1999-2026")
36
37 using namespace std;
38
39 NSISRT_DEFINEGLOBALS();
40 bool g_dopause=false;
41 NStreamEncoding g_outputenc;
42 #ifdef _WIN32
43 UINT g_wincon_orgoutcp;
44 #ifdef _UNICODE
45 WINSIO_OSDATA g_osdata_stdout, g_osdata_stderr;
46 #endif
47 #endif
48 const TCHAR *g_argv0=0;
49
50 static void dopause(void)
51 {
52 if (!g_dopause) return;
53 if (g_display_errors) _ftprintf(g_output,_T("MakeNSIS done - hit enter to close..."));
54 fflush(stdout);
55 int a;
56 while ((a=_gettchar()) != _T('\r') && a != _T('\n') && a != 27/*esc*/);
57 }
58
59 void quit()
60 {
61 if (g_display_errors)
62 {
63 PrintColorFmtMsg_WARN(_T("\nNote: you may have one or two (large) stale temporary file(s) ")
64 _T("left in your temporary directory (Generally this only happens on Windows 9x).\n"));
65 }
66 exit(1);
67 }
68
69 static void myatexit()
70 {
71 dopause();
72 ResetPrintColor();
73 bool oneoutputstream = g_output == g_errout;
74 if (g_output != stdout && g_output ) fclose(g_output);
75 if (g_errout != stderr && g_errout && !oneoutputstream) fclose(g_errout);
76 g_output = g_errout = 0;
77 #ifdef _WIN32
78 SetConsoleOutputCP(g_wincon_orgoutcp);
79 #endif
80 }
81
82 static void sigint(int sig)
83 {
84 if (g_display_errors)
85 {
86 PrintColorFmtMsg_WARN(_T("\n\nAborting on Ctrl+C...\n"));
87 }
88 quit();
89 }
90
91 #ifdef _WIN32
92 static DWORD WINAPI sigint_event_msg_handler(LPVOID ThreadParam)
93 {
94 using namespace MakensisAPI;
95 HANDLE hEvent = 0;
96 if (ThreadParam)
97 {
98 TCHAR eventnamebuf[100];
99 wsprintf(eventnamebuf, SigintEventNameFmt, (HWND)ThreadParam);
100 hEvent = OpenEvent(SYNCHRONIZE, FALSE, eventnamebuf);
101 }
102 if (!hEvent) hEvent = OpenEvent(SYNCHRONIZE, FALSE, SigintEventNameLegacy);
103
104 if (hEvent)
105 {
106 if (WaitForSingleObject(hEvent, INFINITE) == WAIT_OBJECT_0)
107 raise(SIGINT);
108 CloseHandle(hEvent);
109 }
110
111 return 0;
112 }
113
114 static UINT_PTR QueryHost(HWND hHost, UINT_PTR wp, UINT_PTR lp=0, UINT_PTR def=0) { return hHost ? SendMessage(hHost, MakensisAPI::QUERYHOST, wp, lp) : def; }
115 #else //! _WIN32
116 static inline UINT_PTR QueryHost(HWND hHost, UINT_PTR wp, UINT_PTR lp=0, UINT_PTR def=0) { return def; }
117 #endif //~ _WIN32
118
119 static void init_signals(HWND notify_hwnd)
120 {
121 atexit(myatexit);
122 signal(SIGINT,sigint);
123
124 #ifdef _WIN32
125 DWORD id;
126 HANDLE hThread = CreateThread(NULL, 0, sigint_event_msg_handler, (LPVOID)notify_hwnd, 0, &id);
127 if (hThread)
128 {
129 SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
130 CloseHandle(hThread);
131 }
132 #endif
133 }
134
135 static void print_logo()
136 {
137 _ftprintf(g_output,_T("MakeNSIS %") NPRIs _T(" - Copyright ") NSIS_COPYYEARS _T(" Contributors\n")
138 _T("See the file COPYING for license details.\n")
139 _T("Credits can be found in the Users Manual.\n\n"), NSIS_VERSION);
140 fflush(g_output);
141 }
142
143 static void print_license()
144 {
145 _ftprintf(g_output,_T("Copyright (C) ") NSIS_COPYYEARS _T(" Nullsoft and Contributors\n\n")
146 _T("This license applies to everything in the NSIS package, except where otherwise\n")
147 _T("noted.\n\n")
148 _T("This software is provided 'as-is', without any express or implied warranty.\n")
149 _T("In no event will the authors be held liable for any damages arising from the\n")
150 _T("use of this software.\n\n")
151 _T("Permission is granted to anyone to use this software for any purpose, including\n")
152 _T("commercial applications, and to alter it and redistribute it freely, subject to\n")
153 _T("the following restrictions:\n")
154 _T(" 1. The origin of this software must not be misrepresented; you must not claim\n")
155 _T(" that you wrote the original software. If you use this software in a\n")
156 _T(" product, an acknowledgment in the product documentation would be\n")
157 _T(" appreciated but is not required.\n")
158 _T(" 2. Altered source versions must be plainly marked as such, and must not be\n")
159 _T(" misrepresented as being the original software.\n")
160 _T(" 3. This notice may not be removed or altered from any source distribution.\n\n")
161 _T("In addition to this license, different licenses apply to the included\n")
162 _T("compression modules. See the file COPYING for details.\n"));
163 fflush(g_output);
164 }
165
166 static void print_usage()
167 {
168 _ftprintf(g_output,_T("Usage:\n")
169 _T(" ") _T("makensis [ option | script.nsi | - ] [...]\n")
170 _T("\n")
171 _T("Options:\n")
172 _T(" ") OPT_STR _T("CMDHELP [item] prints out help for 'item', or lists all commands\n")
173 _T(" ") OPT_STR _T("HDRINFO prints information about what options makensis was compiled with\n")
174 _T(" ") OPT_STR _T("LICENSE prints the makensis software license\n")
175 _T(" ") OPT_STR _T("VERSION prints the makensis version and exits\n")
176 //_T(" ") OPT_STR _T("HELP this usage info\n")
177 #ifdef _WIN32
178 _T(" ") OPT_STR _T("Px sets the compiler process priority, where x is 5=realtime,4=high,\n")
179 _T(" ") _T(" 3=above normal,2=normal,1=below normal,0=idle\n")
180 #endif
181 _T(" ") OPT_STR _T("Vx verbosity where x is 4=all,3=no script,2=no info,1=no warnings,0=none\n")
182 _T(" ") OPT_STR _T("WX treat warnings as errors\n")
183 _T(" ") OPT_STR _T("Ofile specifies a text file to log compiler output (default is stdout)\n")
184 _T(" ") OPT_STR _T("LAUNCH executes the generated installer\n")
185 _T(" ") OPT_STR _T("PAUSE pauses after execution\n")
186 _T(" ") OPT_STR _T("NOCONFIG disables inclusion of <path to makensis.exe>") PLATFORM_PATH_SEPARATOR_STR _T("nsisconf.nsh\n")
187 _T(" ") OPT_STR _T("NOCD disables the current directory change to that of the .nsi file\n")
188 _T(" ") OPT_STR _T("INPUTCHARSET <") TSTR_INPUTCHARSET _T(">\n")
189 #ifdef _WIN32
190 _T(" ") OPT_STR _T("OUTPUTCHARSET <") TSTR_OUTPUTCHARSET _T(">\n")
191 #endif
192 _T(" ") OPT_STR _T("[SAFE]PPO preprocess to stdout/file\n")
193 _T(" ") OPT_STR _T("Ddefine[=value] defines the symbol \"define\" for the script [to value]\n")
194 _T(" ") OPT_STR _T("Xscriptcmd executes scriptcmd in script (i.e. \"") OPT_STR _T("XOutFile inst.exe\")\n")
195 _T(" ") _T(" parameters are processed by order (") OPT_STR _T("Ddef ins.nsi != ins.nsi ") OPT_STR _T("Ddef)\n")
196 _T("\n")
197 _T("For script file name, you can use - to read from the standard input\n")
198 #ifdef _WIN32
199 _T("You can also use - as an option character: -PAUSE as well as /PAUSE\n")
200 #endif
201 _T("You can use a double-dash to end options processing: makensis -- -ins.nsi\n"));
202 fflush(g_output);
203 }
204
205 static void print_stub_info(CEXEBuild& build)
206 {
207 if (build.display_info)
208 {
209 _ftprintf(g_output,_T("Size of first header is %lu bytes.\n"),(unsigned long)sizeof(firstheader));
210 _ftprintf(g_output,_T("Size of main header is %lu bytes.\n"),(unsigned long)build.get_header_size());
211 _ftprintf(g_output,_T("Size of each section is %lu bytes.\n"),(unsigned long)sizeof(section));
212 _ftprintf(g_output,_T("Size of each page is %lu bytes.\n"),(unsigned long)sizeof(page));
213 _ftprintf(g_output,_T("Size of each instruction is %lu bytes.\n"),(unsigned long)sizeof(entry));
214 int x=build.definedlist.getnum();
215 _ftprintf(g_output,_T("\nDefined symbols: "));
216 for (int i=0; i<x; i++)
217 {
218 _ftprintf(g_output,_T("%")NPRIs,build.definedlist.getname(i));
219 TCHAR *p=build.definedlist.getvalue(i);
220 if (*p) _ftprintf(g_output,_T("=%")NPRIs,p);
221 if (i<x-1) _ftprintf(g_output,_T(","));
222 }
223 if (!x) _ftprintf(g_output,_T("none"));
224 _ftprintf(g_output,_T("\n"));
225 fflush(g_output);
226 }
227 }
228
229 static tstring get_home()
230 {
231 TCHAR *home = _tgetenv(
232 #ifdef _WIN32
233 _T("APPDATA")
234 #else
235 _T("HOME")
236 #endif
237 );
238
239 return home ? home : _T("");
240 }
241
242 static int process_config(CEXEBuild& build, tstring& conf)
243 {
244 NIStream strm;
245 if (strm.OpenFileForReading(conf.c_str()))
246 {
247 build.INFO_MSG(_T("Processing config: %") NPRIs _T("\n"),conf.c_str());
248 int ret=build.process_script(strm,conf.c_str());
249 if (ret != PS_OK && ret != PS_EOF)
250 {
251 build.ERROR_MSG(_T("Error in config on line %d -- aborting creation process\n"),build.linecnt);
252 return 1;
253 }
254 build.SCRIPT_MSG(_T("\n")); // Extra newline to separate the config from the real script
255 }
256 return 0;
257 }
258
259 static int change_to_script_dir(CEXEBuild& build, tstring& script)
260 {
261 tstring dir = get_dir_name(get_full_path(script));
262 if (!dir.empty())
263 {
264 build.SCRIPT_MSG(_T("Changing directory to: \"%") NPRIs _T("\"\n"),dir.c_str());
265 if (_tchdir(dir.c_str()))
266 {
267 build.ERROR_MSG(_T("Error changing directory to \"%") NPRIs _T("\"\n"),dir.c_str());
268 return 1;
269 }
270 build.SCRIPT_MSG(_T("\n"));
271 }
272 return 0;
273 }
274
275 static inline bool HasReqParam(TCHAR**argv,int argi,int argc,bool silent=false)
276 {
277 if (argi>=argc || !*argv[argi])
278 {
279 if (!silent) PrintColorFmtMsg_ERR(_T("Error: Missing required parameter!\n"));
280 return false;
281 }
282 return true;
283 }
284
285 #ifdef NSIS_HPUX_ALLOW_UNALIGNED_DATA_ACCESS
286 extern "C" void allow_unaligned_data_access();
287 #endif
288
289 static inline int makensismain(int argc, TCHAR **argv)
290 {
291
292 #ifdef NSIS_HPUX_ALLOW_UNALIGNED_DATA_ACCESS
293 allow_unaligned_data_access();
294 #endif
295 assert(sizeof(UINT_PTR) == sizeof(void*));
296 assert('a' + 25 == 'z' && '0' < 'A' && 'A' < 'a'); // ASCII, do you speak it?
297 assert(sizeof(wchar_t) > 1 && sizeof(wchar_t) <= 4);
298 assert(sizeof(WINWCHAR) == 2 && sizeof(WORD) == 2);
299 assert(sizeof(WINWCHAR) == sizeof(WCHAR)); // Not really required but if WCHAR changes we need to know
300
301 g_argv0=argv[0];
302
303 if (!NSISRT_Initialize())
304 {
305 _ftprintf(stderr,_T("NSISRT_Initialize failed!\n"));
306 return 1;
307 }
308
309 HWND hostnotifyhandle=0;
310 const TCHAR*stdoutredirname=0;
311 NStreamEncoding inputenc, &outputenc = g_outputenc;
312 int argpos=0;
313 unsigned char do_cd=true, noconfig=false, do_exec=false;
314 bool no_logo=true, warnaserror=false;
315 bool initialparsefail=false, in_files=false;
316 bool oneoutputstream=false;
317 signed char pponly=0;
318 #ifdef _WIN32
319 signed char outputbom=1;
320
321 #ifdef DEBUG
322 assert(CP_ACP == outputenc.GetCodepage()); // Required by CEXEBuild::notify() char* legacy handling.
323 #endif
324 #endif //~ _WIN32
325
326 // Some parameters have to be parsed early so we can initialize stdout and the "host API".
327 while (++argpos < argc && !initialparsefail)
328 {
329 if (!IS_OPT(argv[argpos])) break; // must be a filename, stop parsing
330 if (!_tcscmp(argv[argpos], _T("--"))) break; // stop parsing
331 if (_T('-') == argv[argpos][0] && !argv[argpos][1]) continue; // stdin
332
333 const TCHAR *swname = &argv[argpos][1];
334 if (!_tcsicmp(swname,_T("INPUTCHARSET")) || !_tcsicmp(swname,_T("ICS"))) ++argpos; // Skip
335 else if (!_tcsicmp(swname,_T("VERSION"))) argc=0;
336 else if (!_tcsicmp(swname,_T("NOTIFYHWND")))
337 {
338 initialparsefail=!HasReqParam(argv,++argpos,argc,true);
339 if (initialparsefail) break;
340 hostnotifyhandle=(HWND)(INT_PTR) _ttol(argv[argpos]); // MSDN says we should sign extend HWNDs: msdn.microsoft.com/en-us/library/aa384203
341 #ifdef _WIN32
342 if (!IsWindow(hostnotifyhandle)) hostnotifyhandle=0;
343 #endif
344 }
345 else if (!_tcsicmp(swname,_T("OUTPUTCHARSET")) || !_tcsicmp(swname,_T("OCS")))
346 {
347 initialparsefail=!HasReqParam(argv,++argpos,argc,true);
348 if (initialparsefail) break;
349 #ifdef _WIN32
350 bool bom;
351 WORD cp=GetEncodingFromString(argv[argpos],bom);
352 if (NStreamEncoding::UNKNOWN == cp)
353 {
354 ++initialparsefail;
355 }
356 else
357 {
358 outputbom=bom ? 1 : -1;
359 outputenc.SetCodepage(cp);
360 }
361 #else
362 outputenc.SetCodepage(NStreamEncoding::UNKNOWN);
363 #endif
364 }
365 #ifdef _WIN32
366 else if (!_tcsicmp(swname,_T("RAW")))
367 {
368 // Emulate the scratchpaper.com fork and its /RAW switch.
369 // NOTE: Unlike the fork, we print \r\n and not just \n.
370 outputbom=0;
371 outputenc.SetCodepage(NStreamEncoding::UTF16LE);
372 }
373 #endif
374 else if (!_tcsicmp(swname,_T("PPO")) || !_tcsicmp(swname,_T("SafePPO")))
375 {
376 pponly = S7IsChEqualI('s',swname[0]) ? 1 : -1;
377 }
378 else if (S7IsChEqualI('v',swname[0]) && swname[1] && !swname[2])
379 {
380 no_logo=swname[1] >= _T('0') && swname[1] <= _T('2');
381 }
382 else if (!_tcsicmp(swname,_T("WX")))
383 {
384 warnaserror = true;
385 }
386 // This must be parsed last because it will eat other switches
387 else if (S7IsChEqualI('o',swname[0]) && swname[1]) stdoutredirname=swname+1;
388 }
389
390
391 #ifdef _WIN32
392 g_wincon_orgoutcp = GetConsoleOutputCP();
393 #endif
394
395 init_signals(hostnotifyhandle);
396
397
398 FILE*stdoutredir=stdout;
399 if (stdoutredirname) stdoutredir=my_fopen(stdoutredirname,"w");
400 g_output=stdoutredir;
401 if (!g_output)
402 g_output=stdout; // We could not open stdoutredirname, fall back to stdout
403 else if (stdoutredirname)
404 oneoutputstream=true; // -O used, put all output in the same file
405 if (oneoutputstream || !(1 & QueryHost(hostnotifyhandle,MakensisAPI::QH_ENABLESTDERR,0,1)))
406 g_errout=g_output;
407 #if defined(_WIN32) && defined(_UNICODE)
408 if (hostnotifyhandle)
409 {
410 // The host can override the output format if they want to
411 LPARAM lp=MAKELONG(outputenc.GetCodepage(),outputbom);
412 LRESULT mr=QueryHost(hostnotifyhandle,MakensisAPI::QH_OUTPUTCHARSET,lp);
413 if (mr) outputenc.SetCodepage((WORD)(--mr)), outputbom = -1;
414 }
415
416 if (( !WinStdIO_OStreamInit(g_osdata_stdout,g_output,outputenc.GetCodepage(),outputbom))
417 || (g_errout != g_output && !WinStdIO_OStreamInit(g_osdata_stderr,g_errout,outputenc.GetCodepage(),outputbom)))
418 {
419 assert(!"StdIO init failed");
420 return 1;
421 }
422 #endif
423 // g_output is now initialized and Print*/_[f]tprintf can be used
424 if (!stdoutredir) PrintColorFmtMsg_WARN(_T("Error opening output log for writing! Using stdout.\n"));
425
426 unsigned int nousage=0, performed=0;
427 unsigned int files_processed=0;
428 unsigned int cmds_processed=0;
429
430 CEXEBuild build(pponly, warnaserror);
431 try
432 {
433 build.initialize(argv[0]);
434 }
435 catch (exception& err)
436 {
437 PrintColorFmtMsg_ERR(_T("Error initializing CEXEBuild: %") NPRIs _T("\n"), CtoTStrParam(err.what()));
438 return 1;
439 }
440
441 #ifdef _WIN32
442 build.notify_hwnd=hostnotifyhandle;
443 #else
444 const TCHAR*const badnonwinswitchfmt=OPT_STR _T("%") NPRIs _T(" is disabled for non Win32 platforms.");
445 if (hostnotifyhandle)
446 build.warning(DW_CMDLINE_UNSUPP_NIX,badnonwinswitchfmt,_T("NOTIFYHWND"));
447 if (NStreamEncoding::UNKNOWN==outputenc.GetCodepage())
448 build.warning(DW_CMDLINE_UNSUPP_NIX,badnonwinswitchfmt,_T("OUTPUTCHARSET"));
449 #endif // ~_WIN32
450
451 if (!argc)
452 {
453 _ftprintf(g_output,NSIS_VERSION);
454 fflush(g_output);
455 return 0;
456 }
457 if (!no_logo && !pponly) print_logo();
458
459
460 argpos=initialparsefail ? argc : 1;
461 while (argpos < argc)
462 {
463 if (!_tcscmp(argv[argpos], _T("--")))
464 in_files=1;
465 else if (IS_OPT(argv[argpos]) && _tcscmp(argv[argpos], _T("-")) && !in_files)
466 {
467 const TCHAR* const swname = &argv[argpos][1];
468 if (!_tcsicmp(swname,_T("PPO")) || !_tcsicmp(swname,_T("SafePPO"))) {} // Already parsed
469 else if (!_tcsicmp(swname,_T("WX"))) {} // Already parsed
470 else if (!_tcsicmp(swname,_T("NOCD"))) do_cd=false;
471 else if (!_tcsicmp(swname,_T("NOCONFIG"))) noconfig++;
472 else if (!_tcsicmp(swname,_T("PAUSE"))) g_dopause=true;
473 else if (!_tcsicmp(swname,_T("LAUNCH"))) do_exec++;
474 else if (!_tcsicmp(swname,_T("HELP")))
475 {
476 print_usage();
477 performed |= ++nousage;
478 }
479 else if (!_tcsicmp(swname,_T("LICENSE")))
480 {
481 if (build.display_info) print_license();
482 performed |= ++nousage;
483 }
484 else if (!_tcsicmp(swname,_T("CMDHELP")))
485 {
486 if (build.print_cmdhelp(argpos < argc-1 ? argv[++argpos] : NULL, true))
487 performed |= ++nousage;
488 }
489 else if (!_tcsicmp(swname,_T("HDRINFO")))
490 {
491 print_stub_info(build);
492 performed |= ++nousage;
493 }
494 else if (!_tcsicmp(swname,_T("INPUTCHARSET")) || !_tcsicmp(swname,_T("ICS")))
495 {
496 if (!HasReqParam(argv, ++argpos, argc)) break;
497 WORD cp = GetEncodingFromString(argv[argpos]);
498 if (NStreamEncoding::UNKNOWN == cp)
499 {
500 if (_tcsicmp(argv[argpos], _T("AUTO")))
501 build.warning(DW_CMDLINE_BAD_INPUTENC, OPT_STR _T("INPUTCHARSET: Ignoring invalid charset %") NPRIs , argv[argpos]);
502 cp = NStreamEncoding::AUTO;
503 }
504 inputenc.SafeSetCodepage(cp);
505 }
506 else if (S7IsChEqualI('v',*swname) &&
507 argv[argpos][2] >= _T('0') && argv[argpos][2] <= _T('4') && !argv[argpos][3])
508 {
509 int v=argv[argpos][2]-_T('0');
510 build.set_verbosity(v);
511 }
512 else if (S7IsChEqualI('p',*swname) &&
513 argv[argpos][2] >= _T('0') && argv[argpos][2] <= _T('5') && !argv[argpos][3])
514 {
515 #ifdef _WIN32
516 // priority setting added 01-2007 by Comm@nder21
517 int p=argv[argpos][2]-_T('0');
518 HANDLE hProc = GetCurrentProcess();
519 struct
520 {
521 DWORD priority, fallback;
522 } static const classes[] = {
523 {IDLE_PRIORITY_CLASS, IDLE_PRIORITY_CLASS},
524 {BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS},
525 {NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS},
526 {ABOVE_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS},
527 {HIGH_PRIORITY_CLASS, HIGH_PRIORITY_CLASS},
528 {REALTIME_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS}
529 };
530 if (!SetPriorityClass(hProc, classes[p].priority))
531 SetPriorityClass(hProc, classes[p].fallback);
532 if (p == 5) build.warning(DW_CMDLINE_HIGHPRIORITY,_T("makensis is running in REALTIME priority mode!"));
533 #else
534 build.warning(DW_CMDLINE_UNSUPP_NIX,badnonwinswitchfmt,_T("Px"));
535 #endif
536 }
537 // Already parsed these (must adjust argpos)
538 else if (!_tcsicmp(swname,_T("NOTIFYHWND"))) ++argpos;
539 else if (!_tcsicmp(swname,_T("OUTPUTCHARSET")) || !_tcsicmp(swname,_T("OCS"))) ++argpos;
540 // These must be parsed last because they will eat other switches
541 else if (S7IsChEqualI('d',swname[0]) && swname[1])
542 {
543 TCHAR *p=argv[argpos]+2;
544 TCHAR *s=_tcsdup(p),*v;
545 build.INFO_MSG(_T("Command line defined: \"%") NPRIs _T("\"\n"),p);
546 v=_tcsstr(s,_T("="));
547 if (v) *v++=0;
548 build.define(s,v?v:_T(""));
549 free(s);
550 }
551 else if (S7IsChEqualI('x',swname[0]) && swname[1])
552 {
553 if (build.process_oneline(swname+1,build.get_commandlinecode_filename(),argpos+1,build.PLF_VIRTUALFILE) != PS_OK)
554 {
555 return 1;
556 }
557 cmds_processed++;
558 }
559 // Already parsed these ("VERSION" never gets this far)
560 #ifdef _WIN32
561 else if (!_tcsicmp(swname,_T("RAW"))) {}
562 #endif
563 else if (S7IsChEqualI('o',swname[0]) && swname[1]) {}
564 else
565 break;
566 }
567 else
568 {
569 files_processed++;
570 if (!_tcscmp(argv[argpos],_T("-")) && !in_files)
571 g_dopause=false;
572 if (!noconfig)
573 {
574 noconfig=true;
575 tstring main_conf;
576 TCHAR* env_var = _tgetenv(_T("NSISCONFDIR"));
577 if (env_var == NULL)
578 #ifndef NSIS_CONFIG_CONST_DATA_PATH
579 main_conf = get_dir_name(get_executable_dir(argv[0]));
580 #else
581 main_conf = _T(PREFIX_CONF);
582 #endif
583 else
584 main_conf = env_var;
585 main_conf += PLATFORM_PATH_SEPARATOR_STR;
586 main_conf += _T("nsisconf.nsh");
587 if (process_config(build, main_conf))
588 return 1;
589
590 tstring home_conf = get_home();
591 if (home_conf != _T(""))
592 {
593 home_conf += PLATFORM_PATH_SEPARATOR_STR;
594 #ifdef _WIN32
595 home_conf += _T("nsisconf.nsh");
596 #else
597 home_conf += _T(".nsisconf.nsh");
598 #endif
599 if (process_config(build, home_conf))
600 return 1;
601 }
602 }
603
604 {
605 tstring nsifile;
606 NIStream strm;
607 if (!_tcscmp(argv[argpos],_T("-")) && !in_files)
608 {
609 strm.OpenStdIn(inputenc);
610 nsifile = _T("<stdin>");
611 }
612 else
613 {
614 nsifile = argv[argpos];
615 if (!strm.OpenFileForReading(nsifile.c_str(),inputenc))
616 {
617 nsifile += _T(".nsi");
618 if (!strm.OpenFileForReading(nsifile.c_str(),inputenc))
619 {
620 nsifile = argv[argpos];
621 build.ERROR_MSG(_T("Can't open script \"%") NPRIs _T("\"\n"),nsifile.c_str());
622 return 1;
623 }
624 }
625 build.set_default_output_filename(remove_file_extension(get_full_path(nsifile))+_T(".exe"));
626 if (do_cd)
627 {
628 if (change_to_script_dir(build, nsifile))
629 return 1;
630 }
631 }
632
633 build.notify(MakensisAPI::NOTIFY_SCRIPT,nsifile.c_str());
634 TCHAR bufcpdisp[20];
635 strm.StreamEncoding().GetCPDisplayName(bufcpdisp);
636 build.INFO_MSG(_T("Processing script file: \"%") NPRIs _T("\" (%") NPRIs _T(")\n"),nsifile.c_str(),bufcpdisp);
637 int ret=build.process_script(strm,nsifile.c_str());
638
639 if (ret != PS_EOF && ret != PS_OK)
640 {
641 build.ERROR_MSG(_T("Error in script \"%") NPRIs _T("\" on line %d -- aborting creation process\n"),nsifile.c_str(),build.linecnt);
642 return 1;
643 }
644 }
645 }
646 argpos++;
647 }
648
649 bool parsed_all_params = argpos >= argc, processed_any = files_processed || cmds_processed;
650 if (!parsed_all_params || !processed_any)
651 {
652 if (build.display_errors && !nousage)
653 {
654 print_usage();
655 }
656 return performed && parsed_all_params ? 0 : 1;
657 }
658
659 if (build.preprocessonly) return 0;
660
661 if (build.display_info)
662 {
663 _ftprintf(g_output,_T("\nProcessed "));
664 if (files_processed) _ftprintf(g_output,_T("%d file%") NPRIs _T(", "),files_processed,files_processed==1?_T(""):_T("s"));
665 if (cmds_processed) _ftprintf(g_output,_T("%d command line command%") NPRIs _T(", "),cmds_processed,cmds_processed==1?_T(""):_T("s"));
666 _ftprintf(g_output,_T("writing output (%") NPRIs _T("):\n"),build.get_target_suffix());
667 fflush(g_output);
668 }
669
670 if (build.write_output())
671 {
672 build.ERROR_MSG(_T("Error - aborting creation process\n"));
673 return 1;
674 }
675
676 if (do_exec)
677 {
678 const TCHAR *app = build.get_output_filename();
679 #ifdef _WIN32
680 ShellExecute(0, 0, app, 0, 0, SW_SHOW);
681 #else
682 const TCHAR *cmd = _tgetenv(_T("NSISLAUNCHCOMMAND"));
683 sane_system(replace_all(cmd ? cmd : _T("wine start '%1'"), _T("%1"), app).c_str());
684 #endif
685 }
686
687 return 0;
688 }
689
690
691 #ifndef NDEBUG
692 # ifdef _MSC_VER
693 # include <crtdbg.h>
694 # endif
695 #endif
696
697 NSIS_ENTRYPOINT_TMAIN
698 int _tmain(int argc, TCHAR **argv)
699 {
700 #ifndef NDEBUG
701 #ifdef _MSC_VER
702 const int dbgchkthorough = true, checkeveryallocfree = dbgchkthorough ? _CRTDBG_CHECK_ALWAYS_DF : 0;
703 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF | checkeveryallocfree | _CRTDBG_LEAK_CHECK_DF);
704 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE), _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
705 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE), _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
706 //_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE), _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
707 #endif
708 #endif
709 int retval = makensismain(argc,argv);
710 #ifndef NDEBUG
711 #ifdef _MSC_VER
712 assert(_CrtCheckMemory());
713 #endif
714 #endif
715 return retval;
716 }
717
718
719 #if !defined(_WIN32) && defined(_UNICODE)
720 #include <errno.h>
721 int main(int argc, char **argv)
722 {
723 errno = ENOMEM;
724 int wargc = 0;
725 wchar_t term[1], *p, **wargv = (wchar_t **) malloc((argc+1) * sizeof(void*));
726 if (wargv)
727 {
728 for ( ; wargc < argc; ++wargc )
729 if ((p = NSISRT_mbtowc(argv[wargc]))) wargv[wargc] = p; else break;
730 }
731 if (wargc == argc)
732 *term = L'\0', wargv[wargc] = term, errno = _tmain(wargc,wargv);
733 else
734 wprintf(L"FATAL: main argv conversion failed!\n");
735 #ifndef NDEBUG // Normally we just leak
736 if (wargv) for ( int i = 0; i < wargc; ++i ) NSISRT_free(wargv[i]);
737 free(wargv);
738 #endif
739 return errno;
740 }
741 #endif
742