00001
00002
00003
00004
00005
00006 #include "mdb.h"
00007 #include <stdio.h>
00008 #include <stdarg.h>
00009 #include <string.h>
00010 #include <sys/types.h>
00011 #include "proto.h"
00012
00013 #define OUTBUFSIZE 512
00014 #define PAGESIZE 24
00015
00016 PRIVATE int forceupper = FALSE;
00017 PRIVATE int someupper = FALSE;
00018 PRIVATE int stringcount = 0;
00019 PRIVATE char *string_ptr = NULL;
00020 PRIVATE char *stringstart = NULL;
00021
00022 PRIVATE char outbuf[OUTBUFSIZE];
00023 PRIVATE FILE *cmdfile = stdin;
00024 PRIVATE FILE *outfile = stdout;
00025 PRIVATE FILE *logfile;
00026 PRIVATE int lineno;
00027
00028 _PROTOTYPE( int _doprnt, (const char *format, va_list ap, FILE *stream ));
00029
00030 PUBLIC char *get_cmd(cbuf, csize)
00031 char *cbuf;
00032 int csize;
00033 {
00034 char *r;
00035
00036 fflush(stdout);
00037 if( cmdfile == stdin && outfile == stdout )
00038 printf("* ");
00039 r = fgets(cbuf, csize, cmdfile);
00040 if ( r == NULL && cmdfile != stdin ) {
00041 cmdfile = stdin;
00042 return get_cmd(cbuf, csize);
00043 }
00044
00045 if ( logfile != NULL ) {
00046 fprintf( logfile, "%s", cbuf );
00047 lineno++;
00048 }
00049
00050 return r;
00051 }
00052
00053 PUBLIC void openin(s)
00054 char *s;
00055 {
00056 char *t;
00057
00058 if ((t = strchr(s,'\n')) != NULL) *t = '\0';
00059 if ((t = strchr(s,' ')) != NULL) *t = '\0';
00060 cmdfile = fopen(s,"r");
00061 if (cmdfile == NULL) {
00062 Printf("Cannot open %s for input\n",s);
00063 cmdfile = stdin;
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 PUBLIC int Printf(const char *format, ...)
00074 {
00075 va_list ap;
00076 int retval;
00077 FILE tmp_stream;
00078
00079 va_start(ap, format);
00080
00081 tmp_stream._fd = -1;
00082 tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
00083 tmp_stream._buf = (unsigned char *) outbuf;
00084 tmp_stream._ptr = (unsigned char *) outbuf;
00085 tmp_stream._count = 512;
00086
00087 retval = _doprnt(format, ap, &tmp_stream);
00088 putc('\0',&tmp_stream);
00089
00090 va_end(ap);
00091
00092 outstr(outbuf);
00093
00094 return retval;
00095 }
00096
00097
00098
00099
00100 PUBLIC void logging( c, name )
00101 int c;
00102 char *name;
00103 {
00104 char *t;
00105
00106 if ( c == 'q' && logfile != NULL ) {
00107 fclose(logfile);
00108 return;
00109 }
00110
00111 if ((t = strchr(name,'\n')) != NULL) *t = '\0';
00112 if ((t = strchr(name,' ' )) != NULL) *t = '\0';
00113 if ( logfile != NULL ) fclose(logfile);
00114
00115 if ( strlen(name) > 0 ) {
00116 logfile = fopen(name,"w");
00117
00118 if (logfile == NULL) {
00119 Printf("Cannot open %s for output\n",name);
00120 return;
00121 }
00122
00123
00124 if ( c == 'L' ) {
00125 fclose(outfile);
00126 outfile = NULL;
00127 }
00128 }
00129 else
00130
00131 {
00132 if ( logfile != NULL ) fclose(logfile);
00133 outfile = stdout;
00134 outbyte('\n');
00135 }
00136
00137 }
00138
00139
00140 PUBLIC void do_error(m)
00141 char *m;
00142 {
00143 outstr(m);
00144 outstr(": ");
00145 outstr(strerror(errno));
00146 outstr("\n");
00147 }
00148
00149 PUBLIC void closestring()
00150 {
00151
00152
00153 stringcount = 0;
00154 stringstart = string_ptr = NULL;
00155 }
00156
00157 PUBLIC int mytolower(ch)
00158 int ch;
00159 {
00160
00161
00162 if (ch >= 'A' && ch <= 'Z')
00163 ch += 'a' - 'A';
00164 return ch;
00165 }
00166
00167
00168 PUBLIC void openstring(string)
00169 char *string;
00170 {
00171
00172
00173 stringcount = 0;
00174 stringstart = string_ptr = string;
00175 }
00176
00177 PUBLIC void outbyte(byte)
00178 int byte;
00179 {
00180
00181
00182 if (forceupper && byte >= 'a' && byte <= 'z')
00183 byte += 'A' - 'a';
00184 if (string_ptr != NULL)
00185 {
00186 if ((*string_ptr++ = byte) == '\t')
00187 stringcount = 8 * (stringcount / 8 + 1);
00188 else
00189 ++stringcount;
00190 }
00191 else
00192 {
00193 if ( paging && byte == '\n' ) {
00194 lineno++;
00195 if ( lineno >= PAGESIZE) {
00196 if ( cmdfile == stdin ) {
00197 printf("\nMore...any key to continue");
00198 fgets( outbuf, OUTBUFSIZE-1, cmdfile );
00199 }
00200 }
00201 lineno = 0;
00202 }
00203
00204 if ( outfile != NULL )
00205 putc(byte,outfile);
00206
00207 if ( logfile != NULL && byte != '\r' )
00208 putc(byte,logfile);
00209 }
00210 }
00211
00212
00213 PUBLIC void outcomma()
00214 {
00215
00216
00217 outbyte(',');
00218 }
00219
00220 PRIVATE char hexdigits[] = "0123456789ABCDEF";
00221 PUBLIC void outh4(num)
00222 unsigned num;
00223 {
00224
00225
00226 outbyte(hexdigits[num % 16]);
00227 }
00228
00229 PUBLIC void outh8(num)
00230 unsigned num;
00231 {
00232
00233
00234 outh4(num / 16);
00235 outh4(num);
00236 }
00237
00238 PUBLIC void outh16(num)
00239 unsigned num;
00240 {
00241
00242
00243 outh8(num / 256);
00244 outh8(num);
00245 }
00246
00247 PUBLIC void outh32(num)
00248 unsigned num;
00249 {
00250
00251
00252 outh16((u16_t) (num >> 16));
00253 outh16((u16_t) num);
00254 }
00255
00256 PUBLIC void outspace()
00257 {
00258
00259
00260 outbyte(' ');
00261 }
00262
00263 PUBLIC void outstr(s)
00264 register char *s;
00265 {
00266
00267
00268 while (*s)
00269 outbyte(*s++);
00270 }
00271
00272 PUBLIC void outtab()
00273 {
00274
00275
00276 outbyte('\t');
00277 }
00278
00279 PUBLIC void outustr(s)
00280 register char *s;
00281 {
00282
00283
00284 forceupper = someupper;
00285 while (*s)
00286 outbyte(*s++);
00287 forceupper = FALSE;
00288 }
00289
00290
00291 PUBLIC int stringpos()
00292 {
00293
00294
00295 return string_ptr - stringstart;
00296 }
00297
00298 PUBLIC int stringtab()
00299 {
00300
00301
00302 return stringcount;
00303 }
00304