Extract smaller functions from the main() function #78

This commit is contained in:
Thomas Jensen 2021-04-17 14:29:01 +02:00
parent 82bc084bad
commit 434d5971bf
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB

View File

@ -159,27 +159,11 @@ static int build_design(design_t **adesigns, const char *cld)
/* _\|/_
(o o)
+----oOO-{_}-OOo------------------------------------------------------------+
| P r o g r a m S t a r t |
+--------------------------------------------------------------------------*/
int main(int argc, char *argv[])
/**
* Process command line options and store the result in the global `opt` struct. May exit the program.
*/
static void handle_command_line(int argc, char *argv[])
{
int rc; /* general return code */
size_t pad;
int i;
int saved_designwidth; /* opt.design->minwith backup, used for mending */
int saved_designheight; /* opt.design->minheight backup, used for mending */
#ifdef DEBUG
fprintf (stderr, "BOXES STARTING ...\n");
#endif
/*
* Process command line options
*/
#ifdef DEBUG
fprintf (stderr, "Processing Command Line ...\n");
#endif
@ -197,19 +181,15 @@ int main(int argc, char *argv[])
}
memcpy(&opt, parsed_opts, sizeof(opt_t));
BFREE(parsed_opts);
}
/*
* Store system character encoding
*/
setlocale(LC_ALL, ""); /* switch from default "C" encoding to system encoding */
encoding = check_encoding(opt.encoding, locale_charset());
#ifdef DEBUG
fprintf (stderr, "Character Encoding = %s\n", encoding);
#endif
/*
* Parse config file(s), then reset design pointer
*/
/**
* Parse config file(s), then reset design pointer. May exit the program.
*/
static void handle_config_parsing()
{
char *config_file = discover_config_file(0);
if (config_file == NULL) {
exit(EXIT_FAILURE);
@ -223,7 +203,7 @@ int main(int argc, char *argv[])
num_designs = (int) r_num_designs;
}
else {
rc = build_design(&designs, opt.cld);
int rc = build_design(&designs, opt.cld);
if (rc) {
exit(EXIT_FAILURE);
}
@ -231,28 +211,17 @@ int main(int argc, char *argv[])
}
BFREE (opt.design);
opt.design = designs;
}
/*
* If "-l" option was given, list designs and exit.
*/
if (opt.l) {
rc = list_designs();
exit(rc);
}
/*
* If "-q" option was given, print results of tag query and exit.
*/
if (opt.query != NULL && opt.query[0] != NULL && !query_is_undoc()) {
rc = query_by_tag();
exit(rc);
}
/*
* Adjust box size and indentmode to command line specification
* Increase box width/height by width/height of empty sides in order
* to match appearance of box with the user's expectations (if -s).
*/
/**
* Adjust box size to command line specification.
* Increase box width/height by width/height of empty sides in order to match appearance of box with the user's
* expectations (if -s).
*/
static void apply_expected_size()
{
if (opt.reqheight > (long) opt.design->minheight) {
opt.design->minheight = opt.reqheight;
}
@ -275,6 +244,188 @@ int main(int argc, char *argv[])
opt.design->minheight += opt.design->shape[SE].height;
}
}
}
/**
* Read all input lines and store the result in the global `input` structure. May exit the program.
*/
static void handle_input()
{
#ifdef DEBUG
fprintf (stderr, "Reading all input ...\n");
#endif
input_t *raw_input = NULL;
if (opt.mend != 0) {
raw_input = read_all_input();
if (raw_input == NULL) {
exit(EXIT_FAILURE);
}
}
if (analyze_input(raw_input ? raw_input : &input)) {
exit(EXIT_FAILURE);
}
if (raw_input) {
memcpy(&input, raw_input, sizeof(input_t));
BFREE(raw_input);
}
if (input.num_lines == 0) {
exit(EXIT_SUCCESS);
}
}
/**
* Adjust box size to fit requested padding value.
* Command line-specified box size takes precedence over padding.
*/
static void adjust_size_and_padding()
{
for (int i = 0; i < NUM_SIDES; ++i) {
if (opt.padding[i] > -1) {
opt.design->padding[i] = opt.padding[i];
}
}
size_t pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
if (pad > 0) {
pad += input.num_lines;
pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
if (pad > opt.design->minheight) {
if (opt.reqheight) {
for (int i = 0; i < (int) (pad - opt.design->minheight); ++i) {
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]) {
opt.design->padding[i % 2 ? BTOP : BBOT] -= 1;
} else {
break;
}
}
}
else {
opt.design->minheight = pad;
}
}
}
pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
if (pad > 0) {
pad += input.maxline;
pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
if (pad > opt.design->minwidth) {
if (opt.reqwidth) {
for (int i = 0; i < (int) (pad - opt.design->minwidth); ++i) {
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]) {
opt.design->padding[i % 2 ? BLEF : BRIG] -= 1;
} else {
break;
}
}
}
else {
opt.design->minwidth = pad;
}
}
}
}
/**
* Generate box. May exit the program.
*/
static void handle_generate_box()
{
#ifdef DEBUG
fprintf (stderr, "Generating Box ...\n");
#endif
sentry_t *thebox = (sentry_t *) calloc(NUM_SIDES, sizeof(sentry_t));
if (thebox == NULL) {
perror(PROJECT);
exit(EXIT_FAILURE);
}
int rc = generate_box(thebox);
if (rc) {
exit(EXIT_FAILURE);
}
output_box(thebox);
}
/**
* Remove box. May exit the program.
*/
static void handle_remove_box()
{
#ifdef DEBUG
fprintf (stderr, "Removing Box ...\n");
#endif
if (opt.killblank == -1) {
if (empty_side(opt.design->shape, BTOP) && empty_side(opt.design->shape, BBOT)) {
opt.killblank = 0;
} else {
opt.killblank = 1;
}
}
int rc = remove_box();
if (rc) {
exit(EXIT_FAILURE);
}
rc = apply_substitutions(&input, 1);
if (rc) {
exit(EXIT_FAILURE);
}
output_input(opt.mend > 0);
}
/* _\|/_
(o o)
+----oOO-{_}-OOo------------------------------------------------------------+
| P r o g r a m S t a r t |
+--------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
int rc; /* general return code */
int saved_designwidth; /* opt.design->minwith backup, used for mending */
int saved_designheight; /* opt.design->minheight backup, used for mending */
#ifdef DEBUG
fprintf (stderr, "BOXES STARTING ...\n");
#endif
handle_command_line(argc, argv);
/* Store system character encoding */
setlocale(LC_ALL, ""); /* switch from default "C" encoding to system encoding */
encoding = check_encoding(opt.encoding, locale_charset());
#ifdef DEBUG
fprintf (stderr, "Character Encoding = %s\n", encoding);
#endif
handle_config_parsing();
/* If "-l" option was given, list designs and exit. */
if (opt.l) {
rc = list_designs();
exit(rc);
}
/* If "-q" option was given, print results of tag query and exit. */
if (opt.query != NULL && opt.query[0] != NULL && !query_is_undoc()) {
rc = query_by_tag();
exit(rc);
}
apply_expected_size();
if (opt.indentmode) {
opt.design->indentmode = opt.indentmode;
}
@ -289,126 +440,15 @@ int main(int argc, char *argv[])
opt.design->minwidth = saved_designwidth;
opt.design->minheight = saved_designheight;
/*
* Read input lines
*/
#ifdef DEBUG
fprintf (stderr, "Reading all input ...\n");
#endif
input_t *raw_input = NULL;
if (opt.mend != 0) {
raw_input = read_all_input();
if (raw_input == NULL) {
exit(EXIT_FAILURE);
}
}
if (analyze_input(raw_input ? raw_input : &input)) {
exit(EXIT_FAILURE);
}
if (raw_input) {
memcpy(&input, raw_input, sizeof(input_t));
BFREE(raw_input);
}
if (input.num_lines == 0) {
exit(EXIT_SUCCESS);
}
handle_input();
/*
* Adjust box size to fit requested padding value
* Command line-specified box size takes precedence over padding.
*/
for (i = 0; i < NUM_SIDES; ++i) {
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.num_lines;
pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
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]) {
opt.design->padding[i % 2 ? BBOT : BTOP] -= 1;
} else if (opt.design->padding[i % 2 ? BTOP : BBOT]) {
opt.design->padding[i % 2 ? BTOP : BBOT] -= 1;
} else {
break;
}
}
}
else {
opt.design->minheight = pad;
}
}
}
pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
if (pad > 0) {
pad += input.maxline;
pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
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]) {
opt.design->padding[i % 2 ? BRIG : BLEF] -= 1;
} else if (opt.design->padding[i % 2 ? BLEF : BRIG]) {
opt.design->padding[i % 2 ? BLEF : BRIG] -= 1;
} else {
break;
}
}
}
else {
opt.design->minwidth = pad;
}
}
}
adjust_size_and_padding();
if (opt.r) {
/*
* Remove box
*/
#ifdef DEBUG
fprintf (stderr, "Removing Box ...\n");
#endif
if (opt.killblank == -1) {
if (empty_side(opt.design->shape, BTOP) && empty_side(opt.design->shape, BBOT)) {
opt.killblank = 0;
} else {
opt.killblank = 1;
}
}
rc = remove_box();
if (rc) {
exit(EXIT_FAILURE);
}
rc = apply_substitutions(&input, 1);
if (rc) {
exit(EXIT_FAILURE);
}
output_input(opt.mend > 0);
handle_remove_box();
}
else {
/*
* Generate box
*/
sentry_t *thebox;
#ifdef DEBUG
fprintf (stderr, "Generating Box ...\n");
#endif
thebox = (sentry_t *) calloc(NUM_SIDES, sizeof(sentry_t));
if (thebox == NULL) {
perror(PROJECT);
exit(EXIT_FAILURE);
}
rc = generate_box(thebox);
if (rc) {
exit(EXIT_FAILURE);
}
output_box(thebox);
handle_generate_box();
}
} while (opt.mend > 0);