summaryrefslogtreecommitdiff
path: root/columns/columns.c
blob: 7819c003019e5c9fc5d4f4993466b3038ca7c364 (plain)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769

/*
 *  \file columns.c
 *
 *  Time-stamp:        "2012-03-31 13:37:39 bkorb"
 *
 *  Columns Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
 *  Columns is free software.
 *  This file is part of AutoGen.
 *
 *  AutoGen copyright (c) 1992-2012 by Bruce Korb - all rights reserved
 *
 *  AutoGen 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 of the License, or
 *  (at your option) any later version.
 *
 *  AutoGen 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, see <http://www.gnu.org/licenses/>.
 */
#ifndef NUL
# define NUL '\0'
#endif

struct print_list {
    char**     papz;
    int        index;
};

typedef struct print_list tPrintList, *tpPrintList;

int maxEntryWidth = 0;
int fillColumnCt  = 0;

#define LINE_LIMIT 4096
char zLine[ LINE_LIMIT ];
char zFmtLine[ LINE_LIMIT ];

char**       papzLines  = (char**)NULL;
char const * pzLinePfx  = "";
char const * pzFirstPfx = NULL;
size_t       allocCt    = 0;
size_t       usedCt     = 0;
size_t       columnCt   = 0;
size_t       columnSz   = 0;
size_t       indentSize = 0;

/* = = = START-STATIC-FORWARD = = = */
static void
fserr_die(char const * fmt, ...);

static inline void *
malloc_or_die(size_t sz);

static char const *
construct_first_pfx(char const * f_indent);

static uint32_t
pad_indentation(char const * pzIndentArg, char const ** pfx);

static void
readLines(void);

static void
writeColumns(void);

static void
trim_last_separation(void);

static void
writeRows(void);

static int
emitWord(char const * word, size_t len, int col);

static void
writeFill(void);

static int
compProc(const void * p1, const void * p2);
/* = = = END-STATIC-FORWARD = = = */

int
main(int argc, char ** argv)
{
    (void)optionProcess( &columnsOptions, argc, argv );

    if (HAVE_OPT( INDENT )) {
        indentSize = pad_indentation( OPT_ARG(INDENT), &pzLinePfx);
        OPT_VALUE_WIDTH -= indentSize;

        pzFirstPfx = HAVE_OPT(FIRST_INDENT)
            ? construct_first_pfx( OPT_ARG(FIRST_INDENT))
            : pzLinePfx;
    }

    if (HAVE_OPT( LINE_SEPARATION ))
        OPT_VALUE_WIDTH -= strlen( OPT_ARG( LINE_SEPARATION ));

    if (HAVE_OPT( COL_WIDTH ))
        columnSz = OPT_VALUE_COL_WIDTH;

    if (HAVE_OPT( COLUMNS ))
        columnCt = OPT_VALUE_COLUMNS;

    if (OPT_VALUE_WIDTH <= 16)
        OPT_VALUE_WIDTH = 16;

    readLines();

    if (HAVE_OPT( SORT ))
        qsort( (void*)papzLines, usedCt, sizeof( char* ), &compProc );

    if (HAVE_OPT( BY_COLUMNS ))
         writeColumns();
    else if (HAVE_OPT(FILL))
        writeFill();
    else writeRows();

    return EXIT_SUCCESS;
}


static void
fserr_die(char const * fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    fprintf(stderr, "%s fserr %d (%s):  ", columnsOptions.pzProgName,
            errno, strerror(errno));
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(EXIT_FAILURE);
}

static inline void *
malloc_or_die(size_t sz)
{
    void * res = malloc(sz);
    if (res == NULL) {
        errno = ENOMEM;
        fserr_die("could not allocate %d bytes", sz);
    }
    return res;
}

