Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libdss/dss-scan.h
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2002-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
/*
23
* scan head query
24
*/
25
26
static const char scan_usage[] =
27
"[-1ls5P?\n@(#)$Id: dss scan query (AT&T Research) 2011-06-15 $\n]"
28
USAGE_LICENSE
29
"[+PLUGIN?\findex\f]"
30
"[+DESCRIPTION?The \bdss\b \bscan\b query reads records from each input"
31
" \afile\a and passes the records to the remainder of the query."
32
" If \afile\a is omitted then the standard input is read.]"
33
"\n"
34
"\n [ file ... ] \n"
35
"\n";
36
37
static int
38
scan_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
39
{
40
Dss_t* dss = DSS(cx);
41
char** argv = (char**)data;
42
char** files = expr->files;
43
char* file;
44
Sfio_t* sp;
45
Dssfile_t* ip;
46
Dssrecord_t* record;
47
int errors;
48
49
errors = error_info.errors;
50
for (;;)
51
{
52
switch (optget(argv, scan_usage))
53
{
54
case '?':
55
if (disc->errorf)
56
(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg);
57
continue;
58
case ':':
59
if (disc->errorf)
60
(*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg);
61
continue;
62
}
63
break;
64
}
65
if (error_info.errors > errors)
66
return -1;
67
argv += opt_info.index;
68
if (file = *argv)
69
argv++;
70
else if (files)
71
{
72
argv = files;
73
files = 0;
74
if (file = *argv)
75
argv++;
76
}
77
expr = expr->pass;
78
sp = 0;
79
for (;;)
80
{
81
if (sp)
82
{
83
if (!(file = sfgetr(sp, '\n', 1)))
84
{
85
sfclose(sp);
86
sp = 0;
87
goto next;
88
}
89
}
90
else if (file && *file == '<')
91
{
92
while (isspace(*++file));
93
if (!(sp = sfopen(NiL, file, "r")))
94
{
95
if (disc->errorf)
96
(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "%s: cannot read file list", file);
97
goto next;
98
}
99
continue;
100
}
101
if (ip = dssfopen(dss, file, NiL, DSS_FILE_READ, NiL))
102
{
103
if (dssbeg(dss, expr))
104
{
105
dssfclose(ip);
106
return -1;
107
}
108
while (record = dssfread(ip))
109
if (dsseval(dss, expr, record) < 0)
110
{
111
dssfclose(ip);
112
return -1;
113
}
114
dssfclose(ip);
115
}
116
next:
117
if (!sp && !(file = *argv++))
118
{
119
if (!files)
120
break;
121
argv = files;
122
files = 0;
123
if (!(file = *argv++))
124
break;
125
}
126
}
127
return 0;
128
}
129
130
#define QUERY_scan \
131
{ \
132
"scan", \
133
"scan all input files", \
134
CXH, \
135
scan_beg, \
136
0, \
137
0, \
138
0, \
139
0, \
140
0, \
141
1 \
142
}
143
144