1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* Copyright 1997 Acorn Computers Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef TRACE
/* Undef USE_TML to use a debug file 'TMLDebug' in PipeFS rather */
/* than the TML card output routines. */
#define USE_TML
#ifdef USE_TML
/* -*-C-*-
*
* Copyright(c) 1994 Acorn Computers Ltd., Cambridge, England
*
* svcprint.c
*
* print debugging information down the TML tube link.
*
* provides:
* %s - string
* %C - character in range 20->7F (space printed if outside range)
* %c - any character
* %X - reverses order of bytes if >2 (or >4) specified as width
* %x - hex
* %B - reverses order of bytes if >2 (or >4) specified as width
* %b - binary
* %D - reverses order of bytes if >2 (or >4) specified as width
* %d - decimal
* %p - pointer ("@xxxxxxxx")
*
* field width can be specified by placing a decimal number after the "%"
* character... if the width is started by a "0" then numbers are padded
* with "0"...
*
* standard format specifiers o,u,e,f and g are NOT supported
*
*/
#include "kernel.h"
#include "swis.h"
#include "svcprint.h"
#include "inetlib.h"
#ifndef NULL
# define NULL ((void *)0)
#endif
#ifdef DEBUG
/*int debug = 1;*/
#endif
/**********************************************************************/
/*
* call processor mode independant character output routine
*/
static void oswrch(unsigned char ch)
{
_kernel_swi_regs regset;
/*
* use HostFS_WriteC (SWI 0x40102) to print the character
*/
regset.r[0] = ch;
(void)_kernel_swi(0x40102, ®set, ®set);
}
/**********************************************************************/
/*
* Printn prints a number n in base b, minimum width w adding pad chars
* if needed.
*/
static void printn(unsigned n, unsigned b, int w, char pad)
{
if( n >= b )
{
printn(n / b, b, --w, pad);
oswrch("0123456789abcdef"[n % b]);
}
else
{
while (--w > 0)
oswrch(pad);
oswrch("0123456789abcdef"[n]);
}
}
/**********************************************************************/
static void prf(char *format, unsigned *argp, ...)
{
register int b; /* base to be used when displaying numbers */
register int c; /* current character read from format string */
register int w; /* field width */
char pad; /* field padding character */
unsigned val; /* value of argument */
loop:
val = *argp;
w = 0;
pad = ' ';
while( (c = *format++) != '%' )
{
if( c == '\0' )
return;
if( c == '\n' )
oswrch('\r');
oswrch(c);
}
again:
/*
* we have a special format command
*/
c = *format++;
switch( c )
{
case 's':
{
/* string */
char *p = (char *)*argp++;
int width = 0;
if (p != NULL)
{
/* NOT a NULL pointer */
while (*p)
{
oswrch(*p++);
width++;
}
}
while( width++ < w )
oswrch(' ');
goto loop;
}
case 'C':
if( (*argp < ' ') || (*argp > '~') )
{
oswrch(' ');
argp++;
goto loop;
}
case 'c':
/* character */
oswrch(*argp++);
goto loop;
case '0':
if (w == 0)
pad = '0';
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* field width */
w = (w * 10) + ((int) c - '0');
goto again;
case 'X':
/* hex number */
if (w > 4)
val = (unsigned)ntohl(val);
else
{
if (w > 2)
val = (unsigned)ntohs(val);
}
/*
* NB
*
* fall through to set base
*/
case 'x':
/* hex number */
b = 16;
goto number;
case 'p':
/* pointer */
oswrch('@');
w = 8;
pad = '0';
b = 16;
goto number;
case 'B':
/* binary number */
if (w > 4)
val = (unsigned)ntohl(val);
else
{
if( w > 2 )
val = (unsigned)ntohs(val);
}
/*
* NB
*
* fall through to set base
*/
case 'b':
/* binary number */
b = 2;
goto number;
case 'D':
/* decimal number */
if (w > 4)
val = (unsigned)ntohl(val);
else
{
if (w > 2)
val = (unsigned)ntohs(val);
}
/*
* NB
*
* fall through to set base
*/
case 'd':
b = 10;
/*
* NB
*
* fall through to write number
*/
number:
printn(val,b,w,pad);
argp++;
break;
} /* switch */
goto loop;
}
/**********************************************************************/
#if 0
# define USETUBE
#endif /* 0/1 */
void Printf(char *format, ...)
{
unsigned *argp = (unsigned *)&format;
static int inprf;
#ifdef USETUBE
_kernel_swi_regs reglist;
_kernel_oserror *err;
/* int s = splhi(); */
if( (err = _kernel_swi(0x40100, ®list, ®list)) != NULL )
{
prf("HostVDU: ", 0);
prf(err->errmess, 0);
/* splx(s); */
return;
}
#endif /* USETUBE */
/*int s = splhi();*/
if (inprf)
return;
inprf=1;
prf(format, (argp + 1));
inprf=0;
/*splx(s);*/
#ifdef USETUBE
if( (err = _kernel_swi(0x40101, ®list, ®list)) != NULL )
{
prf("TubeVDU: ", 0);
prf(err->errmess, 0);
/* splx(s); */
return;
}
/* splx(s); */
#endif /* USETUBE */
}
/**********************************************************************/
/* EOF svcprint.c */
#else
/* Make Printf use a PipeFS file instead of outputting to a TML card. */
/* Because the routine calls fopen then fclose to avoid needing any */
/* initialisation or finalisation calls, do something like: */
/* */
/* REPEAT:OSCLI "Type Pipe:$.TMLDebug":UNTIL FALSE */
/* */
/* in a Task Window to list the output during run-time (assuming this */
/* was for a multitasking Wimp application). It isn't ideal, but it */
/* works out as better than nothing. */
/* */
/* History: */
/* */
/* 11/08/97 (ADH): Created */
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
void Printf(char * format, ...)
{
va_list ptr;
FILE * fp;
#define Debug_FileName "Devices:Parallel"
fp = fopen(Debug_FileName, "wab");
if (!fp) fp = fopen(Debug_FileName, "wb");
if (!fp) return;
setvbuf(fp, NULL, _IONBF, 0);
va_start(ptr, format);
vfprintf(fp, format, ptr);
va_end(ptr);
fclose(fp);
}
#endif
#else
static void svcprint_keep_compiler_happy(void)
{
int a;
a = 0;
}
#endif