git @ Cat's Eye Technologies OpenZz / master src / source.c
master

Tree @master (Download .tar.gz)

source.c @masterraw · history · blame

  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
/* 
    Zz Dynamic Parser Library
    Copyright (C) 1989 - I.N.F.N - S.Cabasino, P.S.Paolucci, G.M.Todesco

    The Zz Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This Zz Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*============================================================================
  SOURCE.C
  Defines routines for managing the reading of data from various sources.
  Note that interactive terminal (tty) control is done through another 
  file (zzi.c) because the functionality is usually only required for
  development/testing purposes and not in the general zz library.
=============================================================================*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "avl.h"
#include "zlex.h"
#include "list.h"
#include "trace.h"
#include "source.h"
#include "rule.h"

/*PROTOTYPES*/
int parse(struct s_nt *);
void get_extension(char *, char *);
int change_extension(char *, const char *);

int (*find_prompt_proc)()=0;
int (*source_line_routine)()=0;

// Global data - current source(ptr to item in source stack) and current token
struct s_source *cur_source;
struct s_content cur_token;
 
#define SOURCE_N 49 /*era 49 e prima ancora 20*/
#define BACK_N 20

// Source stack and stack top index
static struct s_source source_stack[SOURCE_N];
static int source_sp=0;

// 'Back' stack with stack top index
static struct s_content back[BACK_N];
static int back_n=0;

/* no dot here !!! */
static const char *in_ext = "zz";

/**
 * Declare and provide constructors for output channel
 * Default output is set to stdout
 */
FILE *zz_chanout=0;
// Following initializers work but are not needed because each source
// tests to make sure the output stream is valiid.
// static void zz_chanout_construct (void) __attribute__((constructor));
// static void zz_chanout_construct (void) { zz_chanout = stdout; }

// Prototypes:
void next_token_file(struct s_source *src);
void next_token_list(struct s_source *src);


/*---------------------------------------------------------------------*/

void zz_set_output(filename)
     const char *filename;
{
  if(filename) {
    zz_chanout = fopen(filename,"w");

    if(!zz_chanout) {
      printf("zz: unable to open output file %s (%s)\n",filename, strerror(errno));
      zz_chanout=stdout;
    }
  }
  else /* code relies on this feauture... ugly!!! */
    zz_chanout=stdout;
}

void zz_set_output_stream(output_stream)
     FILE *output_stream;
{
  zz_assert(output_stream);
  zz_chanout=output_stream;
}

/*--------------------------------------------------------------------*/

/**
 * new_source() - set's the global 'cur_source' pointer to point to
 *   to a new (initialized with default values) s_source object.
 *   Call this to creat a new source and then populate the fields of
 *   'cur_source' as appropriate.
 */
struct s_source *new_source(next_token_function)
     void (*next_token_function)(struct s_source *src);
{
  INIT_ZLEX /* For safety */
    
    if (!next_token_function) {
      printf("Internal error - null next_token_function passed to new_source()");
      exit(0);
    }

    if(source_sp>=SOURCE_N-1) {
      printf("internal error - new_source: too many sources\n");
      exit(1);
    }
  
  if(cur_source) {
    if(cur_source->type==SOURCE_LIST)
      cur_source->src.list.current_pos = get_list_pos(cur_source->src.list.list);

    cur_source->cur_token = cur_token;
  }

  // Set the global cur_source pointer to the next free object on the source stack:
  cur_source = & source_stack [source_sp++];

  // Initialize default values in the new source
  cur_source->type = NO_SOURCE;
  cur_source->eof=0;
  cur_source->line_n = 0;

  // Assign appropriate token processing function:
  cur_source->read_next_token = next_token_function;

  return cur_source;
}


/*--------------------------------------------------------------------*/

void pop_source(void)
{
struct s_source *next;
if(!cur_source) return;
if(cur_source->type == SOURCE_FILE)
  {
   fclose(cur_source->src.file.chan);
   free(cur_source->src.file.filename);
  }
cur_source->type = NO_SOURCE;
source_sp--;
if(source_sp<=0) cur_source = 0;
else
  {
   cur_source = &source_stack[source_sp-1];
   if(cur_source->type==SOURCE_LIST)
     list_seek(cur_source->src.list.list,cur_source->src.list.current_pos);
   cur_token = cur_source->cur_token;
  }
}


