Rev 11125 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 11125 | Rev 12243 | ||
---|---|---|---|
Line 22... | Line 22... | ||
22 | void calc_columnof(char *p); |
22 | void calc_columnof(char *p); |
23 | 23 | ||
24 | /* It is this routine which uses print_precision. */ |
24 | /* It is this routine which uses print_precision. */ |
25 | void float2str(double d, char *p) |
25 | void float2str(double d, char *p) |
26 | { |
26 | { |
27 |
|
27 | char buf[16]; |
28 |
|
28 | int i; |
29 |
|
29 | if(d==0) { |
30 |
|
30 | ovlstrcpy(p,"0"); return; |
31 |
|
31 | } |
32 |
|
32 | if(!isfinite(d)) { /* isinf, isnan possibly not available */ |
33 |
|
33 | if (d == d) ovlstrcpy(p, (d > 0)? "Inf": "-Inf"); else ovlstrcpy(p,"NaN"); |
34 |
|
34 | return; |
35 |
|
35 | } |
36 |
|
36 | if(d<1000000 && d>-1000000 && d==floor(d)) { |
37 |
|
37 | mystrncpy(p,int2str(d),MAX_LINELEN); return; |
38 |
|
38 | } |
39 |
|
39 | i=print_precision; |
40 |
|
40 | if(i<2) i=2; |
41 |
|
41 | if(i>32) i=32; /* Simple limitation. */ |
42 |
|
42 | buf[0]='%';buf[1]='.'; |
43 |
|
43 | if(i>=10) { |
44 |
|
44 | buf[2]='0'+i/10; buf[3]='0'+i%10; buf[4]='g'; buf[5]=0; |
45 |
|
45 | } |
46 |
|
46 | else { |
47 |
|
47 | buf[2]='0'+i; buf[3]='g'; buf[4]=0; |
48 |
|
48 | } |
49 |
|
49 | snprintf(p,MAX_LINELEN,buf,(double) d); |
50 |
|
50 | if(isspace(*p)) ovlstrcpy(p,find_word_start(p)); |
51 | } |
51 | } |
52 | 52 | ||
53 | /* substitute variable names by their environment strings |
53 | /* substitute variable names by their environment strings |
54 | * The buffer pointed to by p must have enough space |
54 | * The buffer pointed to by p must have enough space |
55 | * (defined by MAX_LINELEN). |
55 | * (defined by MAX_LINELEN). |
56 | */ |
56 | */ |
57 | char *substit(char *p) |
57 | char *substit(char *p) |
58 | { |
58 | { |
59 |
|
59 | char *pp, *p2, *ev; |
60 |
|
60 | char *oldlast, *oldnext, *oldend, *newend; |
61 |
|
61 | char buf[MAX_LINELEN+1], oldbuf[MAX_LINELEN+1]; |
62 |
|
62 | int ln; |
63 | 63 | ||
64 |
|
64 | if((p2=strchr(p,'$'))==NULL) return p; |
65 |
|
65 | if(substnest>SUBST_LIMIT) module_error("subst_exceeded"); |
66 |
|
66 | ln=strlen(p); if(ln>=MAX_LINELEN) goto too_long; |
67 |
|
67 | memmove(oldbuf,p,ln); oldbuf[ln]=0; oldend=oldbuf+ln; |
68 |
|
68 | newend=p; oldlast=oldnext=oldbuf; |
69 |
|
69 | for(pp=oldbuf+(p2-p); pp!=NULL; pp=strchr(pp,'$')) { |
70 |
|
70 | if(*(pp+1)=='$') { /* escaped dollar sign */ |
71 |
|
71 | pp++; if(newend-p+(pp-oldlast)>=MAX_LINELEN) goto too_long; |
72 |
|
72 | memmove(newend,oldlast,pp-oldlast); newend+=pp-oldlast; |
73 |
|
73 | pp++; oldlast=pp; continue; |
74 |
|
74 | } |
75 |
|
75 | switch(*(pp+1)) { |
76 |
|
76 | case 0: { |
77 |
|
77 | *pp=0; oldend--; goto end; |
78 |
|
78 | } |
79 |
|
79 | case '(': { |
80 |
|
80 | p2=find_matching(pp+2,')'); |
81 |
|
81 | if(p2==NULL) { |
82 |
|
82 | module_error("unmatched_parentheses"); |
83 |
|
83 | *p=0; return p; |
84 |
|
84 | } |
85 |
|
85 | *p2++=0; oldnext=p2; memmove(buf,pp+2,p2-pp-2); |
86 |
|
86 | substnest++; substit(buf); substnest--; |
87 |
|
87 | break; |
88 |
|
88 | } |
89 | 89 | ||
90 |
|
90 | case '[': { |
91 |
|
91 | double d; |
92 |
|
92 | p2=find_matching(pp+2,']'); |
93 |
|
93 | if(p2==NULL) { |
94 |
|
94 | module_error("unmatched_parentheses"); |
95 |
|
95 | *p=0; return p; |
96 |
|
96 | } |
97 |
|
97 | *p2=0; ovlstrcpy(buf,pp+2); oldnext=p2+1; |
98 |
|
98 | substnest++; substit(buf); substnest--; |
99 |
|
99 | d=evalue(buf); float2str(d,buf); |
100 |
|
100 | goto replace; |
101 |
|
101 | } |
102 | 102 | ||
103 |
|
103 | default: { |
104 |
|
104 | for(p2=pp+1; myisalnum(*p2) || *p2=='_'; p2++); |
105 |
|
105 | oldnext=p2; memmove(buf,pp+1,p2-(pp+1)); buf[p2-(pp+1)]=0; |
106 |
|
106 | goto noarray; |
107 |
|
107 | } |
108 |
|
108 | } |
109 |
|
109 | if((p2=strchr(buf,'['))!=NULL) { |
110 |
|
110 | char *pt1, tbuf[MAX_LINELEN+1]; |
111 |
|
111 | *p2++=0; pt1=find_matching(p2,']'); |
112 |
|
112 | if(pt1==NULL) {buf[0]=0; goto replace;} |
113 |
|
113 | *pt1=0; pt1=strchr(p2,';'); |
114 |
|
114 | if(pt1==NULL) { |
115 |
|
115 | if(*find_word_start(p2)==0) {*p2=0; goto noarray;} |
116 |
|
116 | snprintf(tbuf,sizeof(tbuf),"%s of $%s",p2,buf); |
117 |
|
117 | calc_itemof(tbuf); ovlstrcpy(buf,tbuf); goto replace; |
118 |
|
118 | } |
119 |
|
119 | else { |
120 |
|
120 | *pt1++=0; p2=find_word_start(p2); |
121 |
|
121 | if(*p2) { |
122 |
|
122 | snprintf(tbuf,sizeof(tbuf),"%s of $%s",p2,buf); |
123 |
|
123 | calc_rowof(tbuf); |
124 |
|
124 | } |
125 |
|
125 | else { |
126 |
|
126 | snprintf(tbuf,sizeof(tbuf),"$%s",buf); substit(tbuf); |
127 |
|
127 | } |
128 |
|
128 | if(*find_word_start(pt1)) { |
129 |
|
129 | snprintf(buf,sizeof(buf),"%s of %s",pt1,tbuf); |
130 |
|
130 | calc_columnof(buf); goto replace; |
131 |
|
131 | } |
132 |
|
132 | else ovlstrcpy(buf,tbuf); |
133 |
|
133 | goto replace; |
134 |
|
134 | } |
135 |
|
135 | } |
136 |
|
136 | noarray: ev=getvar(buf); ln=getvar_len; |
137 |
|
137 | if(ev==NULL) ev=""; |
138 |
|
138 | if(strchr(ev,'$')==NULL) goto rep2; |
139 |
|
139 | memmove(buf,ev,ln); buf[ln]=0; |
140 |
|
140 | substnest++; substit(buf); substnest--; |
141 |
|
141 | replace: ev=buf; ln=strlen(ev); |
142 |
|
142 | rep2: |
143 |
|
143 | if(pp>oldlast) { |
144 |
|
144 | if(newend-p+(pp-oldlast)>=MAX_LINELEN) goto too_long; |
145 |
|
145 | memmove(newend,oldlast,pp-oldlast); newend+=pp-oldlast; |
146 |
|
146 | } |
147 |
|
147 | if(ln>0) { |
148 |
|
148 | if((newend-p)+ln>=MAX_LINELEN) goto too_long; |
149 |
|
149 | memmove(newend,ev,ln); newend+=ln; |
150 |
|
150 | } |
151 |
|
151 | pp=oldlast=oldnext; |
152 |
|
152 | continue; |
153 |
|
153 | } |
154 |
|
154 | end: |
155 |
|
155 | if(oldlast<oldend) { |
156 |
|
156 | if(newend-p+(oldend-oldlast)>=MAX_LINELEN) goto too_long; |
157 |
|
157 | memmove(newend,oldlast,oldend-oldlast); newend+=oldend-oldlast; |
158 |
|
158 | } |
159 |
|
159 | *newend=0; return p; |
160 |
|
160 | too_long: user_error("cmd_output_too_long"); return NULL; |
161 | } |
161 | } |
162 | 162 | ||
163 | struct forstruct forstruct; |
163 | struct forstruct forstruct; |
164 | 164 | ||
165 | int for_getvar(char *p) |
165 | int for_getvar(char *p) |
166 | { |
166 | { |
167 |
|
167 | char *vp, buf[MAX_LINELEN+1]; |
168 |
|
168 | mystrncpy(buf,p,sizeof(buf)); substit(buf); |
169 |
|
169 | vp=find_word_start(buf); for(p=vp; myisalnum(*p) || *p=='_'; p++); |
170 |
|
170 | *p=0; if(p-vp<=0 || p-vp>MAX_NAMELEN) return -1; |
171 |
|
171 | mystrncpy(forstruct.var,vp,sizeof(forstruct.var)); |
172 |
|
172 | return 0; |
173 | } |
173 | } |
174 | 174 | ||
175 | /* If bufp=NULL then numeric. Else string-wise. |
175 | /* If bufp=NULL then numeric. Else string-wise. |
176 | * returns 0 if success, |
176 | * returns 0 if success, |
177 | * -1 if syntax error, 1 if bad values |
177 | * -1 if syntax error, 1 if bad values |
178 | */ |
178 | */ |
179 | int cutfor(char *p, char *bufp) |
179 | int cutfor(char *p, char *bufp) |
180 | { |
180 | { |
181 |
|
181 | char *eqp, *fromp, *top, *stepp, *inp; |
182 | 182 | ||
183 |
|
183 | p=find_word_start(p); |
184 |
|
184 | inp=find_word_start(find_word_end(p)); |
185 |
|
185 | if(wordchr(inp,"in")==inp) { |
186 |
|
186 | char buf[MAX_LINELEN+1], *bp; |
187 | int i; |
187 | int i; |
188 |
|
188 | double d; |
189 |
|
189 | *inp=0; inp+=strlen("in"); |
190 |
|
190 | forin: inp=find_word_start(inp); forstruct.type=for_in; |
191 |
|
191 | if(for_getvar(p)) return -1; |
192 |
|
192 | if(bufp!=NULL) bp=bufp; else bp=buf; |
193 |
|
193 | mystrncpy(bp,inp,MAX_LINELEN); substit(bp); |
194 |
|
194 | strip_trailing_spaces(bp); |
195 |
|
195 | if(bp[0]==0) { |
196 |
|
196 | forstruct.from=0; forstruct.to=-1; forstruct.step=1; return 0; |
197 |
|
197 | } |
198 |
|
198 | for(i=0, inp=bp; i<MAX_VALUE_LIST && inp!=NULL; inp=top) { |
199 |
|
199 | top=strparchr(inp,','); if(top) *top++=0; |
200 |
|
200 | if(bufp==NULL) { |
201 |
|
201 | d=evalue(inp); if(isfinite(d)) forstruct.list[i++]=d; |
202 |
|
202 | else return 1; |
203 |
|
203 | } |
204 |
|
204 | else { |
205 |
|
205 | strip_trailing_spaces(inp); |
206 |
|
206 | forstruct.pos[i++]=find_word_start(inp); |
207 | } |
- | |
208 | } |
207 | } |
209 | forstruct.from=0; forstruct.to=i-1; forstruct.step=1; return 0; |
- | |
210 | } |
208 | } |
- | 209 | forstruct.from=0; forstruct.to=i-1; forstruct.step=1; return 0; |
|
- | 210 | } |
|
211 |
|
211 | top=wordchr(p,"to"); if(top==NULL) { |
212 |
|
212 | inp=strchr(p,'='); if(inp==NULL) return -1; |
213 |
|
213 | *inp++=0; goto forin; |
214 |
|
214 | } |
215 |
|
215 | *top=0; top+=strlen("to"); |
216 |
|
216 | stepp=wordchr(top,"step"); if(stepp!=NULL) { |
217 |
|
217 | *stepp=0; stepp+=strlen("step"); forstruct.step=evalue(stepp); |
218 |
|
218 | } |
219 |
|
219 | else forstruct.step=1; |
220 |
|
220 | forstruct.to=evalue(top); forstruct.type=for_from; |
221 |
|
221 | eqp=strchr(p,'='); fromp=wordchr(p,"from"); inp=wordchr(p,"in"); |
222 |
|
222 | if(eqp!=NULL && (fromp==NULL || eqp<fromp)) { |
223 |
|
223 | *eqp++=0; fromp=eqp; |
224 |
|
224 | } |
225 |
|
225 | else { |
226 |
|
226 | if(fromp==NULL) return -1; |
227 |
|
227 | *fromp=0; fromp+=strlen("from"); |
228 |
|
228 | } |
229 |
|
229 | forstruct.from=evalue(fromp); |
230 |
|
230 | if(for_getvar(p)) return -1; |
231 |
|
231 | if(!isfinite(forstruct.from+forstruct.to+forstruct.step)) return 1; |
232 |
|
232 | else return 0; |
233 | } |
233 | } |
234 | - |