• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ascii-boxes / boxes / 11084634932

28 Sep 2024 02:22PM UTC coverage: 87.312% (-1.6%) from 88.939%
11084634932

push

github

tsjensen
Updates based on @msaft's comments

3132 of 3809 branches covered (82.23%)

Branch coverage included in aggregate %.

5091 of 5609 relevant lines covered (90.76%)

175335.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.69
/src/boxes.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
4
 *
5
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6
 * License, version 3, as published by the Free Software Foundation.
7
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
9
 * details.
10
 * You should have received a copy of the GNU General Public License along with this program.
11
 * If not, see <https://www.gnu.org/licenses/>.
12
 *
13
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14
 */
15

16
#include "config.h"
17

18
#ifndef __MINGW32__
19
#include <ncurses.h>
20
#endif
21
#include <locale.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <uniconv.h>
26
#include <unistd.h>
27
#ifdef _WIN32
28
#include <windows.h>
29
#endif
30

31
#include "boxes.h"
32
#include "bxstring.h"
33
#include "cmdline.h"
34
#include "discovery.h"
35
#include "generate.h"
36
#include "input.h"
37
#include "list.h"
38
#include "logging.h"
39
#include "parsing.h"
40
#include "query.h"
41
#include "remove.h"
42
#include "shape.h"
43
#include "tools.h"
44
#include "unicode.h"
45

46

47

48
/*       _\|/_
49
         (o o)
50
 +----oOO-{_}-OOo------------------------------------------------------------+
51
 |                    G l o b a l   V a r i a b l e s                        |
52
 +--------------------------------------------------------------------------*/
53

54
design_t *designs = NULL;            /* available box designs */
55
int num_designs = 0;                 /* number of designs after parsing */
56

57
opt_t opt;                           /* command line options */
58

59
input_t input;                       /* input lines */
60

61
int color_output_enabled;            /* Flag indicating if ANSI color codes should be printed (1) or not (0) */
62

63

64

65
/*       _\|/_
66
         (o o)
67
 +----oOO-{_}-OOo------------------------------------------------------------+
68
 |                           F u n c t i o n s                               |
69
 +--------------------------------------------------------------------------*/
70

71

72
static int build_design(design_t **adesigns, const char *cld)
8✔
73
/*
74
 *  Build a box design.
75
 *
76
 *      adesigns    Pointer to global designs list
77
 *      cld         the W shape as specified on the command line
78
 *
79
 *  Builds the global design list containing only one design which was
80
 *  built from the -c command line definition.
81
 *
82
 *  RETURNS:  != 0   on error (out of memory)
83
 *            == 0   on success
84
 *
85
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
86
 */
