File: calc.l

package info (click to toggle)
funtools 1.4.4+dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,168 kB
  • ctags: 10,760
  • sloc: ansic: 87,238; sh: 9,727; lex: 4,595; asm: 3,281; ada: 1,681; makefile: 1,458; pascal: 1,089; cpp: 1,001; cs: 879; perl: 161; yacc: 64; sed: 32; csh: 10; tcl: 9
file content (808 lines) | stat: -rw-r--r-- 17,952 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
%option caseless
%array

%{
#include <ctype.h>
#include <funtoolsP.h>
#include <macro.h>
#include <calc.h>
#include <tabcalc_c.h>

/* define the types of event records we have to handle */
#define REC_CUR		1
#define REC_PREV	2
#define REC_NEXT	4

/* misc */
#define MAX_INCLUDE_DEPTH  100
#define SEGINC            1024
#define MAXCODE 	     4

/* symbols for each column we encounter */
typedef struct calccoltab {
  struct calccoltab *next;
  char *name;
  char *sname;
  char *cname;
  char *tdim;
  int type;
  int n;
  double tlmin;
  double tlmax;
  double binsiz;
  double tscale;
  double tzero;
  int scaled;
  int exists;
  int settype;
} *CalcCols, CalcColRec;

static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static int include_stack_ptr = 0;
static Fun ifun;
static CalcCols columns;
static int laststart;

static char *prog;
static char *command;
static char *autodeclare;
static char *mbuf;
static int mlen;
static int n;
static int var;
static int rectype;
static int args;
static int doreplace=0;

static char *code[MAXCODE];
static char *expr;
static char **strptr=&expr;
static int ncode[MAXCODE];
static int nexpr;
static int xnl;
static int *lenptr=&nexpr;

static void setn _PRx((int newn));
static char *_CalcMake _PRx((void));
static int _CalcColProc _PRx((char *s, int expl));
static int _CalcColName _PRx((char *sname));
static CalcCols _CalcColNew _PRx((Fun fun, 
                                  char *name, char *sname, char *cname, 
			          int type, int n, 
				  double tlmin, double tlmax, double binsiz,
				  double tscale, double tzero, int scaled,
				  int exists, int settype));
static CalcCols _CalcColLookup _PRx((char *cname));

int _calcerror _PRx((char *msg));
%}

DIG	[0-9]
DIG2	[0-9a-fA-F]
BINARY  0[bB][01]+
INT1	[-+]?{DIG}+L?
INT2	[-+]?0[xX]{DIG2}+L?
INT	({INT1}|{INT2})
FLOAT1  [-+]?{DIG}+\.?([eE][-+]?{DIG}+)?
FLOAT2  [-+]?{DIG}*\.{DIG}+([eE][-+]?{DIG}+)?
FLOAT   ({FLOAT1}|{FLOAT2})
NUM	({INT}|{FLOAT})

SNAME	(cur|prev|next)
NAME	[A-Za-z~_][0-9A-Za-z~_]*(\[[0-9]+\])?
TYPE    (\[{INT}\])?(:{NUM}?[a-z]?){1,4}
COL	{SNAME}->{NAME}{TYPE}?
ECOL    explicit
FILE	@[0-9A-Za-z~_/\-\.]*(\[.*\])?
VAR     [ \t]*(char|short|int|float|double)
INCL    ^#[\t ]*include[ \t]*(\".*\"|<.*>)
DEF     ^#[\t ]*define[ \t]+.*
XCOM	#
BCOM    "/*"
ECOM    "*/"

%x INCLUDE
%x VAR
%x COM
%x XCOM
%x ECOL
%%

global		{ setn(0);  var = 0; }
local		{ setn(1);  var = 0; }
before		{ setn(2);  var = 0; }
after		{ setn(3);  var = 0; }
end		{ setn(-1); var = 0; }

