/*static char *SCCSID = "@(#)dprintf.c	6.2 91/01/14";*/
#define SCCSID "@(#)dprintf.c	6.2 91/01/14"
/*
 *	IBM/Microsoft Confidential
 *
 *	Copyright (c) IBM Corporation 1987, 1989
 *	Copyright (c) Microsoft Corporation 1987, 1989
 *
 *	All Rights Reserved
 *
 */

/***    dprintf.c - COM2 output
 *
 *  Ripped off from Z editor
 */
#define INCL_DOSFILEMGR
#define INCL_NOPMAPI

#include <os2.h>
#include <fsd.h>

#include "cdfs.h"
#include "sem.h"
#include "path.h"
#include "vol.h"
#include "buffer.h"
#include "file.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 cdecl dprintf (fmt, ...)
char *fmt;
{
	format (fmt, (PVOID) (&fmt+1));

}
