1 /* $Header: form.c,v 3.0 89/10/18 15:17:26 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 89/10/18 15:17:26 lwall
28 line_t oldline = line;
29 int oldsave = savestack->ary_fill;
31 str = fcmd->f_unparsed;
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");
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)
111 linebeg = fcmd->f_next;
116 if (fcmd->f_unparsed)
117 form_parseargs(fcmd);
118 switch (fcmd->f_type) {
123 (void)eval(fcmd->f_expr,G_SCALAR,sp);
124 str = stack->ary_array[sp+1];
129 while (size && *s && *s != '\n') {
133 if (*s && index(chopset,(*d++ = *s++)))
135 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
140 else if (chophere && chophere < s && *s && index(chopset,*s))
142 if (fcmd->f_flags & FC_CHOP) {
145 size += (s - chophere);
147 if (fcmd->f_flags & FC_MORE &&
148 *chophere && strNE(chophere,"\n")) {
153 while (d[-1] == ' ' && size < fcmd->f_size) {
161 while (*chophere && index(chopset,*chophere))
163 str_chop(str,chophere);
165 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
166 size = 0; /* no spaces before newline */
173 (void)eval(fcmd->f_expr,G_SCALAR,sp);
174 str = stack->ary_array[sp+1];
175 t = s = str_get(str);
179 while (size && *s && *s != '\n') {
183 if (*s && index(chopset,*s++))
185 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
190 else if (chophere && chophere < s && *s && index(chopset,*s))
192 if (fcmd->f_flags & FC_CHOP) {
195 size += (s - chophere);
197 while (*chophere && index(chopset,*chophere))
207 (void)bcopy(t,d,size);
210 if (fcmd->f_flags & FC_CHOP)
211 str_chop(str,chophere);
216 (void)eval(fcmd->f_expr,G_SCALAR,sp);
217 str = stack->ary_array[sp+1];
218 t = s = str_get(str);
222 while (size && *s && *s != '\n') {
226 if (*s && index(chopset,*s++))
228 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
233 else if (chophere && chophere < s && *s && index(chopset,*s))
235 if (fcmd->f_flags & FC_CHOP) {
238 size += (s - chophere);
240 while (*chophere && index(chopset,*chophere))
246 while (size > halfsize) {
251 (void)bcopy(t,d,size);
254 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
255 size = 0; /* no spaces before newline */
262 if (fcmd->f_flags & FC_CHOP)
263 str_chop(str,chophere);
267 (void)eval(fcmd->f_expr,G_SCALAR,sp);
268 str = stack->ary_array[sp+1];
272 orec->o_lines += countlines(s);
273 (void)bcopy(s,d,size);
275 linebeg = fcmd->f_next;
285 register int count = 0;
294 do_write(orec,stio,sp)
299 FILE *ofp = stio->ofp;
303 fprintf(stderr,"left=%ld, todo=%ld\n",
304 (long)stio->lines_left, (long)orec->o_lines);
306 if (stio->lines_left < orec->o_lines) {
307 if (!stio->top_stab) {
311 stio->top_name = savestr("top");
312 topstab = stabent(stio->top_name,FALSE);
313 if (!topstab || !stab_form(topstab)) {
314 stio->lines_left = 100000000;
317 stio->top_stab = topstab;
319 if (stio->lines_left >= 0 && stio->page > 0)
320 (void)putc('\f',ofp);
321 stio->lines_left = stio->page_len;
323 format(&toprec,stab_form(stio->top_stab),sp);
324 fputs(toprec.o_str,ofp);
325 stio->lines_left -= toprec.o_lines;
328 fputs(orec->o_str,ofp);
329 stio->lines_left -= orec->o_lines;