Mercurial > hg > Members > kono > nitros9-code
annotate 3rdparty/utils/boisy/grep.asm @ 3295:6b7a7b233925 default tip
makefile: Allow PORTS with level1/2 mix
https://sourceforge.net/p/nitros9/feature-requests/10/
author | Tormod Volden <debian.tormod@gmail.com> |
---|---|
date | Tue, 19 Apr 2022 18:12:17 +0200 |
parents | 0e08f0830fd8 |
children |
rev | line source |
---|---|
0 | 1 *************************************************************************** |
2 * GREP - Pattern matching utility | |
3 * | |
4 * The usage for GREP is: | |
5 * | |
6 * Grep <-c> "pattern" [file] (in that order) | |
7 * | |
8 * The -c option tells grep to search with case sensitivity. | |
9 * The pattern must be enclosed in double quotes ("). | |
10 * If no file is specified, input is taken from StdIn | |
11 * | |
12 * Use: asm #16K grep.a o=grep | |
13 * You may have to modify the "use" directive... | |
14 * | |
15 * I got tired of waiting for someone to write this, so I took | |
16 * the challenge. I'm sure there are others who could use this. | |
17 * As always, you're free to distribute this source as long as | |
18 * long as this header is intact with the code. Enjoy! | |
19 * | |
20 * | |
21 * By: Boisy G. Pitre | |
22 * Southern Station, Box 8455 | |
23 * Hattiesburg, MS 39406-8455 | |
24 * Internet: bgpitre@seabass.st.usm.edu | |
25 * | |
26 | |
27 nam grep | |
28 ttl pattern matching utility | |
29 | |
30 ifp1 | |
31 use defsfile | |
32 endc | |
33 | |
1242
bdd2f61d5dbc
Fixed case issues in several files (thanks Rodney H.)
boisy
parents:
1231
diff
changeset
|
34 mod Size,Name,Prgrm+Objct,ReEnt+1,Start,Fin |
1260 | 35 |
0 | 36 Name fcs /Grep/ |
37 Ed fcb $01 | |
38 | |
39 MaskByte rmb 1 Byte used for masking case | |
40 Path rmb 1 path of file (or StdIn) | |
41 StrSiz rmb 1 Size of pattern | |
42 Counter rmb 1 Counter for pattern size | |
43 ByteCmp rmb 1 Buffer to store masked byte | |
44 LineSiz rmb 2 Size of input line | |
45 SrchStr rmb 80 Line buffer | |
1231 | 46 numflag rmb 1 print line numbers if set |
47 linecnt rmb 3 up to 999999 lines | |
48 linestr rmb 7 buffer for 6-digit number+space | |
0 | 49 Line rmb 250 Line max is 250 chars |
50 Stack rmb 200 | |
51 Params rmb 200 | |
52 Fin equ . | |
53 | |
1260 | 54 HelpMsg fcc /Usage: Grep <-c> <-n> "pattern" [file]/ |
55 fcb C$CR | |
0 | 56 HelpLen equ *-HelpMsg |
57 | |
58 Start clr Path Clear path (assume StdIn) | |
1231 | 59 clr numflag |
60 clr linecnt | |
61 clr linecnt+1 | |
62 clr linecnt+2 | |
0 | 63 lda #%00100000 Assume masking |
64 sta MaskByte | |
65 | |
66 Parse lda ,x+ Get char off cmd line | |
1260 | 67 cmpa #C$SPAC is it a space? |
0 | 68 beq Parse yep, get next char |
1260 | 69 cmpa #C$CR is it a CR? |
0 | 70 beq Help Yep, premature, so show help |
71 cmpa #'- is it a dash? | |
72 beq Parse2 yeah, go to option handler | |
73 cmpa #'" is it a quote? | |
74 beq GetStr yep, go to pattern handler | |
75 bra Help else wrong usage, show help | |
76 | |
77 Parse2 lda ,x+ get char after dash | |
1260 | 78 anda #$DF and mask it |
1231 | 79 cmpa #'N is it an N for line numbers? |
80 bne Parse3 nope, try C | |
81 sta numflag set the line numbers flag | |
82 bra Parse and resume parsing | |
83 Parse3 cmpa #'C is it a C for case sensitivity? | |
0 | 84 bne Help nope, bad option, show help |
85 clr MaskByte else clear the mask byte | |
86 bra Parse and go back to parsing routine | |
87 | |
88 GetStr leay SrchStr,u point to pattern buffer | |
89 clr StrSiz and clear the size variable | |
90 | |
91 Store lda ,x+ get char | |
92 cmpa #'" is it the ending quote? | |
93 beq ChckFile yep, see if a file was specified | |
1260 | 94 cmpa #C$CR is it a CR? |
0 | 95 beq Help Yep, in middle of quote! show help |
96 ora MaskByte else mask char | |
97 sta ,y+ and save it in buffer | |
98 inc StrSiz increment the size by one | |
99 bra Store and get the next char | |
100 | |
1260 | 101 EOF cmpb #E$EOF Is error an end-of-file? |
0 | 102 bne Error nope, other error |
1231 | 103 bra Done else we're done |
0 | 104 |
1231 | 105 Help leax <HelpMsg,pcr Point to help message |
0 | 106 ldy #HelpLen load length |
107 lda #2 to StdErr | |
108 os9 I$WritLn and write | |
109 bcs Error exit if error | |
1231 | 110 |
111 Done clrb clear error register | |
112 Error os9 F$Exit and exit! | |
0 | 113 |
114 ChckFile lda ,x get char | |
1260 | 115 cmpa #C$CR is it a CR? |
0 | 116 beq ReadIn yep, we'll use StdIn |
1260 | 117 cmpa #C$SPAC is it a space? |
0 | 118 bne GetFile nope, its a filename char |
119 leax 1,x else increment X | |
120 bra ChckFile and get the next char | |
121 | |
1242
bdd2f61d5dbc
Fixed case issues in several files (thanks Rodney H.)
boisy
parents:
1231
diff
changeset
|
122 GetFile lda #READ. Open for read |
0 | 123 os9 I$Open |
124 bcs Error | |
125 sta Path and save the path | |
126 | |
127 ReadIn ldy #250 max. read = 250 chars | |
128 leax Line,u point X to line buffer | |
129 lda Path load A with path number | |
130 os9 I$ReadLn and get a line of chars | |
131 bcs EOF if error, check for EOF | |
132 sty LineSiz save bytes read | |
1231 | 133 * count lines in BCD, 6-digit version (3 bytes) |
134 lda linecnt+2 | |
135 adda #1 | |
136 daa | |
137 sta linecnt+2 | |
138 bcc Match | |
139 adca linecnt+1 | |
140 daa | |
141 sta linecnt+1 | |
142 bcc Match | |
143 adca linecnt | |
144 daa | |
145 sta linecnt | |
0 | 146 |
147 Match ldb StrSiz load B with pattern size | |
148 stb Counter store it in counter | |
149 leay SrchStr,u point Y to pattern | |
150 | |
151 Loop dec LineSiz+1 decrement line size counter | |
152 beq ReadIn if at end, read in another line | |
153 lda ,x+ else load A with char at X (line) | |
154 ora MaskByte mask it | |
155 sta ByteCmp store it in comparison location | |
156 lda ,y+ load A with char at Y (pattern) | |
157 cmpa ByteCmp compare it with saved byte | |
158 beq GetNext | |
159 bra Match else start from beginning of pattern | |
160 GetNext dec Counter decrement counter | |
1231 | 161 beq doline if at end, print the line (match!) |
0 | 162 bra Loop else check next char |
163 | |
1231 | 164 doline tst <numflag |
165 bne bcdtoasc | |
166 | |
0 | 167 PrnLine leax Line,u point to line buffer |
168 ldy #250 max. chars = 250 | |
1231 | 169 PrnLine2 |
0 | 170 lda #1 to StdOut |
171 os9 I$WritLn and write the line | |
172 bcs Error exit if error | |
173 bra ReadIn else get next line | |
174 | |
1231 | 175 bcdtoasc |
176 leay linestr,u | |
177 ldb <linecnt | |
1260 | 178 bsr btod convert all 6 digits |
1231 | 179 ldb <linecnt+1 |
180 bsr btod | |
181 ldb <linecnt+2 | |
182 bsr btod | |
1260 | 183 ldb #C$SPAC |
1231 | 184 stb ,y |
1260 | 185 leax linestr+1,u but print only last 5 |
186 ldy #256 | |
187 * to print 6 digits change previous 2 lines to leax linestr,u/ldy #257 | |
1231 | 188 bra PrnLine2 |
189 | |
190 btod pshs b | |
191 lsrb | |
192 lsrb | |
193 lsrb | |
194 lsrb | |
195 bsr btod2 | |
196 puls b | |
197 btod2 andb #$0F | |
198 addb #'0 | |
199 stb ,y+ | |
200 rts | |
201 | |
0 | 202 emod |
203 Size equ * | |
204 end | |
1260 | 205 |