OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [doc/] [makedoc.c] - Blame information for rev 452

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/* chew
2
   Copyright (C) 1990-1992 Free Software Foundation, Inc.
3
   Contributed by steve chamberlain @cygnus
4
 
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
 
20
/*
21
 Yet another way of extracting documentation from source.
22
 No, I haven't finished it yet, but I hope you people like it better
23
 that the old way
24
 
25
 sac
26
 
27
Basically, this is a sort of string forth, maybe we should call it
28
struth?
29
 
30
You define new words thus:
31
: <newword> <oldwords> ;
32
There is  no
33
 
34
*/
35
 
36
 
37
 
38
#include "ansidecl.h"
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <ctype.h>
42
#include <string.h>
43
 
44
#define DEF_SIZE 5000
45
#define STACK 50
46
#define MIN_CMDLEN      4       /* Minimum length of a command */
47
 
48
int internal_wanted;
49
int internal_mode;
50
int Verbose=0;
51
 
52
 
53
 
54
/* Here is a string type ... */
55
 
56
typedef struct buffer
57
{
58
    char *ptr;
59
    unsigned int write_idx;
60
    unsigned int size;
61
} string_type;
62
 
63
 
64
 
65
 
66
 
67
 
68
 
69
static void DEFUN(init_string_with_size,(buffer, size),
70
           string_type *buffer AND
71
           unsigned int size )
72
{
73
  buffer->write_idx = 0;
74
  buffer->size = size;
75
  buffer->ptr = malloc(size);
76
}
77
 
78
static void DEFUN(init_string,(buffer),
79
           string_type *buffer)
80
{
81
    init_string_with_size(buffer, DEF_SIZE);
82
 
83
}
84
 
85
static int DEFUN(find, (str, what),
86
          string_type *str AND
87
          char *what)
88
{
89
    unsigned int i;
90
    char *p;
91
    p = what;
92
    for (i = 0; i < str->write_idx && *p; i++)
93
    {
94
        if (*p == str->ptr[i])
95
         p++;
96
        else
97
         p = what;
98
    }
99
    return (*p == 0);
100
 
101
}
102
 
103
static void DEFUN(write_buffer,(buffer),
104
           string_type *buffer)
105
{
106
    fwrite(buffer->ptr, buffer->write_idx, 1, stdout);
107
}
108
 
109
 
110
static void DEFUN(delete_string,(buffer),
111
           string_type *buffer)
112
{
113
    free(buffer->ptr);
114
}
115
 
116
 
117
static char *DEFUN(addr, (buffer, idx),
118
            string_type *buffer AND
119
            unsigned int idx)
120
{
121
    return buffer->ptr + idx;
122
}
123
 
124
static char DEFUN(at,(buffer, pos),
125
           string_type *buffer AND
126
           unsigned int pos)
127
{
128
    if ( pos >= buffer->write_idx)
129
    {
130
        return 0;
131
    }
132
    return buffer->ptr[pos];
133
}
134
 
135
static void DEFUN(catchar,(buffer, ch),
136
           string_type *buffer AND
137
           char ch)
138
{
139
  if (buffer->write_idx == buffer->size)
140
  {
141
    buffer->size *=2;
142
    buffer->ptr = realloc(buffer->ptr, buffer->size);
143
  }
144
 
145
  buffer->ptr[buffer->write_idx ++ ] = ch;
146
}
147
 
148
 
149
static void DEFUN(overwrite_string,(dst,   src),
150
           string_type *dst AND
151
           string_type *src)
152
{
153
    free(dst->ptr);
154
    dst->size = src->size;
155
    dst->write_idx = src->write_idx;
156
    dst->ptr = src->ptr;
157
}
158
 
159
static void DEFUN(catstr,(dst, src),
160
           string_type *dst AND
161
           string_type *src)
162
{
163
    unsigned int i;
164
    for (i = 0; i < src->write_idx; i++)
165
    {
166
        catchar(dst, src->ptr[i]);
167
    }
168
}
169
 
170
 
171
static void DEFUN(cattext,(buffer, string),
172
           string_type *buffer AND
173
           char *string)
174
{
175
 
176
    while (*string)
177
    {
178
        catchar(buffer, *string);
179
        string++;
180
    }
181
}
182
 
183
static void DEFUN(catbuf,(buffer, buf, len),
184
           string_type *buffer AND
185
           char *buf AND
186
           unsigned int len)
187
{
188
 
189
    while (len--)
190
    {
191
        catchar(buffer, *buf);
192
        buf++;
193
    }
194
}
195
 
196
 
197
 
198
static unsigned int
199
DEFUN(skip_white_and_stars,(src, idx),
200
      string_type *src AND
201
      unsigned int idx)
202
{
203
    while (isspace(at(src,idx))
204
           || (at(src,idx) == '*' && at(src,idx +1) !='/'))
205
     idx++;
206
    return idx;
207
 
208
 
209
}
210
/***********************************************************************/
211
 
