Subversion Repositories Kolibri OS

Rev

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