RPM Package Manager, CVS Repository
  http://rpm5.org/cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  j...@rpm5.org
  Module: rpm                              Date:   28-Apr-2016 20:23:06
  Branch: rpm-5_4                          Handle: 2016042818230600

  Modified files:           (Branch: rpm-5_4)
    rpm/rpmdb               tprintf.c

  Log:
    - WIP.

  Summary:
    Revision    Changes     Path
    1.1.2.3     +381 -39    rpm/rpmdb/tprintf.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/rpmdb/tprintf.c
  ============================================================================
  $ cvs diff -u -r1.1.2.2 -r1.1.2.3 tprintf.c
  --- rpm/rpmdb/tprintf.c       26 Apr 2016 10:15:36 -0000      1.1.2.2
  +++ rpm/rpmdb/tprintf.c       28 Apr 2016 18:23:06 -0000      1.1.2.3
  @@ -1,22 +1,22 @@
  -#include <stdio.h>
  +#include "system.h"
  +
   #include <inttypes.h>
   #include <emmintrin.h>
  -
   #include <printf.h>
  -
  -#include <stdlib.h>
  -#include <string.h>
   #include <stdarg.h>
  -#include <ctype.h>
   #include <arpa/inet.h>
   
  +#include "debug.h"
  +
   #define      USE_EXAMPLE_IPADDR      0
   #define      USE_EXAMPLE_FASTA       0
   #define      USE_EXAMPLE_WIDGET      0
  -#define      USE_EXAMPLE_XMM         1
  +#define      USE_EXAMPLE_XMM         0
  +#define      USE_EXAMPLE_HEX         1
   
   static int _tpf_debug;
   
  +#if  USE_EXAMPLE_XMM
   static int pa_xmm;
   static int mod_v4f;
   static int mod_v2d;
  @@ -24,6 +24,14 @@
   static int mod_v8i;
   static int mod_v4i;
   static int mod_v2i;
  +#endif
  +#if  USE_EXAMPLE_HEX
  +static int pa_hex;
  +static int mod_phh;
  +static int mod_hC;
  +static int mod_hD;
  +static int mod_hN;
  +#endif
   
   #ifdef       REFERENCE
   struct printf_info {
  @@ -48,6 +56,7 @@
   };
   #endif
   
  +/* XXX this needs %02X padding/capitalization. */
   static char *itoa(int value, char *result, int base)
   {
       /* check that the base if valid */
  @@ -81,9 +90,9 @@
       return result;
   }
   
  -static void dumpPrintfInfo(const char *msg, const struct printf_info * info)
  +static char * info2fmt(const struct printf_info * info, char *b)
   {
  -    char b[64], *be = b;
  +    char *be = b;
   
       *be++ = '%';
       /* optional parameter: [0-9]+ '$' */
  @@ -103,10 +112,14 @@
       if (info->group)
        *be++ = '\'';
   
  -    /* optional minimum field width */
  -    if (info->width) {
  -     (void) itoa(info->width, be, 10);
  -     be += strlen(be);
  +    if (info->user == 0) {
  +     /* optional minimum field width */
  +     if (info->width) {
  +         (void) itoa(info->width, be, 10);
  +         be += strlen(be);
  +     }
  +    } else {
  +     *be++ = '*';
       }
   
       /* optional precision */
  @@ -117,38 +130,74 @@
       }
   
       /* optional type modifier */
  -    if (info->is_long_double)
  -     *be++ = 'L';    /* XXX 'll' instead? */
  -    else if (info->is_long)
  -     *be++ = 'l';
  -    else if (info->is_short)
  -     *be++ = 'h';
  -    else if (info->is_char) {
  -     *be++ = 'h';
  -     *be++ = 'h';
  -    }
  -
  -    /* conversion character */
  -    switch (info->spec) {
  -    case 'c':
  -    case 's':
  -     if (info->wide)
  +    if (info->user) {
  +#if  USE_EXAMPLE_XMM
  +     if (info->user & mod_v4f)
  +         be = stpcpy(be, "v4f");
  +     if (info->user & mod_v2d)
  +         be = stpcpy(be, "v2d");
  +     if (info->user & mod_v16i)
  +         be = stpcpy(be, "v16i");
  +     if (info->user & mod_v8i)
  +         be = stpcpy(be, "v8i");
  +     if (info->user & mod_v4i)
  +         be = stpcpy(be, "v4i");
  +     if (info->user & mod_v2i)
  +         be = stpcpy(be, "v2i");
  +#endif
  +#if  USE_EXAMPLE_HEX
  +     if (info->user & mod_phh)
  +         be = stpcpy(be, "phh");
  +#ifdef       NOTYET
  +     if (info->user & mod_hC)
  +         be = stpcpy(be, "hC");
  +     if (info->user & mod_hD)
  +         be = stpcpy(be, "hD");
  +     if (info->user & mod_hN)
  +         be = stpcpy(be, "hN");
  +#endif
  +#endif
  +    } else {
  +     if (info->is_long_double)
  +         *be++ = 'L';        /* XXX 'll' instead? */
  +     else if (info->is_long)
            *be++ = 'l';
  -     /*@fallthrough@*/
  -    default:
  -     *be++ = info->spec;
  -     break;
  +     else if (info->is_short)
  +         *be++ = 'h';
  +     else if (info->is_char) {
  +         *be++ = 'h';
  +         *be++ = 'h';
  +     }
  +
  +     /* conversion character */
  +     switch (info->spec) {
  +     case 'c':
  +     case 's':
  +         if (info->wide)
  +             *be++ = 'l';
  +         break;
  +     }
       }
  +    *be++ = info->spec;
       *be = '\0';
   
  -    fprintf(stderr, "    %s info %p fmt \"%s\"\n", msg, info, b);
  +    return b;
  +}
  +
  +static void dumpPrintfInfo(const char *msg, const struct printf_info * info)
  +{
  +    char b[64];
  +
  +    fprintf(stderr, "    %s \"%s\"\n", msg, info2fmt(info, b));
   
       if (info->extra || info->i18n)
        fprintf(stderr, "flags: %s%s\n",
                (info->extra            ? " EXTRA " : ""),
                (info->i18n             ? " I18N " : ""));
  +#ifdef       DYING
       if (info->user) {
        fprintf(stderr, " user: %d", (unsigned)info->user);
  +#if  USE_EXAMPLE_XMM
        if (info->user & mod_v4f)
            fprintf(stderr, " %s", "v4f");
        if (info->user & mod_v2d)
  @@ -161,8 +210,22 @@
            fprintf(stderr, " %s", "v4i");
        if (info->user & mod_v2i)
            fprintf(stderr, " %s", "v2i");
  +#endif
  +#if  USE_EXAMPLE_HEX
  +     if (info->user & mod_phh)
  +         fprintf(stderr, " %s", "phh");
  +#ifdef       NOTYET
  +     if (info->user & mod_hC)
  +         fprintf(stderr, " %s", "hC");
  +     if (info->user & mod_hD)
  +         fprintf(stderr, " %s", "hD");
  +     if (info->user & mod_hN)
  +         fprintf(stderr, " %s", "hN");
  +#endif
  +#endif
        fprintf(stderr, "\n");
       }
  +#endif       /* DYING */
   }
   
   #ifdef       REFERENCE
  @@ -254,10 +317,10 @@
                %pISfc  1.2.3.4 or[1:2:3:4:5:6:7:8] / 123456789
                %pISsc  1.2.3.4 or[1:2:3:4:5:6:7:8] % 1234567890
                %pISpfc 1.2.3.4:12345 or[1:2:3:4:5:6:7:8]:12345 / 123456789
  -     %pUb    00010203 - 0405 - 0607 - 0809 - 0 a0b0c0d0e0f
  -     %pUB    00010203 - 0405 - 0607 - 0809 - 0 A0B0C0D0E0F
  -     %pUl    03020100 - 0504 - 0706 - 0809 - 0 a0b0c0e0e0f
  -     %pUL    03020100 - 0504 - 0706 - 0809 - 0 A0B0C0E0E0F
  +     %pUb    00010203-0405-0607-0809-0a0b0c0d0e0f
  +     %pUB    00010203-0405-0607-0809-0A0B0C0D0E0F
  +     %pUl    03020100-0504-0706-0809-0a0b0c0e0e0f
  +     %pUL    03020100-0504-0706-0809-0A0B0C0E0E0F
        %pd     { , 2, 3, 4}
        %pD     { , 2, 3, 4}
   
  @@ -527,6 +590,8 @@
   
   static void xmm_va(void *mem, va_list * ap)
   {
  +if (_tpf_debug)
  +fprintf(stderr, "--> %s(%p, %p)\n", __FUNCTION__, mem, ap);
       __m128 v = va_arg(*ap, __m128);
       memcpy(mem, &v, sizeof(v));
   }
  @@ -586,7 +651,6 @@
       return ret;
   }
   
  -
   static int
   xmm_printf_x(FILE * fp, const struct printf_info *info,
             const void *const *args)
  @@ -663,6 +727,208 @@
   
   /*==============================================================*/
   
  +#if  USE_EXAMPLE_HEX
  +
  +static const char hexdigs[] = "0123456789ABCDEF";
  +
  +typedef      union dataPtrs_u DataPtrs; 
  +union dataPtrs_u {
  +    void * ptr;
  +    uint8_t * ui8p;
  +    uint16_t * ui16p;
  +    uint32_t * ui32p;
  +    uint64_t * ui64p;
  +    const char * str;
  +    unsigned char * blob;
  +    const char ** argv;
  +};
  +
  +static void hex_va(void *mem, va_list * ap)
  +{
  +    DataPtrs p;
  +    p.ptr = va_arg(*ap, void *);
  +if (_tpf_debug)
  +fprintf(stderr, "\tmemcpy(%p, %p, %u) p.ptr %p\n", mem, &p, 
(unsigned)(sizeof(p)), p.ptr);
  +    memcpy(mem, &p, sizeof(p));
  +}
  +
  +static int
  +hex_ais(const struct printf_info *info, size_t n, int *argtypes, int *sizes)
  +{
  +    char b[64];
  +    int ret = 1;
  +
  +    argtypes[0] = pa_hex;
  +    sizes[0] = sizeof(uint8_t *);    /* XXX HACK */
  +if (_tpf_debug)
  +fprintf(stderr, "<-- %s(\"%s\", %u) ret %d %s *sizes %d pad '%c' field 
%u/%u\n", __FUNCTION__, info2fmt(info, b), (unsigned)n, ret, 
atype[*argtypes&0x1f], *sizes, info->pad, info->width, info->prec);
  +    return ret;
  +}
  +
  +static int
  +hex_printf_p(FILE * fp, const struct printf_info *info,
  +          const void *const *args)
  +{
  +    char b[64];
  +    int len = 0;
  +    int ret = 0;
  +
  +#ifdef       NOTYET
  +    if (info->user & mod_v4f) {
  +     DataPtrs u;
  +     memcpy(&u, *((void ***) args)[0], sizeof(__m128));
  +     char *buf;
  +     len = asprintf(&buf, "{%f, %f, %f, %f}", u.f[0], u.f[1], u.f[2],
  +                  u.f[3]);
  +     fputs(buf, fp);
  +     free(buf);
  +    } else if (info->user & mod_v2d) {
  +     DataPtrs u;
  +     memcpy(&u, *((void ***) args)[0], sizeof(__m128d));
  +     char *buf;
  +     len = asprintf(&buf, "{%f, %f}", u.d[0], u.d[1]);
  +     fputs(buf, fp);
  +     free(buf);
  +    } else
  +#endif
  +     ret = -2;
  +
  +if (_tpf_debug)
  +fprintf(stderr, "<-- %s(\"%s\") ret %d len %d\n", __FUNCTION__, 
info2fmt(info, b), ret, len);
  +    return ret;
  +}
  +
  +static int
  +hex_printf_dox(FILE * fp, const struct printf_info *info,
  +          const void *const *args)
  +{
  +    char b[64];
  +    int len = 0;
  +    int ret = 0;
  +
  +    if (info->user & mod_phh) {
  +     DataPtrs p;
  +     const char * pfx = NULL;
  +     int base;
  +     int ndigs;
  +     char pad;
  +     int ilen;
  +
  +     memcpy(&p, *((void ***) args)[0], sizeof(p.ptr));
  +
  +     pad = (info->pad == '0' ? '\0' : info->pad);
  +     switch (info->spec) {
  +     case 'd':
  +         pfx = NULL;
  +         base = 10;
  +         ndigs = 3;
  +         break;
  +     case 'o':
  +         pfx = (info->alt ? "0" : NULL);
  +         base = 8;
  +         ndigs = 3;
  +         break;
  +     case 'x':
  +     default:
  +         pfx = (info->alt ? "0x" : NULL);
  +         base = 16;
  +         ndigs = 2;
  +         break;
  +     }
  +     ilen = ndigs;
  +     if (info->alt) {
  +         pad = ',';
  +         if (pfx)
  +             ilen += strlen(pfx);
  +     }
  +     
  +     len = (pad
  +         ? (ilen+1) * info->width - 1
  +         : ilen * info->width );
  +     char *buf = malloc(len + 1);
  +     char *be = buf;
  +     for (int j = 0; j < info->width; j++) {
  +         unsigned v = p.ui8p[j];
  +         if (j > 0 && pad)
  +             *be++ = pad;
  +         if (pfx)
  +             be = stpcpy(be, pfx);
  +         switch (info->spec) {
  +         case 'd':
  +             itoa(v, be, base);
  +             be += strlen(be);
  +             break;
  +         case 'o':
  +             *be++ = hexdigs[(v >> 6) & 007];
  +             *be++ = hexdigs[(v >> 3) & 007];
  +             *be++ = hexdigs[(v     ) & 007];
  +             break;
  +         case 'x':
  +             *be++ = hexdigs[(v >> 4) & 0x0f];
  +             *be++ = hexdigs[(v     ) & 0x0f];
  +             break;
  +         }
  +         *be = '\0';
  +     }
  +     fputs(buf, fp);
  +     free(buf);
  +    } else if (info->user & mod_hC) {
  +#ifdef       NOTYET
  +fprintf(stderr, "\t%s %p\n", "hC", *((void ***)args)[0]);
  +     union {
  +         __m128i v;
  +         uint16_t i[8];
  +     } u;
  +     memcpy(&u, *((void ***) args)[0], sizeof(__m128i));
  +     char *buf;
  +     len = asprintf(&buf,
  +                  "{%" PRIx16 ", %" PRIx16 ", %" PRIx16 ", %" PRIx16
  +                  ", %" PRIx16 ", %" PRIx16 ", %" PRIx16 ", %" PRIx16
  +                  "}", u.i[0], u.i[1], u.i[2], u.i[3], u.i[4], u.i[5],
  +                  u.i[6], u.i[7]);
  +     fputs(buf, fp);
  +     free(buf);
  +#endif
  +    } else if (info->user & mod_hD) {
  +#ifdef       NOTYET
  +fprintf(stderr, "\t%s %p\n", "hD", *((void ***)args)[0]);
  +     union {
  +         __m128i v;
  +         uint32_t i[4];
  +     } u;
  +     memcpy(&u, *((void ***) args)[0], sizeof(__m128i));
  +     char *buf;
  +     len = asprintf(&buf,
  +                  "{%" PRIx32 ", %" PRIx32 ", %" PRIx32 ", %" PRIx32
  +                  "}", u.i[0], u.i[1], u.i[2], u.i[3]);
  +     fputs(buf, fp);
  +     free(buf);
  +#endif
  +    } else if (info->user & mod_hN) {
  +#ifdef       NOTYET
  +fprintf(stderr, "\t%s %p\n", "hN", *((void ***)args)[0]);
  +     union {
  +         __m128i v;
  +         uint64_t i[2];
  +     } u;
  +     memcpy(&u, *((void ***) args)[0], sizeof(__m128i));
  +     char *buf;
  +     len = asprintf(&buf, "{%" PRIx64 ", %" PRIx64 "}", u.i[0], u.i[1]);
  +     fputs(buf, fp);
  +     free(buf);
  +#endif
  +    } else
  +     ret = -2;
  +
  +if (_tpf_debug)
  +fprintf(stderr, "<-- %s(\"%s\") ret %d len %d\n", __FUNCTION__, 
info2fmt(info, b), ret, len);
  +
  +    return ret;
  +}
  +#endif                               /* USE_EXAMPLE_HEX */
  +
  +/*==============================================================*/
  +
   #pragma GCC diagnostic push
   #pragma GCC diagnostic ignored "-Wformat"
   #pragma GCC diagnostic ignored "-Wformat-extra-args"
  @@ -803,6 +1069,82 @@
   
   /*==============================================================*/
   
  +#if  USE_EXAMPLE_HEX
  +
  +fprintf(stderr, "==> HEX\n");
  +
  +    pa_hex = register_printf_type(hex_va);
  +
  +    mod_phh = register_printf_modifier(L"phh");
  +#ifdef       NOTYET
  +    mod_hC = register_printf_modifier(L"hC");
  +    mod_hD = register_printf_modifier(L"hD");
  +    mod_hN = register_printf_modifier(L"hN");
  +#endif
  +
  +    if (!_tpf_debug) {
  +     register_printf_specifier('p', hex_printf_p, hex_ais);
  +     register_printf_specifier('d', hex_printf_dox, hex_ais);
  +    }
  +    register_printf_specifier('o', hex_printf_dox, hex_ais);
  +    register_printf_specifier('x', hex_printf_dox, hex_ais);
  +
  +    uint8_t b[] = {
  +     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  +     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  +     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  +     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  +    };
  +
  +fprintf(stderr, "========== %p[%u]\n", b, sizeof(b));
  +
  +    if (!_tpf_debug) {
  +#ifdef       BUGGY
  +     fprintf(fp, "%%*phhp[%u]: %*phhp[%u]\n", sizeof(b)/4, b, sizeof(b));    
fflush(fp);
  +#endif
  +     fprintf(fp, "   %%*phhp: %*phhp\n", sizeof(b)/4, b);    fflush(fp);
  +#ifdef       NOTYET
  +     fprintf(fp, "%*hp\n", sizeof(b), b);    fflush(fp);
  +     fprintf(fp, "%*hCp\n", sizeof(b), b);   fflush(fp);
  +     fprintf(fp, "%*hDp\n", sizeof(b), b);   fflush(fp);
  +     fprintf(fp, "%*hNp\n", sizeof(b), b);   fflush(fp);
  +
  +     fprintf(fp, "%#lx\n", (unsigned long)b);        fflush(fp);
  +#endif
  +     fprintf(fp, "   %%*phhd: %*phhd\n", sizeof(b)/4, b);    fflush(fp);
  +     fprintf(fp, "%%*.16phhd: %*.16phhd\n", sizeof(b)/4, b); fflush(fp);
  +     fprintf(fp, "  %% *phhd: % *phhd\n", sizeof(b)/4, b);   fflush(fp);
  +#ifdef       BUGGY   /* XXX same as %u */
  +     fprintf(fp, "  %%* phhd: %* phhd\n", sizeof(b)/4, b);   fflush(fp);
  +#endif
  +     fprintf(fp, "  %%0*phhd: %0*phhd\n", sizeof(b)/4, b);   fflush(fp);
  +     fprintf(fp, "  %%#*phhd: %#*phhd\n", sizeof(b)/4, b);   fflush(fp);
  +     fprintf(fp, "  %%'*phhd: %'*phhd\n", sizeof(b)/4, b);   fflush(fp);
  +    }
  +    fprintf(fp, "   %%*phhx: %*phhx\n", sizeof(b)/4, b);     fflush(fp);
  +    fprintf(fp, "%%*.16phhx: %*.16phhx\n", sizeof(b)/4, b);  fflush(fp);
  +    fprintf(fp, "  %% *phhx: % *phhx\n", sizeof(b)/4, b);    fflush(fp);
  +#ifdef       BUGGY   /* XXX same as %u */
  +    fprintf(fp, "  %%* phhx: %* phhx\n", sizeof(b)/4, b);    fflush(fp);
  +#endif
  +    fprintf(fp, "  %%0*phhx: %0*phhx\n", sizeof(b)/4, b);    fflush(fp);
  +    fprintf(fp, "  %%#*phhx: %#*phhx\n", sizeof(b)/4, b);    fflush(fp);
  +    fprintf(fp, "  %%'*phhx: %'*phhx\n", sizeof(b)/4, b);    fflush(fp);
  +
  +    fprintf(fp, "   %%*phho: %*phho\n", sizeof(b)/4, b);     fflush(fp);
  +    fprintf(fp, "  %%#*phho: %#*phho\n", sizeof(b)/4, b);    fflush(fp);
  +#ifdef       NOTYET
  +    fprintf(fp, "%*hCx\n", sizeof(b), b);    fflush(fp);
  +    fprintf(fp, "%*hDx\n", sizeof(b), b);    fflush(fp);
  +    fprintf(fp, "%*hNx\n", sizeof(b), b);    fflush(fp);
  +#endif
  +
  +fprintf(stderr, "<== HEX\n");
  +
  +#endif                               /* USE_EXAMPLE_XMM */
  +
  +/*==============================================================*/
  +
       return 0;
   }
   
  @@ .
______________________________________________________________________
RPM Package Manager                                    http://rpm5.org
CVS Sources Repository                                rpm-cvs@rpm5.org

Reply via email to