summaryrefslogtreecommitdiffstats
path: root/exim_monitor/em_main.c
blob: 9c7f442e5b4e974d51f49cd2ba64b94a5be1ec56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*************************************************
*                  Exim Monitor                  *
*************************************************/

/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */


#include "em_hdr.h"

/* This module contains the main program of the Exim monitor, which
sets up the world and then lets the XtAppMainLoop function
run things off X events. */


/*************************************************
*               Static variables                 *
*************************************************/

/* Fallback resources */

static String fallback_resources[] = {"eximon.geometry: +150+0", NULL};

/* X11 fixed argument lists */

static Arg quit_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {XtNlabel,    (XtArgVal) " Quit "},
  {"left",      XawChainLeft},
  {"right",     XawChainLeft},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg resize_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
  {XtNlabel,    (XtArgVal) " Size "},
  {"left",      XawChainLeft},
  {"right",     XawChainLeft},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg update_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {XtNlabel,    (XtArgVal) " Update "},
  {"left",      XawChainLeft},
  {"right",     XawChainLeft},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg hide_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
  {XtNlabel,    (XtArgVal) " Hide "},
  {"left",      XawChainLeft},
  {"right",     XawChainLeft},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg unhide_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {XtNfromHoriz,(XtArgVal) NULL},         /* must be second */
  {XtNlabel,    (XtArgVal) " Unhide "},
  {"left",      XawChainLeft},
  {"right",     XawChainLeft},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg log_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {"editType",  XawtextEdit},
  {"useStringInPlace", (XtArgVal)TRUE},
  {"string",    (XtArgVal)""},            /* dummy to get it going */
  {"scrollVertical", XawtextScrollAlways},
  {"scrollHorizontal", XawtextScrollAlways},
  {"right",     XawChainRight},
  {"top",       XawChainTop},
  {"bottom",    XawChainTop}
};

static Arg queue_args[] = {
  {XtNfromVert, (XtArgVal) NULL},         /* must be first */
  {"editType",  XawtextEdit},
  {"string",    (XtArgVal)""},            /* dummy to get it going */
  {"scrollVertical", XawtextScrollAlways},
  {"right",     XawChainRight},
  {"top",       XawChainTop},
  {"bottom",    XawChainBottom}
};

static Arg sizepos_args[] = {
  {"width",     (XtArgVal)NULL},
  {"height",    (XtArgVal)NULL},
  {"x",         (XtArgVal)NULL},
  {"y",         (XtArgVal)NULL}
};

XtActionsRec menu_action_table[] = {
  { "menu-create",  menu_create } };

/* Types of non-message dialog action */

enum { da_hide };

/* Miscellaneous local variables */

static int dialog_action;
static int tick_stripchart_accumulator = 999999;
static int tick_interval = 2;
static int maxposset = 0;
static int minposset = 0;
static int x_adjustment = -1;
static int y_adjustment = -1;
static Dimension screenwidth, screenheight;
static Dimension original_x, original_y;
static Dimension maxposx, maxposy;
static Dimension minposx, minposy;
static Dimension maxwidth, maxheight;
static Widget outer_form_widget;
static Widget hide_widget;
static Widget above_queue_widget;




#ifdef STRERROR_FROM_ERRLIST
/*************************************************
*     Provide strerror() for non-ANSI libraries  *
*************************************************/

/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
in their libraries, but can provide the same facility by this simple
alternative function. */

uschar *
strerror(int n)
{
if (n < 0 || n >= sys_nerr) return "unknown error number";
return sys_errlist[n];
}
#endif /* STRERROR_FROM_ERRLIST */



/*************************************************
*         Handle attempts to write the log       *
*************************************************/

/* The message gets written to stderr when log_write() is called from a
utility. The message always gets '\n' added on the end of it. These calls come
from modules such as store.c when things go drastically wrong (e.g. malloc()
failing). In normal use they won't get obeyed.

Arguments:
  selector  not relevant when running a utility
  flags     not relevant when running a utility
  format    a printf() format
  ...       arguments for format

Returns:    nothing
*/