87
{
88
    design_t *dp;                        /* pointer to design to be created */
89
    sentry_t *c;                         /* pointer to current shape */
90
    int i;
91
    int rc;
92

93
    *adesigns = (design_t *) calloc(1, sizeof(design_t));
8✔
94
    if (*adesigns == NULL) {
8✔
95
        perror(PROJECT);
×
96
        return 1;
×
97
    }
98
    dp = *adesigns;                      /* for readability */
8✔
99

100
    dp->name = "<Command Line Definition>";
8✔
101
    dp->aliases = (char **) calloc(1, sizeof(char *));
8✔
102
    dp->sample = bxs_from_ascii("n/a");
8✔
103
    dp->indentmode = DEF_INDENTMODE;
8✔
104
    dp->padding[BLEF] = 1;
8✔
105
    dp->defined_in = bxs_from_ascii("(command line)");
8✔
106

107
    dp->tags = (char **) calloc(2, sizeof(char *));
8✔
108
    dp->tags[0] = "transient";
8✔
109

110
    /* We always use UTF-8, which is correct for Linux and MacOS, and for modern Windows configured for UTF-8. */
111
    uint32_t *cld_u32 = u32_strconv_from_arg(cld, "UTF-8");
8✔
112
    bxstr_t *cldW = bxs_from_unicode(cld_u32);
8✔
113
    BFREE(cld_u32);
8✔
114

115
    dp->shape[W].name = W;
8✔
116
    dp->shape[W].height = 1;
8✔
117
    dp->shape[W].width = cldW->num_columns;
8✔
118
    dp->shape[W].elastic = 1;
8✔
119
    rc = genshape(dp->shape[W].width, dp->shape[W].height, &(dp->shape[W].chars), &(dp->shape[W].mbcs));
8✔
120
    if (rc) {
8!
121
        return rc;
×
122
    }
123
    bxs_free(dp->shape[W].mbcs[0]);
8✔
124
    dp->shape[W].mbcs[0] = cldW;
8✔
125
    strcpy(dp->shape[W].chars[0], cld);
8✔
126

127
    for (i = 0; i < NUM_SHAPES; ++i) {
136✔
128
        c = dp->shape + i;
128✔
129

130
        if (i == NNW || i == NNE || i == WNW || i == ENE || i == W
160✔
131
                || i == WSW || i == ESE || i == SSW || i == SSE) {
92✔
132
                    continue;
72✔
133
        }
134

135
        switch (i) {
56✔
136
            case NW:
8✔
137
            case SW:
138
                c->width = dp->shape[W].width;
16✔
139
                c->height = 1;
16✔
140
                c->elastic = 0;
16✔
141
                break;
16✔
142

143
            case NE:
8✔
144
            case SE:
145
                c->width = 1;
16✔
146
                c->height = 1;
16✔
147
                c->elastic = 0;
16✔
148
                break;
16✔
149

150
            case N:
12✔
151
            case S:
152
            case E:
153
                c->width = 1;
24✔
154
                c->height = 1;
24✔
155
                c->elastic = 1;
24✔
156
                break;
24✔
157

158
            default:
159
                fprintf(stderr, "%s: internal error\n", PROJECT);
×
160
                return 1;
×
161
        }
162
        c->name = i;
56✔
163

164
        rc = genshape(c->width, c->height, &(c->chars), &(c->mbcs));
56✔
165
        if (rc) {
56✔
166
            return rc;
×
167
        }
168
    }
28✔
169

170
    dp->maxshapeheight = 1;
8✔
171
    dp->minwidth = dp->shape[W].width + 2;
8✔
172
    dp->minheight = 3;
8✔
173

174
    return 0;
8✔
175
}
4✔
176

177

178

179
/**
180
 * Process command line options and store the result in the global `opt` struct. May exit the program.
181
 */
182
static void handle_command_line(int argc, char *argv[])
802✔
183
{
184
    log_debug(__FILE__, MAIN, "Processing Command Line ...\n");
802✔
185

186
    opt_t *parsed_opts = process_commandline(argc, argv);
802✔
187
    if (parsed_opts == NULL) {
802✔
188
        exit(EXIT_FAILURE);
20✔
189
    }
190
    if (parsed_opts->help) {
782✔
191
        usage_long(stdout);
2✔
192
        exit(EXIT_SUCCESS);
2✔
193
    }
194
    if (parsed_opts->version_requested) {
780!
195
        printf("%s version %s\n", PROJECT, VERSION);
×
196
        exit(EXIT_SUCCESS);
×
197
    }
198
    memcpy(&opt, parsed_opts, sizeof(opt_t));
780✔
199
    BFREE(parsed_opts);
780✔
200
}
780✔
201

202

203

204
/**
205
 * Parse config file(s), then reset design pointer. May exit the program.
206
 */
207
static void handle_config_parsing()
780✔
208
{
209
    bxstr_t *config_file = discover_config_file(0);
780✔
210
    if (config_file == NULL) {
780✔
211
        exit(EXIT_FAILURE);
6✔
212
    }
213
    if (opt.cld == NULL) {
774✔
214
        size_t r_num_designs = 0;
766✔
215
        designs = parse_config_files(config_file, &r_num_designs);
766✔
216
        if (designs == NULL) {
766✔
217
            exit(EXIT_FAILURE);
6✔
218
        }
219
        num_designs = (int) r_num_designs;
760✔
220
    }
380✔
221
    else {
222
        int rc = build_design(&designs, opt.cld);
8✔
223
        if (rc) {
8✔
224
            exit(EXIT_FAILURE);
×
225
        }
226
        num_designs = 1;
8✔
227
    }
228
    BFREE (opt.design);
768✔
229
    opt.design = designs;
768✔
230
}
768✔
231