static char const *
construct_first_pfx(char const * f_indent)
{
    static char const pad_fmt[] = "%%-%ds";
    // 24 > log10(0xFFFFFFFFFFFFFFFFULL)
    char pfx_buf[24 + sizeof(pad_fmt)];
    size_t firstSize = pad_indentation(f_indent, &pzFirstPfx);
    size_t len;
    char * res;

    /*
     *  If this first line exceeds the indentation size, then we
     *  need to append a newline and any indentation.
     */
    if (firstSize > indentSize) {
        size_t sep_len = HAVE_OPT(LINE_SEPARATION)
            ? strlen( OPT_ARG(LINE_SEPARATION)) : 0;
        len = firstSize + sep_len + indentSize + 3;
        sprintf(pfx_buf, pad_fmt, (int)firstSize);
    } else {
        len = indentSize + 3;
        sprintf(pfx_buf, pad_fmt, (int)indentSize);
    }

    res = malloc_or_die(len);
    snprintf(res, len, pfx_buf, pzFirstPfx);
    pzFirstPfx = res;

    if (firstSize > indentSize) {
        char * p = res + firstSize;

        if (HAVE_OPT( LINE_SEPARATION )) {
            len = strlen(OPT_ARG(LINE_SEPARATION));
            memcpy(p, OPT_ARG(LINE_SEPARATION), len);
            p  += len;
        }

        sprintf(p, "\n%s", pzLinePfx);
    }

    return res;
}

static uint32_t
pad_indentation(char const * pzIndentArg, char const ** pfx)
{
    char * pz;
    unsigned int cct;

    errno = 0;
    cct = (unsigned int)strtoul(pzIndentArg, &pz, 0);

    /*
     *  IF the indent argument is a number
     */
    if ((*pz == NUL) && (errno == 0) && (cct < OPT_VALUE_WIDTH)) {        
        char * p;

        /*
         *  Allocate a string to hold the line prefix
         */
        *pfx = p = malloc_or_die( (size_t)cct + 1 );

        /*
         *  Set it to a NUL terminated string of spaces
         */
        if (cct > 0)
            memset(p, ' ', (size_t)cct);
        p[cct] = NUL;

    } else {
        /*
         *  Otherwise, set the line prefix to whatever the string is.
         *  It will not be the empty string because that is handled
         *  as an indent count of zero and is ignored.
         */
        char const * p = pzIndentArg;
        *pfx = pzIndentArg;
        cct     =  0;

        for (;;) {
            /*
             *  Compute the length of the last line of the prefix.
             */
            switch (*p++) {
            case NUL:
                goto colsCounted;

            case '\t':
                cct += OPT_VALUE_TAB_WIDTH;
                cct -= (cct % OPT_VALUE_TAB_WIDTH);
                break;

            case '\n':
            case '\f':
            case '\r':
                cct = 0;
                break;

            default:
                cct++;
                break;

            case '\a':
                break;
            }
        } colsCounted:;
    }

    return cct;
}


