]> git.sur5r.net Git - cc65/blob - libsrc/atmos/mainargs.s
Added SER_ prefix. Whitespace cleanup
[cc65] / libsrc / atmos / mainargs.s
1 ;
2 ; 2003-03-07, Ullrich von Bassewitz
3 ; 2011-01-28, Stefan Haubenthal
4 ; 2014-09-10, Greg King
5 ;
6 ; Set up arguments for main
7 ;
8
9         .constructor    initmainargs, 24
10         .import         __argc, __argv
11
12         .include        "atmos.inc"
13         .macpack        generic
14
15 MAXARGS  = 10                   ; Maximum number of arguments allowed
16 REM      = $9D                  ; BASIC token-code
17
18
19 ;---------------------------------------------------------------------------
20 ; Get possible command-line arguments. Goes into the special ONCE segment,
21 ; which will be reused after the startup code is run.
22
23 .segment        "ONCE"
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
30         ldy     #FNAME_LEN      ; Limit the length
31         lda     #0              ; The terminating NUL character
32         beq     L1              ; Branch always
33 L0:     lda     CFOUND_NAME,y
34 L1:     sta     name,y
35         dey
36         bpl     L0
37         inc     __argc          ; argc always is equal to, at least, 1
38
39 ; Find the "rem" token.
40
41         ldx     #0
42 L2:     lda     BASIC_BUF,x
43         beq     done            ; No "rem", no args.
44         inx
45         cmp     #REM
46         bne     L2
47
48 ; The arguments must be copied to a safe place because BASIC's input buffer
49 ; might be re-used by the stdin console.
50
51         ldy     #(SCREEN_XSIZE * 2 - 1) - 1
52 L3:     lda     BASIC_BUF,y
53         sta     args,y
54         dey
55         bpl     L3
56
57         ldy     #1 * 2          ; Point to second argv slot
58
59 ; Find the next argument
60
61 next:   lda     BASIC_BUF,x
62         beq     done            ; End of line reached
63         inx
64         cmp     #' '            ; Skip leading spaces
65         beq     next
66
67 ; Found start of next argument. We've incremented the pointer in X already, so
68 ; it points to the second character of the argument. This is useful since we
69 ; will check now for a quoted argument, in which case we will have to skip this
70 ; first character.
71
72 found:  cmp     #'"'            ; Is the argument quoted?
73         beq     setterm         ; Jump if so
74         dex                     ; Reset pointer to first argument character
75         lda     #' '            ; A space ends the argument
76 setterm:sta     term            ; Set end of argument marker
77
78 ; Now, store a pointer, to the argument, into the next slot.
79
80         txa                     ; Get low byte
81         add     #<args
82         sta     argv,y          ; argv[y]=&arg
83         lda     #>$0000
84         adc     #>args
85         sta     argv+1,y
86         iny
87         iny
88         inc     __argc          ; Found another arg
89
90 ; Search for the end of the argument
91
92 argloop:lda     BASIC_BUF,x
93         beq     done
94         inx
95         cmp     term
96         bne     argloop
97
98 ; We've found the end of the argument. X points one character behind it, and
99 ; A contains the terminating character. To make the argument a valid C string,
100 ; replace the terminating character by a zero.
101
102         lda     #0
103         sta     args-1,x
104
105 ; Check if the maximum number of command line arguments is reached. If not,
106 ; parse the next one.
107
108         lda     __argc          ; Get low byte of argument count
109         cmp     #MAXARGS        ; Maximum number of arguments reached?
110         bcc     next            ; Parse next one if not
111
112 ; (The last vector in argv[] already is NULL.)
113
114 done:   lda     #<argv
115         ldx     #>argv
116         sta     __argv
117         stx     __argv + 1
118         rts
119
120 .endproc
121
122 .segment        "INIT"
123
124 term:   .res    1
125 name:   .res    FNAME_LEN + 1
126 args:   .res    SCREEN_XSIZE * 2 - 1
127
128 .data
129
130 ; This array has zeroes when initmainargs starts.
131 ; char* argv[MAXARGS+1]={name};
132
133 argv:   .addr   name
134         .res    MAXARGS * 2