summaryrefslogtreecommitdiffstats
path: root/rpmconstant/constant.c
blob: e0438aae767839a605ad1b76b0e3d0fac0aeb8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Nanar <nanardon@mandrake.org>
 * $Id$
 */

#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;
}