212
 
213
string_type stack[STACK];
214
string_type *tos;
215
 
216
unsigned int idx = 0; /* Pos in input buffer */
217
string_type *ptr; /* and the buffer */
218
typedef void (*stinst_type)(NOARGS);
219
stinst_type *pc;
220
stinst_type sstack[STACK];
221
stinst_type *ssp = &sstack[0];
222
int istack[STACK];
223
int *isp = &istack[0];
224
 
225
typedef int *word_type;
226
 
227
 
228
 
229
struct dict_struct
230
{
231
    char *word;
232
    struct dict_struct *next;
233
   stinst_type *code;
234
    int code_length;
235
    int code_end;
236
    int var;
237
 
238
};
239
typedef struct dict_struct dict_type;
240
#define WORD(x) static void x(NOARGS)
241
 
242
static void DEFUN(exec,(word),
243
                  dict_type *word)
244
{
245
    pc = word->code;
246
    while (*pc)
247
    {
248
        (*pc)();
249
    }
250
 
251
}
252
WORD(call)
253
{
254
stinst_type *oldpc = pc;
255
    dict_type *e;
256
    e =  (dict_type *)(pc [1]);
257
    exec(e);
258
    pc = oldpc + 2;
259
 
260
}
261
 
262
WORD(remchar)
263
{
264
    tos->write_idx--;
265
    pc++;
266
 
267
}
268
 
269
WORD(push_number)
270
{
271
    isp++;
272
    pc++;
273
    *isp = (int)(*pc);
274
    pc++;
275
 
276
}
277
 
278
 
279
 
280
 
281
WORD(push_text)
282
{
283
 
284
    tos++;
285
    init_string(tos);
286
    pc++;
287
    cattext(tos,*((char **)pc));
288
    pc++;
289
 
290
}
291
 
292
 
293
 
294
/* This function removes everything not inside comments starting on
295
   the first char of the line from the  string, also when copying
296
   comments, removes blank space and leading *'s
297
   Blank lines are turned into one blank line
298
 */
299
 
300
static void
301
DEFUN(remove_noncomments,(src,dst),
302
           string_type *src AND
303
           string_type *dst)
304
{
305
    unsigned int idx = 0;
306
 
307
    while (at(src,idx))
308
    {
309
        /* Now see if we have a comment at the start of the line */
310
        if (at(src,idx) == '\n'
311
            && at(src,idx+1) ==  '/'
312
            && at(src,idx+2) == '*')
313
        {
314
            idx+=3;
315
 
316
            idx = skip_white_and_stars(src,idx);
317
 
318
            /* Remove leading dot */
319
            if (at(src, idx) == '.')
320
             idx++;
321
 
322
            /* Copy to the end of the line, or till the end of the
323
               comment */
324
            while (at(src, idx))
325
            {
326
                if (at(src, idx) == '\n')
327
                {
328
                    /* end of line, echo and scrape of leading blanks  */
329
                    if (at(src,idx +1) == '\n')
330
                     catchar(dst,'\n');
331
                    catchar(dst,'\n');
332
                    idx++;
333
                    idx =   skip_white_and_stars(src, idx);
334
                }
335
                else if (at(src, idx) == '*' && at(src,idx+1) == '/')
336
                {
337
                    idx +=2 ;
338
                    cattext(dst,"\nENDDD\n");
339
                    break;
340
                }
341
                else
342
                {
343
                    catchar(dst, at(src, idx));
344
                    idx++;
345
                }
346
            }
347
        }
348
        else idx++;
349
    }
350
}
351
/* turn foobar name(stuff); into foobar EXFUN(name,(stuff));
352
 
353
 */
354
 
355
static void
356
DEFUN_VOID(exfunstuff)
357
{
358
    unsigned int openp;
359
    unsigned int fname;
360
    unsigned int idx;
361
    string_type out;
362
    init_string(&out);
363
 
364
 
365
    /* make sure that it's not already exfuned */
366
    if(find(tos,"EXFUN") || find(tos,"PROTO") || !find(tos,"(")) {
367
            catstr(&out,tos);
368
        }
369
    else
370
    {
371
 
372
        /*Find the open paren*/
373
        for (openp = 0; at(tos, openp) != '('  && at(tos,openp); openp++)
374
         ;
375
 
376
        fname = openp;
377
        /* Step back to the fname */
378
        fname--;
379
        while (fname && isspace(at(tos, fname)))
380
         fname --;
381
        while (fname && !isspace(at(tos,fname)) && at(tos,fname) != '*')
382
         fname--;
383
 
384
        fname++;
385
 
386
        for (idx = 0; idx < fname; idx++)
387
        {
388
            catchar(&out, at(tos,idx));
389
        }
390
 
391
        cattext(&out,"EXFUN(");
392
        for (idx = fname; idx < openp; idx++)
393
        {
394
            catchar(&out, at(tos,idx));
395
        }
396
        cattext(&out,", ");
397
        while (at(tos,idx) && at(tos,idx) !=';')
398
        {
399
            catchar(&out, at(tos, idx));
400
            idx++;
401
        }
402
        cattext(&out,");\n");
403
    }
404
    overwrite_string(tos, &out);
405
    pc++;
406
 
407
}
408
 
