Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / mcomm.c
1 /* $Id: mcomm.c,v 3.2 1994/06/24 12:05:26 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*      mcomm.c -- change ".lcomm name" into ".comm name" where "name"
7         is specified in a list.
8 */
9 #include <stdio.h>
10
11 #define IDFSIZE 4096
12
13 char *readfile();
14
15 struct node {
16         char *name;
17         struct node *left, *right;
18 };
19
20 char *
21 Malloc(n)
22         unsigned n;
23 {
24         char *space;
25         char *malloc();
26
27         if ((space = malloc(n)) == 0) {
28                 fprintf(stderr, "out of memory\n");
29                 exit(1);
30         }
31         return space;
32 }
33
34 struct node *make_tree();
35
36 #define new_node() ((struct node *) Malloc(sizeof (struct node)))
37
38 main(argc, argv)
39         char *argv[];
40 {
41         char *nl_file, *as_file;
42         char *nl_text, *as_text;
43         struct node *nl_tree = 0;
44         int nl_siz, as_siz;
45
46         if (argc != 3) {
47                 fprintf(stderr, "use: %s namelist assembler_file\n", argv[0]);
48                 exit(1);
49         }
50         nl_file = argv[1];
51         as_file = argv[2];
52
53         if ((nl_text = readfile(nl_file, &nl_siz)) == 0) {
54                 fprintf(stderr, "%s: cannot read namelist %s\n",
55                         argv[0], nl_file);
56                 exit(1);
57         }
58
59         if ((as_text = readfile(as_file, &as_siz)) == 0) {
60                 fprintf(stderr, "%s: cannot read assembler file %s\n",
61                         argv[0], as_file);
62                 exit(1);
63         }
64
65         nl_tree = make_tree(nl_text);
66         edit(as_text, nl_tree);
67
68         if (writefile(as_file, as_text, as_siz) == 0) {
69                 fprintf(stderr, "%s: cannot write to %s\n", argv[0], as_file);
70                 exit(1);
71         }
72         return 0;
73 }
74
75 #include <sys/types.h>
76 #include <stat.h>
77
78 char *
79 readfile(filename, psiz)
80         char *filename;
81         int *psiz;
82 {
83         struct stat stbuf;      /* for `stat' to get filesize           */
84         register int fd;        /* filedescriptor for `filename'        */
85         register char *cbuf;    /* pointer to buffer to be returned     */
86
87         if (((fd = open(filename, 0)) < 0) || (fstat(fd, &stbuf) != 0))
88                 return 0;
89         cbuf = Malloc(stbuf.st_size + 1);
90         if (read(fd, cbuf, stbuf.st_size) != stbuf.st_size)
91                 return 0;
92         cbuf[stbuf.st_size] = '\0';
93         close(fd);              /* filedes no longer needed     */
94         *psiz = stbuf.st_size;
95         return cbuf;
96 }
97
98 int
99 writefile(filename, text, size)
100         char *filename, *text;
101 {
102         register fd;
103
104         if ((fd = open(filename, 1)) < 0)
105                 return 0;
106         if (write(fd, text, size) != size)
107                 return 0;
108         close(fd);
109         return 1;
110 }
111
112 struct node *
113 make_tree(nl)
114         char *nl;
115 {
116         char *id = nl;
117         struct node *tree = 0;
118
119         while (*nl) {
120                 if (*nl == '\n') {
121                         *nl = '\0';
122                         insert(&tree, id);
123                         id = ++nl;
124                 }
125                 else {
126                         ++nl;
127                 }
128         }
129         return tree;
130 }
131
132 insert(ptree, id)
133         struct node **ptree;
134         char *id;
135 {
136         register cmp;
137
138         if (*ptree == 0) {
139                 register struct node *nnode = new_node();
140
141                 nnode->name = id;
142                 nnode->left = nnode->right = 0;
143                 *ptree = nnode;
144         }
145         else
146         if ((cmp = strcmp((*ptree)->name, id)) < 0)
147                 insert(&((*ptree)->right), id);
148         else
149         if (cmp > 0)
150                 insert(&((*ptree)->left), id);
151 }
152
153 struct node *
154 find(tree, id)
155         struct node *tree;
156         char *id;
157 {
158         register cmp;
159
160         if (tree == 0)
161                 return 0;
162         if ((cmp = strcmp(tree->name, id)) < 0)
163                 return find(tree->right, id);
164         if (cmp > 0)
165                 return find(tree->left, id);
166         return tree;
167 }
168
169 edit(text, tree)
170         char *text;
171         struct node *tree;
172 {
173         register char *ptr = text;
174         char idbuf[IDFSIZE];
175         register char *id;
176         register char *save_ptr;
177
178         while (*ptr) {
179                 if (
180                         *ptr   == '.' &&
181                         *++ptr == 'l' &&
182                         *++ptr == 'c' &&
183                         *++ptr == 'o' &&
184                         *++ptr == 'm' &&
185                         *++ptr == 'm' &&
186                         (*++ptr == ' ' || *ptr == '\t')
187                 )
188                 {
189                         save_ptr = ptr - 6;
190                         while (*++ptr == ' ' || *ptr == '\t')
191                                 ;
192                         if (*ptr == '_')
193                                 ++ptr;
194                         if (InId(*ptr)) {
195                                 id = &idbuf[0];
196                                 *id++ = *ptr++;
197                                 while (InId(*ptr))
198                                         *id++ = *ptr++;
199                                 *id = '\0';
200                                 if (find(tree, idbuf) != 0) {
201                                         *save_ptr++ = ' ';
202                                         *save_ptr++ = '.';
203                                 }
204                         }
205                 }
206                 while (*ptr && *ptr++ != '\n')
207                         ;
208         }
209 }
210
211 InId(c)
212 {
213         switch (c) {
214
215         case 'a': case 'b': case 'c': case 'd': case 'e':
216         case 'f': case 'g': case 'h': case 'i': case 'j':
217         case 'k': case 'l': case 'm': case 'n': case 'o':
218         case 'p': case 'q': case 'r': case 's': case 't':
219         case 'u': case 'v': case 'w': case 'x': case 'y':
220         case 'z':
221         case 'A': case 'B': case 'C': case 'D': case 'E':
222         case 'F': case 'G': case 'H': case 'I': case 'J':
223         case 'K': case 'L': case 'M': case 'N': case 'O':
224         case 'P': case 'Q': case 'R': case 'S': case 'T':
225         case 'U': case 'V': case 'W': case 'X': case 'Y':
226         case 'Z':
227         case '_':
228         case '.':
229         case '0': case '1': case '2': case '3': case '4':
230         case '5': case '6': case '7': case '8': case '9':
231                 return 1;
232         
233         default:
234                 return 0;
235         }
236 }
237
238 puttree(nd)
239         struct node *nd;
240 {
241         if (nd) {
242                 puttree(nd->left);
243                 printf("%s\n", nd->name);
244                 puttree(nd->right);
245         }
246 }