From: Holger Schemel Date: Fri, 2 Oct 1998 15:13:32 +0000 (+0200) Subject: rnd-19981002-1 X-Git-Tag: 1.2.0^2~68 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=29014045f4de045f8452fdf7ab32622c94b37eef;p=rocksndiamonds.git rnd-19981002-1 --- diff --git a/src/Makefile b/src/Makefile index f390e365..a6ce59b4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -23,6 +23,7 @@ SCORE_ENTRIES = -DMANY_PER_NAME # many score entries per name # files (and the mask files ('*Maske.gif') to xbm files). # XPM_INCLUDE_FILE = -DXPM_INCLUDE_FILE="" +# EXTRA_X11_LIBS = -lXpm CONFIG = $(GAME_DIR) $(SOUNDS) $(JOYSTICK) \ $(SCORE_ENTRIES) $(XPM_INCLUDE_FILE) @@ -41,7 +42,9 @@ DEBUG = -DDEBUG -g -Wall # # for SunOS and others # LIBS = -L/usr/X11R6/lib -lXpm -lX11 -lm -LIBS = -L/usr/X11R6/lib -lX11 -lm +# LIBS = -L/usr/X11R6/lib -lX11 -lm + +LIBS = -L/usr/X11R6/lib $(EXTRA_X11_LIBS) -lX11 -lm # CFLAGS = -O2 $(CONFIG) $(SYSTEM) CFLAGS = $(DEBUG) $(CONFIG) $(SYSTEM) $(INCL) @@ -61,7 +64,6 @@ SRCS = main.c \ joystick.c \ cartoons.c \ random.c \ - gfxload.c \ gifload.c \ gif.c \ send.c \ @@ -83,7 +85,6 @@ OBJS = main.o \ joystick.o \ cartoons.o \ random.o \ - gfxload.o \ gifload.o \ gif.o \ send.o \ diff --git a/src/files.c b/src/files.c index 9e35c060..9030319d 100644 --- a/src/files.c +++ b/src/files.c @@ -83,14 +83,14 @@ BOOL LoadLevelInfo() if (!(file=fopen(filename,"r"))) { - fprintf(stderr,"%s: cannot load level info '%s'!\n",progname,filename); + Error(ERR_RETURN, "cannot load level info '%s'", filename); return(FALSE); } fscanf(file,"%s\n",cookie); if (strcmp(cookie,LEVELDIR_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of level info file!\n",progname); + Error(ERR_RETURN, "wrong format of level info file"); fclose(file); return(FALSE); } @@ -111,7 +111,7 @@ BOOL LoadLevelInfo() if (!num_leveldirs) { - fprintf(stderr,"%s: empty level info '%s'!\n",progname,filename); + Error(ERR_RETURN, "empty level info '%s'", filename); return(FALSE); } @@ -128,10 +128,10 @@ void LoadLevel(int level_nr) sprintf(filename,"%s/%s/%d", level_directory,leveldir[leveldir_nr].filename,level_nr); - if (!(file=fopen(filename,"r"))) + if (!(file = fopen(filename,"r"))) { /* - fprintf(stderr,"%s: cannot load level '%s'!\n",progname,filename); + Error(ERR_RETURN, "cannot load level '%s'", filename); */ } else @@ -141,8 +141,7 @@ void LoadLevel(int level_nr) if (strcmp(cookie,LEVEL_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of level file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of level file '%s'", filename); fclose(file); file = NULL; } @@ -237,8 +236,7 @@ void LoadLevelTape(int level_nr) levelrec_10 = TRUE; else if (strcmp(cookie,LEVELREC_COOKIE)) /* unknown tape format */ { - fprintf(stderr,"%s: wrong format of level recording file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of level recording file '%s'", filename); fclose(file); file = NULL; } @@ -288,8 +286,7 @@ void LoadLevelTape(int level_nr) fclose(file); if (i != tape.length) - fprintf(stderr,"%s: level recording file '%s' corrupted!\n", - progname,filename); + Error(ERR_RETURN, "level recording file '%s' corrupted", filename); tape.length_seconds = GetTapeLength(); } @@ -304,18 +301,12 @@ void LoadScore(int level_nr) sprintf(filename,"%s/%s/%s", level_directory,leveldir[leveldir_nr].filename,SCORE_FILENAME); - if (!(file=fopen(filename,"r"))) + if (!(file = fopen(filename,"r"))) { if (!CreateNewScoreFile()) - { - fprintf(stderr,"%s: cannot create score file '%s'!\n", - progname,filename); - } - else if (!(file=fopen(filename,"r"))) - { - fprintf(stderr,"%s: cannot load score for level %d!\n", - progname,level_nr); - } + Error(ERR_RETURN, "cannot create score file '%s'", filename); + else if (!(file = fopen(filename,"r"))) + Error(ERR_RETURN, "cannot load score for level %d", level_nr); } if (file) @@ -323,7 +314,7 @@ void LoadScore(int level_nr) fgets(cookie,SCORE_COOKIE_LEN,file); if (strcmp(cookie,SCORE_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of score file!\n",progname); + Error(ERR_RETURN, "wrong format of score file '%s'", filename); fclose(file); file = NULL; } @@ -379,18 +370,12 @@ void LoadPlayerInfo(int mode) new_player = default_player; - if (!(file=fopen(filename,"r"))) + if (!(file = fopen(filename,"r"))) { if (!CreateNewNamesFile(mode)) - { - fprintf(stderr,"%s: cannot create names file '%s'!\n", - progname,filename); - } - else if (!(file=fopen(filename,"r"))) - { - fprintf(stderr,"%s: cannot load player information '%s'!\n", - progname,filename); - } + Error(ERR_RETURN, "cannot create names file '%s'", filename); + else if (!(file = fopen(filename,"r"))) + Error(ERR_RETURN, "cannot load player information file '%s'", filename); } if (file) @@ -400,8 +385,7 @@ void LoadPlayerInfo(int mode) version_10_file = TRUE; else if (strcmp(cookie,NAMES_COOKIE)) /* ungültiges Format? */ { - fprintf(stderr,"%s: wrong format of names file '%s'!\n", - progname,filename); + Error(ERR_RETURN, "wrong format of names file '%s'", filename); fclose(file); file = NULL; } @@ -437,11 +421,9 @@ void LoadPlayerInfo(int mode) new_player = default_player; fclose(file); - if (!(file=fopen(filename,"a"))) - { - fprintf(stderr,"%s: cannot append new player to names file '%s'!\n", - progname,filename); - } + if (!(file = fopen(filename,"a"))) + Error(ERR_RETURN, "cannot append new player to names file '%s'", + filename); else { for(i=0;ialias_name); SendProtocolVersionToServer(); @@ -119,15 +115,15 @@ void InitSound() #ifndef MSDOS if (access(sound_device_name,W_OK)<0) { - fprintf(stderr,"%s: cannot access sound device - no sounds\n",progname); - sound_status=SOUND_OFF; + Error(ERR_RETURN, "cannot access sound device - no sounds"); + sound_status = SOUND_OFF; return; } if ((sound_device=open(sound_device_name,O_WRONLY))<0) { - fprintf(stderr,"%s: cannot open sound device - no sounds\n",progname); - sound_status=SOUND_OFF; + Error(ERR_RETURN, "cannot open sound device - no sounds"); + sound_status = SOUND_OFF; return; } @@ -165,14 +161,14 @@ void InitSoundProcess() #ifndef MSDOS if (pipe(sound_pipe)<0) { - fprintf(stderr,"%s: cannot create pipe - no sounds\n",progname); + Error(ERR_RETURN, "cannot create pipe - no sounds"); sound_status=SOUND_OFF; return; } if ((sound_process_id=fork())<0) { - fprintf(stderr,"%s: cannot create child process - no sounds\n",progname); + Error(ERR_RETURN, "cannot create child process - no sounds"); sound_status=SOUND_OFF; return; } @@ -194,16 +190,16 @@ void InitJoystick() #ifndef MSDOS if (access(joystick_device_name[joystick_nr],R_OK)<0) { - fprintf(stderr,"%s: cannot access joystick device '%s'\n", - progname,joystick_device_name[joystick_nr]); + Error(ERR_RETURN, "cannot access joystick device '%s'", + joystick_device_name[joystick_nr]); joystick_status = JOYSTICK_OFF; return; } if ((joystick_device=open(joystick_device_name[joystick_nr],O_RDONLY))<0) { - fprintf(stderr,"%s: cannot open joystick device '%s'\n", - progname,joystick_device_name[joystick_nr]); + Error(ERR_RETURN, "cannot open joystick device '%s'", + joystick_device_name[joystick_nr]); joystick_status = JOYSTICK_OFF; return; } @@ -215,37 +211,16 @@ void InitJoystick() #endif } -void InitDisplay(int argc, char *argv[]) +void InitDisplay() { - char *display_name = NULL; XVisualInfo vinfo_template, *vinfo; int num_visuals; unsigned int depth; - int i; - - /* get X server to connect to, if given as an argument */ - for (i=1;ipicture_filename); - CloseAll(); - exit(-1); - } + Error(ERR_EXIT, "cannot get graphics for '%s'", pic->picture_filename); } /* zugehörige Maske laden (wenn vorhanden) */ @@ -784,12 +734,7 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) #endif if (!clipmask[pos]) - { - fprintf(stderr, "%s: cannot get clipmask for '%s'.\n", - progname, pic->picture_filename); - CloseAll(); - exit(-1); - } + Error(ERR_EXIT, "cannot get clipmask for '%s'", pic->picture_filename); } } diff --git a/src/joystick.c b/src/joystick.c index 0e1a42cd..8392a8a1 100644 --- a/src/joystick.c +++ b/src/joystick.c @@ -16,6 +16,7 @@ #endif #include "joystick.h" +#include "misc.h" /* #include "tools.h" @@ -93,8 +94,7 @@ int Joystick() #ifndef MSDOS if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl)) { - fprintf(stderr,"%s: cannot read joystick settings - no joystick support\n", - progname); + Error(ERR_RETURN, "cannot read joystick settings - no joystick support"); joystick_status = JOYSTICK_OFF; return(0); } diff --git a/src/main.c b/src/main.c index 53157870..66f85d38 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include "events.h" #include "sound.h" #include "joystick.h" +#include "misc.h" #ifdef MSDOS #include @@ -192,42 +193,42 @@ int background_loop[] = }; int num_bg_loops = sizeof(background_loop)/sizeof(int); -char *progname; +char *program_name; #define MAX_OPTION_LEN 1024 static void fatal_option() { fprintf(stderr,"Try '%s --help' for more information.\n", - progname); + program_name); exit(1); } static void fatal_unrecognized_option(char *option) { fprintf(stderr,"%s: unrecognized option '%s'\n", - progname, option); + program_name, option); fatal_option(); } static void fatal_option_requires_argument(char *option) { fprintf(stderr,"%s: option '%s' requires an argument\n", - progname, option); + program_name, option); fatal_option(); } -static void fatal_invalid_argument(char *option) +static void fatal_invalid_option_argument(char *option) { fprintf(stderr,"%s: option '%s' has invalid argument\n", - progname, option); + program_name, option); fatal_option(); } static void fatal_too_many_arguments() { fprintf(stderr,"%s: too many arguments\n", - progname); + program_name); fatal_option(); } @@ -237,31 +238,27 @@ int main(int argc, char *argv[]) { char **options_left = &argv[1]; - progname = &argv[0][strlen(argv[0])]; - while (progname != argv[0]) - if (*progname-- == '/') - break; + program_name = (strrchr(argv[0],'/') ? strrchr(argv[0],'/') + 1 : argv[0]); - while (options_left) + 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; + int option_len = strlen(option); - if (strcmp(option, "--") == 0) /* end of argument list */ - break; + strcpy(option_str, option); /* copy argument into buffer */ + option = option_str; - if (strncmp(option, "--", 2)) /* treat '--' like '-' */ - option++; - option_len = strlen(option); + if (strcmp(option, "--") == 0) /* stop scanning arguments */ + break; if (option_len >= MAX_OPTION_LEN) fatal_unrecognized_option(option); - strcpy(option_str, option); - option = option_str; + if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */ + option++; option_arg = strchr(option, '='); if (option_arg == NULL) /* no '=' in option */ @@ -270,49 +267,66 @@ int main(int argc, char *argv[]) { *option_arg++ = '\0'; /* cut argument from option */ if (*option_arg == '\0') /* no argument after '=' */ - fatal_invalid_argument(option); + fatal_invalid_option_argument(option); } - if (strncmp(option, "-help", option_len) == 0) + option_len = strlen(option); + + if (strcmp(option, "-") == 0) + fatal_unrecognized_option(option); + else if (strncmp(option, "-help", option_len) == 0) { - printf("Usage: rocksndiamonds [options] [server.name [port]]\n" + 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" - " -v, --verbose verbose mode\n"); + " -v, --verbose verbose mode\n", + program_name); exit(0); } else if (strncmp(option, "-display", option_len) == 0) { if (option_arg == NULL) - fatal_option_requires_argument(option); + fatal_option_requires_argument(option_str); display_name = option_arg; if (option_arg == next_option) options_left++; + + printf("--display == '%s'\n", display_name); } else if (strncmp(option, "-levels", option_len) == 0) { if (option_arg == NULL) - fatal_option_requires_argument(option); + fatal_option_requires_argument(option_str); level_directory = option_arg; if (option_arg == next_option) options_left++; + + printf("--levels == '%s'\n", level_directory); } else if (strncmp(option, "-verbose", option_len) == 0) { + printf("--verbose\n"); + verbose = TRUE; } else if (*option == '-') - fatal_unrecognized_option(option); + fatal_unrecognized_option(option_str); else if (server_host == NULL) + { server_host = *options_left; + + printf("server.name == '%s'\n", server_host); + } else if (server_port == 0) { server_port = atoi(*options_left); if (server_port < 1024) fatal("Bad port number"); + + printf("port == %d\n", server_port); } else fatal_too_many_arguments(); @@ -320,7 +334,10 @@ int main(int argc, char *argv[]) options_left++; } - + /* + printf("All went fine -- exiting\n"); + exit(0); + */ /* if (argc>1) diff --git a/src/main.h b/src/main.h index fde687b1..c8d1a717 100644 --- a/src/main.h +++ b/src/main.h @@ -321,6 +321,7 @@ extern char *joystick_device_name[2]; extern char *level_directory; extern int width, height; +extern char *display_name; extern char *server_host; extern int server_port; extern int networking; @@ -392,7 +393,7 @@ extern char *sound_name[]; extern int background_loop[]; extern int num_bg_loops; -extern char *progname; +extern char *program_name; /* often used screen positions */ diff --git a/src/misc.c b/src/misc.c index 253a8d7f..c4c0352d 100644 --- a/src/misc.c +++ b/src/misc.c @@ -17,8 +17,10 @@ #include #include #include +#include #include "misc.h" +#include "init.h" #include "tools.h" #include "sound.h" #include "random.h" @@ -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"); } } @@ -187,3 +188,59 @@ void MarkTileDirty(int x, int y) redraw_mask |= REDRAW_TILES; } } + +void Error(BOOL fatal_error, char *format_str, ...) +{ + FILE *output_stream = stderr; + va_list ap; + char *format_ptr; + char *s_value; + int i_value; + double d_value; + + va_start(ap, format_str); /* ap points to first unnamed argument */ + + fprintf(output_stream, "%s: ", program_name); + + 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, "\nfatal(): invalid format string: %s\n", format_str); + exit(-1); + } + } + + va_end(ap); + + fprintf(output_stream, "\n"); + + if (fatal_error) + { + fprintf(output_stream, "%s: aborting\n", program_name); + CloseAll(); + exit(1); + } +} diff --git a/src/misc.h b/src/misc.h index 99c867a0..b3f530e7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -21,6 +21,9 @@ #define NEW_RANDOMIZE -1 +#define ERR_RETURN 0 +#define ERR_EXIT 1 + void InitCounter(void); unsigned long Counter(void); void Delay(unsigned long); @@ -33,5 +36,6 @@ unsigned int RND(unsigned int); unsigned int InitRND(long); char *GetLoginName(void); void MarkTileDirty(int, int); +void Error(BOOL, char *, ...); #endif diff --git a/src/sound.c b/src/sound.c index af9af974..4a323818 100644 --- a/src/sound.c +++ b/src/sound.c @@ -12,6 +12,8 @@ ***********************************************************/ #include "sound.h" +#include "misc.h" + #ifdef MSDOS extern void sound_handler(struct SoundControl); #endif @@ -71,10 +73,7 @@ void SoundServer() if (!FD_ISSET(sound_pipe[0], &sound_fdset)) continue; if (read(sound_pipe[0], &snd_ctrl, sizeof(snd_ctrl)) != sizeof(snd_ctrl)) - { - fprintf(stderr,"%s: broken pipe - no sounds\n",progname); - exit(0); - } + Error(ERR_EXIT, "broken pipe - no sounds"); #ifdef VOXWARE @@ -484,22 +483,13 @@ void HPUX_Audio_Control() audio_ctl = open("/dev/audioCtl", O_WRONLY | O_NDELAY); if (audio_ctl == -1) - { - fprintf(stderr,"%s: cannot open /dev/audioCtl - no sounds\n",progname); - exit(0); - } + Error(ERR_EXIT, "cannot open /dev/audioCtl - no sounds"); if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1) - { - fprintf(stderr,"%s: no audio info - no sounds\n",progname); - exit(0); - } + Error(ERR_EXIT, "no audio info - no sounds"); if (ioctl(audio_ctl, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_ULAW) == -1) - { - fprintf(stderr,"%s: ulaw audio not available - no sounds\n",progname); - exit(0); - } + Error(ERR_EXIT, "ulaw audio not available - no sounds"); ioctl(audio_ctl, AUDIO_SET_CHANNELS, 1); ioctl(audio_ctl, AUDIO_SET_SAMPLE_RATE, 8000); @@ -642,15 +632,13 @@ BOOL LoadSound(struct SoundInfo *snd_info) #ifndef MSDOS if (!(file=fopen(filename,"r"))) { - fprintf(stderr,"%s: cannot open sound file '%s' - no sounds\n", - progname,filename); + Error(ERR_RETURN, "cannot open sound file '%s' - no sounds", filename); return(FALSE); } if (fseek(file,0,SEEK_END)<0) { - fprintf(stderr,"%s: cannot read sound file '%s' - no sounds\n", - progname,filename); + Error(ERR_RETURN, "cannot read sound file '%s' - no sounds", filename); fclose(file); return(FALSE); } @@ -660,16 +648,14 @@ BOOL LoadSound(struct SoundInfo *snd_info) if (!(snd_info->file_ptr=malloc(snd_info->file_len))) { - fprintf(stderr,"%s: out of memory (this shouldn't happen :) - no sounds\n", - progname); + Error(ERR_RETURN, "out of memory (this shouldn't happen :) - no sounds"); fclose(file); return(FALSE); } if (fread(snd_info->file_ptr,1,snd_info->file_len,file)!=snd_info->file_len) { - fprintf(stderr,"%s: cannot read sound file '%s' - no sounds\n", - progname,filename); + Error(ERR_RETURN, "cannot read sound file '%s' - no sounds", filename); fclose(file); return(FALSE); } @@ -682,8 +668,8 @@ BOOL LoadSound(struct SoundInfo *snd_info) snd_info->file_len != be2long(&sound_header->chunk_size)+8 || strncmp(sound_header->magic_8SVX,"8SVX",4)) { - fprintf(stderr,"%s: '%s' is not an IFF/8SVX file or broken- no sounds\n", - progname,filename); + Error(ERR_RETURN, "'%s' is not an IFF/8SVX file or broken - no sounds", + filename); return(FALSE); } @@ -725,8 +711,7 @@ BOOL LoadSound(struct SoundInfo *snd_info) snd_info->sample_ptr = load_sample(filename); if(!snd_info->sample_ptr) { - fprintf(stderr,"%s: cannot read sound file '%s' - no sounds\n", - progname,filename); + Error(ERR_RETURN, "cannot read sound file '%s' - no sounds", filename); fclose(file); return(FALSE); } @@ -777,8 +762,8 @@ void PlaySoundExt(int nr, int volume, int stereo, BOOL loop) #ifndef MSDOS if (write(sound_pipe[1], &snd_ctrl, sizeof(snd_ctrl))<0) { - fprintf(stderr,"%s: cannot pipe to child process - no sounds\n",progname); - sound_status=SOUND_OFF; + Error(ERR_RETURN, "cannot pipe to child process - no sounds"); + sound_status = SOUND_OFF; return; } #else @@ -827,8 +812,8 @@ void StopSoundExt(int nr, int method) #ifndef MSDOS if (write(sound_pipe[1], &snd_ctrl, sizeof(snd_ctrl))<0) { - fprintf(stderr,"%s: cannot pipe to child process - no sounds\n",progname); - sound_status=SOUND_OFF; + Error(ERR_RETURN, "cannot pipe to child process - no sounds"); + sound_status = SOUND_OFF; return; } #else