Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/dsslib/lookup/lookup.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2010-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
static const char lookup_usage[] =
23
"[+PLUGIN?\findex\f]"
24
"[+DESCRIPTION?The lookup checks if the \avariable\a operand value "
25
"appears in the \afile\a operand, where \afile\a contains one value per "
26
"line.]"
27
"\n"
28
"\nfile variable\n"
29
"\n"
30
;
31
32
#include <dsslib.h>
33
#include <dt.h>
34
35
typedef struct Value_s
36
{
37
Dtlink_t link;
38
char value[1];
39
} Value_t;
40
41
typedef struct State_s
42
{
43
Dtdisc_t dictdisc;
44
Dt_t* dict;
45
Cxvariable_t* variable;
46
Vmalloc_t* vm;
47
} State_t;
48
49
extern Dsslib_t dss_lib_lookup;
50
51
static int
52
lookup_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
53
{
54
char** argv = (char**)data;
55
int errors = error_info.errors;
56
char* s;
57
State_t* state;
58
Sfio_t* sp;
59
Value_t* v;
60
Vmalloc_t* vm;
61
char buf[PATH_MAX];
62
63
if (!(vm = vmopen(Vmdcheap, Vmlast, 0)) || !(state = vmnewof(vm, 0, State_t, 1, 0)))
64
{
65
if (vm)
66
vmclose(vm);
67
if (disc->errorf)
68
(*disc->errorf)(cx, disc, ERROR_SYSTEM|2, "out of space");
69
return -1;
70
}
71
state->vm = vm;
72
state->dictdisc.key = offsetof(Value_t, value);
73
if (!(state->dict = dtnew(vm, &state->dictdisc, Dtset)))
74
goto bad;
75
sfprintf(cx->buf, "%s%s", strchr(dss_lib_lookup.description, '['), lookup_usage);
76
s = sfstruse(cx->buf);
77
for (;;)
78
{
79
switch (optget(argv, s))
80
{
81
case '?':
82
if (disc->errorf)
83
{
84
(*disc->errorf)(cx, disc, ERROR_USAGE|4, "%s", opt_info.arg);
85
}
86
else
87
goto bad;
88
continue;
89
case ':':
90
if (disc->errorf)
91
(*disc->errorf)(cx, disc, 2, "%s", opt_info.arg);
92
else
93
goto bad;
94
continue;
95
}
96
break;
97
}
98
if (error_info.errors > errors)
99
goto bad;
100
argv += opt_info.index;
101
if (!argv[0] || !argv[1] || argv[2])
102
{
103
if (disc->errorf)
104
(*disc->errorf)(cx, disc, 2, "file and variable arguments expected");
105
goto bad;
106
}
107
if (!(state->variable = cxvariable(cx, argv[1], NiL, disc)))
108
{
109
if (disc->errorf)
110
(*disc->errorf)(cx, disc, 2, "%s: variable not defined", argv[1]);
111
goto bad;
112
}
113
if (!(sp = dssfind(argv[0], NiL, DSS_VERBOSE, buf, sizeof(buf), disc)))
114
goto bad;
115
while (s = sfgetr(sp, '\n', SF_STRING))
116
{
117
if (!(v = vmnewof(vm, NiL, Value_t, 1, sfvalue(sp))))
118
{
119
if (disc->errorf)
120
(*disc->errorf)(cx, disc, ERROR_SYSTEM|2, "out of space");
121
goto bad;
122
}
123
strcpy(v->value, s);
124
dtinsert(state->dict, v);
125
}
126
sfclose(sp);
127
expr->data = state;
128
return 0;
129
bad:
130
vmclose(vm);
131
return -1;
132
}
133
134
static int
135
lookup_sel(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
136
{
137
register State_t* state = (State_t*)expr->data;
138
Cxoperand_t val;
139
140
if (cxcast(cx, &val, state->variable, cx->state->type_string, data, NiL))
141
return -1;
142
return !!dtmatch(state->dict, val.value.string.data);
143
}
144
145
static int
146
lookup_end(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
147
{
148
register State_t* state = (State_t*)expr->data;
149
150
vmclose(state->vm);
151
return 0;
152
}
153
154
static Cxquery_t queries[] =
155
{
156
{
157
"lookup",
158
"look up variable value in file",
159
CXH,
160
lookup_beg,
161
lookup_sel,
162
0,
163
lookup_end
164
},
165
{0}
166
};
167
168
Dsslib_t dss_lib_lookup =
169
{
170
"lookup",
171
"lookup query"
172
"[-1lms5P?\n@(#)$Id: dss lookup query (AT&T Research) 2010-04-20 $\n]"
173
USAGE_LICENSE,
174
CXH,
175
0,
176
0,
177
0,
178
0,
179
0,
180
0,
181
&queries[0]
182
};
183
184