source: trunk/minix/commands/mdb/io.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 4.8 KB
Line 
1/*
2 * io.c for mdb
3 * all the i/o is here
4 * NB: Printf()
5 */
6#include "mdb.h"
7#include <stdio.h>
8#include <stdarg.h>
9#include <string.h>
10#include <sys/types.h>
11#include "proto.h"
12
13#define OUTBUFSIZE 512
14#define PAGESIZE 24
15
16PRIVATE int forceupper = FALSE;
17PRIVATE int someupper = FALSE;
18PRIVATE int stringcount = 0;
19PRIVATE char *string_ptr = NULL; /* stringptr ambiguous at 8th char */
20PRIVATE char *stringstart = NULL;
21
22PRIVATE char outbuf[OUTBUFSIZE];
23PRIVATE FILE *cmdfile = stdin;
24PRIVATE FILE *outfile = stdout;
25PRIVATE FILE *logfile;
26PRIVATE int lineno;
27
28_PROTOTYPE( int _doprnt, (const char *format, va_list ap, FILE *stream ));
29
30PUBLIC char *get_cmd(cbuf, csize)
31char *cbuf;
32int csize;
33{
34char *r;
35
36 fflush(stdout);
37 if( cmdfile == stdin && outfile == stdout )
38 printf("* ");
39 r = fgets(cbuf, csize, cmdfile);
40 if ( r == NULL && cmdfile != stdin ) {
41 cmdfile = stdin;
42 return get_cmd(cbuf, csize);
43 }
44
45 if ( logfile != NULL ) {
46 fprintf( logfile, "%s", cbuf );
47 lineno++;
48 }
49
50 return r;
51}
52
53PUBLIC void openin(s)
54char *s;
55{
56char *t;
57
58 if ((t = strchr(s,'\n')) != NULL) *t = '\0';
59 if ((t = strchr(s,' ')) != NULL) *t = '\0';
60 cmdfile = fopen(s,"r");
61 if (cmdfile == NULL) {
62 Printf("Cannot open %s for input\n",s);
63 cmdfile = stdin;
64 }
65}
66
67
68/* Special version of printf
69 * really sprintf()
70 * from MINIX library
71 * followed by outstr()
72 */
73PUBLIC int Printf(const char *format, ...)
74{
75 va_list ap;
76 int retval;
77 FILE tmp_stream;
78
79 va_start(ap, format);
80
81 tmp_stream._fd = -1;
82 tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
83 tmp_stream._buf = (unsigned char *) outbuf;
84 tmp_stream._ptr = (unsigned char *) outbuf;
85 tmp_stream._count = 512;
86
87 retval = _doprnt(format, ap, &tmp_stream);
88 putc('\0',&tmp_stream);
89
90 va_end(ap);
91
92 outstr(outbuf);
93
94 return retval;
95}
96
97/*
98 * Set logging options
99 */
100PUBLIC void logging( c, name )
101int c;
102char *name;
103{
104char *t;
105
106 if ( c == 'q' && logfile != NULL ) {
107 fclose(logfile);
108 return;
109 }
110
111 if ((t = strchr(name,'\n')) != NULL) *t = '\0';
112 if ((t = strchr(name,' ' )) != NULL) *t = '\0';
113 if ( logfile != NULL ) fclose(logfile);
114
115 if ( strlen(name) > 0 ) {
116 logfile = fopen(name,"w");
117
118 if (logfile == NULL) {
119 Printf("Cannot open %s for output\n",name);
120 return;
121 }
122
123 /* Close standard output file for L */
124 if ( c == 'L' ) {
125 fclose(outfile);
126 outfile = NULL;
127 }
128 }
129 else
130 /* Reset */
131 {
132 if ( logfile != NULL ) fclose(logfile);
133 outfile = stdout;
134 outbyte('\n');
135 }
136
137}
138
139/* Output system error string */
140PUBLIC void do_error(m)
141char *m;
142{
143 outstr(m);
144 outstr(": ");
145 outstr(strerror(errno));
146 outstr("\n");
147}
148
149PUBLIC void closestring()
150{
151/* close string device */
152
153 stringcount = 0;
154 stringstart = string_ptr = NULL;
155}
156
157PUBLIC int mytolower(ch)
158int ch;
159{
160/* convert char to lower case */
161
162 if (ch >= 'A' && ch <= 'Z')
163 ch += 'a' - 'A';
164 return ch;
165}
166
167
168PUBLIC void openstring(string)
169char *string;
170{
171/* open string device */
172
173 stringcount = 0;
174 stringstart = string_ptr = string;
175}
176
177PUBLIC void outbyte(byte)
178int byte;
179{
180/* print char to currently open output devices */
181
182 if (forceupper && byte >= 'a' && byte <= 'z')
183 byte += 'A' - 'a';
184 if (string_ptr != NULL)
185 {
186 if ((*string_ptr++ = byte) == '\t')
187 stringcount = 8 * (stringcount / 8 + 1);
188 else
189 ++stringcount;
190 }
191 else
192 {
193 if ( paging && byte == '\n' ) {
194 lineno++;
195 if ( lineno >= PAGESIZE) {
196 if ( cmdfile == stdin ) {
197 printf("\nMore...any key to continue");
198 fgets( outbuf, OUTBUFSIZE-1, cmdfile );
199 }
200 }
201 lineno = 0;
202 }
203
204 if ( outfile != NULL )
205 putc(byte,outfile);
206 /* Do not log CR */
207 if ( logfile != NULL && byte != '\r' )
208 putc(byte,logfile);
209 }
210}
211
212
213PUBLIC void outcomma()
214{
215/* print comma */
216
217 outbyte(',');
218}
219
220PRIVATE char hexdigits[] = "0123456789ABCDEF";
221PUBLIC void outh4(num)
222unsigned num;
223{
224/* print 4 bits hex */
225
226 outbyte(hexdigits[num % 16]);
227}
228
229PUBLIC void outh8(num)
230unsigned num;
231{
232/* print 8 bits hex */
233
234 outh4(num / 16);
235 outh4(num);
236}
237
238PUBLIC void outh16(num)
239unsigned num;
240{
241/* print 16 bits hex */
242
243 outh8(num / 256);
244 outh8(num);
245}
246
247PUBLIC void outh32(num)
248unsigned num;
249{
250/* print 32 bits hex */
251
252 outh16((u16_t) (num >> 16));
253 outh16((u16_t) num);
254}
255
256PUBLIC void outspace()
257{
258/* print space */
259
260 outbyte(' ');
261}
262
263PUBLIC void outstr(s)
264register char *s;
265{
266/* print string */
267
268 while (*s)
269 outbyte(*s++);
270}
271
272PUBLIC void outtab()
273{
274/* print tab */
275
276 outbyte('\t');
277}
278
279PUBLIC void outustr(s)
280register char *s;
281{
282/* print string, perhaps converting case to upper */
283
284 forceupper = someupper;
285 while (*s)
286 outbyte(*s++);
287 forceupper = FALSE;
288}
289
290
291PUBLIC int stringpos()
292{
293/* return current offset of string device */
294
295 return string_ptr - stringstart;
296}
297
298PUBLIC int stringtab()
299{
300/* return current "tab" spot of string device */
301
302 return stringcount;
303}
304
Note: See TracBrowser for help on using the repository browser.