-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
260 lines (220 loc) · 5.92 KB
/
print.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/********************************************
prtypes_int.c
copyright 1991-1993,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
#include "mawk.h"
#include "bi_vars.h"
#include "bi_funct.h"
#include "types_string.h"
#include "field.h"
#include "scan.h"
#include "files.h"
#include "types_int.h"
#include "printf.h"
static void print_cell( CELL *, FILE * );
/* Once execute() starts the sprintf code is (belatedly) the only
code allowed to use string_buff */
static void
print_cell( CELL * p, FILE * fp )
{
size_t len;
switch ( p->type ) {
case C_NOINIT:
break;
case C_MBSTRN:
case C_STRING:
case C_STRNUM:
switch ( len= string( p )->len ) {
case 0:
break;
case 1:
putc( string( p )->str[0], fp );
break;
default:
fwrite( string( p )->str, 1, len, fp );
}
break;
case C_DOUBLE: {
double d= p->dval;
if ( is_int_double( d ) ) {
#if LONG64
fprintf( fp, "%ld", (int64_t)d );
#else
fprintf( fp, "%lld", (int64_t)d );
#endif
} else {
fprintf( fp, string( OFMT )->str, p->dval );
}
} break;
default:
bozo( "bad cell passed to print_cell" );
}
}
/* on entry to bi_print or bi_printf the stack is:
sp[0] = an integer k
if ( k < 0 ) output is to a file with name in sp[-1]
{ so open file and sp -= 2 }
sp[0] = k >= 0 is the number of print args
sp[-k] holds the first argument
*/
CELL *
bi_print( CELL * sp ) /* stack ptr passed in */
{
register CELL * p;
register int k;
FILE * fp;
k= sp->type;
if ( k < 0 ) {
/* k holds redirection */
if ( ( --sp )->type < C_STRING )
cast1_to_s( sp );
fp= (FILE *)file_find( string( sp ), k );
free_STRING( string( sp ) );
k= ( --sp )->type;
/* k now has number of arguments */
} else
fp= stdout;
if ( k ) {
p = sp - k; /* clear k variables off the stack */
sp= p - 1;
k--;
while ( k > 0 ) {
print_cell( p, fp );
print_cell( OFS, fp );
cell_destroy( p );
p++;
k--;
}
print_cell( p, fp );
cell_destroy( p );
} else { /* print $0 */
sp--;
print_cell( &field[0], fp );
}
print_cell( ORS, fp );
if ( ferror( fp ) ) {
write_error( fp );
mawk_exit( 2 );
}
return sp;
}
/* first argument is the format as a Form* */
CELL *
bi_printf( CELL * sp )
{
int k;
FILE * fp;
const Form * form;
k= sp->type;
if ( k < 0 ) {
/* k has redirection */
if ( ( --sp )->type < C_STRING )
cast1_to_s( sp );
fp= (FILE *)file_find( string( sp ), k );
free_STRING( string( sp ) );
k= ( --sp )->type;
/* k is now number of args including format */
} else
fp= stdout;
sp-= k; /* sp points at the format string */
k--;
form= (const Form *)sp->ptr;
do_xprintf( fp, form, sp + 1 );
/* cleanup arguments on eval stack */
{
CELL * p;
for ( p= sp + 1; k > 0; k--, p++ )
cell_destroy( p );
}
return sp - 1;
}
/* The format was not a constant string so must be parsed at run-time */
CELL *
bi_printf1( CELL * sp )
{
int k;
FILE * fp;
const Form * form;
k= sp->type;
if ( k < 0 ) {
/* k has redirection */
if ( ( --sp )->type < C_STRING )
cast1_to_s( sp );
fp= (FILE *)file_find( string( sp ), k );
free_STRING( string( sp ) );
k= ( --sp )->type;
/* k is now number of args including format */
} else
fp= stdout;
sp-= k; /* sp points at the format string */
k--;
if ( sp->type < C_STRING )
cast1_to_s( sp );
/* parse form doesn't return on error */
form= parse_form( string( sp ) );
if ( form->num_args > k ) {
rt_error( "not enough arguments passed to printf(\"%s\")",
string( sp )->str );
}
do_xprintf( fp, form, sp + 1 );
free_STRING( string( sp ) );
/* cleanup arguments on eval stack */
{
CELL * p;
for ( p= sp + 1; k; k--, p++ )
cell_destroy( p );
}
return sp - 1;
}
/* format is not constant string */
CELL *
bi_sprintf1( CELL * sp )
{
int argcnt= sp->type;
STRING * sval;
const Form * form;
sp-= argcnt; /* sp points at the format string */
argcnt--;
if ( sp->type != C_STRING )
cast1_to_s( sp );
form= parse_form( string( sp ) );
if ( form->num_args > argcnt ) {
rt_error( "not enough arguments passed to sprintf(\"%s\")",
string( sp )->str );
}
sval= do_xprintf( 0, form, sp + 1 );
free_STRING( string( sp ) );
sp->ptr= (void *)sval; /* sp->type == C_STRING */
/* cleanup */
{
CELL * p;
for ( p= sp + 1; argcnt; argcnt--, p++ )
cell_destroy( p );
}
return sp;
}
/* format is constant string parsed to Form* */
CELL *
bi_sprintf( CELL * sp )
{
int argcnt= sp->type;
STRING * sval;
sp-= argcnt; /* sp points at the format string */
argcnt--;
sval = do_xprintf( 0, (const Form *)sp->ptr, sp + 1 );
sp->type= C_STRING;
sp->ptr = (void *)sval;
/* cleanup */
{
CELL * p;
for ( p= sp + 1; argcnt; argcnt--, p++ )
cell_destroy( p );
}
return sp;
}