{FILE}		{
		char *s;
		if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
		  _calcerror("include files are nested too deeply");
		if( !(s = (char *)FileContents(yytext+1, 0, NULL)) )
		  _calcerror("can't access include file");
		else {
		  include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
		  BEGIN INITIAL; yy_scan_string(s); BEGIN INITIAL;
		  if( s ) xfree(s);
		}
}

{VAR}		{ 
		if( var ){ BEGIN VAR; setn(1); }
		_CalcCat(yytext, strptr, lenptr);
}
<VAR>;		{
		_CalcCat(yytext, strptr, lenptr);
		_CalcCat("\n", strptr, lenptr);
		if( var ){ BEGIN INITIAL; setn(-1); }
}
<VAR>.		{ _CalcCat(yytext, strptr, lenptr); }

{INCL}          { _CalcCat(yytext, strptr, lenptr); }

{DEF}           { _CalcCat(yytext, strptr, lenptr); }

^[ \t]*{XCOM}   { laststart = YY_START; xnl=0; BEGIN XCOM; }
{XCOM}          { laststart = YY_START; xnl=1; BEGIN XCOM; }
<XCOM>\n        { BEGIN laststart; if( xnl ) unput('\n'); }
<XCOM>.         { /* ignore comments up to eol */ ; }

{BCOM}		{
	         _CalcCat(yytext, strptr, lenptr);
		 laststart = YY_START; BEGIN COM;
}
<COM>{ECOM}	{ _CalcCat(yytext, strptr, lenptr); BEGIN laststart; }
<COM>\n	        { _CalcCat(yytext, strptr, lenptr); }
<COM>.	        { _CalcCat(yytext, strptr, lenptr); }

{COL}		{ _CalcColProc(yytext, 0); var=0;}

{ECOL}		{  laststart = YY_START; BEGIN ECOL; }
<ECOL>{NAME}	{ _CalcColProc(yytext, 1); var=0; }
<ECOL>[, \t]	{ ; }
<ECOL>\n	{ BEGIN laststart; }

\n		{ _CalcCat(yytext, strptr, lenptr);}

[ \t]*		{ _CalcCat(yytext, strptr, lenptr);}

.		{ _CalcCat(yytext, strptr, lenptr); var=0;}

<<EOF>>		{
		if ( --include_stack_ptr < 0 ){
		  prog = _CalcMake();
		  yy_delete_buffer( YY_CURRENT_BUFFER );
                  yyterminate();
		} else {
		  yy_delete_buffer( YY_CURRENT_BUFFER );
		  yy_switch_to_buffer(include_stack[include_stack_ptr] );
		}
}

%%

/*
 *
 * Private Routines 
 *
 *
 */

/* setn -- set pointer to where expression is stored */
#ifdef YY_USE_PROTOS
static void setn(int newn)
#else
static void setn(newn)
    int newn;
#endif
{
  if( newn >=0 ){
    n = newn;
    strptr = &code[n]; lenptr = &ncode[n];
  }
  else{
    strptr = &expr;    lenptr = &nexpr;
  }
}

/*
 *
 * _CalcDatatype -- return C data type as a string
 *
 */
#ifdef YY_USE_PROTOS
static char *
_CalcDatatype(int type)
#else
static char *_CalcDatatype(type)
	int type;
#endif
{
    switch ( type ) {
     case 'A':		return "char";
     case 'X':		return "char";
     case 'B':		return "unsigned char";
     case 'I':		return "short";
     case 'U':		return "unsigned short";
     case 'J':		return "int";
     case 'K':		return "long long";
     case 'V':		return "unsigned int";
     case 'L':		return "char";
     case 'E':		return "float";
     case 'D':		return "double";
    }
    _calcerror("unknown/illegal data type for new column");
    return "???";
}

/*
 *
 * _CalcColName -- see if sname is a recognized column name
 *
 */
#ifdef YY_USE_PROTOS
static int
_CalcColName(char *sname)
#else
static int _CalcColName(sname)
     char *sname;
