Updated installer to get rid of "parameters.nsh", now using StdUtils plug-in to get...
[LameXP.git] / etc / NSIS / parameters.nsh
blobae1060a8f2903225061aa996332ac6d0c7523293
1 !ifndef __PREFIX__
2   !define __PREFIX__ ""
3 !endif
6 ; StrStr
8 Function ${__PREFIX__}StrStr
9 /*After this point:
10   ------------------------------------------
11   $R0 = SubString (input)
12   $R1 = String (input)
13   $R2 = SubStringLen (temp)
14   $R3 = StrLen (temp)
15   $R4 = StartCharPos (temp)
16   $R5 = TempStr (temp)*/
18   ;Get input from user
19   Exch $R0
20   Exch
21   Exch $R1
22   Push $R2
23   Push $R3
24   Push $R4
25   Push $R5
27   ;Get "String" and "SubString" length
28   StrLen $R2 $R0
29   StrLen $R3 $R1
30   ;Start "StartCharPos" counter
31   StrCpy $R4 0
33   ;Loop until "SubString" is found or "String" reaches its end
34   loop:
35     ;Remove everything before and after the searched part ("TempStr")
36     StrCpy $R5 $R1 $R2 $R4
38     ;Compare "TempStr" with "SubString"
39     StrCmp $R5 $R0 done
40     ;If not "SubString", this could be "String"'s end
41     IntCmp $R4 $R3 done 0 done
42     ;If not, continue the loop
43     IntOp $R4 $R4 + 1
44     Goto loop
45   done:
47 /*After this point:
48   ------------------------------------------
49   $R0 = ResultVar (output)*/
51   ;Remove part before "SubString" on "String" (if there has one)
52   StrCpy $R0 $R1 `` $R4
54   ;Return output to user
55   Pop $R5
56   Pop $R4
57   Pop $R3
58   Pop $R2
59   Pop $R1
60   Exch $R0
61 FunctionEnd
64 ; GetParameters
65  ; input, none
66  ; output, top of stack (replaces, with e.g. whatever)
67  ; modifies no other variables.
69 Function ${__PREFIX__}GetParameters
70   Push $R0
71   Push $R1
72   Push $R2
73   Push $R3
75   StrCpy $R2 1
76   StrLen $R3 $CMDLINE
78   ;Check for quote or space
79   StrCpy $R0 $CMDLINE $R2
80   StrCmp $R0 '"' 0 +3
81     StrCpy $R1 '"'
82     Goto loop
83   StrCpy $R1 " "
85   loop:
86     IntOp $R2 $R2 + 1
87     StrCpy $R0 $CMDLINE 1 $R2
88     StrCmp $R0 $R1 get
89     StrCmp $R2 $R3 get
90     Goto loop
92   get:
93     IntOp $R2 $R2 + 1
94     StrCpy $R0 $CMDLINE 1 $R2
95     StrCmp $R0 " " get
96     StrCpy $R0 $CMDLINE "" $R2
98   Pop $R3
99   Pop $R2
100   Pop $R1
101   Exch $R0
102 FunctionEnd
105 ; GetParameterValue
106 ; Chris Morgan<cmorgan@alum.wpi.edu> 5/10/2004
107 ; -Updated 4/7/2005 to add support for retrieving a command line switch
108 ;  and additional documentation
110 ; Searches the command line input, retrieved using GetParameters, for the
111 ; value of an option given the option name.  If no option is found the
112 ; default value is placed on the top of the stack upon function return.
114 ; This function can also be used to detect the existence of just a
115 ; command line switch like /OUTPUT  Pass the default and "OUTPUT"
116 ; on the stack like normal.  An empty return string "" will indicate
117 ; that the switch was found, the default value indicates that
118 ; neither a parameter or switch was found.
120 ; Inputs - Top of stack is default if parameter isn't found,
121 ;  second in stack is parameter to search for, ex. "OUTPUT"
122 ; Outputs - Top of the stack contains the value of this parameter
123 ;  So if the command line contained /OUTPUT=somedirectory, "somedirectory"
124 ;  will be on the top of the stack when this function returns
126 ; Register usage
127 ;$R0 - default return value if the parameter isn't found
128 ;$R1 - input parameter, for example OUTPUT from the above example
129 ;$R2 - the length of the search, this is the search parameter+2
130 ;      as we have '/OUTPUT='
131 ;$R3 - the command line string
132 ;$R4 - result from StrStr calls
133 ;$R5 - search for ' ' or '"'
135 Function ${__PREFIX__}GetParameterValue
136   Exch $R0  ; get the top of the stack(default parameter) into R0
137   Exch      ; exchange the top of the stack(default) with
138             ; the second in the stack(parameter to search for)
139   Exch $R1  ; get the top of the stack(search parameter) into $R1
141   ;Preserve on the stack the registers used in this function
142   Push $R2
143   Push $R3
144   Push $R4
145   Push $R5
147   Strlen $R2 $R1+2    ; store the length of the search string into R2
149   Call ${__PREFIX__}GetParameters  ; get the command line parameters
150   Pop $R3                          ; store the command line string in R3
152   # search for quoted search string
153   StrCpy $R5 '"'      ; later on we want to search for a open quote
154   Push $R3            ; push the 'search in' string onto the stack
155   Push '"/$R1='       ; push the 'search for'
156   Call ${__PREFIX__}StrStr         ; search for the quoted parameter value
157   Pop $R4
158   StrCpy $R4 $R4 "" 1   ; skip over open quote character, "" means no maxlen
159   StrCmp $R4 "" "" next ; if we didn't find an empty string go to next
161   # search for non-quoted search string
162   StrCpy $R5 ' '             ; later on we want to search for a space since we
163                              ; didn't start with an open quote '"' we shouldn't
164                              ; look for a close quote '"'
165   Push $R3                   ; push the command line back on the stack for searching
166   Push '/$R1='               ; search for the non-quoted search string
167   Call ${__PREFIX__}StrStr
168   Pop $R4
170   ; $R4 now contains the parameter string starting at the search string,
171   ; if it was found
172 next:
173   StrCmp $R4 "" check_for_switch ; if we didn't find anything then look for
174                                  ; usage as a command line switch
175   # copy the value after /$R1= by using StrCpy with an offset of $R2,
176   # the length of '/OUTPUT='
177   StrCpy $R0 $R4 "" $R2  ; copy commandline text beyond parameter into $R0
178   # search for the next parameter so we can trim this extra text off
179   Push $R0
180   Push $R5                    ; search for either the first space ' ', or the first
181                               ; quote '"'
182                               ; if we found '"/output' then we want to find the
183                               ; ending ", as in '"/output=somevalue"'
184                               ; if we found '/output' then we want to find the first
185                               ; space after '/output=somevalue'
186   Call ${__PREFIX__}StrStr    ; search for the next parameter
187   Pop $R4
188   StrCmp $R4 "" done  ; if 'somevalue' is missing, we are done
189   StrLen $R4 $R4      ; get the length of 'somevalue' so we can copy this
190                       ; text into our output buffer
191   StrCpy $R0 $R0 -$R4 ; using the length of the string beyond the value,
192                       ; copy only the value into $R0
193   goto done           ; if we are in the parameter retrieval path skip over
194                       ; the check for a command line switch
196 ; See if the parameter was specified as a command line switch, like '/output'
197 check_for_switch:
198   Push $R3                  ; push the command line back on the stack for searching
199   Push '/$R1'               ; search for the non-quoted search string
200   Call ${__PREFIX__}StrStr
201   Pop $R4
202   StrCmp $R4 "" done        ; if we didn't find anything then use the default
203   StrCpy $R0 ""             ; otherwise copy in an empty string since we found the
204                             ; parameter, just didn't find a value
206 done:
207   Pop $R5
208   Pop $R4
209   Pop $R3
210   Pop $R2
211   Pop $R1
212   Exch $R0 ; put the value in $R0 at the top of the stack
213 FunctionEnd
216 ; Wrapper macros
218 !if "${__PREFIX__}" != "un."
219   !macro GetCommandlineParameter param default var
220     Push "${param}"         ; push the search string onto the stack
221     Push "${default}"       ; push a default value onto the stack
222     Call GetParameterValue
223     Pop ${var}
224   !macroend
225 !else
226   !macro un.GetCommandlineParameter param default var
227     Push "${param}"         ; push the search string onto the stack
228     Push "${default}"       ; push a default value onto the stack
229     Call un.GetParameterValue
230     Pop ${var}
231   !macroend
232 !endif
235 !undef __PREFIX__