1 /*------------------------------------------------------------*
2  | bi_funct.c                                                 |
3  | copyright 1999,  Andrew Sumner                             |
4  | copyright 1991,  Michael D. Brennan                        |
5  |                                                            |
6  | This is a source file for the awka package, a translator   |
7  | of the AWK programming language to ANSI C.                 |
8  |                                                            |
9  | The file is a heavily modified version of bi_funct.c from  |
10  | Mawk, an implementation of the AWK processing language,    |
11  | distributed by Michael Brennan under the GPL.              |
12  |                                                            |
13  | This program is free software; you can redistribute it     |
14  | and/or modify it under the terms of the GNU General Public |
15  | License as published by the Free Software Foundation;      |
16  | either version 2 of the License, or any later version.     |
17  |                                                            |
18  | This program is distributed in the hope that it will be    |
19  | useful, but WITHOUT ANY WARRANTY; without even the implied |
20  | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    |
21  | PURPOSE.  See the GNU General Public License for more      |
22  | details.                                                   |
23  |                                                            |
24  | You should have received a copy of the GNU General Public  |
25  | License along with this program; if not, write to the      |
26  | Free Software Foundation, Inc., 675 Mass Ave, Cambridge,   |
27  | MA 02139, USA.                                             |
28  *------------------------------------------------------------*/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 /*
35  * Revision 1.1  1999/03/17
36  * Basically reduced functions to stubs where possible, as
37  * they don't need to do anything, just exist for the purposes
38  * of parsing and translation.  - andrew.
39  */
40 
41 #include "awka.h"
42 #include "bi_funct.h"
43 #include "bi_vars.h"
44 #include "memory.h"
45 #include "init.h"
46 #include "files.h"
47 #include "fin.h"
48 #include "field.h"
49 #include "repl.h"
50 #include <math.h>
51 
52 /* global for the disassembler */
53 char *bi_names[] =
54 {
55    "length",
56    "index",
57    "substr",
58    "sprintf",
59    "sin",
60    "cos",
61    "atan2",
62    "exp",
63    "log",
64    "int",
65    "sqrt",
66    "rand",
67    "srand",
68    "close",
69    "system",
70    "toupper",
71    "tolower",
72    "fflush",
73    "gensub",
74    NULL
75 };
76 
77 BI_REC bi_funct[] =
78 {                                /* info to load builtins */
79 
80    "length", bi_length, 0, 1,        /* special must come first */
81    "index", bi_index, 2, 2,
82    "substr", bi_substr, 2, 3,
83    "sprintf", bi_sprintf, 1, 255,
84    "sin", bi_sin, 1, 1,
85    "cos", bi_cos, 1, 1,
86    "atan2", bi_atan2, 2, 2,
87    "exp", bi_exp, 1, 1,
88    "log", bi_log, 1, 1,
89    "int", bi_int, 1, 1,
90    "sqrt", bi_sqrt, 1, 1,
91    "rand", bi_rand, 0, 0,
92    "srand", bi_srand, 0, 1,
93    "close", bi_close, 1, 1,
94    "system", bi_system, 1, 1,
95    "toupper", bi_toupper, 1, 1,
96    "tolower", bi_tolower, 1, 1,
97    "fflush", bi_fflush, 0, 1,
98    "gensub", bi_gensub, 3, 4,
99 
100    (char *) 0, (PF_CP) 0, 0, 0} ;
101 
102 char
bi_funct_find(char * name)103 bi_funct_find(char *name)
104 {
105   int i=0;
106 
107   while (bi_names[i])
108   {
109     if (!strcmp(name, bi_names[i]))
110       break;
111     i++;
112   }
113 
114   if (bi_names[i])
115     return 1;
116   return 0;
117 }
118 
119 /* load built-in functions in symbol table */
120 void
bi_funct_init()121 bi_funct_init()
122 {
123    register BI_REC *p ;
124    register SYMTAB *stp ;
125 
126    /* length is special (posix bozo) */
127    stp = insert(bi_funct->name) ;
128    stp->type = ST_LENGTH ;
129    stp->stval.bip = bi_funct ;
130 
131    for (p = bi_funct + 1; p->name; p++)
132    {
133       stp = insert(p->name) ;
134       stp->type = ST_BUILTIN ;
135       stp->stval.bip = p ;
136    }
137 
138    /* seed rand() off the clock */
139    {
140       CELL c ;
141 
142       c.type = 0 ;  bi_srand(&c) ;
143    }
144 
145 }
146 
147 char *
str_str(target,key,key_len)148 str_str(target, key, key_len)
149    register char *target ;
150    char *key ;
151    unsigned key_len ;
152 {
153    register int k = key[0] ;
154 
155    switch (key_len)
156    {
157       case 0:
158          return (char *) 0 ;
159       case 1:
160          return strchr(target, k) ;
161       case 2:
162          {
163             int k1 = key[1] ;
164             while (target = strchr(target, k))
165                if (target[1] == k1)  return target ;               else  target++ ;
166             /*failed*/
167             return (char *) 0 ;
168          }
169    }
170 
171    key_len-- ;
172    while (target = strchr(target, k))
173    {
174       if (strncmp(target + 1, key + 1, key_len) == 0)  return target ;
175       else  target++ ;
176    }
177    /*failed*/
178    return (char *) 0 ;
179 }
180 
181 
182 /*
183  * following functions are stubs, because the translator needs
184  * them to exist but they are never actually called.
185  */
186 CELL *
bi_gensub(sp)187 bi_gensub(sp)
188    CELL *sp ;
189 {
190    return (CELL *) NULL ;
191 }
192 
193 CELL *
bi_argval(sp)194 bi_argval(sp)
195    CELL *sp ;
196 {
197    return (CELL *) NULL ;
198 }
199 
200 CELL *
bi_argcount(sp)201 bi_argcount(sp)
202    CELL *sp ;
203 {
204    return (CELL *) NULL ;
205 }
206 
207 CELL *
bi_asort(sp)208 bi_asort(sp)
209    CELL *sp ;
210 {
211    return (CELL *) NULL ;
212 }
213 
214 CELL *
bi_alength(sp)215 bi_alength(sp)
216    CELL *sp ;
217 {
218    return (CELL *) NULL ;
219 }
220 
221 CELL *
bi_totitle(sp)222 bi_totitle(sp)
223    CELL *sp ;
224 {
225    return sp ;
226 }
227 
228 CELL *
bi_ascii(sp)229 bi_ascii(sp)
230    register CELL *sp ;
231 {
232    return sp ;
233 }
234 
235 CELL *
bi_char(sp)236 bi_char(sp)
237   register CELL *sp ;
238 {
239   return sp;
240 }
241 
242 
243 CELL *
bi_trim(sp)244 bi_trim(sp)
245    register CELL *sp ;
246 {
247    return sp ;
248 }
249 
250 CELL *
bi_rtrim(sp)251 bi_rtrim(sp)
252    register CELL *sp ;
253 {
254    return sp ;
255 }
256 
257 
258 CELL *
bi_ltrim(sp)259 bi_ltrim(sp)
260    register CELL *sp ;
261 {
262    return sp ;
263 }
264 
265 CELL *
bi_left(sp)266 bi_left(sp)
267    CELL *sp ;
268 {
269    return sp ;
270 }
271 
272 CELL *
bi_right(sp)273 bi_right(sp)
274    CELL *sp ;
275 {
276    return sp ;
277 }
278 
279 CELL *
bi_min(sp)280 bi_min(sp)
281   register CELL *sp ;
282 {
283   return sp ;
284 }
285 
286 CELL *
bi_max(sp)287 bi_max(sp)
288   register CELL *sp ;
289 {
290   return sp ;
291 }
292 
293 /************************************************
294   bit operations (extensions still)
295  ************************************************/
296 
297 CELL *
bi_and(sp)298 bi_and(sp)
299   register CELL *sp ;
300 {
301   return sp ;
302 }
303 
304 CELL *
bi_or(sp)305 bi_or(sp)
306   register CELL *sp ;
307 {
308   return sp ;
309 }
310 
311 CELL *
bi_xor(sp)312 bi_xor(sp)
313   register CELL *sp ;
314 {
315   return sp ;
316 }
317 
318 CELL *
bi_compl(sp)319 bi_compl(sp)
320   register CELL *sp ;
321 {
322   return sp ;
323 }
324 
325 CELL *
bi_lshift(sp)326 bi_lshift(sp)
327   register CELL *sp ;
328 {
329   return sp ;
330 }
331 
332 CELL *
bi_rshift(sp)333 bi_rshift(sp)
334   register CELL *sp ;
335 {
336   return sp ;
337 }
338 
339 CELL *
bi_time(sp)340 bi_time(sp)
341    register CELL *sp ;
342 {
343    return sp ;
344 }
345 
346 CELL *
bi_systime(sp)347 bi_systime(sp)
348    register CELL *sp ;
349 {
350    return sp ;
351 }
352 
353 CELL *
bi_localtime(sp)354 bi_localtime(sp)
355    register CELL *sp ;
356 {
357    return sp ;
358 }
359 
360 CELL *
bi_mktime(sp)361 bi_mktime(sp)
362    register CELL *sp ;
363 {
364    return sp ;
365 }
366 
367 CELL *
bi_gmtime(sp)368 bi_gmtime(sp)
369    register CELL *sp ;
370 {
371    return sp ;
372 }
373 
374 CELL *
bi_strftime(sp)375 bi_strftime(sp)
376    register CELL *sp ;
377 {
378    return sp ;
379 }
380 
381 /**************************************************
382  string builtins (except split (in split.c) and [g]sub (at end))
383  **************************************************/
384 
385 CELL *
bi_length(sp)386 bi_length(sp)
387    register CELL *sp ;
388 {
389    return sp ;
390 }
391 
392 CELL *
bi_index(sp)393 bi_index(sp)
394    register CELL *sp ;
395 {
396    return sp ;
397 }
398 
399 /*  substr(s, i, n)
400     if l = length(s)  then get the characters
401     from  max(1,i) to min(l,n-i-1) inclusive */
402 
403 CELL *
bi_substr(sp)404 bi_substr(sp)
405    CELL *sp ;
406 {
407    return sp ;
408 }
409 
410 /*
411   match(s,r)
412   sp[0] holds r, sp[-1] holds s
413 */
414 
415 CELL *
bi_match(sp)416 bi_match(sp)
417    register CELL *sp ;
418 {
419    return sp ;
420 }
421 
422 CELL *
bi_toupper(sp)423 bi_toupper(sp)
424    CELL *sp ;
425 {
426    return sp ;
427 }
428 
429 CELL *
bi_tolower(sp)430 bi_tolower(sp)
431    CELL *sp ;
432 {
433    return sp ;
434 }
435 
436 CELL *
bi_sin(sp)437 bi_sin(sp)
438    register CELL *sp ;
439 {
440    return sp ;
441 }
442 
443 CELL *
bi_cos(sp)444 bi_cos(sp)
445    register CELL *sp ;
446 {
447    return sp ;
448 }
449 
450 CELL *
bi_atan2(sp)451 bi_atan2(sp)
452    register CELL *sp ;
453 {
454    return sp ;
455 }
456 
457 CELL *
bi_log(sp)458 bi_log(sp)
459    register CELL *sp ;
460 {
461    return sp ;
462 }
463 
464 CELL *
bi_exp(sp)465 bi_exp(sp)
466    register CELL *sp ;
467 {
468    return sp ;
469 }
470 
471 CELL *
bi_int(sp)472 bi_int(sp)
473    register CELL *sp ;
474 {
475    return sp ;
476 }
477 
478 CELL *
bi_sqrt(sp)479 bi_sqrt(sp)
480    register CELL *sp ;
481 {
482    return sp ;
483 }
484 
485 CELL *
bi_srand(sp)486 bi_srand(sp)
487    register CELL *sp ;
488 {
489    return sp ;
490 }
491 
492 CELL *
bi_rand(sp)493 bi_rand(sp)
494    register CELL *sp ;
495 {
496    return sp ;
497 }
498 
499 CELL *
bi_close(sp)500 bi_close(sp)
501    register CELL *sp ;
502 {
503    return sp ;
504 }
505 
506 
507 CELL *
bi_fflush(sp)508 bi_fflush(sp)
509    register CELL *sp ;
510 {
511    return sp ;
512 }
513 
514 
515 CELL *
bi_system(sp)516 bi_system(sp)
517    CELL *sp ;
518 {
519    return sp ;
520 }
521 
522 CELL *
bi_getline(sp)523 bi_getline(sp)
524    register CELL *sp ;
525 {
526    return sp ;
527 }
528 
529 CELL *
bi_sub(sp)530 bi_sub(sp)
531    register CELL *sp ;
532 {
533    return sp ;
534 }
535 
536 CELL *
bi_gsub(sp)537 bi_gsub(sp)
538    register CELL *sp ;
539 {
540    return sp ;
541 }
542 
543 CELL *
bi_split(sp)544 bi_split(sp)
545    register CELL *sp ;
546 {
547    return sp ;
548 }
549 
550