/*--------------------------------------------------------------------*/

// Initialization function for file source:
int source_file(char *filename)
{
  FILE *chan;
  chan = fopen(filename,"r");

  if(!chan) 
    return 0;
  
  new_source(next_token_file);

  cur_source->type = SOURCE_FILE;
  cur_source->src.file.chan = chan;
  cur_source->src.file.filename = (char*)malloc(strlen(filename)+1);
  strcpy(cur_source->src.file.filename,filename);
  cur_source->src.file.old = cur_source->src.file.s = 0;
  cur_source->src.file.row[0]='\0';
  return 1;
}

/*--------------------------------------------------------------------*/


int source_pipe(void)
{
  new_source(next_token_file);

  cur_source->type = SOURCE_FILE;
  cur_source->src.file.chan = stdin;
  cur_source->src.file.filename = "stdin";
  cur_source->src.file.old = cur_source->src.file.s = 0;
  cur_source->src.file.row[0]='\0';
  return 1;
}


/*--------------------------------------------------------------------*/

int source_list(struct s_content *list, void *id)
{
  if(list->tag!=tag_list)
    {
      printf("Internal error - source_list; bad argument\n");
      exit(1);
    }

  new_source(next_token_list);

  cur_source->type = SOURCE_LIST;
  cur_source->src.list.list = list;
  cur_source->src.list.id = id;
  list_seek(list,0);
  cur_source->line_n=1;
  return 1;
}


/*--------------------------------------------------------------------*/
/*
  blocca lo stream di input. un successivo next_token ritornera' EOF
*/
void stop_source(void)
{
  if(cur_source) 
    cur_source->eof = 1;
}


/*--------------------------------------------------------------------*/

/**
 * Read the next token
*/
int next_token(struct s_content *token)
{
  int status;

  if(back_n>0) {
    zz_trace("next_token back!!!\n");
    *token = back[--back_n];
    return 1;
  }

  if(!cur_source || cur_source->eof) {
    token->tag=tag_eof;
    return 0;
  }

  status = 1;

  while(status)
    {
      zz_trace(">>> status=%d\n", status);

      // Cause the next token to be processed from current source:
      cur_source->read_next_token(cur_source);

      switch(status)
	{
	case 1:
	  if(cur_token.tag!=tag_cont)
	    status=0;
	  else 
	    status=2;
	  break;
	case 2:
	  if(cur_token.tag==tag_eol) 
	    status=1;
	  else 
	    if(cur_token.tag!=tag_cont) 
	      status=0;
	  //else if(cur_token.tag==tag_eof) status=0;
	}
    } 

  *token = cur_token;

  zz_trace("next_token '%z'\n", token); 

  return !cur_source->eof;
}

/*
  if(kill && cur_token.tag!=tag_eol)
  {
  cur_token.tag = tag_eof;
  if(cur_source->type==SOURCE_FILE) kill=0;
  return;      
  }
*/


/*--------------------------------------------------------------------*/


void next_token_file(src)
     struct s_source *src;
{
int i,ret;
char *s,*t;
if(!cur_source->src.file.s) 
  {
    zz_trace("reading new line...\n");
   /* NEED TO READ A NEW LINE */
   cur_source->src.file.row[0]='\0';
   ret = (int) fgets(cur_source->src.file.row,132,cur_source->src.file.chan);
   cur_source->eof = (ret==0);
   cur_source->line_n ++;
   s = t = cur_source->src.file.row;
   for(i=0;i<132 && *t && *t!='\n';i++) t++;
   if(*t=='\n') *t='\0';
   if(source_line_routine && source_sp==1)
     (*source_line_routine)(s,cur_source->line_n,cur_source->src.file.filename);
   cur_source->src.file.s = cur_source->src.file.old = s;
   if(cur_source->eof)
     cur_token.tag = tag_eof;
   else
     zlex(&(cur_source->src.file.s),&cur_token);
  }
else
  {
   /* DO NOT NEED TO READ NEW LINE */
   cur_source->src.file.old = cur_source->src.file.s;
   zlex(&(cur_source->src.file.s),&cur_token);
  }
 if(cur_token.tag==tag_eol) 
   {
     cur_source->src.file.s=0;
     zz_trace("tag_eol... s=0\n");
   }
}


