Mercurial > hg > Members > kono > nitros9-code
annotate level1/cmds/ngu.asm @ 1919:028161cd3535
uses ss.fd
author | boisy |
---|---|
date | Fri, 25 Nov 2005 12:39:54 +0000 |
parents | 20841f236e32 |
children |
rev | line source |
---|---|
820 | 1 ******************************************************************** |
2 * NGU - The "Next Great Utility" | |
3 * | |
4 * $Id$ | |
5 * | |
6 * NGU is a template for writing utilities under OS-9/6809. It has | |
7 * robust option handling and is littered with comments to help you | |
8 * write your own utilities. | |
9 * | |
10 * NGU uses a two-pass method for parsing the command line. On the | |
11 * first pass, dash options are processed and internal flags are set | |
12 * accordingly. As the options are processed, they are cleared to | |
13 * spaces so that they won't be present on the second pass. | |
14 * | |
902 | 15 * e.g. |
16 * 1st pass: ngu -x foo1 -y foo2 -t=bar1 -ab | |
17 * 2nd pass: ngu foo1 foo2 | |
18 * | |
19 * For the second pass, NGU parses the remaining arguments, which don't | |
20 * begin with -. Presumably these are filenames or other names that are to be | |
820 | 21 * processed. |
22 * | |
23 * Features: | |
823 | 24 * - Written for 6809 and 6309 processors in fast, compact |
25 * assembly language. | |
26 * | |
820 | 27 * - Both options and files can be specified anywhere |
28 * on the command line | |
29 * (i.e ngu -a test1 -b test2 -c=foo) | |
30 * | |
902 | 31 * - Multiple options can be combined behind one dash: |
820 | 32 * (i.e ngu -ab test1 -c=foo test2 test3) |
33 * | |
34 * - Several useful assembly routines are provided for | |
35 * copying strings and determining string length. | |
36 * | |
37 * Limitations: | |
823 | 38 * - Only single character option names can be processed. |
902 | 39 * Multi-character option names (i.e. -delete) aren't supported. |
820 | 40 * |
902 | 41 * - The current file counter is one byte, counting a maximum |
820 | 42 * of 255 files. |
43 * | |
1325
84ea83668304
Redid comments, reset all rev nibbles that weren't explictly set to 0
boisy
parents:
929
diff
changeset
|
44 * Edt/Rev YYYY/MM/DD Modified by |
84ea83668304
Redid comments, reset all rev nibbles that weren't explictly set to 0
boisy
parents:
929
diff
changeset
|
45 * Comment |
820 | 46 * ------------------------------------------------------------------ |
1325
84ea83668304
Redid comments, reset all rev nibbles that weren't explictly set to 0
boisy
parents:
929
diff
changeset
|
47 * 1 2003/01/11 Boisy G. Pitre |
84ea83668304
Redid comments, reset all rev nibbles that weren't explictly set to 0
boisy
parents:
929
diff
changeset
|
48 * Put your development info here. |
820 | 49 |
50 nam NGU | |
51 ttl The "Next Great Utility" | |
52 | |
53 ifp1 | |
54 use defsfile | |
55 endc | |
56 | |
57 * Here are some tweakable options | |
58 DOHELP set 1 1 = include help info | |
929
c04528763543
stack/parameter size increased to prevent collisions with memory
boisy
parents:
902
diff
changeset
|
59 STACKSZ set 128 estimated stack size in bytes |
c04528763543
stack/parameter size increased to prevent collisions with memory
boisy
parents:
902
diff
changeset
|
60 PARMSZ set 256 estimated parameter size in bytes |
820 | 61 COPTSIZ set 64 max size of C option's parameter |
62 | |
63 * Module header definitions | |
64 tylg set Prgrm+Objct | |
65 atrv set ReEnt+rev | |
1332 | 66 rev set $00 |
820 | 67 edition set 1 |
68 | |
69 mod eom,name,tylg,atrv,start,size | |
70 | |
71 * Your utility's static storage vars go here | |
72 org 0 | |
902 | 73 * These vars are used by the base template and shouldn't be removed |
820 | 74 parmptr rmb 2 pointer to our command line params |
75 bufptr rmb 2 pointer to user expandable buffer | |
76 bufsiz rmb 2 size of user expandable buffer | |
902 | 77 filecnt rmb 1 |
78 * These vars are used for this example, it will probably change for you | |
820 | 79 gota rmb 1 |
80 gotb rmb 1 | |
81 coptflg rmb 1 1 = this option has been processed once already | |
823 | 82 cleartop equ . everything up to here gets cleared at start |
820 | 83 copt rmb COPTSIZ buffer for what follows after -c= |
84 * Next is a user adjustable buffer with # modifier on command line. | |
902 | 85 * Some utilities won't need this flexibility, some will. |
820 | 86 * Currently set up to be larger for Level 2 than Level 1 |
87 * Note: this buffer must come just before the stack | |
88 IFGT Level-1 | |
89 bigbuff rmb 8*1024 8K default buffer for Level 2 | |
90 ELSE | |
91 bigbuff rmb 512 512 byte default buffer for Level 1 | |
92 ENDC | |
93 * Finally the stack for any PSHS/PULS/BSR/LBSRs that we might do | |
929
c04528763543
stack/parameter size increased to prevent collisions with memory
boisy
parents:
902
diff
changeset
|
94 rmb STACKSZ+PARMSZ |
820 | 95 size equ . |
96 | |
97 * The utility name and edition goes here | |
821 | 98 name fcs /NGU/ |
820 | 99 fcb edition |
100 | |
101 * Place constant strings here | |
102 IFNE DOHELP | |
103 HlpMsg fcb C$LF | |
104 fcc /Use: NGU [<opts>] <path> [<path>] [<opts>]/ | |
105 fcb C$LF | |
106 fcc / -a option 1/ | |
107 fcb C$LF | |
108 fcc / -b option 2/ | |
109 fcb C$LF | |
822 | 110 fcc / -c=f option 3/ |
111 fcb C$LF | |
823 | 112 CR fcb C$CR |
820 | 113 HlpMsgL equ *-HlpMsg |
114 ENDC | |
821 | 115 UnkOpt fcc /unknown option: / |
116 UnkOptL equ *-UnkOpt | |
820 | 117 |
118 * Here's how registers are set when this process is forked: | |
119 * | |
120 * +-----------------+ <-- Y (highest address) | |
121 * ! Parameter ! | |
122 * ! Area ! | |
123 * +-----------------+ <-- X, SP | |
124 * ! Data Area ! | |
125 * +-----------------+ | |
126 * ! Direct Page ! | |
127 * +-----------------+ <-- U, DP (lowest address) | |
128 * | |
129 * D = parameter area size | |
130 * PC = module entry point abs. address | |
131 * CC = F=0, I=0, others undefined | |
132 | |
823 | 133 * The start of the program is here. |
820 | 134 * Before any command line processing is done, we clear out |
823 | 135 * our static memory from U to cleartop, then determine the |
136 * size of our data area (minus the stack). | |
820 | 137 start pshs u,x save registers for later |
823 | 138 leax <cleartop,u point to end of area to zero out |
820 | 139 IFNE H6309 |
821 | 140 subr u,x subtract U from X |
141 tfr x,w and put X in W | |
142 clr ,-s put a zero on the stack | |
143 tfm s,u+ and use TFM to clear starting at U | |
144 leas 1,s clean up the stack | |
820 | 145 ELSE |
146 pshs x save end pointer on stack | |
147 clrnxt clr ,u+ clear out | |
148 cmpu ,s done? | |
149 bne clrnxt branch if not | |
150 leas 2,s else clear stack | |
151 ENDC | |
152 puls x,u and restore our earlier saved registers | |
153 leay bigbuff,u point Y to copy buffer offset in U | |
154 stx <parmptr save parameter pointer | |
155 sty <bufptr save pointer to buffer | |
156 tfr s,d place top of stack in D | |
157 IFNE H6309 | |
158 subr y,d | |
159 ELSE | |
160 pshs y save Y on stack | |
161 subd ,s++ get size of space between copybuf and X | |
162 ENDC | |
929
c04528763543
stack/parameter size increased to prevent collisions with memory
boisy
parents:
902
diff
changeset
|
163 subd #STACKSZ+PARMSZ subtract out our stack/param size |
820 | 164 std <bufsiz size of our buffer |
165 | |
166 * At this point we have determined our buffer space and saved pointers | |
821 | 167 * for later use. Now we will parse the command line for options that |
854 | 168 * begin with a dash. |
169 * Note that X will NOT point to a space, but to either a CR (if no | |
170 * parameters were passed) or the first non-space character of the | |
171 * parameter. | |
172 * Here we merely grab the byte at X into A and test for end of line, | |
902 | 173 * exiting if so. Utilities that don't require arguments should |
174 * comment out the following three lines. | |
854 | 175 lda ,x get first char |
820 | 176 cmpa #C$CR CR? |
177 lbeq ShowHelp if so, no parameters... show help and exit | |
178 GetChar lda ,x+ get next character on cmd line | |
179 cmpa #C$CR CR? | |
180 lbeq DoNGU if so, do whatever this utility does | |
181 cmpa #'- is it an option? | |
182 beq GetDash if so, process it | |
183 inc <filecnt else must be a non-option argument (file) | |
184 lbsr SkipNSpc move past the argument | |
185 ChkDash lbsr SkipSpcs and any following spaces | |
186 bra GetChar start processing again | |
1332 | 187 GetDash clr -1,x and wipe out the dash from the cmd line |
820 | 188 GetDash2 ldd ,x+ load option char and char following |
822 | 189 ora #$20 make lowercase |
190 IsItA cmpa #'a is it this option? | |
820 | 191 bne IsItB branch if not |
192 inc <gota | |
829
a114971abd3b
Changed the name of a def because it collided with Level 2
boisy
parents:
823
diff
changeset
|
193 bra FixCmdLn |
822 | 194 IsItB cmpa #'b is it this option? |
820 | 195 bne IsItC branch if not |
196 inc <gotb | |
197 bra FixCmdLn | |
822 | 198 IsItC cmpa #'c is it this option? |
820 | 199 bne BadOpt branch if not |
200 tst <coptflg was this option already specified? | |
201 bne BadOpt show help if so | |
202 cmpb #'= 2nd char =? | |
823 | 203 lbne ShowHelp show help if not |
820 | 204 inc <coptflg else tag this option as parsed |
1332 | 205 * ldb #C$SPAC get space |
206 clr -$01,x write over c | |
207 clr ,x+ and = sign, inc X to dest dir | |
820 | 208 * check for valid char after -c= |
209 lda ,x | |
1332 | 210 lbeq ShowHelp |
820 | 211 cmpa #C$SPAC |
212 lbeq ShowHelp | |
213 cmpa #C$CR | |
214 lbeq ShowHelp | |
215 leay <copt,u point Y to parameber buffer | |
216 tfr y,d transfer Y to D | |
217 addd #COPTSIZ | |
218 pshs b,a save updated ptr value | |
219 L0339 lda ,x get byte at X | |
1332 | 220 clr ,x+ store nul byte at X and inc |
820 | 221 sta ,y+ save loaded byte at Y and inc |
222 cmpy ,s are we at end? | |
223 beq L035D branch if so (buffer too small) | |
1332 | 224 tsta else is char in A a nul byte? |
225 beq L0350 branch if so | |
226 cmpa #C$SPAC a space? | |
820 | 227 beq L0350 branch if so |
228 cmpa #C$CR cr? | |
229 bne L0339 get next byte if not | |
230 L0350 leax -1,x | |
231 sta ,x restore previous A | |
232 leas $02,s kill stack | |
233 lbra ChkDash | |
234 L035D leas $02,s | |
235 ldb #$BF else buffer size too small | |
236 orcc #Carry | |
237 lbra Exit | |
1332 | 238 FixCmdLn clr -$01,x and wipe out option character |
820 | 239 cmpb #'0 |
240 lblt ChkDash start dash option processing again | |
241 lbra GetDash possibly another option following? | |
242 | |
243 * We branch here if we encounter an unknown option character | |
821 | 244 * A = bad option character |
245 BadOpt leax UnkOpt,pcr | |
246 ldy #UnkOptL | |
247 ldb #C$CR | |
248 pshs d save bad option and CR on stack | |
249 lda #$02 stderr | |
250 os9 I$Write | |
251 leax ,s point X at option char on stack | |
252 os9 I$WritLn print option and CR | |
253 puls d clean up stack | |
254 lbra ShowHelp | |
820 | 255 |
256 | |
257 * At this point options are processed. | |
258 * We load X with our parameter pointer and go down the command line | |
259 * looking at each file to process (options have been wiped out with | |
260 * spaces) | |
823 | 261 * |
262 * Note, the following two instructions may not be needed, depending on | |
263 * if your utility requires a non-option on the command line. | |
820 | 264 DoNGU tst <filecnt we should have at least one file on cmdline |
265 lbeq ShowHelp if not, exit with error | |
1332 | 266 ldx <parmptr get our parameter pointer |
823 | 267 DoLoop lbsr SkipSpcs skip any leading spaces |
268 cmpa #C$CR end of parameters? | |
269 beq ExitOk if so, end the utility | |
270 pshs x save pointer to arg | |
271 bsr ProcFile process file at X | |
272 puls x get arg pointer | |
273 lbsr SkipNSpc skip the argument we just processed | |
274 bra DoLoop | |
275 | |
276 * This routine processes one file at a time. | |
277 * Entry: X = ptr to argument on the command line. | |
278 * On exit, X can point to the argument or past it. | |
279 * Note that there are NO leading spaces. | |
280 * They have been skipped by the caller. | |
281 * The following code just echos the command line argument, followed | |
282 * by a carriage return. | |
283 ProcFile | |
284 pshs x save ptr | |
285 lda #$01 standard output | |
286 bsr StrLen get length of argument in Y | |
287 puls x recover ptr | |
288 os9 I$Write write name out | |
289 leax CR,pcr point to carriage return | |
290 os9 I$WritLn write it out | |
291 rts | |
820 | 292 |
293 ShowHelp equ * | |
294 IFNE DOHELP | |
295 leax >HlpMsg,pcr point to help message | |
296 ldy #HlpMsgL get length | |
297 lda #$02 std error | |
298 os9 I$WritLn write it | |
299 ENDC | |
300 ExitOk clrb clear carry | |
301 Exit os9 F$Exit and exit | |
302 | |
303 * This routine counts the number of non-whitespace characters | |
304 * starting at X | |
305 * | |
306 * Entry: | |
1332 | 307 * X = ptr to string (space, nul byte or CR terminated) |
820 | 308 * Exit: |
823 | 309 * Y = length of string |
820 | 310 * X = ptr to byte after string |
823 | 311 StrLen pshs a |
312 ldy #$0000 | |
820 | 313 StrLenLp lda ,x+ |
1332 | 314 beq StrLenEx |
820 | 315 cmpa #C$SPAC |
316 beq StrLenEx | |
317 cmpa #C$CR | |
318 beq StrLenEx | |
823 | 319 leay 1,y |
820 | 320 bra StrLenLp |
823 | 321 StrLenEx puls a,pc |
820 | 322 |
323 * This routine copies a string of text from X to Y until | |
324 * a whitespace character or CR is encountered | |
325 * | |
326 * Entry: | |
327 * X = ptr to src string | |
328 * Y = ptr to dest string | |
329 * Exit: | |
330 * D = number of bytes copied | |
331 * X = ptr to byte after original string | |
332 * Y = ptr to byte after copied string | |
333 StrCpy pshs u | |
334 ldu #$0000 | |
335 CopyFnLp lda ,x+ | |
1332 | 336 beq CopyFnEx |
820 | 337 cmpa #C$SPAC |
338 beq CopyFnEx | |
339 cmpa #C$CR | |
340 beq CopyFnEx | |
341 sta ,y+ | |
342 leau 1,u | |
343 bra CopyFnLp | |
344 CopyFnEx tfr u,d | |
345 puls u,pc | |
346 | |
1332 | 347 * This routine skip over spaces and nul bytes |
820 | 348 * |
349 * Entry: | |
350 * X = ptr to data to parse | |
351 * Exit: | |
352 * X = ptr to first non-whitespace char | |
353 * A = non-whitespace char | |
354 SkipSpcs lda ,x+ | |
1332 | 355 beq SkipSpcs |
820 | 356 cmpa #C$SPAC |
357 beq SkipSpcs | |
358 leax -1,x | |
359 rts | |
360 | |
1332 | 361 * This routine skips over everything but spaces, nul bytes and CRs |
820 | 362 * |
363 * Entry: | |
364 * X = ptr to data to parse | |
365 * Exit: | |
366 * X = ptr to first whitespace char | |
367 * A = whitespace char | |
368 SkipNSpc lda ,x+ | |
1332 | 369 beq EatOut |
820 | 370 cmpa #C$SPAC |
371 beq EatOut | |
372 cmpa #C$CR | |
373 bne SkipNSpc | |
374 EatOut leax -1,x | |
375 rts | |
376 | |
377 emod | |
378 eom equ * | |
379 end |