2005-08-04 10:44:16 +02:00
|
|
|
/*
|
|
|
|
* io.c for mdb
|
|
|
|
* all the i/o is here
|
|
|
|
* NB: Printf()
|
|
|
|
*/
|
|
|
|
#include "mdb.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "proto.h"
|
|
|
|
|
|
|
|
#define OUTBUFSIZE 512
|
|
|
|
#define PAGESIZE 24
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int forceupper = FALSE;
|
|
|
|
static int someupper = FALSE;
|
|
|
|
static int stringcount = 0;
|
|
|
|
static char *string_ptr = NULL; /* stringptr ambiguous at 8th char */
|
|
|
|
static char *stringstart = NULL;
|
|
|
|
|
|
|
|
static char outbuf[OUTBUFSIZE];
|
|
|
|
static FILE *cmdfile = stdin;
|
|
|
|
static FILE *outfile = stdout;
|
|
|
|
static FILE *logfile;
|
|
|
|
static int lineno;
|
2005-08-04 10:44:16 +02:00
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
int _doprnt(const char *format, va_list ap, FILE *stream );
|
2005-08-04 10:44:16 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
char *get_cmd(cbuf, csize)
|
2005-08-04 10:44:16 +02:00
|
|
|
char *cbuf;
|
|
|
|
int csize;
|
|
|
|
{
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
if( cmdfile == stdin && outfile == stdout )
|
|
|
|
printf("* ");
|
|
|
|
r = fgets(cbuf, csize, cmdfile);
|
|
|
|
if ( r == NULL && cmdfile != stdin ) {
|
|
|
|
cmdfile = stdin;
|
|
|
|
return get_cmd(cbuf, csize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( logfile != NULL ) {
|
|
|
|
fprintf( logfile, "%s", cbuf );
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void openin(s)
|
2005-08-04 10:44:16 +02:00
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((t = strchr(s,'\n')) != NULL) *t = '\0';
|
|
|
|
if ((t = strchr(s,' ')) != NULL) *t = '\0';
|
|
|
|
cmdfile = fopen(s,"r");
|
|
|
|
if (cmdfile == NULL) {
|
|
|
|
Printf("Cannot open %s for input\n",s);
|
|
|
|
cmdfile = stdin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Special version of printf
|
|
|
|
* really sprintf()
|
|
|
|
* from MINIX library
|
|
|
|
* followed by outstr()
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int Printf(const char *format, ...)
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int retval;
|
2011-04-27 15:00:52 +02:00
|
|
|
#ifndef __NBSD_LIBC
|
2005-08-04 10:44:16 +02:00
|
|
|
FILE tmp_stream;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
tmp_stream._fd = -1;
|
|
|
|
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
|
|
|
|
tmp_stream._buf = (unsigned char *) outbuf;
|
|
|
|
tmp_stream._ptr = (unsigned char *) outbuf;
|
|
|
|
tmp_stream._count = 512;
|
|
|
|
|
|
|
|
retval = _doprnt(format, ap, &tmp_stream);
|
|
|
|
putc('\0',&tmp_stream);
|
2011-04-27 15:00:52 +02:00
|
|
|
#else
|
|
|
|
va_start(ap, format);
|
2005-08-04 10:44:16 +02:00
|
|
|
|
2011-04-27 15:00:52 +02:00
|
|
|
retval = vsnprintf(outbuf, OUTBUFSIZE, format, ap);
|
|
|
|
#endif
|
2005-08-04 10:44:16 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
outstr(outbuf);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set logging options
|
|
|
|
*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void logging( c, name )
|
2005-08-04 10:44:16 +02:00
|
|
|
int c;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ( c == 'q' && logfile != NULL ) {
|
|
|
|
fclose(logfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((t = strchr(name,'\n')) != NULL) *t = '\0';
|
|
|
|
if ((t = strchr(name,' ' )) != NULL) *t = '\0';
|
|
|
|
if ( logfile != NULL ) fclose(logfile);
|
|
|
|
|
|
|
|
if ( strlen(name) > 0 ) {
|
|
|
|
logfile = fopen(name,"w");
|
|
|
|
|
|
|
|
if (logfile == NULL) {
|
|
|
|
Printf("Cannot open %s for output\n",name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close standard output file for L */
|
|
|
|
if ( c == 'L' ) {
|
|
|
|
fclose(outfile);
|
|
|
|
outfile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Reset */
|
|
|
|
{
|
|
|
|
if ( logfile != NULL ) fclose(logfile);
|
|
|
|
outfile = stdout;
|
|
|
|
outbyte('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output system error string */
|
2012-03-25 20:25:53 +02:00
|
|
|
void do_error(m)
|
2005-08-04 10:44:16 +02:00
|
|
|
char *m;
|
|
|
|
{
|
|
|
|
outstr(m);
|
|
|
|
outstr(": ");
|
|
|
|
outstr(strerror(errno));
|
2006-03-15 12:29:22 +01:00
|
|
|
outstr("\n");
|
2005-08-04 10:44:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void closestring()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* close string device */
|
|
|
|
|
|
|
|
stringcount = 0;
|
|
|
|
stringstart = string_ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
int mytolower(ch)
|
2005-08-04 10:44:16 +02:00
|
|
|
int ch;
|
|
|
|
{
|
|
|
|
/* convert char to lower case */
|
|
|
|
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
ch += 'a' - 'A';
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void openstring(string)
|
2005-08-04 10:44:16 +02:00
|
|
|
char *string;
|
|
|
|
{
|
|
|
|
/* open string device */
|
|
|
|
|
|
|
|
stringcount = 0;
|
|
|
|
stringstart = string_ptr = string;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outbyte(byte)
|
2005-08-04 10:44:16 +02:00
|
|
|
int byte;
|
|
|
|
{
|
|
|
|
/* print char to currently open output devices */
|
|
|
|
|
|
|
|
if (forceupper && byte >= 'a' && byte <= 'z')
|
|
|
|
byte += 'A' - 'a';
|
|
|
|
if (string_ptr != NULL)
|
|
|
|
{
|
|
|
|
if ((*string_ptr++ = byte) == '\t')
|
|
|
|
stringcount = 8 * (stringcount / 8 + 1);
|
|
|
|
else
|
|
|
|
++stringcount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( paging && byte == '\n' ) {
|
|
|
|
lineno++;
|
|
|
|
if ( lineno >= PAGESIZE) {
|
|
|
|
if ( cmdfile == stdin ) {
|
|
|
|
printf("\nMore...any key to continue");
|
|
|
|
fgets( outbuf, OUTBUFSIZE-1, cmdfile );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lineno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( outfile != NULL )
|
|
|
|
putc(byte,outfile);
|
|
|
|
/* Do not log CR */
|
|
|
|
if ( logfile != NULL && byte != '\r' )
|
|
|
|
putc(byte,logfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outcomma()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* print comma */
|
|
|
|
|
|
|
|
outbyte(',');
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static char hexdigits[] = "0123456789ABCDEF";
|
|
|
|
void outh4(num)
|
2005-08-04 10:44:16 +02:00
|
|
|
unsigned num;
|
|
|
|
{
|
|
|
|
/* print 4 bits hex */
|
|
|
|
|
|
|
|
outbyte(hexdigits[num % 16]);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outh8(num)
|
2005-08-04 10:44:16 +02:00
|
|
|
unsigned num;
|
|
|
|
{
|
|
|
|
/* print 8 bits hex */
|
|
|
|
|
|
|
|
outh4(num / 16);
|
|
|
|
outh4(num);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outh16(num)
|
2005-08-04 10:44:16 +02:00
|
|
|
unsigned num;
|
|
|
|
{
|
|
|
|
/* print 16 bits hex */
|
|
|
|
|
|
|
|
outh8(num / 256);
|
|
|
|
outh8(num);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outh32(num)
|
2005-08-04 10:44:16 +02:00
|
|
|
unsigned num;
|
|
|
|
{
|
|
|
|
/* print 32 bits hex */
|
|
|
|
|
|
|
|
outh16((u16_t) (num >> 16));
|
|
|
|
outh16((u16_t) num);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outspace()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* print space */
|
|
|
|
|
|
|
|
outbyte(' ');
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outstr(s)
|
2005-08-04 10:44:16 +02:00
|
|
|
register char *s;
|
|
|
|
{
|
|
|
|
/* print string */
|
|
|
|
|
|
|
|
while (*s)
|
|
|
|
outbyte(*s++);
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outtab()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* print tab */
|
|
|
|
|
|
|
|
outbyte('\t');
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
void outustr(s)
|
2005-08-04 10:44:16 +02:00
|
|
|
register char *s;
|
|
|
|
{
|
|
|
|
/* print string, perhaps converting case to upper */
|
|
|
|
|
|
|
|
forceupper = someupper;
|
|
|
|
while (*s)
|
|
|
|
outbyte(*s++);
|
|
|
|
forceupper = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
int stringpos()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* return current offset of string device */
|
|
|
|
|
|
|
|
return string_ptr - stringstart;
|
|
|
|
}
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
int stringtab()
|
2005-08-04 10:44:16 +02:00
|
|
|
{
|
|
|
|
/* return current "tab" spot of string device */
|
|
|
|
|
|
|
|
return stringcount;
|
|
|
|
}
|
|
|
|
|