void
log_write(unsigned int selector, int flags, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
selector = selector;     /* Keep picky compilers happy */
flags = flags;
}




/*************************************************
*        Extract port from address string        *
*************************************************/

/* In the spool file, a host plus port is given as an IP address followed by a
dot and a port number. This function decodes this. It is needed by the
spool-reading function, and copied here to avoid having to include the whole
host.c module. One day the interaction between exim and eximon with regard to
included code MUST be tidied up!

Argument:
  address    points to the string; if there is a port, the '.' in the string
             is overwritten with zero to terminate the address

Returns:     0 if there is no port, else the port number.
*/

int
host_address_extract_port(uschar *address)
{
int skip = -3;                     /* Skip 3 dots in IPv4 addresses */
address--;
while (*(++address) != 0)
  {
  int ch = *address;
  if (ch == ':') skip = 0;         /* Skip 0 dots in IPv6 addresses */
    else if (ch == '.' && skip++ >= 0) break;
  }
if (*address == 0) return 0;
*address++ = 0;
return Uatoi(address);
}




/*************************************************
*                SIGCHLD handler                 *
*************************************************/

/* Operations on messages are done in subprocesses; this handler
just catches them when they finish. It causes a queue display update
unless configured not to. */

static void sigchld_handler(int sig)
{
while (waitpid(-1, NULL, WNOHANG) > 0);
signal(sig, sigchld_handler);
if (action_queue_update) tick_queue_accumulator = 999999;
}



/*************************************************
*             Callback routines                  *
*************************************************/


void updateAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;       /* Keep picky compilers happy */
client_data = client_data;
call_data = call_data;
scan_spool_input(TRUE);
queue_display();
tick_queue_accumulator = 0;
}

void hideAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;       /* Keep picky compilers happy */
client_data = client_data;
call_data = call_data;
actioned_message[0] = 0;
dialog_ref_widget = w;
dialog_action = da_hide;
create_dialog(US"Hide addresses ending with", US"");
}

void unhideAction(Widget w, XtPointer client_data, XtPointer call_data)
{
skip_item *sk = queue_skip;

w = w;       /* Keep picky compilers happy */
client_data = client_data;
call_data = call_data;

while (sk != NULL)
  {
  skip_item *next = sk->next;
  store_free(sk);
  sk = next;
  }
queue_skip = NULL;

XtDestroyWidget(unhide_widget);
unhide_widget = NULL;

scan_spool_input(TRUE);
queue_display();
tick_queue_accumulator = 0;
}

void quitAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;       /* Keep picky compilers happy */
client_data = client_data;
call_data = call_data;
exit(0);
}


/* Action when the "Size" button is pressed. This is a kludged up mess
that I made work after much messing around. Reading the position of the
toplevel widget gets the absolute position of the data portion of the window,
excluding the window manager's furniture. However, positioning the toplevel
widget's window seems to position the top corner of the furniture under the twm
window manager, but not under fwvm and others. The two cases are distinguished
by the values of x_adjustment and y_adjustment.

For twm (adjustment >= 0), one has to fudge the miminizing function to ensure
that we go back to exactly the same position as before.

For fwvm (adjustment < 0), one has to fudge the "top left hand corner"
positioning to ensure that the window manager's furniture gets displayed on the
screen. I haven't found a way of discovering the thickness of the furniture, so
some screwed-in values are used.

This is all ad hoc, developed by floundering around as I haven't found any
documentation that tells me what I really should do. */

