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