/*static char *SCCSID = "@(#)dprintf.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)dprintf.c      13.2 90/01/25";*/
/***	dprintf.c - COM2 output
 *
 *  Ripped off from Z editor
 */

#include <os2.h>
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "pb_ifs.h"
#include "superb.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "pr_DEBUG.h"


void putl (ULONG ul, USHORT base)
{
    if (ul / base != 0)
	putl (ul / base, base);
    PUTC ("0123456789ABCDEF"[ul % base]);
}

/*  format - replace the C runtime formatting routines.
 *
 *  format is a near-replacement for the *printf routines in the C runtime.
 *
 *  fmt 	formatting string.  Formats currently understood are:
 *		    %c single character
 *		    %[l]d %[l]x
 *		    %[F]s
 *		    %p
 *		      * may be used to copy in values for m and n from arg
 *			list.
 *		    %%
 *  arg 	is a list of arguments
 */
void format (fmt, pv)
char *fmt;
PVOID pv;
{
    char c;
    PSZ psz;
    USHORT base;

    while (c = *fmt++) {
	if (c == '\n') {
	    PUTC ('\r');
	    PUTC ('\n');
	}
	else
	if (c != '%')
	    PUTC (c);
	else {
	    BOOL fLong = FALSE;
	    BOOL fFar = FALSE;

	    base = 10;
	    if (*fmt == 'l') {
		fLong = TRUE;
		fmt++;
	    }
	    if (*fmt == 'F') {
		fFar = TRUE;
		fmt++;
	    }

	    switch (*fmt++) {
	    case 'c':
		PUTC ((char) * MAKETYPE (pv, PUSHORT));
		++ MAKETYPE (pv, PUSHORT);
		break;
	    case 'x':
		base = 16;
	    case 'd':
		if (fLong) {
		    putl (* MAKETYPE (pv, PULONG), base);
		    ++ MAKETYPE (pv, PULONG);
		} else {
		    putl ((ULONG) * MAKETYPE (pv, PUSHORT), base);
		    ++ MAKETYPE (pv, PUSHORT);
		}
		break;
	    case 's':
		if (fFar) {
		    psz = * MAKETYPE (pv, PSZ far *);
		    ++ MAKETYPE (pv, PSZ far *);
		} else {
		    psz = * MAKETYPE (pv, NPSZ far *);
		    ++ MAKETYPE (pv, NPSZ far *);
		}
		while (*psz != 0)
		    PUTC (*psz++);
		break;
	    case '%':
		PUTC ('%');
		break;
	    case 'p':
		psz = * MAKETYPE (pv, PSZ far *);
		++ MAKETYPE (pv, PSZ far *);
		putl ((LONG)SELECTOROF (psz), 16);
		PUTC (':');
		putl ((LONG)OFFSETOF (psz), 16);
		break;
	    default:
		PUTC ('?');PUTC ('\'');PUTC (fmt[-1]);PUTC('\'');
	    }
	}
    }
}

void far cdecl dprintf (fmt, ...)
char *fmt;
{
    format (fmt, (PVOID) (&fmt+1));

}
