-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssp.c
155 lines (126 loc) · 2.5 KB
/
ssp.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
#include <stdio.h>
#include <string.h>
#include "unitok/unitok.h"
/*
* ssp - standard saol preprocessor
* v1.3
* 02/01/2020
* by Centrix
*/
char *ssp_tok(char *str, int *pos);
int is_kw(char* word);
void exec(int op_code, char* arg);
void macro(char *arg), link(char *arg), prog(char *arg), copy(char *arg);
char macro_names[1024][256], macro_codes[1024][256];
int len = 3, code = 3, pos = 0;
FILE *src, *dst;
int is_end = 0;
int main(int argc, char *argv[]) {
char line[1024], *tok = "";
int pos = 0;
if (argc < 2) {
fprintf(stderr, "Usage: in_file out_file\n");
return 1;
}
src = fopen(argv[1], "r");
dst = fopen(argv[2], "w");
while (fgets(line, 1024, src) != NULL) {
code = 3;
pos = 0;
tok = ssp_tok(line, &pos);
while (tok != NULL) {
if (is_kw(tok) >= 0)
code = is_kw(tok);
if (!isempty(tok))
exec(code, tok);
tok = ssp_tok(line, &pos);
}
}
fclose(src);
fclose(dst);
return is_end;
}
char *ssp_tok(char *str, int *pos) {
static char tok[256];
char *tptr = tok, *sptr = &str[*pos];
int sticking = 0;
if (!(*sptr))
return NULL;
while (*sptr) {
if (strchr("{}", *sptr) != NULL) {
sticking = !sticking;
sptr++;
if (*(sptr-1) == '}') break;
continue;
}
if (sticking || (strchr(" \n", *sptr) == NULL)) {
*tptr = *sptr;
tptr++; sptr++;
}
else
break;
}
*tptr = '\0';
*pos = (sptr - str) + 1;
return tok;
}
int is_kw(char *word) {
char *words[] = {".macro", ".link", ".prog"};
for (int i = 0; i < len; i++) {
if (!strcmp(words[i], word)) return i;
}
return -1;
}
void exec(int op_code, char *arg) {
void (*funcs[])(char*) = {macro, link, prog, copy};
(*funcs[op_code])(arg);
}
void macro(char *arg) {
static int state = 0;
if (!state) {
state = 1;
return ;
}
else if (state == 1) {
sprintf(macro_names[pos], "%s", arg);
state = 2;
}
else {
sprintf(macro_codes[pos++], "%s", arg);
state = 0;
code = 3;
}
is_end++;
}
void link(char *arg) {
FILE *from;
int c = 0;
if (!strcmp(arg, ".link")) return ;
from = fopen(arg, "r");
if (from == NULL) {
fprintf(stderr, "no such file `%s`\n", arg);
return ;
}
while (c != EOF) {
if (c > 0)
putc(c, dst);
c = fgetc(from);
}
putc('\n', dst);
fclose(from);
is_end++;
}
void prog(char *arg) {
code = 3;
}
int is_macro(char *word) {
for (int i = 0; i < pos; i++)
if (!strcmp(macro_names[i], word)) return i;
return -1;
}
void copy(char *arg) {
if (is_macro(arg) >= 0)
fprintf(dst, "%s ", macro_codes[is_macro(arg)]);
else
fprintf(dst, "%s ", arg);
}