-
Notifications
You must be signed in to change notification settings - Fork 0
/
kludge.c
176 lines (167 loc) · 4.22 KB
/
kludge.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
/* kludge.c */
#include "packman.h"
int kludgereplace(char **text,int *pos,struct kludgelist *klq,struct reroutetab *rrt) {
struct kludgelist *wlk;
int qq;
char txt[255];
printmsg(3,"KludgeReplace\n");
printmsg(1,"Kludge : ");
qq=0;
while((*text)[*pos+qq]!=CR) {
txt[qq]=(*text)[*pos+qq];
qq++;
}
txt[qq]='\0';
strcat(txt," -> ");
printmsg(1,txt);
wlk=klq;
while(strncmp(wlk->klg.kludge,(*text)+*pos,strlen(wlk->klg.kludge))!=0) {
wlk=wlk->next;
if (wlk==NULL) {
printmsg(1,"Not found\n");
return(-1);
}
}
if (wlk->klg.repl==NULL) {
printmsg(1,"No Action\n");
return(0);
}
if (wlk->klg.repl==NIL) {
int len=0;
printmsg(1,"Remove\n");
while((*text)[*pos+len]!=CARRIAGERETURN) len++;
len++; /* to catch CR */
(*pos)--;
replacestring(text,len+1,*pos,""); /* len+1 and pos-1 to catch CTRLA */
return(0);
} else {
printmsg(1,"Modify\n");
(*pos)+=strlen(wlk->klg.kludge);
parsespec(text,pos,&(wlk->klg),rrt);
return(0);
}
}
int parsespec(char **text,int *pos,struct kludge *klg,struct reroutetab *rrt) {
char symbol;
char *line;
char *rep;
int repeat,type=0,keep=0,len;
printmsg(3,"ParseSpec\n");
line=klg->spec;
rep=klg->repl;
state(LOOP): switch(symbol) {
case '%' : nextstate(TOKEN);
case '\0': nextstate(keepEND);
default : if ((*text)[*pos]!=symbol) {
printmsg(0,"Kludge spefication doesn't match\n");
type=-1;
nextstate(keepEND);
} else {
rep++;
(*pos)++;
nextstate(LOOP);
}
}
state(TOKEN): if (isnum(symbol)) {
line--;
getnum(&repeat,&line);
nextstate(TYPE);
} else {
switch(symbol) {
case '%' : if ((*text)[*pos]!=symbol) {
printmsg(0,"Kludge specification doesn't match\n");
type=-1;
nextstate(keepEND);
} else {
rep++;
(*pos)++;
keep=0;
nextstate(LOOP);
}
case 's' : if (!keep) {
len=0;
while((*text)[*pos+len]!=CR) len++;
replacestring(text,len,*pos,rep);
}
keep=0;
nextstate(keepEND);
case 'k' : keep=1;
nextstate(TOKEN);
default: printmsg(0,"Invalid kludge specification\n");
type=-1;
nextstate(keepEND);
}
}
state(TYPE): switch(symbol) {
case 'a' : if (repeat==0) { char *oldrep;
oldrep=rep;
while((*text)[*pos]!=CR) {
doaddress(text,pos,&rep,rrt,keep);
rep=oldrep;
if ((*text)[*pos]!=CR) {
while(!isnum((*text)[*pos])) (*pos)++;
}
}
} else {
while(repeat--) {
doaddress(text,pos,&rep,rrt,keep);
}
}
keep=0;
nextstate(LOOP);
case 'w' : if (repeat==0) { char *oldrep;
oldrep=rep;
while(*rep!='\0') {
doword(text,pos,&rep,keep);
rep=oldrep;
}
} else {
while(repeat--) {
doword(text,pos,&rep,keep);
}
}
keep=0;
nextstate(LOOP);
case 'n' : if (repeat==0) { char *oldrep;
oldrep=rep;
while(*rep!='\0') {
donum(text,pos,&rep,keep);
rep=oldrep;
}
} else {
while(repeat--) {
donum(text,pos,&rep,keep);
}
}
keep=0;
nextstate(LOOP);
default: keep=0;
printmsg(0,"Unknown token in kludge specification\n");
nextstate(LOOP);
}
state(END): return(type);
}
int parserepl(char **rep) {
char *line;
char symbol;
int type=0;
printmsg(3,"ParseRepl\n");
line=*rep;
state(BEGIN): switch(symbol) {
case '%' : nextstate(TYPE);
default: printmsg(0,"Replacement specification doesn't match\n");
type=-1;
nextstate(keepEND);
}
state(TYPE): switch(symbol) {
case 'o' : type=1;
nextstate(keepEND);
case 'd' : type=2;
nextstate(keepEND);
default: printmsg(0,"Unknown token in replacement specification\n");
type=-1;
nextstate(keepEND);
}
state(END): *rep=line;
return(type);
}