Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libdss/dss-print.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
* print query
24
*/
25
26
static const char print_usage[] =
27
"[-1ls5P?\n@(#)$Id: dss print query (AT&T Research) 2011-08-19 $\n]"
28
USAGE_LICENSE
29
"[+PLUGIN?\findex\f]"
30
"[+DESCRIPTION?The \bdss\b \bprint\b query formats and prints the "
31
"current record according to \aformat\a. If \aformat\a is omitted then "
32
"the default method \bprint\b format is used; an error occurs if there "
33
"is no default \bprint\b format.]"
34
"[+?\aformat\a follows \bprintf\b(3) conventions, except that "
35
"\bsfio\b(3) inline ids are used instead of arguments: "
36
"%[-+]][\awidth\a[.\aprecis\a[.\abase\a]]]]]](\aid\a[:\adetails\a]])\achar\a.]"
37
"[+?If \achar\a is \bs\b then the string form of the value of \aid\a is "
38
"listed, otherwise the corresponding numeric form is listed. If "
39
"\awidth\a is omitted then the default width is assumed. \adetails\a "
40
"optionally specify field type specific details. Documentation for "
41
"format details appear with \bdss\b types that support them. For "
42
"example, a \bstrftime\b(3) format for \btime_t\b fields, or a field "
43
"separator for array fields.]"
44
"[+?A format specification %(:\adetails\a:)\achar\a (\aid\a omitted) "
45
"causes no output but instead specifies default \adetails\a for all "
46
"subsequent %...\achar\a. Multiple default specifications may appear.]"
47
"[+?The default print format is \fprint\f. The \bdf\b(1), \bls\b(1), "
48
"\bpax\b(1) and \bps\b(1) commands have \b--format\b options in this "
49
"same style.]"
50
"[a:all?Print the name and value of field, one per line, using the field "
51
"default output format.]"
52
"\n"
53
"\n[ format ]\n"
54
"\n";
55
56
static int
57
print_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
58
{
59
char** argv = (char**)data;
60
int all = 0;
61
int errors = error_info.errors;
62
63
for (;;)
64
{
65
switch (optget(argv, print_usage))
66
{
67
case 'a':
68
all = 1;
69
continue;
70
case '?':
71
if (disc->errorf)
72
(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg);
73
continue;
74
case ':':
75
if (disc->errorf)
76
(*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg);
77
continue;
78
}
79
break;
80
}
81
if (error_info.errors > errors)
82
return -1;
83
argv += opt_info.index;
84
if (all)
85
expr->data = 0;
86
else if (!(expr->data = *argv++))
87
{
88
argv--;
89
if (!(expr->data = (char*)DSS(cx)->meth->print))
90
{
91
if (disc->errorf)
92
(*disc->errorf)(NiL, disc, 2, "%s: no default method print format", DSS(cx)->meth->name);
93
return -1;
94
}
95
}
96
if (*argv)
97
{
98
if (disc->errorf)
99
(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", optusage(NiL));
100
return -1;
101
}
102
return cx->referencef ? dssprintf(DSS(cx), cx, 0, (char*)expr->data, NiL) : 0;
103
}
104
105
static int
106
print_act(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
107
{
108
return dssprintf(DSS(cx), cx, expr->op, (char*)expr->data, data);
109
}
110
111
static int
112
print_ref(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
113
{
114
char* s;
115
char** a;
116
117
if (cx->referencef && (a = (char**)data) && *a++)
118
while (s = *a++)
119
if (*s != '-')
120
return dssprintf(DSS(cx), cx, 0, s, NiL);
121
else if (*(s + 1) == '-' && !*(s + 2))
122
return *a ? dssprintf(DSS(cx), cx, 0, *a, NiL) : 0;
123
return 0;
124
}
125
126
#define QUERY_print \
127
{ \
128
"print", \
129
"format and print the current record", \
130
CXH, \
131
print_beg, \
132
0, \
133
print_act, \
134
0, \
135
0, \
136
print_ref, \
137
0 \
138
}
139
140