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

  Server: rpm5.org                         Name:   Olivier Thauvin
  Root:   /v/rpm/cvs                       Email:  [EMAIL PROTECTED]
  Module: rpm                              Date:   21-Jul-2007 19:28:20
  Branch: HEAD                             Handle: 2007072118281900

  Added files:
    rpm/rpmconstant         Makefile.am constant.c rpmconstant.c rpmconstant.h
                            rpmconstant_internal.c rpmh2tbl
  Modified files:
    rpm                     configure.ac

  Log:
    - add rpmconstant lib

  Summary:
    Revision    Changes     Path
    2.209       +1  -0      rpm/configure.ac
    1.1         +37 -0      rpm/rpmconstant/Makefile.am
    1.1         +108 -0     rpm/rpmconstant/constant.c
    1.1         +191 -0     rpm/rpmconstant/rpmconstant.c
    1.1         +209 -0     rpm/rpmconstant/rpmconstant.h
    1.1         +60 -0      rpm/rpmconstant/rpmconstant_internal.c
    1.1         +214 -0     rpm/rpmconstant/rpmh2tbl
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/configure.ac
  ============================================================================
  $ cvs diff -u -r2.208 -r2.209 configure.ac
  --- rpm/configure.ac  19 Jul 2007 19:07:56 -0000      2.208
  +++ rpm/configure.ac  21 Jul 2007 17:28:19 -0000      2.209
  @@ -1224,6 +1224,7 @@
       doc/manual/Makefile doc/fr/Makefile doc/ja/Makefile doc/ko/Makefile
       doc/pl/Makefile doc/ru/Makefile doc/sk/Makefile python/Makefile
       python/rpm/Makefile scripts/rpm.pc
  +    rpmconstant/Makefile
   ])
   
   dnl # explicit list of commands to execute
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/Makefile.am
  ============================================================================
  $ cvs diff -u -r0 -r1.1 Makefile.am
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ Makefile.am       2007-07-21 19:28:19 +0200
  @@ -0,0 +1,37 @@
  +# $Id: Makefile.am,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  +
  +AUTOMAKE_OPTIONS = 1.4 foreign
  +
  +LINT = splint
  +
  +AM_CPPFLAGS = \
  +     -I$(srcdir) \
  +     -I$(top_srcdir) \
  +     -I$(top_srcdir)/build \
  +     -I$(top_srcdir)/lib \
  +     -I$(top_srcdir)/rpmdb \
  +     -I$(top_srcdir)/rpmio \
  +     -I$(top_srcdir)/misc
  +
  +bin_PROGRAMS = rpmconstant
  +lib_LTLIBRARIES     = librpmconstant.la
  +librpmconstant_la_LDFLAGS = -release $(LT_CURRENT).$(LT_REVISION)
  +librpmconstant_la_SOURCES = rpmconstant.c \
  +                                 rpmconstant.h \
  +                                                 rpmconstanttbl.c \
  +                                                     rpmconstant_internal.c \
  +                                                     rpmh2tbl
  +
  +rpmconstant_SOURCES = constant.c
  +rpmconstant_LDADD   = librpmconstant.la
  +pkgincdir = $(pkgincludedir)
  +pkginc_HEADERS = rpmconstant.h
  +
  +BUILT_SOURCES = rpmconstanttbl.c
  +CLEANFILES = rpmconstanttbl.c
  +
  +rpmconstanttbl.c: Makefile.am rpmh2tbl
  +     $(srcdir)/rpmh2tbl $(top_srcdir) 
