X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fmisc.c;h=6cbd9655e60cd32ed28a514fef251691a8736932;hp=1ab116d6b771458990b295f4d676b32f36707142;hb=e5c5bf5c4a76a04f9bf64e92227bf2ef969fd25c;hpb=823bddb0d9cc63ddda17a2cd20266aa3b82bde38 diff --git a/src/misc.c b/src/misc.c index 1ab116d6..6cbd9655 100644 --- a/src/misc.c +++ b/src/misc.c @@ -11,17 +11,19 @@ * misc.c * ***********************************************************/ -#include "misc.h" -#include "tools.h" -#include "sound.h" -#include "random.h" - #include #include #include #include #include #include +#include + +#include "misc.h" +#include "init.h" +#include "tools.h" +#include "sound.h" +#include "random.h" static unsigned long mainCounter(int mode) { @@ -73,8 +75,7 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) delay.tv_usec = 1000 * (milliseconds_delay % 1000); if (select(0, NULL, NULL, NULL, &delay) != 0) - fprintf(stderr,"%s: in function sleep_milliseconds: select() failed!\n", - progname); + Error(ERR_RETURN, "sleep_milliseconds(): select() failed"); } } @@ -83,7 +84,8 @@ void Delay(unsigned long delay) /* Sleep specified number of milliseconds */ sleep_milliseconds(delay); } -BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay) +boolean FrameReached(unsigned long *frame_counter_var, + unsigned long frame_delay) { unsigned long actual_frame_counter = FrameCounter; @@ -95,7 +97,8 @@ BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay) return(TRUE); } -BOOL DelayReached(unsigned long *counter_var, unsigned long delay) +boolean DelayReached(unsigned long *counter_var, + unsigned long delay) { unsigned long actual_counter = Counter(); @@ -187,3 +190,204 @@ void MarkTileDirty(int x, int y) redraw_mask |= REDRAW_TILES; } } + +void GetOptions(char *argv[]) +{ + char **options_left = &argv[1]; + + /* initialize global program options */ + options.display_name = NULL; + options.server_host = NULL; + options.server_port = 0; + options.serveronly = FALSE; + options.network = FALSE; + options.verbose = FALSE; + + while (*options_left) + { + char option_str[MAX_OPTION_LEN]; + char *option = options_left[0]; + char *next_option = options_left[1]; + char *option_arg = NULL; + int option_len = strlen(option); + + strcpy(option_str, option); /* copy argument into buffer */ + option = option_str; + + if (strcmp(option, "--") == 0) /* stop scanning arguments */ + break; + + if (option_len >= MAX_OPTION_LEN) + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option); + + if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */ + option++; + + option_arg = strchr(option, '='); + if (option_arg == NULL) /* no '=' in option */ + option_arg = next_option; + else + { + *option_arg++ = '\0'; /* cut argument from option */ + if (*option_arg == '\0') /* no argument after '=' */ + Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str); + } + + option_len = strlen(option); + + if (strcmp(option, "-") == 0) + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option); + else if (strncmp(option, "-help", option_len) == 0) + { + printf("Usage: %s [options] [server.name [port]]\n" + "Options:\n" + " -d, --display machine:0 X server display\n" + " -l, --levels directory alternative level directory\n" + " -s, --serveronly only start network server\n" + " -n, --network network multiplayer game\n" + " -v, --verbose verbose mode\n", + program_name); + exit(0); + } + else if (strncmp(option, "-display", option_len) == 0) + { + if (option_arg == NULL) + Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); + + options.display_name = option_arg; + if (option_arg == next_option) + options_left++; + + printf("--display == '%s'\n", options.display_name); + } + else if (strncmp(option, "-levels", option_len) == 0) + { + if (option_arg == NULL) + Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); + + level_directory = option_arg; + if (option_arg == next_option) + options_left++; + + printf("--levels == '%s'\n", level_directory); + } + else if (strncmp(option, "-network", option_len) == 0) + { + printf("--network\n"); + + options.network = TRUE; + } + else if (strncmp(option, "-serveronly", option_len) == 0) + { + printf("--serveronly\n"); + + options.serveronly = TRUE; + } + else if (strncmp(option, "-verbose", option_len) == 0) + { + printf("--verbose\n"); + + options.verbose = TRUE; + } + else if (*option == '-') + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str); + else if (options.server_host == NULL) + { + options.server_host = *options_left; + + printf("server.name == '%s'\n", options.server_host); + } + else if (options.server_port == 0) + { + options.server_port = atoi(*options_left); + if (options.server_port < 1024) + Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port); + + printf("port == %d\n", options.server_port); + } + else + Error(ERR_EXIT_HELP, "too many arguments"); + + options_left++; + } +} + +void Error(int mode, char *format_str, ...) +{ + FILE *output_stream = stderr; + char *process_name = ""; + + if (mode == ERR_EXIT_SOUNDSERVER) + process_name = " sound server"; + + if (format_str) + { + va_list ap; + char *format_ptr; + char *s_value; + int i_value; + double d_value; + + fprintf(output_stream, "%s%s: ", program_name, process_name); + + va_start(ap, format_str); /* ap points to first unnamed argument */ + + for(format_ptr=format_str; *format_ptr; format_ptr++) + { + if (*format_ptr != '%') + { + fprintf(output_stream, "%c", *format_ptr); + continue; + } + + switch(*++format_ptr) + { + case 'd': + i_value = va_arg(ap, int); + fprintf(output_stream, "%d", i_value); + break; + + case 'f': + d_value = va_arg(ap, double); + fprintf(output_stream, "%f", d_value); + break; + + case 's': + s_value = va_arg(ap, char *); + fprintf(output_stream, "%s", s_value); + break; + + default: + fprintf(stderr, "\n%s: Error(): invalid format string: %s\n", + program_name, format_str); + CloseAllAndExit(10); + } + } + + va_end(ap); + + fprintf(output_stream, "\n"); + } + + if (mode == ERR_EXIT_HELP) + fprintf(output_stream, "%s: Try option '--help' for more information.\n", + program_name); + + if (mode != ERR_RETURN) + { + fprintf(output_stream, "%s%s: aborting\n", program_name, process_name); + CloseAllAndExit(1); + } +} + +void *checked_malloc(unsigned long size) +{ + void *ptr; + + ptr = malloc(size); + + if (ptr == NULL) + Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size); + + return ptr; +}