/*--------------------------------------------------------------------*/


void next_token_list(src)
     struct s_source *src;
{
struct s_content *tmp;
tmp = next_list_item(cur_source->src.list.list);
if(!tmp)
  {
   cur_token.tag=tag_eol;
   s_content_value(cur_token)=0;
   cur_source->eof=1;
  }
else
  {
   if(tmp->tag==tag_eol) 
     cur_source->line_n++;
   cur_token = *tmp;
  }
}


/*---------------------------------------------------------------------*/


int zz_parse_pipe()
{
  int ret;

  if(!zz_chanout) 
    zz_set_output(0);

  if(!source_pipe()) {
    printf("zz: pipe not found\n");
    return 0;
  }

  ret = parse(find_nt("root"));

  pop_source();

  return ret;
}


/*--------------------------------------------------------------------*/


int back_token(token)
struct s_content *token;
{
if(back_n>=BACK_N-1) return 0;
back[back_n++] = *token;
return 1;
}


/*--------------------------------------------------------------------*/


static void cur_list_row(lst,row,curpos)
struct s_content *lst;
char row[];
int *curpos;
{
char item[256],*t;
struct s_content *cnt;
int k,j,i,pos,len,flag,totlen;
pos = get_list_pos(lst);
i = pos-2;
len = 0;
flag=0;
while(i>=0)
  {
   list_seek(lst,i);
   cnt=next_list_item(lst);
   if(cnt->tag==tag_eol && i<pos-2) break;
   sprintz(item,"%z",cnt);
   len+=strlen(item)+1;
   if(len>60) {flag=1;break;}
   i--;
  }
i++;
row[0]='\0';
if(flag)
  strcpy(row,"... ");
list_seek(lst,i);
for(j=i;j<pos-1;j++)
  {
   cnt = next_list_item(lst);
   if(cnt)
     {
      sprintz(item,"%z ",cnt);
      strcat(row,item);
     }
  }  
k = totlen = strlen(row);
while(1)
  {
   cnt = next_list_item(lst);
   if(!cnt || cnt->tag==tag_eol || cnt->tag==tag_eof) break;
   sprintz(item,"%z ",cnt);
   if(strlen(item)+totlen>200) 
     {
      strcat(row,"...");
      break;
     }
   strcat(row,item);
   totlen+strlen(item);
  }  
t = row+k;while(*t==' ' || *t=='\t')k++,t++;
*curpos = k;
list_seek(lst,pos);
}


/*--------------------------------------------------------------------*/

char *source_name(source)
     struct s_source *source;
{
char *s;
switch(source->type)
  {
  case SOURCE_FILE:
    s=source->src.file.filename;
    break;
  case SOURCE_TT:
    s="stdin";
    break;
  case SOURCE_LIST:
    s="ZZ-action";
    break;
  default:
    s="unknown source type";
  }
return s;
}


/*--------------------------------------------------------------------*/


int source_line(source)
     struct s_source *source;
{
  return source->line_n;
}


/*--------------------------------------------------------------------*/


int get_current_line(void)
{
struct s_source *source;
int sp;
sp = source_sp-1;
if(sp<0)
  {
   return -1;
  }
while(sp>0 && source_stack[sp].type==SOURCE_LIST)
  sp--;
if(sp<0)
  return -1;
source = &source_stack[sp];
return source->line_n;
}


/*--------------------------------------------------------------------*/

char *get_source_name(void)
{
  if(!cur_source) 
    return "NOSOURCE";
  else 
    return source_name(cur_source);
}


/*--------------------------------------------------------------------*/


int get_source_line(void)
{
  if(!cur_source) 
    return 0;
  else 
    return source_line(cur_source);
}

/*--------------------------------------------------------------------*/

void get_source_file(char *buffer)
{
int i;
struct s_source *source;
for(i=source_sp-1;i>=0;i--)
  {
   if(source_stack[i].type==SOURCE_FILE ||
      source_stack[i].type==SOURCE_TT) break;
  }
if(i<0)
  strcpy(buffer,"noname");
else
  {
   if(source_stack[i].type==SOURCE_FILE)
     strcpy(buffer,source_stack[i].src.file.filename);
   else
     strcpy(buffer,"stdin");
  }
}


