-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoma.c
238 lines (199 loc) · 8.81 KB
/
foma.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
/* Foma: a finite-state toolkit and library. */
/* Copyright © 2008-2011 Mans Hulden */
/* This file is part of foma. */
/* Foma is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License version 2 as */
/* published by the Free Software Foundation. */
/* Foma is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License */
/* along with foma. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <readline/readline.h>
#include "foma.h"
/* Front-end behavior variables */
int pipe_mode = 0;
int quiet_mode = 0;
static int use_readline = 1;
int promptmode = PROMPT_MAIN;
int apply_direction;
/* Variable to pass the position of rl completion to our completer */
static int smatch;
char *usagestring = "Usage: foma [-e \"command\"] [-f run-once-script] [-l startupscript] [-p] [-q] [-s] [-v]\n";
static char** my_completion(const char*, int ,int);
char *my_generator(const char* , int);
char *cmd [] = {"ambiguous upper","apply down","apply med","apply up","apropos","assert-stack","clear stack","compact sigma","complete net","compose net","concatenate net","crossproduct net","define","determinize net","echo","eliminate flags","eliminate flag","export cmatrix","extract ambiguous","extract unambiguous","help license","help warranty","ignore net","intersect net","invert net","label net","letter machine","load defined","lower-side net","minimize net","name net","negate net","one-plus net","pop stack","print defined","print dot","print lower-words","print cmatrix","print name","print net","print random-lower","print random-upper","print random-words","print sigma","print size","print shortest-string","print shortest-string-length","print upper-words","prune net","push defined","quit","read att","read cmatrix","read prolog","read lexc","read regex","read spaced-text","read text","reverse net","rotate stack","save defined","save stack","set","show variables","show variable","shuffle net","sigma","sigma net","source","sort net","substitute defined","substitute symbol","system","test unambiguous","test star-free","test equivalent","test functional","test identity","test lower-universal","test upper-universal","test non-null","test null","test sequential","turn stack","twosided flag-diacritics","undefine","union net","upper-side net","view net","write att","write prolog","zero-plus net",NULL};
char *abbrvcmd [] = {"ambiguous","down","up","med","size","loadd","lower-words","upper-words","net","random-lower","random-upper","random-words","regex","rpl","au revoir","bye","exit","saved","ss","stack","tunam","tid","tfu","tlu","tuu","tnu","tnn","tseq","tsf","equ","pss","psz","ratt","tfd","hyvästi","watt","wpl","examb","exunamb",NULL};
/* #include "yy.tab.h" */
int view_net(struct fsm *net);
extern int input_is_file;
extern int add_history (const char *);
extern int my_yyparse(char *my_string);
void print_help();
void xprintf(char *string) { return ; printf("%s",string); }
char disclaimer[] = "Foma, version 0.9.15alpha\nCopyright © 2008-2011 Mans Hulden\nThis is free software; see the source code for copying conditions.\nThere is ABSOLUTELY NO WARRANTY; for details, type \"help license\"\n\nType \"help\" to list all commands available.\nType \"help <topic>\" or help \"<operator>\" for further help.\n\n";
/* A static variable for holding the line. */
static char *command = (char *)NULL;
char *flex_command = NULL;
static char *line_read = (char *)NULL;
char no_readline_line[512];
/* Read a string, and return a pointer to it.
Returns NULL on EOF. */
char *rl_gets(char *prompt) {
/* If the buffer has already been allocated,
return the memory to the free pool. */
if (use_readline == 1) {
if (line_read) {
free(line_read);
line_read = (char *)NULL;
}
}
if (use_readline == 0) {
printf("%s",prompt);
line_read = fgets(no_readline_line, 511, stdin);
if (line_read != NULL) {
strip_newline(line_read);
}
} else {
line_read = readline(prompt);
}
/* If the line has any text in it,
save it on the history. */
if (use_readline == 1) {
if (line_read && *line_read)
add_history(line_read);
}
return (line_read);
}
int main(int argc, char *argv[]) {
int opt;
char *scriptfile, prompt[50];
extern void my_interfaceparse(char *my_string);
/* YY_BUFFER_STATE flex_command; */
stack_init();
while ((opt = getopt(argc, argv, "e:f:hl:pqrsv")) != -1) {
switch(opt) {
case 'e':
my_interfaceparse(optarg);
break;
case 'f':
scriptfile = file_to_mem(optarg);
if (scriptfile != NULL) {
input_is_file = 1;
my_interfaceparse(scriptfile);
}
exit(0);
case 'h':
print_help();
exit(0);
case 'l':
scriptfile = file_to_mem(optarg);
if (scriptfile != NULL) {
input_is_file = 1;
my_interfaceparse(scriptfile);
xxfree(scriptfile);
}
break;
case 'p':
pipe_mode = 1;
break;
case 'q':
quiet_mode = 1;
break;
case 'r':
use_readline = 0;
break;
case 's':
exit(0);
case 'v':
printf("%s %i.%i.%i%s\n",argv[0],MAJOR_VERSION,MINOR_VERSION,BUILD_VERSION,STATUS_VERSION);
exit(0);
default:
fprintf(stderr, "%s", usagestring);
exit(EXIT_FAILURE);
}
}
if (!pipe_mode && !quiet_mode)
printf("%s",disclaimer);
rl_basic_word_break_characters = " >";
rl_attempted_completion_function = my_completion;
for(;;) {
if (promptmode == PROMPT_MAIN)
sprintf(prompt, "foma[%i]: ",stack_size());
if (promptmode == PROMPT_A && apply_direction == AP_D)
sprintf(prompt, "apply down> ");
if (promptmode == PROMPT_A && apply_direction == AP_U)
sprintf(prompt, "apply up> ");
if (promptmode == PROMPT_A && apply_direction == AP_M)
sprintf(prompt, "apply med> ");
if (pipe_mode || quiet_mode)
prompt[0] = '\0';
command = rl_gets(prompt);
if (command == NULL && promptmode == PROMPT_MAIN) {
printf("\n");
exit(0);
}
if (command == NULL && promptmode == PROMPT_A) {
/* apply_clear(); */
promptmode = PROMPT_MAIN;
printf("\n");
continue;
}
input_is_file = 0;
my_interfaceparse(command);
}
}
void print_help() {
printf("%s",usagestring);
printf("Options:\n");
printf("-e \"command\"\texecute a command on startup (-e can be invoked several times)\n");
printf("-f scriptfile\tread commands from scriptfile on startup, and quit\n");
printf("-l scriptfile\tread commands from scriptfile on startup\n");
printf("-p\t\tpipe-mode\n");
printf("-q\t\tquiet mode (more quiet than pipe-mode)\n");
printf("-r\t\tdon't use readline library for input\n");
printf("-s\t\tstop execution and exit\n");
printf("-v\t\tprint version number\n");
}
static char **my_completion(const char *text, int start, int end) {
char **matches;
matches = (char **)NULL;
smatch = start;
matches = rl_completion_matches ((char*)text, &my_generator);
return (matches);
}
char *my_generator(const char *text, int state) {
static int list_index, list_index2, len, nummatches;
char *name;
text = rl_line_buffer;
if (!state) {
list_index = 0;
list_index2 = 0;
nummatches = 0;
len = strlen(text);
}
while ((name = cmd[list_index])) {
list_index++;
if (strncmp (name, text, len) == 0) {
nummatches++;
/* Can't use xxstrdup here */
return(strdup(name+smatch));
}
}
if (rl_point > 0) {
while ((name = abbrvcmd[list_index2])) {
list_index2++;
/* Can't use xxstrdup here */
if (strncmp (name, text, len) == 0)
return(strdup(name+smatch));
}
}
/* If no names matched, then return NULL. */
return ((char *)NULL);
}