Initial revision
[ssr] / StraySrc / Utilities / s / enumerate
1 ;
2 ; enumerate.s
3 ;
4 ; Run a command on lots of files (MDW)
5 ;
6 ; © 1994-1998 Straylight
7 ;
8
9 ;----- Licensing note -------------------------------------------------------
10 ;
11 ; This file is part of Straylight's core utilities (coreutils).
12 ;
13 ; Coreutils is free software; you can redistribute it and/or modify
14 ; it under the terms of the GNU General Public License as published by
15 ; the Free Software Foundation; either version 2, or (at your option)
16 ; any later version.
17 ;
18 ; Coreutils is distributed in the hope that it will be useful,
19 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ; GNU General Public License for more details.
22 ;
23 ; You should have received a copy of the GNU General Public License
24 ; along with Coreutils. If not, write to the Free Software Foundation,
25 ; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 ;----- Standard stuff -------------------------------------------------------
28
29 GET libs:swis
30 GET libs:header
31
32 IMPORT version
33
34 ;----- Main code ------------------------------------------------------------
35
36 AREA |!!!Util$$Header|,CODE,READONLY
37
38 main ROUT
39
40 ; --- Pass the command string to OS_ReadArgs ---
41
42 STMDB R13!,{R14} ;Keep my return address
43 ADR R0,enum_commDef ;Point to command definition
44 MOV R2,R12 ;Point to the output buffer
45 MOV R3,#&100 ;Size of my buffer
46 SWI XOS_ReadArgs ;Parse up my command string
47 LDMVSIA R13!,{PC} ;If it failed, quit now
48
49 ; --- Check validity of arguments ---
50
51 LDMIA R12,{R10,R11} ;Load info from the buffer
52
53 LDR R14,[R12,#8] ;Load the help flag
54 CMP R14,#0 ;Does he want help?
55 BNE enum_showHelp ;Yes -- display helpful text
56
57 CMP R11,#0 ;Is there a command to do?
58 BEQ enum_noCommand ;No -- complain about this
59 CMP R10,#0 ;Is there a directory?
60 ADREQ R10,enum_currDir ;No -- use current director
61
62 ADD R14,R12,#12 ;Point to nofiles/nodirs
63 LDMIA R14,{R8,R9} ;Load these flags out
64 CMP R8,#0 ;Check for no files
65 CMPNE R9,#0 ;And for no directories
66 BNE enum_trivial ;If both set, it's easy!
67
68 ; --- Copy the command into a buffer ---
69
70 ADD R0,R12,#&100 ;Point to second buffer
71
72 00main LDRB R14,[R11],#1 ;Get a byte from the command
73 CMP R14,#32 ;Is this a nice character?
74 STRCSB R14,[R0],#1 ;Store it in the buffer
75 BCS %00main ;If more to do, loop round
76
77 MOV R14,#' ' ;A space to put on the end
78 STRB R14,[R0],#1 ;Stick it on the end
79 SUB R11,R0,R12 ;Find length of this string
80 SUB R11,R11,#&100
81
82 ; --- Copy the name after the command ---
83
84 ADD R7,R12,#&300 ;Point to fourth buffer
85 MOV R0,R10 ;Point to pathname
86
87 01main LDRB R14,[R0],#1 ;Get a dirname byte
88 CMP R14,#32 ;Is this the end yet?
89 STRCSB R14,[R7],#1 ;Store it in the buffer
90 BCS %01main ;If more to do, do more
91
92 MOV R14,#'.' ;Put a dot after the dirname
93 STRB R14,[R7],#1 ;Store that on the end
94
95 ; --- Set up for main scanning loop ---
96
97 ADR R6,enum_star ;Find all names in the dir
98 MOV R5,#&80 ;Size of the buffer
99 MOV R4,#0 ;This is the first call
100
101 ; --- Read directory entries and do commands ---
102
103 02main MOV R3,#1 ;Read one entry at a time
104 ADD R2,R12,#&200 ;Point to the buffer
105 MOV R1,R10 ;Point to the directory name
106 MOV R0,#10 ;Read files and information
107 SWI XOS_GBPB ;Get a directory entry
108 LDMVSIA R13!,{PC} ;If it failed, return error
109 CMN R4,#1 ;Is that the end of it all?
110 LDMEQIA R13!,{PC}^ ;Yes -- return to caller
111
112 ; --- Find out if we process this entry ---
113
114 LDR R0,[R2,#16] ;Get the object type
115 CMP R0,#1 ;Is the object a file?
116 BNE %03main ;No -- check for a directory
117 CMP R8,#0 ;Are we to process files?
118 BNE %02main ;No -- ignore this entry
119 B %04main ;Skip ahead to handle file
120
121 03main CMP R9,#0 ;Are we to process dirs?
122 BNE %02 ;No -- ignore this entry
123
124 ; --- Process the current entry ---
125
126 04main ADD R0,R2,#20 ;Point to the actual name
127 MOV R1,R7 ;Point to the command head
128 05main LDRB R14,[R0],#1 ;Get a byte from the name
129 STRB R14,[R1],#1 ;Store it in the buffer
130 CMP R14,#0 ;Is that the end?
131 BNE %05main ;No -- go round again
132
133 STMFD R13!,{R4} ;Save the current index
134 ADD R0,R12,#&300 ;Point to full pathname
135 ADD R1,R12,#&200 ;Use buffer three for output
136 MOV R2,#&100 ;It's 256 bytes in size
137 ADD R3,R12,#&100 ;Point to template string
138 MOV R4,R11 ;Get length of template
139 SWI XOS_SubstituteArgs ;Try and substitute things
140 LDMFD R13!,{R4} ;Reload the directory index
141 LDMVSIA R13!,{PC} ;If it failed, return
142
143 MOV R0,R1 ;Point to output buffer
144 SWI XOS_CLI ;Perform the command
145 BVC %02main ;If it was OK continue
146
147 LDR R14,[R12,#20] ;Is the x flag on?
148 TEQ R14,#0 ;Don't corrupt the V flag
149 BNE %02main ;Yes -- ignore the error
150 LDMFD R13!,{PC} ;Otherwise return to caller
151
152 enum_commDef DCB "dir," ;Offset 0
153 DCB "cmd," ;Offset 4
154
155 DCB "help/S," ;Offset 8
156 DCB "noFiles/S," ;Offset 12
157 DCB "noDirs/S," ;Offset 16
158 DCB "noErrors=x/S," ;Offset 20
159 DCB 0
160
161 enum_currDir DCB "@",0
162
163 enum_star DCB "*",0
164
165 enum_showHelp ADR R0,enum_helpText ;Point to the help text
166 MOV R1,#0 ;No didtionary, please
167 LDR R2,=version ;Find the version string
168 ADR R14,main ;Point to my base address
169 ADD R2,R14,R2 ;Find the actual string
170 SWI XOS_PrettyPrint ;Display the string
171 LDMIA R13!,{PC}^ ;And return to caller
172
173 enum_helpText DCB "enumerate ",27,0,13
174 DCB 13
175 DCB "Syntax: enumerate [<options>] [-dir <directory>] "
176 DCB "-cmd <command>",13
177 DCB 13
178 DCB "Executes <command> on each file/directory in "
179 DCB "<directory>. If no <directory> is specified, the "
180 DCB "current directory is used.",13
181 DCB 13
182 DCB "Options (which may be abbreviated) are:"
183 DCB 13
184 DCB "-help",9,9,"Display this help text",13
185 DCB "-noDirs",9,9,"Do not execute command on "
186 DCB "directories",13
187 DCB "-noFiles",9,"Do not execute command on files",13
188 DCB "-noErrors",9,"Ignore errors from command",13
189 DCB 0
190
191 enum_noCommand ADR R0,enum_noCmdErr
192 LDMIA R13!,{R14}
193 ORRS PC,R14,#&10000000
194
195 enum_trivial ADR R0,enum_easyErr
196 LDMIA R13!,{R14}
197 ORRS PC,R14,#&10000000
198
199 enum_noCmdErr DCD 1
200 DCB "enumerate: No command specified.",0
201
202 enum_easyErr DCD 1
203 DCB "enumerate: Nothing to do -- -nodirs and "
204 DCB "-nofiles both set!",0
205
206 ;----- That's all, folks ----------------------------------------------------
207
208 END