File: option.c

package info (click to toggle)
dangen 0.5-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,868 kB
  • sloc: ansic: 49,744; sh: 999; yacc: 494; makefile: 273; lex: 142
file content (160 lines) | stat: -rw-r--r-- 3,339 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* $Id: option.c,v 1.60 2005/07/01 03:23:10 oohara Exp $ */

/* VERSION */
#include <config.h>

#include <stdio.h>
/* strcmp */
#include <string.h>
/* malloc, atexit */
#include <stdlib.h>

/* COPYRIGHT_STRING */
#include "const.h"

#include "option.h"

static option *option_pointer = NULL;

static void option_quit(void);

/* return 0 on success, 1 on error */
int
set_option(int argc, char *argv[])
{
  int i;

  /* sanity check */
  if (argc <= 0)
  {
    fprintf(stderr, "set_option: argc is non-positive\n");
    return 1;
  }
  if (argv == NULL)
  {
    fprintf(stderr, "set_option: argv is NULL\n");
    return 1;
  }

  if (option_pointer != NULL)
    free(option_pointer);
  option_pointer = (option *) malloc(sizeof(option));
  if (option_pointer == NULL)
  {
    fprintf(stderr, "set_option: malloc failed\n");
    return 1;
  }

  if (atexit(option_quit) != 0)
  {
    fprintf(stderr, "set_option: cannot register option_quit to exit\n");
    option_quit();
    return 1;
  }

  option_pointer->free_select = 0;
  option_pointer->full_screen = 0;
  option_pointer->help = 0;
  option_pointer->slow = 0;
  option_pointer->version = 0;

  for (i = 1; i < argc; i++)
  {
    if (strcmp(argv[i], "--free-select") == 0)
    {
      option_pointer->free_select = 1;
    }
    else if (strcmp(argv[i], "--full-screen") == 0)
    {
      option_pointer->full_screen = 1;
    }
    else if (strcmp(argv[i], "--help") == 0)
    {
      option_pointer->help = 1;
      /* ignore the rest */
      break;
    }
    else if (strcmp(argv[i], "--slow") == 0)
    {
      option_pointer->slow = 1;
    }
    else if (strcmp(argv[i], "--version") == 0)
    {
      option_pointer->version = 1;
      /* ignore the rest */
      break;
    }
    else
    {
      fprintf(stderr, "set_option: unknown option (arg %d)\n", i);
      return 1;
    }
  }

  return 0;
}

/* return 1 (true) or 0 (false) */
int
cheating(void)
{
  /* sanity check */
  if (option_pointer == NULL)
  {
    fprintf(stderr, "cheating: option_pointer is NULL\n");
    return 0;
  }

  if (option_pointer->free_select != 0)
    return 1;
  if (option_pointer->slow != 0)
    return 1;

  return 0;
}

void
do_help(void)
{
  printf("Usage: dangen [options]\n"
         "shoot 'em up game where accurate shooting matters\n"
         "Options:\n"
         );
  printf("  --free-select     allow selecting any stage\n"
         "  --full-screen     run the game in the full screen mode\n"
         "  --help            print this message\n"
         );
  printf("  --slow            enable the slow mode (CAPS lock)\n"
         );
  printf("  --version         print version information\n"
         "\n"
         "Report bugs to <oohara@libra.interq.or.jp>.\n"
         );
}

void
do_version(void)
{
  printf("dangen %s\n", VERSION);
  printf("%s\n", COPYRIGHT_STRING);
  printf("This program is free software; you can redistribute it and/or\n"
         "modify it under the terms of either the GNU General Public License\n"
         "version 2 or the Artistic License Version 2.0beta5.\n");
  printf("dangen comes with NO WARRANTY, to the extent permitted by law.\n");
}

const option *
get_option(void)
{
  return (const option *) option_pointer;
}

static void
option_quit(void)
{
  if (option_pointer != NULL)
  {
    free(option_pointer);
    option_pointer = NULL;
  }
}