Use stdarg.h style functions for printf()

git-svn-id: https://svn.code.sf.net/p/freedos/svn/kernel/trunk@394 6ac86273-5f31-0410-b378-82cca8765d1b
This commit is contained in:
Bart Oldeman 2002-08-02 21:45:18 +00:00
parent 8320afcb82
commit f8c1206a51
1 changed files with 47 additions and 39 deletions

View File

@ -30,6 +30,13 @@
#ifdef FORSYS #ifdef FORSYS
#include <io.h> #include <io.h>
#include <stdarg.h>
#else
/* copied from bcc (Bruce's C compiler) stdarg.h */
typedef char *va_list;
#define va_start(arg, last) ((arg) = (char *) (&(last)+1))
#define va_arg(arg, type) (((type *)(arg+=sizeof(type)))[-1])
#define va_end(arg)
#endif #endif
/*#define DOSEMU */ /*#define DOSEMU */
@ -49,7 +56,6 @@ static char buff[MAX_BUFSIZE];
#define printf init_printf #define printf init_printf
#define sprintf init_sprintf #define sprintf init_sprintf
#define charp init_charp #define charp init_charp
#define hexd init_hexd
#define hexDigits init_hexDigits #define hexDigits init_hexDigits
#endif #endif
@ -63,8 +69,8 @@ static BYTE *charp = 0;
STATIC VOID handle_char(COUNT); STATIC VOID handle_char(COUNT);
STATIC VOID put_console(COUNT); STATIC VOID put_console(COUNT);
STATIC BYTE * ltob(LONG, BYTE *, COUNT); STATIC BYTE * ltob(LONG, BYTE *, COUNT);
STATIC COUNT do_printf(CONST BYTE *, REG BYTE **); STATIC COUNT do_printf(CONST BYTE *, REG va_list);
WORD CDECL printf(CONST BYTE * fmt, ...); int CDECL printf(CONST BYTE * fmt, ...);
/* The following is user supplied and must match the following prototype */ /* The following is user supplied and must match the following prototype */
VOID cso(COUNT); VOID cso(COUNT);
@ -96,10 +102,21 @@ VOID put_console(COUNT c)
{ {
buff[buff_offset] = 0; buff[buff_offset] = 0;
buff_offset = 0; buff_offset = 0;
#ifdef __TURBOC__
_ES = FP_SEG(buff); _ES = FP_SEG(buff);
_DX = FP_OFF(buff); _DX = FP_OFF(buff);
_AX = 0x13; _AX = 0x13;
__int__(0xe6); __int__(0xe6);
#elif defined(I86)
asm
{
push ds;
pop es;
mov dx, offset buff;
mov ax, 0x13;
int 0xe6;
}
#endif
} }
else else
{ {
@ -120,7 +137,7 @@ VOID put_console(COUNT c)
_AX = 0x0e00 | c; _AX = 0x0e00 | c;
_BX = 0x0070; _BX = 0x0070;
__int__(0x10); __int__(0x10);
#else #elif defined(I86)
__asm __asm
{ {
mov al, byte ptr c; mov al, byte ptr c;
@ -182,18 +199,22 @@ BYTE *ltob(LONG n, BYTE * s, COUNT base)
#define RIGHT 1 #define RIGHT 1
/* printf -- short version of printf to conserve space */ /* printf -- short version of printf to conserve space */
WORD CDECL printf(CONST BYTE * fmt, ...) int CDECL printf(CONST BYTE * fmt, ...)
{ {
va_list arg;
va_start(arg, fmt);
charp = 0; charp = 0;
return do_printf(fmt, (BYTE **) & fmt + 1); return do_printf(fmt, arg);
} }
WORD CDECL sprintf(BYTE * buff, CONST BYTE * fmt, ...) int CDECL sprintf(BYTE * buff, CONST BYTE * fmt, ...)
{ {
WORD ret; WORD ret;
va_list arg;
va_start(arg, fmt);
charp = buff; charp = buff;
ret = do_printf(fmt, (BYTE **) & fmt + 1); ret = do_printf(fmt, arg);
handle_char(NULL); handle_char(NULL);
return ret; return ret;
} }
@ -207,7 +228,7 @@ ULONG FAR retcs(int i)
return *(ULONG *)p; return *(ULONG *)p;
} }
*/ */
COUNT do_printf(CONST BYTE * fmt, BYTE ** arg) COUNT do_printf(CONST BYTE * fmt, va_list arg)
{ {
int base; int base;
BYTE s[11], FAR * p; BYTE s[11], FAR * p;
@ -270,30 +291,28 @@ COUNT do_printf(CONST BYTE * fmt, BYTE ** arg)
return 0; return 0;
case 'c': case 'c':
handle_char(*(COUNT *) arg++); handle_char(va_arg(arg, int));
continue; continue;
case 'p': case 'p':
{ {
UWORD w[2]; UWORD w0 = va_arg(arg, UWORD);
w[1] = *((UWORD *) arg); char *tmp = charp;
arg += sizeof(UWORD) / sizeof(BYTE *); sprintf(s, "%04x:%04x", va_arg(arg, UWORD), w0);
w[0] = *((UWORD *) arg); p = s;
arg += sizeof(UWORD) / sizeof(BYTE *); charp = tmp;
do_printf("%04x:%04x", (BYTE **) & w); goto do_outputstring;
continue;
} }
case 's': case 's':
p = *arg++; p = va_arg(arg, char *);
goto do_outputstring; goto do_outputstring;
case 'F': case 'F':
fmt++; fmt++;
/* we assume %Fs here */ /* we assume %Fs here */
case 'S': case 'S':
p = *((BYTE FAR **) arg); p = va_arg(arg, char FAR *);
arg += sizeof(BYTE FAR *) / sizeof(BYTE *);
goto do_outputstring; goto do_outputstring;
case 'i': case 'i':
@ -315,24 +334,10 @@ COUNT do_printf(CONST BYTE * fmt, BYTE ** arg)
lprt: lprt:
if (longarg) if (longarg)
{ currentArg = va_arg(arg, long);
currentArg = *((LONG *) arg);
arg += sizeof(LONG) / sizeof(BYTE *);
}
else else
{ currentArg = base < 0 ? (long)va_arg(arg, int) :
if (base < 0) (long)va_arg(arg, unsigned int);
{
currentArg = *((int *)arg);
arg += sizeof(int) / sizeof(BYTE *);
}
else
{
currentArg = *((unsigned int *)arg);
arg += sizeof(unsigned int) / sizeof(BYTE *);
}
}
ltob(currentArg, s, base); ltob(currentArg, s, base);
p = s; p = s;
@ -361,9 +366,11 @@ COUNT do_printf(CONST BYTE * fmt, BYTE ** arg)
} }
} }
va_end(arg);
return 0; return 0;
} }
#ifndef _INIT
void hexd(char *title, UBYTE FAR * p, COUNT numBytes) void hexd(char *title, UBYTE FAR * p, COUNT numBytes)
{ {
int loop, start = 0; int loop, start = 0;
@ -381,6 +388,7 @@ void hexd(char *title, UBYTE FAR * p, COUNT numBytes)
printf("\n"); printf("\n");
} }
} }
#endif
#ifdef TEST #ifdef TEST
/* /*
@ -394,7 +402,7 @@ void hexd(char *title, UBYTE FAR * p, COUNT numBytes)
and run. if strings are wrong, the program will wait for the ANYKEY and run. if strings are wrong, the program will wait for the ANYKEY
*/ */
#include <c:\tc\include\conio.h> #include <conio.h>
void cso(char c) void cso(char c)
{ {
putch(c); putch(c);