diff --git a/Docs/src/defines.but b/Docs/src/defines.but index 747d3bbe..b16d989d 100644 --- a/Docs/src/defines.but +++ b/Docs/src/defines.but @@ -10,15 +10,19 @@ Define/conditional compilation related commands: \S1{define} !define -\c [/date] gflag [value] +\c ([/date] gflag [value]) | (/math gflag val1 OP val2) -This command will add 'gflag' to the global define list. This will have a similar effect as using the /D switch on the command line (only the define only becomes effective after the !define command). +This command will add \e{gflag} to the global define list. This will have a similar effect as using the /D switch on the command line (only the define only becomes effective after the !define command). If \e{/date} is used, \e{value} will be passed into strftime and the result will be used as the value of \e{gflag}. strftime converts special symbols into certain parts of the current time or date. For example, %H will be converted into the current hour in 24-hour format. For a complete list of available symbols, search for strftime on \W{http://msdn.microsoft.com/}{MSDN}. On POSIX, you can get the list by using \c{man strftime}. +If \e{/math} is used, the result of 'val1 OP val2', where OP may be +,-,*,/ or % , will be used as the value of \e{gflag}. Note that val1 AND val2 MUST be integer values! + \c !define USE_SOMETHING \c !define VERSION 1.2 \c !define /date NOW "%H:%M:%S %d %b, %Y" +\c !define /math RESULT 3 + 10 +\c !define /math REST 15 % ${RESULT} \S1{undef} !undef diff --git a/Source/Tests/preprocessor.nsi b/Source/Tests/preprocessor.nsi index d1fb0349..1446143a 100644 --- a/Source/Tests/preprocessor.nsi +++ b/Source/Tests/preprocessor.nsi @@ -84,6 +84,46 @@ f !error "!if 'test' == 'test' is true!" !endif +; testing of two math functions and a macro hack :) +!define increase "!insertmacro increase" +!macro increase DEFINE + !define /math ${DEFINE}_MACROTEMP ${${DEFINE}} + 1 + !undef ${DEFINE} + !define ${DEFINE} ${${DEFINE}_MACROTEMP} + !undef ${DEFINE}_MACROTEMP +!macroend + +!define number1 1 # 1 +!define /math number2 2 + 3 +!define /math number3 ${number2} - ${number1} +${increase} number3 +!define /math number4 2 * ${number3} +!define /math number5 ${number4} % 3 + +!if ${number1} != 1 + !error "number1 != 1" +!endif + +!if ${number2} != 5 + !error "number1 != 5" +!endif + +!if ${number3} != 5 + !error "number1 != 5" +!endif + +!if ${number4} != 10 + !error "number1 != 10" +!endif + +!if ${number5} != 1 + !error "number1 != 1" +!endif + +!undef number1 +!undef number2 +!undef number3 +; end math functions # this should just give a warning, not an error !include /NONFATAL file_that_doesnt_exist.nsh diff --git a/Source/script.cpp b/Source/script.cpp index a94343f2..385d23c2 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -405,7 +405,7 @@ parse_again: int istrue=0; int mod=0; - + int p=0; if (tkid == TOK_P_IF) { @@ -2698,7 +2698,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) value=line.gettoken_str(3); time_t rawtime; - time(&rawtime); + time(&rawtime); datebuf[0]=0; size_t s=strftime(datebuf,sizeof(datebuf),value,localtime(&rawtime)); @@ -2711,6 +2711,40 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) value=datebuf; date=true; + + } else if (!stricmp(define,"/math")) { + + int value1; + int value2; + char *mathop; + + if (line.getnumtokens()!=6) PRINTHELP() + + define = line.gettoken_str(2); + value1 = line.gettoken_int(3)); + mathop = line.gettoken_str(4); + value2 = line.gettoken_int(5); + + if (!strcmp(mathop,"+")) { + sprintf(value,"%d",value1+value2); + } else if (!strcmp(mathop,"-")) { + sprintf(value,"%d",value1-value2); + } else if (!strcmp(mathop,"*")) { + sprintf(value,"%d",value1*value2); + } else if (!strcmp(mathop,"%")) { + if (value2==0) { + ERROR_MSG("!define /math: division by zero! (\"%i / %i\")\n",value1,value2); + return PS_ERROR; + } + sprintf(value,"%d",value1/value2); + } else if (!strcmp(mathop,"%")) { + if (value2==0) { + ERROR_MSG("!define /math: division by zero! (\"%i %% %i\")\n",value1,value2); + return PS_ERROR; + } + sprintf(value,"%d",value1%value2); + } else PRINTHELP() + } else { if (line.getnumtokens()==4) PRINTHELP() diff --git a/Source/tokens.cpp b/Source/tokens.cpp index d9805ce3..7bdae21e 100644 --- a/Source/tokens.cpp +++ b/Source/tokens.cpp @@ -229,7 +229,7 @@ static tokenType tokenlist[TOK__LAST] = {TOK_P_IFDEF,"!ifdef",1,-1,"symbol [| symbol2 [& symbol3 [...]]]",TP_ALL}, {TOK_P_IFNDEF,"!ifndef",1,-1,"symbol [| symbol2 [& symbol3 [...]]]",TP_ALL}, {TOK_P_ENDIF,"!endif",0,0,"",TP_ALL}, -{TOK_P_DEFINE,"!define",1,2,"[/date] symbol [value]",TP_ALL}, +{TOK_P_DEFINE,"!define",1,4,"([/date] symbol [value]) | (/math symbol val1 OP val2)\n OP=(+ - * / %)",TP_ALL}, {TOK_P_UNDEF,"!undef",1,1,"symbol [value]",TP_ALL}, {TOK_P_ELSE,"!else",0,-1,"[if[macro][n][def] ...]",TP_ALL}, {TOK_P_ECHO,"!echo",1,0,"message",TP_ALL},