]> git.sur5r.net Git - cc65/blob - libsrc/cbm610/mainargs.s
Protect random counter against ProDOS.
[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-09-10, 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 MAXARGS  = 10                   ; Maximum number of arguments allowed
35 REM      = $8f                  ; BASIC token-code
36 NAME_LEN = 16                   ; Maximum length of command-name
37
38 ; Get possible command-line arguments. Goes into the special ONCE segment,
39 ; which may be reused after the startup code is run.
40 ;
41 .segment        "ONCE"
42
43 initmainargs:
44
45 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
46 ; statement.  Save the "most-recent filename" as argument #0.
47
48         jsr     sys_bank
49         ldy     #FNAM
50         lda     (sysp0),y       ; Get file-name pointer from system bank
51         sta     ptr1
52         iny
53         lda     (sysp0),y
54         sta     ptr1+1
55         iny                     ; FNAM_BANK
56         lda     (sysp0),y
57         tax
58         ldy     #FNAM_LEN
59         lda     (sysp0),y
60         tay
61         lda     #0              ; The terminating NUL character
62         stx     IndReg          ; Look for name in correct bank
63         cpy     #NAME_LEN + 1
64         blt     L1
65         ldy     #NAME_LEN       ; Limit the length
66         bne     L1              ; Branch always
67 L0:     lda     (ptr1),y
68 L1:     sta     name,y
69         dey
70         bpl     L0
71         jsr     restore_bank
72         inc     __argc          ; argc always is equal to at least 1
73
74 ; Find a "rem" token.
75
76         ldx     #0
77 L2:     lda     BASIC_BUF,x
78         bze     done            ; No "rem," no args.
79         inx
80         cmp     #REM
81         bne     L2
82         ldy     #1 * 2
83
84 ; Find the next argument.
85
86 next:   lda     BASIC_BUF,x
87         bze     done            ; End of line reached
88         inx
89         cmp     #' '            ; Skip leading spaces
90         beq     next
91
92 ; Found start of next argument. We've incremented the pointer in X already, so
93 ; it points to the second character of the argument. That is useful because we
94 ; will check now for a quoted argument; in which case, we will have to skip that
95 ; first character.
96
97 found:  cmp     #'"'            ; Is the argument quoted?
98         beq     setterm         ; Jump if so
99         dex                     ; Reset pointer to first argument character
100         lda     #' '            ; A space ends the argument
101 setterm:sta     term            ; Set end-of-argument marker
102
103 ; Now, store a pointer to the argument into the next slot.
104
105         txa                     ; Get low byte
106         add     #<BASIC_BUF
107         sta     argv,y          ; argv[y]= &arg
108         lda     #>$0000
109         adc     #>BASIC_BUF
110         sta     argv+1,y
111         iny
112         iny
113         inc     __argc          ; Found another arg
114
115 ; Search for the end of the argument.
116
117 argloop:lda     BASIC_BUF,x
118         bze     done
119         inx
120         cmp     term
121         bne     argloop
122
123 ; We've found the end of the argument. X points one character behind it, and
124 ; A contains the terminating character. To make the argument a valid C string,
125 ; replace the terminating character by a zero.
126
127         lda     #$00
128         sta     BASIC_BUF-1,x
129
130 ; Check if the maximum number of command-line arguments is reached. If not,
131 ; parse the next one.
132
133         lda     __argc          ; Get low byte of argument count
134         cmp     #MAXARGS        ; Maximum number of arguments reached?
135         blt     next            ; Parse next one if not
136
137 ; (The last vector in argv[] already is NULL.)
138
139 done:   lda     #<argv
140         ldx     #>argv
141         sta     __argv
142         stx     __argv + 1
143         rts
144
145 .segment        "INIT"
146
147 term:   .res    1
148 name:   .res    NAME_LEN + 1
149
150 .data
151
152 ; char* argv[MAXARGS+1]={name};
153 argv:   .addr   name
154         .res    MAXARGS * 2