File: mgt.c

package info (click to toggle)
mgt 2.31-7
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 460 kB
  • ctags: 582
  • sloc: ansic: 4,852; sh: 411; pascal: 329; makefile: 88; csh: 3
file content (199 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download | duplicates (3)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* "mgt" Copyright (c) 1991 Shodan  */


#include <signal.h>
#include "mgt.h"

int retVal = 0;
interface *io;			/* current interface routines */
int boardsize;			/* global for board size */
char *info[LASTINFO - FIRSTINFO + 2];
int handicap;
char *commentBuf;		/* pointer to current node's comment */
extern interface asciiInterface;
int prisoners[2];
FILE *input = 0;
char name_buf[512];
char *files[MAX_FILES];
int filecount, currentfile;

#ifdef DEBUG
FILE *debug;
unsigned long totalmemory;
#endif

#ifdef MGT_IBM
extern unsigned _stklen = 32000;
#endif

typedef struct {
   char *arg;
   int *flag;
   char **str;
}  argType;


int mailFlag = 0;
int saveShort = 0;
int tutor = 0;
char *saveName;



static argType argTable[] =
{
   {"-m", &mailFlag, &saveName},
   {"-s", &saveShort, NULL},
   {"-t", &tutor, NULL}
};


FUNCTION main(argc, argv)
int argc;
char **argv;
{

#ifdef DEBUG
   debug = fopen("debug", "w");
#endif

   io = &asciiInterface;
   input = stdin;
   init(argv, &argc);
   parseLine(argc, argv);
   doit();
   myexit();
}



FUNCTION void die()
{
   signal(SIGINT, SIG_DFL);
   myexit();
}


FUNCTION void myexit()
{
   (*io->clearScreen) ();
   (*io->refreshIO) ();
   (*io->close) ();
   printf("Thank you for using mgt\n");
   exit(retVal);
}



FUNCTION void openfile(f)
char *f;
{
#ifdef MGT_LIB
   static char game_lib[] = MGT_LIB;
#endif				/* MGT_LIB */
   strcpy(name_buf, f);
   if (!(input = fopen(f, "r")))
      strcpy(name_buf, "");
#ifdef MGT_LIB
   /* if not in local directory, try library */
   if (!input) {
      strcpy(name_buf, game_lib);
      strcat(name_buf, f);
      input = fopen(name_buf, "r");
   }
#endif				/* MGT_LIB */
}


FUNCTION void barf(s)
char *s;
{
   int i;
   for (i = 24; i--;)
      fprintf(stderr, "\n");
   fprintf(stderr, "%s\n", s);
   exit(1);
}


FUNCTION void initEnv()
{
   extern char *getenv();
   char *env;

   if (env = getenv("MGT"))
      while (*env) {
	 while (*env && *env != '_')
	    env++;
	 if (*env)
	    env++;
	 if (*env)
	    (*io->readEnv) (&env);
      }
}


FUNCTION void init(argv, argc)
char *argv[];
int *argc;
{
   initEnv();
   (*io->open) (argv, argc);
   signal(SIGINT, die);
   readInit();
}



FUNCTION void helpCommandLine(errMesg)
char *errMesg;
{
   (*io->close) ();
   fprintf(stderr, "\nERROR: %s\n\n", errMesg);
   fprintf(stderr, "                   MGT %s\n", VERSION);
   fprintf(stderr, "\nUsage: mgt [opts] [files]\n");
   fprintf(stderr, "[opts] is any of:\n");
   fprintf(stderr, "-m filename        mail mode.  Autosave on quit.\n");
   fprintf(stderr, "-s                 use short format when saving.\n\n");
   exit(2);
}


FUNCTION void parseLine(argc, argv)
int argc;
char **argv;
{
   int i;

   filecount = 0;

   while (++argv, --argc > 0) {
      if (**argv != '-') {
	 files[filecount++] = *argv;
	 if (filecount == MAX_FILES)
	    helpCommandLine("Too many files specified");
      } else {
	 for (i = 0; i < sizeof(argTable) / sizeof(argType); i++)
	    if (!strcmp(argTable[i].arg, *argv)) {
	       if (argTable[i].str)
		  if (--argc)
		     *(argTable[i].str) = (*++argv);
		  else
		     helpCommandLine("String expected on option");
	       (*(argTable[i].flag))--;
	       break;
	    }
	 if (i == sizeof(argTable) / sizeof(argType))
	    helpCommandLine("Bad option");
      }
   }
   if (!filecount)
      input = stdin;
   else {
      currentfile = 0;
      openfile(files[0]);
      if (!input)
	 helpCommandLine("File not found");
   }

}