232

233

234
/**
235
 * Adjust box size to command line specification.
236
 * Increase box width/height by width/height of empty sides in order to match appearance of box with the user's
237
 * expectations (if -s).
238
 */
239
static void apply_expected_size()
702✔
240
{
241
    if (opt.reqheight > (long) opt.design->minheight) {
702✔
242
        opt.design->minheight = opt.reqheight;
120✔
243
    }
60✔
244
    if (opt.reqwidth > (long) opt.design->minwidth) {
702✔
245
        opt.design->minwidth = opt.reqwidth;
120✔
246
    }
60✔
247
    if (opt.reqwidth) {
702✔
248
        if (empty_side(opt.design->shape, BRIG)) {
130✔
249
            opt.design->minwidth += opt.design->shape[SE].width;
4✔
250
        }
2✔
251
        if (empty_side(opt.design->shape, BLEF)) {
130✔
252
            opt.design->minwidth += opt.design->shape[NW].width;
4✔
253
        }
2✔
254
    }
65✔
255
    if (opt.reqheight) {
702✔
256
        if (empty_side(opt.design->shape, BTOP)) {
130✔
257
            opt.design->minheight += opt.design->shape[NW].height;
2✔
258
        }
1✔
259
        if (empty_side(opt.design->shape, BBOT)) {
130✔
260
            opt.design->minheight += opt.design->shape[SE].height;
2✔
261
        }
1✔
262
    }
65✔
263
}
702✔
264

265

266

267
/**
268
 * Read all input lines and store the result in the global `input` structure. May exit the program.
269
 */
270
static void handle_input()
848✔
271
{
272
    log_debug(__FILE__, MAIN, "Reading all input ...\n");
848✔
273

274
    input_t *raw_input = NULL;
848✔
275
    if (opt.mend != 0) {
848✔
276
        raw_input = read_all_input();
702✔
277
        if (raw_input == NULL) {
702!
278
            exit(EXIT_FAILURE);
×
279
        }
280
    }
351✔
281
    if (analyze_input(raw_input ? raw_input : &input)) {
848✔
282
        exit(EXIT_FAILURE);
×
283
    }
284
    if (raw_input) {
848✔
285
        memcpy(&input, raw_input, sizeof(input_t));
702✔
286
        BFREE(raw_input);
702✔
287
    }
351✔
288

289
    if (is_debug_logging(MAIN)) {
848✔
290
        log_debug(__FILE__, MAIN, "Effective encoding: %s\n", encoding);
6✔
291
        print_input_lines(NULL);
6✔
292
    }
3✔
293
    if (input.num_lines == 0) {
848✔
294
        exit(EXIT_SUCCESS);
×
295
    }
296
}
848✔
297

298

299

300
/**
301
 * Adjust box size to fit requested padding value.
302
 * Command line-specified box size takes precedence over padding.
303
 */