static void
readLines(void)
{
    int   sepLen;

    if (HAVE_OPT(SEPARATION))
         sepLen = strlen(OPT_ARG(SEPARATION));
    else sepLen = 0;


    /*
     *  Read the input text, stripping trailing white space
     */
    for (;;) {
        char*   pzL;
        char*   pzText = fgets(zLine, (int)sizeof( zLine ), stdin);
        size_t  len;

        if (pzText == NULL)
            break;

        /*
         *  Trim off trailing white space.
         */
        len = strlen( pzText );
        pzText += len;
        while (isspace(pzText[-1])) {
            if (--pzText == zLine) {
                if (HAVE_OPT(FILL))
                    break;
                goto next_line;
            }
            len--;
        }

        *pzText = NUL;

        /*
         *  IF the input lines are to be reformatted,
         *  THEN the length is the result of the sprintf
         *  Else, compute the length.
         */
        if (HAVE_OPT(FORMAT)) {
            pzText = zFmtLine;
            len = snprintf(zFmtLine, sizeof(zFmtLine), OPT_ARG(FORMAT), zLine);
        } else {
            pzText = zLine;
        }

        /*
         *  Allocate a string and space in the pointer array.
         */
        len += sepLen + 1;
        pzL = (char*)malloc_or_die( len );
        if (++usedCt > allocCt) {
            allocCt += 128;
            papzLines = (char**)realloc( (void*)papzLines,
                                         sizeof( char* ) * allocCt );
        }
        papzLines[ usedCt-1 ] = pzL;

        /*
         *  Copy the text and append the separation character
         */
        strcpy( pzL, pzText );

        /*
         *  Initially, all strings have the separator,
         *  the entries may get reordered.
         */
        if (sepLen > 0)
            strcat(pzL, OPT_ARG(SEPARATION));

        if ((int)len > maxEntryWidth)
            maxEntryWidth = len;
    next_line:;
    }

    if (maxEntryWidth == 0) {
        fputs( "columns warning:  no input text was read\n", stderr );
        exit( EXIT_SUCCESS );
    }

    /*
     *  Set the line width to the amount of space we have to play with.
     */
    if ((OPT_VALUE_WIDTH < maxEntryWidth) && (! HAVE_OPT(FILL)))
        OPT_VALUE_WIDTH = maxEntryWidth;

    /*
     *  If we do not have a column size set,
     *  then figure out what it must be.
     */
    if (columnSz == 0) {
        /*
         *  IF the column count has not been set,
         *  THEN compute it.
         */
        if (columnCt == 0)
            columnCt = OPT_VALUE_WIDTH / maxEntryWidth;

        /*
         *  IF there are to be multiple columns, ...
         */
        if (columnCt > 1) {
            int  spreadwidth = OPT_VALUE_WIDTH - maxEntryWidth;
            int  sz = spreadwidth / (columnCt-1);

            /*
             *  Either there is room for added space,
             *  or we must (possibly) reduce the number of columns
             */
            if (sz >= maxEntryWidth)
                columnSz = sz;
            else {
                columnCt = (spreadwidth / maxEntryWidth) + 1;
                if (columnCt > 1)
                     columnSz = spreadwidth / (columnCt - 1);
                else columnSz = OPT_VALUE_WIDTH;
            }
        }
    }

    /*
     *  Otherwise, the column size has been set.  Ensure it is sane.
     */
    else {
        bool compute_val = (columnCt == 0);

        /*
         *  Increase the column size to the width of the widest entry
         */
        if (maxEntryWidth > (int)columnSz)
            columnSz = maxEntryWidth;

        /*
         *  IF     we have not been provided a column count
         *    *OR* we are set to overfill the output line,
         *  THEN compute the number of columns.
         */
        if (! compute_val) {
            unsigned long width = maxEntryWidth + (columnSz * (columnCt-1));
            compute_val = width > (unsigned)OPT_VALUE_WIDTH;
        }
        if (compute_val)
            columnCt = ((OPT_VALUE_WIDTH - maxEntryWidth) / columnSz) + 1;
    }

    /*
     *  Ensure that any "spread" we added to the column size
     *  does not exceed the parameterized limit.
     */
    if (   HAVE_OPT( SPREAD )
           && ((maxEntryWidth + OPT_VALUE_SPREAD - 1) < (int)columnSz))
        columnSz = maxEntryWidth + OPT_VALUE_SPREAD - 1;
}


