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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* $RCSfile: form.c,v $$Revision: 4.0.1.2 $$Date: 91/11/05 17:18:43 $
*
* Copyright (c) 1991, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
* $Log: form.c,v $
* Revision 4.0.1.2 91/11/05 17:18:43 lwall
* patch11: formats didn't fill their fields as well as they could
* patch11: ^ fields chopped hyphens on line break
* patch11: # fields could write outside allocated memory
*
* Revision 4.0.1.1 91/06/07 11:07:59 lwall
* patch4: new copyright notice
* patch4: default top-of-form format is now FILEHANDLE_TOP
*
* Revision 4.0 91/03/20 01:19:23 lwall
* 4.0 baseline.
*
*/
#include "EXTERN.h"
#include "perl.h"
/* Forms stuff */
void
form_parseargs(fcmd)
register FCMD *fcmd;
{
register int i;
register ARG *arg;
register int items;
STR *str;
ARG *parselist();
line_t oldline = curcmd->c_line;
int oldsave = savestack->ary_fill;
str = fcmd->f_unparsed;
curcmd->c_line = fcmd->f_line;
fcmd->f_unparsed = Nullstr;
(void)savehptr(&curstash);
curstash = str->str_u.str_hash;
arg = parselist(str);
restorelist(oldsave);
items = arg->arg_len - 1; /* ignore $$ on end */
for (i = 1; i <= items; i++) {
if (!fcmd || fcmd->f_type == F_NULL)
fatal("Too many field values");
dehoist(arg,i);
fcmd->f_expr = make_op(O_ITEM,1,
arg[i].arg_ptr.arg_arg,Nullarg,Nullarg);
if (fcmd->f_flags & FC_CHOP) {
if ((fcmd->f_expr[1].arg_type & A_MASK) == A_STAB)
fcmd->f_expr[1].arg_type = A_LVAL;
else if ((fcmd->f_expr[1].arg_type & A_MASK) == A_EXPR)
fcmd->f_expr[1].arg_type = A_LEXPR;
else
fatal("^ field requires scalar lvalue");
}
fcmd = fcmd->f_next;
}
if (fcmd && fcmd->f_type)
fatal("Not enough field values");
curcmd->c_line = oldline;
Safefree(arg);
str_free(str);
}
int newsize;
#define CHKLEN(allow) \
newsize = (d - orec->o_str) + (allow); \
if (newsize >= curlen) { \
curlen = d - orec->o_str; \
GROWSTR(&orec->o_str,&orec->o_len,orec->o_len + (allow)); \
d = orec->o_str + curlen; /* in case it moves */ \
curlen = orec->o_len - 2; \
}
format(orec,fcmd,sp)
register struct outrec *orec;
register FCMD *fcmd;
int sp;
{
register char *d = orec->o_str;
register char *s;
register int curlen = orec->o_len - 2;
register int size;
FCMD *nextfcmd;
FCMD *linebeg = fcmd;
char tmpchar;
char *t;
CMD mycmd;
STR *str;
char *chophere;
mycmd.c_type = C_NULL;
orec->o_lines = 0;
for (; fcmd; fcmd = nextfcmd) {
nextfcmd = fcmd->f_next;
CHKLEN(fcmd->f_presize);
/*SUPPRESS 560*/
if (s = fcmd->f_pre) {
while (*s) {
if (*s == '\n') {
while (d > orec->o_str && (d[-1] == ' ' || d[-1] == '\t'))
d--;
if (fcmd->f_flags & FC_NOBLANK) {
if (d == orec->o_str || d[-1] == '\n') {
orec->o_lines--; /* don't print blank line */
linebeg = fcmd->f_next;
break;
}
else if (fcmd->f_flags & FC_REPEAT)
nextfcmd = linebeg;
else
linebeg = fcmd->f_next;
}
else
linebeg = fcmd->f_next;
}
*d++ = *s++;
}
}
if (fcmd->f_unparsed)
form_parseargs(fcmd);
switch (fcmd->f_type) {
case F_NULL:
orec->o_lines++;
break;
case F_LEFT:
(void)eval(fcmd->f_expr,G_SCALAR,sp);
str = stack->ary_array[sp+1];
s = str_get(str);
size = fcmd->f_size;
CHKLEN(size);
chophere = Nullch;
while (size && *s && *s != '\n') {
if (*s == '\t')
*s = ' ';
size--;
if (*s && index(chopset,(*d++ = *s++)))
chophere = s;
if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
*s = ' ';
}
if (size || !*s)
chophere = s;
else if (chophere && chophere < s && *s && index(chopset,*s))
chophere = s;
if (fcmd->f_flags & FC_CHOP) {
if (!chophere)
chophere = s;
size += (s - chophere);
d -= (s - chophere);
if (fcmd->f_flags & FC_MORE &&
*chophere && strNE(chophere,"\n")) {
while (size < 3) {
d--;
size++;
}
while (d[-1] == ' ' && size < fcmd->f_size) {
d--;
size++;
}
*d++ = '.';
*d++ = '.';
*d++ = '.';
size -= 3;
}
while (*chophere && index(chopset,*chophere)
&& isSPACE(*chophere))
chophere++;
str_chop(str,chophere);
}
if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
size = 0; /* no spaces before newline */
while (size) {
size--;
*d++ = ' ';
}
break;
case F_RIGHT:
(void)eval(fcmd->f_expr,G_SCALAR,sp);
str = stack->ary_array[sp+1];
t = s = str_get(str);
size = fcmd->f_size;
CHKLEN(size);
chophere = Nullch;
while (size && *s && *s != '\n') {
if (*s == '\t')
*s = ' ';
size--;
if (*s && index(chopset,*s++))
chophere = s;
if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
*s = ' ';
}
if (size || !*s)
chophere = s;
else if (chophere && chophere < s && *s && index(chopset,*s))
chophere = s;
if (fcmd->f_flags & FC_CHOP) {
if (!chophere)
chophere = s;
size += (s - chophere);
s = chophere;
while (*chophere && index(chopset,*chophere)
&& isSPACE(*chophere))
chophere++;
}
tmpchar = *s;
*s = '\0';
while (size) {
size--;
*d++ = ' ';
}
size = s - t;
(void)bcopy(t,d,size);
d += size;
*s = tmpchar;
if (fcmd->f_flags & FC_CHOP)
str_chop(str,chophere);
break;
case F_CENTER: {
int halfsize;
(void)eval(fcmd->f_expr,G_SCALAR,sp);
str = stack->ary_array[sp+1];
t = s = str_get(str);
size = fcmd->f_size;
CHKLEN(size);
chophere = Nullch;
while (size && *s && *s != '\n') {
if (*s == '\t')
*s = ' ';
size--;
if (*s && index(chopset,*s++))
chophere = s;
if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
*s = ' ';
}
if (size || !*s)
chophere = s;
else if (chophere && chophere < s && *s && index(chopset,*s))
chophere = s;
if (fcmd->f_flags & FC_CHOP) {
if (!chophere)
chophere = s;
size += (s - chophere);
s = chophere;
while (*chophere && index(chopset,*chophere)
&& isSPACE(*chophere))
chophere++;
}
tmpchar = *s;
*s = '\0';
halfsize = size / 2;
while (size > halfsize) {
size--;
*d++ = ' ';
}
size = s - t;
(void)bcopy(t,d,size);
d += size;
*s = tmpchar;
if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
size = 0; /* no spaces before newline */
else
size = halfsize;
while (size) {
size--;
*d++ = ' ';
}
if (fcmd->f_flags & FC_CHOP)
str_chop(str,chophere);
break;
}
case F_LINES:
(void)eval(fcmd->f_expr,G_SCALAR,sp);
str = stack->ary_array[sp+1];
s = str_get(str);
size = str_len(str);
CHKLEN(size+1);
orec->o_lines += countlines(s,size) - 1;
(void)bcopy(s,d,size);
d += size;
if (size && s[size-1] != '\n') {
*d++ = '\n';
orec->o_lines++;
}
linebeg = fcmd->f_next;
break;
case F_DECIMAL: {
double value;
(void)eval(fcmd->f_expr,G_SCALAR,sp);
str = stack->ary_array[sp+1];
size = fcmd->f_size;
CHKLEN(size+1);
/* If the field is marked with ^ and the value is undefined,
blank it out. */
if ((fcmd->f_flags & FC_CHOP) && !str->str_pok && !str->str_nok) {
while (size) {
size--;
*d++ = ' ';
}
break;
}
value = str_gnum(str);
if (fcmd->f_flags & FC_DP) {
sprintf(d, "%#*.*f", size, fcmd->f_decimals, value);
} else {
sprintf(d, "%*.0f", size, value);
}
d += size;
break;
}
}
}
CHKLEN(1);
*d++ = '\0';
}
countlines(s,size)
register char *s;
register int size;
{
register int count = 0;
while (size--) {
if (*s++ == '\n')
count++;
}
return count;
}
do_write(orec,stab,sp)
struct outrec *orec;
STAB *stab;
int sp;
{
register STIO *stio = stab_io(stab);
FILE *ofp = stio->ofp;
#ifdef DEBUGGING
if (debug & 256)
fprintf(stderr,"left=%ld, todo=%ld\n",
(long)stio->lines_left, (long)orec->o_lines);
#endif
if (stio->lines_left < orec->o_lines) {
if (!stio->top_stab) {
STAB *topstab;
char tmpbuf[256];
if (!stio->top_name) {
if (!stio->fmt_name)
stio->fmt_name = savestr(stab_name(stab));
sprintf(tmpbuf, "%s_TOP", stio->fmt_name);
topstab = stabent(tmpbuf,FALSE);
if (topstab && stab_form(topstab))
stio->top_name = savestr(tmpbuf);
else
stio->top_name = savestr("top");
}
topstab = stabent(stio->top_name,FALSE);
if (!topstab || !stab_form(topstab)) {
stio->lines_left = 100000000;
goto forget_top;
}
stio->top_stab = topstab;
}
if (stio->lines_left >= 0 && stio->page > 0)
(void)putc('\f',ofp);
stio->lines_left = stio->page_len;
stio->page++;
format(&toprec,stab_form(stio->top_stab),sp);
fputs(toprec.o_str,ofp);
stio->lines_left -= toprec.o_lines;
}
forget_top:
fputs(orec->o_str,ofp);
stio->lines_left -= orec->o_lines;
}
|