-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtetx.c
427 lines (346 loc) · 7.3 KB
/
tetx.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* tetx.c
TE text tool.
Copyright (c) 2021 Miguel Garcia / FloppySoftware
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program 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 this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Author's contact:
http://www.floppysoftware.es/
https://cpm-connections.blogspot.com
To compile with MESCC under CP/M:
cc tetx
ccopt tetx
zsm tetx
hextocom tetx
Usage:
tetx
Revisions:
03 Nov 2021 : Start.
04 Nov 2021 : v1.00.
20 Nov 2021 : Refactorized in order to read full lines. Add -S option.
20 Nov 2021 : v1.01.
25 Nov 2021 : Bugfix on error opening file.
25 Nov 2021 : v1.02.
Notes:
-
*/
/* MESCC libraries
---------------
*/
#define CC_STDIO /* Support for stdin, stdout, stderr */
#define CC_REDIR /* Support for command line redirection - needs CC_STDIO */
#define CC_FGETS /* Include fgets() */
#include <mescc.h>
#include <ctype.h>
#include <conio.h>
#include <fileio.h>
#include <printf.h>
#include <fprintf.h>
#include <redir.h>
/* Project defs.
-------------
*/
#define APP_NAME "TETX"
#define APP_VERSION "v1.02 / 25 Nov 2021"
#define APP_COPYRGT "(c) 2021 Miguel Garcia / FloppySoftware"
#define APP_INFO "TE text tool."
#define APP_USAGE "tetx [-options...] [fname...] [> fname]"
#define APP_OPT_E "-e Skip empty lines."
#define APP_OPT_L "-l Convert to lowercase."
#define APP_OPT_N "-n Print line numbers."
#define APP_OPT_T "-t[N] Convert tab to spaces (default = 4)."
#define APP_OPT_U "-u Convert to uppercase."
#define APP_OPT_W "-wN Max. width of lines (32..255)."
#define APP_OPT_S "-s Trim spaces on the right."
#define APP_EX_1 "tetx -ne mydoc.txt"
#define APP_EX_2 "tetx -t4u -n letter1.txt letter2.txt > letters.txt"
#define TAB_SPACES 4 /* How many spaces are a tab by default */
#define TAB_MIN 1 /* Minimum number of spaces for a tab */
#define TAB_MAX 16 /* Maximum number of spaces for a tab */
#define WIDTH_MIN 32 /* Minimum width of lines */
#define WIDTH_MAX 255 /* Maximum width of lines */
#define BUF_SIZE 257 /* WIDTH_MAX + \n + \0 */
/* Options flags
-------------
*/
#define FLAG_E 1 /* Skip empty lines ? */
#define FLAG_L 2 /* Convert to lowercase */
#define FLAG_N 4 /* Print line numbers */
#define FLAG_T 8 /* Convert tab to spaces */
#define FLAG_U 16 /* Convert to uppercase */
#define FLAG_W 32 /* Max. width of lines */
#define FLAG_S 64 /* Trim spaces on the right */
/* Globals
-------
*/
int flags;
int line;
int tab_spaces;
int width;
int col;
char buf[BUF_SIZE];
/* Program entry
-------------
*/
main(argc, argv)
int argc;
unsigned int argv[]; // char *argv[] - unsupported by MESCC (yet?)
{
int i, n;
char *p;
/* Show usage if there are no arguments */
if(argc == 1) {
usage();
}
/* Defaults */
flags = 0;
line = 1;
tab_spaces = TAB_SPACES;
/* Check arguments */
for(i = 1; i < argc; ++i) {
p = argv[i];
if(*p == '-') {
while(*++p) {
switch(*p) {
case 'E' : flags |= FLAG_E; break;
case 'L' : flags |= FLAG_L; break;
case 'N' : flags |= FLAG_N; break;
case 'T' : flags |= FLAG_T;
n = 0;
if(isdigit(*(p+1))) {
do {
n = n * 10 + (*(++p) - '0');
} while(isdigit(*(p+1)));
if(n >= TAB_MIN && n <= TAB_MAX) {
tab_spaces = n;
}
else {
error("Illegal number of spaces");
}
}
break;
case 'U' : flags |= FLAG_U; break;
case 'W' : flags |= FLAG_W;
n = 0;
if(isdigit(*(p+1))) {
do {
n = n * 10 + (*(++p) - '0');
} while(isdigit(*(p+1)));
if(n >= WIDTH_MIN && n <= WIDTH_MAX) {
width = n;
}
else {
error("Illegal width of lines");
}
}
break;
case 'S' : flags |= FLAG_S; break;
default : error("Unknown option");
}
}
}
else {
break;
}
}
/* Process files */
if(i == argc) {
tx_file("-");
}
else {
for(i = i; i < argc; ++i) {
if(tx_file(argv[i]))
return -1;
}
}
/* Return success */
return 0;
}
/* Process file
------------
*/
tx_file(fn)
char *fn;
{
FILE *fp;
int len;
/* Open */
if(*fn == '-' && !fn[1]) {
fp = stdin;
}
else if((fp = fopen(fn, "r")) == NULL) {
error_fname("Can't open", fn);
}
/* Process lines */
for(;;) {
if(!fgets(buf, BUF_SIZE, fp)) {
break;
}
len = strlen(buf);
if(buf[len - 1] == '\n') {
buf[--len] = '\0';
}
else if(len > WIDTH_MAX)
{
error("Line too long");
}
tx_line(len);
}
/* Close */
if(fp != stdin) {
fclose(fp);
}
/* Success */
return 0;
}
/* Process line
------------
*/
tx_line(len)
int len;
{
int i;
/* Setup */
col = 0;
/* Trim spaces on the right ? */
if(flags & FLAG_S) {
while(len) {
if(buf[len - 1] != ' ') {
break;
}
buf[--len] = '\0';
}
}
/* Lowercase ? */
if(flags & FLAG_L) {
for(i = 0; i < len; ++i) {
buf[i] = tolower(buf[i]);
}
}
/* Uppercase ? */
if(flags & FLAG_U) {
for(i = 0; i < len; ++i) {
buf[i] = toupper(buf[i]);
}
}
/* Process characters */
for(i = 0; i < len; ++i) {
tx_out(buf[i]);
}
tx_out('\n');
}
/* Process character and output result
-----------------------------------
*/
tx_out(ch)
int ch;
{
int k;
/* New line ? */
if(ch == '\n') {
/* Empty line ? */
if(col == 0) {
/* Skip empty lines ? */
if(flags & FLAG_E) {
return;
}
}
}
/* Start of line ? */
if(col == 0) {
/* Print line numbers */
if(flags & FLAG_N) {
printf("%5d ", line++);
col += 6;
}
}
/* New line ? */
if(ch == '\n') {
putchar('\n');
col = 0;
return;
}
/* Tab ? */
if(ch == '\t') {
if(flags & FLAG_T) {
k = tab_spaces - (col % tab_spaces);
/* Max. width ? */
if(flags & FLAG_W) {
if(col + k >= width) {
tx_out('\n');
tx_out('\t');
return;
}
}
col += k;
while(k--) {
putchar(' ');
}
return;
}
}
/* Max. width ? */
if(flags & FLAG_W) {
if(col + 1 > width) {
tx_out('\n');
tx_out(ch);
return;
}
}
/* Print character */
putchar(ch);
++col;
}
/* Show usage and exit
-------------------
*/
usage()
{
fprintf(stderr, "%s %s - %s\n\n", APP_NAME, APP_VERSION, APP_COPYRGT);
fprintf(stderr, "%s\n\n", APP_INFO);
fprintf(stderr, "Usage:\n\t%s\n\n", APP_USAGE);
fprintf(
stderr,
"Options:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\n",
APP_OPT_E,
APP_OPT_L,
APP_OPT_N,
APP_OPT_T,
APP_OPT_U,
APP_OPT_W,
APP_OPT_S
);
fprintf(stderr, "Examples:\n\t%s\n\t%s\n",
APP_EX_1,
APP_EX_2
);
exit(0);
}
/* Print error and exit
--------------------
*/
error(msg)
char *msg;
{
fprintf(stderr, "%s: %s.\n", APP_NAME, msg);
exit(-1);
}
/* Print error and exit
--------------------
*/
error_fname(msg, fname)
char *msg, *fname;
{
fprintf(stderr, "%s: %s \"%s\".\n", APP_NAME, msg, fname);
exit(-1);
}