static void
writeColumns(void)
{
    char zFmt[ 12 ];
    int  colCt, rowCt, col, row;
    tpPrintList pPL;

    colCt = columnCt;
    snprintf(zFmt, sizeof(zFmt), "%%-%ds", (int)columnSz);

    if (colCt == 1) {
        writeRows();
        return;
    }

    pPL = (tpPrintList)malloc_or_die( colCt * sizeof( tPrintList ));

    /*
     *  This "loop" is normally executed half way through and exited.
     *  IF, however, we would  produce an empty final column,
     *  we will reduce our column count and line width and then
     *  try the top-of-column pointer computation again.
     *
     *  The problem solved here is that sometimes, when the
     *  number of entries in a row is greater than the number of rows,
     *  it is possible that all the entries that would have been
     *  in the last column are, instead, essentially put on the
     *  last row.  That will leave the final column empty.
     *  We could regroup at that point and spread the columns some more,
     *  but, if done, is an exercise for later.
     */
    for (;;) {
        int  rem;
        int  fsz;

        rowCt = (usedCt/colCt) + ((usedCt % colCt) ? 1 : 0);

        /*
         *  FOR each column, compute the address of the pointer to
         *  the string at the top of the column, and the index of
         *  that entry.
         */
        for (col = 0; col < colCt ; col++) {
            pPL[col].papz  = papzLines + (col * rowCt);
            pPL[col].index = col * rowCt;
        }

        /*
         *  IF the final column is not empty,
         *  THEN break out and start printing.
         */
        if (pPL[colCt-1].index < (int)usedCt)
            break;

        /*
         *  The last column is blank, so we reduce our column count,
         *  even if the user specified a count!!
         */
        colCt--;
        rem   = OPT_VALUE_WIDTH - (colCt * maxEntryWidth);

        if ((rem == 0) || (colCt < 2) || (columnSz > 0))
            fsz = maxEntryWidth;
        else
            fsz = maxEntryWidth + (rem / (colCt-1));
        snprintf( zFmt, sizeof(zFmt), "%%-%ds", fsz );
    }

    /*
     *  Now, actually print each row...
     */
    for ( row = 0 ;; ) {
        char*  pzL;
        char*  pzE;

        if (pzLinePfx != NULL)
            fputs( pzLinePfx, stdout );

        /*
         *  Increment the index of the current entry in the last column.
         *  IF it goes beyond the end of the entries in use,
         *  THEN reduce our column count.
         */
        if ((pPL[colCt-1].index)++ >= (int)usedCt)
            colCt--;

        /*
         *  Get the address of the string in the last column.
         */
        pzE = *(pPL[colCt-1].papz++);

        col = 0;

        /*
         *  FOR every column except the last,
         *     print the entry with the width format
         *
         *  No need to worry about referring to a non-existent entry.  Only
         *  the last column might have that problem, and we addressed it above
         *  where the column count got decremented.
         */
        while (++col < colCt) {
            pzL = *(pPL[col-1].papz++);
            fprintf( stdout, zFmt, pzL );
            free( (void*)pzL );
        }

        /*
         *  See if we are on the last row. If so, then this is the last entry.
         *  Strip any separation characters, emit the entry and break out.
         */
        if (++row == rowCt) {
            /*
             *  IF we have a separator,
             *  THEN remove it from the last entry.
             */
            if (HAVE_OPT( SEPARATION )) {
                char* pz = pzE + strlen( pzE )
                         - strlen( OPT_ARG(SEPARATION));
                *pz = NUL;
            }

            fputs(pzE, stdout);
            if (HAVE_OPT(ENDING))
                fputs(OPT_ARG(ENDING), stdout);

            putc( '\n', stdout );
            break;
        }

        /*
         *  Print the last entry on the line, without the width format.
         *  If we have line separation (which does not apply to the last
         *  line), then emit those characters, too.
         */
        fputs( pzE, stdout );
        if (HAVE_OPT( LINE_SEPARATION ))
            fputs( OPT_ARG( LINE_SEPARATION ), stdout );

        putc( '\n', stdout );
        free( (void*)pzE );
    }

    free(pPL);
}

static void
trim_last_separation(void)
{
    char* pz = papzLines[ usedCt-1 ];
    pz += strlen(pz) - strlen( OPT_ARG(SEPARATION));
    *pz = NUL;
}

