Initial revision
[ssr] / StraySrc / Libraries / Sapphire / choices / s / choices
1 ;
2 ; choices.choices.s
3 ;
4 ; Locate user-configurable resource files
5 ;
6 ; © 1995 Straylight
7 ;
8
9 ;----- Standard header ------------------------------------------------------
10
11 GET libs:header
12 GET libs:swis
13
14 GET libs:stream
15
16 ;----- External dependencies ------------------------------------------------
17
18 GET sapphire:res
19 GET sapphire:sapphire
20 GET sapphire:string
21
22 ;----- Main code ------------------------------------------------------------
23
24 AREA |Sapphire$$Code|,CODE,READONLY
25
26 ; --- choices_useChoices ---
27 ;
28 ; On entry: R0 == 0 to disable use of `Choices', >0 to enable, or <0 to
29 ; read
30 ;
31 ; On exit: R0 == previous setting
32 ;
33 ; Use: This call allows an application to determine whether its
34 ; user-configurable resources are looked for in Acorn's
35 ; `Choices' directory, or only in the application's directory.
36 ;
37 ; An application would typically make this call after parsing
38 ; its command-line options, to allow a Network Manager to
39 ; enable the `Choices' support.
40
41 EXPORT choices_useChoices
42 choices_useChoices ROUT
43
44 STMFD R13!,{R12,R14} ;Save some registers
45 WSPACE chs__wSpace ;Find my workspace
46 MOVS R14,R0 ;Copy arg, and set Z and N
47 LDR R0,chs__flags ;Load the flags word
48 ORRPL R14,R0,#chFlag__useChs ;If N clear, set the flag
49 BICEQ R14,R14,#chFlag__useChs ;But if Z set, clear it
50 STRPL R14,chs__flags ;If changed, save flag back
51 AND R0,R0,#chFlag__useChs ;Set up return value
52 LDMFD R13!,{R12,PC}^ ;And return to caller
53
54 LTORG
55
56 ; --- choices_find ---
57 ;
58 ; On entry: R0 == pointer to resource filename
59 ; R1 == pointer to buffer to build filename in
60 ; R2 == flags:
61 ; bit 0 == file will be written to (use Choices$Write)
62 ;
63 ; On exit: R0 == pointer to filename
64 ; R1 == pointer to terminating NULL
65 ; CS if the file was found, else CC
66 ;
67 ; Use: Locates a user-configurable resource file. The search order
68 ; depends on (a) whether the file will be written (bit 0 of R2)
69 ; and (b) whether the use of Acorn's `Choices' system is
70 ; enabled.
71 ;
72 ; The search order is as follows:
73 ;
74 ; * Choices:appName.leaf / <Choices$Write>.appName.leaf
75 ; * res_find(leaf)
76 ;
77 ; (i.e. it looks in the `Choices' structure first, and if that
78 ; fails, it will use res_find).
79
80 EXPORT choices_find
81 choices_find ROUT
82
83 STMFD R13!,{R2-R6,R12,R14} ;Save some registers
84 WSPACE chs__wSpace ;Locate my workspace
85 MOV R5,R0 ;Get the leafname safely
86 MOV R6,R1 ;And the buffer pointer
87 LDR R14,chs__flags ;Load the flags
88 TST R14,#chFlag__useChs ;Are we using Acorn's thing?
89 BEQ %50choices_find ;No -- just use res then
90
91 ; --- Now sort out what we're doing ---
92
93 TST R2,#1 ;Are we writing the file?
94 BNE %20choices_find ;Yes -- that's special
95
96 ; --- We're trying to read ---
97
98 ADR R0,chs__path ;Point to the variable name
99 MOV R1,R11 ;Build value in scratchpad
100 MOV R2,#256 ;Say this is very big
101 MOV R3,#0 ;This is the first call
102 MOV R4,#3 ;Please convert to a string
103 SWI XOS_ReadVarVal ;Try to read the value
104 BVS %50choices_find ;If it failed, try res_find
105
106 MOV R0,R6 ;Point to his buffer
107 ADR R1,chs__prefix ;Point to path prefix
108 BL str_cpy ;Copy that over
109 ADD R2,R0,#10 ;Allow 10 chars for appname
110 LDR R1,sapph_appName ;Find application's name
111 BL str_cpy ;Copy that over
112 CMP R0,R2 ;Have we gone too far?
113 MOVCS R0,R2 ;Yes -- backpedal a bit
114 MOV R14,#'.' ;Store directory separator
115 STRB R14,[R0],#1 ;Save that out
116 MOV R1,R5 ;Point to his leafname
117 BL str_cpy ;Add that to the end too
118 MOV R1,R0 ;Get the end of the buffer
119 MOV R0,R6 ;Point to buffer start
120 BL res_exists ;Is the file there?
121 BCS %90choices_find ;Yes -- return then
122 B %50choices_find ;No luck -- use res_find
123
124 ; --- We're trying to write ---
125
126 20choices_find ADR R0,chs__write ;Point to the variable name
127 MOV R1,R11 ;Build value in scratchpad
128 MOV R2,#256 ;Say this is very big
129 MOV R3,#0 ;This is the first call
130 MOV R4,#3 ;Please convert to a string
131 SWI XOS_ReadVarVal ;Try to read the value
132 BVS %50choices_find ;If it failed, try res_find
133
134 MOV R0,R6 ;Point to his buffer
135 MOV R14,#'<' ;Wrap name in `<>'s
136 STRB R14,[R0],#1 ;Save that in the buffer
137 ADR R1,chs__write ;Point to write prefix
138 BL str_cpy ;Copy that over
139 MOV R14,#'>' ;Close odd brackets
140 STRB R14,[R0],#1 ;Save that in the buffer
141 MOV R14,#'.' ;Add a directory separator
142 STRB R14,[R0],#1 ;Save that in the buffer
143 ADD R2,R0,#10 ;Allow 10 chars for appname
144 LDR R1,sapph_appName ;Find application's name
145 BL str_cpy ;Copy that over
146 CMP R0,R2 ;Have we gone too far?
147 MOVCS R0,R2 ;Yes -- backpedal a bit
148 MOV R2,R0 ;Remember where we are
149 MOV R14,#0 ;Terminate name here
150 STRB R14,[R0],#1 ;Save that out
151
152 ; --- Create <Choices$Write>.appName ---
153
154 MOV R4,#0 ;Default number of entries
155 MOV R1,R6 ;Point to the buffer
156 MOV R0,#8 ;Create a directory
157 SWI XOS_File ;Try to create it at least
158 BVS %50choices_find ;If no luck, use res_find
159
160 ; --- Now attach the leafname ---
161
162 MOV R0,R2 ;Find the end of the name
163 MOV R14,#'.' ;Add a directory separator
164 STRB R14,[R0],#1 ;Store that
165 MOV R1,R5 ;Point to the leafname
166 BL str_cpy ;Copy that on the end
167 MOV R0,R6 ;Point to the buffer
168 MOV R1,R0 ;Get the end of the buffer
169 BL res_exists ;Does that exist?
170 B %90choices_find ;Return that anyway
171
172 ; --- We had no luck ourselves -- try res_find ---
173
174 50choices_find MOV R0,R5 ;Point to the leafname
175 MOV R1,R6 ;And to the buffer
176 BL res_find ;Try to find the file
177
178 ; --- Return finally ---
179 ;
180 ; The correct file-exists state is already in C, as is the
181 ; end pointer.
182
183 90choices_find LDMFD R13!,{R2-R6,R12,R14} ;Restore registers
184 ORRCSS PC,R14,#C_flag ;If C set, set C on exit
185 BICCCS PC,R14,#C_flag ;Else clear C
186
187 chs__path DCB "Choices$Path",0
188 chs__prefix DCB "Choices:",0
189 chs__write DCB "Choices$Write",0
190
191 LTORG
192
193 chs__wSpace DCD 0
194
195 ;----- Workspace ------------------------------------------------------------
196
197 ^ 0,R12
198 chs__wStart # 0
199
200 chs__flags # 4 ;Various interesting flags
201
202 chs__wSize EQU {VAR}-chs__wStart
203
204 chFlag__useChs EQU (1<<0) ;Use Acorn's `Choices' system
205
206 AREA |Sapphire$$LibData|,CODE,READONLY
207
208 DCD chs__wSize
209 DCD chs__wSpace
210 DCD 256
211 DCD 0
212
213 ;----- That's all, folks ----------------------------------------------------
214
215 END