409
 
410
 
411
/* turn {*
412
   and *} into comments */
413
 
414
WORD(translatecomments)
415
{
416
    unsigned int idx = 0;
417
    string_type out;
418
    init_string(&out);
419
 
420
    while (at(tos, idx))
421
    {
422
        if (at(tos,idx) == '{' && at(tos,idx+1) =='*')
423
        {
424
            cattext(&out,"      /*");
425
            idx+=2;
426
        }
427
        else if (at(tos,idx) == '*' && at(tos,idx+1) =='}')
428
        {
429
            cattext(&out,"*/");
430
            idx+=2;
431
        }
432
        else
433
        {
434
            catchar(&out, at(tos, idx));
435
            idx++;
436
        }
437
    }
438
 
439
 
440
    overwrite_string(tos, &out);
441
 
442
    pc++;
443
 
444
}
445
 
446
/* find something like
447
   QUICKREF
448
     memchar ansi  pure
449
 
450
     into
451
     merge with words on tos and output them to stderror
452
 
453
*/
454
WORD(quickref)
455
{
456
  string_type *nos = tos-1;
457
  unsigned int nosscan = 0;
458
  unsigned int idx = 0;
459
 
460
  while (at(tos, idx))
461
  {
462
    if (at(tos,idx) == '~')
463
    {
464
      /* Skip the whitespace */
465
      while (at(nos, nosscan) == ' ')
466
       nosscan++;
467
 
468
      /* Sub the next word from the nos*/
469
      while (at(nos, nosscan) != ' ' &&
470
             at(nos, nosscan) != 0)
471
      {
472
        fprintf(stderr, "%c", at(nos, nosscan));
473
        nosscan++;
474
      }
475
    }
476
 
477
    else
478
    {
479
      fprintf(stderr,"%c", at(tos, idx));
480
 
481
    }
482
    idx++;
483
  }
484
 
485
  delete_string(tos);
486
  delete_string(nos);
487
  tos-=2;
488
  pc++;
489
 
490
}
491
 
492
#if 0
493
/* turn everything not starting with a . into a comment */
494
 
495
WORD(manglecomments)
496
{
497
    unsigned int idx = 0;
498
    string_type out;
499
    init_string(&out);
500
 
501
    while (at(tos, idx))
502
    {
503
        if (at(tos,idx) == '\n' && at(tos,idx+1) =='*')
504
        {
505
            cattext(&out,"      /*");
506
            idx+=2;
507
        }
508
        else if (at(tos,idx) == '*' && at(tos,idx+1) =='}')
509
        {
510
            cattext(&out,"*/");
511
            idx+=2;
512
        }
513
        else
514
        {
515
            catchar(&out, at(tos, idx));
516
            idx++;
517
        }
518
    }
519
 
520
 
521
    overwrite_string(tos, &out);
522
 
523
    pc++;
524
 
525
}
526
#endif
527
 
528
/* Mod tos so that only lines with leading dots remain */
529
static void
530
DEFUN_VOID(outputdots)
531
{
532
    unsigned int idx = 0;
533
    string_type out;
534
    init_string(&out);
535
 
536
    while (at(tos, idx))
537
    {
538
        if (at(tos, idx) == '\n' && at(tos, idx+1) == '.')
539
        {
540
            idx += 2;
541
 
542
            while (at(tos, idx) && at(tos, idx)!='\n')
543
            {
544
                if (at(tos,idx) == '{' && at(tos,idx+1) =='*')
545
                {
546
                    cattext(&out," /*");
547
                    idx+=2;
548
                }
549
                else if (at(tos,idx) == '*' && at(tos,idx+1) =='}')
550
                {
551
                    cattext(&out,"*/");
552
                    idx+=2;
553
                }
554
                else
555
                {
556
                    catchar(&out, at(tos, idx));
557
                    idx++;
558
                }
559
            }
560
            catchar(&out,'\n');
561
        }
562
        else
563
        {
564
            idx++;
565
        }
566
    }
567
 
568
    overwrite_string(tos, &out);
569
    pc++;
570
 
571
}
572
 
