| 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 |
/* |
| 17 |
* Project-wide globals and data structures |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef BOXES_H |
| 21 |
#define BOXES_H |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <unitypes.h> |
| 25 |
|
| 26 |
#include "bxstring.h" |
| 27 |
#include "regulex.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#define PROJECT "boxes" /* name of program */ |
| 32 |
#define VERSION "--BVERSION--" /* current release of project */ |
| 33 |
#define GLOBALCONF "--GLOBALCONF--" /* name of system-wide config file */ |
| 34 |
|
| 35 |
|
| 36 |
#define DEF_INDENTMODE 'b' /* default indent mode of a design (indent box, not text) */ |
| 37 |
|
| 38 |
|
| 39 |
/* |
| 40 |
* max. supported line length in bytes |
| 41 |
* This is how many characters of a line will be read. Anything beyond |
| 42 |
* will be discarded. The line feed character at the end does not count. |
| 43 |
* (This should have been done via sysconf(), but I didn't do it in order |
| 44 |
* to ease porting to non-unix platforms.) |
| 45 |
*/ |
| 46 |
#if defined(LINE_MAX_BYTES) && (LINE_MAX_BYTES < 2048) |
| 47 |
#undef LINE_MAX_BYTES |
| 48 |
#endif |
| 49 |
#ifndef LINE_MAX_BYTES |
| 50 |
#define LINE_MAX_BYTES 16382 |
| 51 |
#endif |
| 52 |
|
| 53 |
/* Macro to declare a function parameter as intentionally unused in order to avoid compiler warnings */ |
| 54 |
#define UNUSED(variable) ((void)(variable)) |
| 55 |
|
| 56 |
|
| 57 |
#define BTOP 0 /* for use with sides */ |
| 58 |
#define BRIG 1 |
| 59 |
#define BBOT 2 |
| 60 |
#define BLEF 3 |
| 61 |
|
| 62 |
|
| 63 |
typedef enum { |
| 64 |
NW, NNW, N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW |
| 65 |
} shape_t; |
| 66 |
|
| 67 |
|
| 68 |
typedef struct { |
| 69 |
shape_t name; |
| 70 |
char **chars; |
| 71 |
bxstr_t **mbcs; |
| 72 |
size_t height; |
| 73 |
size_t width; |
| 74 |
|
| 75 |
/** elastic is used only in original definition */ |
| 76 |
int elastic; |
| 77 |
|
| 78 |
/** For each shape line 0..height-1, a flag which is 1 if all shapes to the left of this shape are blank on the |
| 79 |
* same shape line. Always 1 if the shape is part of the left (west) box side. */ |
| 80 |
int *blank_leftward; |
| 81 |
|
| 82 |
/** For each shape line 0..height-1, a flag which is 1 if all shapes to the right of this shape are blank on the |
| 83 |
* same shape line. Always 1 if the shape is part of the right (east) box side. */ |
| 84 |
int *blank_rightward; |
| 85 |
} sentry_t; |
| 86 |
|
| 87 |
#define SENTRY_INITIALIZER (sentry_t) {NW, NULL, NULL, 0, 0, 0, NULL, NULL} |
| 88 |
|
| 89 |
#define NUM_SHAPES 16 |
| 90 |
#define NUM_SIDES 4 |
| 91 |
#define NUM_CORNERS 4 |
| 92 |
|
| 93 |
|
| 94 |
typedef struct { |
| 95 |
bxstr_t *search; |
| 96 |
bxstr_t *repstr; |
| 97 |
pcre2_code *prog; /* compiled search pattern */ |
| 98 |
int line; /* line of definition in config file */ |
| 99 |
char mode; /* 'g' or 'o' */ |
| 100 |
} reprule_t; |
| 101 |
|
| 102 |
|
| 103 |
typedef struct { |
| 104 |
char *name; /* primary name of the box design */ |
| 105 |
char **aliases; /* zero-terminated array of alias names of the design */ |
| 106 |
bxstr_t *author; /* creator of the configuration file entry */ |
| 107 |
bxstr_t *designer; /* creator of the original ASCII artwork */ |
| 108 |
bxstr_t *sample; /* the complete sample block in one string */ |
| 109 |
char indentmode; /* 'b', 't', or 'n' */ |
| 110 |
sentry_t shape[NUM_SHAPES]; |
| 111 |
size_t maxshapeheight; /* height of highest shape in design */ |
| 112 |
size_t minwidth; |
| 113 |
size_t minheight; |
| 114 |
int padding[NUM_SIDES]; |
| 115 |
char **tags; |
| 116 |
bxstr_t *defined_in; /* path to config file where this was defined */ |
| 117 |
|
| 118 |
reprule_t *current_rule; |
| 119 |
reprule_t *reprules; /* applied when drawing a box */ |
| 120 |
size_t num_reprules; |
| 121 |
reprule_t *revrules; /* applied upon removal of a box */ |
| 122 |
size_t num_revrules; |
| 123 |
} design_t; |
| 124 |
|
| 125 |
extern design_t *designs; |
| 126 |
extern int num_designs; |
| 127 |
|
| 128 |
|
| 129 |
typedef struct { /* Command line options: */ |
| 130 |
char valign; /** `-a`: text position inside box */ |
| 131 |
char halign; /** `-a`: ( h[lcr]v[tcb] ) */ |
| 132 |
char justify; /** `-a`: 'l', 'c', 'r', or '\0' */ |
| 133 |
char *cld; /** `-c`: commandline design definition */ |
| 134 |
int color; /** `--color` or `--no-color`: `force_monochrome`, `color_from_terminal`, or `force_ansi_color` */ |
| 135 |
design_t *design; /** `-d`: currently used box design */ |
| 136 |
int design_choice_by_user; /** `-d`, `-c`: true if design was chosen by user */ |
| 137 |
char *eol; /** `-e`: line break to use. Never NULL, default to "\n". */ |
| 138 |
int eol_overridden; /** `-e`: 0: value in `eol` is the default; 1: value in `eol` specified via `-e` */ |
| 139 |
char *f; /** `-f`: config file path */ |
| 140 |
int help; /** `-h`: flags if help argument was specified */ |
| 141 |
char indentmode; /** `-i`: 'b', 't', 'n', or '\0' */ |
| 142 |
int killblank; /** `-k`: kill blank lines, -1 if not set */ |
| 143 |
int l; /** `-l`: list available designs */ |
| 144 |
int mend; /** `-m`: 1 if -m is given, 2 in 2nd loop */ |
| 145 |
char *encoding; /** `-n`: character encoding override for input and output text */ |
| 146 |
int padding[NUM_SIDES]; /** `-p`: in spaces or lines resp. */ |
| 147 |
char **query; /** `-q`: parsed tag query expression passed in via -q; also, special handling of web UI needs */ |
| 148 |
int r; /** `-r`: remove box from input */ |
| 149 |
long reqwidth; /** `-s`: requested box width */ |
| 150 |
long reqheight; /** `-s`: requested box height */ |
| 151 |
int tabstop; /** `-t`: tab stop distance */ |
| 152 |
char tabexp; /** `-t`: tab expansion mode (for leading tabs) */ |
| 153 |
int version_requested; /** `-v`: request to show version number */ |
| 154 |
int *debug; /** `-x debug:`: activate debug logging for given debug log areas */ |
| 155 |
int qundoc; /** `-x (undoc)`: flag if "(undoc)" was specified, put directly before "debug:" */ |
| 156 |
FILE *infile; |
| 157 |
FILE *outfile; |
| 158 |
} opt_t; |
| 159 |
|
| 160 |
extern opt_t opt; |
| 161 |
|
| 162 |
/* The possible values of the `color` field from `opt_t`: */ |
| 163 |
#define force_monochrome 0 |
| 164 |
#define color_from_terminal 1 |
| 165 |
#define force_ansi_color 2 |
| 166 |
|
| 167 |
/** Flag indicating if ANSI color codes should be printed (1) or not (0) */ |
| 168 |
extern int color_output_enabled; |
| 169 |
|
| 170 |
|
| 171 |
typedef struct { |
| 172 |
bxstr_t *text; /* text content of the line as a boxes string */ |
| 173 |
uint32_t *cache_visible; /* only the visible characters of `text`, initially NULL. This is a cache. */ |
| 174 |
size_t *tabpos; /* tab positions in expanded work strings, or NULL if not needed */ |
| 175 |
size_t tabpos_len; /* number of tabs in a line */ |
| 176 |
} line_t; |
| 177 |
|
| 178 |
typedef struct { |
| 179 |
line_t *lines; |
| 180 |
size_t num_lines; /* number of entries in input */ |
| 181 |
size_t maxline; /* length in columns of longest input line */ |
| 182 |
size_t indent; /* common number of leading spaces found in all input lines */ |
| 183 |
int final_newline; /* true if the last line of input ends with newline */ |
| 184 |
} input_t; |
| 185 |
|
| 186 |
extern input_t input; |
| 187 |
|
| 188 |
|
| 189 |
#endif /* BOXES_H */ |
| 190 |
|
| 191 |
/* vim: set cindent sw=4: */ |