]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer/include/LyXUtils.nsh
Consider inset strings in simple find/replaceAll (#12049)
[features.git] / development / Win32 / packaging / installer / include / LyXUtils.nsh
1 # This script contains the following functions:
2 #
3 # - LaTeXCheck (check installed LaTeX-system),
4 #    (only used by Small and Complete installer), uses:
5 #    StrPointer
6 #    StrPoint
7 #    RevStrPointer
8 #    RevStrPoint
9 #
10 # - PATHCheck (checks for a path in a semicolon separated path list like the PATH variable), uses:
11 #    StrPointer
12 #    StrPoint
13 #    RevStrPointer
14 #    RevStrPoint
15 #
16 # - un.DelAppPathSub and UnAppPreSuff,
17 #    (delete the folder ~\Documents and Settings\username\Application Data\LyX for all users), uses:
18 #    un.GetParentA
19 #    un.GetUsers
20 #    un.StrPoint
21 #    StrPointer
22 #    StrPoint
23 #    UnAppPreSuff
24 #
25 # - FileCheck (checks if a given file exists)
26 #
27 #--------------------------
28
29 !macro StrPointer FindStr SearchStr Pointer
30  # searches for a string/character (SearchStr) in another string (FindStr)
31  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
32  # if nothing was found or the search is impossible the Pointer is set to -1
33  
34  StrLen $R2 ${SearchStr}
35  StrLen $R4 ${FindStr}
36  StrCpy $R5 0
37  ${if} $R2 == 0
38  ${orif} $R4 == 0
39   Goto NotFound
40  ${endif}
41  IntCmp $R4 $R2 loopA NotFound
42  loopA:
43   StrCpy $R3 ${FindStr} $R2 $R5
44   StrCmp $R3 ${SearchStr} Found
45   IntOp $R5 $R5 + 1
46   IntCmp $R4 $R5 loopA NotFound
47   Goto loopA
48  Found:
49   StrCpy ${Pointer} $R5
50   Goto done
51  NotFound:
52   StrCpy ${Pointer} "-1"
53  done:
54
55 !macroend
56  
57 #--------------------------------
58
59 Function StrPoint
60  !insertmacro StrPointer $String $Search $Pointer
61 FunctionEnd
62
63 #--------------------------------
64
65 !macro RevStrPointer FindStr SearchStr Pointer
66  # searches for a string/character (SearchStr) in another string (FindStr) in reverse order
67  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
68  # if nothing was found or the search is impossible the Pointer is set to +1
69  
70  StrLen $R2 ${SearchStr}
71  StrLen $R4 ${FindStr}
72  ${if} $R2 == 0
73  ${orif} $R4 == 0
74   Goto NotFound
75  ${endif}
76  IntCmp $R4 $R2 loopA NotFound
77  StrCpy $R5 "-$R2"
78  loopA:
79   StrCpy $R3 ${FindStr} $R2 $R5
80   StrCmp $R3 ${SearchStr} Found
81   IntOp $R5 $R5 - 1
82   IntCmp "$R5" "-$R4" loopA NotFound
83   Goto loopA
84  Found:
85   StrCpy ${Pointer} $R5
86   Goto done
87  NotFound:
88   StrCpy ${Pointer} "+1"
89  done:
90
91 !macroend
92  
93 #--------------------------------
94
95  Function RevStrPoint
96   !insertmacro RevStrPointer $String $Search $Pointer
97  FunctionEnd
98
99 #--------------------------------
100
101 !macro AppPreSuff AppPre AppSuff
102  # the APPDATA path for a local user has for WinXP and 2000 the following structure:
103  # C:\Documents and Settings\username\Application Data
104  # for Win Vista the structure is:
105  # C:\Users\username\AppData\Roaming
106  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
107  # and the "Application Data" substring into the variable "AppSuff"
108   
109   # switch temporarily to local user because the all users application data path is in
110   # Vista only C:\ProgramData 
111   SetShellVarContext current
112   StrCpy $String "$APPDATA"
113   Var /GLOBAL APPDATemp
114   StrCpy $APPDATemp "$APPDATA"
115   ${If} $MultiUser.Privileges == "Admin"
116    ${OrIf} $MultiUser.Privileges == "Power"
117     SetShellVarContext all # move back to all users
118   ${endif}
119   StrCpy $Search "\"
120   Call StrPoint # search for the first "\"
121   IntOp $Pointer $Pointer + 1 # jump after the "\"
122   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
123   StrCpy $0 $Pointer
124   Call StrPoint # search for the second "\"
125   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
126   StrCpy ${AppPre} $APPDATemp $0 # save the part before the second "\"
127   IntOp $Pointer $Pointer + 1 # jump after the "\"
128   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
129   Call StrPoint # search for the third "\"
130   IntOp $Pointer $Pointer + 1 # jump after the "\"
131   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
132
133 !macroend
134
135 #--------------------------------
136
137 Function un.GetParentA
138  # deletes a subfolder of the APPDATA path for all users
139  # used by the function "un.getUsers"
140
141   Exch $R0
142   Push $R1
143   Push $R2
144   Push $R3
145   StrCpy $R1 0
146   StrLen $R2 $R0
147   loop:
148    IntOp $R1 $R1 + 1
149    IntCmp $R1 $R2 get 0 get
150    StrCpy $R3 $R0 1 -$R1
151    StrCmp $R3 "\" get
152   Goto loop
153   get:
154    StrCpy $R0 $R0 -$R1
155    Pop $R3
156    Pop $R2
157    Pop $R1
158    Exch $R0
159    
160 FunctionEnd
161
162 #--------------------------------
163
164 Function un.GetUsers
165  # reads the subfolders of the "Documents and Settings" folder to get a list of the users
166
167   StrCpy $R3 ""
168   Push "$PROFILE"
169   Call un.GetParentA
170   Pop $R2
171   StrCpy $R2 "$R2"
172   FindFirst $R0 $R1 "$R2\*"
173   StrCmp $R1 "" findend 0
174   findloop:
175    IfFileExists "$R2\$R1\*.*" 0 notDir
176    StrCmp $R1 "." notDir
177    StrCmp $R1 ".." notDir
178    StrCmp $R1 "All Users" notDir
179    StrCmp $R1 "Default User" notDir
180    StrCmp $R1 "All Users.WINNT" notDir
181    StrCmp $R1 "Default User.WINNT" notDir  
182   StrCpy $R3 "$R3|$R1"
183   notDir:
184    FindNext $R0 $R1
185    StrCmp $R1 "" findend 0
186   Goto findloop
187   findend:
188    FindClose $R0
189   
190 FunctionEnd
191
192 #--------------------------------
193
194 Function un.StrPoint
195  !insertmacro StrPointer $String $Search $Pointer
196 FunctionEnd
197
198 #--------------------------------
199
200 !macro UnAppPreSuff AppPre AppSuff
201  # the APPDATA path for a local user has for WinXP and 2000 the following structure:
202  # C:\Documents and Settings\username\Application Data
203  # for Win Vista the structure is:
204  # C:\Users\username\AppData\Roaming
205  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
206  # and the "Application Data" substring into the variable "AppSuff"
207   
208   SetShellVarContext current # switch temoprarily to local user
209   StrCpy $String "$APPDATA"
210   StrCpy $APPDATemp "$APPDATA"
211   ${if} $MultiUser.Privileges == "Admin"
212   ${orif} $MultiUser.Privileges == "Power"
213    SetShellVarContext all # move back to all users
214   ${endif}
215   StrCpy $Search "\"
216   Call un.StrPoint # search for the first "\"
217   IntOp $Pointer $Pointer + 1 # jump after the "\"
218   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
219   StrCpy $0 $Pointer
220   Call un.StrPoint # search for the second "\"
221   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
222   StrCpy ${AppPre} $APPDATemp $0 # save the part before the second "\"
223   IntOp $Pointer $Pointer + 1 # jump after the "\"
224   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
225   Call un.StrPoint # search for the third "\"
226   IntOp $Pointer $Pointer + 1 # jump after the "\"
227   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
228
229 !macroend
230
231 #--------------------------------
232
233 Function un.DelAppPathSub
234  # deletes a subfolder of the APPDATA path for all users
235
236   # get list of all users
237   Push $R0
238   Push $R1
239   Push $R2
240   Push $R3
241   Call un.GetUsers
242   StrCpy $UserList $R3 "" 1 # cut off the "|" at the end of the list
243   Pop $R3
244   Pop $R2
245   Pop $R1
246   Pop $R0
247   
248   # the usernames in the list of all users is separated by "|"
249   loop:
250    StrCpy $String "$UserList"
251    StrCpy $Search "|"
252    Call un.StrPoint # search for the "|"
253    StrCmp $Pointer "-1" ready
254    StrCpy $0 $UserList $Pointer # $0 contains now the username
255    IntOp $Pointer $Pointer + 1 # jump after the "|"
256    StrCpy $UserList $UserList "" $Pointer # cut off the first username in the list
257    # generate the string for the current user
258    # AppPre and AppSuff are generated in the macro "AppPreSuff"
259    RMDir /r "$AppPre\$0\$AppSuff\$AppSubfolder" # delete the folder
260   Goto loop
261   ready:
262   StrCpy $0 $UserList
263   RMDir /r "$AppPre\$0\$AppSuff\$AppSubfolder" # delete the folder
264   
265 FunctionEnd
266
267 #--------------------------------
268
269 !macro FileCheck Result FileName FilePath
270  # checks if a file exists, returns "True" or "False"
271
272  Push $0
273  Push $1
274  StrCpy $0 ""
275  StrCpy $1 ""
276  FileOpen $0 "${FilePath}\${FileName}" r
277  ${if} $0 = ""
278   StrCpy $1 "False"
279  ${Else}
280   StrCpy $1 "True"
281  ${endif}
282  FileClose $0
283  StrCpy ${Result} $1
284  Pop $1
285  Pop $0
286
287 !macroend
288
289 #------------------------------------------
290
291 Function LaTeXCheck
292  # searches the string "$Search" in the string "$String" and extracts the path around it
293  # it is checked if the file "latex.exe" exists in the extracted path
294
295    StartCheck:
296    StrLen $3 $String
297    Call StrPoint
298    ${if} $Pointer == "-1" # if nothing was found
299     StrCpy $PathLaTeX ""
300     Return
301    ${endif}
302    IntOp $3 $3 - $Pointer
303    StrCpy $4 $String $3 "-$3" # $4 is now the part behind the $Search string
304    StrCpy $String $String $Pointer # $String is now the part before the $Search string
305    StrCpy $Search ":" # search for the ":" after the first previous drive letter
306    Call RevStrPoint
307    IntOp $Pointer $Pointer - 1 # jump before the ":" to the drive letter
308    StrCpy $Pointer $Pointer "" 1 # cut of the "-" sign
309    StrCpy $PathLaTeX $String $Pointer "-$Pointer"
310    StrCpy $String $4
311    StrCpy $Search ";" # search for the following ";" that separates the different paths
312    Call StrPoint
313    ${if} $Pointer != "-1" # if something was found
314     StrCpy $String $String $Pointer
315    ${endif}
316    StrCpy $PathLaTeX "$PathLaTeX$String"
317    # check if the latex.exe exists in the $PathLaTeX folder
318    !insertmacro FileCheck $5 "latex.exe" "$PathLaTeX"
319    ${if} $5 == "False" # delete the entry with the wrong path to the latex.exe and try again
320     StrCpy $PathLaTeX ""
321     StrLen $3 $String
322     StrCpy $String $4 "" $3
323     ${if} $2 == "TeXLive"
324      StrCpy $Search "TeXLive"
325     ${else}
326      StrCpy $Search "miktex"
327     ${endif}
328     Goto StartCheck
329    ${endif}
330
331 FunctionEnd
332
333 #------------------------------------------
334
335 !macro PATHCheck PathResult FileName
336  # searches the string "$Search" in the string "$String" and extracts the path around it
337  # it is checked if the given filename exists
338  
339    !define ID ${__LINE__}
340    StrLen $3 $String
341    Call StrPoint
342    ${if} $Pointer == "-1" # if nothing was found
343     StrCpy ${PathResult} "False"
344     Goto EndPATHCheck_${ID}
345    ${endif}
346    IntOp $3 $3 - $Pointer
347    StrCpy $4 $String $3 "-$3" # $4 is now the part behind the $Search string
348    StrCpy $String $String $Pointer # $String is now the part before the $Search string
349    StrCpy $Search ":" # search for the ":" after the first previous drive letter
350    Call RevStrPoint
351    IntOp $Pointer $Pointer - 1 # jump before the ":" to the drive letter
352    StrCpy $Pointer $Pointer "" 1 # cut of the "-" sign
353    StrCpy ${PathResult} $String $Pointer "-$Pointer"
354    StrCpy $String $4
355    StrCpy $Search ";" # search for the following ";" that separates the different paths
356    Call StrPoint
357    ${if} $Pointer != "-1" # if something was found
358     StrCpy $String $String $Pointer
359    ${endif}
360    StrCpy ${PathResult} "${PathResult}$String"
361    # check if the FileName exists in the ${Result} folder
362    !insertmacro FileCheck $Tmp ${FileName} ${PathResult}
363    ${if} $Tmp == "False"
364     StrCpy ${PathResult} "False"
365    ${endif}
366    EndPATHCheck_${ID}:
367    !undef ID
368
369 !macroend