- simplify findhole() for use for 1 page only
[minix.git] / commands / mdb / io.c
blob874fde4d1aaeeef9293b443fd7c28b99513e86de
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"
13 #define OUTBUFSIZE 512
14 #define PAGESIZE 24
16 PRIVATE int forceupper = FALSE;
17 PRIVATE int someupper = FALSE;
18 PRIVATE int stringcount = 0;
19 PRIVATE char *string_ptr = NULL; /* stringptr ambiguous at 8th char */
20 PRIVATE char *stringstart = NULL;
22 PRIVATE char outbuf[OUTBUFSIZE];
23 PRIVATE FILE *cmdfile = stdin;
24 PRIVATE FILE *outfile = stdout;
25 PRIVATE FILE *logfile;
26 PRIVATE int lineno;
28 _PROTOTYPE( int _doprnt, (const char *format, va_list ap, FILE *stream ));
30 PUBLIC char *get_cmd(cbuf, csize)
31 char *cbuf;
32 int csize;
34 char *r;
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);
45 if ( logfile != NULL ) {
46 fprintf( logfile, "%s", cbuf );
47 lineno++;
50 return r;
53 PUBLIC void openin(s)
54 char *s;
56 char *t;
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;
68 /* Special version of printf
69 * really sprintf()
70 * from MINIX library
71 * followed by outstr()
73 PUBLIC int Printf(const char *format, ...)
75 va_list ap;
76 int retval;
77 FILE tmp_stream;
79 va_start(ap, format);
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;
87 retval = _doprnt(format, ap, &tmp_stream);
88 putc('\0',&tmp_stream);
90 va_end(ap);
92 outstr(outbuf);
94 return retval;
97 /*
98 * Set logging options
100 PUBLIC void logging( c, name )
101 int c;
102 char *name;
104 char *t;
106 if ( c == 'q' && logfile != NULL ) {
107 fclose(logfile);
108 return;
111 if ((t = strchr(name,'\n')) != NULL) *t = '\0';
112 if ((t = strchr(name,' ' )) != NULL) *t = '\0';
113 if ( logfile != NULL ) fclose(logfile);
115 if ( strlen(name) > 0 ) {
116 logfile = fopen(name,"w");
118 if (logfile == NULL) {
119 Printf("Cannot open %s for output\n",name);
120 return;
123 /* Close standard output file for L */
124 if ( c == 'L' ) {
125 fclose(outfile);
126 outfile = NULL;
129 else
130 /* Reset */
132 if ( logfile != NULL ) fclose(logfile);
133 outfile = stdout;
134 outbyte('\n');
139 /* Output system error string */
140 PUBLIC void do_error(m)
141 char *m;
143 outstr(m);
144 outstr(": ");
145 outstr(strerror(errno));
146 outstr("\n");
149 PUBLIC void closestring()
151 /* close string device */
153 stringcount = 0;
154 stringstart = string_ptr = NULL;
157 PUBLIC int mytolower(ch)
158 int ch;
160 /* convert char to lower case */
162 if (ch >= 'A' && ch <= 'Z')
163 ch += 'a' - 'A';
164 return ch;
168 PUBLIC void openstring(string)
169 char *string;
171 /* open string device */
173 stringcount = 0;
174 stringstart = string_ptr = string;
177 PUBLIC void outbyte(byte)
178 int byte;
180 /* print char to currently open output devices */
182 if (forceupper && byte >= 'a' && byte <= 'z')
183 byte += 'A' - 'a';
184 if (string_ptr != NULL)
186 if ((*string_ptr++ = byte) == '\t')
187 stringcount = 8 * (stringcount / 8 + 1);
188 else
189 ++stringcount;
191 else
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 );
201 lineno = 0;
204 if ( outfile != NULL )
205 putc(byte,outfile);
206 /* Do not log CR */
207 if ( logfile != NULL && byte != '\r' )
208 putc(byte,logfile);
213 PUBLIC void outcomma()
215 /* print comma */
217 outbyte(',');
220 PRIVATE char hexdigits[] = "0123456789ABCDEF";
221 PUBLIC void outh4(num)
222 unsigned num;
224 /* print 4 bits hex */
226 outbyte(hexdigits[num % 16]);
229 PUBLIC void outh8(num)
230 unsigned num;
232 /* print 8 bits hex */
234 outh4(num / 16);
235 outh4(num);
238 PUBLIC void outh16(num)
239 unsigned num;
241 /* print 16 bits hex */
243 outh8(num / 256);
244 outh8(num);
247 PUBLIC void outh32(num)
248 unsigned num;
250 /* print 32 bits hex */
252 outh16((u16_t) (num >> 16));
253 outh16((u16_t) num);
256 PUBLIC void outspace()
258 /* print space */
260 outbyte(' ');
263 PUBLIC void outstr(s)
264 register char *s;
266 /* print string */
268 while (*s)
269 outbyte(*s++);
272 PUBLIC void outtab()
274 /* print tab */
276 outbyte('\t');
279 PUBLIC void outustr(s)
280 register char *s;
282 /* print string, perhaps converting case to upper */
284 forceupper = someupper;
285 while (*s)
286 outbyte(*s++);
287 forceupper = FALSE;
291 PUBLIC int stringpos()
293 /* return current offset of string device */
295 return string_ptr - stringstart;
298 PUBLIC int stringtab()
300 /* return current "tab" spot of string device */
302 return stringcount;