1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * $Id: discuss.c,v 1.60 2007-08-09 20:41:31 amb Exp $ |
---|
10 | * |
---|
11 | * A simple shell-type user interface to discuss; uses Ken Raeburn's |
---|
12 | * ss library for the command interpreter. |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #ifndef lint |
---|
18 | static char rcsid_discuss_c[] = |
---|
19 | "$Id: discuss.c,v 1.60 2007-08-09 20:41:31 amb Exp $"; |
---|
20 | #endif /* lint */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <sys/file.h> |
---|
25 | #include <signal.h> |
---|
26 | #include <string.h> |
---|
27 | #include <sys/wait.h> |
---|
28 | #include <pwd.h> |
---|
29 | #include <ss/ss.h> |
---|
30 | #include <discuss/discuss.h> |
---|
31 | #include "config.h" |
---|
32 | #include "globals.h" |
---|
33 | |
---|
34 | #ifdef lint |
---|
35 | #define DONT_USE(var) var=var; |
---|
36 | #else /* lint */ |
---|
37 | #define DONT_USE(var) ; |
---|
38 | #endif /* lint */ |
---|
39 | |
---|
40 | #define FREE(ptr) { if (ptr) free(ptr); } |
---|
41 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
---|
42 | |
---|
43 | extern ss_request_table discuss_cmds; |
---|
44 | |
---|
45 | char dsc_version[] = "1.7"; |
---|
46 | int sci_idx; |
---|
47 | |
---|
48 | extern char *temp_file, *pgm, *user_id; |
---|
49 | |
---|
50 | /* EXTERNAL ROUTINES */ |
---|
51 | |
---|
52 | tfile unix_tfile(); |
---|
53 | char *local_realm(); |
---|
54 | static char buf[BUFSIZ]; |
---|
55 | char *buffer = buf; |
---|
56 | |
---|
57 | int main (argc, argv) |
---|
58 | int argc; |
---|
59 | char **argv; |
---|
60 | { |
---|
61 | int code; |
---|
62 | char *initial_meeting = (char *)NULL; |
---|
63 | char *subsystem_name = "discuss"; |
---|
64 | char *argv0 = argv[0]; |
---|
65 | char *initial_request = (char *)NULL; |
---|
66 | bool quit = FALSE; /* quit after processing request */ |
---|
67 | bool flame = FALSE; /* Have we flamed them for multiple */ |
---|
68 | |
---|
69 | signal(SIGPIPE, SIG_IGN); |
---|
70 | editor_path = getenv ("DISCUSS_EDITOR"); |
---|
71 | |
---|
72 | while (++argv, --argc) { |
---|
73 | if (!strcmp(*argv, "-prompt")) { |
---|
74 | if (argc == 1) { |
---|
75 | fprintf(stderr, |
---|
76 | "No argument supplied with -prompt\n"); |
---|
77 | exit(1); |
---|
78 | } |
---|
79 | argc--; argv++; |
---|
80 | subsystem_name = *argv; |
---|
81 | } |
---|
82 | else if (!strcmp(*argv, "-request") || !strcmp(*argv, "-rq")) { |
---|
83 | if (argc == 1) { |
---|
84 | fprintf(stderr, |
---|
85 | "No string supplied with -request.\n"); |
---|
86 | exit(1); |
---|
87 | } |
---|
88 | argc--; argv++; |
---|
89 | initial_request = *argv; |
---|
90 | } |
---|
91 | else if (!strcmp(*argv, "-quit")) |
---|
92 | quit = TRUE; |
---|
93 | else if (!strcmp(*argv, "-no_quit")) |
---|
94 | quit = FALSE; |
---|
95 | else if (!strcmp(*argv, "-editor")) { |
---|
96 | if (argc == 1) { |
---|
97 | fprintf(stderr, "No editor name supplied with -editor\n"); |
---|
98 | exit(1); |
---|
99 | } |
---|
100 | if (!use_editor) { |
---|
101 | fprintf(stderr, "Both -editor and -no_editor specified\n"); |
---|
102 | exit(1); |
---|
103 | } |
---|
104 | --argc; |
---|
105 | editor_path = *(++argv); |
---|
106 | } |
---|
107 | else if (!strcmp(*argv, "-no_editor")) |
---|
108 | use_editor = FALSE; |
---|
109 | else if (**argv == '-') { |
---|
110 | fprintf(stderr, "Unknown control argument %s\n", |
---|
111 | *argv); |
---|
112 | fprintf(stderr, "Usage: %s [ -prompt name ] [ -request name ] [ -quit ]\n\t\t[ -editor editor_path ] [ -no_editor ]\n", |
---|
113 | argv0); |
---|
114 | exit(1); |
---|
115 | } |
---|
116 | else { |
---|
117 | if (initial_meeting) { |
---|
118 | if (!flame) { |
---|
119 | fprintf(stderr, |
---|
120 | "More than one meeting name supplied on command line; using %s\n", |
---|
121 | initial_meeting); |
---|
122 | flame = TRUE; |
---|
123 | } |
---|
124 | } else initial_meeting = *argv; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | { |
---|
129 | char *user; |
---|
130 | struct passwd *user_pw = getpwuid(getuid()); |
---|
131 | register char *realm = local_realm(); |
---|
132 | |
---|
133 | |
---|
134 | if (user_pw == NULL) { |
---|
135 | fprintf(stderr, |
---|
136 | "You do not appear in /etc/passwd. Cannot continue.\n"); |
---|
137 | exit(1); |
---|
138 | } |
---|
139 | user = user_pw -> pw_name; |
---|
140 | user_id = malloc((unsigned)(strlen(user)+strlen(realm)+2)); |
---|
141 | strcpy(user_id, user); |
---|
142 | strcat(user_id, "@"); |
---|
143 | strcat(user_id, realm); |
---|
144 | } |
---|
145 | |
---|
146 | sci_idx = ss_create_invocation(subsystem_name, dsc_version, |
---|
147 | (char *)NULL, &discuss_cmds, &code); |
---|
148 | if (code) { |
---|
149 | com_err (subsystem_name, code, "creating invocation"); |
---|
150 | exit(1); |
---|
151 | } |
---|
152 | (void) ss_add_info_dir(sci_idx, INFO_DIR, &code); |
---|
153 | if (code) { |
---|
154 | ss_perror(sci_idx, code, INFO_DIR); |
---|
155 | } |
---|
156 | |
---|
157 | #if defined(__APPLE__) && defined(__MACH__) |
---|
158 | add_error_table(&et_disc_error_table); |
---|
159 | add_error_table(&et_dsc_error_table); |
---|
160 | #else |
---|
161 | initialize_disc_error_table(); |
---|
162 | initialize_dsc_error_table(); |
---|
163 | #endif |
---|
164 | |
---|
165 | temp_file = malloc(64); |
---|
166 | pgm = malloc(64); |
---|
167 | (void) sprintf(temp_file, "/tmp/mtg%d.%d", (int)getuid(), getpid()); |
---|
168 | |
---|
169 | if (code = find_rc_filename()) { |
---|
170 | register char *prompt; |
---|
171 | ss_perror(sci_idx, code, ""); |
---|
172 | fprintf(stderr, "\n\ |
---|
173 | If you are using discuss for the first time, you need to run the 'dsc_setup'\n\ |
---|
174 | command from the shell.\n\n"); |
---|
175 | fflush(stderr); |
---|
176 | prompt = "Run dsc_setup now? (y or n) "; |
---|
177 | while (getyn(prompt, 'y')) { |
---|
178 | printf("\nRunning dsc_setup...\n"); |
---|
179 | system("dsc_setup"); |
---|
180 | if (code = find_rc_filename()) { |
---|
181 | ss_perror(sci_idx, code, ""); |
---|
182 | prompt = |
---|
183 | "\nThat didn't seem to work; try again? (y or n)"; |
---|
184 | } else break; |
---|
185 | } |
---|
186 | if (code) |
---|
187 | exit (1); |
---|
188 | } |
---|
189 | else if (!quit) { |
---|
190 | printf("Discuss version %s. Type '?' for a list of commands.\n", |
---|
191 | dsc_version); |
---|
192 | if (!initial_meeting) |
---|
193 | printf("\n"); |
---|
194 | } |
---|
195 | |
---|
196 | if (initial_meeting != (char *)NULL) { |
---|
197 | (void) sprintf(buffer, "goto %s", initial_meeting); |
---|
198 | code = ss_execute_line(sci_idx, buffer); |
---|
199 | if (code != 0) |
---|
200 | ss_perror(sci_idx, code, initial_meeting); |
---|
201 | } |
---|
202 | if (initial_request != (char *)NULL) { |
---|
203 | code = ss_execute_line(sci_idx, initial_request); |
---|
204 | if (code != 0) |
---|
205 | ss_perror(sci_idx, code, initial_request); |
---|
206 | } |
---|
207 | if (!quit || code) |
---|
208 | code = ss_listen (sci_idx); |
---|
209 | leave_mtg(); /* clean up after ourselves */ |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | int getyn(prompt,def) |
---|
214 | char *prompt, def; |
---|
215 | { |
---|
216 | char inp[128]; |
---|
217 | |
---|
218 | for (;;) { |
---|
219 | (void) printf("%s ",prompt); |
---|
220 | if (fgets (inp, 128, stdin) == NULL) |
---|
221 | return FALSE; |
---|
222 | else if (inp[0] == '\n') |
---|
223 | inp[0] = def; |
---|
224 | if (inp[0] == 'y' || inp[0] == 'Y') |
---|
225 | return 1; |
---|
226 | else if (inp[0] == 'n' || inp[0] == 'N') |
---|
227 | return 0; |
---|
228 | printf("Please enter 'Yes' or 'No'\n\n"); |
---|
229 | } |
---|
230 | } |
---|