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

ascii-boxes / boxes / 4940444976

10 May 2023 07:09PM UTC coverage: 81.882% (+0.09%) from 81.792%
4940444976

push

github

Thomas Jensen
Fix color test cases for GitHub Actions without TERM

2159 of 2905 branches covered (74.32%)

Branch coverage included in aggregate %.

3341 of 3812 relevant lines covered (87.64%)

6280.68 hits per line

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

84.55
/src/boxes.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2023 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
#include <ncurses.h>
19
#include <locale.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <uniconv.h>
24
#include <unistd.h>
25

26
#include "boxes.h"
27
#include "cmdline.h"
28
#include "discovery.h"
29
#include "generate.h"
30
#include "input.h"
31
#include "list.h"
32
#include "parsing.h"
33
#include "query.h"
34
#include "remove.h"
35
#include "tools.h"
36
#include "unicode.h"
37

38

39

40
/*       _\|/_
41
         (o o)
42
 +----oOO-{_}-OOo------------------------------------------------------------+
43
 |                    G l o b a l   V a r i a b l e s                        |
44
 +--------------------------------------------------------------------------*/
45

46
design_t *designs = NULL;            /* available box designs */
47
int num_designs = 0;                 /* number of designs after parsing */
48

49
opt_t opt;                           /* command line options */
50

51
input_t input;                       /* input lines */
52

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

55

56

57
/*       _\|/_
58
         (o o)
59
 +----oOO-{_}-OOo------------------------------------------------------------+
60
 |                           F u n c t i o n s                               |
61
 +--------------------------------------------------------------------------*/
62

63

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

85
    *adesigns = (design_t *) calloc(1, sizeof(design_t));
4✔
86
    if (*adesigns == NULL) {
4!
87
        perror(PROJECT);
×
88
        return 1;
×
89
    }
90
    dp = *adesigns;                      /* for readability */
4✔
91

92
    dp->name = "<Command Line Definition>";
4✔
93
    dp->aliases = (char **) calloc(1, sizeof(char *));
4✔
94
    dp->created = bxs_from_ascii("now");
4✔
95
    dp->revision = "1.0";
4✔
96
    dp->sample = bxs_from_ascii("n/a");
4✔
97
    dp->indentmode = DEF_INDENTMODE;
4✔
98
    dp->padding[BLEF] = 1;
4✔
99
    dp->defined_in = bxs_from_ascii("(command line)");
4✔
100

101
    dp->tags = (char **) calloc(2, sizeof(char *));
4✔
102
    dp->tags[0] = "transient";
4✔
103

104
    uint32_t *cld_u32 = u32_strconv_from_arg(cld, "UTF-8");  /* CHECK wrong on Windows (UTF-16) or different IME */
4✔
105
    bxstr_t *cldW = bxs_from_unicode(cld_u32);
4✔
106
    BFREE(cld_u32);
4!
107

108
    dp->shape[W].height = 1;
4✔
109
    dp->shape[W].width = cldW->num_columns;
4✔
110
    dp->shape[W].elastic = 1;
4✔
111
    rc = genshape(dp->shape[W].width, dp->shape[W].height, &(dp->shape[W].chars), &(dp->shape[W].mbcs));
4✔
112
    if (rc) {
4!
113
        return rc;
×
114
    }
115
    bxs_free(dp->shape[W].mbcs[0]);
4✔
116
    dp->shape[W].mbcs[0] = cldW;
4✔
117
    strcpy(dp->shape[W].chars[0], cld);
4✔
118

119
    for (i = 0; i < NUM_SHAPES; ++i) {
68✔
120
        c = dp->shape + i;
64✔
121

122
        if (i == NNW || i == NNE || i == WNW || i == ENE || i == W
64✔
123
                || i == WSW || i == ESE || i == SSW || i == SSE) {
44✔
124
                    continue;
36✔
125
        }
126

127
        switch (i) {
28!
128
            case NW:
8✔
129
            case SW:
130
                c->width = dp->shape[W].width;
8✔
131
                c->height = 1;
8✔
132
                c->elastic = 0;
8✔
133
                break;
8✔
134

135
            case NE:
8✔
136
            case SE:
137
                c->width = 1;
8✔
138
                c->height = 1;
8✔
139
                c->elastic = 0;
8✔
140
                break;
8✔
141

142
            case N:
12✔
143
            case S:
144
            case E:
145
                c->width = 1;
12✔
146
                c->height = 1;
12✔
147
                c->elastic = 1;
12✔
148
                break;
12✔
149

150
            default:
×
151
                fprintf(stderr, "%s: internal error\n", PROJECT);
×
152
                return 1;                /* never happens ;-) */
×
153
        }
154

155
        rc = genshape(c->width, c->height, &(c->chars), &(c->mbcs));
28✔
156
        if (rc) {
28!
157
            return rc;
×
158
        }
159
    }
