]> andersk Git - moira.git/blame - clients/mmoira/stubs.c
added prefix column to subnet table
[moira.git] / clients / mmoira / stubs.c
CommitLineData
c34403d7 1/*
2** Stub functions
3*/
4
698271c7 5#ifdef POSIX
6#include <unistd.h>
7#endif
1cfb1b96 8#include <ctype.h>
ca5beb9a 9#include <stdio.h>
698271c7 10#include <string.h>
892c98b2 11#include <X11/StringDefs.h>
c34403d7 12#include <X11/Intrinsic.h>
892c98b2 13#include <X11/Core.h>
4fa286c6 14#include <X11/cursorfont.h>
c34403d7 15#include <Xm/Text.h>
1cfb1b96 16#include <Xm/MessageB.h>
b34d5002 17#include "mmoira.h"
698271c7 18#include <sys/types.h>
b34d5002 19#include <sys/file.h>
698271c7 20#include <fcntl.h>
c34403d7 21
4fa286c6 22static char rcsid[] = "$Header$";
23
892c98b2 24void extra_help_callback();
4271ed91 25extern EntryForm *MoiraForms[];
1cfb1b96 26static DoMoiraSelect(), DoReference();
892c98b2 27
c34403d7 28static Widget logwidget = NULL;
29
ad9fdb22 30
31/* No-op action to mask built-in actions */
32
33void noopACT(w, event, p, n)
34Widget w;
35XEvent *event;
36String *p;
37Cardinal *n;
38{
39}
40
41
42/* Action to do a moira retrieve on what was clicked at */
43
44void moiraRetrieveACT(w, event, p, n)
45Widget w;
46XEvent *event;
47String *p;
48Cardinal *n;
49{
50 XmTextPosition pos;
51 XButtonEvent *be;
52
53 be = (XButtonEvent *)event;
54
55 pos = XmTextXYToPos(w, be->x, be->y);
56 DoMoiraSelect(pos, w, 0);
57}
58
59
60/* Action to modify the moira object that was clicked on */
61
62void moiraModifyACT(w, event, p, n)
63Widget w;
64XEvent *event;
65String *p;
66Cardinal *n;
67{
68 XmTextPosition pos;
69 XButtonEvent *be;
70
71 be = (XButtonEvent *)event;
72
73 pos = XmTextXYToPos(w, be->x, be->y);
74 DoMoiraSelect(pos, w, 1);
75}
76
77
78static XtActionsRec myactions[] = {
79 { "moiraRetrieve", moiraRetrieveACT },
80 { "moiraModify", moiraModifyACT },
81 { "noop", noopACT },
82};
83
4271ed91 84
ad9fdb22 85/* Create the log window, and setup the translation table to taking
86 * mouse clicks in it.
87 */
88
c34403d7 89Widget
90SetupLogWidget(parent)
91Widget parent;
92{
892c98b2 93 Arg wargs[10];
94 int n;
ad9fdb22 95 XtTranslations trans;
892c98b2 96
c34403d7 97 if (logwidget)
98 return (logwidget);
99
892c98b2 100 n = 0;
101 XtSetArg(wargs[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
102 XtSetArg(wargs[n], XmNeditable, False); n++;
103
104 logwidget = XmCreateScrolledText( parent,
105 "logwidget",
106 wargs, n);
107 XtManageChild(logwidget);
108
ad9fdb22 109 XtAppAddActions(XtWidgetToApplicationContext(logwidget),
110 myactions, XtNumber(myactions));
f8f9e226 111 trans = XtParseTranslationTable(resources.log_trans);
ad9fdb22 112 XtOverrideTranslations(logwidget, trans);
c34403d7 113 return (logwidget);
114}
115
deb58b9e 116
ad9fdb22 117/* This actually does all of the work for handling mouse clicks. It
118 * finds the surrounding text and decides what to do with the click,
119 * the actually starts the moira query or pops up the form.
120 */
deb58b9e 121
122static DoMoiraSelect(pos, w, modify)
123XmTextPosition pos;
124Widget w;
125int modify;
126{
127 char *log, *p, *p1, *p2;
128 char name[256], type[256], type2[256];
129
130 log = XmTextGetString(w);
131 for (p = &log[pos]; p > log; p--)
132 if (isspace(*p) || *p == ':') break;
133
134 /* p now points to the beginning of the word on which the mouse was
135 * clicked. Begin gross hacking to find the name and type of object.
136 */
137 /* get name */
138 p1 = p + 1;
139 p2 = name;
140 while (*p1 && !isspace(*p1)) *p2++ = *p1++;
141 *p2 = 0;
142 /* backup two words before for type2 */
143 while (p >= log && isspace(*p) || *p == ':') p--;
144 while (p >= log && !isspace(*p)) p--;
145 while (p >= log && isspace(*p)) p--;
146 while (p >= log && !isspace(*p)) p--;
147 p++;
148 p2 = type2;
149 /* new get type2 */
150 if (p > log) {
151 while (!isspace(*p)) *p2++ = *p++;
152 *p2++ = *p++;
153 /* skip additional whitespace */
154 while (isspace(*p)) p++;
155 }
156 /* get type & rest of type2 */
157 p1 = type;
158 while (*p && *p != ':' && !isspace(*p)) *p2++ = *p1++ = *p++;
159 *p2 = *p1 = 0;
160 /* Done! */
161#ifdef DEBUG
162 printf("name \"%s\", type \"%s\" type2 \"%s\"\n", name, type, type2);
163#endif /* DEBUG */
164 if (!strcasecmp(type, "USER") ||
165 !strcmp(type, "name") ||
166 !strcmp(type2, "Modified by") ||
167 !strcmp(type2, "User Ownership") ||
168 !strcmp(type2, "Login name") ||
169 !strcmp(type, "login")) {
170 DoReference(name, "select_user", MM_MOD_USER, MM_SHOW_USER,
171 "get_user_account_by_login", modify);
172 } else if (!strcasecmp(type, "LIST") ||
173 !strcmp(type2, "Group Ownership")) {
174 DoReference(name, "select_list", MM_MOD_LIST, MM_SHOW_LIST,
175 "get_list_info", modify);
176 } else if (!strcasecmp(type, "MACHINE") ||
177 !strcmp(type, "host") ||
86293668 178 !strcasecmp(type, "Server") ||
4271ed91 179 !strcmp(type, "on") ||
deb58b9e 180 !strcmp(type, "Box")) {
181 DoReference(name, "select_machine", MM_MOD_MACH, MM_SHOW_MACH,
182 "get_machine", modify);
183 } else if (!strcasecmp(type, "CLUSTER")) {
184 DoReference(name, "select_cluster", MM_MOD_CLUSTER, MM_SHOW_CLUSTER,
185 "get_cluster", modify);
186 } else if (!strcasecmp(type, "FILESYSTEM") ||
187 !strcmp(type, "FILESYS") ||
188 !strcmp(type2, "syslib Data")) {
189 DoReference(name, "select_filsys", MM_MOD_FILSYS, MM_SHOW_FILSYS,
190 "get_filesys_by_label", modify);
86293668 191 } else if (!strcmp(type2, "Filesystem Group")) {
192 DoReference(name, "order_fsgroup", MM_MOV_FSGROUP, MM_SHOW_FSGROUP,
193 "get_filesys_by_label", modify);
deb58b9e 194 } else if (!strcmp(type, "Printer") ||
195 !strcmp(type2, "lpr Data")) {
196 DoReference(name, "select_printer", MM_MOD_PCAP, MM_SHOW_PCAP,
197 "get_printcap_entry", modify);
1cfb1b96 198 } else if (!strcmp(type, "Service")) {
199 DoReference(name, "select_service", MM_MOD_SERVICE, MM_SHOW_SERVICE,
200 "get_server_info", modify);
86293668 201/* This code isn't complete, and it's not clear that we really want it...
202 } else if (!strcmp(type, "Packname")) {
203 char name2[256];
204 sprintf(name2, "%s:%s", type2, name);
698271c7 205 p = strrchr(name2, '/');
86293668 206 if (p) *p = 0;
207 DoReference(name, "select_nfs", MM_MOD_NFS, MM_SHOW_NFS,
208 "get_nfsphys", modify);
209*/ } else {
deb58b9e 210 XBell(XtDisplay(w), 100);
211 }
212 XtFree(log);
213}
214
215
ad9fdb22 216/* This is the heart of handling the reference once we have found out
217 * what type of reference it is.
218 */
219
deb58b9e 220static DoReference(name, formname, modop, showop, query, modify)
221char *name;
222char *formname;
223int modop;
224int showop;
225char *query;
226int modify;
227{
228 EntryForm *form, f;
229 MenuItem m;
230 char *argv[2], **aargv;
231 int status;
232
233 if (modify) {
234 form = GetAndClearForm(formname);
235 StoreField(form, 0, name);
236 form->menu = &m;
237 m.operation = modop;
238 m.query = query;
239 m.argc = 1;
86293668 240 if (showop == MM_SHOW_FSGROUP) {
241 DisplayForm(form);
242 MoiraValueChanged(form, form->inputlines[0]);
243 } else
244 MoiraFormApply(0, form);
deb58b9e 245 } else {
246 f.menu = &m;
86293668 247 if (showop == MM_SHOW_FILSYS ||
248 showop == MM_SHOW_FSGROUP) f.extrastuff = (caddr_t) sq_create();
deb58b9e 249 argv[0] = name;
250 m.operation = showop;
251 status = MoiraQuery(query, 1, argv, DisplayCallback, &f);
86293668 252 if (showop == MM_SHOW_FILSYS ||
253 showop == MM_SHOW_FSGROUP) {
deb58b9e 254 while (sq_get_data(f.extrastuff, &aargv)) {
255 ShowFilsys(aargv);
256 }
257 sq_destroy(f.extrastuff);
258 }
259 if (status)
260 com_err(program_name, status, " while looking up data");
261 else
262 AppendToLog("\n");
263 }
264}
265
266
c34403d7 267/*
268** PopupErrorMessage(text)
269**
270** Given a char* pointing to an error message, possibly with imbedded
271** newlines, display the text in a popup window and put two buttons
892c98b2 272** at the bottom of the window, labelled "OK" and "Cancel." Pop down
c34403d7 273** when one of the buttons is pressed.
274**
892c98b2 275** Return 0 if "OK" is pressed, 1 for "Cancel."
c34403d7 276*/
277
892c98b2 278Boolean
279PopupErrorMessage(text, extrahelp)
c34403d7 280char *text;
892c98b2 281char *extrahelp;
c34403d7 282{
b2d21e59 283 static Widget child;
892c98b2 284 Arg wargs[10];
285 int n;
b2d21e59 286 static XmString label;
287
ca5beb9a 288 if (tty) {
289 printf("%s\r\n", text);
290 return;
291 }
292
b2d21e59 293 if (label) {
294 XtFree(label);
295 XtDestroyWidget(child);
296 }
892c98b2 297
298
299 label = XmStringCreateLtoR( text, XmSTRING_DEFAULT_CHARSET);
300
301 n = 0;
302 XtSetArg(wargs[n], XmNmessageString, label); n++;
303
304 child = (Widget) XmCreateErrorDialog(logwidget, "errormessage", wargs, n);
305 if (extrahelp)
306 XtAddCallback (child, XmNhelpCallback, extra_help_callback, extrahelp);
307 else
308 XtUnmanageChild(XmMessageBoxGetChild (child, XmDIALOG_HELP_BUTTON));
309 XtUnmanageChild(XmMessageBoxGetChild (child, XmDIALOG_CANCEL_BUTTON));
310 XtManageChild(child);
c34403d7 311}
312
313/*
314** PopupHelpWindow(text)
315**
316** Given a char* pointing to an help message, possibly with imbedded
317** newlines, display the text in a popup window and put a single button
318** at the bottom of the window, labelled "OK." Pop down when the
319** the buttons is pressed.
320*/
321
322void
323PopupHelpWindow(text)
324char *text;
325{
b2d21e59 326 static Widget child;
892c98b2 327 Arg wargs[10];
328 int n;
892c98b2 329
b2d21e59 330 static XmString label;
331 if (label) {
332 XtFree(label);
333 XtDestroyWidget(child);
334 }
892c98b2 335
336 label = XmStringCreateLtoR( text, XmSTRING_DEFAULT_CHARSET);
337
338 n = 0;
339 XtSetArg(wargs[n], XmNmessageString, label); n++;
340
341 child = (Widget) XmCreateMessageDialog(logwidget, "helpmessage", wargs, n);
342 XtUnmanageChild(XmMessageBoxGetChild (child, XmDIALOG_CANCEL_BUTTON));
343 XtUnmanageChild(XmMessageBoxGetChild (child, XmDIALOG_HELP_BUTTON));
344
345 XtManageChild(child);
c34403d7 346}
347
348/*
349** Given a char* to a single line of text, append this line at the bottom
350** of the log window. Return 0 of the append was sucessful, non-zero
351** for an error condition.
352*/
353
354int
355AppendToLog(text)
356char *text;
357{
358 XmTextPosition pos;
b34d5002 359 char *string, *p;
c34403d7 360
ca5beb9a 361 if (tty) {
362 fputs(text, stdout);
363 return;
364 }
365
c34403d7 366 string = XmTextGetString(logwidget);
367 pos = strlen(string);
368 XtFree(string);
369
370 XmTextReplace(logwidget, pos, pos, text);
892c98b2 371 XmTextSetCursorPosition(logwidget, pos + strlen(text));
86293668 372 if (pos > resources.maxlogsize) {
373 for (p = &string[pos-resources.maxlogsize]; *p && *p != '\n'; p++);
b34d5002 374 if (*p)
375 pos = p - string;
376 XmTextReplace(logwidget, 0, pos, "");
377 }
c34403d7 378}
379
892c98b2 380void
4fa286c6 381MakeWatchCursor(topW)
382Widget topW;
892c98b2 383{
ad9fdb22 384 static Cursor mycursor = NULL;
4271ed91 385 EntryForm **fp;
4fa286c6 386
ad9fdb22 387 if (!topW)
388 return;
389
390 if (!mycursor)
391 mycursor = XCreateFontCursor (XtDisplay(topW), XC_watch);
4fa286c6 392
ad9fdb22 393 XDefineCursor(XtDisplay(topW), XtWindow(topW), mycursor);
4271ed91 394 for (fp = MoiraForms; *fp; fp++)
395 if ((*fp)->formpointer && XtIsManaged((*fp)->formpointer))
396 XDefineCursor(XtDisplay(topW), XtWindow((*fp)->formpointer), mycursor);
892c98b2 397}
c34403d7 398
399void
4fa286c6 400MakeNormalCursor(topW)
401Widget topW;
c34403d7 402{
4271ed91 403 EntryForm **fp;
404
405 if (!topW)
406 return;
4fa286c6 407
4271ed91 408 XUndefineCursor(XtDisplay(topW), XtWindow(topW));
409 for (fp = MoiraForms; *fp; fp++)
410 if ((*fp)->formpointer && XtIsManaged((*fp)->formpointer))
411 XUndefineCursor(XtDisplay(topW), XtWindow((*fp)->formpointer));
c34403d7 412}
413
892c98b2 414/*
415** Move through the fields of the spec and make certain that the
416** form's widgets actually reflect the current values.
417*/
418
c34403d7 419void
892c98b2 420UpdateForm(spec)
421EntryForm *spec;
c34403d7 422{
892c98b2 423 UserPrompt **myinputlines = spec->inputlines;
424 UserPrompt *current;
425 Arg wargs[10];
426 int n, kidcount;
427 Widget kid;
428
ca5beb9a 429 if (tty || spec->formpointer == NULL) return;
deb58b9e 430
892c98b2 431 for ( current = (*myinputlines);
432 current;
433 myinputlines++, current = (*myinputlines)) {
434
b34d5002 435 if (current->changed) {
436 n = 0;
437 XtSetArg(wargs[n], XmNsensitive,
438 current->insensitive ? False : True); n++;
439 XtSetValues(current->mywidget, wargs, n);
440 current->changed = False;
441 }
892c98b2 442
443 switch (current->type) {
444 case FT_STRING:
445 if (current->returnvalue.stringvalue) {
446 XmTextSetString (current->mywidget, current->returnvalue.stringvalue);
447 }
448 break;
449
450 case FT_BOOLEAN:
451 n = 0;
b2d21e59 452 XtSetArg(wargs[n], XmNset,
453 current->returnvalue.booleanvalue ? True : False); n++;
892c98b2 454 XtSetValues (current->mywidget, wargs, n);
455 break;
456
457 case FT_KEYWORD:
86293668 458 kidcount = NumChildren(current->mywidget);
892c98b2 459 while(kidcount--) {
460 n = 0;
86293668 461 kid = NthChild(current->mywidget, kidcount);
b2d21e59 462 if (current->returnvalue.stringvalue &&
463 (!strcmp (XtName(kid), current->returnvalue.stringvalue))) {
892c98b2 464 XtSetArg(wargs[n], XmNset, True);
465 n++;
466 }
467 else {
468 XtSetArg(wargs[n], XmNset, False);
469 n++;
470 }
471 XtSetValues (kid, wargs, n);
472 }
473 break;
474
475 case FT_NUMBER:
476 break;
477 }
478 n = 0;
479 XtSetArg(wargs[n], XtNsensitive, !(current->insensitive)); n++;
480 XtSetValues (current->mywidget, wargs, n);
481 }
c34403d7 482}
483
484void
892c98b2 485extra_help_callback(w, client_data, call_data)
486Widget w;
487char *client_data;
488XmAnyCallbackStruct *call_data;
c34403d7 489{
4271ed91 490 help(client_data);
c34403d7 491}
b34d5002 492
493
494int write_log_to_file(fn)
495char *fn;
496{
497 char *string, buf[256];
498 int fd, i;
499 extern int errno;
500
501 string = XmTextGetString(logwidget);
502 fd = open(fn, O_WRONLY|O_CREAT, 0666);
503 if (fd < 0) {
504 sprintf(buf, "opening output file \"%s\"", fn);
505 com_err(program_name, errno, buf);
506 return(1);
507 }
508 if ((i = write(fd, string, strlen(string))) < 0) {
509 sprintf(buf, "writing output file \"%s\"", fn);
510 com_err(program_name, errno, buf);
511 return(1);
512 }
513 if ((i = close(fd)) < 0) {
514 sprintf(buf, "closing output file \"%s\"", fn);
515 com_err(program_name, errno, buf);
516 return(1);
517 }
518 return(0);
519}
520
521
deb58b9e 522void yesCallback(w, ret, dummy)
b34d5002 523Widget w;
deb58b9e 524XtPointer ret;
525XtPointer dummy;
b34d5002 526{
deb58b9e 527 int *ip = (int *)ret;
1cfb1b96 528 *ip = 1;
b34d5002 529}
530
deb58b9e 531void noCallback(w, ret, dummy)
b34d5002 532Widget w;
deb58b9e 533XtPointer ret;
534XtPointer dummy;
b34d5002 535{
deb58b9e 536 int *ip = (int *)ret;
1cfb1b96 537 *ip = -1;
b34d5002 538}
539
540
541static int value;
deb58b9e 542static XtCallbackRec yescb[] = { { yesCallback, (XtPointer) &value },
543 { NULL, NULL} };
544static XtCallbackRec nocb[] = { { noCallback, (XtPointer) &value },
545 {NULL, NULL} };
b34d5002 546
547Boolean AskQuestion(text, helpname)
deb58b9e 548char *text, *helpname;
b34d5002 549{
550 static Widget child;
551 Arg wargs[10];
552 int n;
553 static XmString label, yes = NULL, no;
554 XEvent event;
1cfb1b96 555 XtAppContext _XtDefaultAppContext();
b34d5002 556
e1d476e9 557 if (tty) {
558 char buf[256];
559
560 while (1) {
561 printf("%s (Y/N) ", text);
562 fflush(stdout);
563 if (mgets(buf, sizeof(buf)))
564 return(False);
565 if (buf[0] == 'T' || buf[0] == 't' ||
566 buf[0] == 'Y' || buf[0] == 'y')
567 return(True);
568 else if (buf[0] == 'F' || buf[0] == 'f' ||
569 buf[0] == 'N' || buf[0] == 'n')
570 return(False);
571 printf("Please answer Yes or No\r\n");
572 }
573 }
574
b34d5002 575 if (!yes) {
576 yes = XmStringCreate("Yes", XmSTRING_DEFAULT_CHARSET);
577 no = XmStringCreate("No", XmSTRING_DEFAULT_CHARSET);
578 }
579 if (label) {
580 XtFree(label);
581 XtDestroyWidget(child);
582 }
583
584 label = XmStringCreateLtoR( text, XmSTRING_DEFAULT_CHARSET);
585
586 n = 0;
587 XtSetArg(wargs[n], XmNmessageString, label); n++;
588 XtSetArg(wargs[n], XmNokLabelString, yes); n++;
589 XtSetArg(wargs[n], XmNcancelLabelString, no); n++;
590 XtSetArg(wargs[n], XmNokCallback, yescb); n++;
591 XtSetArg(wargs[n], XmNcancelCallback, nocb); n++;
592
593 child = (Widget) XmCreateQuestionDialog(logwidget, "question", wargs, n);
594 if (helpname)
595 XtAddCallback (child, XmNhelpCallback, extra_help_callback, helpname);
596 else
597 XtUnmanageChild(XmMessageBoxGetChild (child, XmDIALOG_HELP_BUTTON));
598
599 XtManageChild(child);
600 value = 0;
601 while (value == 0) {
602 XtAppNextEvent(_XtDefaultAppContext(), &event);
603 XtDispatchEvent(&event);
604 }
605 if (value > 0)
606 return(1);
607 else
608 return(0);
609}
610
611
612
613/******* temporary ********/
614display_error(msg)
615char *msg;
616{
4271ed91 617 PopupErrorMessage(msg, "no_more_help");
b34d5002 618}
This page took 0.148693 seconds and 5 git commands to generate.