Subversion Repositories Kolibri OS

Rev

Rev 1236 | Rev 1293 | 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 3         ;;
  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.60'
  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. purge mov
  53.  
  54. include 'thread.inc'
  55. include 'ipc.inc'
  56. include 'kobra.inc'
  57.  
  58. ;;--------------------------------------------------------------------------------------------------
  59. ;; Basic initialization
  60. START:
  61.         ;; Initialize process heap
  62.         mcall 68,11
  63.         test eax, eax
  64.         jz exit
  65.  
  66.         ;; Import modules
  67.         stdcall dll.Load,importTable
  68.         test eax, eax
  69.         jnz exit
  70.  
  71. ;;--------------------------------------------------------------------------------------------------
  72. ;; Reading config
  73. read_ini_path:                                                                                                  ;; Read search path
  74.         ;; First read config in /sys/etc
  75.         invoke ini.get_str, etc_cfg, cfg_main, cfg_path, search_path, PATH_MAX_LEN, empty_str
  76.  
  77.         ;; Next, read config from current directory
  78.         ;; Find end of path string
  79. .find_path_eol:
  80.         mov edi, path
  81.         mov ecx, PATH_MAX_LEN
  82.         xor al, al
  83.         cld
  84.         repne scasb
  85.  
  86.         ;; Append ext to run path (NOTE: this work only when config file has name <launch-file-name>.cfg and ext size is dword)
  87.         mov eax, dword [cfg_ext]
  88.         dec edi
  89.         mov dword [edi], eax
  90.         mov byte [edi+5], 0
  91.  
  92.         ;; Currently there is no checking for repeating pathes, so we should only concatenate two strings
  93.         ;; So we need to find end of current search_path string
  94. .find_search_eol:
  95.         mov edi, search_path
  96.         mov ecx, PATH_MAX_LEN
  97.         xor al, al
  98.         ;cld
  99.         repne scasb
  100.         dec edi
  101.  
  102.         ;; Now we need to correct buffer length
  103.         mov eax, path+PATH_MAX_LEN
  104.         sub eax, edi
  105.         ;; Read ini
  106.         invoke ini.get_str, path, cfg_main, cfg_path, edi, PATH_MAX_LEN, empty_str
  107.  
  108. read_ini_debug:                                                                                         ;; Read debug options
  109.         ;; Read debug options from config files
  110.         invoke ini.get_option_str, etc_cfg, cfg_debug, cfg_debug, debug_strings, DEBUG_MAX_LEN, DEBUG_DEFAULT
  111.         invoke ini.get_option_str, path, cfg_debug, cfg_debug, debug_strings, DEBUG_MAX_LEN, eax
  112.         mov [debug_option], eax
  113.        
  114.         test eax, eax                           ;; No console
  115.         je .ok
  116.        
  117.         jmp .con_init
  118.  
  119. .console_err:
  120.         mov byte [debug_option], 0
  121.         jmp .ok
  122.  
  123. .con_init:
  124.         stdcall dll.Load, consoleImport
  125.         test eax, eax
  126.         jnz .console_err
  127.         invoke con.init, -1, -1, -1, -1, window_title
  128.  
  129. .read_level:
  130.         invoke ini.get_int, etc_cfg, cfg_debug, cfg_level, 0
  131.         invoke ini.get_int, path, cfg_debug, cfg_level, eax
  132.         mov byte [debug_level], al
  133. .ok:
  134.  
  135. read_ini_kobra:
  136.         invoke ini.get_bool, etc_cfg, cfg_kobra, cfg_use, 0
  137.         invoke ini.get_bool, path, cfg_kobra, cfg_use, eax
  138.        
  139.         mov byte [kobra_use], al
  140.  
  141. ;;--------------------------------------------------------------------------------------------------
  142. ;; Parse command line options
  143. parse_args:
  144.         ;; Now parse command line arguments
  145.         ;; TODO: use optparse library
  146.         ;; Currently the only argument to parse is program name with its' arguments
  147.  
  148. .skip_spaces:
  149.         mov ecx, -1
  150.         mov edi, args
  151.         mov al, ' '
  152.         xor bl, bl
  153.         ;cld
  154.         repe scasb
  155.  
  156.         push edi
  157.  
  158.         mov ecx, -1
  159. @@:
  160.         scasb
  161.         je @f
  162.         xchg al, bl
  163.         dec edi
  164.         scasb
  165.         jne @b
  166. @@:
  167.         mov dword [prog_args], edi
  168.  
  169.         pop edi
  170.         dec edi
  171.         ;; Now edi = program name
  172.  
  173. ;;--------------------------------------------------------------------------------------------------
  174. ;; Finding file
  175. search_file:
  176.         push edi
  177.         mov esi, search_path
  178.         xchg esi, [esp]
  179. .loop:
  180.         or dl, dl
  181.         je .prn_dbg
  182.         xor dl, dl
  183.         jmp .prn_end
  184. .prn_dbg:
  185.         push eax
  186.         mov al, byte [debug_option]
  187. ;       dec al
  188.         test al, al
  189.         je .prn_stp
  190.         mov al, byte [debug_level]
  191.         or al, al
  192.         je .prn_stp
  193.         dec al
  194.         or al, al
  195.         je .prn_1
  196. .prn_1:
  197.         cinvoke con.printf, message_dbg_not_found, buff
  198.  
  199. .prn_stp:
  200.         pop eax
  201. .prn_end:
  202.         xor eax, eax                                    ;; When we check is proramme launched we are checking for eax
  203.         xchg esi, [esp]
  204.         mov edi, buff
  205. .copy_path:
  206.         lodsb
  207.         cmp al, ';'
  208.         je .copy_file
  209.         or al, al
  210.         je exit
  211.         stosb
  212.         jmp .copy_path
  213.  
  214. .copy_file:
  215.         xchg esi, [esp]
  216.         push esi
  217. .cp_file_loop:
  218.         lodsb
  219.         or al, al
  220.         je .copy_end
  221.         cmp al, ' '
  222.         je .copy_end
  223.         stosb
  224.         jmp .cp_file_loop
  225.  
  226. .copy_end:
  227.         pop esi
  228.         xor al, al
  229.         stosb
  230.  
  231.         ;; Try to launch
  232.  
  233.         mov dword [LaunchStruct.Function], 7
  234.         push dword [prog_args]
  235.         pop dword [LaunchStruct.Arguments]
  236.         mov dword [LaunchStruct.Flags], 0
  237.         mov dword [LaunchStruct.Zero], 0
  238.         mov dword [LaunchStruct.FileNameP], buff
  239.         mcall 70, LaunchStruct
  240.         cmp eax, 0
  241.         jl .loop
  242.  
  243. ;;--------------------------------------------------------------------------------------------------
  244. ;; Exit
  245. exit:
  246.         mov dword [tid], eax
  247.         ;; If console is present we should write some info
  248.         mov al, byte [debug_option]
  249.         cmp al, DEBUG_CONSOLE
  250.         jne .kobra
  251.  
  252. .write_console:
  253.         mov eax, dword [tid]
  254.         test eax, eax
  255.         jz .write_error
  256. .write_launched:
  257.         cinvoke con.printf, message_ok, buff, eax, eax
  258.         jmp .wr_end
  259. .write_error:
  260.         pop edi
  261.         cinvoke con.printf, message_error, edi
  262. .wr_end:
  263.         invoke con.exit, 0
  264.  
  265. .kobra:
  266.         mov al, byte [kobra_use]
  267.         test al, al
  268.         je .close
  269.        
  270. .register:
  271.         mov dword [IPC_area], buff
  272.         call IPC_init
  273. ;       jnz .close
  274.        
  275.         mov dword [thread_find_buff], another_buff
  276.        
  277.         call kobra_register
  278.        
  279.         test eax, eax
  280.         jnz .close
  281.        
  282.         ;; Prepare message
  283.         mov dword [kobra_message], KOBRA_MESSAGE_LAUNCH_STATE
  284.        
  285.         mov eax, dword [tid]
  286.         mov dword [kobra_message+4], eax
  287.        
  288. .kobra_send:
  289.         stdcall kobra_send_message, kobra_group_launch_reactive, kobra_message, 8
  290.  
  291. .close:
  292.         mcall -1
  293.  
  294. ;; End of code
  295. ;;--------------------------------------------------------------------------------------------------
  296.  
  297. ;;--------------------------------------------------------------------------------------------------
  298. ;; Imports
  299. align 16
  300. importTable:
  301.  
  302. library libini, 'libini.obj' ;,                           \
  303. ;        libio, 'libio.obj',                            \
  304.  
  305. import  libini, \
  306.         ini.get_str        ,'ini_get_str', \
  307. \;        ini.set_str      ,'ini_set_str', \
  308.         ini.get_int        ,'ini_get_int', \
  309. \;        ini.set_int      ,'ini_set_int', \
  310. \;        ini.get_color    ,'ini_get_color', \
  311. \;        ini.set_color    ,'ini_set_color', \
  312.         ini.get_option_str ,'ini_get_option_str', \
  313.         ini.get_bool       ,'ini_get_bool';,\
  314.  
  315. ;import  libio, \
  316. ;        file_find_first,'file_find_first', \
  317. ;        file_find_next ,'file_find_next', \
  318. ;        file_find_close,'file_find_close', \
  319. ;        file_size      ,'file_size', \
  320. ;        file_open      ,'file_open', \
  321. ;        file_read      ,'file_read', \
  322. ;        file_write     ,'file_write', \
  323. ;        file_seek      ,'file_seek', \
  324. ;        file_tell      ,'file_tell', \
  325. ;        file_eof?      ,'file_eof?', \
  326. ;        file_truncate  ,'file_truncate', \
  327. ;        file_close     ,'file_close'
  328.  
  329. consoleImport:
  330. library                                                 \
  331.         conlib, 'console.obj'
  332.  
  333. import conlib,\
  334.         con.init,         'con_init',\
  335.         con.exit,         'con_exit',\
  336.         con.printf,       'con_printf' ;,\
  337. ;        con.write_asciiz, 'con_write_asciiz'
  338.  
  339. ;;--------------------------------------------------------------------------------------------------
  340. ;; Data
  341. align 16
  342. APP_DATA:
  343.  
  344. ;; Window title
  345. window_title:
  346.         db APP_NAME, ' ', APP_VERSION, 0
  347.  
  348. ;; Messages
  349. message_dbg_not_found:
  350.         db '%s not found', 10, 0
  351.  
  352. message_error:
  353.         db 'File (%s) not found!', 0
  354.  
  355. message_ok:
  356.         db '%s loaded succesfully. PID: %d (0x%X)', 0
  357.  
  358. ;; Configuration path
  359. etc_cfg:
  360.         db '/sys/etc/'
  361. cfg_name:
  362.         db 'launch'
  363. cfg_ext:
  364.         db '.cfg', 0
  365.  
  366. ;; Strings in config file
  367. cfg_main:
  368.         db 'main', 0
  369. cfg_path:
  370.         db 'path', 0
  371. cfg_debug:
  372.         db 'debug', 0
  373. cfg_level:
  374.         db 'level', 0
  375. cfg_kobra:
  376.         db 'kobra', 0
  377. cfg_use:
  378.         db 'use', 0
  379.  
  380. ;; List of debug modes for parsing debug option
  381. debug_strings:
  382.         dd debug_no
  383.         dd debug_console
  384.         dd 0
  385.  
  386. debug_no:
  387.         db 'no', 0
  388. debug_console:
  389.         db 'console', 0
  390.  
  391. ;; Empty string
  392. empty_str:
  393.         db 0
  394.  
  395. kobra_group_launch_reactive:
  396.         db 'launch_reactive', 0
  397.  
  398. ;;--------------------------------------------------------------------------------------------------
  399. ;; Configuration options
  400. debug_level:
  401.         db 0
  402.  
  403. ; debug_kobra:
  404. ;       db 0
  405.  
  406. ;; debug option (bool)
  407. debug_option:
  408.         db 0
  409.  
  410. kobra_use:
  411.         db 0
  412.  
  413. ;;--------------------------------------------------------------------------------------------------
  414. tid:
  415.         dd 0
  416.  
  417. LaunchStruct FileInfoRun
  418.  
  419. args: db 0
  420. APP_END:
  421. rb 255
  422.  
  423. prog_args:
  424.         rd 1
  425. path:
  426.         rb 1024
  427.  
  428. ;; Search path will be here
  429. search_path:
  430.         rb PATH_MAX_LEN
  431.  
  432. ;; Buffer
  433. buff:
  434.         rb BUFF_SIZE
  435.  
  436. another_buff:
  437.         rb 0x1000
  438.  
  439. kobra_message:
  440.         rb 0x100
  441.  
  442. rb 0x1000       ;; 4 Kb stack
  443. APP_STACK:
  444. MEM_END:
  445.