home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / exehead / Main.c < prev    next >
C/C++ Source or Header  |  2004-02-06  |  8KB  |  278 lines

  1. /*
  2.   Nullsoft "SuperPimp" Installation System - main.c - executable header main code
  3.  
  4.   Copyright (C) 1999-2004 Nullsoft, Inc.
  5.  
  6.   This software is provided 'as-is', without any express or implied
  7.   warranty.  In no event will the authors be held liable for any damages
  8.   arising from the use of this software.
  9.  
  10.   Permission is granted to anyone to use this software for any purpose,
  11.   including commercial applications, and to alter it and redistribute it
  12.   freely, subject to the following restrictions:
  13.  
  14.   1. The origin of this software must not be misrepresented; you must not
  15.      claim that you wrote the original software. If you use this software
  16.      in a product, an acknowledgment in the product documentation would be
  17.      appreciated but is not required.
  18.   2. Altered source versions must be plainly marked as such, and must not be
  19.      misrepresented as being the original software.
  20.   3. This notice may not be removed or altered from any source distribution.
  21.  
  22.   This source distribution includes portions of zlib. see zlib/zlib.h for
  23.   its license and so forth. Note that this license is also borrowed from zlib.
  24. */
  25.  
  26.  
  27. #include "../Platform.h"
  28. #include <shlobj.h>
  29. #include "resource.h"
  30. #include "util.h"
  31. #include "fileform.h"
  32. #include "state.h"
  33. #include "ui.h"
  34. #include "lang.h"
  35. #include "state.h"
  36.  
  37. #if !defined(NSIS_CONFIG_VISIBLE_SUPPORT) && !defined(NSIS_CONFIG_SILENT_SUPPORT)
  38. #error One of NSIS_CONFIG_SILENT_SUPPORT or NSIS_CONFIG_VISIBLE_SUPPORT must be defined.
  39. #endif
  40. #ifdef NSIS_COMPRESS_WHOLE
  41. extern HANDLE dbd_hFile;
  42. #endif
  43.  
  44. char g_caption[NSIS_MAX_STRLEN*2];
  45. int g_filehdrsize;
  46. #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
  47. HWND g_hwnd;
  48. HANDLE g_hInstance;
  49. #endif
  50.  
  51. void NSISCALL CleanUp();
  52.  
  53. char *ValidateTempDir()
  54. {
  55.   validate_filename(state_temp_dir);
  56.   if (!validpathspec(state_temp_dir))
  57.     return NULL;
  58.   addtrailingslash(state_temp_dir);
  59.   CreateDirectory(state_temp_dir, NULL);
  60.   // g_caption is used as a temp var here
  61.   return my_GetTempFileName(g_caption, state_temp_dir);
  62. }
  63.  
  64. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpszCmdParam, int nCmdShow)
  65. {
  66.   int ret;
  67.   const char *m_Err = _LANG_ERRORWRITINGTEMP;
  68.  
  69.   int cl_flags = 0;
  70.  
  71.   char *realcmds;
  72.   char seekchar=' ';
  73.   char *cmdline;
  74.  
  75.   InitCommonControls();
  76.  
  77. #if defined(NSIS_SUPPORT_ACTIVEXREG) || defined(NSIS_SUPPORT_CREATESHORTCUT)
  78.   {
  79.     extern HRESULT g_hres;
  80.     g_hres=OleInitialize(NULL);
  81.   }
  82. #endif
  83.  
  84.   GetTempPath(NSIS_MAX_STRLEN, state_temp_dir);
  85.   if (!ValidateTempDir())
  86.   {
  87.     GetWindowsDirectory(state_temp_dir, NSIS_MAX_STRLEN - 5); // leave space for \Temp
  88.     lstrcat(state_temp_dir, "\\Temp");
  89.     if (!ValidateTempDir())
  90.     {
  91.       goto end;
  92.     }
  93.   }
  94.   DeleteFile(g_caption);
  95.  
  96.   mystrcpy(g_caption,_LANG_GENERIC_ERROR);
  97.  
  98.   lstrcpyn(state_command_line, GetCommandLine(), NSIS_MAX_STRLEN);
  99.  
  100. #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
  101.   g_hInstance = GetModuleHandle(NULL);
  102. #endif//NSIS_CONFIG_VISIBLE_SUPPORT
  103.  
  104.   cmdline = state_command_line;
  105.   if (*cmdline == '\"') seekchar = *cmdline++;
  106.  
  107.   cmdline=findchar(cmdline, seekchar);
  108.   cmdline=CharNext(cmdline);
  109.   realcmds=cmdline;
  110.  
  111.   while (*cmdline)
  112.   {
  113.     // skip over any spaces
  114.     while (*cmdline == ' ') cmdline++;
  115.     
  116.     // get char we should look for to get the next parm
  117.     seekchar = ' ';
  118.     if (cmdline[0] == '\"')
  119.     {
  120.       cmdline++;
  121.       seekchar = '\"';
  122.     }
  123.  
  124.     // is it a switch?
  125.     if (cmdline[0] == '/')
  126.     {
  127.       cmdline++;
  128.  
  129. // this only works with spaces because they have just one bit on
  130. #define END_OF_ARG(c) (((c)|' ')==' ')
  131.  
  132. #if defined(NSIS_CONFIG_VISIBLE_SUPPORT) && defined(NSIS_CONFIG_SILENT_SUPPORT)
  133.       if (cmdline[0] == 'S' && END_OF_ARG(cmdline[1]))
  134.         cl_flags |= FH_FLAGS_SILENT;
  135. #endif//NSIS_CONFIG_SILENT_SUPPORT && NSIS_CONFIG_VISIBLE_SUPPORT
  136. #ifdef NSIS_CONFIG_CRC_SUPPORT
  137.       if (*(DWORD*)cmdline == CHAR4_TO_DWORD('N','C','R','C') && END_OF_ARG(cmdline[4]))
  138.         cl_flags |= FH_FLAGS_NO_CRC;
  139. #endif//NSIS_CONFIG_CRC_SUPPORT
  140.  
  141.       if (*(WORD*)cmdline == CHAR2_TO_WORD('D','='))
  142.       {
  143.         cmdline[-1]=0; // keep this from being passed to uninstaller if necessary
  144.         mystrcpy(state_install_directory,cmdline+2);
  145.         break; // /D= must always be last
  146.       }
  147.     }
  148.  
  149.     // skip over our parm
  150.     cmdline = findchar(cmdline, seekchar);
  151.     // skip the quote
  152.     if (*cmdline == '\"')
  153.       cmdline++;
  154.   }
  155.  
  156.   m_Err = loadHeaders(cl_flags);
  157.   if (m_Err) goto end;
  158.  
  159. #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
  160.   if (g_is_uninstaller)
  161.   {
  162.     char *p = findchar(realcmds, 0);
  163.  
  164.     while (p >= realcmds && (p[0] != '_' || p[1] != '?' || p[2] != '=')) p--;
  165.  
  166.     m_Err = _LANG_UNINSTINITERROR;
  167.  
  168.     if (p >= realcmds)
  169.     {
  170.       *p=0; // terminate before the "_?="
  171.       p+=3; // skip over _?=
  172.       if (is_valid_instpath(p))
  173.       {
  174.         mystrcpy(state_install_directory, p);
  175.         mystrcpy(state_output_directory, p);
  176.         m_Err = 0;
  177.       }
  178.       else
  179.       {
  180.         goto end;
  181.       }
  182.     }
  183.     else
  184.     {
  185.       int x;
  186.  
  187.       for (x = 0; x < 26; x ++)
  188.       {
  189.         // File name need slash before because temp dir was changed by validate_filename
  190.         static char s[]="A~NSISu_.exe";
  191.         static char buf2[NSIS_MAX_STRLEN*2];
  192.         static char ibuf[NSIS_MAX_STRLEN];
  193.  
  194.         buf2[0]='\"';
  195.         mystrcpy(buf2+1,state_temp_dir);
  196.         lstrcat(buf2,s);
  197.  
  198.         DeleteFile(buf2+1); // clean up after all the other ones if they are there
  199.  
  200.         if (m_Err) // not done yet
  201.         {
  202.           // get current name
  203.           int l=GetModuleFileName(g_hInstance,ibuf,sizeof(ibuf));
  204.           // check if it is ?~NSISu_.exe - if so, fuck it
  205.           if (!lstrcmpi(ibuf+l-(sizeof(s)-2),s+1)) break;
  206.  
  207.           // copy file
  208.           if (CopyFile(ibuf,buf2+1,FALSE))
  209.           {
  210.             HANDLE hProc;
  211. #ifdef NSIS_SUPPORT_MOVEONREBOOT
  212.             MoveFileOnReboot(buf2+1,NULL);
  213. #endif
  214.             if (state_install_directory[0]) mystrcpy(ibuf,state_install_directory);
  215.             else trimslashtoend(ibuf);
  216.             lstrcat(buf2,"\" ");
  217.             lstrcat(buf2,realcmds);
  218.             lstrcat(buf2," _?=");
  219.             lstrcat(buf2,ibuf);
  220.             // add a trailing backslash to make sure is_valid_instpath will not fail when it shouldn't
  221.             addtrailingslash(buf2);
  222.             hProc=myCreateProcess(buf2,state_temp_dir);
  223.             if (hProc)
  224.             {
  225.               CloseHandle(hProc);
  226.               // success
  227.               m_Err = 0;
  228.             }
  229.           }
  230.         }
  231.         s[0]++;
  232.       }
  233.       goto end;
  234.     }
  235.   }
  236. #endif//NSIS_CONFIG_UNINSTALL_SUPPORT
  237.  
  238.   ret = ui_doinstall();
  239.  
  240. #ifdef NSIS_CONFIG_LOG
  241. #ifndef NSIS_CONFIG_LOG_ODS
  242.   log_write(1);
  243. #endif//!NSIS_CONFIG_LOG_ODS
  244. #endif//NSIS_CONFIG_LOG
  245. end:
  246.  
  247.   CleanUp();
  248.  
  249.   if (m_Err)
  250.     my_MessageBox(m_Err, MB_OK | MB_ICONSTOP | (IDOK << 20));
  251.  
  252. #if defined(NSIS_SUPPORT_ACTIVEXREG) || defined(NSIS_SUPPORT_CREATESHORTCUT)
  253.   OleUninitialize();
  254. #endif
  255.  
  256.   ExitProcess(ret);
  257. }
  258.  
  259. void NSISCALL CleanUp()
  260. {
  261.   if (g_db_hFile != INVALID_HANDLE_VALUE)
  262.   {
  263.     CloseHandle(g_db_hFile);
  264.     g_db_hFile = INVALID_HANDLE_VALUE;
  265.   }
  266. #ifdef NSIS_COMPRESS_WHOLE
  267.   if (dbd_hFile != INVALID_HANDLE_VALUE)
  268.   {
  269.     CloseHandle(dbd_hFile);
  270.     dbd_hFile = INVALID_HANDLE_VALUE;
  271.   }
  272. #endif
  273. #ifdef NSIS_CONFIG_PLUGIN_SUPPORT
  274.   // Clean up after plug-ins
  275.   doRMDir(state_plugins_dir, 1);
  276. #endif // NSIS_CONFIG_PLUGIN_SUPPORT
  277. }
  278.