]> git.sur5r.net Git - cc65/blob - libsrc/telestrat/mainargs.s
remove TABs
[cc65] / libsrc / telestrat / mainargs.s
1 ;
2 ; 2003-03-07, Ullrich von Bassewitz
3 ; 2011-01-28, Stefan Haubenthal
4 ; 2014-09-10, Greg King
5 ;
6 ; Set up arguments for main
7 ;
8
9         .constructor    initmainargs, 24
10         .import         __argc, __argv
11         .import         ptr1
12         .include        "telestrat.inc"
13         .macpack        generic
14
15 MAXARGS  = 10                   ; Maximum number of arguments allowed
16
17
18
19
20
21 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
22 ; statement.  Save the "most-recent filename" as argument #0.
23 initmainargs:
24
25         ldx     #0      ; Limit the length
26 L0:     lda     BUFEDT,x
27         beq     L3
28         cmp     #' '
29         bne     L1
30         lda     #0
31         beq     L3
32 L1:     sta     name,x
33         inx
34         cpx     #FNAME_LEN
35         bne     L0
36         lda     #0
37 L3:
38         sta     name,x
39         inc     __argc          ; argc always is equal to, at least, 1
40
41
42         ldy     #1 * 2          ; Point to second argv slot
43
44 next:   lda     BUFEDT,x
45         beq     done            ; End of line reached
46         inx
47         cmp     #' '            ; Skip leading spaces
48         beq     next
49
50 found:  cmp     #'"'            ; Is the argument quoted?
51         beq     setterm         ; Jump if so
52         dex                     ; Reset pointer to first argument character
53
54         lda     #' '            ; A space ends the argument
55 setterm:sta     term            ; Set end of argument marker
56
57 ; Now, store a pointer, to the argument, into the next slot.
58
59         txa                     ; Get low byte
60         clc
61         adc     #<BUFEDT
62         bcc     L4
63         inc     L5+1
64 L4:
65         sta     argv,y          ; argv[y]=&arg
66 L5:
67         lda     #>BUFEDT
68         sta     argv+1,y
69         iny
70         iny
71         inc     __argc          ; Found another arg
72
73 ; Search for the end of the argument
74
75
76
77 argloop:lda     BUFEDT,x
78         beq     done
79         inx
80         cmp     term
81         bne     argloop
82
83 ; We've found the end of the argument. X points one character behind it, and
84 ; A contains the terminating character. To make the argument a valid C string,
85 ; replace the terminating character by a zero.
86
87         lda     #0
88         sta     BUFEDT-1,x
89
90 ; Check if the maximum number of command line arguments is reached. If not,
91 ; parse the next one.
92
93         lda     __argc          ; Get low byte of argument count
94         cmp     #MAXARGS        ; Maximum number of arguments reached?
95         bcc     next            ; Parse next one if not
96
97
98 done:   lda     #<argv
99         ldx     #>argv
100         sta     __argv
101         stx     __argv + 1
102         rts
103
104
105 .segment        "INIT"
106
107 term:   .res    1
108
109
110 .data
111
112 name:   .res    FNAME_LEN + 1
113 args:   .res    SCREEN_XSIZE * 2 - 1
114
115 param_found:
116                 .res 1
117 ; char* argv[MAXARGS+1]={name};
118 argv:
119                 .addr   name
120         .res    MAXARGS * 2