Subversion Repositories Kolibri OS

Rev

Rev 1042 | Rev 1236 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1042 vkos 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 .                                                   ;;
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'
1050 vkos 21
APP_VERSION fix '0.1.4'
1042 vkos 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 .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, ' '
1050 vkos 166
	xor bl, bl
1042 vkos 167
	;cld
168
	repe scasb
169
 
170
	push edi
171
 
172
	mov ecx, -1
1050 vkos 173
@@:
174
	scasb
175
	je @f
176
	xchg al, bl
177
	dec edi
178
	scasb
179
	jne @b
180
@@:
1042 vkos 181
	mov dword [prog_args], edi
182
 
183
	pop edi
184
	dec edi
185
	;; Now edi = program name
186
 
187
	;; End of preparations! Now we can find file and launch it.
188
search_file:
189
	push edi
190
	mov esi, search_path
191
	xchg esi, [esp]
192
.loop:
193
	or dl, dl
194
	je .prn_dbg
195
	xor dl, dl
196
	jmp .prn_end
197
.prn_dbg:
198
	push eax
199
	mov al, byte [debug_option]
200
	dec al
201
	test al, al
202
	je .prn_stp
203
	mov eax, dword [debug_level]
204
	or eax, eax
205
	je .prn_stp
206
	dec eax
207
	or eax, eax
208
	je .prn_1
209
.prn_1:
210
	cinvoke con.printf, message_dbg_not_found, buff
211
 
212
.prn_stp:
213
	pop eax
214
.prn_end:
215
	xor eax, eax					;; When we check is proramme launched we are checking for eax
216
	xchg esi, [esp]
217
	mov edi, buff
218
.copy_path:
219
	lodsb
220
	cmp al, ';'
221
	je .copy_file
222
	or al, al
223
	je exit
224
	stosb
225
	jmp .copy_path
226
 
227
.copy_file:
228
	xchg esi, [esp]
229
	push esi
230
.cp_file_loop:
231
	lodsb
232
	or al, al
233
	je .copy_end
234
	cmp al, ' '
235
	je .copy_end
236
	stosb
237
	jmp .cp_file_loop
238
 
239
.copy_end:
240
	pop esi
241
	xor al, al
242
	stosb
243
 
244
	;; Try to launch
245
 
246
	mov dword [LaunchStruct.Function], 7
247
	push dword [prog_args]
248
	pop dword [LaunchStruct.Arguments]
249
	mov dword [LaunchStruct.Flags], 0
250
	mov dword [LaunchStruct.Zero], 0
251
	mov dword [LaunchStruct.FileNameP], buff
252
	mcall 70, LaunchStruct
253
	cmp eax, 0
254
	jl .loop
255
 
256
exit:
257
	push eax
258
	;; If console is present we should write some info
259
	mov al, byte [debug_option]
260
	cmp al, 2
261
	je .write_console
262
 
263
.close:
264
	mcall -1
265
 
266
.write_console:
267
	pop eax
268
	test eax, eax
269
	jz .write_error
270
.write_launched:
271
	cinvoke con.printf, message_ok, buff, eax, eax
272
	jmp .wr_end
273
.write_error:
274
	pop edi
275
	cinvoke con.printf, message_error, edi
276
.wr_end:
277
	invoke con.exit, 0
278
	jmp .close
279
 
280
align 16
281
importTable:
282
library 						\
283
	libini, 'libini.obj';,                           \
284
;        libio, 'libio.obj',                            \
285
 
286
import	libini, \
287
	ini.get_str  ,'ini.get_str', \
288
\;        ini.set_str  ,'ini.set_str', \
289
	ini.get_int  ,'ini.get_int';, \
290
\;        ini.set_int  ,'ini.set_int', \
291
;        ini.get_color,'ini.get_color', \
292
;        ini.set_color,'ini.set_color'
293
 
294
;import  libio, \
295
;        file.find_first,'file.find_first', \
296
;        file.find_next ,'file.find_next', \
297
;        file.find_close,'file.find_close', \
298
;        file.size      ,'file.size', \
299
;        file.open      ,'file.open', \
300
;        file.read      ,'file.read', \
301
;        file.write     ,'file.write', \
302
;        file.seek      ,'file.seek', \
303
;        file.tell      ,'file.tell', \
304
;        file.eof?      ,'file.eof?', \
305
;        file.truncate  ,'file.truncate', \
306
;        file.close     ,'file.close'
307
 
308
consoleImport:
309
library 						\
310
	conlib, 'console.obj'
311
 
312
import conlib, \
313
	con.init,	  'con.init',\
314
	con.exit,	  'con.exit',\
315
	con.printf,	  'con.printf';,\
316
;        con.write_asciiz, 'con.write_asciiz'
317
 
318
align 16
319
APP_DATA:
320
 
321
WinTitle:
322
	db APP_NAME, ' ', APP_VERSION, 0
323
 
324
message_dbg_not_found:
325
	db '%s not found', 10, 0
326
 
327
message_error:
328
	db 'File (%s) not found!', 0
329
 
330
message_ok:
331
	db '%s loaded succesfully. PID: %d (0x%X)'
332
 
333
empty_str:
334
	db 0
335
etc_cfg:
336
	db '/sys/etc/'
337
cfg_name:
338
	db 'launch'
339
cfg_ext:
340
	db '.cfg', 0
341
 
342
cfg_main:
343
	db 'main', 0
344
cfg_path:
345
	db 'path', 0
346
cfg_debug:
347
	db 'debug', 0
348
cfg_level:
349
	db 'level', 0
350
 
351
modes:
352
	dd debug_no
353
	dd debug_console
354
	dd 0
355
 
356
debug_no:
357
	db 'no', 0
358
debug_console:
359
	db 'console', 0
360
 
361
modes_nums:
362
debug_no_num:
363
	db 1
364
 
365
debug_console_num:
366
	db 2
367
 
368
debug_level:
369
	dd 0
370
 
371
LaunchStruct FileInfoRun
372
 
373
args: db 0
374
APP_END:
375
rb 255
376
 
377
prog_args:
378
	rd 1
379
path:
380
	rb 1024
381
 
382
search_path:
383
	rb PATH_MAX_LEN
384
 
385
debug_option:
386
	rb DEBUG_MAX_LEN
387
 
388
buff:
389
	rb BUFF_SIZE
390
 
391
rb 0x1000	;; 4 Kb stack
392
APP_STACK:
393
MEM_END: