Apply new code format from CLion

This commit is contained in:
Thomas Jensen 2021-02-03 20:46:48 +01:00
parent 53e7782b5f
commit 6a3d0e715c
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB

View File

@ -46,6 +46,8 @@
#include "remove.h"
#include "unicode.h"
#ifdef __MINGW32__
#include <windows.h>
#endif
@ -59,23 +61,6 @@ extern int optind, opterr, optopt; /* for getopt() */
#define BOXES_CONFIG ".boxes"
#endif
/*
* Some source comments (AD 1999):
*
* - The decision to number box shapes in clockwise order was a
* major design mistake. Treatment of box parts of the same
* alignment (N-S and E-W) is usually combined in one function,
* which must now deal with the numbering being reversed all the
* time.
* - All shapes defined in a box design must be used in any box of
* that design at least once. In other words, there must not be
* a shape which is defined in the config file but cannot be
* found in an actual box of that design. This sort of limits
* how small your boxes can get. However, in practice it is not
* a problem, because boxes which must be small usually consist
* of small shapes which can be packed pretty tightly anyway.
*/
/* _\|/_
(o o)
@ -215,8 +200,9 @@ static int get_config_file()
char exepath[256]; /* for constructing config file path */
#endif
if (yyin != stdin)
if (yyin != stdin) {
return 0; /* we're already ok */
}
/*
* Try getting it from the BOXES environment variable
@ -288,8 +274,7 @@ static int get_config_file()
else {
#ifndef __MINGW32__
/* Do not print this warning on windows. */
fprintf (stderr, "%s: warning: Environment variable HOME not set!\n",
PROJECT);
fprintf(stderr, "%s: warning: Environment variable HOME not set!\n", PROJECT);
#endif
}
@ -385,16 +370,16 @@ static int process_commandline (int argc, char *argv[])
opt.tabstop = DEF_TABSTOP;
opt.tabexp = 'e';
opt.killblank = -1;
for (idummy=0; idummy<ANZ_SIDES; ++idummy)
for (idummy = 0; idummy < ANZ_SIDES; ++idummy) {
opt.padding[idummy] = -1;
}
yyin = stdin;
/*
* Intercept '--help' and '-?' cases first, as they are not supported by getopt()
*/
if (argc >= 2 && argv[1] != NULL
&& (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0))
{
&& (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)) {
usage_long(stdout);
return 42;
}
@ -465,14 +450,14 @@ static int process_commandline (int argc, char *argv[])
errfl = 1;
break;
}
if (errfl)
if (errfl) {
break;
else
} else {
++pdummy;
}
}
if (errfl) {
fprintf (stderr, "%s: Illegal text format -- %s\n",
PROJECT, optarg);
fprintf(stderr, "%s: Illegal text format -- %s\n", PROJECT, optarg);
return 1;
}
break;
@ -491,8 +476,7 @@ static int process_commandline (int argc, char *argv[])
templine.len = strlen(opt.cld);
templine.text = opt.cld;
if (empty_line(&templine)) {
fprintf (stderr, "%s: boxes may not consist entirely "
"of whitespace\n", PROJECT);
fprintf(stderr, "%s: boxes may not consist entirely of whitespace\n", PROJECT);
return 1;
}
}
@ -518,8 +502,7 @@ static int process_commandline (int argc, char *argv[])
*/
f = fopen(optarg, "r");
if (f == NULL) {
fprintf (stderr, "%s: Couldn\'t open config file \'%s\' "
"for input.\n", PROJECT, optarg);
fprintf(stderr, "%s: Couldn\'t open config file \'%s\' for input.\n", PROJECT, optarg);
return 1;
}
rc = is_dir(optarg);
@ -528,8 +511,7 @@ static int process_commandline (int argc, char *argv[])
return 1;
}
else if (rc) {
fprintf (stderr, "%s: Alleged config file '%s' is a "
"directory\n", PROJECT, optarg);
fprintf(stderr, "%s: Alleged config file '%s' is a directory\n", PROJECT, optarg);
fclose(f);
return 1;
}
@ -554,13 +536,13 @@ static int process_commandline (int argc, char *argv[])
* Indentation mode
*/
optlen = strlen(optarg);
if (optlen <= 3 && !strncasecmp ("box", optarg, optlen))
if (optlen <= 3 && !strncasecmp("box", optarg, optlen)) {
opt.indentmode = 'b';
else if (optlen <= 4 && !strncasecmp ("text", optarg, optlen))
} else if (optlen <= 4 && !strncasecmp("text", optarg, optlen)) {
opt.indentmode = 't';
else if (optlen <= 4 && !strncasecmp ("none", optarg, optlen))
} else if (optlen <= 4 && !strncasecmp("none", optarg, optlen)) {
opt.indentmode = 'n';
else {
} else {
fprintf(stderr, "%s: invalid indentation mode\n", PROJECT);
return 1;
}
@ -571,11 +553,11 @@ static int process_commandline (int argc, char *argv[])
*/
case 'k':
if (opt.killblank == -1) {
if (strisyes (optarg))
if (strisyes(optarg)) {
opt.killblank = 1;
else if (strisno (optarg))
} else if (strisno(optarg)) {
opt.killblank = 0;
else {
} else {
fprintf(stderr, "%s: -k: invalid parameter\n", PROJECT);
return 1;
}
@ -647,12 +629,12 @@ static int process_commandline (int argc, char *argv[])
errfl = 1;
break;
}
if (errfl)
if (errfl) {
break;
}
}
if (errfl) {
fprintf (stderr, "%s: invalid padding specification - "
"%s\n", PROJECT, optarg);
fprintf(stderr, "%s: invalid padding specification - %s\n", PROJECT, optarg);
return 1;
}
break;
@ -676,12 +658,16 @@ static int process_commandline (int argc, char *argv[])
* Specify desired box target size
*/
pdummy = strchr(optarg, 'x');
if (!pdummy) pdummy = strchr (optarg, 'X');
if (!pdummy) {
pdummy = strchr(optarg, 'X');
}
if (pdummy) {
*pdummy = '\0';
}
errno = 0;
if (optarg != pdummy) opt.reqwidth = strtol (optarg, NULL, 10);
if (optarg != pdummy) {
opt.reqwidth = strtol(optarg, NULL, 10);
}
if (pdummy) {
opt.reqheight = strtol(pdummy + 1, NULL, 10);
*pdummy = 'x';
@ -742,7 +728,8 @@ static int process_commandline (int argc, char *argv[])
printf("%s version %s\n", PROJECT, VERSION);
return 42;
case ':': case '?':
case ':':
case '?':
/*
* Missing argument or illegal option - do nothing else
*/
@ -774,8 +761,7 @@ static int process_commandline (int argc, char *argv[])
}
else if (argv[optind + 1] && argv[optind + 2]) { /* illegal third file */
fprintf (stderr, "%s: illegal parameter -- %s\n",
PROJECT, argv[optind+2]);
fprintf(stderr, "%s: illegal parameter -- %s\n", PROJECT, argv[optind + 2]);
usage_short(stderr);
return 1;
}
@ -787,8 +773,7 @@ static int process_commandline (int argc, char *argv[])
else {
opt.infile = fopen(argv[optind], "r");
if (opt.infile == NULL) {
fprintf (stderr, "%s: Can\'t open input file -- %s\n", PROJECT,
argv[optind]);
fprintf(stderr, "%s: Can\'t open input file -- %s\n", PROJECT, argv[optind]);
return 9; /* can't read infile */
}
}
@ -803,8 +788,9 @@ static int process_commandline (int argc, char *argv[])
opt.outfile = fopen(argv[optind + 1], "w");
if (opt.outfile == NULL) {
perror(PROJECT);
if (opt.infile != stdin)
if (opt.infile != stdin) {
fclose(opt.infile);
}
return 10;
}
}
@ -815,30 +801,26 @@ static int process_commandline (int argc, char *argv[])
*/
if (opt.cld == NULL) {
rc = get_config_file(); /* sets yyin and yyfilename */
if (rc) /* may change working directory */
if (rc) { /* may change working directory */
return rc;
}
}
#if defined(DEBUG) || 0
fprintf (stderr, "Command line option settings (excerpt):\n");
fprintf (stderr, "- Padding: l:%d t:%d r:%d b:%d\n", opt.padding[BLEF],
opt.padding[BTOP], opt.padding[BRIG], opt.padding[BBOT]);
fprintf (stderr, "- Requested box size: %ldx%ld\n", opt.reqwidth,
opt.reqheight);
fprintf (stderr, "- Requested box size: %ldx%ld\n", opt.reqwidth, opt.reqheight);
fprintf (stderr, "- Tabstop distance: %d\n", opt.tabstop);
fprintf (stderr, "- Tab handling: \'%c\'\n", opt.tabexp);
fprintf (stderr, "- Alignment: horiz %c, vert %c\n",
opt.halign?opt.halign:'?', opt.valign?opt.valign:'?');
fprintf (stderr, "- Indentmode: \'%c\'\n",
opt.indentmode? opt.indentmode: '?');
fprintf (stderr, "- Line justification: \'%c\'\n",
opt.justify? opt.justify: '?');
fprintf (stderr, "- Alignment: horiz %c, vert %c\n", opt.halign?opt.halign:'?', opt.valign?opt.valign:'?');
fprintf (stderr, "- Indentmode: \'%c\'\n", opt.indentmode? opt.indentmode: '?');
fprintf (stderr, "- Line justification: \'%c\'\n", opt.justify? opt.justify: '?');
fprintf (stderr, "- Kill blank lines: %d\n", opt.killblank);
fprintf (stderr, "- Remove box: %d\n", opt.r);
fprintf (stderr, "- Special handling for Web UI: %d\n", opt.q);
fprintf (stderr, "- Mend box: %d\n", opt.mend);
fprintf (stderr, "- Design Definition W shape: %s\n",
opt.cld? opt.cld: "n/a");
fprintf (stderr, "- Design Definition W shape: %s\n", opt.cld? opt.cld: "n/a");
#endif
return 0;
@ -888,31 +870,37 @@ static int build_design (design_t **adesigns, const char *cld)
dp->shape[W].width = strlen(cld);
dp->shape[W].elastic = 1;
rc = genshape(dp->shape[W].width, dp->shape[W].height, &(dp->shape[W].chars));
if (rc)
if (rc) {
return rc;
}
strcpy(dp->shape[W].chars[0], cld);
for (i = 0; i < ANZ_SHAPES; ++i) {
c = dp->shape + i;
if (i == NNW || i == NNE || i == WNW || i == ENE || i == W
|| i == WSW || i == ESE || i == SSW || i == SSE)
|| i == WSW || i == ESE || i == SSW || i == SSE) {
continue;
}
switch (i) {
case NW: case SW:
case NW:
case SW:
c->width = dp->shape[W].width;
c->height = 1;
c->elastic = 0;
break;
case NE: case SE:
case NE:
case SE:
c->width = 1;
c->height = 1;
c->elastic = 0;
break;
case N: case S: case E:
case N:
case S:
case E:
c->width = 1;
c->height = 1;
c->elastic = 1;
@ -924,9 +912,10 @@ static int build_design (design_t **adesigns, const char *cld)
}
rc = genshape(c->width, c->height, &(c->chars));
if (rc)
if (rc) {
return rc;
}
}
dp->maxshapeheight = 1;
dp->minwidth = dp->shape[W].width + 2;
@ -936,6 +925,7 @@ static int build_design (design_t **adesigns, const char *cld)
}
static char *escape(const char *org, const int pLength)
{
char *result = (char *) calloc(1, 2 * strlen(org) + 1);
@ -951,12 +941,15 @@ static char* escape (const char* org, const int pLength)
}
static int style_sort(const void *p1, const void *p2)
{
return strcasecmp((const char *) ((*((design_t **) p1))->name),
(const char *) ((*((design_t **) p2))->name));
}
static int list_styles()
/*
* Generate sorted listing of available box styles.
@ -983,8 +976,9 @@ static int list_styles()
fprintf(opt.outfile, "Complete Design Information for \"%s\":\n",
d->name);
fprintf(opt.outfile, "-----------------------------------");
for (i=strlen(d->name); i>0; --i)
for (i = strlen(d->name); i > 0; --i) {
fprintf(opt.outfile, "-");
}
fprintf(opt.outfile, "\n");
fprintf(opt.outfile, "Author: %s\n",
@ -1018,10 +1012,11 @@ static int list_styles()
fprintf(opt.outfile, "%d. (%s) \"%s\" WITH \"%s\"\n", i + 1,
d->reprules[i].mode == 'g' ? "glob" : "once",
d->reprules[i].search, d->reprules[i].repstr);
if (i < (int) d->anz_reprules - 1)
if (i < (int) d->anz_reprules - 1) {
fprintf(opt.outfile, " ");
}
}
}
else {
fprintf(opt.outfile, "none\n");
}
@ -1031,10 +1026,11 @@ static int list_styles()
fprintf(opt.outfile, "%d. (%s) \"%s\" TO \"%s\"\n", i + 1,
d->revrules[i].mode == 'g' ? "glob" : "once",
d->revrules[i].search, d->revrules[i].repstr);
if (i < (int) d->anz_revrules - 1)
if (i < (int) d->anz_revrules - 1) {
fprintf(opt.outfile, " ");
}
}
}
else {
fprintf(opt.outfile, "none\n");
}
@ -1047,21 +1043,25 @@ static int list_styles()
|| d->padding[BBOT] || d->padding[BLEF]) {
if (d->padding[BLEF]) {
fprintf(opt.outfile, "left %d", d->padding[BLEF]);
if (d->padding[BTOP] || d->padding[BRIG] || d->padding[BBOT])
if (d->padding[BTOP] || d->padding[BRIG] || d->padding[BBOT]) {
fprintf(opt.outfile, ", ");
}
}
if (d->padding[BTOP]) {
fprintf(opt.outfile, "top %d", d->padding[BTOP]);
if (d->padding[BRIG] || d->padding[BBOT])
if (d->padding[BRIG] || d->padding[BBOT]) {
fprintf(opt.outfile, ", ");
}
}
if (d->padding[BRIG]) {
fprintf(opt.outfile, "right %d", d->padding[BRIG]);
if (d->padding[BBOT])
if (d->padding[BBOT]) {
fprintf(opt.outfile, ", ");
}
if (d->padding[BBOT])
}
if (d->padding[BBOT]) {
fprintf(opt.outfile, "bottom %d", d->padding[BBOT]);
}
fprintf(opt.outfile, "\n");
}
else {
@ -1078,8 +1078,9 @@ static int list_styles()
fprintf(opt.outfile, "Elastic Shapes: ");
sstart = 0;
for (i = 0; i < ANZ_SHAPES; ++i) {
if (isempty(d->shape+i))
if (isempty(d->shape + i)) {
continue;
}
if (d->shape[i].elastic) {
fprintf(opt.outfile, "%s%s", sstart ? ", " : "", shape_name[i]);
sstart = 1;
@ -1125,8 +1126,9 @@ static int list_styles()
return 1;
}
for (i=0; i<anz_designs; ++i)
for (i = 0; i < anz_designs; ++i) {
list[i] = &(designs[i]);
}
qsort(list, design_idx + 1, sizeof(design_t *), style_sort);
sprintf(buf, "%d", anz_designs);
@ -1136,8 +1138,9 @@ static int list_styles()
anz_designs, anz_designs == 1 ? "" : "s", yyfilename);
fprintf(opt.outfile, "-----------------------%s",
anz_designs == 1 ? "" : "-");
for (i=strlen(yyfilename)+strlen(buf); i>0; --i)
for (i = strlen(yyfilename) + strlen(buf); i > 0; --i) {
fprintf(opt.outfile, "-");
}
fprintf(opt.outfile, "\n\n");
}
for (i = 0; i < anz_designs; ++i) {
@ -1236,8 +1239,9 @@ static int apply_substitutions (const int mode)
char buf[LINE_MAX_BYTES * 2];
size_t buf_len; /* length of string in buf */
if (opt.design == NULL)
if (opt.design == NULL) {
return 1;
}
if (mode == 0) {
anz_rules = opt.design->anz_reprules;
@ -1261,7 +1265,9 @@ static int apply_substitutions (const int mode)
rules[j].prog = regcomp(rules[j].search);
}
opt.design->current_rule = NULL;
if (errno) return 3;
if (errno) {
return 3;
}
/*
* Apply regular expression substitutions to input lines
@ -1282,7 +1288,9 @@ static int apply_substitutions (const int mode)
#ifdef REGEXP_DEBUG
fprintf (stderr, "%d\n", buf_len);
#endif
if (errno) return 1;
if (errno) {
return 1;
}
BFREE (input.lines[k].text);
input.lines[k].text = (char *) strdup(buf);
@ -1293,12 +1301,12 @@ static int apply_substitutions (const int mode)
input.lines[k].len = buf_len;
if (input.lines[k].len > input.maxline)
if (input.lines[k].len > input.maxline) {
input.maxline = input.lines[k].len;
}
#ifdef REGEXP_DEBUG
fprintf (stderr, "input.lines[%d] == {%d, \"%s\"}\n", k,
input.lines[k].len, input.lines[k].text);
fprintf (stderr, "input.lines[%d] == {%d, \"%s\"}\n", k, input.lines[k].len, input.lines[k].text);
#endif
}
opt.design->current_rule = NULL;
@ -1311,11 +1319,12 @@ static int apply_substitutions (const int mode)
if (opt.design->indentmode == 't') {
int rc;
rc = get_indent(input.lines, input.anz_lines);
if (rc >= 0)
if (rc >= 0) {
input.indent = (size_t) rc;
else
} else {
return 4;
}
}
return 0;
}
@ -1605,6 +1614,7 @@ static int read_all_input (const int use_stdin)
}
/* _\|/_
(o o)
+----oOO-{_}-OOo------------------------------------------------------------+
@ -1630,10 +1640,12 @@ int main (int argc, char *argv[])
fprintf (stderr, "Processing Command Line ...\n");
#endif
rc = process_commandline(argc, argv);
if (rc == 42)
if (rc == 42) {
exit(EXIT_SUCCESS);
if (rc)
}
if (rc) {
exit(EXIT_FAILURE);
}
/*
* Store system character encoding
@ -1652,13 +1664,15 @@ int main (int argc, char *argv[])
#endif
if (opt.cld == NULL) {
rc = yyparse();
if (rc)
if (rc) {
exit(EXIT_FAILURE);
}
}
else {
rc = build_design(&designs, opt.cld);
if (rc)
if (rc) {
exit(EXIT_FAILURE);
}
anz_designs = 1;
}
BFREE (opt.design);
@ -1677,30 +1691,38 @@ int main (int argc, char *argv[])
* Increase box width/height by width/height of empty sides in order
* to match appearance of box with the user's expectations (if -s).
*/
if (opt.reqheight > (long) opt.design->minheight)
if (opt.reqheight > (long) opt.design->minheight) {
opt.design->minheight = opt.reqheight;
if (opt.reqwidth > (long) opt.design->minwidth)
}
if (opt.reqwidth > (long) opt.design->minwidth) {
opt.design->minwidth = opt.reqwidth;
}
if (opt.reqwidth) {
if (empty_side (opt.design->shape, BRIG))
if (empty_side(opt.design->shape, BRIG)) {
opt.design->minwidth += opt.design->shape[SE].width;
if (empty_side (opt.design->shape, BLEF))
}
if (empty_side(opt.design->shape, BLEF)) {
opt.design->minwidth += opt.design->shape[NW].width;
}
}
if (opt.reqheight) {
if (empty_side (opt.design->shape, BTOP))
if (empty_side(opt.design->shape, BTOP)) {
opt.design->minheight += opt.design->shape[NW].height;
if (empty_side (opt.design->shape, BBOT))
}
if (empty_side(opt.design->shape, BBOT)) {
opt.design->minheight += opt.design->shape[SE].height;
}
if (opt.indentmode)
}
if (opt.indentmode) {
opt.design->indentmode = opt.indentmode;
}
saved_designwidth = opt.design->minwidth;
saved_designheight = opt.design->minheight;
do {
if (opt.mend == 1) /* Mending a box works in two phases: */
if (opt.mend == 1) { /* Mending a box works in two phases: */
opt.r = 0; /* opt.mend == 2: remove box */
}
--opt.mend; /* opt.mend == 1: add it back */
opt.design->minwidth = saved_designwidth;
opt.design->minheight = saved_designheight;
@ -1712,19 +1734,22 @@ int main (int argc, char *argv[])
fprintf (stderr, "Reading all input ...\n");
#endif
rc = read_all_input(opt.mend);
if (rc)
if (rc) {
exit(EXIT_FAILURE);
if (input.anz_lines == 0)
}
if (input.anz_lines == 0) {
exit(EXIT_SUCCESS);
}
/*
* Adjust box size to fit requested padding value
* Command line-specified box size takes precedence over padding.
*/
for (i = 0; i < ANZ_SIDES; ++i) {
if (opt.padding[i] > -1)
if (opt.padding[i] > -1) {
opt.design->padding[i] = opt.padding[i];
}
}
pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
if (pad > 0) {
pad += input.anz_lines;
@ -1732,14 +1757,15 @@ int main (int argc, char *argv[])
if (pad > opt.design->minheight) {
if (opt.reqheight) {
for (i = 0; i < (int) (pad - opt.design->minheight); ++i) {
if (opt.design->padding[i%2?BBOT:BTOP])
if (opt.design->padding[i % 2 ? BBOT : BTOP]) {
opt.design->padding[i % 2 ? BBOT : BTOP] -= 1;
else if (opt.design->padding[i%2?BTOP:BBOT])
} else if (opt.design->padding[i % 2 ? BTOP : BBOT]) {
opt.design->padding[i % 2 ? BTOP : BBOT] -= 1;
else
} else {
break;
}
}
}
else {
opt.design->minheight = pad;
}
@ -1752,14 +1778,15 @@ int main (int argc, char *argv[])
if (pad > opt.design->minwidth) {
if (opt.reqwidth) {
for (i = 0; i < (int) (pad - opt.design->minwidth); ++i) {
if (opt.design->padding[i%2?BRIG:BLEF])
if (opt.design->padding[i % 2 ? BRIG : BLEF]) {
opt.design->padding[i % 2 ? BRIG : BLEF] -= 1;
else if (opt.design->padding[i%2?BLEF:BRIG])
} else if (opt.design->padding[i % 2 ? BLEF : BRIG]) {
opt.design->padding[i % 2 ? BLEF : BRIG] -= 1;
else
} else {
break;
}
}
}
else {
opt.design->minwidth = pad;
}
@ -1774,18 +1801,20 @@ int main (int argc, char *argv[])
fprintf (stderr, "Removing Box ...\n");
#endif
if (opt.killblank == -1) {
if (empty_side (opt.design->shape, BTOP)
&& empty_side (opt.design->shape, BBOT))
if (empty_side(opt.design->shape, BTOP) && empty_side(opt.design->shape, BBOT)) {
opt.killblank = 0;
else
} else {
opt.killblank = 1;
}
}
rc = remove_box();
if (rc)
if (rc) {
exit(EXIT_FAILURE);
}
rc = apply_substitutions(1);
if (rc)
if (rc) {
exit(EXIT_FAILURE);
}
output_input(opt.mend > 0);
}
@ -1804,8 +1833,9 @@ int main (int argc, char *argv[])
exit(EXIT_FAILURE);
}
rc = generate_box(thebox);
if (rc)
if (rc) {
exit(EXIT_FAILURE);
}
output_box(thebox);
}
} while (opt.mend > 0);