/*---------------------------------------------------------------------*/


int zz_parse_file(filename)
     const char* filename;
{
  int ret;
  char full[256],type[40];
  type[0] = '\0';

  if(!zz_chanout) 
    zz_set_output(0);

  strcpy(full,filename);

  get_extension(full,type);

  if(!type[0] && strcmp(full,"/dev/tty"))
    change_extension(full, in_ext);

  if(!source_file(full)) {
    printf("zz_parse_file(): file %s not found\n",full);
    return 0;
  }

  ret = parse(find_nt("root"));
  pop_source();

  return ret;
}


/*--------------------------------------------------------------------*/


void fprint_source_position(chan,print_action_flag)
     FILE *chan;
     int print_action_flag;
{
struct s_source *source; 
char *t;
char row[256];
char *prompt = "| ";
int i,errpos,sp,flag;

sp = source_sp-1;
if(sp<0)
  {
   fprintf(chan,"%sno active input stream\n",prompt);
   return;
  }
if(!print_action_flag)
  {
   while(sp>0 && source_stack[sp].type==SOURCE_LIST)
     sp--;
   if(sp<0)
     {
      fprintf(chan,"%sno file input stream\n",prompt);
      return;
    }
  }

flag=1;
while(sp>=0)
  {
    if(flag==0) fprintf(chan,"%scalled by:\n",prompt);
    flag=0;
    source = &source_stack[sp--];
    switch(source->type)
      {
       case SOURCE_TT:
         strcpy(row,source->src.tt.row);
	 t = source->src.tt.old;while(*t==' ' || *t=='\t')t++;
	 errpos = t-source->src.tt.row;
	 break;
    
       case SOURCE_FILE:
	 strcpy(row,source->src.file.row);
	 for(i=0;row[i] && row[i]!='\n';i++);row[i]='\0';
	 t = source->src.file.old;while(*t==' ' || *t=='\t')t++;
	 errpos = t-source->src.file.row;
	 break;
    
       case SOURCE_LIST:
	 cur_list_row(source->src.list.list,row,&errpos);     
	 break;
    
       default:
	 row[0]='\0';
	 errpos= -1;
      }
    if(errpos>=0)
      {
       if(source->eof)
         fprintf(chan,"%sEND OF FILE\n",prompt);
       else
	 {
	  for(i=0;row[i];i++)
	    if(row[i]=='\t')
	      row[i]=' ';
	  fprintf(chan,"%s%s\n%s",prompt,row,prompt);
	  for(i=0;i<errpos;i++) fprintf(chan," ");
	  fprintf(chan,"^\n");
	 }
      }
    fprintf(chan,"%sline %d of %s\n",
	    prompt,source->line_n,source_name(source));
/*    if(source->type==SOURCE_TT || source->type==SOURCE_FILE) break; */
  }
}


/*--------------------------------------------------------------------*/

/*
set_cont_prompt()
{
  if(cur_source->type!=SOURCE_TT)
    return;

  cur_source->src.tt.prompt = ".. ";
}
*/

/*--------------------------------------------------------------------*/


int pretend_eof(void)
{
if(cur_source) cur_source->eof=1;
return 0;
}


/*--------------------------------------------------------------------*/

int read_once_only(char *id)
{
struct node {char *id;} *p; 
static TREE *tree=0;
if(!tree) tree=avl_tree_nodup_str(struct node,id);
p=avl_locate(tree,id);
if(!p)
  {
   p=(struct node*)malloc(sizeof(struct node));
   p->id = (char*)malloc(strlen(id)+1);
   strcpy(p->id,id);
   avl_insert(tree,p);
  }
else
  {
   if(cur_source) cur_source->eof=1;
  }
return 0;
}

/*---------------------------------------------------------------------*/

void zz_set_default_extension(ext)
     const char *ext;
{
  in_ext = ext;
}


/*---------------------------------------------------------------------*/


static char rcsid[] = "$Id: source.c,v 1.15 2002/05/09 17:23:49 kibun Exp $ ";