StdUtils.nsh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #################################################################################
  2. # StdUtils plug-in for NSIS
  3. # Copyright (C) 2004-2018 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. # DEVELOPER NOTES:
  22. # - Please see "https://github.com/lordmulder/stdutils/" for news and updates!
  23. # - Please see "Docs\StdUtils\StdUtils.html" for detailed function descriptions!
  24. # - Please see "Examples\StdUtils\StdUtilsTest.nsi" for usage examples!
  25. #################################################################################
  26. # FUNCTION DECLARTIONS
  27. #################################################################################
  28. !ifndef ___STDUTILS__NSH___
  29. !define ___STDUTILS__NSH___
  30. !define StdUtils.Time '!insertmacro _StdU_Time' #time(), as in C standard library
  31. !define StdUtils.GetMinutes '!insertmacro _StdU_GetMinutes' #GetSystemTimeAsFileTime(), returns the number of minutes
  32. !define StdUtils.GetHours '!insertmacro _StdU_GetHours' #GetSystemTimeAsFileTime(), returns the number of hours
  33. !define StdUtils.GetDays '!insertmacro _StdU_GetDays' #GetSystemTimeAsFileTime(), returns the number of days
  34. !define StdUtils.Rand '!insertmacro _StdU_Rand' #rand(), as in C standard library
  35. !define StdUtils.RandMax '!insertmacro _StdU_RandMax' #rand(), as in C standard library, with maximum value
  36. !define StdUtils.RandMinMax '!insertmacro _StdU_RandMinMax' #rand(), as in C standard library, with minimum/maximum value
  37. !define StdUtils.RandList '!insertmacro _StdU_RandList' #rand(), as in C standard library, with list support
  38. !define StdUtils.RandBytes '!insertmacro _StdU_RandBytes' #Generates random bytes, returned as Base64-encoded string
  39. !define StdUtils.FormatStr '!insertmacro _StdU_FormatStr' #sprintf(), as in C standard library, one '%d' placeholder
  40. !define StdUtils.FormatStr2 '!insertmacro _StdU_FormatStr2' #sprintf(), as in C standard library, two '%d' placeholders
  41. !define StdUtils.FormatStr3 '!insertmacro _StdU_FormatStr3' #sprintf(), as in C standard library, three '%d' placeholders
  42. !define StdUtils.ScanStr '!insertmacro _StdU_ScanStr' #sscanf(), as in C standard library, one '%d' placeholder
  43. !define StdUtils.ScanStr2 '!insertmacro _StdU_ScanStr2' #sscanf(), as in C standard library, two '%d' placeholders
  44. !define StdUtils.ScanStr3 '!insertmacro _StdU_ScanStr3' #sscanf(), as in C standard library, three '%d' placeholders
  45. !define StdUtils.TrimStr '!insertmacro _StdU_TrimStr' #Remove whitspaces from string, left and right
  46. !define StdUtils.TrimStrLeft '!insertmacro _StdU_TrimStrLeft' #Remove whitspaces from string, left side only
  47. !define StdUtils.TrimStrRight '!insertmacro _StdU_TrimStrRight' #Remove whitspaces from string, right side only
  48. !define StdUtils.RevStr '!insertmacro _StdU_RevStr' #Reverse a string, e.g. "reverse me" <-> "em esrever"
  49. !define StdUtils.ValidFileName '!insertmacro _StdU_ValidFileName' #Test whether string is a valid file name - no paths allowed
  50. !define StdUtils.ValidPathSpec '!insertmacro _StdU_ValidPathSpec' #Test whether string is a valid full(!) path specification
  51. !define StdUtils.ValidDomainName '!insertmacro _StdU_ValidDomain' #Test whether string is a valid host name or domain name
  52. !define StdUtils.StrToUtf8 '!insertmacro _StdU_StrToUtf8' #Convert string from Unicode (UTF-16) or ANSI to UTF-8 bytes
  53. !define StdUtils.StrFromUtf8 '!insertmacro _StdU_StrFromUtf8' #Convert string from UTF-8 bytes to Unicode (UTF-16) or ANSI
  54. !define StdUtils.SHFileMove '!insertmacro _StdU_SHFileMove' #SHFileOperation(), using the FO_MOVE operation
  55. !define StdUtils.SHFileCopy '!insertmacro _StdU_SHFileCopy' #SHFileOperation(), using the FO_COPY operation
  56. !define StdUtils.AppendToFile '!insertmacro _StdU_AppendToFile' #Append contents of an existing file to another file
  57. !define StdUtils.ExecShellAsUser '!insertmacro _StdU_ExecShlUser' #ShellExecute() as NON-elevated user from elevated installer
  58. !define StdUtils.InvokeShellVerb '!insertmacro _StdU_InvkeShlVrb' #Invokes a "shell verb", e.g. for pinning items to the taskbar
  59. !define StdUtils.ExecShellWaitEx '!insertmacro _StdU_ExecShlWaitEx' #ShellExecuteEx(), returns the handle of the new process
  60. !define StdUtils.WaitForProcEx '!insertmacro _StdU_WaitForProcEx' #WaitForSingleObject(), e.g. to wait for a running process
  61. !define StdUtils.GetParameter '!insertmacro _StdU_GetParameter' #Get the value of a specific command-line option
  62. !define StdUtils.TestParameter '!insertmacro _StdU_TestParameter' #Test whether a specific command-line option has been set
  63. !define StdUtils.ParameterCnt '!insertmacro _StdU_ParameterCnt' #Get number of command-line tokens, similar to argc in main()
  64. !define StdUtils.ParameterStr '!insertmacro _StdU_ParameterStr' #Get the n-th command-line token, similar to argv[i] in main()
  65. !define StdUtils.GetAllParameters '!insertmacro _StdU_GetAllParams' #Get complete command-line, but without executable name
  66. !define StdUtils.GetRealOSVersion '!insertmacro _StdU_GetRealOSVer' #Get the *real* Windows version number, even on Windows 8.1+
  67. !define StdUtils.GetRealOSBuildNo '!insertmacro _StdU_GetRealOSBld' #Get the *real* Windows build number, even on Windows 8.1+
  68. !define StdUtils.GetRealOSName '!insertmacro _StdU_GetRealOSStr' #Get the *real* Windows version, as a "friendly" name
  69. !define StdUtils.GetOSEdition '!insertmacro _StdU_GetOSEdition' #Get the Windows edition, i.e. "workstation" or "server"
  70. !define StdUtils.GetOSReleaseId '!insertmacro _StdU_GetOSRelIdNo' #Get the Windows release identifier (on Windows 10)
  71. !define StdUtils.VerifyOSVersion '!insertmacro _StdU_VrfyRealOSVer' #Compare *real* operating system to an expected version number
  72. !define StdUtils.VerifyOSBuildNo '!insertmacro _StdU_VrfyRealOSBld' #Compare *real* operating system to an expected build number
  73. !define StdUtils.HashText '!insertmacro _StdU_HashText' #Compute hash from text string (CRC32, MD5, SHA1/2/3, BLAKE2)
  74. !define StdUtils.HashFile '!insertmacro _StdU_HashFile' #Compute hash from file (CRC32, MD5, SHA1/2/3, BLAKE2)
  75. !define StdUtils.NormalizePath '!insertmacro _StdU_NormalizePath' #Simplifies the path to produce a direct, well-formed path
  76. !define StdUtils.GetParentPath '!insertmacro _StdU_GetParentPath' #Get parent path by removing the last component from the path
  77. !define StdUtils.SplitPath '!insertmacro _StdU_SplitPath' #Split the components of the given path
  78. !define StdUtils.GetDrivePart '!insertmacro _StdU_GetDrivePart' #Get drive component of path
  79. !define StdUtils.GetDirectoryPart '!insertmacro _StdU_GetDirPart' #Get directory component of path
  80. !define StdUtils.GetFileNamePart '!insertmacro _StdU_GetFNamePart' #Get file name component of path
  81. !define StdUtils.GetExtensionPart '!insertmacro _StdU_GetExtnPart' #Get file extension component of path
  82. !define StdUtils.TimerCreate '!insertmacro _StdU_TimerCreate' #Create a new event-timer that will be triggered periodically
  83. !define StdUtils.TimerDestroy '!insertmacro _StdU_TimerDestroy' #Destroy a running timer created with TimerCreate()
  84. !define StdUtils.ProtectStr '!insertmacro _StdU_PrtctStr' #Protect a given String using Windows' DPAPI
  85. !define StdUtils.UnprotectStr '!insertmacro _StdU_UnprtctStr' #Unprotect a string that was protected via ProtectStr()
  86. !define StdUtils.GetLibVersion '!insertmacro _StdU_GetLibVersion' #Get the current StdUtils library version (for debugging)
  87. !define StdUtils.SetVerbose '!insertmacro _StdU_SetVerbose' #Enable or disable "verbose" mode (for debugging)
  88. #################################################################################
  89. # MACRO DEFINITIONS
  90. #################################################################################
  91. !macro _StdU_Time out
  92. StdUtils::Time /NOUNLOAD
  93. pop ${out}
  94. !macroend
  95. !macro _StdU_GetMinutes out
  96. StdUtils::GetMinutes /NOUNLOAD
  97. pop ${out}
  98. !macroend
  99. !macro _StdU_GetHours out
  100. StdUtils::GetHours /NOUNLOAD
  101. pop ${out}
  102. !macroend
  103. !macro _StdU_GetDays out
  104. StdUtils::GetDays /NOUNLOAD
  105. pop ${out}
  106. !macroend
  107. !macro _StdU_Rand out
  108. StdUtils::Rand /NOUNLOAD
  109. pop ${out}
  110. !macroend
  111. !macro _StdU_RandMax out max
  112. push ${max}
  113. StdUtils::RandMax /NOUNLOAD
  114. pop ${out}
  115. !macroend
  116. !macro _StdU_RandMinMax out min max
  117. push ${min}
  118. push ${max}
  119. StdUtils::RandMinMax /NOUNLOAD
  120. pop ${out}
  121. !macroend
  122. !macro _StdU_RandList count max
  123. push ${max}
  124. push ${count}
  125. StdUtils::RandList /NOUNLOAD
  126. !macroend
  127. !macro _StdU_RandBytes out count
  128. push ${count}
  129. StdUtils::RandBytes /NOUNLOAD
  130. pop ${out}
  131. !macroend
  132. !macro _StdU_FormatStr out format val
  133. push `${format}`
  134. push ${val}
  135. StdUtils::FormatStr /NOUNLOAD
  136. pop ${out}
  137. !macroend
  138. !macro _StdU_FormatStr2 out format val1 val2
  139. push `${format}`
  140. push ${val1}
  141. push ${val2}
  142. StdUtils::FormatStr2 /NOUNLOAD
  143. pop ${out}
  144. !macroend
  145. !macro _StdU_FormatStr3 out format val1 val2 val3
  146. push `${format}`
  147. push ${val1}
  148. push ${val2}
  149. push ${val3}
  150. StdUtils::FormatStr3 /NOUNLOAD
  151. pop ${out}
  152. !macroend
  153. !macro _StdU_ScanStr out format input default
  154. push `${format}`
  155. push `${input}`
  156. push ${default}
  157. StdUtils::ScanStr /NOUNLOAD
  158. pop ${out}
  159. !macroend
  160. !macro _StdU_ScanStr2 out1 out2 format input default1 default2
  161. push `${format}`
  162. push `${input}`
  163. push ${default1}
  164. push ${default2}
  165. StdUtils::ScanStr2 /NOUNLOAD
  166. pop ${out1}
  167. pop ${out2}
  168. !macroend
  169. !macro _StdU_ScanStr3 out1 out2 out3 format input default1 default2 default3
  170. push `${format}`
  171. push `${input}`
  172. push ${default1}
  173. push ${default2}
  174. push ${default3}
  175. StdUtils::ScanStr3 /NOUNLOAD
  176. pop ${out1}
  177. pop ${out2}
  178. pop ${out3}
  179. !macroend
  180. !macro _StdU_TrimStr var
  181. push ${var}
  182. StdUtils::TrimStr /NOUNLOAD
  183. pop ${var}
  184. !macroend
  185. !macro _StdU_TrimStrLeft var
  186. push ${var}
  187. StdUtils::TrimStrLeft /NOUNLOAD
  188. pop ${var}
  189. !macroend
  190. !macro _StdU_TrimStrRight var
  191. push ${var}
  192. StdUtils::TrimStrRight /NOUNLOAD
  193. pop ${var}
  194. !macroend
  195. !macro _StdU_RevStr var
  196. push ${var}
  197. StdUtils::RevStr /NOUNLOAD
  198. pop ${var}
  199. !macroend
  200. !macro _StdU_ValidFileName out test
  201. push `${test}`
  202. StdUtils::ValidFileName /NOUNLOAD
  203. pop ${out}
  204. !macroend
  205. !macro _StdU_ValidPathSpec out test
  206. push `${test}`
  207. StdUtils::ValidPathSpec /NOUNLOAD
  208. pop ${out}
  209. !macroend
  210. !macro _StdU_ValidDomain out test
  211. push `${test}`
  212. StdUtils::ValidDomainName /NOUNLOAD
  213. pop ${out}
  214. !macroend
  215. !macro _StdU_StrToUtf8 out str
  216. push `${str}`
  217. StdUtils::StrToUtf8 /NOUNLOAD
  218. pop ${out}
  219. !macroend
  220. !macro _StdU_StrFromUtf8 out trnc str
  221. push ${trnc}
  222. push `${str}`
  223. StdUtils::StrFromUtf8 /NOUNLOAD
  224. pop ${out}
  225. !macroend
  226. !macro _StdU_SHFileMove out from to hwnd
  227. push `${from}`
  228. push `${to}`
  229. push ${hwnd}
  230. StdUtils::SHFileMove /NOUNLOAD
  231. pop ${out}
  232. !macroend
  233. !macro _StdU_SHFileCopy out from to hwnd
  234. push `${from}`
  235. push `${to}`
  236. push ${hwnd}
  237. StdUtils::SHFileCopy /NOUNLOAD
  238. pop ${out}
  239. !macroend
  240. !macro _StdU_AppendToFile out from dest offset maxlen
  241. push `${from}`
  242. push `${dest}`
  243. push ${offset}
  244. push ${maxlen}
  245. StdUtils::AppendToFile /NOUNLOAD
  246. pop ${out}
  247. !macroend
  248. !macro _StdU_ExecShlUser out file verb args
  249. push `${file}`
  250. push `${verb}`
  251. push `${args}`
  252. StdUtils::ExecShellAsUser /NOUNLOAD
  253. pop ${out}
  254. !macroend
  255. !macro _StdU_InvkeShlVrb out path file verb_id
  256. push "${path}"
  257. push "${file}"
  258. push ${verb_id}
  259. StdUtils::InvokeShellVerb /NOUNLOAD
  260. pop ${out}
  261. !macroend
  262. !macro _StdU_ExecShlWaitEx out_res out_val file verb args
  263. push `${file}`
  264. push `${verb}`
  265. push `${args}`
  266. StdUtils::ExecShellWaitEx /NOUNLOAD
  267. pop ${out_res}
  268. pop ${out_val}
  269. !macroend
  270. !macro _StdU_WaitForProcEx out handle
  271. push `${handle}`
  272. StdUtils::WaitForProcEx /NOUNLOAD
  273. pop ${out}
  274. !macroend
  275. !macro _StdU_GetParameter out name default
  276. push `${name}`
  277. push `${default}`
  278. StdUtils::GetParameter /NOUNLOAD
  279. pop ${out}
  280. !macroend
  281. !macro _StdU_TestParameter out name
  282. push `${name}`
  283. StdUtils::TestParameter /NOUNLOAD
  284. pop ${out}
  285. !macroend
  286. !macro _StdU_ParameterCnt out
  287. StdUtils::ParameterCnt /NOUNLOAD
  288. pop ${out}
  289. !macroend
  290. !macro _StdU_ParameterStr out index
  291. push ${index}
  292. StdUtils::ParameterStr /NOUNLOAD
  293. pop ${out}
  294. !macroend
  295. !macro _StdU_GetAllParams out truncate
  296. push `${truncate}`
  297. StdUtils::GetAllParameters /NOUNLOAD
  298. pop ${out}
  299. !macroend
  300. !macro _StdU_GetRealOSVer out_major out_minor out_spack
  301. StdUtils::GetRealOsVersion /NOUNLOAD
  302. pop ${out_major}
  303. pop ${out_minor}
  304. pop ${out_spack}
  305. !macroend
  306. !macro _StdU_GetRealOSBld out
  307. StdUtils::GetRealOsBuildNo /NOUNLOAD
  308. pop ${out}
  309. !macroend
  310. !macro _StdU_GetRealOSStr out
  311. StdUtils::GetRealOsName /NOUNLOAD
  312. pop ${out}
  313. !macroend
  314. !macro _StdU_VrfyRealOSVer out major minor spack
  315. push `${major}`
  316. push `${minor}`
  317. push `${spack}`
  318. StdUtils::VerifyRealOsVersion /NOUNLOAD
  319. pop ${out}
  320. !macroend
  321. !macro _StdU_VrfyRealOSBld out build
  322. push `${build}`
  323. StdUtils::VerifyRealOsBuildNo /NOUNLOAD
  324. pop ${out}
  325. !macroend
  326. !macro _StdU_GetOSEdition out
  327. StdUtils::GetOsEdition /NOUNLOAD
  328. pop ${out}
  329. !macroend
  330. !macro _StdU_GetOSRelIdNo out
  331. StdUtils::GetOsReleaseId /NOUNLOAD
  332. pop ${out}
  333. !macroend
  334. !macro _StdU_HashText out type text
  335. push `${type}`
  336. push `${text}`
  337. StdUtils::HashText /NOUNLOAD
  338. pop ${out}
  339. !macroend
  340. !macro _StdU_HashFile out type file
  341. push `${type}`
  342. push `${file}`
  343. StdUtils::HashFile /NOUNLOAD
  344. pop ${out}
  345. !macroend
  346. !macro _StdU_NormalizePath out path
  347. push `${path}`
  348. StdUtils::NormalizePath /NOUNLOAD
  349. pop ${out}
  350. !macroend
  351. !macro _StdU_GetParentPath out path
  352. push `${path}`
  353. StdUtils::GetParentPath /NOUNLOAD
  354. pop ${out}
  355. !macroend
  356. !macro _StdU_SplitPath out_drive out_dir out_fname out_ext path
  357. push `${path}`
  358. StdUtils::SplitPath /NOUNLOAD
  359. pop ${out_drive}
  360. pop ${out_dir}
  361. pop ${out_fname}
  362. pop ${out_ext}
  363. !macroend
  364. !macro _StdU_GetDrivePart out path
  365. push `${path}`
  366. StdUtils::GetDrivePart /NOUNLOAD
  367. pop ${out}
  368. !macroend
  369. !macro _StdU_GetDirPart out path
  370. push `${path}`
  371. StdUtils::GetDirectoryPart /NOUNLOAD
  372. pop ${out}
  373. !macroend
  374. !macro _StdU_GetFNamePart out path
  375. push `${path}`
  376. StdUtils::GetFileNamePart /NOUNLOAD
  377. pop ${out}
  378. !macroend
  379. !macro _StdU_GetExtnPart out path
  380. push `${path}`
  381. StdUtils::GetExtensionPart /NOUNLOAD
  382. pop ${out}
  383. !macroend
  384. !macro _StdU_TimerCreate out callback interval
  385. GetFunctionAddress ${out} ${callback}
  386. push ${out}
  387. push ${interval}
  388. StdUtils::TimerCreate /NOUNLOAD
  389. pop ${out}
  390. !macroend
  391. !macro _StdU_TimerDestroy out timer_id
  392. push ${timer_id}
  393. StdUtils::TimerDestroy /NOUNLOAD
  394. pop ${out}
  395. !macroend
  396. !macro _StdU_PrtctStr out dpsc salt text
  397. push `${dpsc}`
  398. push `${salt}`
  399. push `${text}`
  400. StdUtils::ProtectStr /NOUNLOAD
  401. pop ${out}
  402. !macroend
  403. !macro _StdU_UnprtctStr out trnc salt data
  404. push `${trnc}`
  405. push `${salt}`
  406. push `${data}`
  407. StdUtils::UnprotectStr /NOUNLOAD
  408. pop ${out}
  409. !macroend
  410. !macro _StdU_GetLibVersion out_ver out_tst
  411. StdUtils::GetLibVersion /NOUNLOAD
  412. pop ${out_ver}
  413. pop ${out_tst}
  414. !macroend
  415. !macro _StdU_SetVerbose enable
  416. Push ${enable}
  417. StdUtils::SetVerboseMode /NOUNLOAD
  418. !macroend
  419. #################################################################################
  420. # MAGIC NUMBERS
  421. #################################################################################
  422. !define StdUtils.Const.ShellVerb.PinToTaskbar 0
  423. !define StdUtils.Const.ShellVerb.UnpinFromTaskbar 1
  424. !define StdUtils.Const.ShellVerb.PinToStart 2
  425. !define StdUtils.Const.ShellVerb.UnpinFromStart 3
  426. !endif # !___STDUTILS__NSH___