573
/* Find lines starting with . and | and put example around them on tos
574
   turn
575
   {*  into open comment and *} into close comment
576
   escape curlies
577
 
578
*/
579
WORD(courierize)
580
{
581
    string_type out;
582
    unsigned int idx = 0;
583
 
584
    init_string(&out);
585
 
586
    while (at(tos, idx))
587
    {
588
        if (at(tos, idx) == '\n'
589
            && (at(tos, idx +1 ) == '.'
590
                || at(tos,idx+1) == '|'))
591
        {
592
            cattext(&out,"\n@smallexample\n");
593
            do
594
            {
595
                idx += 2;
596
 
597
                while (at(tos, idx) && at(tos, idx)!='\n')
598
                {
599
                    if (at(tos,idx)=='{' && at(tos,idx+1) =='*')
600
                    {
601
                        cattext(&out," /*");
602
                        idx+=2;
603
                    }
604
                    else if (at(tos,idx)=='*' && at(tos,idx+1) =='}')
605
                    {
606
                        cattext(&out,"*/");
607
                        idx+=2;
608
                    }
609
                    else if (at(tos,idx) == '{')
610
                    {
611
                        cattext(&out,"@{");
612
                        idx++;
613
                    }
614
                    else if (at(tos,idx) == '}')
615
                    {
616
                        cattext(&out,"@}");
617
                        idx++;
618
                    }
619
                    else
620
                    {
621
                        catchar(&out, at(tos, idx));
622
                        idx++;
623
                    }
624
 
625
                }
626
                catchar(&out,'\n');
627
            }
628
            while (at(tos, idx) == '\n'
629
                   && (at(tos, idx+1) == '.')
630
                   || (at(tos,idx+1) == '|'));
631
            cattext(&out,"@end smallexample");
632
        }
633
        else
634
        {
635
            catchar(&out, at(tos, idx));
636
            idx++;
637
        }
638
    }
639
 
640
    overwrite_string(tos, &out);
641
    pc++;
642
 
643
 
644
}
645
 
646
/*
647
bulletize:  look for bullet item commands at start of line
648
 Bullet list:
649
   O+  emit @itemize @bullet
650
   o   emit @item       [note lowercase]
651
   O-  emit @end itemize
652
 
653
 Variable label list:
654
   o+  emit @table @code
655
   o   emit @item
656
   o-  emit @end table
657
*/
658
 
659
 
660
WORD(bulletize)
661
{
662
  unsigned int idx = 0;
663
  string_type out;
664
  init_string(&out);
665
 
666
  while (at(tos, idx)) {
667
       if (at(tos, idx) == '\n' &&  at(tos, idx+1) == 'o')
668
       {
669
         if (at(tos,idx+2) == '+') {
670
             cattext(&out,"\n@table @code\n");
671
             idx+=3;
672
           }
673
         else if (at(tos,idx+2) == '-') {
674
             cattext(&out,"\n@end table\n");
675
             idx+=3;
676
           }
677
         else if (isspace(at(tos,idx+2))) {
678
             cattext(&out,"\n@item ");
679
             idx+=3;
680
           }
681
         else {
682
             catchar(&out, at(tos, idx));
683
             idx++;
684
           }
685
       }
686
 
687
       else
688
        if (at(tos, idx) == '\n' &&  at(tos, idx+1) == 'O')
689
        {
690
          if (at(tos,idx+2) == '+') {
691
              cattext(&out,"\n@itemize @bullet\n");
692
              idx+=3;
693
            }
694
 
695
          else if (at(tos,idx+2) == '-') {
696
              cattext(&out,"\n@end itemize\n");
697
              idx+=3;
698
            }
699
          else {
700
              catchar(&out, at(tos, idx));
701
              idx++;
702
            }
703
        }
704
        else
705
        {
706
          catchar(&out, at(tos, idx));
707
          idx++;
708
        }
709
    }
710
 
711
  delete_string(tos);
712
  *tos = out;
713
  pc++;
714
 
715
}
716
 
717
/* Turn <<foo>> into @code{foo} in place at TOS
718
   Turn <[foo]> into @var{foo} in place at TOS
719
   nest them too !
720
 
721
*/
722
 
723
 
724
WORD(do_fancy_stuff)
725
 {
726
    unsigned int idx = 0;
727
    string_type out;
728
    init_string(&out);
729
    while (at(tos, idx))
730
    {
731
        if (at(tos, idx) == '<'
732
            && at(tos, idx+1) == '<'
733
            && (!isspace(at(tos,idx + 2)) || at(tos,idx+3) == '>'))
734
        {
735
            /* This qualifies as a << startup */
736
            idx +=2;
737
            cattext(&out,"@code{");
738
          }
739
 
740
        else    if (at(tos, idx) == '<'
741
            && at(tos, idx+1) == '['
742
            && !isspace(at(tos,idx + 2)))
743
        {
744
            /* This qualifies as a <[ startup */
745
            idx +=2;
746
            cattext(&out,"@var{");
747
          }
748
        else if (at(tos, idx) == '>'
749
                 && at(tos,idx+1) =='>')
750
        {
751
 
752
            cattext(&out,"}");
753
            idx+=2;
754
        }
755
        else if (at(tos, idx) == ']'
756
                 && at(tos,idx+1) =='>')
757
        {
758
            cattext(&out,"}");
759
            idx+=2;
760
        }
761
        else
762
        {
763
            catchar(&out, at(tos, idx));
764
            idx++;
765
        }
766
    }
767
    delete_string(tos);
768
    *tos = out;
769
    pc++;
770
 
771
}
772
/* A command is all upper case,and alone on a line */
773
static int
774
DEFUN( iscommand,(ptr, idx),
775
      string_type *ptr AND
776
      unsigned int idx)
