-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre.c
270 lines (226 loc) · 7.38 KB
/
pre.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXLINE 81
#define MAX_LINE_LEN 81
#define HASHSIZE 1
/* hashtable's currently functioning as a linked list. If machine's capable to
keep more arrays - besides the machine image, it's possible to improve the
program preformance by increasing the defined hash size. */
#define MAXLABEL 31
extern char *strdup(const char*);
typedef struct Macro_slot { /* table entry */
struct Macro_slot *next; /* next enrty in chain */
char *name; /* defined macro's name */
char *lines; /* replacement text */
} Macro_slot;
static Macro_slot *m_hashtab[HASHSIZE];
/* a pointer table to store the macros' as a linked list */
struct Macro_slot * macro_alloc(void)
{
Macro_slot *ptr = NULL;
if ((ptr = (Macro_slot *)malloc(sizeof(Macro_slot))) == NULL) {
printf("error: memory allocation\n");
exit(1);
}
ptr->name = NULL;
ptr->lines = NULL;
ptr->next = NULL;
return ptr;
}
/* free_macros: a function to free the memmory allocated to macro declarations*/
void free_macros(void)
{
Macro_slot *ptr = NULL;
int i;
for (i = 0; i < HASHSIZE; i++) {
while (m_hashtab[i]) {
ptr = m_hashtab[i]->next;
free(m_hashtab[i]->lines);
free(m_hashtab[i]->name);
free(m_hashtab[i]);
m_hashtab[i] = ptr;
}
}
}
/* macro_hash: form macro_hash lines for string s */
unsigned macro_hash(char *s)
{
unsigned hashval;
if (s == NULL) {
return 0;
}
for (hashval = 0; *s != '\0'; s++)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
/* mlookup: m stands for "macro". look for s in macros macro_hash tabel */
Macro_slot *mlookup(char *s)
{
Macro_slot *np = NULL;
for (np = m_hashtab[macro_hash(s)]; np!= NULL; np = np->next) {
if (strcmp(s, np->name) == 0)
return np; /* found */
}
return NULL; /* incase not found */
}
/* install_macro: save macro's name and content (lines) in macros' hashtable */
Macro_slot *install_macro(char *name, char *lines)
{
Macro_slot *np = NULL;
unsigned hashval = 0;
if((np = mlookup(name)) == NULL) { /* not found */
np = macro_alloc();
if (np == NULL || (np->name = strdup(name)) == NULL )
return NULL;
hashval = macro_hash(name);
np->next = m_hashtab[hashval];
m_hashtab[hashval] = np;
} else { /* already there */
free((void *) np->lines); /* free previous defenition */
}
np->lines = lines;
return np;
}
/* copy_macro_lines: copy the lines between the macro name and "endm"*/
char *copy_macro_lines(FILE *read)
{
char line[MAXLINE] = {0}; /* initialize to all zeros*/
char *all_line = NULL;
int i;
char c = ' ';
all_line = (char *)malloc(100*sizeof(line)); /* assumed maximum macro size */
*all_line = '\0';
while (strstr(line, "endm") == NULL) {
for (i = 0; i < MAXLINE; i++) {
line[i] = '\0';
}
c = getc(read);
for (i = 0; i < MAXLINE; i++) {
line[i] = c;
c = getc(read);
if (c == '\n') {
line[i+1] = '\n';
break;
}
}
if (strstr(line, "endm") == NULL) {
strcat(all_line, line);
}
}
return all_line;
}
/* copy_two_fields: the function places the first two words from given "curline"
into the given fields pointers. if only one field second ptr reamains empty*/
void copy_two_fields(char *first_field, char *second_field, char *curline)
{
int i;
int j;
j = 0;
for (i = 0; i < MAXLINE; i++) { /* copy the first field */
if (!isspace(curline[i])) {
first_field[j] = curline[i];
j++;
}
if (isspace(curline[i]) && (j > 0)) {
break;
}
}
first_field[j] = '\0';
j = 0;
for (; i < MAXLINE && curline[i] != '\0'; i++) { /* copy the second field */
if (!isspace(curline[i])) {
second_field[j] = curline[i];
j++;
}
if (isspace(curline[i]) && (j > 0)) {
break;
}
}
second_field[j] = 0;
return;
}
/* open_file: check if file exists and open it */
FILE *open_file(char *file_name)
{
FILE *fp = NULL;
if ((fp = fopen(file_name, "r")) == NULL) {
printf("error: unable to find file '%s'\n", file_name);
exit(1);
}
return fp;
}
/* expand_macros: the function gets a file name and file pointer. It creats a
a new new-file - identical to the original file which its name passed, but
with macro-defenitions expanded. After the file created it points the given
file pointer to the new file, with reading permission. For its operatin, the
function usesstwo FILE pointers: the input pointer for reading, and another
for writing. It examines each line separately, and places macros contents
instead of their names into the new file. */
char * expand_macros(char *file_name)
{
FILE *write = NULL, *read = NULL;
Macro_slot *saved_macro = NULL;
char *first_field = NULL, *second_field = NULL, *new_file_name = NULL;
char curline[MAXLINE] = {0}; /* I assumed the max macro size*/
char c = ' ';
int i, empty_line = 1;
if ((new_file_name = strdup(file_name)) == NULL) {
printf("error: file name\n");
exit(1);
}
read = open_file(file_name);
new_file_name[strlen(new_file_name) -1] = 'm';/*now ".am" instead of ".as"*/
write = fopen(new_file_name, "w");
first_field = (char *)malloc(MAXLABEL*sizeof(char));
second_field = (char *)malloc(MAXLABEL*sizeof(char));
while (c != EOF) {
for (i = 0; i < MAXLINE; i++) {
/* copy each line until max-line-length, new line break, or EOF case */
curline[i] = (c = getc(read));
if (!isspace(c))
empty_line = 0;
if (c == EOF) {
fclose(read);
fclose(write);
free_macros();
free(first_field);
free(second_field);
return new_file_name;
}
if (c == '\n') {
curline[i] = '\n';
curline[++i] = '\0';
break;
}
}
/* now check for macros declaratins and names (if not an empty line) */
if (!empty_line)
copy_two_fields(first_field, second_field, curline);
if (strcmp(first_field, "macro") == 0) { /* found a macro */
saved_macro = install_macro(second_field,copy_macro_lines(read));
} else if (strlen(second_field) == 0 && strlen(first_field) > 0) {
/* if the line contains only one field -> maybe a macro name */
if ((saved_macro = mlookup(first_field)) != NULL)
/* searching for the macro in the macro_hash table */
fputs(saved_macro->lines, write);
else { /*not a macro's name -> code's errors will be handled later*/
fputs(curline, write);
}
} else {
fputs(curline, write);
}
/* now get ready for next line */
memset(first_field, '\0', MAXLABEL);
memset(second_field, '\0', MAXLABEL);
memset(curline, '\0', MAXLINE);
empty_line = 1;
}
free(first_field);
free(second_field);
free(new_file_name);
free_macros();
fclose(write);
return new_file_name;
}