304
static void adjust_size_and_padding()
848✔
305
{
306
    for (int i = 0; i < NUM_SIDES; ++i) {
4,240✔
307
        if (opt.padding[i] > -1) {
3,392✔
308
            opt.design->padding[i] = opt.padding[i];
54✔
309
        }
27✔
310
    }
1,696✔
311

312
    size_t pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
848✔
313
    if (pad > 0) {
848✔
314
        pad += input.num_lines;
176✔
315
        pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
176✔
316
        if (pad > opt.design->minheight) {
176✔
317
            if (opt.reqheight) {
168✔
318
                for (int i = 0; i < (int) (pad - opt.design->minheight); ++i) {
14✔
319
                    if (opt.design->padding[i % 2 ? BBOT : BTOP]) {
12✔
320
                        opt.design->padding[i % 2 ? BBOT : BTOP] -= 1;
10✔
321
                    } else if (opt.design->padding[i % 2 ? BTOP : BBOT]) {
7!
322
                        opt.design->padding[i % 2 ? BTOP : BBOT] -= 1;
×
323
                    } else {
324
                        break;
2✔
325
                    }
326
                }
5✔
327
            }
2✔
328
            else {
329
                opt.design->minheight = pad;
164✔
330
            }
331
        }
84✔
332
    }
88✔
333

334
    pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
848✔
335
    if (pad > 0) {
848✔
336
        pad += input.maxline;
734✔
337
        pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
734✔
338
        if (pad > opt.design->minwidth) {
734✔
339
            if (opt.reqwidth) {
620✔
340
                for (int i = 0; i < (int) (pad - opt.design->minwidth); ++i) {
38✔
341
                    if (opt.design->padding[i % 2 ? BRIG : BLEF]) {
32✔
342
                        opt.design->padding[i % 2 ? BRIG : BLEF] -= 1;
26✔
343
                    } else if (opt.design->padding[i % 2 ? BLEF : BRIG]) {
19✔
344
                        opt.design->padding[i % 2 ? BLEF : BRIG] -= 1;
2!
345
                    } else {
1✔
346
                        break;
4✔
347
                    }
348
                }
14✔
349
            }
5✔
350
            else {
351
                opt.design->minwidth = pad;
610✔
352
            }
353
        }
310✔
354
    }
367✔
355
}
848✔
356

357

358

359
/**
360
 * Generate box. May exit the program.
361
 */
362
static void handle_generate_box()
526✔
363
{
364
    log_debug(__FILE__, MAIN, "Generating Box ...\n");
526✔
365

366
    sentry_t *thebox = (sentry_t *) calloc(NUM_SIDES, sizeof(sentry_t));
526✔
367
    if (thebox == NULL) {
526✔
368
        perror(PROJECT);
×
369
        exit(EXIT_FAILURE);
×
370
    }
371
    int rc = generate_box(thebox);
526✔
372
    if (rc) {
526!
373
        exit(EXIT_FAILURE);
×
374
    }
375
    output_box(thebox);
526✔
376
}
526✔
377

378

379

380
/**
381
 * Remove box. May exit the program.
382
 */
383
static void handle_remove_box()
321✔
384
{
385
    log_debug(__FILE__, MAIN, "Removing Box ...\n");
321✔
386

387
    if (opt.killblank == -1) {
321✔
388
        if (empty_side(opt.design->shape, BTOP) && empty_side(opt.design->shape, BBOT)) {
174✔
389
            opt.killblank = 0;
22✔
390
        } else {
11✔
391
            opt.killblank = 1;
152✔
392
        }
393
    }
87✔
394
    int rc = remove_box();
321✔
395
    if (rc) {
320✔
396
        exit(EXIT_FAILURE);
×
397
    }
398
    rc = apply_substitutions(&input, 1);
320✔
399
    if (rc) {
320!
400
        exit(EXIT_FAILURE);
×
401
    }
402
    output_input(opt.mend > 0);
320✔
403
}
320✔
404

405

406

407
#ifndef __MINGW32__
408
    /* These two functions are actually declared in term.h, but for some reason, that can't be included. */
409
    extern NCURSES_EXPORT(int) setupterm(NCURSES_CONST char *, int, int *);
410
    extern NCURSES_EXPORT(int) tigetnum(NCURSES_CONST char *);
411
#endif
412

413
static int terminal_has_colors()
778✔
414
{
415
    int result = 0;
778✔
416
    char *termtype = getenv("TERM");
778✔
417
    #ifdef __MINGW32__
418
        result = 1; /* On Windows, we always assume color capability. */
419
        UNUSED(termtype);
420
    #else
421
        if (termtype != NULL && setupterm(termtype, STDOUT_FILENO, NULL) == OK && tigetnum("colors") >= 8) {
778!
422
            result = 1;
778✔
423
        }
389✔
424
    #endif
425

426
    if (is_debug_logging(MAIN)) {
778✔
427
        #ifdef __MINGW32__
428
            int num_colors = 1;
429
        #else
430
            int num_colors = result ? tigetnum("colors") : 0;
6✔
431
        #endif
432
        log_debug(__FILE__, MAIN, "Terminal \"%s\" %s colors (number of colors = %d).\n",
6!
433
                termtype != NULL ? termtype : "(null)", result ? "has" : "does NOT have", num_colors);
3!
434
    }
3✔
435
    return result;
778✔
436
}
437

