]> git.sur5r.net Git - cc65/blob - libsrc/cbm610/mainargs.s
Merge pull request #130 from greg-king5/caps-lock
[cc65] / libsrc / cbm610 / mainargs.s
1 ; mainargs.s
2 ;
3 ; 2003-03-07, Ullrich von Bassewitz,
4 ;   based on code from Stefan A. Haubenthal, <polluks@web.de>
5 ; 2005-02-26, Ullrich von Bassewitz
6 ; 2014-04-02, Greg King
7 ;
8 ; Scan a group of arguments that are in BASIC's input-buffer.
9 ; Build an array that points to the beginning of each argument.
10 ; Send, to main(), that array and the count of the arguments.
11 ;
12 ; Command-lines look like these lines:
13 ;
14 ; run
15 ; run : rem
16 ; run:rem arg1 " arg 2 is quoted "  arg3 "" arg5
17 ;
18 ; "run" and "rem" are entokenned; the args. are not.  Leading and trailing
19 ; spaces outside of quotes are ignored.
20 ;
21 ; TO-DO:
22 ; - The "file-name" might be a path-name; don't copy the directory-components.
23 ; - Add a control-character quoting mechanism.
24
25         .constructor    initmainargs, 24
26         .import         __argc, __argv
27         .import         sys_bank, restore_bank
28         .import         sysp0:zp, ptr1:zp
29
30         .include        "cbm610.inc"
31         .macpack        generic
32
33
34
35 MAXARGS  = 10                   ; Maximum number of arguments allowed
36 REM      = $8f                  ; BASIC token-code
37 NAME_LEN = 16                   ; maximum length of command-name
38
39 ; Get possible command-line arguments. Goes into the special INIT segment,
40 ; which may be reused after the startup code is run.
41 ;
42 .segment        "INIT"
43
44 initmainargs:
45
46 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
47 ; statement.  Save the "most-recent filename" as argument #0.
48 ; Because the buffer, that we're copying into, was zeroed out,
49 ; we don't need to add a NUL character.
50 ;
51         jsr     sys_bank
52         ldy     #FNAM
53         lda     (sysp0),y       ; Get file-name pointer from system bank
54         sta     ptr1
55         iny
56         lda     (sysp0),y
57         sta     ptr1+1
58         iny                     ; FNAM_BANK
59         lda     (sysp0),y
60         tax
61         ldy     #FNAM_LEN
62         lda     (sysp0),y
63         tay
64         stx     IndReg          ; Look for name in correct bank
65         cpy     #NAME_LEN + 1
66         blt     L1
67         ldy     #NAME_LEN - 1   ; limit the length
68 L0:     lda     (ptr1),y
69         sta     name,y
70 L1:     dey
71         bpl     L0
72         jsr     restore_bank
73         inc     __argc          ; argc always is equal to at least 1
74
75 ; Find a "rem" token.
76 ;
77         ldx     #0
78 L2:     lda     BASIC_BUF,x
79         bze     done            ; no "rem," no args.
80         inx
81         cmp     #REM
82         bne     L2
83         ldy     #1 * 2
84
85 ; Find the next argument.
86 ;
87 next:   lda     BASIC_BUF,x
88         bze     done            ; End of line reached
89         inx
90         cmp     #' '            ; Skip leading spaces
91         beq     next            ;
92
93 ; Found start of next argument. We've incremented the pointer in X already, so
94 ; it points to the second character of the argument. That is useful because we
95 ; will check now for a quoted argument; in which case, we will have to skip that
96 ; first character.
97 ;
98 found:  cmp     #'"'            ; Is the argument quoted?
99         beq     setterm         ; Jump if so
100         dex                     ; Reset pointer to first argument character
101         lda     #' '            ; A space ends the argument
102 setterm:sta     term            ; Set end-of-argument marker
103
104 ; Now, store a pointer to the argument into the next slot.
105 ;
106         txa                     ; Get low byte
107         add     #<BASIC_BUF
108         sta     argv,y          ; argv[y]= &arg
109         lda     #>0
110         adc     #>BASIC_BUF
111         sta     argv+1,y
112         iny
113         iny
114         inc     __argc          ; Found another arg
115
116 ; Search for the end of the argument.
117 ;
118 argloop:lda     BASIC_BUF,x
119         bze     done
120         inx
121         cmp     term
122         bne     argloop
123
124 ; We've found the end of the argument. X points one character behind it, and
125 ; A contains the terminating character. To make the argument a valid C string,
126 ; replace the terminating character by a zero.
127 ;
128         lda     #0
129         sta     BASIC_BUF-1,x
130
131 ; Check if the maximum number of command-line arguments is reached. If not,
132 ; parse the next one.
133 ;
134         lda     __argc          ; Get low byte of argument count
135         cmp     #MAXARGS        ; Maximum number of arguments reached?
136         blt     next            ; Parse next one if not
137
138 ; (The last vector in argv[] already is NULL.)
139 ;
140 done:   lda     #<argv
141         ldx     #>argv
142         sta     __argv
143         stx     __argv + 1
144         rts
145
146 ; These arrays are zeroed before initmainargs is called.
147 ; char  name[16+1];
148 ; char* argv[MAXARGS+1]={name};
149 ;
150 .bss
151 term:   .res    1
152 name:   .res    NAME_LEN + 1
153
154 .data
155 argv:   .addr   name
156         .res    MAXARGS * 2, 0