mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-08-13 08:37:23 +02:00
Extract smaller functions from the main() function #78
This commit is contained in:
212
src/boxes.c
212
src/boxes.c
@ -159,27 +159,11 @@ static int build_design(design_t **adesigns, const char *cld)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* _\|/_
|
/**
|
||||||
(o o)
|
* Process command line options and store the result in the global `opt` struct. May exit the program.
|
||||||
+----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 */
|
|
||||||
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
|
|
||||||
*/
|
*/
|
||||||
|
static void handle_command_line(int argc, char *argv[])
|
||||||
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "Processing Command Line ...\n");
|
fprintf (stderr, "Processing Command Line ...\n");
|
||||||
#endif
|
#endif
|
||||||
@ -197,19 +181,15 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
memcpy(&opt, parsed_opts, sizeof(opt_t));
|
memcpy(&opt, parsed_opts, sizeof(opt_t));
|
||||||
BFREE(parsed_opts);
|
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);
|
char *config_file = discover_config_file(0);
|
||||||
if (config_file == NULL) {
|
if (config_file == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -223,7 +203,7 @@ int main(int argc, char *argv[])
|
|||||||
num_designs = (int) r_num_designs;
|
num_designs = (int) r_num_designs;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rc = build_design(&designs, opt.cld);
|
int rc = build_design(&designs, opt.cld);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -231,28 +211,17 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
BFREE (opt.design);
|
BFREE (opt.design);
|
||||||
opt.design = designs;
|
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
|
* Adjust box size to command line specification.
|
||||||
* to match appearance of box with the user's expectations (if -s).
|
* 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) {
|
if (opt.reqheight > (long) opt.design->minheight) {
|
||||||
opt.design->minheight = opt.reqheight;
|
opt.design->minheight = opt.reqheight;
|
||||||
}
|
}
|
||||||
@ -275,23 +244,15 @@ int main(int argc, char *argv[])
|
|||||||
opt.design->minheight += opt.design->shape[SE].height;
|
opt.design->minheight += opt.design->shape[SE].height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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: */
|
|
||||||
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;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Read input lines
|
/**
|
||||||
|
* Read all input lines and store the result in the global `input` structure. May exit the program.
|
||||||
*/
|
*/
|
||||||
|
static void handle_input()
|
||||||
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "Reading all input ...\n");
|
fprintf (stderr, "Reading all input ...\n");
|
||||||
#endif
|
#endif
|
||||||
@ -312,23 +273,29 @@ int main(int argc, char *argv[])
|
|||||||
if (input.num_lines == 0) {
|
if (input.num_lines == 0) {
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Adjust box size to fit requested padding value
|
|
||||||
|
/**
|
||||||
|
* Adjust box size to fit requested padding value.
|
||||||
* Command line-specified box size takes precedence over padding.
|
* Command line-specified box size takes precedence over padding.
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < NUM_SIDES; ++i) {
|
static void adjust_size_and_padding()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NUM_SIDES; ++i) {
|
||||||
if (opt.padding[i] > -1) {
|
if (opt.padding[i] > -1) {
|
||||||
opt.design->padding[i] = opt.padding[i];
|
opt.design->padding[i] = opt.padding[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
|
|
||||||
|
size_t pad = opt.design->padding[BTOP] + opt.design->padding[BBOT];
|
||||||
if (pad > 0) {
|
if (pad > 0) {
|
||||||
pad += input.num_lines;
|
pad += input.num_lines;
|
||||||
pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
|
pad += opt.design->shape[NW].height + opt.design->shape[SW].height;
|
||||||
if (pad > opt.design->minheight) {
|
if (pad > opt.design->minheight) {
|
||||||
if (opt.reqheight) {
|
if (opt.reqheight) {
|
||||||
for (i = 0; i < (int) (pad - opt.design->minheight); ++i) {
|
for (int 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;
|
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]) {
|
||||||
@ -343,13 +310,14 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
|
pad = opt.design->padding[BLEF] + opt.design->padding[BRIG];
|
||||||
if (pad > 0) {
|
if (pad > 0) {
|
||||||
pad += input.maxline;
|
pad += input.maxline;
|
||||||
pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
|
pad += opt.design->shape[NW].width + opt.design->shape[NE].width;
|
||||||
if (pad > opt.design->minwidth) {
|
if (pad > opt.design->minwidth) {
|
||||||
if (opt.reqwidth) {
|
if (opt.reqwidth) {
|
||||||
for (i = 0; i < (int) (pad - opt.design->minwidth); ++i) {
|
for (int 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;
|
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]) {
|
||||||
@ -364,11 +332,37 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (opt.r) {
|
|
||||||
/*
|
|
||||||
* Remove box
|
/**
|
||||||
|
* 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
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "Removing Box ...\n");
|
fprintf (stderr, "Removing Box ...\n");
|
||||||
#endif
|
#endif
|
||||||
@ -379,7 +373,7 @@ int main(int argc, char *argv[])
|
|||||||
opt.killblank = 1;
|
opt.killblank = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rc = remove_box();
|
int rc = remove_box();
|
||||||
if (rc) {
|
if (rc) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -390,25 +384,71 @@ int main(int argc, char *argv[])
|
|||||||
output_input(opt.mend > 0);
|
output_input(opt.mend > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
|
||||||
/*
|
|
||||||
* Generate box
|
/* _\|/_
|
||||||
*/
|
(o o)
|
||||||
sentry_t *thebox;
|
+----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
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "Generating Box ...\n");
|
fprintf (stderr, "BOXES STARTING ...\n");
|
||||||
#endif
|
#endif
|
||||||
thebox = (sentry_t *) calloc(NUM_SIDES, sizeof(sentry_t));
|
|
||||||
if (thebox == NULL) {
|
handle_command_line(argc, argv);
|
||||||
perror(PROJECT);
|
|
||||||
exit(EXIT_FAILURE);
|
/* 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);
|
||||||
}
|
}
|
||||||
rc = generate_box(thebox);
|
|
||||||
if (rc) {
|
/* If "-q" option was given, print results of tag query and exit. */
|
||||||
exit(EXIT_FAILURE);
|
if (opt.query != NULL && opt.query[0] != NULL && !query_is_undoc()) {
|
||||||
|
rc = query_by_tag();
|
||||||
|
exit(rc);
|
||||||
}
|
}
|
||||||
output_box(thebox);
|
|
||||||
|
apply_expected_size();
|
||||||
|
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: */
|
||||||
|
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;
|
||||||
|
|
||||||
|
handle_input();
|
||||||
|
|
||||||
|
adjust_size_and_padding();
|
||||||
|
|
||||||
|
if (opt.r) {
|
||||||
|
handle_remove_box();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
handle_generate_box();
|
||||||
}
|
}
|
||||||
} while (opt.mend > 0);
|
} while (opt.mend > 0);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user