438

439

440
static int check_color_support(int opt_color)
780✔
441
{
442
    int result = 0;
780✔
443
    if (opt_color == force_ansi_color) {
780✔
444
        result = 1;
2✔
445
    }
1✔
446
    else if (opt_color == color_from_terminal) {
778✔
447
        result = terminal_has_colors();
778✔
448
    }
389✔
449

450
    log_debug(__FILE__, MAIN, "Color support %sabled\n", result ? "\x1b[92mEN\x1b[0m" : "DIS");
780!
451
    return result;
780✔
452
}
453

454

455

456
/**
457
 * Switch from default "C" encoding to system encoding.
458
 */
459
static void activateSystemEncoding()
802✔
460
{
461
    #ifdef _WIN32
462
        SetConsoleOutputCP(CP_ACP);
463
        SetConsoleCP(CP_ACP);  
464
        /* If it should one day turn out that this doesn't have the desired effect, try setlocale(LC_ALL, ".UTF8"). */
465
    #else
466
        setlocale(LC_ALL, "");
802✔
467
    #endif
468
}
802✔
469

470

471

472
/*       _\|/_
473
         (o o)
474
 +----oOO-{_}-OOo------------------------------------------------------------+
475
 |                       P r o g r a m   S t a r t                           |
476
 +--------------------------------------------------------------------------*/
477

478
int main(int argc, char *argv[])
785✔
479
{
480
    int rc;                           /* general return code */
481
    int saved_designwidth;            /* opt.design->minwith backup, used for mending */
482
    int saved_designheight;           /* opt.design->minheight backup, used for mending */
483

484
    log_debug(__FILE__, MAIN, "BOXES STARTING ...\n"); /* TODO This line will never execute, because debug not on yet */
785✔
485

486
    /* Temporarily set the system encoding, for proper output of --help text etc. */
487
    activateSystemEncoding();
785✔
488
    encoding = locale_charset();
785✔
489

490
    handle_command_line(argc, argv);
785✔
491

492
    /* Store system character encoding */
493
    encoding = check_encoding(opt.encoding, locale_charset());
774✔
494
    log_debug(__FILE__, MAIN, "Character Encoding = %s\n", encoding);
774✔
495

496
    color_output_enabled = check_color_support(opt.color);
774✔
497

498
    handle_config_parsing();
774✔
499

500
    /* If "-l" option was given, list designs and exit. */
501
    if (opt.l) {
768✔
502
        rc = list_designs();
48✔
503
        exit(rc);
48✔
504
    }
505

506
    /* If "-q" option was given, print results of tag query and exit. */
507
    if (opt.query != NULL && opt.query[0] != NULL) {
720!
508
        rc = query_by_tag();
18✔
509
        exit(rc);
18✔
510
    }
511

512
    apply_expected_size();
702✔
513
    if (opt.indentmode) {
702✔
514
        opt.design->indentmode = opt.indentmode;
4✔
515
    }
2✔
516
    saved_designwidth = opt.design->minwidth;
702✔
517
    saved_designheight = opt.design->minheight;
702✔
518

519
    do {
351✔
520
        if (opt.mend == 1) {  /* Mending a box works in two phases: */
848✔
521
            opt.r = 0;        /* opt.mend == 2: remove box          */
146✔
522
        }
73✔
523
        --opt.mend;           /* opt.mend == 1: add it back         */
848✔
524
        opt.design->minwidth = saved_designwidth;
848✔
525
        opt.design->minheight = saved_designheight;
848✔
526

527
        handle_input();
848✔
528

529
        adjust_size_and_padding();
848✔
530

531
        if (opt.r) {
848✔
532
            handle_remove_box();
322✔
533
        }
161✔
534
        else {
535
            handle_generate_box();
526✔
536
        }
537
    } while (opt.mend > 0);
847✔
538

539
    return EXIT_SUCCESS;
701✔
540
}
541

542
/* vim: set sw=4: */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc