1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
|
\ Copyright (c) 1999 Daniel C. Sobral <dcs@freebsd.org>
\ All rights reserved.
\
\ Redistribution and use in source and binary forms, with or without
\ modification, are permitted provided that the following conditions
\ are met:
\ 1. Redistributions of source code must retain the above copyright
\ notice, this list of conditions and the following disclaimer.
\ 2. Redistributions in binary form must reproduce the above copyright
\ notice, this list of conditions and the following disclaimer in the
\ documentation and/or other materials provided with the distribution.
\
\ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
\ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\ SUCH DAMAGE.
\
\ $Id$
\ Loader.rc support functions:
\
\ initialize_support ( -- ) initialize global variables
\ initialize ( addr len -- ) as above, plus load_conf_files
\ load_conf ( addr len -- ) load conf file given
\ include_conf_files ( -- ) load all conf files in load_conf_files
\ print_syntax_error ( -- ) print line and marker of where a syntax
\ error was detected
\ print_line ( -- ) print last line processed
\ load_kernel ( -- ) load kernel
\ load_modules ( -- ) load modules flagged
\
\ Exported structures:
\
\ string counted string structure
\ cell .addr string address
\ cell .len string length
\ module module loading information structure
\ cell module.flag should we load it?
\ string module.name module's name
\ string module.loadname name to be used in loading the module
\ string module.type module's type
\ string module.args flags to be passed during load
\ string module.beforeload command to be executed before load
\ string module.afterload command to be executed after load
\ string module.loaderror command to be executed if load fails
\ cell module.next list chain
\
\ Exported global variables;
\
\ string conf_files configuration files to be loaded
\ cell modules_options pointer to first module information
\ value verbose? indicates if user wants a verbose loading
\ value any_conf_read? indicates if a conf file was succesfully read
\
\ Other exported words:
\
\ strdup ( addr len -- addr' len) similar to strdup(3)
\ strcat ( addr len addr' len' -- addr len+len' ) similar to strcat(3)
\ s' ( | string' -- addr len | ) similar to s"
\ rudimentary structure support
\ Exception values
1 constant syntax_error
2 constant out_of_memory
3 constant free_error
4 constant set_error
5 constant read_error
6 constant open_error
7 constant exec_error
8 constant before_load_error
9 constant after_load_error
\ Crude structure support
: structure: create here 0 , 0 does> create @ allot ;
: member: create dup , over , + does> cell+ @ + ;
: ;structure swap ! ;
: sizeof ' >body @ state @ if postpone literal then ; immediate
: offsetof ' >body cell+ @ state @ if postpone literal then ; immediate
: ptr 1 cells member: ;
: int 1 cells member: ;
\ String structure
structure: string
ptr .addr
int .len
;structure
\ Module options linked list
structure: module
int module.flag
sizeof string member: module.name
sizeof string member: module.loadname
sizeof string member: module.type
sizeof string member: module.args
sizeof string member: module.beforeload
sizeof string member: module.afterload
sizeof string member: module.loaderror
ptr module.next
;structure
\ Global variables
string conf_files
create module_options sizeof module.next allot
create last_module_option sizeof module.next allot
0 value verbose?
\ Support string functions
: strdup ( addr len -- addr' len )
>r r@ allocate if out_of_memory throw then
tuck r@ move
r>
;
: strcat { addr len addr' len' -- addr len+len' }
addr' addr len + len' move
addr len len' +
;
: s'
[char] ' parse
state @ if
postpone sliteral
then
; immediate
\ Private definitions
vocabulary support-functions
only forth also support-functions definitions
\ Some control characters constants
9 constant tab
10 constant lf
\ Read buffer size
80 constant read_buffer_size
\ Standard suffixes
: load_module_suffix s" _load" ;
: module_loadname_suffix s" _name" ;
: module_type_suffix s" _type" ;
: module_args_suffix s" _flags" ;
: module_beforeload_suffix s" _before" ;
: module_afterload_suffix s" _after" ;
: module_loaderror_suffix s" _error" ;
\ Support operators
: >= < 0= ;
: <= > 0= ;
\ Assorted support funcitons
: free-memory free if free_error throw then ;
\ Assignment data temporary storage
string name_buffer
string value_buffer
\ File data temporary storage
string line_buffer
string read_buffer
0 value read_buffer_ptr
\ File's line reading function
0 value end_of_file?
variable fd
: skip_newlines
begin
read_buffer .len @ read_buffer_ptr >
while
read_buffer .addr @ read_buffer_ptr + c@ lf = if
read_buffer_ptr char+ to read_buffer_ptr
else
exit
then
repeat
;
: scan_buffer ( -- addr len )
read_buffer_ptr >r
begin
read_buffer .len @ r@ >
while
read_buffer .addr @ r@ + c@ lf = if
read_buffer .addr @ read_buffer_ptr + ( -- addr )
r@ read_buffer_ptr - ( -- len )
r> to read_buffer_ptr
exit
then
r> char+ >r
repeat
read_buffer .addr @ read_buffer_ptr + ( -- addr )
r@ read_buffer_ptr - ( -- len )
r> to read_buffer_ptr
;
: line_buffer_resize ( len -- len )
>r
line_buffer .len @ if
line_buffer .addr @
line_buffer .len @ r@ +
resize if out_of_memory throw then
else
r@ allocate if out_of_memory throw then
then
line_buffer .addr !
r>
;
: append_to_line_buffer ( addr len -- )
line_buffer .addr @ line_buffer .len @
2swap strcat
line_buffer .len !
drop
;
: read_from_buffer
scan_buffer ( -- addr len )
line_buffer_resize ( len -- len )
append_to_line_buffer ( addr len -- )
;
: refill_required?
read_buffer .len @ read_buffer_ptr =
end_of_file? 0= and
;
: refill_buffer
0 to read_buffer_ptr
read_buffer .addr @ 0= if
read_buffer_size allocate if out_of_memory throw then
read_buffer .addr !
then
fd @ read_buffer .addr @ read_buffer_size fread
dup -1 = if read_error throw then
dup 0= if true to end_of_file? then
read_buffer .len !
;
: reset_line_buffer
0 line_buffer .addr !
0 line_buffer .len !
;
: read_line
reset_line_buffer
skip_newlines
begin
read_from_buffer
refill_required?
while
refill_buffer
repeat
;
\ Conf file line parser:
\ <line> ::= <spaces><name><spaces>'='<spaces><value><spaces>[<comment>] |
\ <spaces>[<comment>]
\ <name> ::= <letter>{<letter>|<digit>|'_'}
\ <value> ::= '"'{<character_set>|'\'<anything>}'"' | <name>
\ <character_set> ::= ASCII 32 to 126, except '\' and '"'
\ <comment> ::= '#'{<anything>}
0 value parsing_function
0 value end_of_line
0 value line_pointer
: end_of_line?
line_pointer end_of_line =
;
: letter?
line_pointer c@ >r
r@ [char] A >=
r@ [char] Z <= and
r@ [char] a >=
r> [char] z <= and
or
;
: digit?
line_pointer c@ >r
r@ [char] 0 >=
r> [char] 9 <= and
;
: quote?
line_pointer c@ [char] " =
;
: assignment_sign?
line_pointer c@ [char] = =
;
: comment?
line_pointer c@ [char] # =
;
: space?
line_pointer c@ bl =
line_pointer c@ tab = or
;
: backslash?
line_pointer c@ [char] \ =
;
: underscore?
line_pointer c@ [char] _ =
;
: dot?
line_pointer c@ [char] . =
;
: skip_character
line_pointer char+ to line_pointer
;
: skip_to_end_of_line
end_of_line to line_pointer
;
: eat_space
begin
space?
while
skip_character
end_of_line? if exit then
repeat
;
: parse_name ( -- addr len )
line_pointer
begin
letter? digit? underscore? dot? or or or
while
skip_character
end_of_line? if
line_pointer over -
strdup
exit
then
repeat
line_pointer over -
strdup
;
: remove_backslashes { addr len | addr' len' -- addr' len' }
len allocate if out_of_memory throw then
to addr'
addr >r
begin
addr c@ [char] \ <> if
addr c@ addr' len' + c!
len' char+ to len'
then
addr char+ to addr
r@ len + addr =
until
r> drop
addr' len'
;
: parse_quote ( -- addr len )
line_pointer
skip_character
end_of_line? if syntax_error throw then
begin
quote? 0=
while
backslash? if
skip_character
end_of_line? if syntax_error throw then
then
skip_character
end_of_line? if syntax_error throw then
repeat
skip_character
line_pointer over -
remove_backslashes
;
: read_name
parse_name ( -- addr len )
name_buffer .len !
name_buffer .addr !
;
: read_value
quote? if
parse_quote ( -- addr len )
else
parse_name ( -- addr len )
then
value_buffer .len !
value_buffer .addr !
;
: comment
skip_to_end_of_line
;
: white_space_4
eat_space
comment? if ['] comment to parsing_function exit then
end_of_line? 0= if syntax_error throw then
;
: variable_value
read_value
['] white_space_4 to parsing_function
;
: white_space_3
eat_space
letter? digit? quote? or or if
['] variable_value to parsing_function exit
then
syntax_error throw
;
: assignment_sign
skip_character
['] white_space_3 to parsing_function
;
: white_space_2
eat_space
assignment_sign? if ['] assignment_sign to parsing_function exit then
syntax_error throw
;
: variable_name
read_name
['] white_space_2 to parsing_function
;
: white_space_1
eat_space
letter? if ['] variable_name to parsing_function exit then
comment? if ['] comment to parsing_function exit then
end_of_line? 0= if syntax_error throw then
;
: get_assignment
line_buffer .addr @ line_buffer .len @ + to end_of_line
line_buffer .addr @ to line_pointer
['] white_space_1 to parsing_function
begin
end_of_line? 0=
while
parsing_function execute
repeat
parsing_function ['] comment =
parsing_function ['] white_space_1 =
parsing_function ['] white_space_4 =
or or 0= if syntax_error throw then
;
\ Process line
: assignment_type? ( addr len -- flag )
name_buffer .addr @ name_buffer .len @
compare 0=
;
: suffix_type? ( addr len -- flag )
name_buffer .len @ over <= if 2drop false exit then
name_buffer .len @ over - name_buffer .addr @ +
over compare 0=
;
: loader_conf_files?
s" loader_conf_files" assignment_type?
;
: verbose_flag?
s" verbose_loading" assignment_type?
;
: execute?
s" exec" assignment_type?
;
: module_load?
load_module_suffix suffix_type?
;
: module_loadname?
module_loadname_suffix suffix_type?
;
: module_type?
module_type_suffix suffix_type?
;
: module_args?
module_args_suffix suffix_type?
;
: module_beforeload?
module_beforeload_suffix suffix_type?
;
: module_afterload?
module_afterload_suffix suffix_type?
;
: module_loaderror?
module_loaderror_suffix suffix_type?
;
: set_conf_files
conf_files .addr @ ?dup if
free-memory
then
value_buffer .addr @ c@ [char] " = if
value_buffer .addr @ char+ value_buffer .len @ 2 chars -
else
value_buffer .addr @ value_buffer .len @
then
strdup
conf_files .len ! conf_files .addr !
;
: append_to_module_options_list ( addr -- )
module_options @ 0= if
dup module_options !
last_module_option !
else
dup last_module_option @ module.next !
last_module_option !
then
;
: set_module_name ( addr -- )
name_buffer .addr @ name_buffer .len @
strdup
>r over module.name .addr !
r> swap module.name .len !
;
: yes_value?
value_buffer .addr @ value_buffer .len @
2dup s' "YES"' compare >r
2dup s' "yes"' compare >r
2dup s" YES" compare >r
s" yes" compare r> r> r> and and and 0=
;
: find_module_option ( -- addr | 0 )
module_options @
begin
dup
while
dup module.name dup .addr @ swap .len @
name_buffer .addr @ name_buffer .len @
compare 0= if exit then
module.next @
repeat
;
: new_module_option ( -- addr )
sizeof module allocate if out_of_memory throw then
dup sizeof module erase
dup append_to_module_options_list
dup set_module_name
;
: get_module_option ( -- addr )
find_module_option
?dup 0= if new_module_option then
;
: set_module_flag
name_buffer .len @ load_module_suffix nip - name_buffer .len !
yes_value? get_module_option module.flag !
;
: set_module_args
name_buffer .len @ module_args_suffix nip - name_buffer .len !
get_module_option module.args
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_module_loadname
name_buffer .len @ module_loadname_suffix nip - name_buffer .len !
get_module_option module.loadname
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_module_type
name_buffer .len @ module_type_suffix nip - name_buffer .len !
get_module_option module.type
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_module_beforeload
name_buffer .len @ module_beforeload_suffix nip - name_buffer .len !
get_module_option module.beforeload
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_module_afterload
name_buffer .len @ module_afterload_suffix nip - name_buffer .len !
get_module_option module.afterload
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_module_loaderror
name_buffer .len @ module_loaderror_suffix nip - name_buffer .len !
get_module_option module.loaderror
dup .addr @ ?dup if free-memory then
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
strdup
>r over .addr !
r> swap .len !
;
: set_environment_variable
name_buffer .len @
value_buffer .len @ +
5 chars +
allocate if out_of_memory throw then
dup 0 ( addr -- addr addr len )
s" set " strcat
name_buffer .addr @ name_buffer .len @ strcat
s" =" strcat
value_buffer .addr @ value_buffer .len @ strcat
['] evaluate catch if
2drop free drop
set_error throw
else
free-memory
then
;
: set_verbose
yes_value? to verbose?
;
: execute_command
value_buffer .addr @ value_buffer .len @
over c@ [char] " = if
2 chars - swap char+ swap
then
['] evaluate catch if exec_error throw then
;
: process_assignment
name_buffer .len @ 0= if exit then
loader_conf_files? if set_conf_files exit then
verbose_flag? if set_verbose exit then
execute? if execute_command exit then
module_load? if set_module_flag exit then
module_loadname? if set_module_loadname exit then
module_type? if set_module_type exit then
module_args? if set_module_args exit then
module_beforeload? if set_module_beforeload exit then
module_afterload? if set_module_afterload exit then
module_loaderror? if set_module_loaderror exit then
set_environment_variable
;
: free_buffers
line_buffer .addr @ dup if free then
name_buffer .addr @ dup if free then
value_buffer .addr @ dup if free then
or or if free_error throw then
;
: reset_assignment_buffers
0 name_buffer .addr !
0 name_buffer .len !
0 value_buffer .addr !
0 value_buffer .len !
;
\ Higher level file processing
: process_conf
begin
end_of_file? 0=
while
reset_assignment_buffers
read_line
get_assignment
['] process_assignment catch
['] free_buffers catch
swap throw throw
repeat
;
: create_null_terminated_string { addr len -- addr' len }
len char+ allocate if out_of_memory throw then
>r
addr r@ len move
0 r@ len + c!
r> len
;
\ Interface to loading conf files
: load_conf ( addr len -- )
0 to end_of_file?
0 to read_buffer_ptr
create_null_terminated_string
over >r
fopen fd !
r> free-memory
fd @ -1 = if open_error throw then
['] process_conf catch
fd @ fclose
throw
;
: initialize_support
0 read_buffer .addr !
0 conf_files .addr !
0 module_options !
0 last_module_option !
0 to verbose?
;
: print_line
line_buffer .addr @ line_buffer .len @ type cr
;
: print_syntax_error
line_buffer .addr @ line_buffer .len @ type cr
line_buffer .addr @
begin
line_pointer over <>
while
bl emit
char+
repeat
drop
." ^" cr
;
\ Depuration support functions
only forth definitions also support-functions
: test-file
['] load_conf catch dup .
syntax_error = if cr print_syntax_error then
;
: show-module-options
module_options @
begin
?dup
while
." Name: " dup module.name dup .addr @ swap .len @ type cr
." Path: " dup module.loadname dup .addr @ swap .len @ type cr
." Type: " dup module.type dup .addr @ swap .len @ type cr
." Flags: " dup module.args dup .addr @ swap .len @ type cr
." Before load: " dup module.beforeload dup .addr @ swap .len @ type cr
." After load: " dup module.afterload dup .addr @ swap .len @ type cr
." Error: " dup module.loaderror dup .addr @ swap .len @ type cr
." Status: " dup module.flag @ if ." Load" else ." Don't load" then cr
module.next @
repeat
;
only forth also support-functions definitions
\ Variables used for processing multiple conf files
string current_file_name
variable current_conf_files
\ Indicates if any conf file was succesfully read
0 value any_conf_read?
\ loader_conf_files processing support functions
: set_current_conf_files
conf_files .addr @ current_conf_files !
;
: get_conf_files
conf_files .addr @ conf_files .len @ strdup
;
: recurse_on_conf_files?
current_conf_files @ conf_files .addr @ <>
;
: skip_leading_spaces { addr len ptr -- addr len ptr' }
begin
ptr len = if addr len ptr exit then
addr ptr + c@ bl =
while
ptr char+ to ptr
repeat
addr len ptr
;
: get_file_name { addr len ptr -- addr len ptr' addr' len' || 0 }
ptr len = if
addr free abort" Fatal error freeing memory"
0 exit
then
ptr >r
begin
addr ptr + c@ bl <>
while
ptr char+ to ptr
ptr len = if
addr len ptr addr r@ + ptr r> - exit
then
repeat
addr len ptr addr r@ + ptr r> -
;
: get_next_file ( addr len ptr -- addr len ptr' addr' len' | 0 )
skip_leading_spaces
get_file_name
;
: set_current_file_name
over current_file_name .addr !
dup current_file_name .len !
;
: print_current_file
current_file_name .addr @ current_file_name .len @ type
;
: process_conf_errors
dup 0= if true to any_conf_read? drop exit then
>r 2drop r>
dup syntax_error = if
." Warning: syntax error on file " print_current_file cr
print_syntax_error drop exit
then
dup set_error = if
." Warning: bad definition on file " print_current_file cr
print_line drop exit
then
dup read_error = if
." Warning: error reading file " print_current_file cr drop exit
then
dup open_error = if
verbose? if ." Warning: unable to open file " print_current_file cr then
drop exit
then
dup free_error = abort" Fatal error freeing memory"
dup out_of_memory = abort" Out of memory"
throw \ Unknown error -- pass ahead
;
\ Process loader_conf_files recursively
\ Interface to loader_conf_files processing
: include_conf_files
set_current_conf_files
get_conf_files 0
begin
get_next_file ?dup
while
set_current_file_name
['] load_conf catch
process_conf_errors
recurse_on_conf_files? if recurse then
repeat
;
\ Module loading functions
: load_module?
module.flag @
;
: load_parameters ( addr -- addr addrN lenN ... addr1 len1 N )
dup >r
r@ module.args .addr @ r@ module.args .len @
r@ module.loadname .len @ if
r@ module.loadname .addr @ r@ module.loadname .len @
else
r@ module.name .addr @ r@ module.name .len @
then
r@ module.type .len @ if
r@ module.type .addr @ r@ module.type .len @
s" -t "
4 ( -t type name flags )
else
2 ( name flags )
then
r> drop
;
: before_load ( addr -- addr )
dup module.beforeload .len @ if
dup module.beforeload .addr @ over module.beforeload .len @
['] evaluate catch if before_load_error throw then
then
;
: after_load ( addr -- addr )
dup module.afterload .len @ if
dup module.afterload .addr @ over module.afterload .len @
['] evaluate catch if after_load_error throw then
then
;
: load_error ( addr -- addr )
dup module.loaderror .len @ if
dup module.loaderror .addr @ over module.loaderror .len @
evaluate \ This we do not intercept so it can throw errors
then
;
: pre_load_message ( addr -- addr )
verbose? if
dup module.name .addr @ over module.name .len @ type
." ..."
then
;
: load_error_message verbose? if ." failed!" cr then ;
: load_succesful_message verbose? if ." ok" cr then ;
: load_module
load_parameters load
;
: process_module ( addr -- addr )
pre_load_message
before_load
begin
['] load_module catch if
dup module.loaderror .len @ if
load_error \ Command should return a flag!
else
load_error_message true \ Do not retry
then
else
after_load
load_succesful_message true \ Succesful, do not retry
then
until
;
: process_module_errors ( addr ior -- )
dup before_load_error = if
drop
." Module "
dup module.name .addr @ over module.name .len @ type
dup module.loadname .len @ if
." (" dup module.loadname .addr @ over module.loadname .len @ type ." )"
then
cr
." Error executing "
dup module.beforeload .addr @ over module.afterload .len @ type cr
abort
then
dup after_load_error = if
drop
." Module "
dup module.name .addr @ over module.name .len @ type
dup module.loadname .len @ if
." (" dup module.loadname .addr @ over module.loadname .len @ type ." )"
then
cr
." Error executing "
dup module.afterload .addr @ over module.afterload .len @ type cr
abort
then
throw \ Don't know what it is all about -- pass ahead
;
\ Module loading interface
: load_modules ( -- ) ( throws: abort & user-defined )
module_options @
begin
?dup
while
dup load_module? if
['] process_module catch
process_module_errors
then
module.next @
repeat
;
\ Additional functions used in "start"
: initialize ( addr len -- )
initialize_support
strdup conf_files .len ! conf_files .addr !
;
: load_kernel ( -- ) ( throws: abort )
s" load ${kernel} ${kernel_options}" ['] evaluate catch
if s" echo Unable to load kernel: ${kernel_name}" evaluate abort then
;
\ Go back to straight forth vocabulary
only forth also definitions
|