Subversion Repositories Kolibri OS

Rev

Rev 1050 | Rev 1262 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;  Copyright (C) Vasiliy Kosenko (vkos), 2009                                                    ;;
  3. ;;  Launch is free software: you can redistribute it and/or modify it under the terms of the GNU  ;;
  4. ;;  General Public License as published by the Free Software Foundation, either version 2         ;;
  5. ;;  of the License, or (at your option) any later version.                                        ;;
  6. ;;                                                                                                ;;
  7. ;;  Launch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without   ;;
  8. ;;  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;
  9. ;;  General Public License for more details.                                                      ;;
  10. ;;                                                                                                ;;
  11. ;;  You should have received a copy of the GNU General Public License along with Launch.          ;;
  12. ;;  If not, see <http://www.gnu.org/licenses/>.                                                   ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. ;;  Launch finds program in search dirictories and runs it.                                       ;;
  17. ;;  For more details see readme.txt                                                               ;;
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19.  
  20. format binary
  21.  
  22. APP_NAME fix 'Launch'
  23. APP_VERSION fix '0.1.5'
  24.  
  25. use32
  26. org 0x0
  27.  
  28. db 'MENUET01'
  29. dd 0x01
  30. dd START
  31. dd APP_END
  32. dd MEM_END
  33. dd APP_STACK
  34. dd args
  35. dd path
  36.  
  37. define DEBUG_NO 0
  38. define DEBUG_CONSOLE 1
  39. define DEBUG_BOARD 2                    ;; Not used now
  40.  
  41. define PATH_MAX_LEN 1024
  42. define DEBUG_MAX_LEN 8
  43. define DEBUG_DEFAULT 0
  44. define BUFF_SIZE 1024
  45.  
  46. include '../../../proc32.inc'
  47. include '../../../macros.inc'
  48. include 'libio.inc'
  49. include 'mem.inc'
  50. include 'dll.inc'
  51.  
  52. ;;--------------------------------------------------------------------------------------------------
  53. ;; Basic initialization
  54. START:
  55.         ;; Initialize process heap
  56.         mcall 68,11
  57.         test eax, eax
  58.         jz exit
  59.  
  60.         ;; Import modules
  61.         stdcall dll.Load,importTable
  62.         test eax, eax
  63.         jnz exit
  64.  
  65. ;;--------------------------------------------------------------------------------------------------
  66. ;; Reading config
  67. read_ini_path:                                                                                                  ;; Read search path
  68.         ;; First read config in /sys/etc
  69.         invoke ini.get_str, etc_cfg, cfg_main, cfg_path, search_path, PATH_MAX_LEN, empty_str
  70.  
  71.         ;; Next, read config from current directory
  72.         ;; Find end of path string
  73. .find_path_eol:
  74.         mov edi, path
  75.         mov ecx, PATH_MAX_LEN
  76.         xor al, al
  77.         cld
  78.         repne scasb
  79.  
  80.         ;; Append ext to run path (NOTE: this work only when config file has name <launch-file-name>.cfg and ext size is dword)
  81.         mov eax, dword [cfg_ext]
  82.         dec edi
  83.         mov dword [edi], eax
  84.         mov byte [edi+5], 0
  85.  
  86.         ;; Currently there is no checking for repeating pathes, so we should only concatenate two strings
  87.         ;; So we need to find end of current search_path string
  88. .find_search_eol:
  89.         mov edi, search_path
  90.         mov ecx, PATH_MAX_LEN
  91.         xor al, al
  92.         ;cld
  93.         repne scasb
  94.         dec edi
  95.  
  96.         ;; Now we need to correct buffer length
  97.         mov eax, path+PATH_MAX_LEN
  98.         sub eax, edi
  99.         ;; Read ini
  100.         invoke ini.get_str, path, cfg_main, cfg_path, edi, PATH_MAX_LEN, empty_str
  101.  
  102. read_ini_debug:                                                                                         ;; Read debug options
  103.         ;; Read debug options from config files
  104.         invoke ini.get_option_str, etc_cfg, cfg_debug, cfg_debug, debug_strings, DEBUG_MAX_LEN, DEBUG_DEFAULT
  105.         invoke ini.get_option_str, path, cfg_debug, cfg_debug, debug_strings, DEBUG_MAX_LEN, eax
  106.         mov [debug_option], eax
  107.        
  108.         test eax, eax                           ;; No console
  109.         je .ok
  110.        
  111.         jmp .con_init
  112.  
  113. .console_err:
  114.         mov dword [debug_option], 0
  115.         jmp .ok
  116.  
  117. .con_init:
  118.         stdcall dll.Load, consoleImport
  119.         test eax, eax
  120.         jnz .console_err
  121.         invoke con.init, -1, -1, -1, -1, window_title
  122.  
  123. .read_level:
  124.         invoke ini.get_int, etc_cfg, cfg_debug, cfg_level, 0
  125.         invoke ini.get_int, path, cfg_debug, cfg_level, eax
  126.         mov dword [debug_level], eax
  127. .ok:
  128.  
  129. ; read_ini_kobra:
  130. ;       invoke ini.get_str, etc_cfg, cfg_kobra, cfg_use, kobra_use, KOBRA_USE_MAX_LEN, KOBRA_USE_DEFAULT
  131. ;       invoke ini.get_str, path, cfg_kobra, cfg_use, kobra_use, KOBRA_USE_MAX_LEN, kobra_use
  132. ;      
  133. ;       ;; Now convert string option to acceptable type
  134.        
  135.  
  136. ;;--------------------------------------------------------------------------------------------------
  137. ;; Parse command line options
  138. parse_args:
  139.         ;; Now parse command line arguments
  140.         ;; TODO: use optparse library
  141.         ;; Currently the only argument to parse is program name with its' arguments
  142.  
  143. .skip_spaces:
  144.         mov ecx, -1
  145.         mov edi, args
  146.         mov al, ' '
  147.         xor bl, bl
  148.         ;cld
  149.         repe scasb
  150.  
  151.         push edi
  152.  
  153.         mov ecx, -1
  154. @@:
  155.         scasb
  156.         je @f
  157.         xchg al, bl
  158.         dec edi
  159.         scasb
  160.         jne @b
  161. @@:
  162.         mov dword [prog_args], edi
  163.  
  164.         pop edi
  165.         dec edi
  166.         ;; Now edi = program name
  167.  
  168. ;;--------------------------------------------------------------------------------------------------
  169. ;; Finding file
  170. search_file:
  171.         push edi
  172.         mov esi, search_path
  173.         xchg esi, [esp]
  174. .loop:
  175.         or dl, dl
  176.         je .prn_dbg
  177.         xor dl, dl
  178.         jmp .prn_end
  179. .prn_dbg:
  180.         push eax
  181.         mov al, byte [debug_option]
  182. ;       dec al
  183.         test al, al
  184.         je .prn_stp
  185.         mov eax, dword [debug_level]
  186.         or eax, eax
  187.         je .prn_stp
  188.         dec eax
  189.         or eax, eax
  190.         je .prn_1
  191. .prn_1:
  192.         cinvoke con.printf, message_dbg_not_found, buff
  193.  
  194. .prn_stp:
  195.         pop eax
  196. .prn_end:
  197.         xor eax, eax                                    ;; When we check is proramme launched we are checking for eax
  198.         xchg esi, [esp]
  199.         mov edi, buff
  200. .copy_path:
  201.         lodsb
  202.         cmp al, ';'
  203.         je .copy_file
  204.         or al, al
  205.         je exit
  206.         stosb
  207.         jmp .copy_path
  208.  
  209. .copy_file:
  210.         xchg esi, [esp]
  211.         push esi
  212. .cp_file_loop:
  213.         lodsb
  214.         or al, al
  215.         je .copy_end
  216.         cmp al, ' '
  217.         je .copy_end
  218.         stosb
  219.         jmp .cp_file_loop
  220.  
  221. .copy_end:
  222.         pop esi
  223.         xor al, al
  224.         stosb
  225.  
  226.         ;; Try to launch
  227.  
  228.         mov dword [LaunchStruct.Function], 7
  229.         push dword [prog_args]
  230.         pop dword [LaunchStruct.Arguments]
  231.         mov dword [LaunchStruct.Flags], 0
  232.         mov dword [LaunchStruct.Zero], 0
  233.         mov dword [LaunchStruct.FileNameP], buff
  234.         mcall 70, LaunchStruct
  235.         cmp eax, 0
  236.         jl .loop
  237.  
  238. ;;--------------------------------------------------------------------------------------------------
  239. ;; Exit
  240. exit:
  241.         push eax
  242.         ;; If console is present we should write some info
  243.         mov al, byte [debug_option]
  244.         cmp al, DEBUG_CONSOLE
  245.         jne .close
  246.  
  247. .write_console:
  248.         pop eax
  249.         test eax, eax
  250.         jz .write_error
  251. .write_launched:
  252.         cinvoke con.printf, message_ok, buff, eax, eax
  253.         jmp .wr_end
  254. .write_error:
  255.         pop edi
  256.         cinvoke con.printf, message_error, edi
  257. .wr_end:
  258.         invoke con.exit, 0
  259.  
  260. .close:
  261.         mcall -1
  262.  
  263. ;; End of code
  264. ;;--------------------------------------------------------------------------------------------------
  265.  
  266. align 16
  267. importTable:
  268.  
  269. library libini, 'libini.obj' ;,                           \
  270. ;        libio, 'libio.obj',                            \
  271.  
  272. import  libini, \
  273.         ini.get_str        ,'ini_get_str', \
  274. \;        ini.set_str      ,'ini_set_str', \
  275.         ini.get_int        ,'ini_get_int', \
  276. \;        ini.set_int      ,'ini_set_int', \
  277. \;        ini.get_color    ,'ini_get_color', \
  278. \;        ini.set_color    ,'ini_set_color', \
  279.         ini.get_option_str ,'ini_get_option_str';, \
  280.  
  281. ;import  libio, \
  282. ;        file_find_first,'file_find_first', \
  283. ;        file_find_next ,'file_find_next', \
  284. ;        file_find_close,'file_find_close', \
  285. ;        file_size      ,'file_size', \
  286. ;        file_open      ,'file_open', \
  287. ;        file_read      ,'file_read', \
  288. ;        file_write     ,'file_write', \
  289. ;        file_seek      ,'file_seek', \
  290. ;        file_tell      ,'file_tell', \
  291. ;        file_eof?      ,'file_eof?', \
  292. ;        file_truncate  ,'file_truncate', \
  293. ;        file_close     ,'file_close'
  294.  
  295. consoleImport:
  296. library                                                 \
  297.         conlib, 'console.obj'
  298.  
  299. import conlib,\
  300.         con.init,         'con_init',\
  301.         con.exit,         'con_exit',\
  302.         con.printf,       'con_printf' ;,\
  303. ;        con.write_asciiz, 'con_write_asciiz'
  304.  
  305. align 16
  306. APP_DATA:
  307.  
  308. ;; Window title
  309. window_title:
  310.         db APP_NAME, ' ', APP_VERSION, 0
  311.  
  312. ;; Messages
  313. message_dbg_not_found:
  314.         db '%s not found', 10, 0
  315.  
  316. message_error:
  317.         db 'File (%s) not found!', 0
  318.  
  319. message_ok:
  320.         db '%s loaded succesfully. PID: %d (0x%X)'
  321.  
  322. ;; Empty string
  323. empty_str:
  324.         db 0
  325.  
  326. ;; Configuration path
  327. etc_cfg:
  328.         db '/sys/etc/'
  329. cfg_name:
  330.         db 'launch'
  331. cfg_ext:
  332.         db '.cfg', 0
  333.  
  334. ;; String in config file
  335. cfg_main:
  336.         db 'main', 0
  337. cfg_path:
  338.         db 'path', 0
  339. cfg_debug:
  340.         db 'debug', 0
  341. cfg_level:
  342.         db 'level', 0
  343.  
  344. ;; List of debug modes for parsing debug option
  345. debug_strings:
  346.         dd debug_no
  347.         dd debug_console
  348.         dd 0
  349.  
  350. debug_no:
  351.         db 'no', 0
  352. debug_console:
  353.         db 'console', 0
  354.  
  355. ; modes_nums:
  356. ; debug_no_num:
  357. ;       db 1
  358. ;
  359. ; debug_console_num:
  360. ;       db 2
  361.  
  362. debug_level:
  363.         dd 0
  364.  
  365. debug_kobra:
  366.         db 0
  367.  
  368. LaunchStruct FileInfoRun
  369.  
  370. args: db 0
  371. APP_END:
  372. rb 255
  373.  
  374. prog_args:
  375.         rd 1
  376. path:
  377.         rb 1024
  378.  
  379. ;; Search path will be here
  380. search_path:
  381.         rb PATH_MAX_LEN
  382.  
  383. ;; debug option as number
  384. debug_option:
  385.         dd 0
  386.  
  387. ;; Buffer
  388. buff:
  389.         rb BUFF_SIZE
  390.  
  391. rb 0x1000       ;; 4 Kb stack
  392. APP_STACK:
  393. MEM_END:
  394.