opendir change: refinement
[minix.git] / commands / mdb / io.c
blob7df8146c8206ce16e0d7ddc5cfe776f1f7a4fa52
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 static int forceupper = FALSE;
17 static int someupper = FALSE;
18 static int stringcount = 0;
19 static char *string_ptr = NULL; /* stringptr ambiguous at 8th char */
20 static char *stringstart = NULL;
22 static char outbuf[OUTBUFSIZE];
23 static FILE *cmdfile = stdin;
24 static FILE *outfile = stdout;
25 static FILE *logfile;
26 static int lineno;
28 int _doprnt(const char *format, va_list ap, FILE *stream );
30 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 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 int Printf(const char *format, ...)
75 va_list ap;
76 int retval;
77 #ifndef __NBSD_LIBC
78 FILE tmp_stream;
80 va_start(ap, format);
82 tmp_stream._fd = -1;
83 tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
84 tmp_stream._buf = (unsigned char *) outbuf;
85 tmp_stream._ptr = (unsigned char *) outbuf;
86 tmp_stream._count = 512;
88 retval = _doprnt(format, ap, &tmp_stream);
89 putc('\0',&tmp_stream);
90 #else
91 va_start(ap, format);
93 retval = vsnprintf(outbuf, OUTBUFSIZE, format, ap);
94 #endif
95 va_end(ap);
97 outstr(outbuf);
99 return retval;
103 * Set logging options
105 void logging( c, name )
106 int c;
107 char *name;
109 char *t;
111 if ( c == 'q' && logfile != NULL ) {
112 fclose(logfile);
113 return;
116 if ((t = strchr(name,'\n')) != NULL) *t = '\0';
117 if ((t = strchr(name,' ' )) != NULL) *t = '\0';
118 if ( logfile != NULL ) fclose(logfile);
120 if ( strlen(name) > 0 ) {
121 logfile = fopen(name,"w");
123 if (logfile == NULL) {
124 Printf("Cannot open %s for output\n",name);
125 return;
128 /* Close standard output file for L */
129 if ( c == 'L' ) {
130 fclose(outfile);
131 outfile = NULL;
134 else
135 /* Reset */
137 if ( logfile != NULL ) fclose(logfile);
138 outfile = stdout;
139 outbyte('\n');
144 /* Output system error string */
145 void do_error(m)
146 char *m;
148 outstr(m);
149 outstr(": ");
150 outstr(strerror(errno));
151 outstr("\n");
154 void closestring()
156 /* close string device */
158 stringcount = 0;
159 stringstart = string_ptr = NULL;
162 int mytolower(ch)
163 int ch;
165 /* convert char to lower case */
167 if (ch >= 'A' && ch <= 'Z')
168 ch += 'a' - 'A';
169 return ch;
173 void openstring(string)
174 char *string;
176 /* open string device */
178 stringcount = 0;
179 stringstart = string_ptr = string;
182 void outbyte(byte)
183 int byte;
185 /* print char to currently open output devices */
187 if (forceupper && byte >= 'a' && byte <= 'z')
188 byte += 'A' - 'a';
189 if (string_ptr != NULL)
191 if ((*string_ptr++ = byte) == '\t')
192 stringcount = 8 * (stringcount / 8 + 1);
193 else
194 ++stringcount;
196 else
198 if ( paging && byte == '\n' ) {
199 lineno++;
200 if ( lineno >= PAGESIZE) {
201 if ( cmdfile == stdin ) {
202 printf("\nMore...any key to continue");
203 fgets( outbuf, OUTBUFSIZE-1, cmdfile );
206 lineno = 0;
209 if ( outfile != NULL )
210 putc(byte,outfile);
211 /* Do not log CR */
212 if ( logfile != NULL && byte != '\r' )
213 putc(byte,logfile);
218 void outcomma()
220 /* print comma */
222 outbyte(',');
225 static char hexdigits[] = "0123456789ABCDEF";
226 void outh4(num)
227 unsigned num;
229 /* print 4 bits hex */
231 outbyte(hexdigits[num % 16]);
234 void outh8(num)
235 unsigned num;
237 /* print 8 bits hex */
239 outh4(num / 16);
240 outh4(num);
243 void outh16(num)
244 unsigned num;
246 /* print 16 bits hex */
248 outh8(num / 256);
249 outh8(num);
252 void outh32(num)
253 unsigned num;
255 /* print 32 bits hex */
257 outh16((u16_t) (num >> 16));
258 outh16((u16_t) num);
261 void outspace()
263 /* print space */
265 outbyte(' ');
268 void outstr(s)
269 register char *s;
271 /* print string */
273 while (*s)
274 outbyte(*s++);
277 void outtab()
279 /* print tab */
281 outbyte('\t');
284 void outustr(s)
285 register char *s;
287 /* print string, perhaps converting case to upper */
289 forceupper = someupper;
290 while (*s)
291 outbyte(*s++);
292 forceupper = FALSE;
296 int stringpos()
298 /* return current offset of string device */
300 return string_ptr - stringstart;
303 int stringtab()
305 /* return current "tab" spot of string device */
307 return stringcount;