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

Subversion Repositories openrisc_me

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

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

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

powered by: WebSVN 2.1.0

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