777
{
778
    unsigned int len = 0;
779
    while (at(ptr,idx)) {
780
            if (isupper(at(ptr,idx)) || at(ptr,idx) == ' ' ||
781
                at(ptr,idx) == '_')
782
            {
783
             len++;
784
             idx++;
785
         }
786
            else if(at(ptr,idx) == '\n')
787
            {
788
                /* The length check will never fail on a real command
789
                 * because the commands are screened as the definitions file
790
                 * is read.  */
791
                if (len >= MIN_CMDLEN) return 1;
792
                return 0;
793
            }
794
            else return 0;
795
        }
796
    return 0;
797
 
798
}
799
 
800
 
801
unsigned int
802
DEFUN(copy_past_newline,(ptr, idx, dst),
803
      string_type *ptr AND
804
      unsigned int idx AND
805
      string_type *dst)
806
{
807
    while (at(ptr, idx) && at(ptr, idx) != '\n')
808
    {
809
        catchar(dst, at(ptr, idx));
810
        idx++;
811
 
812
    }
813
    catchar(dst, at(ptr, idx));
814
    idx++;
815
    return idx;
816
 
817
}
818
 
819
WORD(icopy_past_newline)
820
{
821
    tos++;
822
    init_string(tos);
823
    idx = copy_past_newline(ptr, idx, tos);
824
    pc++;
825
}
826
 
827
 
828
/* indent
829
   Take the string at the top of the stack, do some prettying */
830
 
831
 
832
 
833
 
834
WORD(kill_bogus_lines)
835
{
836
    int sl ;
837
 
838
    int idx = 0;
839
    int c;
840
    int dot = 0    ;
841
 
842
    string_type out;
843
    init_string(&out);
844
    /* Drop leading nl */
845
    while (at(tos,idx) == '\n')
846
    {
847
        idx++;
848
    }
849
    c = idx;
850
 
851
    /* Find the last char */
852
    while (at(tos,idx))
853
    {
854
        idx++;
855
    }
856
 
857
    /* find the last non white before the nl */
858
    idx--;
859
 
860
    while (idx && isspace(at(tos,idx)))
861
     idx--;
862
    idx++;
863
 
864
    /* Copy buffer upto last char, but blank lines before and after
865
       dots don't count */
866
    sl = 1;
867
 
868
    while (c < idx)
869
    {
870
        if (at(tos,c) == '\n'
871
            && at(tos,c+1) == '\n'
872
            && at(tos,c+2) == '.')
873
        {
874
            /* Ignore two linelines before  a dot*/
875
            c++;
876
        }
877
        else if (at(tos,c) == '.' && sl)
878
        {
879
            /* remember that this line started with a dot */
880
            dot=2;
881
        }
882
        else if (at(tos,c) == '\n'
883
                 && at(tos,c+1) == '\n'
884
                 && dot)
885
        {
886
            c++;
887
            /* Ignore two newlines when last line was dot */
888
        }
889
 
890
        catchar(&out, at(tos,c));
891
        if (at(tos,c) == '\n')
892
        {
893
            sl = 1;
894
 
895
            if (dot == 2)dot=1;else dot = 0;
896
        }
897
 
898
        c++;
899
 
900
    }
901
 
902
    /* Append nl*/
903
    catchar(&out, '\n');
904
    pc++;
905
    delete_string(tos);
906
    *tos = out;
907
 
908
 
909
}
910
 
911
WORD(indent)
912
{
913
    string_type out;
914
    int tab = 0;
915
    int idx = 0;
916
    int ol =0;
917
    init_string(&out);
918
    while (at(tos,idx)) {
919
            switch (at(tos,idx))
920
            {
921
              case '\n':
922
                cattext(&out,"\n");
923
                idx++;
924
                if (tab)
925
                {
926
                    cattext(&out,"    ");
927
                }
928
                ol = 0;
929
                break;
930
              case '(':
931
                tab++;
932
                if (ol == 0)
933
                    cattext(&out,"   ");
934
                idx++;
935
                cattext(&out,"(");
936
                ol = 1;
937
                break;
938
              case ')':
939
                tab--;
940
                cattext(&out,")");
941
                idx++;
942
                ol=1;
943
 
944
                break;
945
              default:
946
                catchar(&out,at(tos,idx));
947
                ol=1;
948
 
949
                idx++;
950
                break;
951
            }
952
        }
953
 
954
    pc++;
955
    delete_string(tos);
956
    *tos = out;
957
 
958
}
959
 
