]> git.sur5r.net Git - cc65/blob - libsrc/atmos/mainargs.s
Added library reference address to lightpen driver header.
[cc65] / libsrc / atmos / mainargs.s
1 ;
2 ; Ullrich von Bassewitz, 2003-03-07
3 ; Stefan Haubenthal, 2011-01-28
4 ;
5 ; Setup arguments for main
6 ;
7
8         .constructor    initmainargs, 24
9         .import         __argc, __argv
10         .macpack        generic
11
12 MAXARGS  = 10                   ; Maximum number of arguments allowed
13 REM      = $9d                  ; BASIC token-code
14 NAME_LEN = 16                   ; maximum length of command-name
15 BASIC_BUF = $35
16 FNAM     = $293
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     #NAME_LEN - 1   ; limit the length
33 L0:     lda     FNAM,y
34         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         ldy     #1 * 2
48
49 ; Find the next argument
50
51 next:   lda     BASIC_BUF,x
52         beq     done            ; End of line reached
53         inx
54         cmp     #' '            ; Skip leading spaces
55         beq     next            ;
56
57 ; Found start of next argument. We've incremented the pointer in X already, so
58 ; it points to the second character of the argument. This is useful since we
59 ; will check now for a quoted argument, in which case we will have to skip this
60 ; first character.
61
62 found:  cmp     #'"'            ; Is the argument quoted?
63         beq     setterm         ; Jump if so
64         dex                     ; Reset pointer to first argument character
65         lda     #' '            ; A space ends the argument
66 setterm:sta     term            ; Set end of argument marker
67
68 ; Now store a pointer to the argument into the next slot. Since the BASIC
69 ; input buffer is located at the zero page, no calculations are necessary.
70
71         txa                     ; Get low byte
72         add     #<BASIC_BUF     ; Not at page boundary
73         sta     argv,y          ; argv[y]= &arg
74         iny
75         iny
76         inc     __argc          ; Found another arg
77
78 ; Search for the end of the argument
79
80 argloop:lda     BASIC_BUF,x
81         beq     done
82         inx
83         cmp     term
84         bne     argloop
85
86 ; We've found the end of the argument. X points one character behind it, and
87 ; A contains the terminating character. To make the argument a valid C string,
88 ; replace the terminating character by a zero.
89
90         lda     #0
91         sta     BASIC_BUF-1,x
92
93 ; Check if the maximum number of command line arguments is reached. If not,
94 ; parse the next one.
95
96         lda     __argc          ; Get low byte of argument count
97         cmp     #MAXARGS        ; Maximum number of arguments reached?
98         bcc     next            ; Parse next one if not
99
100 ; (The last vector in argv[] already is NULL.)
101
102 done:   lda     #<argv
103         ldx     #>argv
104         sta     __argv
105         stx     __argv + 1
106
107         rts
108
109 .endproc
110
111 ; These arrays are zeroed before initmainargs is called.
112 ; char  name[16+1];
113 ; char* argv[MAXARGS+1]={name};
114 ;
115 .bss
116 term:   .res    1
117 name:   .res    NAME_LEN + 1
118
119 .data
120 argv:   .addr   name
121         .res    MAXARGS * 2