1 /* $Id: fmt.c,v 1.1.1.1 2008/08/24 05:34:47 gmcgarry Exp $ */
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
40 static int type_f(int);
41 static int ne_d(char *s
,char **p
);
42 static int e_d(char *s
,char **p
);
43 static int op_gen(int a
,int b
,int c
,int d
);
44 static char *ap_end(char *s
);
46 #define skip(s) while(*s==' ') s++
49 /* special quote character for stu */
50 extern int cursor
,scale
;
51 extern flag cblank
,cplus
; /*blanks in I and compulsory plus*/
52 struct syl syl
[SYLMX
];
53 int parenlvl
,pc
,revloc
;
54 static char *f_s(char *s
, int curloc
),*f_list(char *),
55 *i_tem(char *),*gt_num(char *s
, int *n
);
60 if((s
=f_s(s
,0))==NULL
)
68 f_s(char *s
, int curloc
)
75 if(parenlvl
++ ==1) revloc
=curloc
;
76 if(op_gen(RET
,curloc
,0,0)<0 ||
89 if((s
=i_tem(s
))==NULL
) return(NULL
);
95 op_gen(REVERT
,revloc
,0,0);
108 if(*s
==')') return(s
);
109 if(ne_d(s
,&t
)) return(t
);
110 if(e_d(s
,&t
)) return(t
);
112 if((curloc
=op_gen(STACK
,n
,0,0))<0) return(NULL
);
113 return(f_s(s
,curloc
));
117 ne_d(char *s
,char **p
)
122 case ':': op_gen(COLON
,0,0,0); break;
124 if(*++s
=='z') op_gen(BZ
,0,0,0);
125 else op_gen(BN
,0,0,0);
139 case '/': op_gen(SLASH
,0,0,0); break;
140 case '-': sign
=1; s
++; /*OUTRAGEOUS CODING TRICK*/
141 case '0': case '1': case '2': case '3': case '4':
142 case '5': case '6': case '7': case '8': case '9':
147 case 'p': if(sign
) n
= -n
; op_gen(P
,n
,0,0); break;
148 case 'x': op_gen(X
,n
,0,0); break;
150 case 'h': op_gen(H
,n
,(int)(s
+1),0);
157 case '\'': op_gen(APOS
,(int)s
,0,0);
173 case 'x': op_gen(X
,1,0,0); break;
174 case 'p': op_gen(P
,1,0,0); break;
182 e_d(char *s
,char **p
)
183 { int n
,w
,d
,e
,found
=0,x
=0;
201 op_gen(x
==1?E
:G
,w
,d
,0);
205 op_gen(x
==1?EE
:GE
,w
,d
,e
);
217 if(*s
>='0' && *s
<='9')
270 op_gen(int a
,int b
,int c
,int d
)
271 { struct syl
*p
= &syl
[pc
];
273 { fprintf(stderr
,"format too complicated:\n%s\n",
284 gt_num(char *s
, int *n
)
292 if(c
>'9' || c
<'0') break;
302 int cnt
[STKSZ
],ret
[STKSZ
],cp
,rp
;
308 return(do_fio(&one
,NULL
,0l));
312 do_fio(ftnint
*number
, char *ptr
, ftnlen len
)
316 for(i
=0;i
<*number
;i
++,ptr
+=len
)
318 loop
: switch(type_f((p
= &syl
[pc
])->op
))
321 fprintf(stderr
,"unknown code in do_fio: %d\n%s\n",
323 err(elist
->cierr
,100,"do_fio");
341 if((n
=(*doed
)(p
,ptr
,len
))>0) err(elist
->cierr
,errno
,"fmt");
342 if(n
<0) err(elist
->ciend
,(EOF
),"fmt");
366 if(!workdone
) return(0);
367 if((n
=(*dorevert
)()) != 0) return(n
);
402 workdone
=cp
=rp
=pc
=cursor
=0;
415 case REVERT
: return(REVERT
);
416 case GOTO
: return(GOTO
);
417 case STACK
: return(STACK
);
421 case T
: case TL
: case TR
:
428 case E
: case EE
: case D
:
438 { if(*s
!=quote
) continue;
439 if(*++s
!=quote
) return(s
);
444 fatal(100,"bad string");