]> andersk Git - moira.git/blob - clients/mmoira/main.c
specify path to synctree
[moira.git] / clients / mmoira / main.c
1 /* $Header$
2  *
3  *      Copyright 1991 by the Massachusetts Institute of Technology.
4  *
5  *      For further information on copyright and distribution 
6  *      see the file mit-copyright.h
7  */
8
9 #include        <mit-copyright.h>
10 #include        <stdio.h>
11 #include        <string.h>
12 #include        <pwd.h>
13 #include        <moira.h>
14 #include        <com_err.h>
15 #include        <X11/StringDefs.h>
16 #include        <Xm/PushB.h>
17 #include        <Xm/BulletinB.h>
18 #include        <Xm/RowColumn.h>
19 #include        "mmoira.h"
20
21 extern MenuItem MenuRoot;
22
23 Widget  CreateMenu(), CreateForm();
24 Widget  BuildMenuTree();
25 void popup_error_hook(), mr_x_input(), EnterPressed(), CancelForm();
26 void ExecuteForm(), DoHelp();
27
28 MoiraResources resources;
29
30 static XrmOptionDescRec options[] = {
31     {"-db",     "*database",    XrmoptionSepArg,        NULL},
32     {"-noauth", "*noAuth",      XrmoptionNoArg,         (caddr_t) "true"},
33     {"-helpfile","*helpFile",   XrmoptionSepArg,        NULL},
34     {"-logsize","*logSize",     XrmoptionSepArg,        NULL},
35 };
36
37
38 #define Offset(field) (XtOffset(MoiraResources *, field))
39
40 static XtResource my_resources[] = {
41     {"formTranslations", XtCString, XtRString, sizeof(String),
42        Offset(form_trans), XtRImmediate, (caddr_t) NULL},
43     {"textTranslations", XtCString, XtRString, sizeof(String),
44        Offset(text_trans), XtRImmediate, (caddr_t) NULL},
45     {"logTranslations", XtCString, XtRString, sizeof(String),
46        Offset(log_trans), XtRImmediate, (caddr_t) NULL},
47     {"helpFile", XtCFile, XtRString, sizeof(String),
48        Offset(help_file), XtRImmediate, (caddr_t) "/afs/athena.mit.edu/system/moira/lib/mmoira.helpfile"},
49     {"database", XtCString, XtRString, sizeof(String),
50        Offset(db), XtRImmediate, (caddr_t) NULL},
51     {"noAuth", XtCBoolean, XtRBoolean, sizeof(Boolean),
52        Offset(noauth), XtRImmediate, (caddr_t) False},
53     {"logSize", XtCValue, XtRInt, sizeof(int),
54        Offset(maxlogsize), XtRImmediate, (caddr_t) MAXLOGSIZE},
55 };
56
57 #undef Offset
58
59 XtActionsRec actions[] = {
60     { "next-or-do-it", EnterPressed },
61     { "cancel-form", CancelForm },
62     { "execute-form", ExecuteForm },
63     { "help", DoHelp }
64 };
65
66
67 Widget  toplevel;
68 char *user, *program_name, *moira_server;
69 int tty;
70
71 main(argc, argv)
72 int argc;
73 char *argv[];
74 {
75         Widget  button, bboard, menuwidget;
76         char *motd, *env, buf[256], host[64];
77         int     n, status;
78         char *getlogin(), *getenv();
79
80         /* I know, this is a crock, but it makes the program work... */
81         env = getenv("XFILESEARCHPATH");
82         if (env) {
83             char buf[256];
84             sprintf(buf, "%s:/afs/athena.mit.edu/system/moira/lib/%N", env);
85             setenv("XFILESEARCHPATH", buf, 1);
86         } else
87           setenv("XFILESEARCHPATH", "/afs/athena.mit.edu/system/moira/lib/%N", 1);
88
89         if (getenv("DISPLAY"))
90           tty = 0;
91         else
92           tty = 1;
93
94         if ((user = getlogin()) == NULL)
95           user = getpwuid((int) getuid())->pw_name;
96         user = (user && strlen(user)) ? strsave(user) : "";
97
98         if ((program_name = strrchr(argv[0], '/')) == NULL)
99           program_name = argv[0];
100         else
101           program_name++;
102         program_name = strsave(program_name);
103
104         resources.help_file = my_resources[3].default_addr;
105         resources.maxlogsize = (int) my_resources[6].default_addr;
106         resources.noauth = 0;
107         resources.db = "";
108         for (n = 1; n < argc; n++) {
109             if (!strcmp(argv[n], "-db") && n+1 < argc) {
110                 resources.db = argv[n+1];
111                 n++;
112             } else if (!strcmp(argv[n], "-helpfile") && n+1 < argc) {
113                 resources.help_file = argv[n+1];
114                 n++;
115             } else if (!strcmp(argv[n], "-logsize") && n+1 < argc) {
116                 resources.maxlogsize = atoi(argv[n+1]);
117                 n++;
118             } else if (!strcmp(argv[n], "-noauth")) {
119                 resources.noauth = 1;
120             } else if (!strcmp(argv[n], "-tty")) {
121                 tty = 1;
122             } else {
123                 printf("%s: unknown option: %s\n", argv[0], argv[n]);
124             }
125         }
126
127         if (!tty) {
128             toplevel = XtInitialize("toplevel", "Moira", options,
129                                     XtNumber(options), &argc, argv);
130
131             XtAppAddActions(XtWidgetToApplicationContext(toplevel),
132                             actions, XtNumber(actions));
133
134             XtGetApplicationResources(toplevel, (caddr_t) &resources, 
135                                       my_resources, XtNumber(my_resources),
136                                       NULL, (Cardinal) 0);
137         }
138
139         moira_server = "";
140         if (resources.db)
141           moira_server = resources.db;
142
143 #ifdef GDSS
144         initialize_gdss_error_table();
145 #endif /* GDSS */
146
147         status = mr_connect(moira_server);
148         if (status) {
149             com_err(program_name, status, " connecting to server");
150             exit(1);
151         }
152         status = mr_motd(&motd);
153         if (status) {
154             com_err(program_name, status, " connecting to server");
155             exit(1);
156         }
157         if (motd) {
158             fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
159                     motd);
160             mr_disconnect();
161             exit(1);
162         }
163
164         if (!resources.noauth) {
165             status = mr_auth("mmoira");
166             if (status == MR_USER_AUTH) {
167                 char buf[BUFSIZ];
168                 com_err(program_name, status, "\nPress [RETURN] to continue");
169                 gets(buf);
170             } else if (status) {
171                 com_err(program_name, status, "; authorization failed - may need to run kinit");
172                 exit(1);
173             }
174         }
175
176         if (!tty) {
177             bboard = XtCreateManagedWidget("bboard",
178                                            xmBulletinBoardWidgetClass,
179                                            toplevel, NULL, 0);
180             menuwidget = BuildMenuTree(bboard, &MenuRoot);
181             SetupLogWidget(bboard);
182
183             XtRealizeWidget(toplevel);
184
185             set_com_err_hook(popup_error_hook);
186             mr_set_alternate_input(ConnectionNumber(XtDisplay(toplevel)),
187                                    mr_x_input);
188         }
189
190         mr_host(host, sizeof(host));
191         if (resources.noauth)
192           sprintf(buf, "UNAUTHENTICATED connection to Moira server %s\n\n",
193                   host);
194         else
195           sprintf(buf, "Connected to Moira server %s\n\n", host);
196         AppendToLog(buf);
197
198         if (tty)
199           TtyMainLoop();
200         else
201           XtMainLoop();
202 }
203
204
205 int MoiraQuery(query, argc, argv, callback, data)
206 char *query;
207 int argc;
208 char **argv;
209 int (*callback)();
210 caddr_t data;
211 {
212     int status;
213
214     if (!tty) {
215         MakeWatchCursor(toplevel);
216         XFlush(XtDisplay(toplevel));
217     }
218     status = mr_query(query, argc, argv, callback, data);
219     if (status != MR_ABORTED && status != MR_NOT_CONNECTED) {
220         if (!tty)
221           MakeNormalCursor(toplevel);
222         return(status);
223     }
224     status = mr_connect(moira_server);
225     if (status) {
226         com_err(program_name, status, " while re-connecting to server %s",
227                 moira_server);
228         if (!tty)
229           MakeNormalCursor(toplevel);
230         return(MR_ABORTED);
231     }
232     status = mr_auth("mmoira");
233     if (status) {
234         com_err(program_name, status, " while re-authenticating to server %s",
235                 moira_server);
236         mr_disconnect();
237         if (!tty)
238           MakeNormalCursor(toplevel);
239         return(MR_ABORTED);
240     }
241     status = mr_query(query, argc, argv, callback, data);
242     if (!tty)
243       MakeNormalCursor(toplevel);
244     return(status);
245
246 }
247
248
249 DisplayForm(spec)
250 EntryForm       *spec;
251 {
252         Widget  w;
253         int i, j;
254         static XtTranslations trans = NULL;
255
256         if (trans == NULL)
257           trans = XtParseTranslationTable(resources.form_trans);
258
259         w = CreateForm(toplevel, spec);
260         XtManageChild(w);
261         for (i = 0; spec->inputlines[i]; i++) {
262             XtOverrideTranslations(spec->inputlines[i]->mywidget, trans);
263             if (spec->inputlines[i]->type == FT_KEYWORD)
264               for (j = 0; j < NumChildren(spec->inputlines[i]->mywidget); j++)
265                 XtOverrideTranslations(NthChild(spec->inputlines[i]->mywidget, j), trans);
266         }
267         /* set the focus to the first line of the form */
268         _XmGrabTheFocus(spec->inputlines[0]->mywidget, NULL);
269 }
270
271
272 void popup_error_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
273 char *who;
274 long code;
275 char *fmt;
276 caddr_t arg1, arg2, arg3, arg4, arg5;
277 {
278     char buf[BUFSIZ], *cp;
279
280     (void) strcpy(buf, who);
281     for (cp = buf; *cp; cp++);
282     *cp++ = ':';
283     *cp++ = ' ';
284     if (code) {
285         (void) strcpy(cp, error_message(code));
286         while (*cp)
287             cp++;
288     }
289     sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
290     display_error(buf);
291 }
292
293
294 void mr_x_input()
295 {
296     XEvent event;
297     XtAppContext _XtDefaultAppContext();
298
299     XtAppNextEvent(_XtDefaultAppContext(), &event);
300     XtDispatchEvent(&event);
301 }
This page took 0.073184 seconds and 5 git commands to generate.