160

161
    dp->maxshapeheight = 1;
4✔
162
    dp->minwidth = dp->shape[W].width + 2;
4✔
163
    dp->minheight = 3;
4✔
164

165
    return 0;
4✔
166
}
167

168

169

170
/**
171
 * Process command line options and store the result in the global `opt` struct. May exit the program.
172
 */
173
static void handle_command_line(int argc, char *argv[])
166✔
174
{
175
    #ifdef DEBUG
176
        fprintf (stderr, "Processing Command Line ...\n");
177
    #endif
178
    opt_t *parsed_opts = process_commandline(argc, argv);
166✔
179
    if (parsed_opts == NULL) {
166✔
180
        exit(EXIT_FAILURE);
10✔
181
    }
182
    if (parsed_opts->help) {
156✔
183
        usage_long(stdout);
1✔
184
        exit(EXIT_SUCCESS);
1✔
185
    }
186
    if (parsed_opts->version_requested) {
155!
187
        printf("%s version %s\n", PROJECT, VERSION);
×
188
        exit(EXIT_SUCCESS);
×
189
    }
190
    memcpy(&opt, parsed_opts, sizeof(opt_t));
155✔
191
    BFREE(parsed_opts);
155!
192
}
155✔
193

194

195

196
/**
197
 * Parse config file(s), then reset design pointer. May exit the program.
198
 */
199
static void handle_config_parsing()
155✔
200
{
201
    bxstr_t *config_file = discover_config_file(0);
155✔
202
    if (config_file == NULL) {
155✔
203
        exit(EXIT_FAILURE);
3✔
204
    }
205
    if (opt.cld == NULL) {
152✔
206
        size_t r_num_designs = 0;
148✔
207
        designs = parse_config_files(config_file, &r_num_designs);
148✔
208
        if (designs == NULL) {
148✔
209
            exit(EXIT_FAILURE);
3✔
210
        }
211
        num_designs = (int) r_num_designs;
145✔
212
    }
213
    else {
214
        int rc = build_design(&designs, opt.cld);
4✔
215
        if (rc) {
4!
216
            exit(EXIT_FAILURE);
×
217
        }
218
        num_designs = 1;
4✔
219
    }
220
    BFREE (opt.design);
149✔
221
    opt.design = designs;
149✔
222
}
149✔
223

224

225

226
/**
227
 * Adjust box size to command line specification.
228
 * Increase box width/height by width/height of empty sides in order to match appearance of box with the user's
229
 * expectations (if -s).
230
 */
231
static void apply_expected_size()
117✔
232
{
233
    if (opt.reqheight > (long) opt.design->minheight) {
117✔
234
        opt.design->minheight = opt.reqheight;
59✔
235
    }
236
    if (opt.reqwidth > (long) opt.design->minwidth) {
117✔
237
        opt.design->minwidth = opt.reqwidth;
59✔
238
    }
239
    if (opt.reqwidth) {
117✔
240
        if (empty_side(opt.design->shape, BRIG)) {
64✔
241
            opt.design->minwidth += opt.design->shape[SE].width;
2✔
242
        }
243
        if (empty_side(opt.design->shape, BLEF)) {
64✔
244
            opt.design->minwidth += opt.design->shape[NW].width;
2✔
245
        }
246
    }
247
    if (opt.reqheight) {
117✔
248
        if (empty_side(opt.design->shape, BTOP)) {
63!
249
            opt.design->minheight += opt.design->shape[NW].height;
×
250
        }
251
        if (empty_side(opt.design->shape, BBOT)) {
63!
252
            opt.design->minheight += opt.design->shape[SE].height;
×
253
        }
254
    }
255
}
117✔
256

257

258

259
/**
260
 * Read all input lines and store the result in the global `input` structure. May exit the program.
261
 */
