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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "buf.h"
#include "ll.h"
char* line(void);
char* wordset(char* txt);
void wsclean(char* txt);
typedef enum {
false,
true
} bool;
FILE* getfile(int argc, char** argv){
if (argc >= 2)
return fopen(argv[1], "r");
else
return stdin;
}
char* chrmult(char c, size_t ct){
char* str = malloc(sizeof(char)*(ct+1)); str[ct] = 0;
memset(str, c, ct);
return str;
}
// file read & buffer fill helpers
FILE* in;
buf* inbuf;
int addchr(void){
int c = fgetc(in);
if (c != EOF) inschrbuf(inbuf, c);
return c;
}
size_t fillbuf(size_t len){ // returns real len
int c;
size_t set;
for (set = buflen(inbuf); set < len && (c = addchr()) != EOF;set++);
return set;
}
size_t chrfill(char chr){
size_t len = buflen(inbuf);
char* str = peekstrbuf(inbuf, 0, len);
char* line = memchr(str,chr,len);
free(str);
if (line) return line-str+1;
for (int c = 0; c != chr && (c = addchr()) != EOF; len++);
return len;
}
#define min(a,b) a < b ? a : b
size_t chrnfill(char chr, size_t sz){//fills to first of chr or sz
size_t len = buflen(inbuf);
size_t lim = min(len,sz);
char* str = peekstrbuf(inbuf, 0, lim);
char* line = memchr(str, chr, lim);
free(str);
if (line) return line-str+1;
for (int c = 0; c != chr && (c = addchr()) != EOF&& len<sz; len++);
return len;
}
// typesetting config and commands to change.
int width = 80;
char* center(char* txt){
size_t len = strlen(txt);
int max = width/2 + len/2; // excluding terminator
int min = max-len+1; // first index of *txt
char* str = malloc(sizeof(char)*(max + 2));
memset(str, ' ', min);
memcpy(str+min, txt, len);
str[max] = '\n';
str[max+1] = 0;
return str;
}
char* setwidth(char* txt){
sscanf(txt, "%d", &width);
return NULL;
}
char* fillline(char* txt){
char* str = malloc(sizeof(char)*(width+2));
str[width] = '\n'; str[width+1] = 0;
memset(str, txt[0], width);
return str;
}
char* leader(char* txt){
char* cnxt;
char* start = strtok_r(txt, "|", &cnxt);
char* repeat = strtok_r(NULL, "|", &cnxt);
char* end = strtok_r(NULL, "|", &cnxt);
char* fin = malloc(sizeof(char)*(width+2));
fin[width] = '\n'; fin[width+1] = 0;
strcpy(fin, start);
size_t max = width-strlen(end); size_t rptln = strlen(repeat);
strcpy(fin+max, end);
for (int i=strlen(start); i<width-strlen(end); i++)
fin[i] = repeat[i % rptln];
return fin;
}
bool endlineset = false;
char* lineset(char* txt){
llnode *head, *tail; head = tail = appendll(NULL, NULL);
size_t ct, len; ct = len = 0;
while (true){
tail->next = appendll(tail, line());
if (endlineset) break;
tail = tail->next;
ct++;
len += strlen(tail->str);
}
free(tail->next->str); free(tail->next); //.ELS cmd
tail = tail->next = head->next; // loop linked list
free(head);
char *lines, *pos;
lines = malloc((sizeof(char)*(len+1)));
// construct lines
int jmp;
for (pos = lines; pos-lines < len; pos += jmp, tail = tail->next){
char* str = tail->str + tail->loc;
char* loc = memchr(str, '\n', strlen(str));
jmp = (loc == NULL) ? 0 : loc-str+1;
memcpy(pos, str, jmp);
tail->loc += jmp;
}
llnode* last = NULL;
/*for (size_t i = 0; i<=ct; i++, last = tail, tail = tail->next){
free(tail->str);
free(last);
}*/
endlineset = false;
wsclean(lines);
return lines;
}
char* finlineset(char* txt){
char* str = malloc(sizeof(char)); str[0] = 0;
endlineset = true;
return str;
}
char* cmds[] = // MUST be sorted alphabetically
{"CT ", "ELS", "FIL ", "LD ", "LS", "W "};
char* (*call[])(char* txt) =
{center, finlineset, fillline, leader, lineset, setwidth};
char* cmd(void){
char* dat = popstrbuf(inbuf, chrfill('\n'));
size_t low = 0; size_t high = sizeof(cmds)/sizeof(*cmds); //len
char* proc = NULL;
while (high >= low){
int mid = ((unsigned int)low + (unsigned int)high) >> 1;
char* mval = cmds[mid];
int cmp = strncmp(dat, mval, strlen(mval));
if (cmp < 0) // dat < mval
high = mid - 1;
else if (cmp > 0)
low = mid + 1;
else{
proc = call[mid](dat+strlen(mval));
break;
}
}
free(dat);
return proc;
}
// normal typesetting
void wsclean(char* txt){
size_t disp = 0;
int firstws;
for (size_t i = 0; i == 0 || txt[i-1] != 0; i++){
if ( (txt[i] == ' ' || txt[i] == '\n') && txt[i-1] != ' '){
firstws = i;
}
if (txt[i] == '\n'){
disp += i-firstws;
}
txt[i-disp] = txt[i];
}
}
char* wordset(char* txt){
char* orig = txt;
size_t ws = width;
for (size_t i=0; txt[i] != 0; i++){ // turn into a nested loop
if (txt[i] == ' ') ws = i;
if (i >= width && ws != i){
txt += ws;
txt[0] = '\n';
ws = width;
i = 0;
}
}
wsclean(orig);
return orig;
}
char* typeset(void){
size_t len = chrfill('\n');
return wordset(popstrbuf(inbuf, len));
}
// a parser to choose when to typeset and when to run a command
char* line(void){
size_t sz;
if ( (sz = fillbuf(2)) == 0)
return "";
char* twobytes = peekstrbuf(inbuf, 0, 2); // .., .\n, or ^.?
if (sz == 1 || twobytes[1] == '\n') return typeset();
if (twobytes[0] == '.') popchrbuf(inbuf);
if (twobytes[0] == '.' && twobytes[1] != '.'){
char* data = cmd();
if (data) return data;
return line();
} else {
return typeset();
}
}
// orchestration
int main(int argc, char** argv){
int c;
in = getfile(argc, argv);
inbuf = newbuf(256);
if (in == NULL){
perror(argv[1]);
return 1;
}
char* out;
while ( (out = line())[0] != '\0'){
printf("%s",out);
// free(out);
}
fclose(in);
return 0;
}
|