#endif
{
  if( !strcasecmp(sname, "prev") ){
    strcpy(sname, "prev");
    rectype |= REC_PREV;
    return REC_PREV;
  }
  if( !strcasecmp(sname, "cur") ){
    strcpy(sname, "cur");
    rectype |= REC_CUR;
    return REC_CUR;
  }
  if( !strcasecmp(sname, "next") ){
    strcpy(sname, "next");
    rectype |= REC_NEXT;
    return REC_NEXT;
  }
  else{
    return 0;
  }
}

/*
 *
 * _CalcCB -- macro expansion callback
 *
 */
#ifdef YY_USE_PROTOS
static char *
_CalcCB(char *name, void *client_data)
#else
static char *_CalcCB(name, client_data)
	char *name;
	void *client_data;
#endif
{
  CalcCols cur;  
  char tbuf[SZ_LINE];
  char tbuf2[SZ_LINE*2];
  char tbuf3[SZ_LINE];
  int i, got, ip;
  
  /* avoid -W unused parameter warning */
  if( 0 ) client_data = client_data;

  /* start at beginning of macro buffer */
  /* allocate the macro buffer */
  if( mbuf ) xfree(mbuf);
  mbuf = NULL;
  mlen = SZ_LINE;

  if( !strcmp(name, "MEMBERS") ){
    if( !columns ){
      _CalcCat("int dummy;\n", &mbuf, &mlen);
      return mbuf;
    }    
    for(cur=columns; cur!=NULL; cur=cur->next){
      snprintf(tbuf, SZ_LINE, "%s %s", _CalcDatatype(cur->type), cur->cname);
      _CalcCat(tbuf, &mbuf, &mlen);
      if( cur->n > 1 ){
        /* if there is no tdim, just make it a simple array */
        /* (also, do the simple thing for bit-fields) */
        if( (cur->tdim == NULL) || (cur->type == 'X') ){
          switch(cur->type){
          case 'X':
            i = (cur->n+7)/8;
            break;
          default:
            i = cur->n;
            break;
          }
          snprintf(tbuf, SZ_LINE, "[%d]", i);
        }
        /* tdim exists, so use it to declare a multidimensional array */
        else{
          newdtable(",()");
          /* I hear tdim is in Fortran order, so reverse it for C order */
          for(*tbuf = '\0', ip=0, got=0; word(cur->tdim, tbuf3, &ip); got++){
            snprintf(tbuf2, SZ_LINE, "[%s]", tbuf3);
            if( *tbuf )
              strcat(tbuf2, tbuf);
            strcpy(tbuf, tbuf2);
          }          
          freedtable();
          /* but if we got nothing out of it, go back to simplicity */
          if( !got )
            snprintf(tbuf, SZ_LINE, "[%d]", cur->n);
        }
        _CalcCat(tbuf, &mbuf, &mlen);
      }
      _CalcCat(";", &mbuf, &mlen);
      if( cur->next )
        _CalcCat("\n", &mbuf, &mlen);
    }
    return mbuf;
  }
  else if( !strcmp(name, "SELECT") ){
    if( !columns ){
      return "";
    }    
    for(cur=columns; cur!=NULL; cur=cur->next){
      *tbuf2 = '\0';
      if( (cur->tlmin != 0.0) || (cur->tlmax != 0.0) ){
        snprintf(tbuf2, SZ_LINE, "%f:%f", cur->tlmin, cur->tlmax);
        if( cur->binsiz != 1.0 ){
	  snprintf(tbuf3, SZ_LINE, ":%f", cur->binsiz);
	  strncat(tbuf2, tbuf3, SZ_LINE);
	}
        if( (cur->tscale != 1.0) || (cur->tzero != 0.0) ){
	  snprintf(tbuf3, SZ_LINE, ";%f", cur->tscale);
	  strncat(tbuf2, tbuf3, SZ_LINE);
	  snprintf(tbuf3, SZ_LINE, ":%f", cur->tzero);
	  strncat(tbuf2, tbuf3, SZ_LINE);
	}
      }
      if( *tbuf2 ){
        snprintf(tbuf, SZ_LINE, "\"%s\", \"%d%c:%s\", ",
		       cur->cname, cur->n, cur->type, tbuf2);
      }
      else{
        snprintf(tbuf, SZ_LINE, "\"%s\", \"%d%c\", ",
		       cur->cname, cur->n, cur->type);
      }
      _CalcCat(tbuf, &mbuf, &mlen);
      if( cur->exists )
        _CalcCat("\"rw\", ", &mbuf, &mlen);
      else
        _CalcCat("\"w\", ", &mbuf, &mlen);
      snprintf(tbuf, SZ_LINE, "FUN_OFFSET(Row, %s),", cur->cname);
      _CalcCat(tbuf, &mbuf, &mlen);
      if( cur->next )
        _CalcCat("\n", &mbuf, &mlen);
    }
    return mbuf;
  }
  else if( !strcmp(name, "RECTYPE") ){
    snprintf(tbuf, SZ_LINE, "%d", rectype);
    _CalcCat(tbuf, &mbuf, &mlen);
    return mbuf;
  }
  else if( !strcmp(name, "EXPR") ){
    if( expr && *expr )
      return(expr);
    else
      return "";
  }
  else if( !strcmp(name, "COMMAND") ){
    if( command && *command )
      return(command);
    else
      return "<build command not available>";
  }
  else if( !strcmp(name, "ARGS") ){
    if( args == 1 )
      return "1";
    else
      return "2";
  }
  else if( !strcmp(name, "AUTO") ){
    if( autodeclare && *autodeclare )
      return autodeclare;
    else
      return "";
  }
  else if( !strcmp(name, "GLOBAL") ){
    if( code[0] && *code[0] )
      return(code[0]);
    else
      return "";
  }
  else if( !strcmp(name, "LOCAL") ){
    if( code[1] && *code[1] )
      return(code[1]);
    else
      return "";
  }
  else if( !strcmp(name, "BEFORE") ){
    if( code[2] && *code[2] )
      return(code[2]);
    else
      return "";
  }
  else if( !strcmp(name, "AFTER") ){
    if( code[3] && *code[3] )
      return(code[3]);
    else
      return "";
  }
  else if( !strcmp(name, "MERGE") ){
    if( doreplace )
      strncpy(tbuf, "replace", SZ_LINE);
    else
      strncpy(tbuf, "update", SZ_LINE);
    _CalcCat(tbuf, &mbuf, &mlen);
    return mbuf;
  }
  else{
    _CalcCat("$", &mbuf, &mlen);
    _CalcCat(name, &mbuf, &mlen);
    return mbuf;
  }
}

