home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / Makensisw / utils.cpp < prev    next >
C/C++ Source or Header  |  2004-01-20  |  25KB  |  921 lines

  1. /*
  2.   Copyright (c) 2002 Robert Rainwater
  3.   Contributors: Justin Frankel, Fritz Elfert, Amir Szekely, Sunil Kamath, Joost Verburg
  4.  
  5.   This software is provided 'as-is', without any express or implied
  6.   warranty.  In no event will the authors be held liable for any damages
  7.   arising from the use of this software.
  8.  
  9.   Permission is granted to anyone to use this software for any purpose,
  10.   including commercial applications, and to alter it and redistribute it
  11.   freely, subject to the following restrictions:
  12.  
  13.   1. The origin of this software must not be misrepresented; you must not
  14.    claim that you wrote the original software. If you use this software
  15.    in a product, an acknowledgment in the product documentation would be
  16.    appreciated but is not required.
  17.   2. Altered source versions must be plainly marked as such, and must not be
  18.    misrepresented as being the original software.
  19.   3. This notice may not be removed or altered from any source distribution.
  20.  
  21. */
  22. #include <windows.h>
  23. #include "resource.h"
  24. #include "makensisw.h"
  25. #include "toolbar.h"
  26. #include "noclib.h"
  27.  
  28. NTOOLTIP g_tip;
  29. LRESULT CALLBACK TipHookProc(int nCode, WPARAM wParam, LPARAM lParam);
  30.  
  31. char g_mru_list[MRU_LIST_SIZE][MAX_PATH] = { NULL, NULL, NULL, NULL, NULL };
  32.  
  33. extern NSCRIPTDATA g_sdata;
  34. extern char *compressor_names[];
  35.  
  36. int SetArgv(char *cmdLine, int *argc, char ***argv)
  37. {
  38.   char *p, *arg, *argSpace;
  39.   int size, argSpaceSize, inquote, copy, slashes;
  40.  
  41.   size = 2;
  42.   for (p = cmdLine; *p != '\0'; p++) {
  43.     if ((*p == ' ') || (*p == '\t')) {
  44.       size++;
  45.       while ((*p == ' ') || (*p == '\t')) {
  46.         p++;
  47.       }
  48.       if (*p == '\0') {
  49.         break;
  50.       }
  51.     }
  52.   }
  53.  
  54.   argSpaceSize = size * sizeof(char *) + lstrlen(cmdLine) + 1;
  55.   argSpace = (char *) GlobalAlloc(GMEM_FIXED, argSpaceSize);
  56.   if (!argSpace)
  57.     return 0;
  58.  
  59.   *argv = (char **) argSpace;
  60.   argSpace += size * sizeof(char *);
  61.   size--;
  62.  
  63.   p = cmdLine;
  64.   for (*argc = 0; *argc < size; (*argc)++) {
  65.     (*argv)[*argc] = arg = argSpace;
  66.     while ((*p == ' ') || (*p == '\t')) {
  67.       p++;
  68.     }
  69.     if (*p == '\0') {
  70.       break;
  71.     }
  72.  
  73.     inquote = 0;
  74.     slashes = 0;
  75.     while (1) {
  76.       copy = 1;
  77.       while (*p == '\\') {
  78.         slashes++;
  79.         p++;
  80.       }
  81.       if (*p == '"') {
  82.         if ((slashes & 1) == 0) {
  83.           copy = 0;
  84.           if ((inquote) && (p[1] == '"')) {
  85.             p++;
  86.             copy = 1;
  87.           }
  88.           else {
  89.             inquote = !inquote;
  90.           }
  91.         }
  92.         slashes >>= 1;
  93.       }
  94.  
  95.       while (slashes) {
  96.         *arg = '\\';
  97.         arg++;
  98.         slashes--;
  99.       }
  100.  
  101.       if ((*p == '\0') || (!inquote && ((*p == ' ') || (*p == '\t')))) {
  102.         break;
  103.       }
  104.       if (copy != 0) {
  105.         *arg = *p;
  106.         arg++;
  107.       }
  108.       p++;
  109.     }
  110.     *arg = '\0';
  111.     argSpace = arg + 1;
  112.   }
  113.   (*argv)[*argc] = NULL;
  114.  
  115.   return argSpaceSize;
  116. }
  117.  
  118. void SetTitle(HWND hwnd,char *substr) {
  119.   char title[64];
  120.   if (substr==NULL) wsprintf(title,"MakeNSISW");
  121.   else wsprintf(title,"MakeNSISW - %s",substr);
  122.   SetWindowText(hwnd,title);
  123. }
  124.  
  125. void SetBranding(HWND hwnd) {
  126.   SetDlgItemText(hwnd, IDC_VERSION, g_sdata.branding);
  127. }
  128.  
  129. void CopyToClipboard(HWND hwnd) {
  130.   if (!hwnd||!OpenClipboard(hwnd)) return;
  131.   int len=SendDlgItemMessage(hwnd,IDC_LOGWIN,WM_GETTEXTLENGTH,0,0);
  132.   HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE,len+1);
  133.   if (!mem) { CloseClipboard(); return; }
  134.   char *existing_text = (char *)GlobalLock(mem);
  135.   if (!existing_text) { CloseClipboard(); return; }
  136.   EmptyClipboard();
  137.   existing_text[0]=0;
  138.   GetDlgItemText(hwnd, IDC_LOGWIN, existing_text, len+1);
  139.   GlobalUnlock(mem);
  140.   SetClipboardData(CF_TEXT,mem);
  141.   CloseClipboard();
  142. }
  143.  
  144. void ClearLog(HWND hwnd) {
  145.   SetDlgItemText(hwnd, IDC_LOGWIN, "");
  146. }
  147.  
  148. void LogMessage(HWND hwnd,const char *str) {
  149.   SendDlgItemMessage(hwnd, IDC_LOGWIN, EM_SETSEL, g_sdata.logLength, g_sdata.logLength);
  150.   g_sdata.logLength += lstrlen(str);
  151.   SendDlgItemMessage(hwnd, IDC_LOGWIN, EM_REPLACESEL, 0, (WPARAM)str);
  152.   SendDlgItemMessage(hwnd, IDC_LOGWIN, EM_SCROLLCARET, 0, 0);
  153. }
  154.  
  155. void ErrorMessage(HWND hwnd,const char *str) {
  156.   if (!str) return;
  157.   char buf[1028];
  158.   wsprintf(buf,"[Error] %s\r\n",str);
  159.   LogMessage(hwnd,buf);
  160. }
  161.  
  162. // Altered by Darren Owen (DrO) on 1/10/2003
  163. void Items(HWND hwnd, int on){
  164.   UINT mf = (!on ? MF_GRAYED : MF_ENABLED);
  165.  
  166.   if(!on)
  167.       g_sdata.focused_hwnd = GetFocus();
  168.   // Altered by Darren Owen (DrO) on 6/10/2003
  169.   else
  170.     EnableWindow(GetDlgItem(hwnd,IDC_CLOSE),1);
  171.  
  172.   EnableWindow(GetDlgItem(hwnd,IDC_CLOSE),on);
  173.   // Altered by Darren Owen (DrO) on 6/10/2003
  174.   if((!g_sdata.retcode && on) || !on)
  175.     EnableWindow(GetDlgItem(hwnd,IDC_TEST),on);
  176.   EnableWindow(GetDlgItem(hwnd,IDC_RECOMPILE_TEST),on);
  177.  
  178.   EnableMenuItem(g_sdata.menu,IDM_SAVE,mf);
  179.   // Altered by Darren Owen (DrO) on 6/10/2003
  180.   if((!g_sdata.retcode && on) || !on)
  181.     EnableMenuItem(g_sdata.menu,IDM_TEST,mf);
  182.   EnableMenuItem(g_sdata.menu,IDM_EXIT,mf);
  183.   EnableMenuItem(g_sdata.menu,IDM_LOADSCRIPT,mf);
  184.   EnableMenuItem(g_sdata.menu,IDM_RECOMPILE,mf);
  185.   EnableMenuItem(g_sdata.menu,IDM_COPY,mf);
  186.   EnableMenuItem(g_sdata.menu,IDM_COPYSELECTED,mf);
  187.   EnableMenuItem(g_sdata.menu,IDM_EDITSCRIPT,mf);
  188.   EnableMenuItem(g_sdata.menu,IDM_CLEARLOG,mf);
  189.   EnableMenuItem(g_sdata.menu,IDM_BROWSESCR,mf);
  190.   EnableMenuItem(g_sdata.menu,IDM_RECOMPILE_TEST,mf);
  191.   EnableMenuItem(g_sdata.menu,IDM_COMPRESSOR,mf);
  192.  
  193.   EnableToolBarButton(IDM_SAVE,on);
  194.   // Altered by Darren Owen (DrO) on 6/10/2003
  195.   if((!g_sdata.retcode && on) || !on)
  196.     EnableToolBarButton(IDM_TEST,on);
  197.   EnableToolBarButton(IDM_EXIT,on);
  198.   EnableToolBarButton(IDM_LOADSCRIPT,on);
  199.   EnableToolBarButton(IDM_RECOMPILE,on);
  200.   EnableToolBarButton(IDM_COPY,on);
  201.   EnableToolBarButton(IDM_EDITSCRIPT,on);
  202.   EnableToolBarButton(IDM_CLEARLOG,on);
  203.   EnableToolBarButton(IDM_BROWSESCR,on);
  204.   EnableToolBarButton(IDM_RECOMPILE_TEST,on);
  205.   EnableToolBarButton(IDM_COMPRESSOR,on);
  206.  
  207.   if(!on) {
  208.     if (!IsWindowEnabled(g_sdata.focused_hwnd))
  209.       SetFocus(GetDlgItem(hwnd,IDC_LOGWIN));
  210.   }
  211.   else
  212.     SetFocus(g_sdata.focused_hwnd);
  213. }
  214.  
  215. void SetCompressorStats()
  216. {
  217.   DWORD line_count, i;
  218.   char buf[1024];
  219.   bool found = false;
  220.  
  221.   line_count = SendDlgItemMessage(g_sdata.hwnd, IDC_LOGWIN, EM_GETLINECOUNT, 0, 0);
  222.   for(i=0; i<line_count; i++) {
  223.     *((LPWORD)buf) = sizeof(buf); 
  224.     SendDlgItemMessage(g_sdata.hwnd, IDC_LOGWIN, EM_GETLINE, (WPARAM)i, (LPARAM)buf);
  225.     if(found) {
  226.       DWORD len = lstrlen(TOTAL_SIZE_COMPRESSOR_STAT);
  227.       lstrcat(g_sdata.compressor_stats,buf);
  228.  
  229.       if(!lstrncmp(buf,TOTAL_SIZE_COMPRESSOR_STAT,len)) {
  230.         break;
  231.       }
  232.     }
  233.     else {
  234.       DWORD len = lstrlen(EXE_HEADER_COMPRESSOR_STAT);
  235.       if(!lstrncmp(buf,EXE_HEADER_COMPRESSOR_STAT,len)) {
  236.         found = true;
  237.         lstrcpy(g_sdata.compressor_stats,"\n\n");
  238.         lstrcat(g_sdata.compressor_stats,buf);
  239.       }
  240.     }
  241.   }
  242. }
  243.  
  244. void CompileNSISScript() {
  245.   static char *s;
  246.   DragAcceptFiles(g_sdata.hwnd,FALSE);
  247.   ClearLog(g_sdata.hwnd);
  248.   SetTitle(g_sdata.hwnd,NULL);
  249.   if (lstrlen(g_sdata.script)==0) {
  250.     LogMessage(g_sdata.hwnd,USAGE);
  251.     EnableMenuItem(g_sdata.menu,IDM_RECOMPILE,MF_GRAYED);
  252.     EnableMenuItem(g_sdata.menu,IDM_EDITSCRIPT,MF_GRAYED);
  253.     EnableMenuItem(g_sdata.menu,IDM_TEST,MF_GRAYED);
  254.     EnableMenuItem(g_sdata.menu,IDM_BROWSESCR,MF_GRAYED);
  255.     // Added by Darren Owen (DrO) on 1/10/2003
  256.     EnableMenuItem(g_sdata.menu,IDM_RECOMPILE_TEST,MF_GRAYED);
  257.  
  258.     EnableToolBarButton(IDM_RECOMPILE,FALSE);
  259.     EnableToolBarButton(IDM_EDITSCRIPT,FALSE);
  260.     EnableToolBarButton(IDM_TEST,FALSE);
  261.     EnableToolBarButton(IDM_RECOMPILE_TEST,FALSE);
  262.     EnableToolBarButton(IDM_BROWSESCR,FALSE);
  263.  
  264.     EnableWindow(GetDlgItem(g_sdata.hwnd,IDC_TEST),0);
  265.     DragAcceptFiles(g_sdata.hwnd,TRUE);
  266.     return;
  267.   }
  268.   if (!g_sdata.appended) {
  269.     if (s) GlobalFree(s);
  270.     char *symbols = BuildSymbols();
  271.     
  272.     char compressor[40];
  273.     if(lstrlen(g_sdata.compressor_name)) {
  274.       wsprintf(compressor,"/X\"SetCompressor /FINAL %s\" ",g_sdata.compressor_name);
  275.     }
  276.     else {
  277.       lstrcpy(compressor,"");
  278.     }
  279.  
  280.     s = (char *)GlobalAlloc(GPTR, lstrlen(g_sdata.script)+lstrlen(symbols)+lstrlen(compressor)+sizeof(EXENAME)+sizeof(" /NOTIFYHWND  ")+20);
  281.     wsprintf(s,"%s %s%s /NOTIFYHWND %d %s",EXENAME,compressor,symbols,g_sdata.hwnd,g_sdata.script);
  282.     GlobalFree(symbols);
  283.     if (g_sdata.script_alloced) GlobalFree(g_sdata.script);
  284.     g_sdata.script_alloced = true;
  285.     g_sdata.script = s;
  286.     g_sdata.appended = TRUE;
  287.   }
  288.   GlobalFree(g_sdata.input_script);
  289.   GlobalFree(g_sdata.output_exe);
  290.   g_sdata.input_script = 0;
  291.   g_sdata.output_exe = 0;
  292.   g_sdata.warnings = 0;
  293.   g_sdata.logLength = 0;
  294.   // Disable buttons during compile
  295.   DisableItems(g_sdata.hwnd);
  296.   DWORD id;
  297.   g_sdata.thread=CreateThread(NULL,0,MakeNSISProc,0,0,&id);
  298. }
  299.  
  300. void RestoreWindowPos(HWND hwnd) {
  301.   HKEY hKey;
  302.   WINDOWPLACEMENT p;
  303.   if (RegOpenKeyEx(REGSEC,REGKEY,0,KEY_READ,&hKey) == ERROR_SUCCESS) {
  304.     DWORD l = sizeof(p);
  305.     DWORD t;
  306.     if ((RegQueryValueEx(hKey,REGLOC,NULL,&t,(unsigned char*)&p,&l)==ERROR_SUCCESS)&&(t == REG_BINARY)&&(l==sizeof(p))) {
  307.       int width, height;
  308.       int windowWidth, windowHeight;
  309.  
  310.       width = GetSystemMetrics(SM_CXFULLSCREEN);
  311.       height = GetSystemMetrics(SM_CYFULLSCREEN);
  312.       height += GetSystemMetrics(SM_CYCAPTION);
  313.       windowWidth = p.rcNormalPosition.right-p.rcNormalPosition.left;
  314.       if(windowWidth > width) {
  315.         p.rcNormalPosition.left = 0;
  316.         p.rcNormalPosition.right = width;
  317.       }
  318.       else if(p.rcNormalPosition.right > width) {
  319.         p.rcNormalPosition.left = width - windowWidth;
  320.         p.rcNormalPosition.right = width;
  321.       }
  322.       else if(p.rcNormalPosition.left < 0) {
  323.         p.rcNormalPosition.left = 0;
  324.         p.rcNormalPosition.right = windowWidth;
  325.       }
  326.  
  327.       windowHeight = p.rcNormalPosition.bottom-p.rcNormalPosition.top;
  328.       if(windowHeight > height) {
  329.         p.rcNormalPosition.top = 0;
  330.         p.rcNormalPosition.bottom = height;
  331.       }
  332.       else if(p.rcNormalPosition.bottom > height) {
  333.         p.rcNormalPosition.top = height - windowHeight;
  334.         p.rcNormalPosition.bottom = height;
  335.       }
  336.       else if(p.rcNormalPosition.top < 0) {
  337.         p.rcNormalPosition.top = 0;
  338.         p.rcNormalPosition.bottom = windowHeight;
  339.       }
  340.  
  341.       p.length = sizeof(p);
  342.       SetWindowPlacement(hwnd, &p);
  343.     }
  344.     RegCloseKey(hKey);
  345.   }
  346. }
  347.  
  348. void SaveWindowPos(HWND hwnd) {
  349.   HKEY hKey;
  350.   WINDOWPLACEMENT p;
  351.   p.length = sizeof(p);
  352.   GetWindowPlacement(hwnd, &p);
  353.   if (RegCreateKey(REGSEC,REGKEY,&hKey) == ERROR_SUCCESS) {
  354.     RegSetValueEx(hKey,REGLOC,0,REG_BINARY,(unsigned char*)&p,sizeof(p));
  355.     RegCloseKey(hKey);
  356.   }
  357. }
  358.  
  359. void RestoreSymbols()
  360. {
  361.   g_sdata.symbols = LoadSymbolSet(NULL);
  362. }
  363.  
  364. void SaveSymbols()
  365. {
  366.   SaveSymbolSet(NULL, g_sdata.symbols);
  367. }
  368.  
  369. void DeleteSymbolSet(char *name)
  370. {
  371.   if(name) {
  372.     HKEY hKey;
  373.     if (RegOpenKeyEx(REGSEC,REGKEY,0,KEY_READ,&hKey) == ERROR_SUCCESS) {
  374.       char subkey[1024];
  375.       wsprintf(subkey,"%s\\%s",REGSYMSUBKEY,name);
  376.       RegDeleteKey(hKey,subkey);
  377.       RegCloseKey(hKey);
  378.     }
  379.   }
  380. }
  381.  
  382. char** LoadSymbolSet(char *name)
  383. {
  384.   HKEY hKey;
  385.   HKEY hSubKey;
  386.   char **symbols = NULL;
  387.   if (RegOpenKeyEx(REGSEC,REGKEY,0,KEY_READ,&hKey) == ERROR_SUCCESS) {
  388.     char subkey[1024];
  389.     if(name) {
  390.       wsprintf(subkey,"%s\\%s",REGSYMSUBKEY,name);
  391.     }
  392.     else {
  393.       lstrcpy(subkey,REGSYMSUBKEY);
  394.     }
  395.     if (RegCreateKey(hKey,subkey,&hSubKey) == ERROR_SUCCESS) {
  396.       char buf[8];
  397.       DWORD l;
  398.       DWORD t;
  399.       DWORD bufSize;
  400.       DWORD i = 0;
  401.       HGLOBAL hMem;
  402.  
  403.       while(TRUE) {
  404.         l = 0;
  405.         bufSize = sizeof(buf);
  406.         if ((RegEnumValue(hSubKey,i, buf, &bufSize,NULL,&t,NULL,&l)==ERROR_SUCCESS)&&(t == REG_SZ)) {
  407.           if(symbols) {
  408.             GlobalUnlock(hMem);
  409.             hMem = GlobalReAlloc(hMem, (i+2)*sizeof(char *), GMEM_MOVEABLE|GMEM_ZEROINIT);
  410.             symbols = (char **)GlobalLock(hMem);
  411.           }
  412.           else {
  413.             hMem = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, (i+2)*sizeof(char *));
  414.             symbols = (char **)GlobalLock(hMem);
  415.           }
  416.           if(symbols) {
  417.             l++;
  418.             symbols[i] = (char *)GlobalAlloc(GPTR, l*sizeof(char));
  419.             if (symbols[i]) {
  420.               RegQueryValueEx(hSubKey,buf,NULL,&t,(unsigned char*)symbols[i],&l);
  421.             }
  422.             else {
  423.               break;
  424.             }
  425.           }
  426.           else {
  427.             break;
  428.           }
  429.           i++;
  430.           symbols[i] = NULL;
  431.         }
  432.         else {
  433.           break;
  434.         }
  435.       }
  436.       RegCloseKey(hSubKey);
  437.     }
  438.     RegCloseKey(hKey);
  439.   }
  440.  
  441.   return symbols;
  442. }
  443.  
  444. void SaveSymbolSet(char *name, char **symbols)
  445. {
  446.   HKEY hKey;
  447.   HKEY hSubKey;
  448.   int n = 0;
  449.   if (RegCreateKey(REGSEC,REGKEY,&hKey) == ERROR_SUCCESS) {
  450.     char subkey[1024];
  451.     if(name) {
  452.       wsprintf(subkey,"%s\\%s",REGSYMSUBKEY,name);
  453.     }
  454.     else {
  455.       lstrcpy(subkey,REGSYMSUBKEY);
  456.     }
  457.  
  458.     if (RegOpenKey(hKey,subkey,&hSubKey) == ERROR_SUCCESS) {
  459.       char buf[8];
  460.       DWORD l;
  461.       while(TRUE) {
  462.         l = sizeof(buf);
  463.         if (RegEnumValue(hSubKey,0, buf, &l,NULL,NULL,NULL,NULL)==ERROR_SUCCESS) {
  464.           RegDeleteValue(hSubKey,buf);
  465.         }
  466.         else {
  467.           break;
  468.         }
  469.       }
  470.       RegCloseKey(hSubKey);
  471.     }
  472.     if(symbols) {
  473.       if (RegCreateKey(hKey,subkey,&hSubKey) == ERROR_SUCCESS) {
  474.         char buf[8];
  475.         n = 0;
  476.         while(symbols[n]) {
  477.           wsprintf(buf,"%d",n);
  478.           RegSetValueEx(hSubKey,buf,0,REG_SZ,(CONST BYTE *)symbols[n],lstrlen(symbols[n])+1);
  479.           n++;
  480.         }
  481.         RegCloseKey(hSubKey);
  482.       }
  483.     }
  484.     RegCloseKey(hKey);
  485.   }
  486. }
  487.  
  488. void ResetObjects() {
  489.   g_sdata.appended = FALSE;
  490.   g_sdata.warnings = FALSE;
  491.   g_sdata.retcode = -1;
  492.   g_sdata.thread = NULL;
  493. }
  494.  
  495. void ResetSymbols() {
  496.   if(g_sdata.symbols) {
  497.     HGLOBAL hMem;
  498.     int i = 0;
  499.     while(g_sdata.symbols[i]) {
  500.       GlobalFree(g_sdata.symbols[i]);
  501.       i++;
  502.     }
  503.     hMem = GlobalHandle(g_sdata.symbols);
  504.     GlobalUnlock(hMem);
  505.     GlobalFree(hMem);
  506.     g_sdata.symbols = NULL;
  507.   }
  508. }
  509.  
  510. int InitBranding() {
  511.   char *s;
  512.   s = (char *)GlobalAlloc(GPTR,lstrlen(EXENAME)+10);
  513.   wsprintf(s,"%s /version",EXENAME);
  514.   {
  515.     STARTUPINFO si={sizeof(si),};
  516.     SECURITY_ATTRIBUTES sa={sizeof(sa),};
  517.     SECURITY_DESCRIPTOR sd={0,};
  518.     PROCESS_INFORMATION pi={0,};
  519.     HANDLE newstdout=0,read_stdout=0; 
  520.  
  521.     OSVERSIONINFO osv={sizeof(osv)};
  522.     GetVersionEx(&osv);
  523.     if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  524.       InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
  525.       SetSecurityDescriptorDacl(&sd,true,NULL,false);
  526.       sa.lpSecurityDescriptor = &sd;
  527.     }
  528.     else sa.lpSecurityDescriptor = NULL;
  529.     sa.bInheritHandle = true;
  530.     if (!CreatePipe(&read_stdout,&newstdout,&sa,0)) {
  531.       return 0;
  532.     }
  533.     GetStartupInfo(&si);
  534.     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  535.     si.wShowWindow = SW_HIDE;
  536.     si.hStdOutput = newstdout;
  537.     si.hStdError = newstdout;
  538.     if (!CreateProcess(NULL,s,NULL,NULL,TRUE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)) {
  539.       CloseHandle(newstdout);
  540.       CloseHandle(read_stdout);
  541.       return 0;
  542.     }
  543.     char szBuf[1024];
  544.     DWORD dwRead = 1;
  545.     DWORD dwExit = !STILL_ACTIVE;
  546.     if (WaitForSingleObject(pi.hProcess,10000)!=WAIT_OBJECT_0) {
  547.       return 0;
  548.     }
  549.     ReadFile(read_stdout, szBuf, sizeof(szBuf)-1, &dwRead, NULL);
  550.     szBuf[dwRead] = 0;
  551.     if (lstrlen(szBuf)==0) return 0;
  552.     g_sdata.branding = (char *)GlobalAlloc(GPTR,lstrlen(szBuf)+6);
  553.     wsprintf(g_sdata.branding,"NSIS %s",szBuf);
  554.     g_sdata.brandingv = (char *)GlobalAlloc(GPTR,lstrlen(szBuf)+1);
  555.     lstrcpy(g_sdata.brandingv,szBuf);
  556.     GlobalFree(s);
  557.   }
  558.   return 1;
  559. }
  560.  
  561. void InitTooltips(HWND h) {
  562.   if (h == NULL)  return;
  563.   my_memset(&g_tip,0,sizeof(NTOOLTIP));
  564.   g_tip.tip_p = h;
  565.   INITCOMMONCONTROLSEX icx;
  566.   icx.dwSize  = sizeof(icx);
  567.   icx.dwICC  = ICC_BAR_CLASSES;
  568.   InitCommonControlsEx(&icx);
  569.   DWORD dwStyle = WS_POPUP | WS_BORDER | TTS_ALWAYSTIP;
  570.   DWORD dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
  571.   g_tip.tip = CreateWindowEx(dwExStyle,TOOLTIPS_CLASS,NULL,dwStyle,0,0,0,0,h,NULL,GetModuleHandle(NULL),NULL);
  572.   if (!g_tip.tip) return;
  573.   g_tip.hook = SetWindowsHookEx(WH_GETMESSAGE,TipHookProc,NULL, GetCurrentThreadId());
  574.   AddTip(GetDlgItem(h,IDC_CLOSE),TEXT("Close MakeNSISW"));
  575.   AddTip(GetDlgItem(h,IDC_TEST),TEXT("Test the installer generated by MakeNSISW"));
  576.   AddToolBarTooltips();
  577. }
  578.  
  579. void DestroyTooltips() {
  580.   UnhookWindowsHookEx(g_tip.hook);
  581. }
  582.  
  583. void AddTip(HWND hWnd,LPSTR lpszToolTip) {
  584.   TOOLINFO ti;
  585.   ti.cbSize = sizeof(TOOLINFO);
  586.   ti.uFlags = TTF_IDISHWND;
  587.   ti.hwnd   = g_tip.tip_p;
  588.   ti.uId = (UINT) hWnd;
  589.   ti.lpszText = lpszToolTip;
  590.   SendMessage(g_tip.tip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti); 
  591. }
  592.  
  593. LRESULT CALLBACK TipHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
  594.   if (nCode < 0) return CallNextHookEx(g_tip.hook, nCode, wParam, lParam); 
  595.   switch (((MSG*)lParam)->message) { 
  596.     case WM_MOUSEMOVE:
  597.       if (IsChild(g_tip.tip_p,((MSG*)lParam)->hwnd)) 
  598.         SendMessage(g_tip.tip, TTM_RELAYEVENT, 0,lParam); 
  599.       break; 
  600.     default: 
  601.       break; 
  602.   } 
  603.   return CallNextHookEx(g_tip.hook, nCode, wParam, lParam); 
  604. }
  605.  
  606. void ShowDocs() {
  607.   char pathf[MAX_PATH],*path;
  608.   GetModuleFileName(NULL,pathf,sizeof(pathf));
  609.   path=my_strrchr(pathf,'\\');
  610.   if(path!=NULL) *path=0;
  611.   lstrcat(pathf,LOCALDOCS);
  612.   if ((int)ShellExecute(g_sdata.hwnd,"open",pathf,NULL,NULL,SW_SHOWNORMAL)<=32) 
  613.   ShellExecute(g_sdata.hwnd,"open",DOCPATH,NULL,NULL,SW_SHOWNORMAL);
  614. }
  615.  
  616. char* BuildSymbols()
  617. {
  618.   char *buf = NULL;
  619.  
  620.   if(g_sdata.symbols) {
  621.     int i=0;
  622.     while(g_sdata.symbols[i]) {
  623.       if(buf) {
  624.         char *buf3 = (char *)GlobalAlloc(GPTR,(lstrlen(buf)+lstrlen(g_sdata.symbols[i])+6)*sizeof(char));
  625.         wsprintf(buf3,"%s \"/D%s\"",buf,g_sdata.symbols[i]);
  626.         GlobalFree(buf);
  627.         buf = buf3;
  628.       }
  629.       else {
  630.         buf = (char *)GlobalAlloc(GPTR,(lstrlen(g_sdata.symbols[i])+5)*sizeof(char));
  631.         wsprintf(buf,"\"/D%s\"",g_sdata.symbols[i]);
  632.       }
  633.       i++;
  634.     }
  635.   }
  636.   else {
  637.     buf = (char *)GlobalAlloc(GPTR, sizeof(char));
  638.     buf[0] = NULL;
  639.   }
  640.  
  641.   return buf;
  642. }
  643.  
  644. BOOL PopMRUFile(char* fname)
  645. {
  646.   int i;
  647.  
  648.   for(i=0; i<MRU_LIST_SIZE; i++) {
  649.     if(!lstrcmpi(g_mru_list[i], fname)) {
  650.       break;
  651.     }
  652.   }
  653.  
  654.   if(i < MRU_LIST_SIZE) {
  655.     int j;
  656.     for(j = i; j < MRU_LIST_SIZE-1; j++) {
  657.       lstrcpy(g_mru_list[j],g_mru_list[j+1]);
  658.     }
  659.     g_mru_list[MRU_LIST_SIZE-1][0]='\0';
  660.     return TRUE;
  661.   }
  662.   else {
  663.     return FALSE;
  664.   }
  665. }
  666.  
  667. BOOL IsValidFile(char *fname)
  668. {
  669.   WIN32_FIND_DATA wfd;
  670.   HANDLE h;
  671.  
  672.   h = FindFirstFile(fname,&wfd);
  673.   if(h != INVALID_HANDLE_VALUE) {
  674.     FindClose(h);
  675.     return true;
  676.   }
  677.   return false;
  678. }
  679.  
  680. void PushMRUFile(char* fname)
  681. {
  682.   int i;
  683.   char buf[MAX_PATH+1];
  684.   DWORD   rv;
  685.   char*  file_part;
  686.   char full_file_name[MAX_PATH+1];
  687.  
  688.   if(!fname || fname[0] == '\0' || fname[0] == '/' || fname[0] == '-') {
  689.     return;
  690.   }
  691.  
  692.   if(fname[0] == '"') {
  693.     fname++;
  694.   }
  695.  
  696.   lstrcpy(buf,fname);
  697.   if(buf[lstrlen(buf)-1] == '"') {
  698.     buf[lstrlen(buf)-1] = '\0';
  699.   }
  700.   my_memset(full_file_name,0,sizeof(full_file_name));
  701.   rv = GetFullPathName(buf,sizeof(full_file_name),full_file_name,&file_part);
  702.   if (rv == 0) {
  703.     return;
  704.   }
  705.  
  706.   if(IsValidFile(full_file_name)) {
  707.     PopMRUFile(full_file_name);
  708.     for(i = MRU_LIST_SIZE - 2; i >= 0; i--) {
  709.       lstrcpy(g_mru_list[i+1], g_mru_list[i]);
  710.     }
  711.     lstrcpy(g_mru_list[0],full_file_name);
  712.     BuildMRUMenus();
  713.   }
  714. }
  715.  
  716. void BuildMRUMenus()
  717. {
  718.   HMENU hMenu = g_sdata.fileSubmenu;
  719.   int i;
  720.   MENUITEMINFO mii;
  721.   char buf[MRU_DISPLAY_LENGTH+1];
  722.   char buf2[MRU_DISPLAY_LENGTH - 6];
  723.   int n;
  724.  
  725.   for(i = 0; i < MRU_LIST_SIZE; i++) {
  726.     DeleteMenu(hMenu, IDM_MRU_FILE+i, MF_BYCOMMAND);
  727.   }
  728.  
  729.   n = GetMenuItemCount(hMenu);
  730.  
  731.   for(i = 0; i < MRU_LIST_SIZE; i++) {
  732.     if(g_mru_list[i][0]) {
  733.       my_memset(buf,0,sizeof(buf));
  734.       my_memset(&mii, 0, sizeof(mii));
  735.       mii.cbSize = sizeof(mii);
  736.       mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  737.       mii.wID = IDM_MRU_FILE+i;
  738.       mii.fType = MFT_STRING;
  739.       if(lstrlen(g_mru_list[i]) > MRU_DISPLAY_LENGTH) {
  740.         char *p = my_strrchr(g_mru_list[i],'\\');
  741.         if(p) {
  742.           p++;
  743.           if(lstrlen(p) > MRU_DISPLAY_LENGTH - 7) {
  744.             my_memset(buf2,0,sizeof(buf2));
  745.             lstrcpyn(buf2,p,MRU_DISPLAY_LENGTH - 9);
  746.             lstrcat(buf2,"...");
  747.  
  748.             lstrcpyn(buf,g_mru_list[i],4);
  749.             lstrcat(buf,"...\\");
  750.             lstrcat(buf,buf2);
  751.           }
  752.           else {
  753.             lstrcpyn(buf,g_mru_list[i],(MRU_DISPLAY_LENGTH - lstrlen(p) - 3));
  754.             lstrcat(buf,"...\\");
  755.             lstrcat(buf,p);
  756.           }
  757.         }
  758.         else {
  759.           lstrcpyn(buf,g_mru_list[i],(MRU_DISPLAY_LENGTH-2));
  760.           lstrcat(buf,"...");
  761.         }
  762.       }
  763.       else {
  764.         lstrcpy(buf, g_mru_list[i]);
  765.       }
  766.  
  767.       mii.dwTypeData = buf;
  768.       mii.cch = lstrlen(buf)+1;
  769.       mii.fState = MFS_ENABLED;
  770.       InsertMenuItem(hMenu, n++, TRUE, &mii);
  771.  
  772.     }
  773.     else {
  774.       break;
  775.     }
  776.   }
  777.  
  778.   hMenu = g_sdata.toolsSubmenu;
  779.   my_memset(&mii, 0, sizeof(mii));
  780.   mii.cbSize = sizeof(mii);
  781.   mii.fMask = MIIM_STATE;
  782.  
  783.   if(g_mru_list[0][0]) {
  784.     mii.fState = MFS_ENABLED;
  785.   }
  786.   else {
  787.     mii.fState = MFS_GRAYED;
  788.   }
  789.  
  790.   SetMenuItemInfo(hMenu, IDM_CLEAR_MRU_LIST,FALSE,&mii);
  791. }
  792.  
  793. void LoadMRUFile(int position)
  794. {
  795.   if (!g_sdata.thread && position >=0 && position < MRU_LIST_SIZE && g_mru_list[position][0]) {
  796.     g_sdata.script = (char *)GlobalAlloc(GPTR,lstrlen(g_mru_list[position])+3);
  797.     wsprintf(g_sdata.script,"\"%s\"",g_mru_list[position]);
  798.     if(IsValidFile(g_mru_list[position])) {
  799.       PushMRUFile(g_mru_list[position]);
  800.     }
  801.     else {
  802.       PopMRUFile(g_mru_list[position]);
  803.       BuildMRUMenus();
  804.     }
  805.     ResetObjects();
  806.     CompileNSISScript();
  807.   }
  808. }
  809.  
  810. void RestoreMRUList()
  811. {
  812.   HKEY hKey;
  813.   HKEY hSubKey;
  814.   int n = 0;
  815.   int i;
  816.   if (RegOpenKeyEx(REGSEC,REGKEY,0,KEY_READ,&hKey) == ERROR_SUCCESS) {
  817.     if (RegCreateKey(hKey,REGMRUSUBKEY,&hSubKey) == ERROR_SUCCESS) {
  818.       char buf[8];
  819.       DWORD l;
  820.       for(int i=0; i<MRU_LIST_SIZE; i++) {
  821.         wsprintf(buf,"%d",i);
  822.         l = sizeof(g_mru_list[n]);
  823.         RegQueryValueEx(hSubKey,buf,NULL,NULL,(unsigned char*)g_mru_list[n],&l);
  824.         if(g_mru_list[n][0] != '\0') {
  825.           n++;
  826.         }
  827.       }
  828.       RegCloseKey(hSubKey);
  829.     }
  830.     RegCloseKey(hKey);
  831.   }
  832.   for(i = n; i < MRU_LIST_SIZE; i++) {
  833.     g_mru_list[i][0] = '\0';
  834.   }
  835.  
  836.   BuildMRUMenus();
  837. }
  838.  
  839. void SaveMRUList()
  840. {
  841.   HKEY hKey;
  842.   HKEY hSubKey;
  843.   int i = 0;
  844.   if (RegCreateKey(REGSEC,REGKEY,&hKey) == ERROR_SUCCESS) {
  845.     if (RegCreateKey(hKey,REGMRUSUBKEY,&hSubKey) == ERROR_SUCCESS) {
  846.       char buf[8];
  847.       for(i = 0; i < MRU_LIST_SIZE; i++) {
  848.         wsprintf(buf,"%d",i);
  849.         RegSetValueEx(hSubKey,buf,0,REG_SZ,(const unsigned char *)g_mru_list[i],lstrlen(g_mru_list[i]));
  850.       }
  851.       RegCloseKey(hSubKey);
  852.     }
  853.     RegCloseKey(hKey);
  854.   }
  855. }
  856.  
  857. void ClearMRUList()
  858. {
  859.   int i;
  860.   for(i=0; i<MRU_LIST_SIZE; i++) {
  861.     g_mru_list[i][0] = '\0';
  862.   }
  863.  
  864.   BuildMRUMenus();
  865. }
  866.  
  867. void RestoreCompressor()
  868. {
  869.   HKEY hKey;
  870.   NCOMPRESSOR v = COMPRESSOR_SCRIPT;
  871.   if (RegOpenKeyEx(REGSEC,REGKEY,0,KEY_READ,&hKey) == ERROR_SUCCESS) {
  872.     char compressor_name[32];
  873.     DWORD l = sizeof(compressor_name);
  874.     DWORD t;
  875.  
  876.     if (RegQueryValueEx(hKey,REGCOMPRESSOR,NULL,&t,(unsigned char*)compressor_name,&l)==ERROR_SUCCESS) {
  877.       int i;
  878.       for(i=(int)COMPRESSOR_SCRIPT; i<= (int)COMPRESSOR_BEST; i++) {
  879.         if(!lstrcmpi(compressor_names[i],compressor_name)) {
  880.           v = (NCOMPRESSOR)i;
  881.           break;
  882.         }
  883.       }
  884.     }
  885.     RegCloseKey(hKey);
  886.   }
  887.   g_sdata.default_compressor=v;
  888. }
  889.  
  890. void SaveCompressor()
  891. {
  892.   HKEY hKey;
  893.   int n = (int)COMPRESSOR_SCRIPT;
  894.   NCOMPRESSOR v = g_sdata.default_compressor;
  895.  
  896.   if(v >= COMPRESSOR_SCRIPT && v <= COMPRESSOR_BEST) {
  897.     n = (int)v;
  898.   }
  899.  
  900.   if (RegCreateKey(REGSEC,REGKEY,&hKey) == ERROR_SUCCESS) {
  901.     RegSetValueEx(hKey,REGCOMPRESSOR,0,REG_SZ,(unsigned char*)compressor_names[n],
  902.                   lstrlen(compressor_names[n]));
  903.     RegCloseKey(hKey);
  904.   }
  905. }
  906.  
  907. BOOL FileExists(char *fname)
  908. {
  909.   WIN32_FIND_DATA wfd;
  910.   HANDLE h;
  911.  
  912.   h = FindFirstFile(fname,&wfd);
  913.   if(h == INVALID_HANDLE_VALUE) {
  914.     return false;
  915.   }
  916.   else {
  917.     FindClose(h);
  918.     return true;
  919.   }
  920. }
  921.