]> git.sur5r.net Git - cc65/blob - libsrc/telemon30/mainargs.s
Correcting spaces
[cc65] / libsrc / telemon30 / 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        "telemon30.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       ;  lda     #0              ; The terminating NUL character
27        ; beq     L1              ; Branch always
28 L0:     lda     BUFEDT,x
29                 beq     L3
30                 cmp     #' '
31                 bne     L1
32                 lda     #0
33                 beq     L3
34 L1:     sta     name,x
35         inx
36                 cpx     #FNAME_LEN
37         bne     L0
38                 lda     #0
39 L3:             
40                 sta     name,x
41         inc     __argc          ; argc always is equal to, at least, 1
42
43
44
45                 
46                 
47         ldy     #1 * 2          ; Point to second argv slot
48                 
49 next:   lda     BUFEDT,x
50         beq     done            ; End of line reached
51         inx
52         cmp     #' '            ; Skip leading spaces
53         beq     next            
54
55                 
56
57                 
58                 
59 found:  cmp     #'"'            ; Is the argument quoted?
60         beq     setterm         ; Jump if so
61         dex                     ; Reset pointer to first argument character
62                 
63         
64         lda     #' '            ; A space ends the argument
65 setterm:sta     term            ; Set end of argument marker
66
67 ; Now, store a pointer, to the argument, into the next slot.
68
69         txa                     ; Get low byte
70                 clc
71                 adc     #<BUFEDT
72                 bcc     L4
73         inc     L5+1
74 L4:             
75                 ;add     #<args
76         sta     argv,y          ; argv[y]=&arg
77 L5:             
78         lda     #>BUFEDT
79         ;adc     #>args
80         sta     argv+1,y
81         iny
82         iny
83         inc     __argc          ; Found another arg
84
85 ; Search for the end of the argument
86
87
88
89 argloop:lda     BUFEDT,x
90         beq     done
91         inx
92         cmp     term
93         bne     argloop
94
95 ; We've found the end of the argument. X points one character behind it, and
96 ; A contains the terminating character. To make the argument a valid C string,
97 ; replace the terminating character by a zero.
98
99         lda     #0
100         sta     BUFEDT-1,x
101
102 ; Check if the maximum number of command line arguments is reached. If not,
103 ; parse the next one.
104
105         lda     __argc          ; Get low byte of argument count
106         cmp     #MAXARGS        ; Maximum number of arguments reached?
107         bcc     next            ; Parse next one if not         
108                 
109         
110                 
111                 
112 done:   lda     #<argv
113         ldx     #>argv
114         sta     __argv
115         stx     __argv + 1
116         rts
117                 
118                 
119                 
120 .segment        "INIT"
121
122 term:   .res    1
123
124
125 .data
126
127 name:   .res    FNAME_LEN + 1
128 args:   .res    SCREEN_XSIZE * 2 - 1
129
130 ptr_current:
131         .res 2
132 param_found:
133                 .res 1
134 ; char* argv[MAXARGS+1]={name};
135 argv:  
136                 .addr   name
137         .res    MAXARGS * 2