-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
284 lines (238 loc) · 7.43 KB
/
main.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include "log.c/src/log.h"
#include "parser.h"
#include "shellstrings.h"
#include "dictionary.h"
#include "main_functions_batch.h"
#include "main_functions_interactive.h"
#include "main_functions_save.h"
#include "main_functions_edit.h"
#include "main_functions_home.h"
#define UNIX_MAX_PATH 4096
/*
* Main function
*
* - parses through initial command line
* - in the absence of a file to parse, open main page, which can either do one of 4 things:
* - load a new dictionary (will replace the one that is parsed)
* - quit
* - launch either interactive or batch mode
* - save file
*
* Sample inputs:
*
* One input
* > ./spellcheck: opens main page
*
* Two inputs
* > ./spellcheck [filename.txt]
*
* Other flags:
* > -d [dictname.txt]: changes dictionary
*
* > -i/-q : interactive, quiet mode
*
* > -l: verbose mode
*
* > -s [filename.txt]: file saving destination
*
* > -c : colored strings
*
* > -v : logging at ERROR level
*
* > -vv : loggig at INFO level
*
* > -vvv : logging at TRACE level
*
* ...
* and so on, with different combinations.
*
* Largest possible number of argc: 9
* > ./spellcheck file.txt -d my_dict.txt -q misspelled.txt -s savefilename.txt -c
*/
/* adjusts log level according to command line input */
void change_log_level(int v) {
// Set log level
switch (v) {
case 0: // Only fatal
log_set_level(LOG_FATAL);
break;
case 1: // Warn, Error
log_set_level(LOG_ERROR);
break;
case 2: // Debug, Info
log_set_level(LOG_INFO);
break;
case 3: // Trace
log_set_level(LOG_TRACE);
break;
default:
log_set_level(LOG_FATAL);
}
}
int main(int argc, char *argv[]) {
if (argc > 9) {
log_error("Too many command line arguments was input.");
shell_usage(false);
log_info("Usage help page provided.");
exit(1);
}
// filenames up to 400 char
char *dict = malloc(UNIX_MAX_PATH * sizeof(char *));
char *filename = malloc(UNIX_MAX_PATH * sizeof(char *));
char *save_file = malloc(UNIX_MAX_PATH * sizeof(char *));
// Default dict name
strcpy(dict, "default");
/*
* 1: Quiet Batch Mode
* 2: Verbose Batch Mode
* 3: Interactive Mode
*/
int mode = INTERACTIVE_MODE;
bool color = false;
int logmode = 0;
// Parse Command Line Arguments
char c;
if (fileexists(argv[optind])) {
strcpy(filename, argv[optind]);
optind++;
}
// Parse the initial command line and
while ((c = getopt(argc, argv, "ilvcqs:d:v")) != -1) {
switch (c) {
case 'd':
if (!fileexists(optarg)) { // this checks if the file actually exists
shell_error("Invalid dictionary file input.", color);
log_fatal("Dictionary file could not be found.");
return EXIT_FAILURE;
}
strcpy(dict, optarg);
if (mode == INTERACTIVE_MODE) {
log_debug("dictionary accepted");
}
break;
case 'i':
mode = INTERACTIVE_MODE;
break;
case 'l':
mode = VERBOSE_MODE;
break;
case 'q':
mode = QUIET_MODE;
break;
case 's':
strcpy(save_file, optarg);
if (mode == INTERACTIVE_MODE) {
shell_input(optarg, "file save destination", color);
}
break;
case 'c':
color = true;
break;
case 'v':
logmode++;
break;
case '?':
shell_usage(color);
exit(0);
break;
default:
shell_error("Invalid format.", color);
log_fatal("Invalid shell line input.");
exit(0);
}
}
change_log_level(logmode);
bool quit = true;
while (quit == true) {
if (fileexists(filename)) { // if file exists, then bypass main page
quit = false;
}
/*
* Main page: Activated if there is no file to be parsed.
* can open help page, quit, or load filename / dictname
*/
log_trace("Printing the main page.");
main_page(&quit, &mode, filename, dict, &color);
if (mode == QUIT) { // user selected "quit" in main_page
log_trace("Quitting the program.");
return 0;
}
int msg;
dict_t *new_dict;
// Initialize dictionary, declare names of files to be used
if (strcmp(dict, "default") == 0) {
// use default dictionary
new_dict = dict_official();
if (new_dict == NULL) {
msg = EXIT_FAILURE;
} else {
msg = EXIT_SUCCESS;
}
} else {
new_dict = dict_new();
msg = dict_read(new_dict, dict);
}
if (msg == EXIT_FAILURE) {
shell_error("Invalid dictionary file input.", color);
log_fatal("Invalid dictionary file input.");
exit(0);
}
// Starting to Parse file! Printing messages accordingly
char *md = shell_modename(mode);
log_trace("Mode set to: %s", md);
if (mode == INTERACTIVE_MODE) {
shell_start_interactive(filename, dict, md, color);
}
char **result = NULL;
// Execute either interactive or batch mode, and save file at end
switch (mode) {
case QUIET_MODE:
log_info("Entering quiet mode.");
result = batch_mode(filename, new_dict, &quit, QUIET_MODE); // pass in dictionary
break;
case VERBOSE_MODE:
log_info("Entering verbose mode.");
result = batch_mode(filename, new_dict, &quit, VERBOSE_MODE); // pass in dictionary
break;
case INTERACTIVE_MODE:
log_info("Entering interactive mode.");
result = interactive_mode(filename, new_dict, &quit, color); // pass in dictionary
break;
default:
break;
}
if (quit == true) {
log_fatal("parsing failed");
return 1;
}
if ((mode != VERBOSE_MODE) && (result != NULL)) { // Save file, a functionality unnecessary for verbose batch mode
if (mode == INTERACTIVE_MODE) {
log_trace("Printing success message.");
shell_edit_success(color);
}
md = strstr(save_file, ".txt\0");
if (md == NULL && (mode == QUIET_MODE)) {
log_trace("Printing result of the file edit.");
shell_print(result);
quit = false;
}
else if (md != NULL) {
log_trace("Saving corrections to the designated file destination: %s", save_file);
save_corrections(save_file, result);
quit = false;
}
else {
log_trace("Printing the save page.");
save_page(filename, result, &quit, color);
}
}
}
log_info("Program ran successfully.");
return 0;
}