960
/* Change the TOS so that all that is left is the stuff inside the
961
 first <<foo>> .
962
*/
963
 
964
WORD(get_stuff_in_angle)
965
{
966
  unsigned int idx = 0;
967
  string_type out;
968
  init_string(&out);
969
 
970
  while (at(tos, idx))
971
    {
972
      if (at(tos,idx) == '<' && at(tos,idx+1) =='<')
973
        {
974
          idx+=2;
975
 
976
          while (!(at(tos,idx) == '>' && at(tos,idx+1) == '>'))
977
            {
978
              catchar(&out, at(tos, idx));
979
              idx++;
980
            }
981
          break;
982
        }
983
      idx++;
984
    }
985
  catchar(&out,'\n');
986
 
987
  overwrite_string(tos, &out);
988
  pc++;
989
}
990
 
991
 
992
WORD(get_stuff_in_command)
993
{
994
  tos++;
995
  init_string(tos);
996
 
997
  while (at(ptr, idx)) {
998
    if (iscommand(ptr, idx))  break;
999
    idx =   copy_past_newline(ptr, idx, tos);
1000
  }
1001
  pc++;
1002
}
1003
 
1004
WORD(swap)
1005
{
1006
    string_type t;
1007
 
1008
    t = tos[0];
1009
    tos[0] = tos[-1];
1010
    tos[-1] =t;
1011
    pc++;
1012
 
1013
}
1014
 
1015
WORD(dup)
1016
{
1017
    tos++;
1018
    init_string(tos);
1019
    catstr(tos, tos-1);
1020
    pc++;
1021
 
1022
}
1023
 
1024
 
1025
 
1026
WORD(icatstr)
1027
{
1028
    catstr(tos-1, tos);
1029
    delete_string(tos);
1030
    tos--;
1031
    pc++;
1032
 
1033
}
1034
 
1035
WORD(skip_past_newline)
1036
{
1037
    while (at(ptr,idx)
1038
           && at(ptr,idx) != '\n')
1039
     idx++;
1040
    idx++;
1041
    pc++;
1042
}
1043
 
1044
 
1045
WORD(internalmode)
1046
{
1047
    internal_mode = *(isp);
1048
    isp--;
1049
    pc++;
1050
}
1051
 
1052
WORD(maybecatstr)
1053
{
1054
    if (internal_wanted == internal_mode)
1055
    {
1056
        catstr(tos-1, tos);
1057
    }
1058
    delete_string(tos);
1059
    tos--;
1060
    pc++;
1061
 
1062
}
1063
 
1064
char *
1065
DEFUN(nextword,(string, word),
1066
      char *string AND
1067
      char **word)
1068
{
1069
  char *word_start;
1070
  int idx;
1071
  char *dst;
1072
  char *src;
1073
 
1074
  int length = 0;
1075
 
1076
  while (isspace(*string) || *string == '-') {
1077
      if (*string == '-')
1078
      {
1079
        while (*string && *string != '\n')
1080
         string++;
1081
 
1082
      }
1083
      else {
1084
          string++;
1085
        }
1086
    }
1087
  if (!*string) return 0;
1088
 
1089
  word_start = string;
1090
  if (*string == '"')
1091
  {
1092
    string++;
1093
    length++;
1094
 
1095
    while (*string != '"')
1096
    {
1097
      string++;
1098
      length++;
1099
    }
1100
  }
1101
  else
1102
  {
1103
 
1104
 
1105
    while (!isspace(*string))
1106
    {
1107
      string++;
1108
      length++;
1109
    }
1110
  }
1111
 
1112
  *word = malloc(length + 1);
1113
 
1114
  dst = *word;
1115
  src = word_start;
1116
 
1117
 
1118
  for (idx= 0; idx < length; idx++)
1119
  {
1120
 
1121
    if (src[idx] == '\\' && src[idx+1] == 'n')
1122
    {
1123
      *dst++ = '\n';
1124
      idx++;
1125
 
1126
    }
1127
    else *dst++ = src[idx];
1128
  }
1129
  *dst++ = 0;
1130
 
1131
 
1132
 
1133
 
1134
 
1135
  if(*string)
1136
   return string + 1;
1137
  else
1138
   return 0;
1139
 
1140
}
1141
dict_type *root;
1142
dict_type *
1143
DEFUN(lookup_word,(word),
1144
      char *word)
1145
{
1146
    dict_type *ptr = root;
1147
    while (ptr) {
1148
            if (strcmp(ptr->word, word) == 0) return ptr;
1149
            ptr = ptr->next;
1150
 
1151
         }
1152
    fprintf(stderr,"Can't find %s\n",word);
1153
    return 0;
1154
 
1155
 
1156
}
1157
 
