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