-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
195 lines (175 loc) · 4.4 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <getopt.h>
#if !defined(AMIGA) && !defined(__mac_os)
#include <malloc.h>
#endif
#include <string.h>
#include <signal.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#if defined(__mac_os)
#include <types.h>
#include <Speech.h>
#else
#include <sys/types.h>
#endif
#include "megahal.h"
#if defined(DEBUG)
#include "debug.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
/* extern errorfp;
extern statusfp;
*/
/* extern noprompt;
extern nowrap;
extern nobanner;
extern typing_delay;
extern speech;
extern quiet;
*/
static struct option long_options[] = {
{"no-prompt", 0, NULL, 'p'},
{"no-wrap", 0, NULL, 'w'},
{"no-banner", 0, NULL, 'b'},
{"help", 0, NULL, 'h'},
{"directory", 1, NULL, 'd'},
{0, 0, 0, 0}
};
void usage()
{
puts("usage: megahal [-[pqrgwbh]]\n" \
"\t-h : show usage\n" \
"\t-p : inhibit prompts\n" \
"\t-q : quiet mode (no replies) enabled at start\n" \
"\t-r : inhibit progress display\n" \
"\t-g : inhibit initial greeting\n" \
"\t-b : inhibit banner display at startup\n" \
"\t-d : sets the directory where your megahal files are\n");
}
/*===========================================================================*/
/*
* Function: Main
*
* Purpose: Initialise everything, and then do an infinite loop. In
* the loop, we read the user's input and reply to it, and
* do some housekeeping task such as responding to special
* commands.
*/
int main(int argc, char **argv)
{
char *input=NULL;
char *output=NULL;
char *my_directory = NULL;
int directory_set;
int c, option_index = 0;
directory_set = 0;
while(1) {
if((c = getopt_long(argc, argv, "hpwbd:", long_options,
&option_index)) == -1)
break;
switch(c) {
case 'p':
megahal_setnoprompt();
break;
case 'w':
megahal_setnowrap();
break;
case 'd':
megahal_setdirectory (optarg);
directory_set = 1;
break;
case 'b':
megahal_setnobanner();
break;
case 'h':
usage();
return 0;
}
}
if (directory_set == 0)
{
if ((my_directory = getenv("MEGAHAL_DIR")))
{
megahal_setdirectory (my_directory);
directory_set = 1;
}
else
{
struct stat dir_stat;
my_directory = getenv ("HOME");
if (my_directory == NULL)
{
fprintf (stderr, "Cannot find your home directory.\n");
exit (1);
}
my_directory = malloc (12 + strlen (my_directory));
strcpy (my_directory, getenv ("HOME"));
strcat (my_directory, "/.megahal");
if (stat (my_directory, &dir_stat))
{
if (errno == ENOENT)
{
directory_set = mkdir (my_directory, S_IRWXU);
if (directory_set != 0)
{
fprintf (stderr, "Could not create %s.\n",
my_directory);
exit (1);
}
megahal_setdirectory (my_directory);
directory_set = 1;
}
}
else
{
if (S_ISDIR(dir_stat.st_mode))
{
megahal_setdirectory (my_directory);
directory_set = 1;
}
else
{
fprintf (stderr, "Could not use megahal directory %s.\n",
my_directory);
exit (1);
}
}
}
}
/*
* Do some initialisation
*/
megahal_initialize();
/*
* Create a dictionary which will be used to hold the segmented
* version of the user's input.
*/
/*
* Load the default MegaHAL personality.
*/
output = megahal_initial_greeting();
megahal_output(output);
/*
* Read input, formulate a reply and display it as output
*/
while(1) {
input = megahal_input("> ");
/*
* If the input was a command, then execute it
*/
if (megahal_command(input) != 0)
continue;
output = megahal_do_reply(input, 1);
megahal_output(output);
}
megahal_cleanup();
exit(0);
}