1158
static void DEFUN_VOID(perform)
1159
{
1160
    tos = stack;
1161
 
1162
    while (at(ptr, idx)) {
1163
            /* It's worth looking through the command list */
1164
            if (iscommand(ptr, idx))
1165
            {
1166
                char *next;
1167
                dict_type *word ;
1168
 
1169
                (void)          nextword(addr(ptr, idx), &next);
1170
 
1171
 
1172
                word = lookup_word(next);
1173
 
1174
 
1175
 
1176
 
1177
                if (word)
1178
                {
1179
                    if(Verbose)  fprintf(stderr, "CMD '%s'\n", word->word);
1180
                    exec(word);
1181
                }
1182
                else
1183
                {
1184
                    fprintf(stderr,"warning, %s is not recognised\n",  next);
1185
                    skip_past_newline();
1186
                }
1187
 
1188
            }
1189
            else skip_past_newline();
1190
 
1191
        }
1192
}
1193
 
1194
dict_type *
1195
DEFUN(newentry,(word),
1196
      char *word)
1197
{
1198
    dict_type *new = (dict_type *)malloc(sizeof(dict_type));
1199
    new->word = word;
1200
    new->next = root;
1201
    root = new;
1202
    new->code = (stinst_type *)malloc(sizeof(stinst_type ));
1203
    new->code_length = 1;
1204
    new->code_end = 0;
1205
    return new;
1206
 
1207
}
1208
 
1209
 
1210
unsigned int
1211
DEFUN(add_to_definition,(entry, word),
1212
      dict_type *entry AND
1213
      stinst_type word)
1214
{
1215
    if (entry->code_end == entry->code_length)
1216
    {
1217
        entry->code_length += 2;
1218
        entry->code =
1219
         (stinst_type *) realloc((char *)(entry->code),
1220
                               entry->code_length *sizeof(word_type));
1221
    }
1222
    entry->code[entry->code_end] = word;
1223
 
1224
return     entry->code_end++;
1225
}
1226
 
1227
 
1228
 
1229
 
1230
 
1231
 
1232
 
1233
void
1234
DEFUN(add_intrinsic,(name, func),
1235
      char *name AND
1236
      void (*func)(NOARGS))
1237
{
1238
    dict_type *new = newentry(name);
1239
    add_to_definition(new, func);
1240
    add_to_definition(new, 0);
1241
}
1242
 
1243
void
1244
DEFUN(add_var,(name),
1245
      char *name)
1246
{
1247
    dict_type *new = newentry(name);
1248
    add_to_definition(new, push_number);
1249
    add_to_definition(new, (stinst_type)(&(new->var)));
1250
    add_to_definition(new,0);
1251
 
1252
}
1253
 
1254
 
1255
 
1256
 
1257
int
1258
DEFUN(compile, (string),
1259
      char *string)
1260
 
1261
{
1262
    int  ret=0;
1263
    /* add words to the dictionary */
1264
    char *word;
1265
    string = nextword(string, &word);
1266
    while (string && *string && word[0])
1267
    {
1268
        if (strcmp(word,"var")==0)
1269
        {
1270
          string=nextword(string, &word);
1271
 
1272
          add_var(word);
1273
          string=nextword(string, &word);
1274
        }
1275
        else
1276
 
1277
        if (word[0] == ':')
1278
        {
1279
            dict_type *ptr;
1280
            /* Compile a word and add to dictionary */
1281
            string = nextword(string, &word);
1282
            if(Verbose)  fprintf(stderr, "Found command '%s'\n", word);
1283
            if(strlen(word) < MIN_CMDLEN)  {
1284
                fprintf(stderr, "ERROR:  Command '%s' is too short ", word);
1285
                fprintf(stderr, "(MIN_CMDLEN is %d)\n", MIN_CMDLEN);
1286
                ret++;
1287
            }
1288
 
1289
            ptr = newentry(word);
1290
            string = nextword(string, &word);
1291
            while (word[0] != ';' )
1292
            {
1293
                 switch (word[0])
1294
                 {
1295
 
1296
 
1297
                   case '"':
1298
                     /* got a string, embed magic push string
1299
                        function */
1300
                     add_to_definition(ptr, push_text);
1301
                     add_to_definition(ptr, (stinst_type)(word+1));
1302
                     break;
1303
                   case '0':
1304
                   case '1':
1305
                   case '2':
1306
                   case '3':
1307
                   case '4':
1308
                   case '5':
1309
                   case '6':
1310
                   case '7':
1311
                   case '8':
1312
                   case '9':
1313
                     /* Got a number, embedd the magic push number
1314
                        function */
1315
                     add_to_definition(ptr, push_number);
1316
                     add_to_definition(ptr, atol(word));
1317
                     break;
1318
                   default:
1319
                     add_to_definition(ptr, call);
1320
                     add_to_definition(ptr, lookup_word(word));
1321
                 }
1322
 
1323
                string = nextword(string, &word);
1324
            }
1325
            add_to_definition(ptr,0);
1326
            string = nextword(string, &word);
1327
        }
1328
        else
1329
        {
1330
            fprintf(stderr,"syntax error at %s\n",string-1);
1331
            ret++;
1332
        }
1333
    }
1334
 
1335
return(ret);
1336
}
1337
 