void resizeAction(Widget button, XtPointer client_data, XtPointer call_data)
{
Dimension x, y;
Dimension width, height;
XWindowAttributes a;
Window w = XtWindow(toplevel_widget);

button = button;    /* Keep picky compilers happy */
client_data = client_data;
call_data = call_data;

/* Get the position and size of the top level widget. */

sizepos_args[0].value = (XtArgVal)(&width);
sizepos_args[1].value = (XtArgVal)(&height);
sizepos_args[2].value = (XtArgVal)(&x);
sizepos_args[3].value = (XtArgVal)(&y);
XtGetValues(toplevel_widget, sizepos_args, 4);

/* Get the position of the widget's window relative to its parent; this
gives the thickness of the window manager's furniture. At least it does
in twm. For fwvm it gives zero. The size/movement function uses this data.
I tried doing this before entering the main loop, but it didn't always
work properly with twm. Running it every time seems to be OK. */

XGetWindowAttributes(X_display, XtWindow(toplevel_widget), &a);
if (a.x != 0) x_adjustment = a.x;
if (a.y != 0) y_adjustment = a.y;

/* If at maximum size, reduce to minimum and move back to where it was
when maximized, if that value is set, allowing for the furniture in cases
where the positioning includes the furniture. */

if (width == maxwidth && height == maxheight)
  {
  maxposx = x;
  maxposy = y;
  maxposset = 1;

  if (minposset)
    xs_SetValues(toplevel_widget, 4,
      "width",     min_width,
      "height",    min_height,
      "x",         minposx - ((x_adjustment >= 0)? x_adjustment : 0),
      "y",         minposy - ((y_adjustment >= 0)? y_adjustment : 0));
  else
    xs_SetValues(toplevel_widget, 2,
      "width",     min_width,
      "height",    min_height);
  }

/* Else always expand to maximum. If currently at minimum size, remember where
it was for coming back. If we don't have a value for the thickness of the
furniture, the implication is that the coordinates position the application
window, so we can't use (0,0) because that loses the furniture. Use screwed in
values that seem to work with fvwm. */

else
  {
  int xx = x;
  int yy = y;

  if (width == min_width && height == min_height)
    {
    minposx = x;
    minposy = y;
    minposset = 1;
    }

  if ((int)(x + maxwidth) > (int)screenwidth ||
      (int)(y + maxheight + 10) > (int)screenheight)
    {
    if (maxposset)
      {
      xx = maxposx - ((x_adjustment >= 0)? x_adjustment : 0);
      yy = maxposy - ((y_adjustment >= 0)? y_adjustment : 0);
      }
    else
      {
      if ((int)(x + maxwidth) > (int)screenwidth)
        xx = (x_adjustment >= 0)? 0 : 4;
      if ((int)(y + maxheight + 10) > (int)screenheight)
        yy = (y_adjustment >= 0)? 0 : 21;
      }

    xs_SetValues(toplevel_widget, 4,
      "width",     maxwidth,
      "height",    maxheight,
      "x",         xx,
      "y",         yy);
    }

  else xs_SetValues(toplevel_widget, 2,
        "width",     maxwidth,
        "height",    maxheight);
  }

/* Ensure the window is at the top */

XRaiseWindow(X_display, w);
}




/*************************************************
*          Handle input from non-msg dialogue    *
*************************************************/

/* The various cases here are: hide domain, (no more yet) */

void NonMessageDialogue(uschar *s)
{
skip_item *sk;

switch(dialog_action)
  {
  case da_hide:

  /* Create the unhide button if not present */

  if (unhide_widget == NULL)
    {
    unhide_args[0].value = (XtArgVal) above_queue_widget;
    unhide_args[1].value = (XtArgVal) hide_widget;
    unhide_widget = XtCreateManagedWidget("unhide", commandWidgetClass,
      outer_form_widget, unhide_args, XtNumber(unhide_args));
    XtAddCallback(unhide_widget, "callback",  unhideAction, NULL);
    }

  /* Add item to skip queue */

  sk = (skip_item *)store_malloc(sizeof(skip_item) + Ustrlen(s));
  sk->next = queue_skip;
  queue_skip = sk;
  Ustrcpy(sk->text, s);
  sk->reveal = time(NULL) + 60 * 60;
  scan_spool_input(TRUE);
  queue_display();
  tick_queue_accumulator = 0;
  break;
  }
}



