Pristine Ack-5.5
[Ack-5.5.git] / util / ceg / EM_parser / obj_EM_pars / dist.c
1 #include <stdio.h>
2 #include "em.h"
3
4 arith cur_pos = 0;
5
6 dist( lab)
7 char *lab;
8
9 /* Just output a reference which must be filled in on a second pass */
10
11 {
12         out( "($%s$ - %ld)", lab, cur_pos);
13 }
14
15
16 #define MAX_LABEL       10
17 #define TRUE            1
18 #define FALSE           0
19
20
21 struct t_label  {
22                         char *lab;
23                         arith position;
24                  };
25
26 struct t_label  label_list[ MAX_LABEL];
27 int n_labs = 0;
28
29 handle_label( lab)
30 char *lab;
31
32 /* Record position of this label */
33
34 {
35         char *Salloc();
36
37         if ( n_labs >= MAX_LABEL)
38                 error( "Too many labels!!\n");
39         else {
40                 label_list[ n_labs].lab = Salloc( lab, strlen( lab) + 1);
41                 label_list[ n_labs++].position = cur_pos;
42         }
43         process_label( lab);
44 }
45
46
47 relocate( f)
48 FILE *f;
49
50 /* Output position-count of the label in file 'f', remove also trailing $ */
51
52 {
53         char buf[256];
54         int i;
55
56         fscanf( f, " %[^$]$", buf);
57         for ( i = 0; i < n_labs; i++)
58                 if ( strcmp( buf, label_list[i].lab) == 0) {
59                         out( "%ld", label_list[i].position);
60                         return;
61                 }
62         error( "relocate() : can't find local label %s\n", buf);
63 }
64
65 #include <system.h>
66
67 File *oldout;
68 extern File *outfile;
69
70 back_patch()
71 /* Echo the text on file '.tmp', but replace every occurence of label-
72  *reference's by its position-count. Reference of label 'foo' is '$foo$'.
73  */
74 {
75         FILE *save;
76         char c, c1;
77
78         sys_close( outfile);
79         outfile = oldout;
80         save = fopen( ".tmp", "r");
81
82         while ( ( c = getc( save)) != EOF)
83                 if ( c == '$')
84                         relocate( save);
85                 else if ( c == '/') {           /* skip comments */
86                         c = getc( save);
87                         out( "/%c", c);
88                         if ( c == '*') {
89                                 c = getc( save);
90                                 do  {
91                                         out( "%c", c);
92                                         c1 = c;
93                                         c = getc( save);
94                                 }
95                                 while ( !( c1 == '*' && c == '/'));
96                                 out( "%c", c);
97                         }
98                 }
99                 else
100                         out( "%c", c);
101         fclose( save);
102 }
103
104
105 save_output()
106 {
107         extern arith cur_pos;
108         extern int n_labs;
109
110         oldout = outfile;
111         if ( ! sys_open( ".tmp", OP_WRITE, &outfile))
112                 fprint( STDERR, "can't open .tmp\n");
113         cur_pos = 0;
114         n_labs = 0;
115 }