Back to index

courier  0.68.2
printf-args.c
Go to the documentation of this file.
00001 /* Decomposed printf argument list.
00002    Copyright (C) 1999, 2002-2003, 2005-2007 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00017    USA.  */
00018 
00019 /* This file can be parametrized with the following macros:
00020      ENABLE_UNISTDIO    Set to 1 to enable the unistdio extensions.
00021      PRINTF_FETCHARGS   Name of the function to be defined.
00022      STATIC             Set to 'static' to declare the function static.  */
00023 
00024 #ifndef PRINTF_FETCHARGS
00025 # include <config.h>
00026 #endif
00027 
00028 /* Specification.  */
00029 #ifndef PRINTF_FETCHARGS
00030 # include "printf-args.h"
00031 #endif
00032 
00033 #ifdef STATIC
00034 STATIC
00035 #endif
00036 int
00037 PRINTF_FETCHARGS (va_list args, arguments *a)
00038 {
00039   size_t i;
00040   argument *ap;
00041 
00042   for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
00043     switch (ap->type)
00044       {
00045       case TYPE_SCHAR:
00046         ap->a.a_schar = va_arg (args, /*signed char*/ int);
00047         break;
00048       case TYPE_UCHAR:
00049         ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
00050         break;
00051       case TYPE_SHORT:
00052         ap->a.a_short = va_arg (args, /*short*/ int);
00053         break;
00054       case TYPE_USHORT:
00055         ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
00056         break;
00057       case TYPE_INT:
00058         ap->a.a_int = va_arg (args, int);
00059         break;
00060       case TYPE_UINT:
00061         ap->a.a_uint = va_arg (args, unsigned int);
00062         break;
00063       case TYPE_LONGINT:
00064         ap->a.a_longint = va_arg (args, long int);
00065         break;
00066       case TYPE_ULONGINT:
00067         ap->a.a_ulongint = va_arg (args, unsigned long int);
00068         break;
00069 #if HAVE_LONG_LONG_INT
00070       case TYPE_LONGLONGINT:
00071         ap->a.a_longlongint = va_arg (args, long long int);
00072         break;
00073       case TYPE_ULONGLONGINT:
00074         ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
00075         break;
00076 #endif
00077       case TYPE_DOUBLE:
00078         ap->a.a_double = va_arg (args, double);
00079         break;
00080       case TYPE_LONGDOUBLE:
00081         ap->a.a_longdouble = va_arg (args, long double);
00082         break;
00083       case TYPE_CHAR:
00084         ap->a.a_char = va_arg (args, int);
00085         break;
00086 #if HAVE_WINT_T
00087       case TYPE_WIDE_CHAR:
00088         /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by
00089            default argument promotions", this is not the case in mingw32,
00090            where wint_t is 'unsigned short'.  */
00091         ap->a.a_wide_char =
00092           (sizeof (wint_t) < sizeof (int)
00093            ? va_arg (args, int)
00094            : va_arg (args, wint_t));
00095         break;
00096 #endif
00097       case TYPE_STRING:
00098         ap->a.a_string = va_arg (args, const char *);
00099         /* A null pointer is an invalid argument for "%s", but in practice
00100            it occurs quite frequently in printf statements that produce
00101            debug output.  Use a fallback in this case.  */
00102         if (ap->a.a_string == NULL)
00103           ap->a.a_string = "(NULL)";
00104         break;
00105 #if HAVE_WCHAR_T
00106       case TYPE_WIDE_STRING:
00107         ap->a.a_wide_string = va_arg (args, const wchar_t *);
00108         /* A null pointer is an invalid argument for "%ls", but in practice
00109            it occurs quite frequently in printf statements that produce
00110            debug output.  Use a fallback in this case.  */
00111         if (ap->a.a_wide_string == NULL)
00112           {
00113             static const wchar_t wide_null_string[] =
00114               {
00115                 (wchar_t)'(',
00116                 (wchar_t)'N', (wchar_t)'U', (wchar_t)'L', (wchar_t)'L',
00117                 (wchar_t)')',
00118                 (wchar_t)0
00119               };
00120             ap->a.a_wide_string = wide_null_string;
00121           }
00122         break;
00123 #endif
00124       case TYPE_POINTER:
00125         ap->a.a_pointer = va_arg (args, void *);
00126         break;
00127       case TYPE_COUNT_SCHAR_POINTER:
00128         ap->a.a_count_schar_pointer = va_arg (args, signed char *);
00129         break;
00130       case TYPE_COUNT_SHORT_POINTER:
00131         ap->a.a_count_short_pointer = va_arg (args, short *);
00132         break;
00133       case TYPE_COUNT_INT_POINTER:
00134         ap->a.a_count_int_pointer = va_arg (args, int *);
00135         break;
00136       case TYPE_COUNT_LONGINT_POINTER:
00137         ap->a.a_count_longint_pointer = va_arg (args, long int *);
00138         break;
00139 #if HAVE_LONG_LONG_INT
00140       case TYPE_COUNT_LONGLONGINT_POINTER:
00141         ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
00142         break;
00143 #endif
00144 #if ENABLE_UNISTDIO
00145       /* The unistdio extensions.  */
00146       case TYPE_U8_STRING:
00147         ap->a.a_u8_string = va_arg (args, const uint8_t *);
00148         /* A null pointer is an invalid argument for "%U", but in practice
00149            it occurs quite frequently in printf statements that produce
00150            debug output.  Use a fallback in this case.  */
00151         if (ap->a.a_u8_string == NULL)
00152           {
00153             static const uint8_t u8_null_string[] =
00154               { '(', 'N', 'U', 'L', 'L', ')', 0 };
00155             ap->a.a_u8_string = u8_null_string;
00156           }
00157         break;
00158       case TYPE_U16_STRING:
00159         ap->a.a_u16_string = va_arg (args, const uint16_t *);
00160         /* A null pointer is an invalid argument for "%lU", but in practice
00161            it occurs quite frequently in printf statements that produce
00162            debug output.  Use a fallback in this case.  */
00163         if (ap->a.a_u16_string == NULL)
00164           {
00165             static const uint16_t u16_null_string[] =
00166               { '(', 'N', 'U', 'L', 'L', ')', 0 };
00167             ap->a.a_u16_string = u16_null_string;
00168           }
00169         break;
00170       case TYPE_U32_STRING:
00171         ap->a.a_u32_string = va_arg (args, const uint32_t *);
00172         /* A null pointer is an invalid argument for "%llU", but in practice
00173            it occurs quite frequently in printf statements that produce
00174            debug output.  Use a fallback in this case.  */
00175         if (ap->a.a_u32_string == NULL)
00176           {
00177             static const uint32_t u32_null_string[] =
00178               { '(', 'N', 'U', 'L', 'L', ')', 0 };
00179             ap->a.a_u32_string = u32_null_string;
00180           }
00181         break;
00182 #endif
00183       default:
00184         /* Unknown type.  */
00185         return -1;
00186       }
00187   return 0;
00188 }