/*************************************************
*              Ticker function                   *
*************************************************/

/* This function is called initially to set up the starting data
values; it then sets a timeout so that it continues to be called
every 2 seconds. */

static void ticker(XtPointer pt, XtIntervalId *i)
{
pipe_item **pp = &pipe_chain;
pipe_item *p = pipe_chain;
tick_queue_accumulator += tick_interval;
tick_stripchart_accumulator += tick_interval;
read_log();

pt = pt;    /* Keep picky compilers happy */
i = i;

/* If we have passed the queue update time, we must do a full
scan of the queue, checking for new arrivals, etc. This will
as a by-product set the count of items for use by the stripchart
display. On some systems, SIGCHLD signals can get lost at busy times,
so just in case, clean up any completed children here. */

if (tick_queue_accumulator >= queue_update)
  {
  scan_spool_input(TRUE);
  queue_display();
  tick_queue_accumulator = 0;
  if (tick_stripchart_accumulator >= stripchart_update)
    tick_stripchart_accumulator = 0;
  while (waitpid(-1, NULL, WNOHANG) > 0);
  }

/* Otherwise, if we have exceeded the stripchart interval,
do a reduced queue scan that simply provides the count for
the stripchart. */

else if (tick_stripchart_accumulator >= stripchart_update)
  {
  scan_spool_input(FALSE);
  tick_stripchart_accumulator = 0;
  }

/* Scan any pipes that are set up for listening to delivery processes,
and display their output if their windows are still open. */

while (p != NULL)
  {
  int count;
  uschar buffer[256];

  while ((count = read(p->fd, buffer, 254)) > 0)
    {
    buffer[count] = 0;
    if (p->widget != NULL) text_show(p->widget, buffer);
    }

  if (count == 0)
    {
    close(p->fd);
    *pp = p->next;
    store_free(p);
    /* If configured, cause display update */
    if (action_queue_update) tick_queue_accumulator = 999999;
    }

  else pp = &(p->next);

  p = *pp;
  }

/* Reset the timer for next time */

XtAppAddTimeOut(X_appcon, tick_interval * 1000, ticker, 0);
}



/*************************************************
*             Find Num Lock modifiers            *
*************************************************/

/* Return a string with the modifiers generated by XK_Num_Lock, or return
NULL if XK_Num_Lock doesn't generate any modifiers. This is needed because Num
Lock isn't always the same modifier on all servers.

Arguments:
  display   the Display
  buf       a buffer in which to put the answers (long enough to hold 5)

Returns:    points to the buffer, or NULL
*/

static uschar *
numlock_modifiers(Display *display, uschar *buf)
{
XModifierKeymap *m;
int i, j;
uschar *ret = NULL;

m = XGetModifierMapping(display);
if (m == NULL)
  {
  printf("Not enough memory\n");
  exit (EXIT_FAILURE);
  }

/* Look at Mod1 through Mod5, and fill in the buffer as necessary. */

buf[0] = 0;
for (i = 3; i < 8; i++)
  {
  for (j = 0; j < m->max_keypermod; j++)
    {
    if (XKeycodeToKeysym(display, m->modifiermap [i*m->max_keypermod + j], 0)
        == XK_Num_Lock)
      {
      sprintf(CS(buf+Ustrlen(buf)), " Mod%d", i-2);
      ret = buf;
      }
    }
  }

XFreeModifiermap(m);
return ret;
}



/*************************************************
*               Initialize                       *
*************************************************/

