/
main.c
287 lines (237 loc) · 11.2 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
285
286
//
// main.c
// morsefeed
//
// Copyright (C) 2018 Michael Budiansky. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions
// and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions
// and the following disclaimer in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "morsefeed.h"
#include "text.h"
#include "vector.h"
#define STATE_FILE_NAME ".morsefeed"
void print_it(void *p);
void print_it(void *p) {
char *txt = *(char **)p;
printf("%s\n", txt);
}
int main(int argc, const char * argv[]) {
MorseFeedError error = MF_NO_ERROR;
const char *home = getenv("HOME");
MorseFeedParams mfp;
const char *state_label = NULL;
StringVector string_storage = string_vector_create(0);
mfp.in_file_name = NULL;
mfp.in_file = NULL;
mfp.out_file = NULL;
mfp.url = NULL;
mfp.state_path = NULL;
mfp.words_per_row = DEFAULT;
mfp.word_count = DEFAULT;
mfp.fork_mbeep = false;
mfp.save_and_use_position = false;
mfp.follow_links = false;
mfp.text_after = NULL;
mfp.text_before = NULL;
mfp.linked_text_after = NULL;
mfp.linked_text_before = NULL;
mfp.freq = DEFAULT;
mfp.paris_wpm = DEFAULT;
mfp.codex_wpm = DEFAULT;
mfp.farnsworth_wpm = DEFAULT;
mfp.word_space_wpm = DEFAULT;
mfp.print_fcc_wpm = false;
mfp.wav_file_name = NULL;
// make path to state file
if (home != NULL) {
mfp.state_path = malloc(strlen(home) + strlen("/") + strlen(STATE_FILE_NAME) + 1);
if (mfp.state_path != NULL) {
strcpy(mfp.state_path, home);
strcat(mfp.state_path, "/");
strcat(mfp.state_path, STATE_FILE_NAME);
}
}
for (int index = 1; index < argc && error == MF_NO_ERROR; index++) {
// -c words_per_row
if (strcmp(argv[index], "-c") == 0 && index + 1 < argc) {
mfp.words_per_row = atoi(argv[++index]);
if (mfp.words_per_row < 1 || mfp.words_per_row > 100) error = MF_INVALID_VALUE;
// -n number of words
} else if (strcmp(argv[index], "-n") == 0 && index + 1 < argc) {
mfp.word_count = atoi(argv[++index]);
if (mfp.word_count < 1) error = MF_INVALID_VALUE;
// -m --mbeep (send to mbeep)
} else if ((strcmp(argv[index], "--mbeep") == 0) ||
(strcmp(argv[index], "-m") == 0)) {
mfp.fork_mbeep = true;
// -f frequency [for mbeep]
} else if (strcmp(argv[index], "-f") == 0 && index + 1 < argc) {
mfp.freq = atof(argv[++index]);
if (mfp.freq < 20.0 || mfp.freq > 20000.0) error = MF_INVALID_FREQUENCY;
// -w --paris-wpm words per minute, PARIS standard [for mbeep]
} else if (((strcmp(argv[index], "--paris-wpm") == 0) ||
(strcmp(argv[index], "-w") == 0)) && index + 1 < argc) {
mfp.paris_wpm = atof(argv[++index]);
if (mfp.paris_wpm < 5.0 || mfp.paris_wpm > 60.0) error = MF_INVALID_WPM;
// --codex-wpm words per minute, CODEX standard [for mbeep]
} else if (strcmp(argv[index], "--codex-wpm") == 0 && index + 1 < argc) {
mfp.codex_wpm = atof(argv[++index]);
if (mfp.codex_wpm < 5.0 || mfp.codex_wpm > 60.0) error = MF_INVALID_WPM;
// -x --farnsworth character speed [for mbeep]
} else if (((strcmp(argv[index], "--farnsworth") == 0) ||
(strcmp(argv[index], "-x") == 0)) && index + 1 < argc) {
mfp.farnsworth_wpm = atof(argv[++index]);
if (mfp.farnsworth_wpm < 5.0 || mfp.farnsworth_wpm > 60.0) error = MF_INVALID_WPM;
// --wss word space speed [for mbeep]
} else if ((strcmp(argv[index], "--wss") == 0) && index + 1 < argc) {
mfp.word_space_wpm = atof(argv[++index]);
if (mfp.word_space_wpm < 5.0 || mfp.word_space_wpm > 60.0) error = MF_INVALID_WPM;
// --fcc print FCC wpm
} else if (strcmp(argv[index], "--fcc") == 0) {
mfp.print_fcc_wpm = true;
// -i input file for text to be converted
} else if (strcmp(argv[index], "-i") == 0 && index + 1 < argc) {
if (mfp.in_file != NULL) {
error = MF_FILE_ALREADY_OPEN_ERROR;
} else {
mfp.in_file_name = argv[++index];
mfp.in_file = fopen(mfp.in_file_name , "r");
}
if (mfp.in_file == NULL) {
error = MF_INPUT_FILE_OPEN_ERROR;
}
// --wav file name for mbeep .wav output [for mbeep]
} else if (strcmp(argv[index], "--wav") == 0 && index + 1 < argc) {
mfp.wav_file_name = argv[++index];
// -u input URL for text-only news site to be converted
} else if (strcmp(argv[index], "-u") == 0 && index + 1 < argc) {
mfp.url = argv[++index];
// -a use text after string
} else if (strcmp(argv[index], "-a") == 0 && index + 1 < argc) {
mfp.text_after = argv[++index];
// -b use text before string
} else if (strcmp(argv[index], "-b") == 0 && index + 1 < argc) {
mfp.text_before = argv[++index];
// -L (follow links)
} else if (strcmp(argv[index], "-L") == 0) {
mfp.follow_links = true;
// -A use text after string
} else if (strcmp(argv[index], "-A") == 0 && index + 1 < argc) {
mfp.linked_text_after = argv[++index];
// -B use text before string
} else if (strcmp(argv[index], "-B") == 0 && index + 1 < argc) {
mfp.linked_text_before = argv[++index];
// -p (save and use position)
} else if (strcmp(argv[index], "-p") == 0) {
if (mfp.state_path == NULL) {
error = MF_NO_STATE_PATH;
} else {
mfp.save_and_use_position = true;
}
// -o output file for converted text
} else if (strcmp(argv[index], "-o") == 0 && index + 1 < argc && mfp.out_file == NULL) {
mfp.out_file = fopen(argv[++index], "w");
if (mfp.out_file == NULL) {
error = MF_OUTPUT_FILE_OPEN_ERROR;
}
// -s save state for re-use
} else if (strcmp(argv[index], "-s") == 0 && index + 1 < argc) {
state_label = argv[++index];
// -r read saved state
} else if (strcmp(argv[index], "-r") == 0 && index + 1 < argc) {
error = read_state(argv[++index], &mfp, &string_storage);
// -v --version print version of mbeep
} else if ((strcmp(argv[index], "--version") == 0) ||
(strcmp(argv[index], "-v") == 0)) {
version();
error = MF_EXIT;
// -h --help print help for mbeep
} else if ((strcmp(argv[index], "--help") == 0) ||
(strcmp(argv[index], "-h") == 0)) {
usage();
error = MF_EXIT;
// --man-page print source for man page
} else if (strcmp(argv[index], "--man-page") == 0) {
man_page_source();
error = MF_EXIT;
// --licence print copyright and license
} else if (strcmp(argv[index], "--license") == 0) {
license();
error = MF_EXIT;
#if DEBUG
// --test run tests
} else if (strcmp(argv[index], "--test") == 0) {
vector_tests();
morsefeed_tests();
error = MF_EXIT;
#endif
} else {
error = MF_INVALID_OPTION;
}
}
if (error == MF_NO_ERROR && state_label != NULL) {
error = save_state(state_label, &mfp);
}
if (error == MF_NO_ERROR) {
error = process_and_send(mfp);
}
if (mfp.in_file != NULL) {
if (mfp.in_file != stdin) fclose(mfp.in_file);
mfp.in_file = NULL;
}
if (mfp.out_file != NULL) {
if (mfp.out_file == stdout) {
fflush(stdout);
} else {
fclose(mfp.out_file);
}
mfp.out_file = NULL;
}
switch (error) {
case MF_NO_ERROR: break;
case MF_EXIT: break;
case MF_PIPE_ERROR: printf("Error: MF_PIPE_ERROR\n"); break;
case MF_FORK_ERROR: printf("Error: MF_FORK_ERROR\n"); break;
case MF_INVALID_VALUE: printf("Error: MF_INVALID_VALUE\n"); break;
case MF_OUT_OF_MEMORY: printf("Error: MF_OUT_OF_MEMORY\n"); break;
case MF_INVALID_FREQUENCY: printf("Error: MF_INVALID_FREQUENCY\n"); break;
case MF_INVALID_WPM: printf("Error: MF_INVALID_WPM\n"); break;
case MF_INVALID_OPTION: printf("Error: MF_INVALID_OPTION\n"); break;
case MF_FILE_READ_ERROR: printf("Error: MF_FILE_READ_ERROR\n"); break;
case MF_URL_READ_ERROR: printf("Error: MF_URL_READ_ERROR\n"); break;
case MF_INPUT_FILE_OPEN_ERROR: printf("Error: MF_INPUT_FILE_OPEN_ERROR\n"); break;
case MF_OUTPUT_FILE_OPEN_ERROR: printf("Error: MF_OUTPUT_FILE_OPEN_ERROR\n"); break;
case MF_POSITION_FILE_OPEN_ERROR: printf("Error: MF_POSITION_FILE_OPEN_ERROR\n"); break;
case MF_FILE_ALREADY_OPEN_ERROR: printf("Error: MF_FILE_ALREADY_OPEN_ERROR\n"); break;
case MF_FILE_WRITE_ERROR: printf("Error: MF_FILE_WRITE_ERROR\n"); break;
case MF_PROGRAM_ERR: printf("Error: MF_PROGRAM_ERR\n"); break;
case MF_NO_STATE_PATH: printf("Error: MF_NO_STATE_PATH\n"); break;
case MF_UNKNOWN_SAVED_STATE: printf("Error: MF_UNKNOWN_SAVED_STATE\n"); break;
case MF_UNKNOWN:
default:
printf("Error: unknown %d\n", error);
break;
};
string_vector_free(&string_storage);
return 0;
}