]> git.sur5r.net Git - cc65/blob - libsrc/apple2/mainargs.s
New loadable mouse drivers
[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 ;
7 ; Scan a group of arguments that are in BASIC's input-buffer.
8 ; Build an array that points to the beginning of each argument.
9 ; Send, to main(), that array and the count of the arguments.
10
11 ; Command-lines look like these lines:
12 ;
13 ; call2048
14 ; call2048 : rem  no arguments because no comma!
15 ; call2048:rem,arg1," arg 2" , arg 3 ,, arg5, ...
16 ;
17 ; "call" and "rem" are entokenned; the args. are not.  Leading spaces are
18 ; ignored; trailing spaces are included -- unless the argument was quoted.
19
20 ; TO-DO:
21 ; - The "file-name" might be a path-name; don't copy the directory-components.
22 ; - Add a control-character quoting mechanism.
23
24         .constructor    initmainargs, 24
25         .import         __argc, __argv
26
27         .include        "apple2.inc"
28
29 ; Maximum number of arguments allowed in the argument table.
30 ; (An argument contains a comma, at least.)
31 ;
32 BASIC_BUF = $200
33 BASIC_BUF_LEN = 239
34 FNAM_LEN = $280
35 FNAM = $281
36 MAXARGS  = BASIC_BUF_LEN - 2    ; (don't count REM and terminating '\0')
37
38 REM      = $b2                  ; BASIC token-code
39 NAME_LEN = 15                   ; maximum length of command-name
40
41 ; Get possible command-line arguments.
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 ; Because the buffer, that we're copying into, was zeroed out,
48 ; we don't need to add a NUL character.
49 ;
50         ldy     FNAM_LEN
51         cpy     #NAME_LEN + 1
52         bcc     L1
53         ldy     #NAME_LEN - 1   ; limit the length
54 L0:     lda     FNAM,y
55         sta     name,y
56 L1:     dey
57         bpl     L0
58         lda     #<name
59         ldx     #>name
60         sta     argv
61         stx     argv + 1
62         inc     __argc          ; argc always is equal to, at least, 1
63
64 ; Find the "rem" token.
65 ;
66         ldx     #0
67 L2:     lda     BASIC_BUF,x
68         beq     done            ; no "rem," no args.
69         inx
70         cmp     #REM
71         bne     L2
72         ldy     #1 * 2
73
74 ; Find the next argument.
75 ;
76 next:   lda     BASIC_BUF,x
77         beq     done
78         inx
79         cmp     #','            ; look for argument-list separator
80         bne     next
81         lda     #$00
82         sta     BASIC_BUF-1,x   ; make the previous arg. be a legal C string
83         inc     __argc          ; found another arg.
84
85 L4:     lda     BASIC_BUF,x
86         beq     point           ; zero-length argument
87         inx
88         cmp     #' '
89         beq     L4              ; skip leading spaces
90
91         cmp     #'"'            ; is argument quoted?
92         beq     L5
93         dex                     ; no, don't skip over character
94         clc                     ; (quotation-mark sets flag)
95 L5:     ror     quoted          ; save it
96
97 ; BASIC's input-buffer starts at the beginning of a RAM page.
98 ; So, we don't need to add the offset -- just store it.
99 ;
100 point:  txa
101         sta     argv,y          ; argv[y]= &arg
102         iny
103         lda     #>BASIC_BUF
104         sta     argv,y
105         iny
106
107         asl     quoted          ; is argument a string-literal?
108         bcc     next            ; no, don't look for ending quotation-mark
109 L7:     lda     BASIC_BUF,x
110         beq     done
111         inx
112         cmp     #'"'
113         bne     L7
114         lda     #$00
115         sta     BASIC_BUF-1,x   ; make this arg. be a legal C string
116         beq     next            ;(bra)
117
118 ; (The last vector in argv[] already is NULL.)
119 ;
120 done:   lda     #<argv
121         ldx     #>argv
122         sta     __argv
123         stx     __argv + 1
124         rts
125
126 ; These arrays are zeroed before initmainargs is called.
127 ; char  name[16+1];
128 ; char* argv[MAXARGS+1]={name};
129 ;
130         .bss
131 quoted: .res    1, %00000000
132 name:   .res    NAME_LEN + 1
133 argv:   .res    (MAXARGS + 1) * 2