1 /* $Header: form.c,v 3.0.1.2 90/08/09 03:38:40 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.2 90/08/09 03:38:40 lwall
10 * patch19: did preliminary work toward debugging packages and evals
12 * Revision 3.0.1.1 90/02/28 17:39:34 lwall
13 * patch9: ... in format threw off subsequent field
15 * Revision 3.0 89/10/18 15:17:26 lwall
34 line_t oldline = curcmd->c_line;
35 int oldsave = savestack->ary_fill;
37 str = fcmd->f_unparsed;
38 curcmd->c_line = fcmd->f_line;
39 fcmd->f_unparsed = Nullstr;
40 (void)savehptr(&curstash);
41 curstash = str->str_u.str_hash;
45 items = arg->arg_len - 1; /* ignore $$ on end */
46 for (i = 1; i <= items; i++) {
47 if (!fcmd || fcmd->f_type == F_NULL)
48 fatal("Too many field values");
50 fcmd->f_expr = make_op(O_ITEM,1,
51 arg[i].arg_ptr.arg_arg,Nullarg,Nullarg);
52 if (fcmd->f_flags & FC_CHOP) {
53 if ((fcmd->f_expr[1].arg_type & A_MASK) == A_STAB)
54 fcmd->f_expr[1].arg_type = A_LVAL;
55 else if ((fcmd->f_expr[1].arg_type & A_MASK) == A_EXPR)
56 fcmd->f_expr[1].arg_type = A_LEXPR;
58 fatal("^ field requires scalar lvalue");
62 if (fcmd && fcmd->f_type)
63 fatal("Not enough field values");
64 curcmd->c_line = oldline;
71 #define CHKLEN(allow) \
72 newsize = (d - orec->o_str) + (allow); \
73 if (newsize >= curlen) { \
74 curlen = d - orec->o_str; \
75 GROWSTR(&orec->o_str,&orec->o_len,orec->o_len + (allow)); \
76 d = orec->o_str + curlen; /* in case it moves */ \
77 curlen = orec->o_len - 2; \
81 register struct outrec *orec;
85 register char *d = orec->o_str;
87 register int curlen = orec->o_len - 2;
97 mycmd.c_type = C_NULL;
99 for (; fcmd; fcmd = nextfcmd) {
100 nextfcmd = fcmd->f_next;
101 CHKLEN(fcmd->f_presize);
102 if (s = fcmd->f_pre) {
105 while (d > orec->o_str && (d[-1] == ' ' || d[-1] == '\t'))
107 if (fcmd->f_flags & FC_NOBLANK) {
108 if (d == orec->o_str || d[-1] == '\n') {
109 orec->o_lines--; /* don't print blank line */
110 linebeg = fcmd->f_next;
113 else if (fcmd->f_flags & FC_REPEAT)
117 linebeg = fcmd->f_next;
122 if (fcmd->f_unparsed)
123 form_parseargs(fcmd);
124 switch (fcmd->f_type) {
129 (void)eval(fcmd->f_expr,G_SCALAR,sp);
130 str = stack->ary_array[sp+1];
135 while (size && *s && *s != '\n') {
139 if (*s && index(chopset,(*d++ = *s++)))
141 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
146 else if (chophere && chophere < s && *s && index(chopset,*s))
148 if (fcmd->f_flags & FC_CHOP) {
151 size += (s - chophere);
153 if (fcmd->f_flags & FC_MORE &&
154 *chophere && strNE(chophere,"\n")) {
159 while (d[-1] == ' ' && size < fcmd->f_size) {
168 while (*chophere && index(chopset,*chophere))
170 str_chop(str,chophere);
172 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
173 size = 0; /* no spaces before newline */
180 (void)eval(fcmd->f_expr,G_SCALAR,sp);
181 str = stack->ary_array[sp+1];
182 t = s = str_get(str);
186 while (size && *s && *s != '\n') {
190 if (*s && index(chopset,*s++))
192 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
197 else if (chophere && chophere < s && *s && index(chopset,*s))
199 if (fcmd->f_flags & FC_CHOP) {
202 size += (s - chophere);
204 while (*chophere && index(chopset,*chophere))
214 (void)bcopy(t,d,size);
217 if (fcmd->f_flags & FC_CHOP)
218 str_chop(str,chophere);
223 (void)eval(fcmd->f_expr,G_SCALAR,sp);
224 str = stack->ary_array[sp+1];
225 t = s = str_get(str);
229 while (size && *s && *s != '\n') {
233 if (*s && index(chopset,*s++))
235 if (*s == '\n' && (fcmd->f_flags & FC_CHOP))
240 else if (chophere && chophere < s && *s && index(chopset,*s))
242 if (fcmd->f_flags & FC_CHOP) {
245 size += (s - chophere);
247 while (*chophere && index(chopset,*chophere))
253 while (size > halfsize) {
258 (void)bcopy(t,d,size);
261 if (fcmd->f_next && fcmd->f_next->f_pre[0] == '\n')
262 size = 0; /* no spaces before newline */
269 if (fcmd->f_flags & FC_CHOP)
270 str_chop(str,chophere);
274 (void)eval(fcmd->f_expr,G_SCALAR,sp);
275 str = stack->ary_array[sp+1];
279 orec->o_lines += countlines(s);
280 (void)bcopy(s,d,size);
282 linebeg = fcmd->f_next;
293 register int count = 0;
302 do_write(orec,stio,sp)
307 FILE *ofp = stio->ofp;
311 fprintf(stderr,"left=%ld, todo=%ld\n",
312 (long)stio->lines_left, (long)orec->o_lines);
314 if (stio->lines_left < orec->o_lines) {
315 if (!stio->top_stab) {
319 stio->top_name = savestr("top");
320 topstab = stabent(stio->top_name,FALSE);
321 if (!topstab || !stab_form(topstab)) {
322 stio->lines_left = 100000000;
325 stio->top_stab = topstab;
327 if (stio->lines_left >= 0 && stio->page > 0)
328 (void)putc('\f',ofp);
329 stio->lines_left = stio->page_len;
331 format(&toprec,stab_form(stio->top_stab),sp);
332 fputs(toprec.o_str,ofp);
333 stio->lines_left -= toprec.o_lines;
336 fputs(orec->o_str,ofp);
337 stio->lines_left -= orec->o_lines;