262
static void handle_input()
118✔
263
{
264
    #ifdef DEBUG
265
        fprintf (stderr, "Reading all input ...\n");
266
    #endif
267
    input_t *raw_input = NULL;
118✔
268
    if (opt.mend != 0) {
118✔
269
        raw_input = read_all_input();
117✔
270
        if (raw_input == NULL) {
117!
271
            exit(EXIT_FAILURE);
×
272
        }
273
    }
274
    if (analyze_input(raw_input ? raw_input : &input)) {
118!
275
        exit(EXIT_FAILURE);
×
276
    }
277
    if (raw_input) {
118✔
278
        memcpy(&input, raw_input, sizeof(input_t));
117✔
279
        BFREE(raw_input);
117!
280
    }
281
    if (input.num_lines == 0) {
118!
282
        exit(EXIT_SUCCESS);
×
283
    }
284
}
118✔
285

286

287

288
/**
289
 * Adjust box size to fit requested padding value.
290
 * Command line-specified box size takes precedence over padding.
291
 */
292
static void adjust_size_and_padding()
118✔
293
{
294
    for (int i = 0; i < NUM_SIDES; ++i) {
590✔
295
        if (opt.padding[i] > -1) {
472✔
296
            opt.design->padding[i] = opt.padding[i];
25✔
297
        }
298
    }
299

300
    size_t pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
118✔
301
    if (pad > 0) {
118✔
302
        pad += input.num_lines;
3✔
303
        pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
3✔
304
        if (pad > opt.design->minheight) {
3!
305
            if (opt.reqheight) {
3✔
306
                for (int i = 0; i < (int) (pad - opt.design->minheight); ++i) {
7✔
307
                    if (opt.design->padding[i % 2 ? BBOT : BTOP]) {
6✔
308
                        opt.design->padding[i % 2 ? BBOT : BTOP] -= 1;
5✔
309
                    } else if (opt.design->padding[i % 2 ? BTOP : BBOT]) {
1!
310
                        opt.design->padding[i % 2 ? BTOP : BBOT] -= 1;
×
311
                    } else {
312
                        break;
1✔
313
                    }
314
                }
315
            }
316
            else {
317
                opt.design->minheight = pad;
1✔
318
            }
319
        }
320
    }
321

322
    pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
118✔
323
    if (pad > 0) {
118✔
324
        pad += input.maxline;
109✔
325
        pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
109✔
326
        if (pad > opt.design->minwidth) {
109✔
327
            if (opt.reqwidth) {
54✔
328
                for (int i = 0; i < (int) (pad - opt.design->minwidth); ++i) {
19✔
329
                    if (opt.design->padding[i % 2 ? BRIG : BLEF]) {
16✔
330
                        opt.design->padding[i % 2 ? BRIG : BLEF] -= 1;
13✔
331
                    } else if (opt.design->padding[i % 2 ? BLEF : BRIG]) {
3✔
332
                        opt.design->padding[i % 2 ? BLEF : BRIG] -= 1;
1!
333
                    } else {
334
                        break;
2✔
335
                    }
336
                }
337
            }
338
            else {
339
                opt.design->minwidth = pad;
49✔
340
            }
341
        }
342
    }
343
}
118✔
344

345

346

347
/**
348
 * Generate box. May exit the program.
349
 */
350
static void handle_generate_box()
113✔
351
{
352
    #ifdef DEBUG
353
        fprintf (stderr, "Generating Box ...\n");
354
    #endif
355
    sentry_t *thebox = (sentry_t *) calloc(NUM_SIDES, sizeof(sentry_t));
113✔
356
    if (thebox == NULL) {
113!
357
        perror(PROJECT);
×
358
        exit(EXIT_FAILURE);
×
359
    }
360
    int rc = generate_box(thebox);
113✔
361
    if (rc) {
113!
362
        exit(EXIT_FAILURE);
×
363
    }
364
    output_box(thebox);
113✔
365
}
113✔
366

367

368

369
/**
370
 * Remove box. May exit the program.
371
 */
372
static void handle_remove_box()
5✔
373
{
374
    #ifdef DEBUG
375
        fprintf (stderr, "Removing Box ...\n");
376
    #endif
377
    if (opt.killblank == -1) {
5✔
378
        if (empty_side(opt.design->shape, BTOP) && empty_side(opt.design->shape, BBOT)) {
4!
379
            opt.killblank = 0;
×
380
        } else {
381
            opt.killblank = 1;
4✔
382
        }
383
    }
384
    int rc = remove_box();
5✔
385
    if (rc) {
5!
386
        exit(EXIT_FAILURE);
×
387
    }
388
    rc = apply_substitutions(&input, 1);
5✔
389
    if (rc) {
5!
390
        exit(EXIT_FAILURE);
×
391
    }
392
    output_input(opt.mend > 0);
5✔
393
}
5✔
394

