]> andersk Git - moira.git/blob - clients/mmoira/main.c
added -noauth & -logsize options
[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        <strings.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
70 main(argc, argv)
71 int argc;
72 char *argv[];
73 {
74         Widget  button, bboard, menuwidget;
75         char *motd, *env, buf[256], host[64];
76         int     n, status;
77         char *getlogin(), *getenv();
78
79         /* I know, this is a crock, but it makes the program work... */
80         env = getenv("XFILESEARCHPATH");
81         if (env) {
82             char buf[256];
83             sprintf(buf, "%s:/afs/athena.mit.edu/system/moira/lib/%N", env);
84             setenv("XFILESEARCHPATH", buf, 1);
85         } else
86           setenv("XFILESEARCHPATH", "/afs/athena.mit.edu/system/moira/lib/%N", 1);
87
88         if ((user = getlogin()) == NULL)
89           user = getpwuid((int) getuid())->pw_name;
90         user = (user && strlen(user)) ? strsave(user) : "";
91
92         if ((program_name = rindex(argv[0], '/')) == NULL)
93           program_name = argv[0];
94         else
95           program_name++;
96         program_name = strsave(program_name);
97
98         toplevel = XtInitialize("toplevel", "Moira", options,
99                                 XtNumber(options), &argc, argv);
100
101         XtAppAddActions(XtWidgetToApplicationContext(toplevel),
102                         actions, XtNumber(actions));
103
104         XtGetApplicationResources(toplevel, (caddr_t) &resources, 
105                                   my_resources, XtNumber(my_resources),
106                                   NULL, (Cardinal) 0);
107
108         moira_server = "";
109         if (resources.db)
110           moira_server = resources.db;
111
112 #ifdef GDSS
113         initialize_gdss_error_table();
114 #endif /* GDSS */
115
116         status = mr_connect(moira_server);
117         if (status) {
118             com_err(program_name, status, " connecting to server");
119             exit(1);
120         }
121         status = mr_motd(&motd);
122         if (status) {
123             com_err(program_name, status, " connecting to server");
124             exit(1);
125         }
126         if (motd) {
127             fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
128                     motd);
129             mr_disconnect();
130             exit(1);
131         }
132
133         if (!resources.noauth) {
134             status = mr_auth("mmoira");
135             if (status == MR_USER_AUTH) {
136                 char buf[BUFSIZ];
137                 com_err(program_name, status, "\nPress [RETURN] to continue");
138                 gets(buf);
139             } else if (status) {
140                 com_err(program_name, status, "; authorization failed - may need to run kinit");
141                 exit(1);
142             }
143         }
144
145         bboard = XtCreateManagedWidget( "bboard",
146                                        xmBulletinBoardWidgetClass,
147                                        toplevel, NULL, 0);
148         menuwidget = BuildMenuTree(bboard, &MenuRoot);
149         SetupLogWidget(bboard);
150
151         XtRealizeWidget(toplevel);
152
153         set_com_err_hook(popup_error_hook);
154         mr_set_alternate_input(ConnectionNumber(XtDisplay(toplevel)),
155                                mr_x_input);
156         mr_host(host, sizeof(host));
157         if (resources.noauth)
158           sprintf(buf, "UNAUTHENTICATED connection to Moira server %s\n\n",
159                   host);
160         else
161           sprintf(buf, "Connected to Moira server %s\n\n", host);
162         AppendToLog(buf);
163
164         XtMainLoop();
165 }
166
167
168 int MoiraQuery(query, argc, argv, callback, data)
169 char *query;
170 int argc;
171 char **argv;
172 int (*callback)();
173 caddr_t data;
174 {
175     int status;
176
177     MakeWatchCursor(toplevel);
178     XFlush(XtDisplay(toplevel));
179     status = mr_query(query, argc, argv, callback, data);
180     if (status != MR_ABORTED && status != MR_NOT_CONNECTED) {
181         MakeNormalCursor(toplevel);
182         return(status);
183     }
184     status = mr_connect(moira_server);
185     if (status) {
186         com_err(program_name, status, " while re-connecting to server %s",
187                 moira_server);
188         MakeNormalCursor(toplevel);
189         return(MR_ABORTED);
190     }
191     status = mr_auth("mmoira");
192     if (status) {
193         com_err(program_name, status, " while re-authenticating to server %s",
194                 moira_server);
195         mr_disconnect();
196         MakeNormalCursor(toplevel);
197         return(MR_ABORTED);
198     }
199     status = mr_query(query, argc, argv, callback, data);
200     MakeNormalCursor(toplevel);
201     return(status);
202
203 }
204
205
206 DisplayForm(spec)
207 EntryForm       *spec;
208 {
209         Widget  w;
210         int i, j;
211         static XtTranslations trans = NULL;
212
213         if (trans == NULL)
214           trans = XtParseTranslationTable(resources.form_trans);
215
216         w = CreateForm(toplevel, spec);
217         XtManageChild(w);
218         for (i = 0; spec->inputlines[i]; i++) {
219             XtOverrideTranslations(spec->inputlines[i]->mywidget, trans);
220             if (spec->inputlines[i]->type == FT_KEYWORD)
221               for (j = 0; j < NumChildren(spec->inputlines[i]->mywidget); j++)
222                 XtOverrideTranslations(NthChild(spec->inputlines[i]->mywidget, j), trans);
223         }
224         /* set the focus to the first line of the form */
225         _XmGrabTheFocus(spec->inputlines[0]->mywidget, NULL);
226 }
227
228
229 void popup_error_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
230 char *who;
231 long code;
232 char *fmt;
233 caddr_t arg1, arg2, arg3, arg4, arg5;
234 {
235     char buf[BUFSIZ], *cp;
236
237     (void) strcpy(buf, who);
238     for (cp = buf; *cp; cp++);
239     *cp++ = ':';
240     *cp++ = ' ';
241     if (code) {
242         (void) strcpy(cp, error_message(code));
243         while (*cp)
244             cp++;
245     }
246     sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
247     display_error(buf);
248 }
249
250
251 void mr_x_input()
252 {
253     XEvent event;
254     XtAppContext _XtDefaultAppContext();
255
256     XtAppNextEvent(_XtDefaultAppContext(), &event);
257     XtDispatchEvent(&event);
258 }
This page took 0.051214 seconds and 5 git commands to generate.