1 /* $Header: form.c,v 4.0 91/03/20 01:19:23 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 4.0 91/03/20 01:19:23 lwall
28 line_t oldline = curcmd->c_line;
29 int oldsave = savestack->ary_fill;
31 str = fcmd->f_unparsed;
32 curcmd->c_line = fcmd->f_line;
33 fcmd->f_unparsed = Nullstr;
34 (void)savehptr(&curstash);
35 curstash = str->str_u.str_hash;
39 items = arg->arg_len - 1; /* ignore $$ on end */
40 for (i = 1; i <= items; i++) {
41 if (!fcmd || fcmd->f_type == F_NULL)
42 fatal("Too many field values");
44 fcmd->f_expr = make_op(O_ITEM,1,
45 arg[i].arg_ptr.arg_arg,Nullarg,Nullarg);
46 if (fcmd->f_flags & FC_CHOP) {
47 if ((fcmd->f_expr[1].arg_type & A_MASK) == A_STAB)
48 fcmd->f_expr[1].arg_type = A_LVAL;
49 else if ((fcmd->f_expr[1].arg_type & A_MASK) == A_EXPR)
50 fcmd->f_expr[1].arg_type = A_LEXPR;
52 fatal("^ field requires scalar lvalue");
56 if (fcmd && fcmd->f_type)
57 fatal("Not enough field values");
58 curcmd->c_line = oldline;
65 #define CHKLEN(allow) \
66 newsize = (d - orec->o_str) + (allow); \
67 if (newsize >= curlen) { \
68 curlen = d - orec->o_str; \
69 GROWSTR(&orec->o_str,&orec->o_len,orec->o_len + (allow)); \
70 d = orec->o_str + curlen; /* in case it moves */ \
71 curlen = orec->o_len - 2; \
75 register struct outrec *orec;
79 register char *d = orec->o_str;
81 register int curlen = orec->o_len - 2;
91 mycmd.c_type = C_NULL;
93 for (; fcmd; fcmd = nextfcmd) {
94 nextfcmd = fcmd->f_next;
95 CHKLEN(fcmd->f_presize);
96 if (s = fcmd->f_pre) {
99 while (d > orec->o_str && (d[-1] == ' ' || d[-1] == '\t'))
101 if (fcmd->f_flags & FC_NOBLANK) {
102 if (d == orec->o_str || d[-1] == '\n') {
103 orec->o_lines--; /* don't print blank line */
104 linebeg = fcmd->f_next;
107 else if (fcmd->f_flags & FC_REPEAT)
110 linebeg = fcmd->f_next;
113 linebeg = fcmd->f_next;
118 if (fcmd->f_unparsed)
119 form_parseargs(fcmd);
120 switch (fcmd->f_type) {
125 (void)eval(fcmd->f_expr,G_SCALAR,sp);
126 str = stack->ary_array[sp+1];
131 while (size && *s && *s != '\n') {
135 if (*s && index(chopset,(*d++ = *s++)))
137 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
142 else if (chophere && chophere < s && *s && index(chopset,*s))
144 if (fcmd->f_flags & FC_CHOP) {
147 size += (s - chophere);
149 if (fcmd->f_flags & FC_MORE &&
150 *chophere && strNE(chophere,"\n")) {
155 while (d[-1] == ' ' && size < fcmd->f_size) {
164 while (*chophere && index(chopset,*chophere))
166 str_chop(str,chophere);
168 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
169 size = 0; /* no spaces before newline */
176 (void)eval(fcmd->f_expr,G_SCALAR,sp);
177 str = stack->ary_array[sp+1];
178 t = s = str_get(str);
182 while (size && *s && *s != '\n') {
186 if (*s && index(chopset,*s++))
188 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
193 else if (chophere && chophere < s && *s && index(chopset,*s))
195 if (fcmd->f_flags & FC_CHOP) {
198 size += (s - chophere);
200 while (*chophere && index(chopset,*chophere))
210 (void)bcopy(t,d,size);
213 if (fcmd->f_flags & FC_CHOP)
214 str_chop(str,chophere);
219 (void)eval(fcmd->f_expr,G_SCALAR,sp);
220 str = stack->ary_array[sp+1];
221 t = s = str_get(str);
225 while (size && *s && *s != '\n') {
229 if (*s && index(chopset,*s++))
231 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
236 else if (chophere && chophere < s && *s && index(chopset,*s))
238 if (fcmd->f_flags & FC_CHOP) {
241 size += (s - chophere);
243 while (*chophere && index(chopset,*chophere))
249 while (size > halfsize) {
254 (void)bcopy(t,d,size);
257 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
258 size = 0; /* no spaces before newline */
265 if (fcmd->f_flags & FC_CHOP)
266 str_chop(str,chophere);
270 (void)eval(fcmd->f_expr,G_SCALAR,sp);
271 str = stack->ary_array[sp+1];
275 orec->o_lines += countlines(s,size) - 1;
276 (void)bcopy(s,d,size);
278 if (size && s[size-1] != '\n') {
282 linebeg = fcmd->f_next;
287 (void)eval(fcmd->f_expr,G_SCALAR,sp);
288 str = stack->ary_array[sp+1];
291 /* If the field is marked with ^ and the value is undefined,
293 if ((fcmd->f_flags & FC_CHOP) && !str->str_pok && !str->str_nok) {
300 value = str_gnum(str);
301 if (fcmd->f_flags & FC_DP) {
302 sprintf(d, "%#*.*f", size, fcmd->f_decimals, value);
304 sprintf(d, "%*.0f", size, value);
319 register int count = 0;
328 do_write(orec,stio,sp)
333 FILE *ofp = stio->ofp;
337 fprintf(stderr,"left=%ld, todo=%ld\n",
338 (long)stio->lines_left, (long)orec->o_lines);
340 if (stio->lines_left < orec->o_lines) {
341 if (!stio->top_stab) {
345 stio->top_name = savestr("top");
346 topstab = stabent(stio->top_name,FALSE);
347 if (!topstab || !stab_form(topstab)) {
348 stio->lines_left = 100000000;
351 stio->top_stab = topstab;
353 if (stio->lines_left >= 0 && stio->page > 0)
354 (void)putc('\f',ofp);
355 stio->lines_left = stio->page_len;
357 format(&toprec,stab_form(stio->top_stab),sp);
358 fputs(toprec.o_str,ofp);
359 stio->lines_left -= toprec.o_lines;
362 fputs(orec->o_str,ofp);
363 stio->lines_left -= orec->o_lines;