/*
 *
 * _CalcMake -- create the string containing the program by macro expansion
 *
 */
#ifdef YY_USE_PROTOS
static char *
_CalcMake(void)
#else
static char *_CalcMake()
#endif
{
  char *s;

  /* make sure we have something */
  if( !expr || !*expr )
    expr = xstrdup(";");
  s = xstrdup(expr);
  nowhite(s, expr);
  if( s ) xfree(s);
  /* add final ';' is necessary (but not if there is any compund statement) */
  if( !strchr(expr,';') && !strchr(expr,'{') )
    _CalcCat(";", &expr, &nexpr);  
  /* expand program body to add specifics of the expression */
  s = ExpandMacro(TABCALC_C, NULL, NULL, 0, _CalcCB, NULL);
  return s;
}

/* look up a column entry, add if not present */
#ifdef YY_USE_PROTOS
CalcCols
_CalcColLookup(char *cname)
#else
CalcCols
_CalcColLookup(cname)
	char *cname;
#endif
{
  CalcCols cur;  
  for(cur=columns; cur!=NULL; cur=cur->next){
    if( !strcasecmp(cname, cur->cname) )
      return cur;
  }
  return NULL;
}

/* look up a symbol table entry, add if not present */
#ifdef YY_USE_PROTOS
CalcCols
_CalcColNew(Fun fun, char *name, char *sname, char *cname, int type, int n,
            double tlmin, double tlmax, double binsiz, 
	    double tscale, double tzero, int scaled,
	    int exists, int settype)
