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