1 /* $Header: form.c,v 3.0.1.1 90/02/28 17:39:34 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 3.0.1.1 90/02/28 17:39:34 lwall
10 * patch9: ... in format threw off subsequent field
12 * Revision 3.0 89/10/18 15:17:26 lwall
31 line_t oldline = line;
32 int oldsave = savestack->ary_fill;
34 str = fcmd->f_unparsed;
36 fcmd->f_unparsed = Nullstr;
37 (void)savehptr(&curstash);
38 curstash = str->str_u.str_hash;
42 items = arg->arg_len - 1; /* ignore $$ on end */
43 for (i = 1; i <= items; i++) {
44 if (!fcmd || fcmd->f_type == F_NULL)
45 fatal("Too many field values");
47 fcmd->f_expr = make_op(O_ITEM,1,
48 arg[i].arg_ptr.arg_arg,Nullarg,Nullarg);
49 if (fcmd->f_flags & FC_CHOP) {
50 if ((fcmd->f_expr[1].arg_type & A_MASK) == A_STAB)
51 fcmd->f_expr[1].arg_type = A_LVAL;
52 else if ((fcmd->f_expr[1].arg_type & A_MASK) == A_EXPR)
53 fcmd->f_expr[1].arg_type = A_LEXPR;
55 fatal("^ field requires scalar lvalue");
59 if (fcmd && fcmd->f_type)
60 fatal("Not enough field values");
68 #define CHKLEN(allow) \
69 newsize = (d - orec->o_str) + (allow); \
70 if (newsize >= curlen) { \
71 curlen = d - orec->o_str; \
72 GROWSTR(&orec->o_str,&orec->o_len,orec->o_len + (allow)); \
73 d = orec->o_str + curlen; /* in case it moves */ \
74 curlen = orec->o_len - 2; \
78 register struct outrec *orec;
82 register char *d = orec->o_str;
84 register int curlen = orec->o_len - 2;
94 mycmd.c_type = C_NULL;
96 for (; fcmd; fcmd = nextfcmd) {
97 nextfcmd = fcmd->f_next;
98 CHKLEN(fcmd->f_presize);
99 if (s = fcmd->f_pre) {
102 while (d > orec->o_str && (d[-1] == ' ' || d[-1] == '\t'))
104 if (fcmd->f_flags & FC_NOBLANK) {
105 if (d == orec->o_str || d[-1] == '\n') {
106 orec->o_lines--; /* don't print blank line */
107 linebeg = fcmd->f_next;
110 else if (fcmd->f_flags & FC_REPEAT)
114 linebeg = fcmd->f_next;
119 if (fcmd->f_unparsed)
120 form_parseargs(fcmd);
121 switch (fcmd->f_type) {
126 (void)eval(fcmd->f_expr,G_SCALAR,sp);
127 str = stack->ary_array[sp+1];
132 while (size && *s && *s != '\n') {
136 if (*s && index(chopset,(*d++ = *s++)))
138 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
143 else if (chophere && chophere < s && *s && index(chopset,*s))
145 if (fcmd->f_flags & FC_CHOP) {
148 size += (s - chophere);
150 if (fcmd->f_flags & FC_MORE &&
151 *chophere && strNE(chophere,"\n")) {
156 while (d[-1] == ' ' && size < fcmd->f_size) {
165 while (*chophere && index(chopset,*chophere))
167 str_chop(str,chophere);
169 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
170 size = 0; /* no spaces before newline */
177 (void)eval(fcmd->f_expr,G_SCALAR,sp);
178 str = stack->ary_array[sp+1];
179 t = s = str_get(str);
183 while (size && *s && *s != '\n') {
187 if (*s && index(chopset,*s++))
189 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
194 else if (chophere && chophere < s && *s && index(chopset,*s))
196 if (fcmd->f_flags & FC_CHOP) {
199 size += (s - chophere);
201 while (*chophere && index(chopset,*chophere))
211 (void)bcopy(t,d,size);
214 if (fcmd->f_flags & FC_CHOP)
215 str_chop(str,chophere);
220 (void)eval(fcmd->f_expr,G_SCALAR,sp);
221 str = stack->ary_array[sp+1];
222 t = s = str_get(str);
226 while (size && *s && *s != '\n') {
230 if (*s && index(chopset,*s++))
232 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
237 else if (chophere && chophere < s && *s && index(chopset,*s))
239 if (fcmd->f_flags & FC_CHOP) {
242 size += (s - chophere);
244 while (*chophere && index(chopset,*chophere))
250 while (size > halfsize) {
255 (void)bcopy(t,d,size);
258 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
259 size = 0; /* no spaces before newline */
266 if (fcmd->f_flags & FC_CHOP)
267 str_chop(str,chophere);
271 (void)eval(fcmd->f_expr,G_SCALAR,sp);
272 str = stack->ary_array[sp+1];
276 orec->o_lines += countlines(s);
277 (void)bcopy(s,d,size);
279 linebeg = fcmd->f_next;
289 register int count = 0;
298 do_write(orec,stio,sp)
303 FILE *ofp = stio->ofp;
307 fprintf(stderr,"left=%ld, todo=%ld\n",
308 (long)stio->lines_left, (long)orec->o_lines);
310 if (stio->lines_left < orec->o_lines) {
311 if (!stio->top_stab) {
315 stio->top_name = savestr("top");
316 topstab = stabent(stio->top_name,FALSE);
317 if (!topstab || !stab_form(topstab)) {
318 stio->lines_left = 100000000;
321 stio->top_stab = topstab;
323 if (stio->lines_left >= 0 && stio->page > 0)
324 (void)putc('\f',ofp);
325 stio->lines_left = stio->page_len;
327 format(&toprec,stab_form(stio->top_stab),sp);
328 fputs(toprec.o_str,ofp);
329 stio->lines_left -= toprec.o_lines;
332 fputs(orec->o_str,ofp);
333 stio->lines_left -= orec->o_lines;