#else
CalcCols
_CalcColNew(fun, name, sname,
            cname, type, n, tlmin, tlmax, binsiz, 
	    tscale, tzero, scaled,
	    exists, settype)
        Fun fun;
	char *name, *sname, *cname;
	int type, n;
        double tlmin, tlmax, binsiz;
	double tscale, tzero;
	int scaled, exists, settype;
#endif
{
  CalcCols sym, cur;  
  int got;
  int enter=1;

  if( (sym=_CalcColLookup(cname)) ){
    if( !settype ) return sym;
    enter=0;
  }
  else{
    /* allocate a new symbol record */
    if( !(sym = (CalcCols)xcalloc(1, sizeof(CalcColRec))) )
      return NULL;
  }

  /* fill in the blanks */
  if( sym->name ) xfree(sym->name);
  sym->name = xstrdup(name);
  if( sym->sname ) xfree(sym->sname);
  sym->sname = xstrdup(sname);
  if( sym->cname ) xfree(sym->cname);
  sym->cname = xstrdup(cname);
  sym->type = type;
  sym->n = n;
  sym->tlmin = tlmin;
  sym->tlmax = tlmax;
  sym->binsiz = binsiz;
  sym->tscale = tscale;
  sym->tzero = tzero;
  sym->scaled = scaled;
  sym->exists = exists;
  sym->settype = settype;

  /* get tdim value, if there is one */
  if( sym->exists && (sym->n>1) )
    sym->tdim = FunParamGets(fun, "TDIM", exists, NULL, &got);

  /* add to list, maintaining order */
  if( enter ){
    if( !columns ){
      columns = sym;
    }
    else{
      for(cur=columns; cur->next!=NULL; cur=cur->next)
        ;
      cur->next = sym;
    }
  }
  return sym;
}

/* process a column */
#ifdef YY_USE_PROTOS
static int
_CalcColProc(char *s, int expl)
#else
static int _CalcColProc(s, expl)
     char *s;
     int expl;
#endif
{
  char *e;
  char *t;
  char name[SZ_LINE];
  char sname[SZ_LINE];
  char cname[SZ_LINE];
  char aname[SZ_LINE];
  char fname[SZ_LINE];
  int ptype;
  int poff;
  int exists;
  int dims;
  int settype=0;
  int type=0;
  int n=0;
  int scaled=0;
  double tlmin=0.0, tlmax=0.0, binsiz=1.0;  
  double tscale=1.0, tzero=0.0;

  /* save expression */
  e = s;

  /* gather struct name */
  for(t=sname; *s && (*s != '-');)
    *t++ = *s++;
  *t = '\0';
  /* skip past "->" */
  if( *s && (*s == '-') ) s += 2;

  /* if this is not a special name, just append it and exit */
  if( !_CalcColName(sname) ){
    if( !expl ){
      _CalcCat(e, &expr, &nexpr);
      return 1;
    }
  }

  /* gather column name */
  for(t=cname; *s && (*s != '[') && (*s != ':');)
    *t++ = *s++;
  *t = '\0';

  /* explicit columns don't have the prefix */
  if( expl && *sname && !*cname ){
    strncpy(cname, sname, SZ_LINE);
    strncpy(sname, "cur", SZ_LINE);
  }

  /* gather array part */
  if( *s && (*s == '[') ){
    for(t=aname; *s && (*s != ':');)
      *t++ = *s++;
    *t = '\0';
  }
  else
    *aname = '\0';
  if( *aname )
    snprintf(fname, SZ_LINE, "%s->%s%s", sname, cname, aname);
  else
    snprintf(fname, SZ_LINE, "%s->%s", sname, cname);

  /* the expression name is the combination of sname (which we will set to
     a struct we know about) and cname (which is a struct member name) */
  snprintf(name, SZ_LINE, "%s->%s", sname, cname);

  /* see if this column exists or if its new */
  exists = FunColumnLookup(ifun, cname, 0, NULL, &type, NULL, NULL, &n, NULL);

  /* gather up specifier info */
  for(; *s; s++){
    if( *s == ':' ){
      _FunColumnType(s+1, &type, &n, &tlmin, &tlmax, &binsiz, &dims,
			  &tscale, &tzero, &scaled, &ptype, &poff);
      settype = 1;
      break;
    }
  }
  if( !exists && !settype && !_CalcColLookup(cname) )
    _calcerror("new column requires a type");
  if( exists && settype )
    doreplace=1;

  /* add this column to the list */
  if(_CalcColNew(ifun, name, sname, cname,
                 type, n, tlmin, tlmax, binsiz, 
		 tscale, tzero, scaled,
		 exists, settype)){
    if( !expl ) _CalcCat(fname, &expr, &nexpr);
    return 1;
  }
  else{
    return _calcerror("could not enter new funcalc column");
  }  
}