395

396

397
/* These two functions are actually declared in term.h, but for some reason, that can't be included. */
398
extern NCURSES_EXPORT(int) setupterm (NCURSES_CONST char *, int, int *);
399
extern NCURSES_EXPORT(int) tigetnum (NCURSES_CONST char *);
400

401
static int terminal_has_colors()
154✔
402
{
403
    int result = 0;
154✔
404
    char *termtype = getenv("TERM");
154✔
405
    if (termtype != NULL && setupterm(termtype, STDOUT_FILENO, NULL) == OK && tigetnum("colors") >= 8) {
154!
406
        result = 1;
1✔
407
    }
408
    #if defined(DEBUG)
409
        int num_colors = result ? tigetnum("colors") : 0;
410
        fprintf(stderr, "Terminal \"%s\" %s colors (number of colors = %d).\n", termtype,
411
                result ? "has" : "does NOT have", num_colors);
412
    #endif
413
    return result;
154✔
414
}
415

416

417

418
static int check_color_support(int opt_color)
155✔
419
{
420
    int result = 0;
155✔
421
    if (opt_color == force_ansi_color) {
155✔
422
        result = 1;
1✔
423
    }
424
    else if (opt_color == color_from_terminal) {
154!
425
        result = terminal_has_colors();
154✔
426
    }
427

428
    #if defined(DEBUG)
429
        fprintf(stderr, "%s: Color support %sabled\n", PROJECT, result ? "\x1b[92mEN\x1b[0m" : "DIS");
430
    #endif
431
    return result;
155✔
432
}
433

434

435

436
/*       _\|/_
437
         (o o)
438
 +----oOO-{_}-OOo------------------------------------------------------------+
439
 |                       P r o g r a m   S t a r t                           |
440
 +--------------------------------------------------------------------------*/
441

442
int main(int argc, char *argv[])
166✔
443
{
444
    int rc;                           /* general return code */
445
    int saved_designwidth;            /* opt.design->minwith backup, used for mending */
446
    int saved_designheight;           /* opt.design->minheight backup, used for mending */
447

448
    #ifdef DEBUG
449
        fprintf (stderr, "BOXES STARTING ...\n");
450
    #endif
451

452
    /* Temporarily set the system encoding, for proper output of --help text etc. */
453
    setlocale(LC_ALL, "");    /* switch from default "C" encoding to system encoding */
166✔
454
    encoding = locale_charset();
166✔
455

456
    handle_command_line(argc, argv);
166✔
457

458
    /* Store system character encoding */
459
    encoding = check_encoding(opt.encoding, locale_charset());
155✔
460
    #ifdef DEBUG
461
        fprintf (stderr, "Character Encoding = %s\n", encoding);
462
    #endif
463

464
    color_output_enabled = check_color_support(opt.color);
155✔
465

466
    handle_config_parsing();
155✔
467

468
    /* If "-l" option was given, list designs and exit. */
469
    if (opt.l) {
149✔
470
        rc = list_designs();
24✔
471
        exit(rc);
24✔
472
    }
473

474
    /* If "-q" option was given, print results of tag query and exit. */
475
    if (opt.query != NULL && opt.query[0] != NULL && !query_is_undoc()) {
125!
476
        rc = query_by_tag();
8✔
477
        exit(rc);
8✔
478
    }
479

480
    apply_expected_size();
117✔
481
    if (opt.indentmode) {
117!
482
        opt.design->indentmode = opt.indentmode;
×
483
    }
484
    saved_designwidth = opt.design->minwidth;
117✔
485
    saved_designheight = opt.design->minheight;
117✔
486

487
    do {
488
        if (opt.mend == 1) {  /* Mending a box works in two phases: */
118✔
489
            opt.r = 0;        /* opt.mend == 2: remove box          */
1✔
490
        }
491
        --opt.mend;           /* opt.mend == 1: add it back         */
118✔
492
        opt.design->minwidth = saved_designwidth;
118✔
493
        opt.design->minheight = saved_designheight;
118✔
494

495
        handle_input();
118✔
496

497
        adjust_size_and_padding();
118✔
498

499
        if (opt.r) {
118✔
500
            handle_remove_box();
5✔
501
        }
502
        else {
503
            handle_generate_box();
113✔
504
        }
505
    } while (opt.mend > 0);
118✔
506

507
    return EXIT_SUCCESS;
117✔
508
}
509

510
/*EOF*/                                                  /* 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