]> git.sur5r.net Git - cc65/blob - libsrc/apple2/mainargs.s
Apple 2 mouse driver and other stuff from Oliver Schmidt
[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 ; Oliver Schmidt, 2005-04-05
8 ;
9 ; Scan a group of arguments that are in BASIC's input-buffer.
10 ; Build an array that points to the beginning of each argument.
11 ; Send, to main(), that array and the count of the arguments.
12
13 ; Command-lines look like these lines:
14 ;
15 ; call2048
16 ; call2048 : rem
17 ; call2048:rem arg1 " arg 2 is quoted "  arg3 "" arg5
18 ;
19 ; "call" and "rem" are entokenned; the args. are not.  Leading and trailing
20 ; spaces outside of quotes are ignored.
21
22 ; TO-DO:
23 ; Add a control-character quoting mechanism.
24
25         .constructor    initmainargs, 18
26         .import         __argc, __argv, __dos_type
27
28         .include        "zeropage.inc"
29         .include        "apple2.inc"
30
31 ; Maximum number of arguments allowed in the argument table.
32 ; (An argument contains a comma, at least.)
33
34 MAXARGS = 10
35
36 BASIC_BUF = $200
37 FNAM_LEN  = $280
38 FNAM      = $281
39 REM       = $B2                 ; BASIC token-code
40
41 ; Get possible command-line arguments. Goes into the special INIT segment,
42 ; which may be reused after the startup code is run
43
44         .segment        "INIT"
45
46 initmainargs:
47
48 ; Assume that the program was loaded, a moment ago, by the traditional BLOAD
49 ; statement of BASIC.SYSTEM. Save the "most-recent filename" as argument #0.
50
51         ldx     __dos_type      ; No ProDOS -> argv[0] = ""
52         beq     :+
53
54 ; Terminate the filename with a zero to make it a valid C string.
55
56         ldx     FNAM_LEN
57 :       lda     #$00
58         sta     FNAM,x
59
60         inc     __argc          ; argc always is equal to, at least, 1
61
62 ; Find the "rem" token.
63
64         ldx     #$00
65 :       lda     BASIC_BUF,x
66         beq     done            ; No "rem" -> no args
67         inx
68         cmp     #REM
69         bne     :-
70         ldy     #$01 * 2        ; Start with argv[1]
71
72 ; Find the next argument.
73
74 next:   lda     BASIC_BUF,x
75         beq     done
76         inx
77         cmp     #' '            ; Skip leading spaces
78         beq     next
79
80 ; Found start of next argument. We've incremented the pointer in X already, so
81 ; it points to the second character of the argument. This is useful since we
82 ; will check now for a quoted argument, in which case we will have to skip this
83 ; first character.
84
85         cmp     #'"'            ; Is the argument quoted?
86         beq     setterm         ; Jump if so
87         dex                     ; Reset pointer to first argument character
88         lda     #' '            ; A space ends the argument
89 setterm:sta     tmp1            ; Set end of argument marker
90
91 ; Now store a pointer to the argument into the next slot. Since the BASIC
92 ; input buffer is located at the start of a RAM page, no calculations are
93 ; necessary.
94
95         txa                     ; Get low byte
96         sta     argv,y          ; argv[y] = &arg
97         iny
98         lda     #>BASIC_BUF
99         sta     argv,y
100         iny
101         inc     __argc          ; Found another arg
102
103 ; Search for the end of the argument
104
105 argloop:lda     BASIC_BUF,x
106         beq     done
107         inx
108         cmp     tmp1
109         bne     argloop
110
111 ; We've found the end of the argument. X points one character behind it, and
112 ; A contains the terminating character. To make the argument a valid C string,
113 ; replace the terminating character by a zero.
114
115         lda     #$00
116         sta     BASIC_BUF-1,x
117
118 ; Check if the maximum number of command line arguments is reached. If not,
119 ; parse the next one.
120
121         lda     __argc          ; Get low byte of argument count
122         cmp     #MAXARGS        ; Maximum number of arguments reached?
123         bcc     next            ; Parse next one if not
124
125 ; (The last vector in argv[] already is NULL.)
126
127 done:   lda     #<argv
128         ldx     #>argv
129         sta     __argv
130         stx     __argv+1
131         rts
132
133 ; This array is zeroed before initmainargs is called.
134 ; char* argv[MAXARGS+1] = {FNAM};
135
136         .data
137
138 argv:   .addr   FNAM
139         .res    MAXARGS * 2