Subversion Repositories Kolibri OS

Rev

Rev 1050 | 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. APP_NAME fix 'Launch'
  21. APP_VERSION fix '0.1.3'
  22.  
  23. use32
  24. org 0x0
  25.  
  26. db 'MENUET01'
  27. dd 0x01
  28. dd START
  29. dd APP_END
  30. dd MEM_END
  31. dd APP_STACK
  32. dd args
  33. dd path
  34.  
  35. define PATH_MAX_LEN 1024
  36. define DEBUG_MAX_LEN 8
  37. define DEBUG_DEFAULT debug_no_num
  38. define BUFF_SIZE 1024
  39.  
  40. include 'macros.inc'
  41. include 'proc32.inc'
  42. include 'libio.inc'
  43. include 'mem.inc'
  44. include 'dll.inc'
  45.  
  46. START:
  47.         ;; Initialize process heap
  48.         mcall 68,11
  49.         test eax, eax
  50.         jz exit
  51.  
  52.         ;; Import modules
  53.         stdcall dll.Load,importTable
  54.         test eax, eax
  55.         jnz exit
  56.  
  57. read_ini:
  58.         ;; First read config in /sys/etc
  59.         invoke ini.get_str, etc_cfg, cfg_main, cfg_path, search_path, PATH_MAX_LEN, empty_str
  60.  
  61.         ;; Next, read config from current directory
  62.         ;; Find end of path string
  63. .find_path_eol:
  64.         mov edi, path
  65.         mov ecx, PATH_MAX_LEN
  66.         xor al, al
  67.         cld
  68.         repne scasb
  69.  
  70.         ;; Append ext to run path (NOTE: this work only when config file has name <launch-file-name>.cfg and ext size is dword)
  71.         mov eax, dword [cfg_ext]
  72.         dec edi
  73.         mov dword [edi], eax
  74.         mov byte [edi+5], 0
  75.  
  76.         ;; Currently there is no checking for repeating pathes, so we should only concatenate two strings
  77.         ;; So we need to find end of current search_path string
  78. .find_search_eol:
  79.         mov edi, search_path
  80.         mov ecx, PATH_MAX_LEN
  81.         xor al, al
  82.         ;cld
  83.         repne scasb
  84.         dec edi
  85.  
  86.         ;; Now we need to correct buffer length
  87.         mov eax, path+PATH_MAX_LEN
  88.         sub eax, edi
  89.         ;; Read ini
  90.         invoke ini.get_str, path, cfg_main, cfg_path, edi, PATH_MAX_LEN, empty_str
  91.  
  92. read_ini_debug:
  93.         ;; Read debug options from config files
  94.         invoke ini.get_str, etc_cfg, cfg_debug, cfg_debug, debug_option, DEBUG_MAX_LEN, DEBUG_DEFAULT
  95.         invoke ini.get_str, path, cfg_debug, cfg_debug, debug_option, DEBUG_MAX_LEN, debug_option
  96.  
  97.         ;; Now convert debug_option from string to number of debug mode
  98.         mov ebx, modes-4
  99. .convert:
  100.         mov al, byte [debug_option]
  101.         cmp al, 10
  102.         jbe .converted
  103.  
  104. .convert_nxt:
  105.         add ebx, 4
  106.         mov esi, dword [ebx]
  107.         test esi, esi
  108.         je .set_default                         ;; String is incorrect, so set default
  109.         mov edi, debug_option
  110. .conv_loop:
  111.         mov al, byte [esi]
  112.         cmp al, byte [edi]
  113.         jne .convert_nxt                        ;; Not equal, so try next
  114.         test al, al
  115.         je .found                               ;; Equal, end of loop
  116.         inc esi
  117.         inc edi
  118.         jmp .conv_loop
  119.  
  120. .set_default:
  121.         mov al, byte [DEBUG_DEFAULT]
  122.         mov byte [debug_option], al
  123.         jmp .converted
  124.  
  125. .found:
  126.         sub ebx, modes
  127.         shr ebx, 2
  128.         add ebx, modes_nums
  129.         mov al, byte [ebx]
  130.         mov byte [debug_option], al
  131.  
  132. .converted:
  133.         dec al
  134.         test al, al
  135.         je .ok
  136.         dec al
  137.         test al, al
  138.         je .con_init
  139.  
  140. .noconsole:
  141.         mov al, 1
  142.         mov byte [debug_option], al
  143.         jmp .ok
  144.  
  145. .con_init:
  146.         stdcall dll.Load, consoleImport
  147.         test eax, eax
  148.         jnz .noconsole
  149.         invoke con.init, -1, -1, -1, -1, WinTitle
  150.  
  151. .read_level:
  152.         invoke ini.get_int, etc_cfg, cfg_debug, cfg_level, 0
  153.         invoke ini.get_int, path, cfg_debug, cfg_level, eax
  154.         mov dword [debug_level], eax
  155. .ok:
  156.  
  157. parse_args:
  158.         ;; Now parse command line arguments
  159.         ;; TODO: use optparse library
  160.         ;; Currently the only argument to parse is program name
  161.  
  162. .skip_spaces:
  163.         mov ecx, -1
  164.         mov edi, args
  165.         mov al, ' '
  166.         ;cld
  167.         repe scasb
  168.  
  169.         push edi
  170.  
  171.         mov ecx, -1
  172.         repne scasb
  173.         mov dword [prog_args], edi
  174.  
  175.         pop edi
  176.         dec edi
  177.         ;; Now edi = program name
  178.  
  179.         ;; End of preparations! Now we can find file and launch it.
  180. search_file:
  181.         push edi
  182.         mov esi, search_path
  183.         xchg esi, [esp]
  184. .loop:
  185.         or dl, dl
  186.         je .prn_dbg
  187.         xor dl, dl
  188.         jmp .prn_end
  189. .prn_dbg:
  190.         push eax
  191.         mov al, byte [debug_option]
  192.         dec al
  193.         test al, al
  194.         je .prn_stp
  195.         mov eax, dword [debug_level]
  196.         or eax, eax
  197.         je .prn_stp
  198.         dec eax
  199.         or eax, eax
  200.         je .prn_1
  201. .prn_1:
  202.         cinvoke con.printf, message_dbg_not_found, buff
  203.  
  204. .prn_stp:
  205.         pop eax
  206. .prn_end:
  207.         xor eax, eax                                    ;; When we check is proramme launched we are checking for eax
  208.         xchg esi, [esp]
  209.         mov edi, buff
  210. .copy_path:
  211.         lodsb
  212.         cmp al, ';'
  213.         je .copy_file
  214.         or al, al
  215.         je exit
  216.         stosb
  217.         jmp .copy_path
  218.  
  219. .copy_file:
  220.         xchg esi, [esp]
  221.         push esi
  222. .cp_file_loop:
  223.         lodsb
  224.         or al, al
  225.         je .copy_end
  226.         cmp al, ' '
  227.         je .copy_end
  228.         stosb
  229.         jmp .cp_file_loop
  230.  
  231. .copy_end:
  232.         pop esi
  233.         xor al, al
  234.         stosb
  235.  
  236.         ;; Try to launch
  237.  
  238.         mov dword [LaunchStruct.Function], 7
  239.         push dword [prog_args]
  240.         pop dword [LaunchStruct.Arguments]
  241.         mov dword [LaunchStruct.Flags], 0
  242.         mov dword [LaunchStruct.Zero], 0
  243.         mov dword [LaunchStruct.FileNameP], buff
  244.         mcall 70, LaunchStruct
  245.         cmp eax, 0
  246.         jl .loop
  247.  
  248. exit:
  249.         push eax
  250.         ;; If console is present we should write some info
  251.         mov al, byte [debug_option]
  252.         cmp al, 2
  253.         je .write_console
  254.  
  255. .close:
  256.         mcall -1
  257.  
  258. .write_console:
  259.         pop eax
  260.         test eax, eax
  261.         jz .write_error
  262. .write_launched:
  263.         cinvoke con.printf, message_ok, buff, eax, eax
  264.         jmp .wr_end
  265. .write_error:
  266.         pop edi
  267.         cinvoke con.printf, message_error, edi
  268. .wr_end:
  269.         invoke con.exit, 0
  270.         jmp .close
  271.  
  272. align 16
  273. importTable:
  274. library                                                 \
  275.         libini, 'libini.obj';,                           \
  276. ;        libio, 'libio.obj',                            \
  277.  
  278. import  libini, \
  279.         ini.get_str  ,'ini.get_str', \
  280. \;        ini.set_str  ,'ini.set_str', \
  281.         ini.get_int  ,'ini.get_int';, \
  282. \;        ini.set_int  ,'ini.set_int', \
  283. ;        ini.get_color,'ini.get_color', \
  284. ;        ini.set_color,'ini.set_color'
  285.  
  286. ;import  libio, \
  287. ;        file.find_first,'file.find_first', \
  288. ;        file.find_next ,'file.find_next', \
  289. ;        file.find_close,'file.find_close', \
  290. ;        file.size      ,'file.size', \
  291. ;        file.open      ,'file.open', \
  292. ;        file.read      ,'file.read', \
  293. ;        file.write     ,'file.write', \
  294. ;        file.seek      ,'file.seek', \
  295. ;        file.tell      ,'file.tell', \
  296. ;        file.eof?      ,'file.eof?', \
  297. ;        file.truncate  ,'file.truncate', \
  298. ;        file.close     ,'file.close'
  299.  
  300. consoleImport:
  301. library                                                 \
  302.         conlib, 'console.obj'
  303.  
  304. import conlib, \
  305.         con.init,         'con.init',\
  306.         con.exit,         'con.exit',\
  307.         con.printf,       'con.printf';,\
  308. ;        con.write_asciiz, 'con.write_asciiz'
  309.  
  310. align 16
  311. APP_DATA:
  312.  
  313. WinTitle:
  314.         db APP_NAME, ' ', APP_VERSION, 0
  315.  
  316. message_dbg_not_found:
  317.         db '%s not found', 10, 0
  318.  
  319. message_error:
  320.         db 'File (%s) not found!', 0
  321.  
  322. message_ok:
  323.         db '%s loaded succesfully. PID: %d (0x%X)'
  324.  
  325. empty_str:
  326.         db 0
  327. etc_cfg:
  328.         db '/sys/etc/'
  329. cfg_name:
  330.         db 'launch'
  331. cfg_ext:
  332.         db '.cfg', 0
  333.  
  334. cfg_main:
  335.         db 'main', 0
  336. cfg_path:
  337.         db 'path', 0
  338. cfg_debug:
  339.         db 'debug', 0
  340. cfg_level:
  341.         db 'level', 0
  342.  
  343. modes:
  344.         dd debug_no
  345.         dd debug_console
  346.         dd 0
  347.  
  348. debug_no:
  349.         db 'no', 0
  350. debug_console:
  351.         db 'console', 0
  352.  
  353. modes_nums:
  354. debug_no_num:
  355.         db 1
  356.  
  357. debug_console_num:
  358.         db 2
  359.  
  360. debug_level:
  361.         dd 0
  362.  
  363. LaunchStruct FileInfoRun
  364.  
  365. args: db 0
  366. APP_END:
  367. rb 255
  368.  
  369. prog_args:
  370.         rd 1
  371. path:
  372.         rb 1024
  373.  
  374. search_path:
  375.         rb PATH_MAX_LEN
  376.  
  377. debug_option:
  378.         rb DEBUG_MAX_LEN
  379.  
  380. buff:
  381.         rb BUFF_SIZE
  382.  
  383. rb 0x1000       ;; 4 Kb stack
  384. APP_STACK:
  385. MEM_END:
  386.