forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psc.c
447 lines (390 loc) · 12 KB
/
psc.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* SC parse routine
*
* usage psc options
* options:
* -L Left justify strings. Default is right justify.
* -r Assemble data into rows first, not columns.
* -R n Increment by n between rows
* -C n Increment by n between columns
* -n n Length of the row (column) should be n.
* -s v Top left location in the spreadsheet should be v; eg, k5
* -d c Use c as the delimiter between the fields.
* -k Keep all delimiters - Default is strip multiple delimiters to 1.
* -f suppress 'format' lines in output
* -S Use strings vs numbers for numbers
* -P Use numbers only when there is no [-+eE] (plain numbers only)
*
* Author: Robert Bond
* Adjustments: Jeff Buhrt, Eric Putz and Chuck Martin
*/
#include "sc.h"
#include "version.h"
#define END 0
#define NUM 1
#define ALPHA 2
#define SPACE 3
#define EOL 4
#define PRINTF_CMD_ERR(x) ": Writing command \"" x "\": %s\n"
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
const char *progname;
static sheet_t cur_sheet;
sheet_t *sht = &cur_sheet;
static int scan(void);
static int getrow(const char *p);
static int getcol(const char *p);
static int curlen;
static int coff, roff;
static int first;
static int effr, effc;
/* option flags reset */
static int colfirst = FALSE;
static int leftadj = FALSE;
static int r0 = 0;
static int c0 = 0;
static int rinc = 1;
static int cinc = 1;
static int len = 20000;
static char delim1 = ' ';
static char delim2 = '\t';
static int strip_delim = TRUE;
static int drop_format = FALSE;
static int strnums = FALSE;
static int plainnums = FALSE;
static char token[1000];
void fatal(const char *str) {
fprintf(stderr, "psc: %s\n", str);
exit(1);
}
void error(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
((int (*)(FILE *, const char *, va_list))vfprintf)(stderr, fmt, ap);
va_end(ap);
}
int main(int argc, char **argv) {
sheet_t *sp = sht;
int exit_status = EXIT_SUCCESS;
int c;
int i, j;
char *p;
progname = argv[0];
while ((c = getopt(argc, argv, "rfLks:R:C:n:d:SPvh?")) != EOF) {
switch (c) {
case 'r':
colfirst = TRUE;
break;
case 'L':
leftadj = TRUE;
break;
case 's':
c0 = getcol(optarg);
r0 = getrow(optarg);
break;
case 'R':
rinc = atoi(optarg);
break;
case 'C':
cinc = atoi(optarg);
break;
case 'n':
len = atoi(optarg);
break;
case 'd':
delim1 = optarg[0];
delim2 = '\0';
break;
case 'k':
strip_delim = FALSE;
break;
case 'f':
drop_format = TRUE;
break;
case 'S':
strnums = TRUE;
break;
case 'P':
plainnums = TRUE;
break;
case 'v':
fprintf(stderr,"%s: %s\n", progname, rev);
return 0;
case 'h':
case '?':
printf("usage: psc options\n"
"options:\n"
" -L Left justify strings. Default is right justify.\n"
" -r Assemble data into rows first, not columns.\n"
" -R n Increment by n between rows\n"
" -C n Increment by n between columns\n"
" -n n Length of the row (column) should be n.\n"
" -s v Top left location in the spreadsheet should be v; eg, k5\n"
" -d c Use c as the delimiter between the fields.\n"
" -k Keep all delimiters - Default is strip multiple delimiters to 1.\n"
" -f suppress 'format' lines in output\n"
" -S Use strings vs numbers for numbers\n"
" -P Use numbers only when there is no [-+eE] (plain numbers only)\n");
return 2;
default:
return EXIT_FAILURE;
}
}
if (optind < argc) {
fprintf(stderr, "%s: %d more argument(s) than expected\n",
progname, argc - optind);
return EXIT_FAILURE;
}
/* setup the spreadsheet arrays */
if (checkbounds(sp, MINROWS, MINCOLS) < 0)
return EXIT_FAILURE;
curlen = 0;
sp->curcol = c0; coff = 0;
sp->currow = r0; roff = 0;
first = TRUE;
for (;;) {
effr = sp->currow + roff;
effc = sp->curcol + coff;
switch (scan()) {
case END:
if (drop_format) exit(exit_status);
for (i = 0; i < sp->maxcols; i++) {
if (sp->colfmt[i].fwidth) {
printf("format %s %d %d %d\n", coltoa(i),
sp->colfmt[i].fwidth + 1, sp->colfmt[i].precision, REFMTFIX);
}
}
exit(exit_status);
case NUM:
first = FALSE;
printf("let %s%d = %s\n", coltoa(effc), effr, token);
if (checkbounds(sp, effr, effc) < 0) {
fprintf(stderr, "Invalid column used: %s\n", coltoa(effc));
exit_status = EXIT_FAILURE;
continue;
}
i = 0;
j = 0;
p = token;
while (*p && *p != '.') {
p++; i++;
}
if (*p) {
p++; i++;
}
while (*p) {
p++; i++; j++;
}
{
int ow, nw;
ow = sp->colfmt[effc].fwidth - sp->colfmt[effc].precision;
if (sp->colfmt[effc].precision < j)
sp->colfmt[effc].precision = j;
if (sp->colfmt[effc].fwidth < i)
sp->colfmt[effc].fwidth = i;
/* now make sure:
* 1234.567890 (format 11 6)
* 1234567.890 (format 11 3)
* both show (format 14 6)
* (really it uses 15 6 to separate columns)
*/
if ((nw = i - j) > ow)
sp->colfmt[effc].fwidth += nw - (sp->colfmt[effc].fwidth - sp->colfmt[effc].precision);
}
break;
case ALPHA:
first = FALSE;
{
const char *cmd = leftadj ? "leftstring" : "rightstring";
printf("%s %s%d = \"%s\"\n", cmd, coltoa(effc), effr, token);
}
if (checkbounds(sp, effr, effc) < 0) {
fprintf(stderr, "Invalid column used: %s\n", coltoa(effc));
exit_status = EXIT_FAILURE;
continue;
}
i = strlen(token);
if (sp->colfmt[effc].fwidth > 1) {
sp->colfmt[effc].fwidth = i;
}
break;
case SPACE:
if (first && strip_delim)
break;
if (colfirst)
roff++;
else
coff++;
break;
case EOL:
curlen++;
roff = 0;
coff = 0;
first = TRUE;
if (colfirst) {
if (curlen >= len) {
sp->curcol = c0;
sp->currow += rinc;
curlen = 0;
} else {
sp->curcol += cinc;
}
} else {
if (curlen >= len) {
sp->currow = r0;
sp->curcol += cinc;
curlen = 0;
} else {
sp->currow += rinc;
}
}
break;
}
}
}
static int scan(void) {
int c;
char *p;
int founddigit;
p = token;
c = getchar();
if (c == EOF)
return END;
if (c == '\n')
return EOL;
if (c == delim1 || c == delim2) {
if (strip_delim) {
while ((c = getchar()) != EOF && (c == delim1 || c == delim2))
continue;
ungetc(c, stdin);
}
return SPACE;
}
if (c == '\"') {
while ((c = getchar()) != EOF && c != '\"' && c != '\n')
*p++ = c;
if (c != '\"')
ungetc(c, stdin);
*p = '\0';
return ALPHA;
}
while (c != delim1 && c != delim2 && c != '\n' && c != EOF) {
*p++ = c;
c = getchar();
}
*p = '\0';
ungetc(c, stdin);
p = token;
c = (unsigned char)*p;
founddigit = FALSE;
/*
* str_nums always returns numbers as strings
* plainnums returns 'numbers' with [-+eE] in them as strings
* lastprtnum makes sure a number ends in one of [0-9eE.]
*/
if (!strnums && (isdigit(c) || c == '.' || c == '-' || c == '+')) {
int lastprtnum = FALSE;
while (isdigit(c) || c == '.' ||
(!plainnums && (c == '-' || c == '+' || c == 'e' || c == 'E')))
{
if (isdigit(c))
lastprtnum = founddigit = TRUE;
else
if (!(c == '.' || c == 'e' || c == 'E'))
lastprtnum = FALSE;
c = (unsigned char)*p++;
}
if (c == '\0' && founddigit && lastprtnum)
return NUM;
else
return ALPHA;
}
return ALPHA;
}
/* turns [A-Z][A-Z] into a number */
static int getcol(const char *p) {
int col = 0;
if (!p)
return 0;
while (*p && !isalphachar(*p))
p++;
if (!*p)
return 0;
// XXX: use more than 2 letters?
col = toupperchar(*p++) - 'A';
if (isalphachar(*p))
col = (col + 1) * 26 + (toupperchar(*p++) - 'A');
return col;
}
/* given a string turn it into a row number */
static int getrow(const char *p) {
int row = 0;
if (!p)
return 0;
while (*p && !isdigitchar(*p))
p++;
while (isdigitchar(*p))
row = row * 10 + *p++ - '0';
return row;
}
/* turns a column number into [A-Z][A-Z] */
const char *coltoa(int col) {
static char rname[8];
char *p = rname;
// XXX: use more than 2 letters?
if (col > 25) {
*p++ = col / 26 + 'A' - 1;
col %= 26;
}
*p++ = col + 'A';
*p = '\0';
return rname;
}
#define GROWALLOC(ptr, curcount, nelem, msg, def) do { \
size_t i__ = (curcount), n__ = (nelem); \
void *newptr__ = realloc(ptr, n__ * sizeof(*(ptr))); \
if (newptr__ == NULL) { \
error(msg); \
return FALSE; \
} \
ptr = newptr__; \
while (i__ < n__) { (ptr)[i__++] = def; } \
} while (0)
static const char nowider[] = "The table cannot be any wider";
/*
* Grow the main && auxiliary tables (update maxrows/maxcols as needed)
* toprow &&/|| topcol tell us how big to become.
* we return TRUE if we could grow, FALSE if not....
*/
static int growtbl(sheet_t *sp, int toprow, int topcol) {
int curcols, currows, newrows, newcols;
newrows = currows = sp->maxrows;
newcols = curcols = sp->maxcols;
if (topcol >= curcols) {
if (topcol > ABSMAXCOLS) {
error(nowider);
return FALSE;
}
newcols = (topcol / GROWAMT + 1) * GROWAMT;
if (newcols > ABSMAXCOLS)
newcols = ABSMAXCOLS;
}
if (toprow >= currows) {
newrows = toprow + 1;
}
if (newcols > curcols) {
//colfmt_t def_colfmt = { FALSE, DEFWIDTH, DEFPREC, DEFREFMT };
colfmt_t def_colfmt = { 0, 0, 0, 0 };
GROWALLOC(sp->colfmt, curcols, newcols, nowider, def_colfmt);
}
sp->maxrows = newrows;
sp->maxcols = newcols;
return TRUE;
}
int checkbounds(sheet_t *sp, int row, int col) {
if (row >= sp->maxrows || col >= sp->maxcols)
return growtbl(sp, row, col) ? 1 : -1;
else
return 0;
}