/*
 *
 * Semi-public Routines 
 *
 *
 */

/*
 *
 * _CalcCat -- add a string to the filter string buffer
 *
 */
#ifdef YY_USE_PROTOS
void
_CalcCat(char *str, char **ostr, int *olen)
#else
void _CalcCat(str, ostr, olen)
     char *str;
     char **ostr;
     int *olen;
#endif
{
  int blen;
  int slen;
  
  if( (str == NULL) || (*str == '\0') )
    return;
  else
    slen = strlen(str) + 1;
  
  if( (*ostr == NULL) || (**ostr == '\0') )
    blen = 0;
  else
    blen = strlen(*ostr);
  
  while( (blen + slen) >= *olen ){
    *olen += SEGINC;
  }
  if( blen == 0 )
    *ostr = (char *)xcalloc(*olen, sizeof(char));
  else
    *ostr = (char *)xrealloc(*ostr, *olen);
  strcat(*ostr, str);
}

#ifdef YY_USE_PROTOS
char *
FunCalcParse(char *iname, char *oname,
             char *cmd, char *ex, char *autod, int narg)
#else
char *FunCalcParse(iname, oname, cmd, ex, autod, narg)
    char *iname;
    char *oname;
    char *cmd;
    char *ex;
    char *autod;
    int narg;
#endif
{
  int i;

  /* avoid -W unused parameter warning */
  if( 0 ) oname = oname;

  /* initialize global variables */
  n = 0;
  rectype = REC_CUR;
  var = 1;
  args = narg;

  /* save command */
  if( command ) xfree(command);
  command = xstrdup(cmd);

  /* save autodeclare */
  if( autodeclare ) xfree(autodeclare);
  autodeclare = xstrdup(autod);

  /* open the input FITS file */
  if( !(ifun = FunOpen(iname, "rc", NULL)) )
    gerror(stderr, "can't FunOpen input file (or find extension): %s\n",
	   iname);

  /* parse expression */
  yy_scan_string(ex);
  yylex();

  /* done with input FITS file */
  FunClose(ifun);
  /* free up space */
  if( expr ){
    xfree(expr);
    expr = NULL;
  }
  nexpr = 0;
  for(i=0; i<MAXCODE; i++){
    if( code[i] ){
      xfree(code[i]);
      code[i] = NULL;
    }
    ncode[i] = 0;
  }
  if( mbuf ){
    xfree(mbuf);
    mbuf = NULL;
    mlen = 0;
  }
  /* return resulting program */
  return prog;
}

#ifdef YY_USE_PROTOS
int
_calcerror(char *msg)
#else
int _calcerror(msg)
     char *msg;
#endif
{
  if( *yytext )
    gerror(stderr,  "%s while processing '%s'\n",
           msg ? msg : "filterr", yytext);
  else
    gerror(stderr,  "%s\n", msg ? msg : "filterr");
  YY_FLUSH_BUFFER;
  yyterminate();
}

#ifdef YY_USE_PROTOS
int yywrap(void)
#else
int yywrap()
#endif
{
  return 1;
}