From a03e48a871f775901c0946f3e4eb34f41a7e5e1a Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Fri, 29 May 2015 18:53:45 -0400 Subject: enable building without curses --- src/globals.h | 9 ++++++++- src/help.c | 9 +++++++-- src/main.c | 6 +++--- src/util.c | 31 ++++++++++++++++++++++++------- 4 files changed, 42 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/globals.h b/src/globals.h index efddce3..58fc0cd 100644 --- a/src/globals.h +++ b/src/globals.h @@ -1,24 +1,31 @@ #ifndef _MARKET_SIM_H_ #define _MARKET_SIM_H_ +//#define WITHOUT_CURSES + #include #include #include #include +#include #include #include #include #include #include -#include #include #include #include + +#ifndef WITHOUT_CURSES #include +#endif #define ARRAYLEN(x) (sizeof(x) / sizeof(x[0])) +#define PROGRAM_NAME "market-sim" + /* VERSION_INFO is supplied by the compiler */ #define PROGRAM_VERSION "v0.3 (" VERSION_INFO ")" diff --git a/src/help.c b/src/help.c index afa2abf..d7732ad 100644 --- a/src/help.c +++ b/src/help.c @@ -19,8 +19,13 @@ void print_usage(int argc, char *argv[]) void print_version(void) { - output("market-sim " PROGRAM_VERSION "\n"); - output("Built with %s.\n", curl_version()); + output(PROGRAM_NAME " " PROGRAM_VERSION "\n"); + output("%s\n", curl_version()); +#ifndef WITHOUT_CURSES + output("%s\n", curses_version()); +#else + output("Built without curses support.\n"); +#endif output("Build date: %s\n", __DATE__); output("Copyright (C) 2015 Franklin Wei.\n\n"); output("License GPLv2: GNU GPL version 2 \n"); diff --git a/src/main.c b/src/main.c index 3c65505..ad8fd63 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,9 @@ int main(int argc, char *argv[]) uint args_status = parse_args(argc, argv, save_file_p); + if(args_status & ARG_FAILURE) + return EXIT_FAILURE; + if(!(args_status & ARG_NOCURSES) && !(args_status & ARG_BATCHMODE)) curses_init(); @@ -38,9 +41,6 @@ int main(int argc, char *argv[]) heading("Market Simulator " PROGRAM_VERSION); - if(args_status & ARG_FAILURE) - return EXIT_FAILURE; - if(args_status & ARG_RESTRICTED) restricted = true; diff --git a/src/util.c b/src/util.c index 7f58436..69896fa 100644 --- a/src/util.c +++ b/src/util.c @@ -6,7 +6,9 @@ void cleanup(void) { curl_global_cleanup(); +#ifndef WITHOUT_CURSES endwin(); +#endif } void sig_handler(int sig) @@ -75,7 +77,7 @@ bool get_stock_info(char *symbol, struct money_t *price, char **name_ret) if(res != CURLE_OK || buf.data[0] != '"') { - fail("Failed to query information for '%s'.", symbol); + return false; } /* null-terminate buffer */ @@ -224,6 +226,7 @@ void print_handler(struct player_t *player) output("Total capital: $%llu.%02llu\n", total / 100, total % 100); } +#ifndef WITHOUT_CURSES static char *read_string_curses(void) { char *ret = malloc(1); @@ -245,6 +248,7 @@ static char *read_string_curses(void) return ret; } +#endif static char *read_string_nocurses(void) { @@ -350,9 +354,7 @@ uint parse_args(int argc, char *argv[], char **port_file) } else { - output("Unrecognized option '%s'\n", arg, argv[0]); - print_usage(argc, argv); - ret |= ARG_FAILURE; + fail("Unrecognized option '%s'\nUse --help for usage information.", arg, argv[0]); } } else @@ -388,11 +390,12 @@ void fail(const char *fmt, ...) va_end(ap); cleanup(); - fprintf(stdout, "FATAL: %s\n", buf); + fprintf(stderr, "FATAL: %s\n", buf); exit(EXIT_FAILURE); } +#ifndef WITHOUT_CURSES static int curses_printf(const char *fmt, ...) { va_list ap; @@ -406,9 +409,11 @@ static int curses_printf(const char *fmt, ...) return ret; } +#endif int (*output)(const char*, ...) = printf; +#ifndef WITHOUT_CURSES void horiz_line_curses(void) { for(int i = 0; i < getmaxx(stdscr); ++i) @@ -416,6 +421,7 @@ void horiz_line_curses(void) output("="); } } +#endif void horiz_line_nocurses(void) { @@ -431,6 +437,7 @@ void horiz_line_nocurses(void) void (*horiz_line)(void) = horiz_line_nocurses; +#ifndef WITHOUT_CURSES void heading_curses(const char *fmt, ...) { char text[128]; @@ -453,6 +460,7 @@ void heading_curses(const char *fmt, ...) for(int i = 0; i < getmaxx(stdscr) - getmaxx(stdscr) / 2 - len - 1 - d; ++i) output("="); } +#endif void heading_nocurses(const char *fmt, ...) { @@ -492,11 +500,14 @@ bool html_out = false; void use_color(int col) { +#ifndef WITHOUT_CURSES if(have_color) { attron(COLOR_PAIR(col)); } - else if(html_out) + else +#endif + if(html_out) { uchar r, g, b; switch(col) @@ -521,18 +532,23 @@ void use_color(int col) void stop_color(int col) { +#ifndef WITHOUT_CURSES if(have_color) { attroff(COLOR_PAIR(col)); } - else if(html_out) + else +#endif + if(html_out) { + (void) col; output(""); } } void curses_init(void) { +#ifndef WITHOUT_CURSES initscr(); echo(); nocbreak(); @@ -558,6 +574,7 @@ void curses_init(void) { have_color = false; } +#endif } bool batch_mode = false; -- cgit v1.1