4 * Copyright 1996 Alexandre Julliard
9 #include "wine/winbase16.h"
12 #include "stackframe.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(string)
20 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
21 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
22 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
23 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
24 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
25 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
26 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
56 static const CHAR null_stringA[] = "(null)";
57 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
59 /***********************************************************************
60 * WPRINTF_ParseFormatA
62 * Parse a format specification. A format specification has the form:
64 * [-][#][0][width][.precision]type
66 * Return value is the length of the format specification in characters.
68 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
75 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
76 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
77 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
78 while ((*p >= '0') && (*p <= '9')) /* width field */
80 res->width = res->width * 10 + *p - '0';
83 if (*p == '.') /* precision field */
86 while ((*p >= '0') && (*p <= '9'))
88 res->precision = res->precision * 10 + *p - '0';
92 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
93 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
94 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
98 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
101 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
105 res->type = WPR_SIGNED;
108 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE))
109 ? WPR_WSTRING : WPR_STRING;
112 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE))
113 ? WPR_STRING : WPR_WSTRING;
116 res->type = WPR_UNSIGNED;
119 res->flags |= WPRINTF_UPPER_HEX;
122 res->type = WPR_HEXA;
124 default: /* unknown format char */
125 res->type = WPR_UNKNOWN;
126 p--; /* print format as normal char */
129 return (INT)(p - format) + 1;
133 /***********************************************************************
134 * WPRINTF_ParseFormatW
136 * Parse a format specification. A format specification has the form:
138 * [-][#][0][width][.precision]type
140 * Return value is the length of the format specification in characters.
142 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
149 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
150 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
151 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
152 while ((*p >= '0') && (*p <= '9')) /* width field */
154 res->width = res->width * 10 + *p - '0';
157 if (*p == '.') /* precision field */
160 while ((*p >= '0') && (*p <= '9'))
162 res->precision = res->precision * 10 + *p - '0';
166 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
167 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
168 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
172 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
175 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
179 res->type = WPR_SIGNED;
182 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
185 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
188 res->type = WPR_UNSIGNED;
191 res->flags |= WPRINTF_UPPER_HEX;
194 res->type = WPR_HEXA;
197 res->type = WPR_UNKNOWN;
198 p--; /* print format as normal char */
201 return (INT)(p - format) + 1;
205 /***********************************************************************
208 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
209 LPSTR number, UINT maxlen )
213 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
214 if (format->width > maxlen) format->width = maxlen;
219 return (format->precision = 1);
221 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
222 for (len = 0; !format->precision || (len < format->precision); len++)
223 if (!*(arg->lpcstr_view + len)) break;
224 if (len > maxlen) len = maxlen;
225 return (format->precision = len);
227 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
228 for (len = 0; !format->precision || (len < format->precision); len++)
229 if (!*(arg->lpcwstr_view + len)) break;
230 if (len > maxlen) len = maxlen;
231 return (format->precision = len);
233 len = sprintf( number, "%d", arg->int_view );
236 len = sprintf( number, "%u", (UINT)arg->int_view );
239 len = sprintf( number,
240 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
241 (UINT)arg->int_view);
242 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
247 if (len > maxlen) len = maxlen;
248 if (format->precision < len) format->precision = len;
249 if (format->precision > maxlen) format->precision = maxlen;
250 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
251 format->precision = format->width;
255 /***********************************************************************
256 * WPRINTF_ExtractVAPtr (Not a Windows API)
258 static WPRINTF_DATA WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
264 result.wchar_view = va_arg( *args, WCHAR ); break;
266 result.char_view = va_arg( *args, CHAR ); break;
268 result.lpcstr_view = va_arg( *args, LPCSTR); break;
270 result.lpcwstr_view = va_arg( *args, LPCWSTR); break;
274 result.int_view = va_arg( *args, INT ); break;
276 result.wchar_view = 0; break;
281 /***********************************************************************
282 * wvsnprintf16 (Not a Windows API)
284 INT16 WINAPI wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
287 WPRINTF_FORMAT format;
291 WPRINTF_DATA cur_arg;
294 while (*spec && (maxlen > 1))
296 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
298 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
299 spec += WPRINTF_ParseFormatA( spec, &format );
302 case WPR_WCHAR: /* No Unicode in Win16 */
304 cur_arg.char_view = VA_ARG16( args, CHAR );
306 case WPR_WSTRING: /* No Unicode in Win16 */
308 seg_str = VA_ARG16( args, SEGPTR );
309 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
310 else cur_arg.lpcstr_view = PTR_SEG_TO_LIN( seg_str );
313 if (!(format.flags & WPRINTF_LONG))
315 cur_arg.int_view = VA_ARG16( args, INT16 );
321 if (format.flags & WPRINTF_LONG)
322 cur_arg.int_view = VA_ARG16( args, UINT );
324 cur_arg.int_view = VA_ARG16( args, UINT16 );
329 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
330 if (!(format.flags & WPRINTF_LEFTALIGN))
331 for (i = format.precision; i < format.width; i++, maxlen--)
335 case WPR_WCHAR: /* No Unicode in Win16 */
337 *p= cur_arg.char_view;
339 else if (format.width > 1) *p++ = ' ';
342 case WPR_WSTRING: /* No Unicode in Win16 */
344 if (len) memcpy( p, cur_arg.lpcstr_view, len );
348 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
351 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
354 format.precision -= 2;
360 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
361 if (len) memcpy( p, number, len );
367 if (format.flags & WPRINTF_LEFTALIGN)
368 for (i = format.precision; i < format.width; i++, maxlen--)
373 return (maxlen > 1) ? (INT)(p - buffer) : -1;
377 /***********************************************************************
378 * wvsnprintfA (Not a Windows API)
380 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec,
383 WPRINTF_FORMAT format;
387 WPRINTF_DATA argData;
389 while (*spec && (maxlen > 1))
391 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
393 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
394 spec += WPRINTF_ParseFormatA( spec, &format );
395 argData = WPRINTF_ExtractVAPtr( &format, &args );
396 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
397 if (!(format.flags & WPRINTF_LEFTALIGN))
398 for (i = format.precision; i < format.width; i++, maxlen--)
403 *p = argData.wchar_view;
405 else if (format.width > 1) *p++ = ' ';
409 *p = argData.char_view;
411 else if (format.width > 1) *p++ = ' ';
415 memcpy( p, argData.lpcstr_view, len );
420 LPCWSTR ptr = argData.lpcwstr_view;
421 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
425 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
428 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
431 format.precision -= 2;
437 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
438 memcpy( p, number, len );
440 /* Go to the next arg */
445 if (format.flags & WPRINTF_LEFTALIGN)
446 for (i = format.precision; i < format.width; i++, maxlen--)
451 TRACE("%s\n",buffer);
452 return (maxlen > 1) ? (INT)(p - buffer) : -1;
456 /***********************************************************************
457 * wvsnprintfW (Not a Windows API)
459 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec,
462 WPRINTF_FORMAT format;
467 while (*spec && (maxlen > 1))
469 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
471 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
472 spec += WPRINTF_ParseFormatW( spec, &format );
473 len = WPRINTF_GetLen( &format, args, number, maxlen - 1 );
474 if (!(format.flags & WPRINTF_LEFTALIGN))
475 for (i = format.precision; i < format.width; i++, maxlen--)
480 *p = va_arg( args, WCHAR );
482 else if (format.width > 1) *p++ = ' ';
486 *p = (WCHAR)va_arg( args, CHAR );
488 else if (format.width > 1) *p++ = ' ';
493 LPCSTR ptr = va_arg( args, LPCSTR );
494 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
498 if (len) memcpy( p, va_arg( args, LPCWSTR ), len * sizeof(WCHAR) );
502 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
505 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
508 format.precision -= 2;
514 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
515 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
516 (void)va_arg( args, INT ); /* Go to the next arg */
521 if (format.flags & WPRINTF_LEFTALIGN)
522 for (i = format.precision; i < format.width; i++, maxlen--)
527 return (maxlen > 1) ? (INT)(p - buffer) : -1;
531 /***********************************************************************
532 * wvsprintf16 (USER.421)
534 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
536 TRACE("for %p got:\n",buffer);
537 return wvsnprintf16( buffer, 0xffff, spec, args );
541 /***********************************************************************
542 * wvsprintfA (USER32.587)
544 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
546 TRACE("for %p got:\n",buffer);
547 return wvsnprintfA( buffer, 0xffffffff, spec, args );
551 /***********************************************************************
552 * wvsprintfW (USER32.588)
554 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
556 TRACE("for %p got:\n",buffer);
557 return wvsnprintfW( buffer, 0xffffffff, spec, args );
561 /***********************************************************************
562 * wsprintf16 (USER.420)
564 /* Winelib version */
565 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, ... )
570 TRACE("for %p got:\n",buffer);
571 va_start( valist, spec );
572 /* Note: we call the 32-bit version, because the args are 32-bit */
573 res = (INT16)wvsnprintfA( buffer, 0xffffffff, spec, valist );
578 /* Emulator version */
579 INT16 WINAPIV WIN16_wsprintf16(void)
585 VA_START16( valist );
586 buffer = VA_ARG16( valist, SEGPTR );
587 spec = VA_ARG16( valist, SEGPTR );
589 res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 0xffff,
590 (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
596 /***********************************************************************
597 * wsprintfA (USER32.585)
599 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
604 TRACE("for %p got:\n",buffer);
605 va_start( valist, spec );
606 res = wvsnprintfA( buffer, 0xffffffff, spec, valist );
612 /***********************************************************************
613 * wsprintfW (USER32.586)
615 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
620 TRACE("wsprintfW for %p\n",buffer);
621 va_start( valist, spec );
622 res = wvsnprintfW( buffer, 0xffffffff, spec, valist );
628 /***********************************************************************
629 * wsnprintfA (Not a Windows API)
631 INT WINAPIV wsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, ... )
636 va_start( valist, spec );
637 res = wvsnprintfA( buffer, maxlen, spec, valist );
643 /***********************************************************************
644 * wsnprintfW (Not a Windows API)
646 INT WINAPIV wsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, ... )
651 va_start( valist, spec );
652 res = wvsnprintfW( buffer, maxlen, spec, valist );
657 /***********************************************************************
658 * _DebugOutput (KERNEL.328)
660 void WINAPIV _DebugOutput( void )
667 char caller[101], temp[512];
669 /* Decode caller address */
670 pModule = NE_GetPtr( CURRENT_STACK16->cs );
673 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
674 for ( i = 0; i < pModule->seg_count; i++, pSeg++ )
675 if ( GlobalHandleToSel16( pSeg->hSeg ) == CURRENT_STACK16->cs )
682 sprintf( caller, "%s %02X:%04X", NE_MODULE_NAME( pModule ),
683 nSeg, CURRENT_STACK16->ip );
685 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
687 /* Build debug message string */
688 VA_START16( valist );
689 flags = VA_ARG16( valist, WORD );
690 spec = VA_ARG16( valist, SEGPTR );
691 wvsnprintf16( temp, sizeof(temp), (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
694 DPRINTF( "_DebugOutput: %s %04X %s\n",
695 caller, flags, debugstr_an(temp, sizeof(temp)) );