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