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

  1. const char *NSIS_VERSION="v2.0";
  2.  
  3. /* 
  4.   Nullsoft Scriptable Install System (NSIS)
  5.   makensis.cpp - installer compiler code
  6.  
  7.   Copyright (C) 1999-2004 Nullsoft, Inc.
  8.  
  9.   This software is provided 'as-is', without any express or implied
  10.   warranty.  In no event will the authors be held liable for any damages
  11.   arising from the use of this software.
  12.  
  13.   Permission is granted to anyone to use this software for any purpose,
  14.   including commercial applications, and to alter it and redistribute it
  15.   freely, subject to the following restrictions:
  16.  
  17.   1. The origin of this software must not be misrepresented; you must not
  18.      claim that you wrote the original software. If you use this software
  19.      in a product, an acknowledgment in the product documentation would be
  20.      appreciated but is not required.
  21.   2. Altered source versions must be plainly marked as such, and must not be
  22.      misrepresented as being the original software.
  23.   3. This notice may not be removed or altered from any source distribution.
  24.  
  25.   This is the zlib/libpng license, which is approved by opensource.org.
  26.  
  27.   Portions Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler (zlib).
  28.   Includes portions derived from bzip2 (see documentation for details).
  29. */
  30.  
  31. #include "Platform.h"
  32. #include <stdio.h>
  33. #include <signal.h>
  34.  
  35. #include "build.h"
  36. #include "util.h"
  37. #include "exedata.h"
  38.  
  39.  
  40. int g_noconfig=0;
  41. int g_display_errors=1;
  42. FILE *g_output=stdout;
  43.  
  44. void quit()
  45. {
  46.   if (g_display_errors) 
  47.   {
  48.     fprintf(g_output,"\nNote: you may have one or two (large) stale temporary file(s)\n"
  49.          "left in your temporary directory (Generally this only happens on Windows 9x).\n");
  50.     fflush(g_output);
  51.   }
  52.   exit(1);
  53. }
  54.  
  55. static void myatexit()
  56. {
  57.   dopause();
  58.   if (g_output != stdout && g_output) fclose(g_output);
  59. }
  60.  
  61. static void sigint(int sig)
  62. {
  63.   if (g_display_errors) 
  64.   {
  65.     fprintf(g_output,"\n\nAborting on Ctrl+C...\n");
  66.     fflush(g_output);
  67.   }
  68.   quit();
  69. }
  70.  
  71. int main(int argc, char **argv)
  72. {
  73.   CEXEBuild build;
  74.   int do_cd=1;
  75.   int outputtried=0;
  76.   int argpos=1;
  77.   int nousage=0;
  78.   int files_processed=0;
  79.   int cmds_processed=0;
  80.   int plugins_processed=0;
  81.   FILE *fp;
  82.   int tmpargpos=1;
  83.   int no_logo=0;
  84.  
  85.   if (argc > 1 && !stricmp(argv[1], "/VERSION"))
  86.   {
  87.     fprintf(g_output,NSIS_VERSION);
  88.     fflush(g_output);
  89.     return 0;
  90.   }
  91.   if (argc > 1 && argv[1][0]=='/' && (argv[1][1]=='v' || argv[1][1]=='V'))
  92.   {
  93.     tmpargpos++;
  94.     if (argv[1][2] <= '2' && argv[1][2] >= '0')
  95.     {
  96.       no_logo=1;
  97.     }
  98.   }
  99.   
  100.   if (!no_logo)
  101.   {
  102.     if (argc > tmpargpos && argv[tmpargpos][0]=='/' && (argv[tmpargpos][1]=='o' || argv[tmpargpos][1]=='O') && argv[tmpargpos][2])
  103.     {
  104.       g_output=fopen(argv[tmpargpos]+2,"w");
  105.       if (!g_output) 
  106.       {
  107.         printf("Error opening output log for writing. Using stdout.\n");
  108.         g_output=stdout;
  109.       }
  110.       outputtried=1;
  111.     }
  112.     fprintf(g_output,"MakeNSIS %s - Copyright 1999-2004 Nullsoft, Inc.\n"
  113.            "\n"
  114.            "Portions Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler (zlib).\n"
  115.            "Includes portions derived from bzip2 (see documentation for details).\n"
  116.            "Portions Copyright (C) 1999-2003 Igor Pavlov (lzma).\n"
  117.            "Contributors: nnop@newmail.ru, Ryan Geiss, Andras Varga, Drew Davidson, Peter Windridge, Dave Laundon, Robert Rainwater, Yaroslav Faybishenko, Jeff Doozan, Amir Szekely, Ximon Eighteen, et al.\n\n",NSIS_VERSION);
  118.     fflush(g_output);
  119.   }
  120.  
  121.   atexit(myatexit);
  122.   signal(SIGINT,sigint);
  123.  
  124.   if (!g_output) g_output=stdout;
  125.   while (argpos < argc)
  126.   {
  127.     if (argv[argpos][0]=='/' && (argv[argpos][1]=='D' || argv[argpos][1]=='d') && argv[argpos][2])
  128.     {
  129.       char *p=argv[argpos]+2;
  130.       if (p[0])
  131.       {
  132.         char *s=strdup(p),*v;
  133.         if (build.display_script) 
  134.         {
  135.           fprintf(g_output,"Command line defined: \"%s\"\n",p);
  136.           fflush(g_output);
  137.         }
  138.         v=strstr(s,"=");
  139.         if (v) *v++=0;
  140.         build.define(s,v?v:"");
  141.         free(s);
  142.       }
  143.       else
  144.       {
  145.         build.warning("command line /D requires argument (i.e. \"/Ddefine\"). ignored.");
  146.       }
  147.     }
  148.     else if (argv[argpos][0]=='/' && (argv[argpos][1]=='X' || argv[argpos][1]=='x') && argv[argpos][2])
  149.     {
  150.       if (build.process_oneline(argv[argpos]+2,"command line",argpos+1) != PS_OK)
  151.       {
  152.         return 1;
  153.       }
  154.       cmds_processed++;
  155.     }
  156.     else if (argv[argpos][0]=='/' && (argv[argpos][1]=='O' || argv[argpos][1]=='o') && argv[argpos][2])
  157.     {
  158.       if (!outputtried)
  159.       {
  160.         g_output=fopen(argv[argpos]+2,"w");
  161.         if (!g_output) 
  162.         {
  163.           if (build.display_errors) printf("Error opening output log for writing. Using stdout.\n");
  164.           g_output=stdout;
  165.         }
  166.         outputtried=1;
  167.       }
  168.     }
  169.     else if (!stricmp(argv[argpos],"/NOCD")) do_cd=0;
  170.     else if (argv[argpos][0] == '/' && (argv[argpos][1] == 'V' || argv[argpos][1] == 'v') && 
  171.              argv[argpos][2] >= '0' && argv[argpos][2] <= '4' && !argv[argpos][3])
  172.     {
  173.       int v=argv[argpos][2]-'0';
  174.       build.display_script=v>3;
  175.       build.display_info=v>2;
  176.       build.display_warnings=v>1;
  177.       build.display_errors=v>0;
  178.       g_display_errors=build.display_errors;
  179.     }
  180.     else if (!stricmp(argv[argpos],"/NOCONFIG")) g_noconfig=1;
  181.     else if (!stricmp(argv[argpos],"/PAUSE")) g_dopause=1;
  182.     else if (!stricmp(argv[argpos],"/LICENSE")) 
  183.     {
  184.       if (build.display_info) 
  185.       {
  186.         fprintf(g_output,"This software is provided 'as-is', without any express or implied warranty.  In\n"
  187.              "no event will the authors be held liable for any damages arising from the use\n"
  188.              "of this software.\n\n"
  189.              "Permission is granted to anyone to use this software for any purpose, including\n"
  190.              "commercial applications, and to alter it and redistribute it freely, subject to\n"
  191.              "the following restrictions:\n"
  192.              "  1. The origin of this software must not be misrepresented; you must not claim\n"
  193.              "     that you wrote the original software. If you use this software in a\n"
  194.              "     product, an acknowledgment in the product documentation would be\n"
  195.              "     appreciated but is not required.\n"
  196.              "  2. Altered source versions must be plainly marked as such, and must not be\n"
  197.              "     misrepresented as being the original software.\n"
  198.              "  3. This notice may not be removed or altered from any source distribution.\n\n");
  199.         fflush(g_output);
  200.       }
  201.       nousage++;
  202.     }
  203.     else if (!stricmp(argv[argpos],"/CMDHELP"))
  204.     {
  205.       if (argpos < argc-1)
  206.         build.print_help(argv[++argpos]);
  207.       else 
  208.         build.print_help(NULL);
  209.       nousage++;
  210.     }
  211.     else if (!stricmp(argv[argpos],"/NOTIFYHWND"))
  212.     {
  213.       build.notify_hwnd=(HWND)atol(argv[++argpos]);
  214.       if (!IsWindow(build.notify_hwnd))
  215.         build.notify_hwnd=0;
  216.     }
  217.     else if (!stricmp(argv[argpos],"/HDRINFO"))
  218.     {
  219.       if (build.display_info) 
  220.       {
  221.         fprintf(g_output,"Size of zlib EXE header is %d bytes.\n",zlib_exeheader_size);
  222.         fprintf(g_output,"Size of bzip2 EXE header is %d bytes.\n",bzip2_exeheader_size);
  223.         fprintf(g_output,"Size of lzma EXE header is %d bytes.\n",lzma_exeheader_size);
  224.         fprintf(g_output,"Size of first header is %d bytes.\n",sizeof(firstheader));
  225.         fprintf(g_output,"Size of main header is %d bytes.\n",sizeof(header));
  226.         fprintf(g_output,"Size of each section is %d bytes.\n",sizeof(section));
  227.         fprintf(g_output,"Size of each page is %d bytes.\n",sizeof(page));
  228.         fprintf(g_output,"Size of each instruction is %d bytes.\n",sizeof(entry));
  229.         int x=build.definedlist.getnum();
  230.         fprintf(g_output,"\nDefined symbols: ");
  231.         for (int i=0; i<x; i++)
  232.         {
  233.           fprintf(g_output,"%s",build.definedlist.getname(i));
  234.           char *p=build.definedlist.getvalue(i);
  235.           if (*p) fprintf(g_output,"=%s",p);
  236.           if (i<x-1) fprintf(g_output,",");
  237.         }
  238.         if (!x) fprintf(g_output,"none");
  239.         fprintf(g_output,"\n");
  240.         fflush(g_output);
  241.       }
  242.       nousage++;
  243.     }
  244.     else 
  245.     {
  246.       if (argv[argpos][0]=='/') break;
  247.       files_processed++;
  248.       if (!strcmp(argv[argpos],"-")) g_dopause=0;
  249.       if (!g_noconfig)
  250.       {
  251.         g_noconfig=1;
  252.         char exepath[1024];
  253.         GetModuleFileName(NULL,exepath,sizeof(exepath)-1);
  254.         //strncpy(exepath,argv[0],1023);
  255.         exepath[1023]=0;
  256.         char *p=exepath;
  257.         while (*p) p++;
  258.         while (p > exepath && *p != '\\') p=CharPrev(exepath,p);
  259.         if (p>exepath) p++;
  260.         strcpy(p,"nsisconf.nsh");
  261.         FILE *cfg=fopen(exepath,"rt");
  262.         if (cfg)
  263.         {
  264.           if (build.display_script) 
  265.           {
  266.             fprintf(g_output,"Processing config: \n");
  267.             fflush(g_output);
  268.           }
  269.           int ret=build.process_script(cfg,exepath);
  270.           fclose(cfg);
  271.           if (ret != PS_OK && ret != PS_EOF)
  272.           {
  273.             if (build.display_errors) 
  274.             {
  275.               fprintf(g_output,"Error in config on line %d -- aborting creation process\n",build.linecnt);
  276.               fflush(g_output);
  277.             }
  278.             return 1;
  279.           }
  280.           if (build.display_script) 
  281.           {
  282.             fprintf(g_output,"\n");
  283.             fflush(g_output);
  284.           }
  285.         }
  286.       }
  287.  
  288.       {
  289.         char sfile[1024];
  290.         if (!strcmp(argv[argpos],"-"))
  291.         {
  292.           fp=stdin;
  293.           strcpy(sfile,"stdin");
  294.         }
  295.         else
  296.         {
  297.           strcpy(sfile,argv[argpos]);
  298.           fp=fopen(sfile,"rt");
  299.           if (!fp)
  300.           {
  301.             sprintf(sfile,"%s.nsi",argv[argpos]);
  302.             fp=fopen(sfile,"rt");
  303.             if (!fp)
  304.             {
  305.               if (build.display_errors) 
  306.               {
  307.                 sfile[strlen(sfile)-4]=0;
  308.                 fprintf(g_output,"Can't open script \"%s\"\n",sfile);
  309.                 fflush(g_output);
  310.               }
  311.               return 1;
  312.             }
  313.           }
  314.           if (do_cd)
  315.           {
  316.             char dirbuf[1024],*p;
  317.             GetFullPathName(sfile,sizeof(dirbuf),dirbuf,&p);
  318.             p=dirbuf;
  319.             while (*p) p++;
  320.             while (p > dirbuf && *p != '\\') p=CharPrev(dirbuf,p);
  321.             *p=0;
  322.             if (dirbuf[0]) 
  323.             {
  324.               if (build.display_script) 
  325.               {
  326.                 fprintf(g_output,"Changing directory to: \"%s\"\n",dirbuf);
  327.                 fflush(g_output);
  328.               }
  329.               if (!SetCurrentDirectory(dirbuf))
  330.               {
  331.                 if (build.display_errors)
  332.                 {
  333.                   fprintf(g_output,"Error changing directory to \"%s\"\n",dirbuf);
  334.                   fflush(g_output);
  335.                 }
  336.                 return 1;
  337.               }
  338.               if (build.display_script) 
  339.               {
  340.                 fprintf(g_output,"\n");
  341.                 fflush(g_output);
  342.               }
  343.             }
  344.           }
  345.         }
  346.  
  347.         if (build.display_script) 
  348.         {
  349.           build.notify(MAKENSIS_NOTIFY_SCRIPT,sfile);
  350.           fprintf(g_output,"Processing script file: \"%s\"\n",sfile);
  351.           fflush(g_output);
  352.         }
  353.         int ret=build.process_script(fp,sfile);
  354.         if (fp != stdin) fclose(fp);
  355.  
  356.         if (ret != PS_EOF && ret != PS_OK)
  357.         {
  358.           if (build.display_errors) 
  359.           {
  360.             fprintf(g_output,"Error in script \"%s\" on line %d -- aborting creation process\n",sfile,build.linecnt);
  361.             fflush(g_output);
  362.           }
  363.           return 1;
  364.         }
  365.       }
  366.     }
  367.     argpos++;
  368.   }
  369.  
  370.   if (argpos<argc || (!files_processed && !cmds_processed))
  371.   {
  372.     if (build.display_errors && !nousage)
  373.     {
  374.       fprintf(g_output,"Usage:\n"
  375.              "  makensis [options] [script.nsi | - [...]]\n"
  376.              "   options are:\n"
  377.              "    /CMDHELP item prints out help for 'item', or lists all commands\n"
  378.              "    /HDRINFO prints information about what options makensis was compiled with\n"
  379.              "    /LICENSE prints the makensis software license\n");
  380.       fprintf(g_output,
  381.              "    /Vx verbosity where x is 4=all,3=no script,2=no info,1=no warnings,0=none\n"
  382.              "    /Ofile specifies a text file to log compiler output (default is stdout)\n"
  383.              "    /PAUSE pauses after execution\n"
  384.              "    /NOCONFIG disables inclusion of <path to makensis.exe>\\nsisconf.nsh\n"
  385.              "    /NOCD disabled the current directory change to that of the .nsi file\n"
  386.              "    /Ddefine[=value] defines the symbol \"define\" for the script [to value]\n"
  387.              "    /Xscriptcmd executes scriptcmd in script (i.e. \"/XOutFile poop.exe\")\n"
  388.              "   for script file name, you can use - to read from the standard input\n");
  389.       fflush(g_output);
  390.     }
  391.     return 1;
  392.   }
  393.  
  394.   if (build.display_info) 
  395.   {
  396.     fprintf(g_output,"\nProcessed ");
  397.     if (files_processed) fprintf(g_output,"%d file%s, ",files_processed,files_processed==1?"":"s");
  398.     if (cmds_processed) fprintf(g_output,"%d command line command%s, ",cmds_processed,cmds_processed==1?"":"s");
  399.     fprintf(g_output,"writing output:\n");
  400.     fflush(g_output);
  401.   }
  402.   
  403.   if (build.write_output())
  404.   { 
  405.     if (build.display_errors) 
  406.     {
  407.       fprintf(g_output,"Error - aborting creation process\n");
  408.       fflush(g_output);
  409.     }
  410.     return 1;
  411.   }
  412.   return 0; 
  413. }
  414.