]> git.sur5r.net Git - cc65/blob - libsrc/c64/mainargs.s
New mainargs.s from Greg King
[cc65] / libsrc / c64 / 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 ;
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 ; run
14 ; run : rem  no arguments because no comma!
15 ; run:rem,arg1," arg 2" , arg 3 ,, arg5, ...
16 ;
17 ; "run" 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        "c64.inc"
28
29 ; Maximum number of arguments allowed in the argument table.
30 ; (An argument contains a comma, at least.)
31 ;
32 MAXARGS  = BASIC_BUF_LEN - 2    ; (don't count REM and terminating '\0')
33
34 REM      = $8f                  ; BASIC token-code
35 NAME_LEN = 16                   ; maximum length of command-name
36
37 ; Get possible command-line arguments.
38 ;
39 initmainargs:
40
41 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
42 ; statement.  Save the "most-recent filename" as argument #0.
43 ; Because the buffer, that we're copying into, was zeroed out,
44 ; we don't need to add a NUL character.
45 ;
46         ldy     FNAM_LEN
47         cpy     #NAME_LEN + 1
48         bcc     L1
49         ldy     #NAME_LEN - 1   ; limit the length
50 L0:     lda     (FNAM),y
51         sta     name,y
52 L1:     dey
53         bpl     L0
54         lda     #<name
55         ldx     #>name
56         sta     argv
57         stx     argv + 1
58         inc     __argc          ; argc always is equal to, at least, 1
59
60 ; Find the "rem" token.
61 ;
62         ldx     #0
63 L2:     lda     BASIC_BUF,x
64         beq     done            ; no "rem," no args.
65         inx
66         cmp     #REM
67         bne     L2
68         ldy     #1 * 2
69
70 ; Find the next argument.
71 ;
72 next:   lda     BASIC_BUF,x
73         beq     done
74         inx
75         cmp     #','            ; look for argument-list separator
76         bne     next
77         lda     #$00
78         sta     BASIC_BUF-1,x   ; make the previous arg. be a legal C string
79         inc     __argc          ; found another arg.
80
81 L4:     lda     BASIC_BUF,x
82         beq     point           ; zero-length argument
83         inx
84         cmp     #' '
85         beq     L4              ; skip leading spaces
86
87         cmp     #'"'            ; is argument quoted?
88         beq     L5
89         dex                     ; no, don't skip over character
90         clc                     ; (quotation-mark sets flag)
91 L5:     ror     quoted          ; save it
92
93 ; BASIC's input-buffer starts at the beginning of a RAM page.
94 ; So, we don't need to add the offset -- just store it.
95 ;
96 point:  txa
97         sta     argv,y          ; argv[y]= &arg
98         iny
99         lda     #>BASIC_BUF
100         sta     argv,y
101         iny
102
103         asl     quoted          ; is argument a string-literal?
104         bcc     next            ; no, don't look for ending quotation-mark
105 L7:     lda     BASIC_BUF,x
106         beq     done
107         inx
108         cmp     #'"'
109         bne     L7
110         lda     #$00
111         sta     BASIC_BUF-1,x   ; make this arg. be a legal C string
112         beq     next            ;(bra)
113
114 ; (The last vector in argv[] already is NULL.)
115 ;
116 done:   lda     #<argv
117         ldx     #>argv
118         sta     __argv
119         stx     __argv + 1
120         rts
121
122 ; These arrays are zeroed before initmainargs is called.
123 ; char  name[16+1];
124 ; char* argv[MAXARGS+1]={name};
125 ;
126         .bss
127 quoted: .res    1, %00000000
128 name:   .res    NAME_LEN + 1
129 argv:   .res    (MAXARGS + 1) * 2