]> git.sur5r.net Git - cc65/blob - libsrc/apple2/mainargs.s
Move constructor code into the INIT segment
[cc65] / libsrc / apple2 / mainargs.s
1 ; mainargs.s
2 ;
3 ; Ullrich von Bassewitz, 2003-03-07
4 ; Based on code from Stefan A. Haubenthal <polluks@web.de>, 2003-11-08
5 ; Greg King, 2003-05-18
6 ; Stefan Haubenthal, 2005-01-07
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 ; call2048
15 ; call2048 : rem
16 ; call2048:rem arg1 " arg 2 is quoted "  arg3 "" arg5
17 ;
18 ; "call" 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
28         .include        "apple2.inc"
29
30 ; Maximum number of arguments allowed in the argument table.
31 ; (An argument contains a comma, at least.)
32 ;
33 BASIC_BUF = $200
34 BASIC_BUF_LEN = 239
35 FNAM_LEN = $280
36 FNAM = $281
37
38 MAXARGS  = 10                   ; Maximum number of arguments allowed
39 REM      = $B2                  ; BASIC token-code
40 NAME_LEN = 15                   ; maximum length of command-name
41         
42 ; Validate sizes
43 .if     MAXARGS > (BASIC_BUF_LEN - 2)/2
44 .error  "Too many arguments"
45 .endif
46
47 ; Get possible command-line arguments. Goes into the special INIT segment,
48 ; which may be reused after the startup code is run
49
50 .segment        "INIT"
51
52 initmainargs:
53
54 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
55 ; statement.  Save the "most-recent filename" as argument #0.
56 ; Because the buffer, that we're copying into, was zeroed out,
57 ; we don't need to add a NUL character.
58 ;
59         ldy     FNAM_LEN
60         cpy     #NAME_LEN + 1
61         bcc     L1
62         ldy     #NAME_LEN - 1   ; limit the length
63 L0:     lda     FNAM,y
64         sta     name,y
65 L1:     dey
66         bpl     L0
67         inc     __argc          ; argc always is equal to, at least, 1
68
69 ; Find the "rem" token.
70
71         ldx     #0
72 L2:     lda     BASIC_BUF,x
73         beq     done            ; no "rem" no args.
74         inx
75         cmp     #REM
76         bne     L2
77         ldy     #1 * 2
78
79 ; Find the next argument.
80
81 next:   lda     BASIC_BUF,x
82         beq     done
83         inx
84         cmp     #' '            ; Skip leading spaces
85         beq     next            ;
86
87 ; Found start of next argument. We've incremented the pointer in X already, so
88 ; it points to the second character of the argument. This is useful since we
89 ; will check now for a quoted argument, in which case we will have to skip this
90 ; first character.
91
92 found:  cmp     #'"'            ; Is the argument quoted?
93         beq     setterm         ; Jump if so
94         dex                     ; Reset pointer to first argument character
95         lda     #' '            ; A space ends the argument
96 setterm:sta     term            ; Set end of argument marker
97
98 ; Now store a pointer to the argument into the next slot. Since the BASIC
99 ; input buffer is located at the start of a RAM page, no calculations are
100 ; necessary.
101
102         txa                     ; Get low byte
103         sta     argv,y          ; argv[y]= &arg
104         iny
105         lda     #>BASIC_BUF
106         sta     argv,y
107         iny
108         inc     __argc          ; Found another arg
109
110 ; Search for the end of the argument
111
112 argloop:lda     BASIC_BUF,x
113         beq     done
114         inx
115         cmp     term
116         bne     argloop
117
118 ; We've found the end of the argument. X points one character behind it, and
119 ; A contains the terminating character. To make the argument a valid C string,
120 ; replace the terminating character by a zero.
121
122         lda     #0
123         sta     BASIC_BUF-1,x
124
125 ; Check if the maximum number of command line arguments is reached. If not,
126 ; parse the next one.
127
128         lda     __argc          ; Get low byte of argument count
129         cmp     #MAXARGS        ; Maximum number of arguments reached?
130         bcc     next            ; Parse next one if not
131
132 ; (The last vector in argv[] already is NULL.)
133
134 done:   lda     #<argv
135         ldx     #>argv
136         sta     __argv
137         stx     __argv + 1
138         rts
139
140 ; These arrays are zeroed before initmainargs is called.
141 ; char  name[NAME_LEN+1];
142 ; char* argv[MAXARGS+1]={name};
143
144 .bss
145 term:   .res    1
146 name:   .res    NAME_LEN + 1
147
148 .data
149 argv:   .addr   name
150         .res    MAXARGS * 2