int main(int argc, char **argv)
{
int i;
struct stat statdata;
uschar modbuf[] = " Mod1 Mod2 Mod3 Mod4 Mod5";
uschar *numlock;
Widget stripchart_form_widget,
       update_widget,
       quit_widget,
       resize_widget;

/* The exim global message_id needs to get set */

message_id_external = message_id_option + 1;
message_id = message_id_external + 1;
message_subdir[1] = 0;

/* Some store needs getting for big_buffer, which is used for
constructing file names and things. This call will initialize
the store_get() function. */

big_buffer = store_get(big_buffer_size, FALSE);

/* Set up the version string and date and output them */

version_init();
printf("\nExim Monitor version %s (compiled %s) initializing\n",
  version_string, version_date);

/* Initialize various things from the environment and arguments. */

init(argc, USS argv);

/* Set up the SIGCHLD handler */

signal(SIGCHLD, sigchld_handler);

/* Get the buffer for storing the string for the log display. */

log_display_buffer = US store_malloc(log_buffer_size);
log_display_buffer[0] = 0;

/* Initialize the data structures for the stripcharts */

stripchart_init();

/* If log_file contains the empty string, then Exim is running using syslog
only, and we can't tail the log. If not, open the log file and position to the
end of it. Before doing so, we have to detect whether the log files are
datestamped, and if so, sort out the name. The string in log_file already has
%s replaced by "main"; if datestamping is occurring, %D or %M will be present.
In fact, we don't need to test explicitly - just process the string with
string_format.

Once opened, save the file's inode so that we can detect when the file is
switched to another one for non-datestamped files. However, allow the monitor
to start up without a log file (can happen if no messages have been sent
today.) */

if (log_file[0] != 0)
  {
  /* Do *not* use "%s" here, we need the %D datestamp in the log_file to
  be expanded! */
  (void)string_format(log_file_open, sizeof(log_file_open), CS log_file, NULL);
  log_datestamping = string_datestamp_offset >= 0;

  LOG = fopen(CS log_file_open, "r");

  if (LOG == NULL)
    {
    printf("*** eximon warning: can't open log file %s - will try "
      "periodically\n", log_file_open);
    }
  else
    {
    fseek(LOG, 0, SEEK_END);
    log_position = ftell(LOG);
    if (fstat(fileno(LOG), &statdata))
      {
      perror("log file fstat");
      fclose(LOG);
      LOG=NULL;
      }
    else
      log_inode = statdata.st_ino;
    }
  }
else
  {
  printf("*** eximon warning: no log file available to tail\n");
  }

/* Now initialize the X world and create the top-level widget */

toplevel_widget = XtAppInitialize(&X_appcon, "Eximon", NULL, 0, &argc, argv,
  fallback_resources, NULL, 0);
X_display = XtDisplay(toplevel_widget);
xs_SetValues(toplevel_widget, 4,
  "title",     window_title,
  "iconName",  window_title,
  "minWidth",  min_width,
  "minHeight", min_height);


/* Create the action for setting up the menu in the queue display
window, and register the action for positioning the menu. */

XtAppAddActions(X_appcon, menu_action_table, 1);
XawSimpleMenuAddGlobalActions(X_appcon);

/* Set up translation tables for the text widgets we use. We don't
want all the generality of editing, etc. that the defaults provide.
This cannot be done before initializing X - the parser complains
about unknown events, modifiers, etc. in an unhelpful way... The
queue text widget has a different table which includes the button
for popping up the menu. Note that the order of things in these
tables is significant. Shift<thing> must come before <thing> as
otherwise it isn't noticed. */

/*
   <FocusIn>:      display-caret(on)\n\
   <FocusOut>:     display-caret(off)\n\
*/

/* The translation manager sets up passive grabs for the menu popups as a
result of MenuPopup(), but the grabs match only the exact modifiers listed,
hence combinations with and without caps-lock and num-lock must be given,
rather than just one "Shift<Btn1Down>" (or whatever menu_event is set to),
despite the fact that that notation (without a leading !) should ignore the
state of other modifiers. Thanks to Kevin Ryde for this information, and for
the function above that discovers which modifier is Num Lock, because it turns
out that it varies from server to server. */

sprintf(CS big_buffer,
  "!%s:            menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
   !Lock %s:       menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
  ", menu_event, menu_event);

numlock = numlock_modifiers(X_display, modbuf); /* Get Num Lock modifier(s) */

if (numlock != NULL) sprintf(CS big_buffer + Ustrlen(big_buffer),
  "!%s %s:         menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
   !Lock %s %s:    menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\
  ", numlock, menu_event, numlock, menu_event);

sprintf(CS big_buffer + Ustrlen(big_buffer),
  "<Btn1Down>:     select-start()\n\
   <Btn1Motion>:   extend-adjust()\n\
   <Btn1Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
   <Btn3Down>:     extend-start()\n\
   <Btn3Motion>:   extend-adjust()\n\
   <Btn3Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
   <Key>Up:        scroll-one-line-down()\n\
   <Key>Down:      scroll-one-line-up()\n\
   Ctrl<Key>R:     search(backward)\n\
   Ctrl<Key>S:     search(forward)\n\
  ");

queue_trans = XtParseTranslationTable(CS big_buffer);

text_trans = XtParseTranslationTable(
  "<Btn1Down>:     select-start()\n\
   <Btn1Motion>:   extend-adjust()\n\
   <Btn1Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
   <Btn3Down>:     extend-start()\n\
   <Btn3Motion>:   extend-adjust()\n\
   <Btn3Up>:       extend-end(PRIMARY,CUT_BUFFER0)\n\
   <Key>Up:        scroll-one-line-down()\n\
   <Key>Down:      scroll-one-line-up()\n\
   Ctrl<Key>R:     search(backward)\n\
   Ctrl<Key>S:     search(forward)\n\
  ");


/* Create a toplevel form widget to hold all the other things */

outer_form_widget = XtCreateManagedWidget("form", formWidgetClass,
  toplevel_widget, NULL, 0);

/* Now create an inner form to hold the stripcharts */

stripchart_form_widget = XtCreateManagedWidget("form", formWidgetClass,
  outer_form_widget, NULL, 0);
xs_SetValues(stripchart_form_widget, 5,
  "defaultDistance", 8,
  "left",            XawChainLeft,
  "right",           XawChainLeft,
  "top",             XawChainTop,
  "bottom",          XawChainTop);

/* Create the queue count stripchart and its label. */

create_stripchart(stripchart_form_widget, queue_stripchart_name);

/* If configured, create the size monitoring stripchart, but
only if the OS supports statfs(). */

if (size_stripchart != NULL)
  {
#ifdef HAVE_STATFS
  if (size_stripchart_name == NULL)
    {
    size_stripchart_name = size_stripchart + Ustrlen(size_stripchart) - 1;
    while (size_stripchart_name > size_stripchart &&
      *size_stripchart_name == '/') size_stripchart_name--;
    while (size_stripchart_name > size_stripchart &&
      *size_stripchart_name != '/') size_stripchart_name--;
    }
  create_stripchart(stripchart_form_widget, size_stripchart_name);
#else
  printf("Can't create size stripchart: statfs() function not available\n");
#endif
  }

/* Now create the configured input/output stripcharts; note
the total number includes the queue stripchart. */

for (i = stripchart_varstart; i < stripchart_number; i++)
  create_stripchart(stripchart_form_widget, stripchart_title[i]);

/* Next in vertical order come the Resize & Quit buttons */

quit_args[0].value = (XtArgVal) stripchart_form_widget;
quit_widget = XtCreateManagedWidget("quit", commandWidgetClass,
  outer_form_widget, quit_args, XtNumber(quit_args));
XtAddCallback(quit_widget, "callback",  quitAction, NULL);

resize_args[0].value = (XtArgVal) stripchart_form_widget;
resize_args[1].value = (XtArgVal) quit_widget;
resize_widget = XtCreateManagedWidget("resize", commandWidgetClass,
  outer_form_widget, resize_args, XtNumber(resize_args));
XtAddCallback(resize_widget, "callback",  resizeAction, NULL);

/* In the absence of log tailing, the quit widget is the one above the
queue listing. */

above_queue_widget = quit_widget;

/* Create an Ascii text widget for the log tail display if we are tailing a
log. Skip it if not. */

if (log_file[0] != 0)
  {
  log_args[0].value = (XtArgVal) quit_widget;
  log_widget = XtCreateManagedWidget("log", asciiTextWidgetClass,
    outer_form_widget, log_args, XtNumber(log_args));
  XawTextDisplayCaret(log_widget, TRUE);
  xs_SetValues(log_widget, 6,
    "editType",  XawtextEdit,
    "translations", text_trans,
    "string",    log_display_buffer,
    "length",    log_buffer_size,
    "height",    log_depth,
    "width",     log_width);

  if (log_font != NULL)
    {
    XFontStruct *f = XLoadQueryFont(X_display, CS log_font);
    if (f != NULL) xs_SetValues(log_widget, 1, "font", f);
    }

  above_queue_widget = log_widget;
  }

/* The update button */

update_args[0].value = (XtArgVal) above_queue_widget;
update_widget = XtCreateManagedWidget("update", commandWidgetClass,
  outer_form_widget, update_args, XtNumber(update_args));
XtAddCallback(update_widget, "callback",  updateAction, NULL);

/* The hide button */

hide_args[0].value = (XtArgVal) above_queue_widget;
hide_args[1].value = (XtArgVal) update_widget;
hide_widget = XtCreateManagedWidget("hide", commandWidgetClass,
  outer_form_widget, hide_args, XtNumber(hide_args));
XtAddCallback(hide_widget, "callback",  hideAction, NULL);

/* Create an Ascii text widget for the queue display. */

queue_args[0].value = (XtArgVal) update_widget;
queue_widget = XtCreateManagedWidget("queue", asciiTextWidgetClass,
  outer_form_widget, queue_args, XtNumber(queue_args));
XawTextDisplayCaret(queue_widget, TRUE);

xs_SetValues(queue_widget, 4,
  "editType",  XawtextEdit,
  "height",    queue_depth,
  "width",     queue_width,
  "translations", queue_trans);

if (queue_font != NULL)
  {
  XFontStruct *f = XLoadQueryFont(X_display, CS queue_font);
  if (f != NULL) xs_SetValues(queue_widget, 1, "font", f);
  }

/* Call the ticker function to get the initial data set up. It
arranges to have itself recalled every 2 seconds. */

ticker(NULL, NULL);

/* Everything is now set up; this flag is used by the regerror
function and also by the queue reader. */

eximon_initialized = TRUE;
printf("\nExim Monitor running\n");

/* Realize the toplevel and thereby get things displayed */

XtRealizeWidget(toplevel_widget);

/* Find out the size of the initial window, and set that as its
maximum. While we are at it, get the initial position. */

sizepos_args[0].value = (XtArgVal)(&maxwidth);
sizepos_args[1].value = (XtArgVal)(&maxheight);
sizepos_args[2].value = (XtArgVal)(&original_x);
sizepos_args[3].value = (XtArgVal)(&original_y);
XtGetValues(toplevel_widget, sizepos_args, 4);

xs_SetValues(toplevel_widget, 2,
  "maxWidth",  maxwidth,
  "maxHeight", maxheight);

/* Set up the size of the screen */

screenwidth = XDisplayWidth(X_display, 0);
screenheight= XDisplayHeight(X_display,0);

/* Register the action table */

XtAppAddActions(X_appcon, actionTable, actionTableSize);

/* Reduce the window to the small size if this is wanted */

if (start_small) resizeAction(NULL, NULL, NULL);

/* Enter the application loop which handles things from here
onwards. The return statement is never obeyed, but is needed to
keep pedantic ANSI compilers happy. */

XtAppMainLoop(X_appcon);

return 0;
}

/* End of em_main.c */