$(top_srcdir)/{lib,build,rpmdb,rpmio,misc}/*.h > $@
  +
  +rpmh2tbl:
  +
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/constant.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 constant.c
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ constant.c        2007-07-21 19:28:20 +0200
  @@ -0,0 +1,108 @@
  +/* Nanar <[EMAIL PROTECTED]>
  + * $Id: constant.c,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  + */
  +
  +#include <stdio.h>
  +#include <string.h>
  +#include "rpmconstant.h"
  +#include <popt.h>
  +
  +
  +
  +int main(int argc, const char *argv[]) {
  +    char * context =NULL;
  +    char c;
  +    const char * name;
  +    int val = 0;
  +    int token;
  +    long qmode = 0;
  +    int showprefix;
  +    
  +    poptContext optcon;
  +
  +#define QMODE_LIST (1 << 0)
  +#define QMODE_SET (1 << 1)
  +#define QMODE_REVERSE (1 << 2)
  +#define QMODE_SHOWPREFIX (1 << 3)
  +    
  +    struct poptOption optionstable[] = {
  +        { "context", 'c', POPT_ARG_STRING, &context, 1, "Set context of 
search", "Context" },
  +        { "list-context", 'l', POPT_BIT_SET, &qmode, QMODE_LIST, "list 
available table", "List" },
  +        { "set", 's', POPT_BIT_SET, &qmode, QMODE_SET, "Show the value for 
all flag", NULL },
  +        { "reverse", 'r', POPT_BIT_SET, &qmode, QMODE_REVERSE, "Make a 
reverse query", "Reverse" },
  +        { "prefix", 'p', POPT_BIT_SET, &qmode, QMODE_SHOWPREFIX, "Show 
prefix on constant", "Prefix" }, 
  +        POPT_AUTOHELP
  +        POPT_TABLEEND
  +    };
  +
  +    optcon = poptGetContext(NULL, argc, argv, optionstable, 0);
  +    
  +    while ((c = poptGetNextOpt(optcon)) >= 0) {}
  +
  +    if (!(qmode & QMODE_LIST) && poptPeekArg(optcon) == NULL) {
  +        poptPrintUsage(optcon, stderr, 0);
  +        exit(1);
  +    }
  +
  +    if (qmode & QMODE_SHOWPREFIX)
  +        showprefix = PREFIXED_YES;
  +    else
  +        showprefix = PREFIXED_NO;
  +
  +    if (qmode & QMODE_LIST) {
  +        rpmconst consti = rpmconstNew();
  +        if (context) {
  +            if (rpmconstInitToContext(consti, context)) {
  +                while (rpmconstNextC(consti)) {
  +                    printf("%s: %d\n", rpmconstName(consti, showprefix), 
rpmconstValue(consti));
  +                }
  +            } else {
  +                printf("context '%s' not found\n", context);
  +            }
  +        } else {
  +            while (rpmconstNextL(consti)) {
  +                printf("%s\n", rpmconstContext(consti));
  +            }
  +        }
  +        consti = rpmconstFree(consti);
  +        return 0;
  +    }
  +    while ((name = poptGetArg(optcon)) != NULL) {
  +        if (qmode & QMODE_REVERSE) {
  +            if(sscanf(name, "%d", &token)) {
  +                if (qmode & QMODE_SET) {
  +                    printf("%d:", token);
  +                    int i = 0;
  +                    while ((val = rpmconstantFindMask(context, token, (void 
*) &name, showprefix))) {
  +                        printf(" %s", name);
  +                        token &= ~val;
  +                        if (++i > 20) return 0; 
  +                    }
  +                    printf("\n");
  +                } else {
  +                    if (rpmconstantFindValue(context, token, (void *) &name, 
showprefix))
  +                        printf("%d: %s\n", token, name);
  +                    else
  +                        printf("%d: Not found\n", token);
  +                }
  +            } else {
  +                printf("%s is not a integer value\n", name);
  +            }
  +        } else if(context) {
  +            if (rpmconstantFindName(context, name, &val, 0)) {
  +                if (~qmode & QMODE_SET) {
  +                    printf("%s: %d\n", name, val);
  +                    val = 0; /* resetting */
  +                }
  +            } else
  +                printf("%s: Not found\n", name);
  +        } else {
  +            poptPrintUsage(optcon, stderr, 0);
  +            exit(1);
  +        }
  +    }
  +    if (qmode & QMODE_SET && !(qmode & QMODE_REVERSE))
  +        printf("Value: %d\n", val);
  +    poptFreeContext(optcon);
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/rpmconstant.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 rpmconstant.c
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ rpmconstant.c     2007-07-21 19:28:20 +0200
  @@ -0,0 +1,191 @@
  +/* Nanar <[EMAIL PROTECTED]>
  + * $Id: rpmconstant.c,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  + */
  +
  +#include <string.h>
  +#define RPMCONSTANT_INTERNAL
  +#include "rpmconstant.h"
  +
  +rpmconst rpmconstNew()
  +{
  +    rpmconst c = NULL;
  +    c = xcalloc(1, sizeof(*c));
  +    c->list = NULL;
  +    c->constant = NULL;
  +    return c;
  +}
  +
  +rpmconst rpmconstFree(rpmconst c)
  +{
  +    free(c);
  +    return c = NULL;
  +}
  +
  +void rpmconstInitC(rpmconst c)
  +{
  +    c->constant = NULL;
  +}
  +
  +int rpmconstNextC(rpmconst c)
  +{
  +    if (c->list == NULL)
  +        return 0;
  +    c->constant = c->constant == NULL ?
  +        rpmConstantListC(c->list) :
  +        rpmConstantNext(c->constant);
  +    return c->constant == NULL ? 0 : 1;
  +}
  +
  +
  +void rpmconstInitL(rpmconst c)
  +{
  +    c->list = NULL;
  +    c->constant = NULL;
  +}
  +
  +int rpmconstNextL(rpmconst c)
  +{
  +    c->list = c->list == NULL ?
  +        (void *) rpmconstanttype :
  +        rpmConstantListNext(c->list);
  +    c->constant = NULL;
  +    return c->list == NULL ? 0 : 1;
  +}
  +
  +const char * rpmconstContext(rpmconst c)
  +{
  +    return rpmConstantListContext(c->list);
  +}
  +
  +const char * rpmconstPrefix(rpmconst c)
  +{
  +    return rpmConstantListPrefix(c->list);
  +}
  +
  +const char * rpmconstName(rpmconst c, int stripprefix)
  +{
  +    char * name;
  +    int len;
  +    name = rpmConstantName(c->constant);
  +    if (stripprefix && name && rpmConstantListPrefix(c->list)) {
  +        len = strlen(rpmConstantListPrefix(c->list));
  +        name += len < strlen(name) ? len : 0;
  +    }
  +    return name;
  +}
  +
  +int rpmconstValue(rpmconst c)
  +{
  +    return rpmConstantValue(c->constant);
  +}
  +
  +int rpmconstInitToContext(rpmconst c, const char * context)
  +{
  +    char * lccontext = strdup(context);
  +    char * ptr;
  +    int rc = 0;
  +    for (ptr = lccontext; *ptr != 0; ptr++)
  +        *ptr = tolower(*ptr);
  +    if (!context) return 0; /* programmer error */
  +    rpmconstInitL(c);
  +    while (rpmconstNextL(c)) {
  +        if (!strcmp(lccontext, rpmconstContext(c))) {
  +            rc = 1;
  +            break;
  +        }
  +    }
  +    free(lccontext);
  +    return rc; /* not found */
  +}
  +
  +int rpmconstNameMatch(rpmconst c, const char * name, int prefixed)
  +{
  +    char * uc;
  +    int rc = 0;
  +    char * ucname = strdup(name);
  +    
  +    for (uc = ucname; *uc != 0; uc++)
  +        *uc = toupper(*uc);
  +    
  +    if (!prefixed) prefixed = ALLCASE_PREFIX;
  +    if (prefixed & WITH_PREFIX)
  +        if (strcmp(ucname, rpmconstName(c, PREFIXED_YES)) == 0)
  +            rc = 1;
  +    if (!rc && (prefixed & WITHOUT_PREFIX))
  +        if (strcmp(ucname, rpmconstName(c, PREFIXED_NO)) == 0)
  +            rc = 1;
  +    free(ucname);
  +    return rc;
  +}
  +
  +int rpmconstFindValue(rpmconst c, const int val)
  +{
  +    rpmconstInitC(c);
  +    while (rpmconstNextC(c)) {
  +        if (val == rpmconstValue(c))
  +            return 1;
  +    }
  +    return 0;
  +}
  +
  +int rpmconstFindMask(rpmconst c, const int val)
  +{
  +    rpmconstInitC(c);
  +    while (rpmconstNextC(c)) {
  +        if (!rpmconstValue(c))
  +            continue;
  +        if (rpmconstValue(c) & val)
  +            return 1;
  +    }
  +    return 0;
  +}
  +
  +int rpmconstFindName(rpmconst c, const char * name, int prefixed)
  +{
  +    rpmconstInitC(c);
  +    while (rpmconstNextC(c)) {
  +        if (rpmconstNameMatch(c, name, prefixed))
  +            return 1;
  +    }
  +    return 0;
  +}
  +
  +int rpmconstantFindValue(char * context, const int val, const char **name, 
int prefixed)
  +{
  +    int rc = 0;
  +    rpmconst c = rpmconstNew();
  +    if (rpmconstInitToContext(c, context))
  +        if (rpmconstFindValue(c, val)) {
  +            *name = rpmconstName(c, prefixed);
  +            rc = 1;
  +        }
  +    c = rpmconstFree(c);
  +    return rc;
  +}
  +    
  +int rpmconstantFindMask(char * context, const int val, const char **name, 
int prefixed)
  +{
  +    int rc = 0;
  +    rpmconst c = rpmconstNew();
  +    if (rpmconstInitToContext(c, context))
  +        if (rpmconstFindMask(c, val)) {
  +            *name = rpmconstName(c, prefixed);
  +            rc = rpmconstValue(c);
  +        }
  +    c = rpmconstFree(c);
  +    return rc;
  +}
  +
  +int rpmconstantFindName(char * context, const char * name, int *val, int 
prefixed)
  +{
  +    int rc = 0;
  +    rpmconst c = rpmconstNew();
  +    if (rpmconstInitToContext(c, context)) {
  +        if (rpmconstFindName(c, name, prefixed)) {
  +            *val |= rpmconstValue(c);
  +            rc = 1;
  +        }
  +    }
  +    c = rpmconstFree(c);
  +    return rc;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/rpmconstant.h
  ============================================================================
  $ cvs diff -u -r0 -r1.1 rpmconstant.h
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ rpmconstant.h     2007-07-21 19:28:20 +0200
  @@ -0,0 +1,209 @@
  +/* Nanar <[EMAIL PROTECTED]>
  + * $Id: rpmconstant.h,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  + */
  +
  +#ifndef H_RPMCONSTANT
  +#define H_RPMCONSTANT
  +
  +#ifndef xcalloc
  +#define xcalloc(n,s) calloc((n),(s))
  +#endif
  +
  +#define PREFIXED_YES 0
  +#define PREFIXED_NO  1
  +
  +#define WITH_PREFIX (1 << 0)
  +#define WITHOUT_PREFIX (1 << 1)
  +#define ALLCASE_PREFIX (WITH_PREFIX | WITHOUT_PREFIX)
  +
  +/**
  + * \ingroup rpmconstant
  + * \file rpmconstant.h
  + *
  + */
  +
  +#include <stdio.h>
  +#include <header.h>
  +#include <rpmio.h>
  +#include <rpmdb.h>
  +#include <rpmts.h>
  +#include <rpmte.h>
  +#include <rpmps.h>
  +#include <rpmds.h>
  +#include <rpmfi.h>
  +#include <rpmpgp.h>
  +#include <misc.h>
  +#include <rpmbuild.h>
  +#include <rpmlib.h>
  +
  +/**
  + * A constant pair name/value
  + */
  +typedef /[EMAIL PROTECTED]@*/ struct rpmconstant_s *rpmconstant;
  +
  +
  +/**
  + * A constant list set
  + */
  +typedef /[EMAIL PROTECTED]@*/ struct rpmconstantlist_s * rpmconstantlist;
  +
  +typedef struct rpmconst_s * rpmconst;
  +
  +#ifdef RPMCONSTANT_INTERNAL
  +
  +/**
  + * A constant pair name/value
  + */
  +struct rpmconstant_s {
  +    const char * name; /*!< Constant name. */
  +/[EMAIL PROTECTED]@*/
  +    int value; /*!< Constant value. */
  +};
  +
  +/**
  + * A contantlist entry
  + */
  +struct rpmconstantlist_s {
  +    const rpmconstant constant; /*<! Constant pointer */
  +/[EMAIL PROTECTED]@*/
  +    char *context; /*<! Name of the list */
  +    char *prefix; /*<! Common prefix of constant name */
  +/[EMAIL PROTECTED]@*/
  +};
  +
  +struct rpmconst_s {
  +    rpmconstantlist list;
  +    rpmconstant constant;
  +};
  +
  +/**
  + * Pointer to first element of rpmconstantlist.
  + */
  +/[EMAIL PROTECTED]@*/
  +/[EMAIL PROTECTED]@*/ /[EMAIL PROTECTED]@*/
  +extern const struct rpmconstantlist_s * rpmconstanttype;
  +/[EMAIL PROTECTED]@*/
  +
  +/**
  + * Return name from contant item.
  + *
  + * @param c     constant item
  + * @return      pointer to name from item
  + */
  +const char * rpmConstantName(rpmconstant c)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return value from constant item.
  + *
  + * @param c     constant item
  + * @return      value from current item
  + */
  +int rpmConstantValue(rpmconstant c)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return next constant item from constant list (or NULL at end of list).
  + *
  + * @param c     current constant item
  + * @return      next constant item
  + */
  +/[EMAIL PROTECTED]@*/
  +rpmconstant rpmConstantNext(rpmconstant c)
  +    /[EMAIL PROTECTED]/;
  +
  +
  +/**
  + * Get a pointer to first rpmconstantlist item
  + *
  + * @return      first constantlist item
  + */
  +rpmconstantlist rpmGetConstantList()
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return next constantlist item from list (or NULL at the end of list).
  + *
  + * @param cl    current constantlist item
  + * @return      next constantlist item
  + */
  +/[EMAIL PROTECTED]@*/
  +rpmconstantlist rpmConstantListNext(rpmconstantlist cl)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return constantlist item corresponding to context
  + *
  + * @param context   ptr to constext
  + * @return          constant list item matching context
  + */
  +rpmconstantlist rpmGetConstantListFromContext(const char * context)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return a pointer to first constant item from constantlist.
  + *
  + * @param cl    constantlist item
  + * @retval      first constant item from list
  + */
  +rpmconstant rpmConstantListC(rpmconstantlist cl)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return the common prefix of constant name.
  + *
  + * @param cl    constantlist item
  + * @return      pointer to prefix string (or NULL is not availlable)
  + */
  +/[EMAIL PROTECTED]@*/
  +const char * rpmConstantListPrefix (rpmconstantlist cl)
  +    /[EMAIL PROTECTED]/;
  +
  +/**
  + * Return context name from constantlist item
  + *
  + * @param cl    constantlist item
  + * @return      pointer to context name
  + */
  +const char * rpmConstantListContext (rpmconstantlist cl)
  +    /[EMAIL PROTECTED]/;
  +
  +#endif
  +
  +rpmconst rpmconstNew();
  +
  +rpmconst rpmconstFree(rpmconst c);
  +
  +void rpmconstInitC(rpmconst c);
  +
  +int rpmconstNextC(rpmconst c);
  +
  +void rpmconstInitL(rpmconst c);
  +
  +int rpmconstNextL(rpmconst c);
  +
  +const char * rpmconstContext(rpmconst c);
  +
  +const char * rpmconstPrefix(rpmconst c);
  +
  +const char * rpmconstName(rpmconst c, int stripprefix);
  +
  +int rpmconstValue(rpmconst c);
  +
  +int rpmconstInitToContext(rpmconst c, const char * context);
  +
  +int rpmconstNameMatch(rpmconst c, const char * name, int prefixed);
  +
  +int rpmconstFindValue(rpmconst c, const int val);
  +
  +int rpmconstFindMask(rpmconst c, const int val);
  +
  +int rpmconstFindName(rpmconst c, const char * name, int prefixed);
  +
  +int rpmconstantFindValue(char * context, const int val, const char **name, 
int prefixed);
  +
  +int rpmconstantFindMask(char * context, const int val, const char **name, 
int prefixed);
  +
  +int rpmconstantFindName(char * context, const char * name, int *val, int 
prefixed);
  +    
  +#endif /* H_RPMCONSTANT */
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/rpmconstant_internal.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 rpmconstant_internal.c
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ rpmconstant_internal.c    2007-07-21 19:28:20 +0200
  @@ -0,0 +1,60 @@
  +/* Nanar <[EMAIL PROTECTED]>
  + * $Id: rpmconstant_internal.c,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  + */
  +
  +#include <string.h>
  +#define RPMCONSTANT_INTERNAL
  +#include "rpmconstant.h"
  +
  +const char * rpmConstantName(rpmconstant c)
  +{
  +    return c->name;
  +}
  +
  +int rpmConstantValue(rpmconstant c)
  +{
  +    return c->value;
  +}
  +
  +rpmconstant rpmConstantNext(rpmconstant c)
  +{
  +    return (c + 1)->name ? c + 1 : NULL;
  +}
  +
  +/**/
  +
  +rpmconstantlist rpmGetConstantList()
  +{
  +    return (void *) rpmconstanttype;
  +}
  +
  +rpmconstantlist rpmConstantListNext(rpmconstantlist cl)
  +{
  +    return (cl + 1)->constant ? cl + 1 : NULL;
  +}
  +
  +rpmconstantlist rpmGetConstantListFromContext(const char * context)
  +{
  +    rpmconstantlist cl;
  +    for (cl = rpmGetConstantList(); cl; cl=rpmConstantListNext(cl)) {
  +        if (context && strcmp(context, rpmConstantListContext(cl)) == 0)
  +            return cl;
  +    }
  +    return NULL;
  +}
  +
  +const char * rpmConstantListPrefix (rpmconstantlist cl)
  +{
  +    return cl->prefix;
  +}
  +
  +const char * rpmConstantListContext (rpmconstantlist cl)
  +{
  +    return cl->context;
  +}
  +
  +rpmconstant rpmConstantListC(rpmconstantlist cl)
  +{
  +    return cl->constant;
  +}
  +
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmconstant/rpmh2tbl
  ============================================================================
  $ cvs diff -u -r0 -r1.1 rpmh2tbl
  --- /dev/null 2007-07-21 19:22:00 +0200
  +++ rpmh2tbl  2007-07-21 19:28:20 +0200
  @@ -0,0 +1,214 @@
  +#!/usr/bin/perl
  +
  +##- Nanar <[EMAIL PROTECTED]>
  +##-
  +##- This program is free software; you can redistribute it and/or modify
  +##- it under the terms of the GNU General Public License as published by
  +##- the Free Software Foundation; either version 2, or (at your option)
  +##- any later version.
  +##-
  +##- This program is distributed in the hope that it will be useful,
  +##- but WITHOUT ANY WARRANTY; without even the implied warranty of
  +##- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  +##- GNU General Public License for more details.
  +##-
  +##- You should have received a copy of the GNU General Public License
  +##- along with this program; if not, write to the Free Software
  +##- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
USA.
  +#
  +# $Id: rpmh2tbl,v 1.1 2007/07/21 17:28:19 nanardon Exp $
  +
  +use strict;
  +use warnings;
  +
  +use Getopt::Long;
  +
  +my @availlabletables;
  +
  +# Binding we want
  +# In this file we'll found
  +# FILE.h => 
  +#   Constant matching the REGEXP goes in this table.
  +#   'REGEXP' => table,
  +
  +my %file_const = (
  +    'rpmts.h' => {
  +        '^_?(RPMVSF_[\w_]+)' => 'rpmvsflags',
  +        '^(RPMTRANS_TYPE_[\w_]+)' => 'rpmtsType',
  +        '^(RPMTS_OP_[\w_]+)' => 'rpmtsOpX',
  +    },
  +    'rpmlib.h' => {
  +        '^(RPMDBI_[\w_]+)' => 'rpmdbi',
  +        '^_?(RPMSENSE_[\w_]+)' => 'rpmsenseflags',
  +        '^(RPMTAG_[\w_]+)' => 'rpmTag',
  +        '^(RPMRC_[\w_]+)' => 'rpmRC',
  +        '^(RPMFILE_[^(STATE_)][\w_]+)' => 'rpmfileAttrs',
  +        '^(RPMFILE_STATE_[\w_]+)' => 'rpmfileState',
  +        '^(RPM_MACHTABLE_[\w_]+)' => 'rpm_machtable',
  +        '^(RPMPROB_FILTER_[\w_]+)' => 'rpmprobFilterFlags',
  +        '^(FA_[\w_]+)' => 'fileAction',
  +        '^(PIPE|CDEV|XDIR|BDEV|REG|LINK|SOCK)$' => 'fileTypes',
  +        '^(RPMTRANS_FLAG_[\w_]+)' => 'rpmtransFlags',
  +        '^(INSTALL_[\w_]+)' => 'rpmInstallInterfaceFlags',
  +        '^(UNINSTALL_[\w_]+)' => 'rpmEraseInterfaceFlags',
  +        '^(RPMSIGTAG_[\w_]+)' => 'rpmtagSignature',
  +    },
  +    'rpmps.h' => {
  +        '^(RPMPROB_[\w_]+)' => 'rpmProblemType',
  +    },
  +    'rpmcli.h' => {
  +        '^(RPMSIGN_[\w_]+)' => 'rpmSignFlags',
  +        '^(VERIFY_[\w_]+)' => 'rpmVerifyFlags',
  +    },
  +    'rpmdb.h' => {
  +        '^(RPMMIRE_[\w_]+)' => 'rpmMireMode',
  +    },
  +    'header.h' => {
  +        '^(HEADER_MAGIC_[\w_]+)' => 'hMagic',
  +        '^(RPM_[\w]+)_TYPE$' => 'rpmTagType',
  +    },
  +    'rpmbuild.h' => {
  +        '^(RPMBUILD_[\w_]+)' => 'rpmBuildFlags',
  +        '^(PART_[\w_]+)' => 'rpmParseState',
  +    },
  +    'rpmerr.h' => {
  +        '^(RPMERR_[\w_]+)' => 'rpmerrCode',
  +    },
  +    'rpmmacro.h' => {
  +        '^(RMIL_[\w_]+)' => 'rpmMacrosContext',
  +    },
  +    'rpmte.h' => {
  +        '^(TR_[\w_]+)' => 'rpmElementType',
  +    },
  +    'rpmlog.h' => {
  +        '^(RPMLOG_(?!PRI$|MAKEPRI$|FAC$|MASK$|UPTO$)[\w_]+)' => 'rpmlog',
  +    },
  +    'rpmmessage.h' => {
  +        '^(RPMCALLBACK_[\w_]+)' => 'rpmCallbackType',
  +        '^(RPMMESS_[\w_]+)' => 'rpmmess',
  +    },
  +);
  +
  +my %tableprefix = (
  +    rpmmess => 'RPMMESS_',
  +    rpmCallbackType => 'RPMCALLBACK_',
  +    rpmlog => 'RPMLOG_',
  +    rpmdbi => 'RPMDBI_',
  +    rpmElementType => 'TR_',
  +    rpmMacrosContext => 'RMIL_',
  +    rpmerrCode => 'RPMERR_',
  +    rpmParseState => 'PART_',
  +    rpmBuildFlags => 'RPMBUILD_',
  +    rpmTagType => 'RPM_',
  +    hMagic => '',
  +    rpmMireMode => 'RPMMIRE_',
  +    rpmsenseflags => 'RPMSENSE_',
  +    rpmtsTransFlag => 'RPMVSF_',
  +    rpmTag => 'RPMTAG_',
  +    rpmtsType => 'RPMTRANS_TYPE_',
  +    rpmtsOpX => 'RPMTS_OP_',
  +    rpmRC => 'RPMRC_',
  +    rpmfileState => 'RPMFILE_STATE_',
  +    rpmfileState => 'RPMFILE_',
  +    rpm_machtable => 'RPM_MACHTABLE_',
  +    rpmprobFilterFlags => 'RPMPROB_FILTER_',
  +    fileAction => 'FA_',
  +    fileTypes => '',
  +    rpmtransFlags => 'RPMTRANS_FLAG_',
  +    rpmInstallInterfaceFlags => 'INSTALL_',
  +    rpmEraseInterfaceFlags => 'UNINSTALL_',
  +    rpmtagSignature => 'RPMSIGTAG_',
  +    rpmProblemType => 'RPMPROB_',
  +    rpmSignFlags => 'RPMSIGN_',
  +    rpmVerifyFlags => 'VERIFY_',
  +    rpmvsflags => 'RPMVSF_',
  +);
  +
  +
  +my $ch = *STDOUT;
  +
  +sub parseconst {
  +    my ($header) = @_;
  +    my ($hbasename) = $header =~ m#(?:.*/)(.*)$#;
  +
  +    my $hconstant = $file_const{$hbasename} or return;
  +    open(my $hheader, "<", $header) or die "Can't open $header\n";
  +
  +    my %constants_found;
  +    my $i;
  +    
  +    my $line = <$hheader>;
  +    $line =~ /^\s*#\s*ifndef\s+(\S+)/;
  +    my $headerdef = $1 if($1);
  +    
  +    while ($line = <$hheader>) {
  +        $line =~ s#^\s*/\*[^\*]*\*/##;
  +        my ($w, $c) = $line =~ 
m!(?:#\s*define\s*)?([\w_]+)[^(/\*)]*(/\*.*\*/)?!;
  +        defined($w) or next;
  +        foreach my $regexp (keys %{$hconstant}) {
  +            if ($w =~ /$regexp/) {
  +                $constants_found{$hconstant->{$regexp}}{$w}{n} ||= ++$i;
  +                $constants_found{$hconstant->{$regexp}}{$w}{c} ||= $c;
  +                $constants_found{$hconstant->{$regexp}}{$w}{s} ||= $1;
  +            }
  +        }
  +    }
  +
  +    close($hheader);
  +
  +    while (my ($tbl, $const) = each (%constants_found)) {
  +        $tableprefix{$tbl} ||= "";
  +        print $ch <<EOF;
  +
  +/* From $hbasename
  + * prefix tag: $tableprefix{$tbl}
  + * table: $tbl */
  +EOF
  +        printf $ch "static const struct rpmconstant_s %sctbl[] = {\n",
  +            $tbl;
  +        print $ch "#ifdef $headerdef\n" if ($headerdef);
  +        foreach my $c (sort { $const->{$a}{n} <=> $const->{$b}{n} } keys 
%$const) {
  +            printf $ch "\t{ \"%s\", %s }, %s\n",
  +                uc($const->{$c}{s} || $c), $c, $const->{$c}{c} || "";
  +        }
  +        print $ch "#endif /* $headerdef */\n" if ($headerdef);
  +        printf $ch "\t{ %s, %s } /* NULL terminated (%s) */\n", "NULL", "0", 
$tbl;
  +        print $ch "};\n";
  +        printf $ch "const struct rpmconstant_s * %sctable = %sctbl;\n\n", 
$tbl, $tbl;
  +
  +        push @availlabletables, $tbl;
  +    }
  +}
  +
  +printf $ch <<EOF
  +/* File automatically generate by $0
  + * Olivier Thauvin <thauvin at aerov.jussieu.fr>
  + * Parsed files:
  + * %s
  + */
  +
  +#define RPMCONSTANT_INTERNAL
  +#include "rpmconstant.h"
  + 
  +EOF
  +, join("\n * ", @ARGV);
  +
  +GetOptions(
  +);
  +
  +foreach (@ARGV) {
  +    parseconst($_);
  +}
  +
  +print $ch "static const struct rpmconstantlist_s rpmconstanttp[] = {\n";
  +foreach (sort(@availlabletables)) {
  +    printf $ch "\t{ %s, %s, %s },\n", 
  +        '(void *)'.$_."ctbl",
  +        '"' . lc($_) . '"',
  +        $tableprefix{$_} ? "\"$tableprefix{$_}\"" : "NULL";
  +}
  +printf $ch "\t{ %s, %s, %s } /* NULL terminated */\n", "(void *) NULL", 
"NULL", "NULL";
  +print $ch "};\n";
  +printf $ch "const struct rpmconstantlist_s * rpmconstanttype = 
rpmconstanttp;\n";
  +print $ch "const int rpmconstanttypesize = sizeof(rpmconstanttp) / 
sizeof(rpmconstanttp[0]) -1;\n";
  +
  @@ .
______________________________________________________________________
RPM Package Manager                                    http://rpm5.org
CVS Sources Repository                                rpm-cvs@rpm5.org

Reply via email to