static void
writeRows(void)
{
    char zFmt[32];
    int  colCt;

    colCt = columnCt;
    snprintf(zFmt, sizeof(zFmt), "%%-%ds", (int)columnSz);

    if (HAVE_OPT( SEPARATION ))
        trim_last_separation();

    if (pzFirstPfx != NULL) {
        fputs( pzFirstPfx, stdout );
        pzFirstPfx = pzLinePfx;
    }

    {
        char**  ppzLL = papzLines;
        size_t  left  = usedCt;
        int     lnNo  = 0;

        /*
         *  FOR every entry we are to emit, ...
         */
        for (;;) {
            char* pzL = *ppzLL++;

            /*
             *  IF this is the last entry,
             *  THEN emit it and a new line and break out
             */
            if (--left <= 0) {
                fputs(pzL, stdout);
                if (HAVE_OPT(ENDING))
                    fputs(OPT_ARG(ENDING), stdout);

                putc('\n', stdout);
                free((void*)pzL);
                break;
            }

            /*
             *  IF the count of entries on this line is still less
             *     than the number of columns,
             *  THEN emit the padded entry
             *  ELSE ...
             */
            if (++lnNo < colCt)
                fprintf( stdout, zFmt, pzL );

            else {
                lnNo = 0;
                /*
                 *  Last entry on the line.  Emit the string without padding.
                 *  IF we have a line separation string, emit that too.
                 */
                fputs( pzL, stdout );
                if (HAVE_OPT( LINE_SEPARATION ))
                    fputs( OPT_ARG( LINE_SEPARATION ), stdout );

                putc( '\n', stdout );

                /*
                 *  Start the next line with any required indentation
                 */
                if (pzFirstPfx != NULL) {
                    fputs( pzFirstPfx, stdout );
                    pzFirstPfx = pzLinePfx;
                }
            }

            free( (void*)pzL );
        }
    }
}

static int
emitWord(char const * word, size_t len, int col)
{
    static int ended_with_period = 0;

    if (col > 0) {
        if ((int)len >= (OPT_VALUE_WIDTH - col)) {
            putc('\n', stdout);
            if (pzLinePfx != NULL)
                fputs(pzLinePfx, stdout);
            col = 0;

        } else {
            if (ended_with_period) {
                putc(' ', stdout);
                col++;
            }
            putc(' ', stdout);
            col++;
        }
    }

    fwrite(word, len, 1, stdout);
    col += len;
    ended_with_period = (word[len - 1] == '.');

    return col;
}

/*
 *  writeFill -- fill the output.  Pack together as much as will fit
 *  on each line.
 */
static void
writeFill(void)
{
    char**  ppzLL = papzLines;
    size_t  left  = usedCt;
    int     colNo = 0;

    if (HAVE_OPT( SEPARATION ))
        trim_last_separation();

    if (pzFirstPfx != NULL)
        fputs(pzFirstPfx, stdout);

    /*
     *  FOR every entry we are to emit, ...
     */
    while (left-- > 0) {
        char* pzL = *ppzLL;

        while (isspace(*pzL))  pzL++;

        /*
         *  Blank lines are magical and trigger a blank line in output.
         */
        if (*pzL == NUL) {
            if (! HAVE_OPT(SORT)) {
                if (colNo > 0) /* guard against multiple blank lines */
                    putc('\n', stdout);
                putc('\n', stdout);
                colNo = -2;
            }

            free(*(ppzLL++));
            continue;
        }

        /*
         *  We are going to emit some output.  Make sure we're indented.
         */
        if (colNo < 0) {
            if (pzLinePfx != NULL)
                fputs(pzLinePfx, stdout);
            colNo = 0;
        }

        do {
            size_t tknlen;

            for (tknlen = 0; pzL[tknlen] != NUL; tknlen++)
                if (isspace(pzL[tknlen]))
                    break;
            colNo = emitWord(pzL, tknlen, colNo);
            pzL += tknlen;
            while (isspace(*pzL))  pzL++;
        } while (*pzL != NUL);

        free(*(ppzLL++));
    }

    if (HAVE_OPT(ENDING) && (left == 0))
        fputs(OPT_ARG(ENDING), stdout);

    putc('\n', stdout);
}


/*
 *  Line comparison procedure
 */
static int
compProc(const void * p1, const void * p2)
{
    char const * pz1 = *(char* const*)p1;
    char const * pz2 = *(char* const*)p2;
    return strcmp(pz1, pz2);
}
/*
 * Local Variables:
 * mode: C
 * c-file-style: "stroustrup"
 * indent-tabs-mode: nil
 * End:
 * end of columns/columns.c */