1338
 
1339
static void DEFUN_VOID(bang)
1340
{
1341
*(int *)((isp[0])) = isp[-1];
1342
isp-=2;
1343
pc++;
1344
 
1345
}
1346
 
1347
WORD(atsign)
1348
{
1349
    isp[0] = *(int *)(isp[0]);
1350
    pc++;
1351
}
1352
 
1353
WORD(hello)
1354
{
1355
 
1356
    printf("hello\n");
1357
    pc++;
1358
}
1359
 
1360
 
1361
 
1362
static void DEFUN(read_in, (str, file),
1363
           string_type *str AND
1364
                  FILE *file)
1365
{
1366
    char buff[10000];
1367
    unsigned int r;
1368
    do
1369
    {
1370
        r = fread(buff, 1, sizeof(buff), file);
1371
        catbuf(str, buff, r);
1372
    }
1373
    while (r);
1374
    buff[0] = 0;
1375
 
1376
    catbuf(str, buff,1);
1377
 
1378
}
1379
 
1380
 
1381
#if 0
1382
static void DEFUN_VOID(usage)
1383
{
1384
    fprintf(stderr,"usage: -[i|v] -f macrofile <file >file\n");
1385
    exit(33);
1386
}
1387
#endif
1388
 
1389
int DEFUN(main,(ac,av),
1390
int ac AND
1391
char *av[])
1392
{
1393
    unsigned int i;
1394
 
1395
 
1396
    string_type buffer;
1397
    string_type pptr;
1398
 
1399
 
1400
    init_string(&buffer);
1401
    init_string(&pptr);
1402
    init_string(stack+0);
1403
    tos=stack+1;
1404
    ptr = &pptr;
1405
 
1406
    add_intrinsic("push_text", push_text);
1407
    add_intrinsic("!", bang);
1408
    add_intrinsic("@", atsign);
1409
    add_intrinsic("hello",hello);
1410
    add_intrinsic("skip_past_newline", skip_past_newline );
1411
    add_intrinsic("catstr", icatstr );
1412
    add_intrinsic("copy_past_newline", icopy_past_newline );
1413
    add_intrinsic("dup", dup );
1414
    add_intrinsic("remchar", remchar );
1415
    add_intrinsic("get_stuff_in_command", get_stuff_in_command );
1416
    add_intrinsic("get_stuff_in_angle", get_stuff_in_angle );
1417
    add_intrinsic("do_fancy_stuff", do_fancy_stuff );
1418
    add_intrinsic("bulletize", bulletize );
1419
    add_intrinsic("courierize", courierize );
1420
    add_intrinsic("swap", swap );
1421
    add_intrinsic("outputdots", outputdots );
1422
    add_intrinsic("exfunstuff", exfunstuff );
1423
    add_intrinsic("maybecatstr", maybecatstr );
1424
    add_intrinsic("translatecomments", translatecomments );
1425
    add_intrinsic("kill_bogus_lines", kill_bogus_lines);
1426
    add_intrinsic("indent", indent);
1427
    add_intrinsic("quickref", quickref);
1428
    add_intrinsic("internalmode", internalmode);
1429
 
1430
    /* Put a nl at the start */
1431
    catchar(&buffer,'\n');
1432
 
1433
    read_in(&buffer, stdin);
1434
    remove_noncomments(&buffer, ptr);
1435
    for (i= 1; i < ac; i++)
1436
    {
1437
        if (av[i][0] == '-')
1438
        {
1439
            if (av[i][1] == 'f')
1440
            {
1441
                string_type b;
1442
                FILE *f;
1443
                init_string(&b);
1444
 
1445
                f  = fopen(av[i+1],"r");
1446
                if (!f)
1447
                {
1448
                  fprintf(stderr,"Can't open the input file %s\n",av[i+1]);
1449
                  return 33;
1450
                }
1451
                if(Verbose)  fprintf(stderr, "Reading -f '%s'\n", av[i+1]);
1452
 
1453
 
1454
                read_in(&b, f);
1455
                if( compile(b.ptr) )  { fclose(f); exit(1); }
1456
                perform();
1457
                fclose(f);
1458
            }
1459
            else    if (av[i][1] == 'i')
1460
            {
1461
                internal_wanted = 1;
1462
            }
1463
            else    if (av[i][1] == 'v')
1464
            {
1465
                Verbose++;
1466
            }
1467
        }
1468
 
1469
    }
1470
    write_buffer(stack+0);
1471
    return 0;
1472
}
1473
 
1474
 
1475
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.