4 * Copyright 1996 Alexandre Julliard
10 #include "wine/winbase16.h"
15 #include "stackframe.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(string
)
23 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
24 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
25 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
26 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
27 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
28 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
29 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
59 static const CHAR null_stringA
[] = "(null)";
60 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
62 /***********************************************************************
63 * WPRINTF_ParseFormatA
65 * Parse a format specification. A format specification has the form:
67 * [-][#][0][width][.precision]type
69 * Return value is the length of the format specification in characters.
71 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
78 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
79 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
80 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
81 while ((*p
>= '0') && (*p
<= '9')) /* width field */
83 res
->width
= res
->width
* 10 + *p
- '0';
86 if (*p
== '.') /* precision field */
89 while ((*p
>= '0') && (*p
<= '9'))
91 res
->precision
= res
->precision
* 10 + *p
- '0';
95 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
96 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
97 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
101 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
104 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
108 res
->type
= WPR_SIGNED
;
111 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
))
112 ? WPR_WSTRING
: WPR_STRING
;
115 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
))
116 ? WPR_STRING
: WPR_WSTRING
;
119 res
->type
= WPR_UNSIGNED
;
122 res
->flags
|= WPRINTF_UPPER_HEX
;
125 res
->type
= WPR_HEXA
;
127 default: /* unknown format char */
128 res
->type
= WPR_UNKNOWN
;
129 p
--; /* print format as normal char */
132 return (INT
)(p
- format
) + 1;
136 /***********************************************************************
137 * WPRINTF_ParseFormatW
139 * Parse a format specification. A format specification has the form:
141 * [-][#][0][width][.precision]type
143 * Return value is the length of the format specification in characters.
145 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
152 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
153 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
154 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
155 while ((*p
>= '0') && (*p
<= '9')) /* width field */
157 res
->width
= res
->width
* 10 + *p
- '0';
160 if (*p
== '.') /* precision field */
163 while ((*p
>= '0') && (*p
<= '9'))
165 res
->precision
= res
->precision
* 10 + *p
- '0';
169 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
170 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
171 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
175 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
178 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
182 res
->type
= WPR_SIGNED
;
185 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
188 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
191 res
->type
= WPR_UNSIGNED
;
194 res
->flags
|= WPRINTF_UPPER_HEX
;
197 res
->type
= WPR_HEXA
;
200 res
->type
= WPR_UNKNOWN
;
201 p
--; /* print format as normal char */
204 return (INT
)(p
- format
) + 1;
208 /***********************************************************************
211 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
212 LPSTR number
, UINT maxlen
)
216 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
217 if (format
->width
> maxlen
) format
->width
= maxlen
;
222 return (format
->precision
= 1);
224 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
225 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
226 if (!*(arg
->lpcstr_view
+ len
)) break;
227 if (len
> maxlen
) len
= maxlen
;
228 return (format
->precision
= len
);
230 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
231 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
232 if (!*(arg
->lpcwstr_view
+ len
)) break;
233 if (len
> maxlen
) len
= maxlen
;
234 return (format
->precision
= len
);
236 len
= sprintf( number
, "%d", arg
->int_view
);
239 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
242 len
= sprintf( number
,
243 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
244 (UINT
)arg
->int_view
);
249 if (len
> maxlen
) len
= maxlen
;
250 if (format
->precision
< len
) format
->precision
= len
;
251 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
252 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
253 format
->precision
= format
->width
;
254 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
258 /***********************************************************************
259 * WPRINTF_ExtractVAPtr (Not a Windows API)
261 static WPRINTF_DATA
WPRINTF_ExtractVAPtr( WPRINTF_FORMAT
*format
, va_list* args
)
267 result
.wchar_view
= va_arg( *args
, WCHAR
); break;
269 result
.char_view
= va_arg( *args
, CHAR
); break;
271 result
.lpcstr_view
= va_arg( *args
, LPCSTR
); break;
273 result
.lpcwstr_view
= va_arg( *args
, LPCWSTR
); break;
277 result
.int_view
= va_arg( *args
, INT
); break;
279 result
.wchar_view
= 0; break;
284 /***********************************************************************
285 * wvsnprintf16 (Not a Windows API)
287 INT16 WINAPI
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
,
290 WPRINTF_FORMAT format
;
294 WPRINTF_DATA cur_arg
;
297 while (*spec
&& (maxlen
> 1))
299 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
301 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
302 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
305 case WPR_WCHAR
: /* No Unicode in Win16 */
307 cur_arg
.char_view
= VA_ARG16( args
, CHAR
);
309 case WPR_WSTRING
: /* No Unicode in Win16 */
311 seg_str
= VA_ARG16( args
, SEGPTR
);
312 if (IsBadReadPtr16(seg_str
, 1 )) cur_arg
.lpcstr_view
= "";
313 else cur_arg
.lpcstr_view
= PTR_SEG_TO_LIN( seg_str
);
316 if (!(format
.flags
& WPRINTF_LONG
))
318 cur_arg
.int_view
= VA_ARG16( args
, INT16
);
324 if (format
.flags
& WPRINTF_LONG
)
325 cur_arg
.int_view
= VA_ARG16( args
, UINT
);
327 cur_arg
.int_view
= VA_ARG16( args
, UINT16
);
332 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
333 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
334 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
338 case WPR_WCHAR
: /* No Unicode in Win16 */
340 *p
= cur_arg
.char_view
;
342 else if (format
.width
> 1) *p
++ = ' ';
345 case WPR_WSTRING
: /* No Unicode in Win16 */
347 if (len
) memcpy( p
, cur_arg
.lpcstr_view
, len
);
351 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
354 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
361 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
362 if (len
) memcpy( p
, number
, len
);
368 if (format
.flags
& WPRINTF_LEFTALIGN
)
369 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
374 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
378 /***********************************************************************
379 * wvsnprintfA (Not a Windows API)
381 INT WINAPI
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
,
384 WPRINTF_FORMAT format
;
388 WPRINTF_DATA argData
;
390 while (*spec
&& (maxlen
> 1))
392 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
394 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
395 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
396 argData
= WPRINTF_ExtractVAPtr( &format
, &args
);
397 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
398 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
399 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
404 *p
= argData
.wchar_view
;
406 else if (format
.width
> 1) *p
++ = ' ';
410 *p
= argData
.char_view
;
412 else if (format
.width
> 1) *p
++ = ' ';
416 memcpy( p
, argData
.lpcstr_view
, len
);
421 LPCWSTR ptr
= argData
.lpcwstr_view
;
422 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
426 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
429 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
436 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
437 memcpy( p
, number
, len
);
443 if (format
.flags
& WPRINTF_LEFTALIGN
)
444 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
449 TRACE("%s\n",buffer
);
450 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
454 /***********************************************************************
455 * wvsnprintfW (Not a Windows API)
457 INT WINAPI
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
,
460 WPRINTF_FORMAT format
;
464 WPRINTF_DATA argData
;
466 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
468 while (*spec
&& (maxlen
> 1))
470 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
472 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
473 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
474 argData
= WPRINTF_ExtractVAPtr( &format
, &args
);
475 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
476 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
477 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
482 *p
= argData
.wchar_view
;
484 else if (format
.width
> 1) *p
++ = ' ';
488 *p
= argData
.char_view
;
490 else if (format
.width
> 1) *p
++ = ' ';
495 LPCSTR ptr
= argData
.lpcstr_view
;
496 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
500 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
504 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
507 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
514 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
515 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
520 if (format
.flags
& WPRINTF_LEFTALIGN
)
521 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
526 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
530 /***********************************************************************
531 * wvsprintf16 (USER.421)
533 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
537 TRACE("for %p got:\n",buffer
);
538 res
= wvsnprintf16( buffer
, 1024, spec
, args
);
539 return ( res
== -1 ) ? 1024 : res
;
543 /***********************************************************************
544 * wvsprintfA (USER32.587)
546 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
550 TRACE("for %p got:\n",buffer
);
551 res
= wvsnprintfA( buffer
, 1024, spec
, args
);
552 return ( res
== -1 ) ? 1024 : res
;
556 /***********************************************************************
557 * wvsprintfW (USER32.588)
559 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
563 TRACE("for %p got:\n",buffer
);
564 /* FIXME: in w*printfW, */
565 /* is maximum buffer size 1024-bytes? (or 1024-wchars?) */
566 res
= wvsnprintfW( buffer
, 1024, spec
, args
);
567 return ( res
== -1 ) ? 1024 : res
;
571 /***********************************************************************
572 * wsprintf16 (USER.420)
574 /* Winelib version */
575 INT16 WINAPIV
wsprintf16( LPSTR buffer
, LPCSTR spec
, ... )
580 TRACE("for %p got:\n",buffer
);
581 va_start( valist
, spec
);
582 /* Note: we call the 32-bit version, because the args are 32-bit */
583 res
= (INT16
)wvsnprintfA( buffer
, 1024, spec
, valist
);
585 return ( res
== -1 ) ? 1024 : res
;
588 /* Emulator version */
589 INT16 WINAPIV
WIN16_wsprintf16(void)
595 VA_START16( valist
);
596 buffer
= VA_ARG16( valist
, SEGPTR
);
597 spec
= VA_ARG16( valist
, SEGPTR
);
599 res
= wvsnprintf16( (LPSTR
)PTR_SEG_TO_LIN(buffer
), 1024,
600 (LPCSTR
)PTR_SEG_TO_LIN(spec
), valist
);
602 return ( res
== -1 ) ? 1024 : res
;
606 /***********************************************************************
607 * wsprintfA (USER32.585)
609 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
614 TRACE("for %p got:\n",buffer
);
615 va_start( valist
, spec
);
616 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
618 return ( res
== -1 ) ? 1024 : res
;
622 /***********************************************************************
623 * wsprintfW (USER32.586)
625 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
630 TRACE("wsprintfW for %p\n",buffer
);
631 va_start( valist
, spec
);
632 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
634 return ( res
== -1 ) ? 1024 : res
;
638 /***********************************************************************
639 * wsnprintfA (Not a Windows API)
641 INT WINAPIV
wsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, ... )
646 va_start( valist
, spec
);
647 res
= wvsnprintfA( buffer
, maxlen
, spec
, valist
);
653 /***********************************************************************
654 * wsnprintfW (Not a Windows API)
656 INT WINAPIV
wsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, ... )
661 va_start( valist
, spec
);
662 res
= wvsnprintfW( buffer
, maxlen
, spec
, valist
);
667 /***********************************************************************
668 * _DebugOutput (KERNEL.328)
670 void WINAPIV
_DebugOutput( void )
677 char caller
[101], temp
[512];
679 /* Decode caller address */
680 pModule
= NE_GetPtr( CURRENT_STACK16
->cs
);
683 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
684 for ( i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++ )
685 if ( GlobalHandleToSel16( pSeg
->hSeg
) == CURRENT_STACK16
->cs
)
692 sprintf( caller
, "%s %02X:%04X", NE_MODULE_NAME( pModule
),
693 nSeg
, CURRENT_STACK16
->ip
);
695 sprintf( caller
, "%04X:%04X", CURRENT_STACK16
->cs
, CURRENT_STACK16
->ip
);
697 /* Build debug message string */
698 VA_START16( valist
);
699 flags
= VA_ARG16( valist
, WORD
);
700 spec
= VA_ARG16( valist
, SEGPTR
);
701 wvsnprintf16( temp
, sizeof(temp
), (LPCSTR
)PTR_SEG_TO_LIN(spec
), valist
);
704 DPRINTF( "_DebugOutput: %s %04X %s\n",
705 caller
, flags
, debugstr_an(temp
, sizeof(temp
)) );