StdUtils.nsh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #################################################################################
  2. # StdUtils plug-in for NSIS
  3. # Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. #
  19. # http://www.gnu.org/licenses/lgpl-2.1.txt
  20. #################################################################################
  21. #################################################################################
  22. # FUNCTION DECLARTIONS
  23. #################################################################################
  24. !define StdUtils.Time '!insertmacro _StdUtils_Time' #time(), as in C standard library
  25. !define StdUtils.GetMinutes '!insertmacro _StdUtils_GetMinutes' #GetSystemTimeAsFileTime(), returns the number of minutes
  26. !define StdUtils.GetHours '!insertmacro _StdUtils_GetHours' #GetSystemTimeAsFileTime(), returns the number of hours
  27. !define StdUtils.GetDays '!insertmacro _StdUtils_GetDays' #GetSystemTimeAsFileTime(), returns the number of days
  28. !define StdUtils.Rand '!insertmacro _StdUtils_Rand' #rand(), as in C standard library
  29. !define StdUtils.RandMax '!insertmacro _StdUtils_RandMax' #rand(), as in C standard library, with maximum value
  30. !define StdUtils.RandMinMax '!insertmacro _StdUtils_RandMinMax' #rand(), as in C standard library, with minimum/maximum value
  31. !define StdUtils.RandList '!insertmacro _StdUtils_RandList' #rand(), as in C standard library, with list support
  32. !define StdUtils.FormatStr '!insertmacro _StdUtils_FormatStr' #sprintf(), as in C standard library, one '%d' placeholder
  33. !define StdUtils.FormatStr2 '!insertmacro _StdUtils_FormatStr2' #sprintf(), as in C standard library, two '%d' placeholders
  34. !define StdUtils.FormatStr3 '!insertmacro _StdUtils_FormatStr3' #sprintf(), as in C standard library, three '%d' placeholders
  35. !define StdUtils.ScanStr '!insertmacro _StdUtils_ScanStr' #sscanf(), as in C standard library, one '%d' placeholder
  36. !define StdUtils.ScanStr2 '!insertmacro _StdUtils_ScanStr2' #sscanf(), as in C standard library, two '%d' placeholders
  37. !define StdUtils.ScanStr3 '!insertmacro _StdUtils_ScanStr3' #sscanf(), as in C standard library, three '%d' placeholders
  38. !define StdUtils.TrimStr '!insertmacro _StdUtils_TrimStr' #Remove whitspaces from string, left and right
  39. !define StdUtils.TrimStrLeft '!insertmacro _StdUtils_TrimStrLeft' #Remove whitspaces from string, left side only
  40. !define StdUtils.TrimStrRight '!insertmacro _StdUtils_TrimStrRight' #Remove whitspaces from string, right side only
  41. !define StdUtils.RevStr '!insertmacro _StdUtils_RevStr' #Reverse a string, e.g. "reverse me" <-> "em esrever"
  42. !define StdUtils.ValidFileName '!insertmacro _StdUtils_ValidFileName' #Test whether string is a valid file name - no paths allowed
  43. !define StdUtils.ValidPathSpec '!insertmacro _StdUtils_ValidPathSpec' #Test whether string is a valid full(!) path specification
  44. !define StdUtils.SHFileMove '!insertmacro _StdUtils_SHFileMove' #SHFileOperation(), using the FO_MOVE operation
  45. !define StdUtils.SHFileCopy '!insertmacro _StdUtils_SHFileCopy' #SHFileOperation(), using the FO_COPY operation
  46. !define StdUtils.AppendToFile '!insertmacro _StdUtils_AppendToFile' #Append contents of an existing file to another file
  47. !define StdUtils.ExecShellAsUser '!insertmacro _StdUtils_ExecShlUser' #ShellExecute() as NON-elevated user from elevated installer
  48. !define StdUtils.InvokeShellVerb '!insertmacro _StdUtils_InvkeShlVrb' #Invokes a "shell verb", e.g. for pinning items to the taskbar
  49. !define StdUtils.ExecShellWaitEx '!insertmacro _StdUtils_ExecShlWaitEx' #ShellExecuteEx(), returns the handle of the new process
  50. !define StdUtils.WaitForProcEx '!insertmacro _StdUtils_WaitForProcEx' #WaitForSingleObject(), e.g. to wait for a running process
  51. !define StdUtils.GetParameter '!insertmacro _StdUtils_GetParameter' #Get the value of a specific command-line option
  52. !define StdUtils.TestParameter '!insertmacro _StdUtils_TestParameter' #Test whether a specific command-line option has been set
  53. !define StdUtils.ParameterCnt '!insertmacro _StdUtils_ParameterCnt' #Get number of command-line tokens, similar to argc in main()
  54. !define StdUtils.ParameterStr '!insertmacro _StdUtils_ParameterStr' #Get the n-th command-line token, similar to argv[i] in main()
  55. !define StdUtils.GetAllParameters '!insertmacro _StdUtils_GetAllParams' #Get complete command-line, but without executable name
  56. !define StdUtils.GetRealOSVersion '!insertmacro _StdUtils_GetRealOSVer' #Get the *real* Windows version number, even on Windows 8.1+
  57. !define StdUtils.GetRealOSBuildNo '!insertmacro _StdUtils_GetRealOSBld' #Get the *real* Windows build number, even on Windows 8.1+
  58. !define StdUtils.GetRealOSName '!insertmacro _StdUtils_GetRealOSStr' #Get the *real* Windows version, as a "friendly" name
  59. !define StdUtils.GetOSEdition '!insertmacro _StdUtils_GetOSEdition' #Get the Windows edition, i.e. "workstation" or "server"
  60. !define StdUtils.VerifyOSVersion '!insertmacro _StdUtils_VrfyRealOSVer' #Compare *real* operating system to an expected version number
  61. !define StdUtils.VerifyOSBuildNo '!insertmacro _StdUtils_VrfyRealOSBld' #Compare *real* operating system to an expected build number
  62. !define StdUtils.HashText '!insertmacro _StdUtils_HashText' #Compute hash from text string (CRC32, MD5, SHA1/2/3, BLAKE2)
  63. !define StdUtils.HashFile '!insertmacro _StdUtils_HashFile' #Compute hash from file (CRC32, MD5, SHA1/2/3, BLAKE2)
  64. !define StdUtils.TimerCreate '!insertmacro _StdUtils_TimerCreate' #Create a new event-timer that will be triggered periodically
  65. !define StdUtils.TimerDestroy '!insertmacro _StdUtils_TimerDestroy' #Destroy a running timer created with TimerCreate()
  66. !define StdUtils.GetLibVersion '!insertmacro _StdUtils_GetLibVersion' #Get the current StdUtils library version (for debugging)
  67. !define StdUtils.SetVerbose '!insertmacro _StdUtils_SetVerbose' #Enable or disable "verbose" mode (for debugging)
  68. #################################################################################
  69. # MACRO DEFINITIONS
  70. #################################################################################
  71. !macro _StdUtils_Time out
  72. StdUtils::Time /NOUNLOAD
  73. pop ${out}
  74. !macroend
  75. !macro _StdUtils_GetMinutes out
  76. StdUtils::GetMinutes /NOUNLOAD
  77. pop ${out}
  78. !macroend
  79. !macro _StdUtils_GetHours out
  80. StdUtils::GetHours /NOUNLOAD
  81. pop ${out}
  82. !macroend
  83. !macro _StdUtils_GetDays out
  84. StdUtils::GetDays /NOUNLOAD
  85. pop ${out}
  86. !macroend
  87. !macro _StdUtils_Rand out
  88. StdUtils::Rand /NOUNLOAD
  89. pop ${out}
  90. !macroend
  91. !macro _StdUtils_RandMax out max
  92. push ${max}
  93. StdUtils::RandMax /NOUNLOAD
  94. pop ${out}
  95. !macroend
  96. !macro _StdUtils_RandMinMax out min max
  97. push ${min}
  98. push ${max}
  99. StdUtils::RandMinMax /NOUNLOAD
  100. pop ${out}
  101. !macroend
  102. !macro _StdUtils_RandList count max
  103. push ${max}
  104. push ${count}
  105. StdUtils::RandList /NOUNLOAD
  106. !macroend
  107. !macro _StdUtils_FormatStr out format val
  108. push '${format}'
  109. push ${val}
  110. StdUtils::FormatStr /NOUNLOAD
  111. pop ${out}
  112. !macroend
  113. !macro _StdUtils_FormatStr2 out format val1 val2
  114. push '${format}'
  115. push ${val1}
  116. push ${val2}
  117. StdUtils::FormatStr2 /NOUNLOAD
  118. pop ${out}
  119. !macroend
  120. !macro _StdUtils_FormatStr3 out format val1 val2 val3
  121. push '${format}'
  122. push ${val1}
  123. push ${val2}
  124. push ${val3}
  125. StdUtils::FormatStr3 /NOUNLOAD
  126. pop ${out}
  127. !macroend
  128. !macro _StdUtils_ScanStr out format input default
  129. push '${format}'
  130. push '${input}'
  131. push ${default}
  132. StdUtils::ScanStr /NOUNLOAD
  133. pop ${out}
  134. !macroend
  135. !macro _StdUtils_ScanStr2 out1 out2 format input default1 default2
  136. push '${format}'
  137. push '${input}'
  138. push ${default1}
  139. push ${default2}
  140. StdUtils::ScanStr2 /NOUNLOAD
  141. pop ${out1}
  142. pop ${out2}
  143. !macroend
  144. !macro _StdUtils_ScanStr3 out1 out2 out3 format input default1 default2 default3
  145. push '${format}'
  146. push '${input}'
  147. push ${default1}
  148. push ${default2}
  149. push ${default3}
  150. StdUtils::ScanStr3 /NOUNLOAD
  151. pop ${out1}
  152. pop ${out2}
  153. pop ${out3}
  154. !macroend
  155. !macro _StdUtils_TrimStr var
  156. push ${var}
  157. StdUtils::TrimStr /NOUNLOAD
  158. pop ${var}
  159. !macroend
  160. !macro _StdUtils_TrimStrLeft var
  161. push ${var}
  162. StdUtils::TrimStrLeft /NOUNLOAD
  163. pop ${var}
  164. !macroend
  165. !macro _StdUtils_TrimStrRight var
  166. push ${var}
  167. StdUtils::TrimStrRight /NOUNLOAD
  168. pop ${var}
  169. !macroend
  170. !macro _StdUtils_RevStr var
  171. push ${var}
  172. StdUtils::RevStr /NOUNLOAD
  173. pop ${var}
  174. !macroend
  175. !macro _StdUtils_ValidFileName out test
  176. push '${test}'
  177. StdUtils::ValidFileName /NOUNLOAD
  178. pop ${out}
  179. !macroend
  180. !macro _StdUtils_ValidPathSpec out test
  181. push '${test}'
  182. StdUtils::ValidPathSpec /NOUNLOAD
  183. pop ${out}
  184. !macroend
  185. !macro _StdUtils_SHFileMove out from to hwnd
  186. push '${from}'
  187. push '${to}'
  188. push ${hwnd}
  189. StdUtils::SHFileMove /NOUNLOAD
  190. pop ${out}
  191. !macroend
  192. !macro _StdUtils_SHFileCopy out from to hwnd
  193. push '${from}'
  194. push '${to}'
  195. push ${hwnd}
  196. StdUtils::SHFileCopy /NOUNLOAD
  197. pop ${out}
  198. !macroend
  199. !macro _StdUtils_AppendToFile out from dest offset maxlen
  200. push '${from}'
  201. push '${dest}'
  202. push ${offset}
  203. push ${maxlen}
  204. StdUtils::AppendToFile /NOUNLOAD
  205. pop ${out}
  206. !macroend
  207. !macro _StdUtils_ExecShlUser out file verb args
  208. push '${file}'
  209. push '${verb}'
  210. push '${args}'
  211. StdUtils::ExecShellAsUser /NOUNLOAD
  212. pop ${out}
  213. !macroend
  214. !macro _StdUtils_InvkeShlVrb out path file verb_id
  215. push "${path}"
  216. push "${file}"
  217. push ${verb_id}
  218. StdUtils::InvokeShellVerb /NOUNLOAD
  219. pop ${out}
  220. !macroend
  221. !macro _StdUtils_ExecShlWaitEx out_res out_val file verb args
  222. push '${file}'
  223. push '${verb}'
  224. push '${args}'
  225. StdUtils::ExecShellWaitEx /NOUNLOAD
  226. pop ${out_res}
  227. pop ${out_val}
  228. !macroend
  229. !macro _StdUtils_WaitForProcEx out handle
  230. push '${handle}'
  231. StdUtils::WaitForProcEx /NOUNLOAD
  232. pop ${out}
  233. !macroend
  234. !macro _StdUtils_GetParameter out name default
  235. push '${name}'
  236. push '${default}'
  237. StdUtils::GetParameter /NOUNLOAD
  238. pop ${out}
  239. !macroend
  240. !macro _StdUtils_TestParameter out name
  241. push '${name}'
  242. StdUtils::TestParameter /NOUNLOAD
  243. pop ${out}
  244. !macroend
  245. !macro _StdUtils_ParameterCnt out
  246. StdUtils::ParameterCnt /NOUNLOAD
  247. pop ${out}
  248. !macroend
  249. !macro _StdUtils_ParameterStr out index
  250. push ${index}
  251. StdUtils::ParameterStr /NOUNLOAD
  252. pop ${out}
  253. !macroend
  254. !macro _StdUtils_GetAllParams out truncate
  255. push '${truncate}'
  256. StdUtils::GetAllParameters /NOUNLOAD
  257. pop ${out}
  258. !macroend
  259. !macro _StdUtils_GetRealOSVer out_major out_minor out_spack
  260. StdUtils::GetRealOsVersion /NOUNLOAD
  261. pop ${out_major}
  262. pop ${out_minor}
  263. pop ${out_spack}
  264. !macroend
  265. !macro _StdUtils_GetRealOSBld out
  266. StdUtils::GetRealOsBuildNo /NOUNLOAD
  267. pop ${out}
  268. !macroend
  269. !macro _StdUtils_GetRealOSStr out
  270. StdUtils::GetRealOsName /NOUNLOAD
  271. pop ${out}
  272. !macroend
  273. !macro _StdUtils_VrfyRealOSVer out major minor spack
  274. push '${major}'
  275. push '${minor}'
  276. push '${spack}'
  277. StdUtils::VerifyRealOsVersion /NOUNLOAD
  278. pop ${out}
  279. !macroend
  280. !macro _StdUtils_VrfyRealOSBld out build
  281. push '${build}'
  282. StdUtils::VerifyRealOsBuildNo /NOUNLOAD
  283. pop ${out}
  284. !macroend
  285. !macro _StdUtils_GetOSEdition out
  286. StdUtils::GetOsEdition /NOUNLOAD
  287. pop ${out}
  288. !macroend
  289. !macro _StdUtils_HashText out type text
  290. push '${type}'
  291. push '${text}'
  292. StdUtils::HashText /NOUNLOAD
  293. pop ${out}
  294. !macroend
  295. !macro _StdUtils_HashFile out type file
  296. push '${type}'
  297. push '${file}'
  298. StdUtils::HashFile /NOUNLOAD
  299. pop ${out}
  300. !macroend
  301. !macro _StdUtils_TimerCreate out callback interval
  302. GetFunctionAddress ${out} ${callback}
  303. push ${out}
  304. push ${interval}
  305. StdUtils::TimerCreate /NOUNLOAD
  306. pop ${out}
  307. !macroend
  308. !macro _StdUtils_TimerDestroy out timer_id
  309. push ${timer_id}
  310. StdUtils::TimerDestroy /NOUNLOAD
  311. pop ${out}
  312. !macroend
  313. !macro _StdUtils_GetLibVersion out_ver out_tst
  314. StdUtils::GetLibVersion /NOUNLOAD
  315. pop ${out_ver}
  316. pop ${out_tst}
  317. !macroend
  318. !macro _StdUtils_SetVerbose on
  319. !if "${on}" != "0"
  320. StdUtils::EnableVerboseMode /NOUNLOAD
  321. !else
  322. StdUtils::DisableVerboseMode /NOUNLOAD
  323. !endif
  324. !macroend
  325. #################################################################################
  326. # MAGIC NUMBERS
  327. #################################################################################
  328. !define StdUtils.Const.ShellVerb.PinToTaskbar 0
  329. !define StdUtils.Const.ShellVerb.UnpinFromTaskbar 1
  330. !define StdUtils.Const.ShellVerb.PinToStart 2
  331. !define StdUtils.Const.ShellVerb.UnpinFromStart 3