source: trunk/athena/bin/discuss/client/discuss.c @ 24481

Revision 24481, 5.3 KB checked in by jdreed, 14 years ago (diff)
Don't use a fixed buffer
Line 
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
18static 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
43extern ss_request_table discuss_cmds;
44
45char    dsc_version[] = "1.7";
46int     sci_idx;
47
48extern char *temp_file, *pgm, *user_id;
49
50/* EXTERNAL ROUTINES */
51
52tfile   unix_tfile();
53char    *local_realm();
54static char     buf[BUFSIZ];
55char    *buffer = buf;
56
57int 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          if (code != EACCES) {
171            fprintf(stderr, "Creating new ~/.meetings file...\n");
172            fflush(stderr);
173            char * cmd = malloc(strlen(DSC_SETUP) + strlen(" -q") + 1);
174
175            sprintf(cmd, "%s -q", DSC_SETUP);
176            system(cmd);
177            free(cmd);
178            code = find_rc_filename(); 
179          }
180          if (code) {
181            ss_perror(sci_idx, code, "while reading meetings file.");
182            exit (1);
183          }
184        }
185        if (!quit) {
186             printf("Discuss version %s.  Type '?' for a list of commands.\n",
187                    dsc_version);
188             if (!initial_meeting)
189                  printf("\n");
190        }
191
192        if (initial_meeting != (char *)NULL) {
193                (void) sprintf(buffer, "goto %s", initial_meeting);
194                code = ss_execute_line(sci_idx, buffer);
195                if (code != 0)
196                        ss_perror(sci_idx, code, initial_meeting);
197        }
198        if (initial_request != (char *)NULL) {
199                code = ss_execute_line(sci_idx, initial_request);
200                if (code != 0)
201                        ss_perror(sci_idx, code, initial_request);
202        }
203        if (!quit || code)
204                code = ss_listen (sci_idx);
205        leave_mtg();                            /* clean up after ourselves */
206        return 0;
207}
208
209int getyn(prompt,def)
210    char *prompt, def;
211{
212        char inp[128];
213
214        for (;;) {
215                (void) printf("%s ",prompt);
216                if (fgets (inp, 128, stdin) == NULL)
217                    return FALSE;
218                else if (inp[0] == '\n')
219                    inp[0] = def;
220                if (inp[0] == 'y' || inp[0] == 'Y')
221                    return 1;
222                else if (inp[0] == 'n' || inp[0] == 'N')
223                    return 0;
224                printf("Please enter 'Yes' or 'No'\n\n");
225        }
226}
Note: See TracBrowser for help on using the repository browser.