Fix warnings on FreeBSD
[oweals/cde.git] / cde / programs / ttsnoop / ttsnoop.C.src
1 /*** DTB_USER_CODE_START vvv Add file header below vvv ***/
2 /* $TOG: ttsnoop.C.src /main/8 1999/09/20 16:07:27 mgreess $ */
3 /*** DTB_USER_CODE_END   ^^^ Add file header above ^^^ ***/
4
5 /*
6  * File: ttsnoop.c
7  * Contains: main() and cross-module connections
8  *
9  * This file was generated by dtcodegen, from project ttsnoop
10  *
11  * Any text may be added between the DTB_USER_CODE_START and
12  * DTB_USER_CODE_END comments (even non-C code). Descriptive comments
13  * are provided only as an aid.
14  *
15  *  ** EDIT ONLY WITHIN SECTIONS MARKED WITH DTB_USER_CODE COMMENTS.  **
16  *  ** ALL OTHER MODIFICATIONS WILL BE OVERWRITTEN. DO NOT MODIFY OR  **
17  *  ** DELETE THE GENERATED COMMENTS!                                 **
18  */
19
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <X11/Intrinsic.h>
28 #include <Xm/Xm.h>
29 #include <Xm/MwmUtil.h>
30 #include <Xm/Protocols.h>
31 #include <Dt/Help.h>
32 #include <Dt/HelpQuickD.h>
33 #include <Dt/HelpDialog.h>
34 #include <unistd.h>
35 #include <Dt/Session.h>
36 #include <Dt/Term.h>
37 #include "ttsnoop_ui.h"
38 #include "apiTracer_ui.h"
39 #include "ttChooser_ui.h"
40 #include "patternProps_ui.h"
41 #include "stringChooser_ui.h"
42 #include "messageProps_ui.h"
43 #include "sessionChooser_ui.h"
44 #include "fileChooser_ui.h"
45 #include "argChooser_ui.h"
46 #include "callbackChooser_ui.h"
47 #include "ttsnoop.h"
48 #include "dtb_utils.h"
49
50
51 /**************************************************************************
52  *** DTB_USER_CODE_START
53  ***
54  *** All necessary header files have been included.
55  ***
56  *** Add include files, types, macros, externs, and user functions here.
57  ***/
58
59 #include <errno.h>
60 #include <stdlib.h>
61 #include <signal.h>
62 #include <sys/wait.h>
63 #include <sys/stat.h>
64 #include <time.h>
65
66 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
67 #include <iostream>
68 #include <strstream>
69 #include <fstream>
70 #else
71 #include <iostream.h>
72 #include <strstream.h>
73 #include <fstream.h>
74 #endif
75
76 #include <Dt/Term.h>
77 #include <Tt/tt_c.h>
78 #include <Tt/tttk.h>
79 #include "tt_c++.h"
80 #include "DtTt.h"
81
82 //#include <Xm/ColorObj.h>
83 //#include <Xm/ColorObjP.h>
84 //extern "C" { Pixmap XmeGetMask( Screen *, const char * ); }
85
86 Boolean         optImmediateTracing     = False;
87 Boolean         optMapOnOutput          = False;
88 Boolean         optImmediateSnooping    = True;
89 Boolean         optImmediateTtOpen      = True;
90 Boolean         optImmediateXSession    = False;
91 char *          optImmediateSession     = 0;
92 Tt_pattern      snoopPat                = 0;
93 Boolean         snoopPatIsRegistered    = False;
94 char *          snoopFile               = 0;
95 Boolean         unlinkSnoopFile         = True;
96 char *          traceFile               = 0;
97 Boolean         unlinkTraceFile         = True;
98 char *          traceScript             = 0;
99 int             globalTimeout           = 20000;
100 unsigned int    globalSaveLines         = 5000;
101 const char *    globalVersionString     = "1.0";
102 char **         scopeFiles              = 0;
103 unsigned int    scopeFilesCount         = 0;
104 char **         ops                     = 0;
105 unsigned int    opsCount                = 0;
106 char **         senders                 = 0;
107 unsigned int    sendersCount            = 0;
108 char *          vtype                   = 0;
109 pid_t           snoopedPid              = -1;
110 char **         snoopedArgs             = 0;
111 unsigned int    snoopedArgsCount        = 0;
112 char *          optTraceScript          = 0;
113 String          apiTracerArgv[ 10 ];
114 String          snooperArgv[ 10 ];
115 std::ostrstream tttraceCmd;
116 std::ofstream   snoopStream;
117
118 // Xt squats on -tf ?! XXX
119 const char Usage[] =
120 "ttsnoop - ToolTalk graphical user interface\n"
121 "Usage: ttsnoop [options] [-F scopefile] [-< procid] [-v media] [-m op]\n"
122 "       ttsnoop [options] -n|-N\n"
123 "       ttsnoop [options] [-e script] command [args]\n"
124 "\n"
125 "-F scopefile   scope initial pattern also to scopefile\n"
126 "-\\< \"procid\"        limit initial pattern to messages from procid\n"
127 "-v mediaType   limit initial pattern to messages for mediaType\n"
128 "-m op          limit initial pattern to given op\n"
129 "\n"
130 "-n             skip initial pattern\n"
131 "-N             skip initial ttdt_open(), also\n"
132 "\n"
133 "command [args] invoke command [with args] and snoop its TT API calls\n"
134 "-e script      Take script as a tttrace setting.  See tttracefile(4).\n"
135 "\n"
136 "options ::=    [-Tu] [-S sessid] [-w n] [-l n] [-o snoopfile] [-O tracefile]\n"
137 "-T             trace (even initial) TT API calls made by ttsnoop\n"
138 "-u             map (de-iconify) on snoop output\n"
139 "-S sessid      set default session to sessid\n"
140 "-X             set default session to tt_X_session of $DISPLAY\n"
141 "-w n           set global timeout to n seconds [default: 20]\n"
142 "-l n           set tttrace dtterm saveLines to n lines [default: 5000]\n"
143 "-o snoopfile   log snoop output to snoopfile\n"
144 "-O tracefile   log api tracing to tracefile\n"
145 "\n"
146 "-xrm \"*DtTerm.saveLines: n\"\n"
147 "-xrm \"*DtTerm.rows: n\"\n"
148 "-xrm \"*DtTerm.columns: n\"\n"
149 "-xrm \"*DtTerm.mapOnOutput: true\"\n";
150
151 static void
152 cleanUp()
153 {
154         if (unlinkSnoopFile) {
155                 if (unlink( snoopFile ) == -1) {
156                         clog << "ttsnoop: unlink( \"" << snoopFile;
157                         clog << "\"): " << strerror( errno ) << endl;
158                 } else {
159                         unlinkSnoopFile = False;
160                 }
161         }
162         if (unlinkTraceFile) {
163                 if (unlink( traceFile ) == -1) {
164                         clog << "ttsnoop: unlink( \"" << traceFile;
165                         clog << "\"): " << strerror( errno ) << endl;
166                 } else {
167                         unlinkTraceFile = False;
168                 }
169         }
170 }
171
172 static void
173 signalHandler(
174         int sig
175 )
176 {
177         pid_t child;
178         int status;
179         switch (sig) {
180             case SIGHUP:
181             case SIGINT:
182             case SIGQUIT:
183             case SIGPIPE:
184             case SIGTERM:
185                 exit( 5 );
186             case SIGCHLD:
187                 child = waitpid( snoopedPid, &status, WNOHANG );
188                 if ((child > 0) && WIFEXITED( status )) {
189                         snoopStream << endl << endl << "SIGCHLD: WEXITSTATUS=="
190                                 << WEXITSTATUS(status) << ": "
191                                 << tttraceCmd.str() << endl << endl;
192                 }
193                 break;
194             case SIGCONT:
195                 break;
196         }
197 }
198
199 #if defined(SVR4) || defined(aix) || defined(hpux) || defined(__linux__) || defined(CSRG_BASED)
200 #if !defined(SIG_PF)
201 typedef void (*sig_pf_t)(int);
202 #define SIG_PF sig_pf_t
203 #endif
204 #endif
205
206 int
207 _tt_sigset(
208         int     sig,
209         SIG_PF  handler )
210 {
211 #if defined(hpux) || defined(__linux__) || defined(CSRG_BASED)
212         struct sigaction act;
213         act.sa_handler = handler;
214         sigemptyset(&act.sa_mask);
215         act.sa_flags = 0;
216         return 0==sigaction(sig, &act, NULL);
217 #elif defined(OPT_BSD_SIGNAL)
218         return SIG_ERR!=signal(sig, handler);
219 #else
220         return SIG_ERR!=sigset(sig, handler);
221 #endif
222 }
223
224 static void
225 installSignalHandler()
226 {
227     _tt_sigset( SIGHUP, signalHandler );
228     _tt_sigset( SIGINT, signalHandler );
229     _tt_sigset( SIGQUIT, signalHandler );
230     _tt_sigset( SIGPIPE, signalHandler );
231     _tt_sigset( SIGTERM, signalHandler );
232     _tt_sigset( SIGCHLD, signalHandler );
233     _tt_sigset( SIGCONT, signalHandler );
234     atexit( cleanUp );
235 }
236
237 void
238 snoopIt(
239         const char *callBackType,
240         void *callBack,
241         Tt_message msg,
242         Tt_pattern pat,
243         Boolean printPat
244 )
245 {
246         if (snoopStream.bad()) {
247                 return;
248         }
249         char timeBuf[ 16 ];
250         time_t now = time( 0 );
251         strftime( timeBuf, sizeof( timeBuf ), "%T", localtime( &now ));
252         snoopStream << timeBuf << " (" << callBackType << ")" << callBack;
253         snoopStream << "( " << "(Tt_message)" << (void *)msg << ", ";
254         if (printPat) {
255                 snoopStream << "(Tt_pattern)" << (void *)pat;
256         } else {
257                 snoopStream << "...";
258         }
259         snoopStream << " ): ";
260         char *msgString = tt_message_print( msg );
261         Tt_status status = tt_ptr_error( msgString );
262         if (status == TT_OK) {
263                 snoopStream << endl << msgString << endl;
264         } else {
265                 snoopStream << status << endl;
266         }
267         tt_free( msgString );
268 }
269
270 static Tt_callback_action
271 justSnoopIt(
272         Tt_message msg,
273         Tt_pattern pat
274 )
275 {
276         DtTtCreated( DTTT_MESSAGE, msg ); // XXX bad idea?
277         snoopIt( "Tt_callback_action", (void *) justSnoopIt, msg, pat, True );
278         return TT_CALLBACK_PROCESSED;
279 }
280
281 Tt_message
282 _DtTtMediaLoadPatCb(
283         Tt_message      msg,
284         void           *clientdata,
285         Tttk_op         op,
286         Tt_status       diagnosis,
287         unsigned char  *contents,
288         int             len,
289         char           *file,
290         char           *docname
291 )
292 {
293         tt_free( (caddr_t)contents );
294         tt_free( file );
295         tt_free( docname );
296         snoopIt( "Ttmedia_load_pat_cb", (void *) _DtTtMediaLoadPatCb, msg );
297         return 0;
298 }
299
300 /*** DTB_USER_CODE_END
301  ***
302  *** End of user code section
303  ***
304  **************************************************************************/
305
306
307
308 /*
309  * command line options...
310  */
311 static XrmOptionDescRec optionDescList[] = {
312     {"-session", "*session", XrmoptionSepArg, (XPointer)NULL}
313     
314     /*** DTB_USER_CODE_START vvv Add structure fields below vvv ***/
315     /*** DTB_USER_CODE_END   ^^^ Add structure fields above ^^^ ***/
316 };
317
318 /*
319  * Application Resources
320  */
321 static XtResource resources[] = {
322     {"session", "Session", XtRString, sizeof(String),
323         XtOffsetOf(DtbAppResourceRec, session_file), XtRImmediate, (XtPointer)NULL}
324     
325     /*** DTB_USER_CODE_START vvv Add structure fields below vvv ***/
326     /*** DTB_USER_CODE_END   ^^^ Add structure fields above ^^^ ***/
327 };
328
329 DtbAppResourceRec       dtb_app_resource_rec;
330
331
332 /*
333  * main for application ttsnoop
334  */
335 int
336 main(int argc, char **argv)
337 {
338     Widget              toplevel = NULL;
339     Display             *display = NULL;
340     XtAppContext        app;
341     Atom                save_yourself_atom;
342
343     Pixmap      icon_pixmap = 0;
344     Pixmap      icon_mask_pixmap = 0;
345     
346     /**************************************************************************
347      *** DTB_USER_CODE_START
348      ***
349      ***  No initialization has been done.
350      ***
351      *** Add local variables and code.
352      ***/
353     
354     /*
355      * The application must call DtTermInitialize() before 
356      * initializing the Xt Toolkit with XtAppInitialize(3X).
357      */
358     DtTermInitialize();
359
360     /*** DTB_USER_CODE_END
361      ***
362      *** End of user code section
363      ***
364      **************************************************************************/
365     
366     toplevel = XtVaAppInitialize(&app, "Ttsnoop",
367         optionDescList, XtNumber(optionDescList),
368         &argc, argv, (String *)NULL,
369         (ArgList) NULL, 0);
370
371     /*
372      * Get display and verify initialization was successful.
373      */
374     if (toplevel != NULL)
375     {
376         display = XtDisplayOfObject(toplevel);
377     }
378     if (display == NULL)
379     {
380         fprintf(stderr, "Could not open display.");
381         exit(1);
382     }
383     
384     /*
385      * Save the toplevel widget so it can be fetched later as needed.
386      */
387     dtb_save_toplevel_widget(toplevel);
388     
389     /*
390      * Save the command used to invoke the application.
391      */
392     dtb_save_command(argv[0]);
393     
394     XtGetApplicationResources(toplevel, (XtPointer)&dtb_app_resource_rec,
395         resources, XtNumber(resources), (Arg *)NULL, 0);
396     
397     
398     /**************************************************************************
399      *** DTB_USER_CODE_START
400      ***
401      ***      A connection to the X server has been established, and all
402      *** initialization has been done.
403      ***
404      ***  Add extra initialization code after this comment.
405      ***/
406
407     /*
408     PixelSet    pixels[MAX_NUM_COLORS];
409     int         colorUse;
410     short       act, inact, prim, second;
411     XmeGetPixelData( DefaultScreen( display ), &colorUse, pixels,
412                      &act, &inact, &prim, &second );
413     Pixmap pixmap = XmGetPixmap( DefaultScreenOfDisplay( display ),
414                                  "DtTtsnp.l", pixels[1].fg, pixels[1].bg );
415     XtVaSetValues( toplevel, XmNiconPixmap, pixmap, NULL) ;
416     pixmap = XmeGetMask( XtScreen( toplevel ), "DtTtsnp.l" );
417     XtVaSetValues( toplevel, XmNiconMask, pixmap, NULL) ;
418     */
419     Boolean committed2Snooping = False;
420     int c;
421     while ((c = getopt(argc, argv, "?F:<:v:nNe:TuS:Xw:l:o:O:")) != -1) {
422             switch (c) {
423                 case 'F':
424                     if (! optImmediateSnooping) {
425                             clog << Usage; exit( 2 );
426                     }
427                     committed2Snooping = 1;
428                     listAppend( scopeFiles, scopeFilesCount, char *, optarg );
429                     break;
430                 case '<':
431                     if (! optImmediateSnooping) {
432                             clog << Usage; exit( 2 );
433                     }
434                     committed2Snooping = 1;
435                     listAppend( senders, sendersCount, char *, optarg );
436                     break;
437                 case 'v':
438                     if ((! optImmediateSnooping) || (vtype != 0)) {
439                             clog << Usage; exit( 2 );
440                     }
441                     committed2Snooping = 1;
442                     vtype = optarg;
443                     break;
444                 case 'm':
445                     if (! optImmediateSnooping) {
446                             clog << Usage; exit( 2 );
447                     }
448                     committed2Snooping = 1;
449                     listAppend( ops, opsCount, char *, optarg );
450                     break;
451                 case 'n':
452                     if (committed2Snooping) {
453                             clog << Usage; exit( 2 );
454                     }
455                     optImmediateSnooping = False;
456                     break;
457                 case 'N':
458                     if (committed2Snooping) {
459                             clog << Usage; exit( 2 );
460                     }
461                     optImmediateSnooping = False;
462                     optImmediateTtOpen = False;
463                     break;
464                 case 'e':
465                     optTraceScript = optarg;
466                     break;
467                 case 'T':
468                     optImmediateTracing = True;
469                     break;
470                 case 'u':
471                     optMapOnOutput = True;
472                     break;
473                 case 'S':
474                     if (optImmediateSession != 0) {
475                             clog << Usage; exit( 2 );
476                     }
477                     optImmediateSession = optarg;
478                     break;
479                 case 'X':
480                     if (optImmediateSession != 0) {
481                             clog << Usage; exit( 2 );
482                     }
483                     optImmediateXSession = True;
484                     break;
485                 case 'w':
486                     globalTimeout = atoi( optarg );
487                     if (globalTimeout > 0) {
488                             globalTimeout *= 1000;
489                     }
490                     break;
491                 case 'l':
492                     globalSaveLines = atoi( optarg );
493                     break;
494                 case 'o':
495                     if (snoopFile != 0) {
496                             clog << Usage; exit( 2 );
497                     }
498                     snoopFile = optarg;
499                     unlinkSnoopFile = False;
500                     break;
501                 case 'O':
502                     if (traceFile != 0) {
503                             clog << Usage; exit( 2 );
504                     }
505                     traceFile = optarg;
506                     unlinkTraceFile = False;
507                     break;
508                 case '?':
509                 default:
510                     clog << Usage; exit( 2 );
511             }
512     }
513
514     installSignalHandler();
515     if (traceFile == 0) {
516             //
517             // Set up fifo for trace output
518             //
519             traceFile = tempnam( 0, "ttsnt" );
520             if (mkfifo( traceFile, S_IWUSR | S_IRUSR ) == -1) {
521                     clog << "ttsnoop: mkfifo( \"" << traceFile << "\" ) = ";
522                     clog << strerror( errno ) << endl;
523             }
524     }
525     apiTracerArgv[ 0 ] = "tail";
526     apiTracerArgv[ 1 ] = "+0f";
527     apiTracerArgv[ 2 ] = traceFile;
528     if (snoopFile == 0) {
529             //
530             // Set up fifo for snoop output
531             //
532             snoopFile = tempnam( 0, "ttsnp" );
533             if (mkfifo( snoopFile, S_IWUSR | S_IRUSR ) == -1) {
534                     clog << "ttsnoop: mkfifo( \"" << snoopFile << "\" ) = ";
535                     clog << strerror( errno ) << endl;
536             }
537     }
538     snooperArgv[ 0 ] = "tail";
539     snooperArgv[ 1 ] = "+0f";
540     snooperArgv[ 2 ] = snoopFile;
541
542     if (optind < argc) {
543             if (committed2Snooping) {
544                     clog << Usage; exit( 2 );
545             }
546             optImmediateSnooping = False;
547             optImmediateTtOpen = False;
548             listAppend( snoopedArgs, snoopedArgsCount, char *, "tttrace" );
549             listAppend( snoopedArgs, snoopedArgsCount, char *, "-o" );
550             listAppend( snoopedArgs, snoopedArgsCount, char *, snoopFile );
551             tttraceCmd << "tttrace -o " << snoopFile;
552             if (optTraceScript != 0) {
553                     listAppend( snoopedArgs, snoopedArgsCount, char *, "-e" );
554                     listAppend( snoopedArgs, snoopedArgsCount, char *,
555                                 optTraceScript );
556                     tttraceCmd << " -e \"" << optTraceScript << "\"";
557             }
558             for (int i = optind; i < argc; i++) {
559                     listAppend( snoopedArgs, snoopedArgsCount, char *,
560                                 argv[i] );
561                     tttraceCmd << " " << argv[i];
562             }
563             tttraceCmd << ends;
564             listAppend( snoopedArgs, snoopedArgsCount, char *, 0 );
565     }
566
567 #if defined(aix)
568 #define AIX_STRING_LIST         (char * const *)
569 #else
570 #define AIX_STRING_LIST
571 #endif
572
573     if (snoopedArgsCount > 0) {
574             snoopedPid = fork();
575             switch (snoopedPid) {
576                 case -1:
577                     clog << "ttsnoop: fork(): " << strerror( errno ) << endl;
578                     exit( 3 );
579                 case 0:
580                     execvp( snoopedArgs[0], AIX_STRING_LIST snoopedArgs );
581                     clog << "ttsnoop: execvp(): " << strerror( errno ) << endl;
582                     exit( 3 );
583             }
584             installSignalHandler();
585     }
586     
587     /*** DTB_USER_CODE_END
588      ***
589      *** End of user code section
590      ***
591      **************************************************************************/
592     
593
594     
595     /*
596      * Initialize all global variables.
597      */
598     dtbTtsnoopTtsnoopWinInfo_clear(&dtb_ttsnoop_ttsnoop_win);
599     dtbApiTracerTracerInfo_clear(&dtb_api_tracer_tracer);
600     dtbTtChooserChooserInfo_clear(&dtb_tt_chooser_chooser);
601     dtbPatternPropsPatternPropsInfo_clear(&dtb_pattern_props_pattern_props);
602     dtbStringChooserStringChooserInfo_clear(&dtb_string_chooser_string_chooser);
603     dtbMessagePropsMessagePropsInfo_clear(&dtb_message_props_message_props);
604     dtbSessionChooserSessionChooserInfo_clear(&dtb_session_chooser_session_chooser);
605     dtbFileChooserFchooserInfo_clear(&dtb_file_chooser_fchooser);
606     dtbArgChooserArgChooserInfo_clear(&dtb_arg_chooser_arg_chooser);
607     dtbCallbackChooserCallbackChooserInfo_clear(&dtb_callback_chooser_callback_chooser);
608     
609     /*
610      * Set up the application's root window.
611      */
612     dtb_ttsnoop_ttsnoop_win.ttsnoopWin = toplevel;
613     dtb_cvt_image_file_to_pixmap(dtb_ttsnoop_ttsnoop_win.ttsnoopWin,
614         "DtTtsnp.l", &icon_pixmap);
615     dtb_cvt_image_file_to_pixmap(dtb_ttsnoop_ttsnoop_win.ttsnoopWin,
616         "DtTtsnp.l_m", &icon_mask_pixmap);
617     XtVaSetValues(dtb_ttsnoop_ttsnoop_win.ttsnoopWin,
618         XmNallowShellResize, True,
619         XmNtitle, "ttsnoop",
620         XmNiconMask, icon_mask_pixmap,
621         XmNiconPixmap, icon_pixmap,
622         XmNbackground, dtb_cvt_string_to_pixel(dtb_ttsnoop_ttsnoop_win.ttsnoopWin, "white"),
623         NULL);
624     
625     dtb_ttsnoop_ttsnoop_win_initialize(&(dtb_ttsnoop_ttsnoop_win), dtb_get_toplevel_widget());
626     
627     /*
628      * Map any initially-visible windows
629      */
630     
631     save_yourself_atom = XmInternAtom(XtDisplay(toplevel),
632         "WM_SAVE_YOURSELF", False);
633     
634     dtb_set_client_session_saveCB((DtbClientSessionSaveCB)NULL);
635     
636     XmAddWMProtocolCallback(toplevel, save_yourself_atom,
637         dtb_session_save, (XtPointer)NULL);
638     
639     
640     /**************************************************************************
641      *** DTB_USER_CODE_START
642      ***
643      ***      All initially-mapped widgets have been created, but not
644      *** realized. Set resources on widgets, or perform other operations
645      *** that must be completed before the toplevel widget is
646      *** realized.
647      ***/
648     
649     /*** DTB_USER_CODE_END
650      ***
651      *** End of user code section
652      ***
653      **************************************************************************/
654     
655
656     XtRealizeWidget(toplevel);
657
658     
659     /**************************************************************************
660      *** DTB_USER_CODE_START
661      ***
662      ***      The initially-mapped widgets have all been realized, and
663      *** the Xt main loop is about to be entered.
664      ***/
665
666     installSignalHandler();
667     if (snoopedArgsCount > 0) {
668             DtTtSetLabel( dtb_ttsnoop_ttsnoop_win.ttsnoopWin_label,
669                           tttraceCmd.str() );
670     }
671     Tt_status status;
672     snoopStream.open( snoopFile, ios::app );
673     std::ostrstream envStr;
674     envStr << "TT_TRACE_SCRIPT=> ";
675     envStr << traceFile << ends;
676     traceScript = envStr.str();
677     if (optImmediateTracing) {
678             turnOnTracing( 0, 0, 0 );
679     }
680     if (optImmediateXSession) {
681             char *display = getenv( "DISPLAY" );
682             optImmediateSession = tt_X_session( display );
683             status = tt_ptr_error( optImmediateSession );
684             if (tt_is_err( status )) {
685                     clog << "ttsnoop: tt_X_session( ";
686                     if (display == 0) {
687                             clog << "0";
688                     } else {
689                             clog << "\"" << display << "\"";
690                     }
691                     clog << " ) = " << (void *)optImmediateSession;
692                     clog << " (" << status << ")" << endl;
693                     exit( 4 );
694             }
695     }
696     if (optImmediateSession != 0) {
697             status = tt_default_session_set( optImmediateSession );
698             if (tt_is_err( status )) {
699                     clog << "ttsnoop: tt_default_session_set( \"";
700                     clog << optImmediateSession << "\" ) = ";
701                     clog << status << endl;
702                     exit( 4 );
703             }
704     }
705     if (optImmediateTtOpen) {
706             int fd;
707             char *procid = ttdt_open( &fd, "Ttsnoop", "CDE",
708                                       globalVersionString, 1 );
709             DtTtSetLabel( dtb_ttsnoop_ttsnoop_win.ttsnoopWin_label,
710                           "ttdt_open()", procid );
711             status = tt_ptr_error( procid );
712             if (tt_is_err( status )) {
713                     char *statmsg = tt_status_message(status);
714                     clog << "ttsnoop: ttdt_open() = ";
715                     clog << status << ":  " << statmsg << endl;
716                     exit( 4 );
717             }
718             XtInputId id = XtAppAddInput( app, fd, (XtPointer)XtInputReadMask,
719                                           tttk_Xt_input_handler, procid );
720             DtTtCreated( DTTT_PROCID, procid, (void *)id );
721             if (optImmediateSnooping) {
722                     Tt_pattern pat = tt_pattern_create();
723                     tt_pattern_category_set( pat, TT_OBSERVE );
724                     if (scopeFilesCount > 0) {
725                             tt_pattern_scope_add( pat, TT_BOTH );
726                             for (int i = 0; i < scopeFilesCount; i++) {
727                                     tt_pattern_file_add( pat, scopeFiles[i] );
728                             }
729                     } else {
730                             tt_pattern_scope_add( pat, TT_SESSION );
731                     }
732                     char *sess = tt_default_session();
733                     tt_pattern_session_add( pat, sess );
734                     tt_free( sess );
735                     for (int i = 0; i < opsCount; i++) {
736                             tt_pattern_op_add( pat, ops[i] );
737                     }
738                     for (int i = 0; i < sendersCount; i++) {
739                             tt_pattern_sender_add( pat, senders[i] );
740                     }
741                     if (vtype != 0) {
742                             tt_pattern_arg_add( pat, TT_MODE_UNDEFINED,
743                                                 vtype, 0 );
744                     }
745                     tt_pattern_callback_add( pat, justSnoopIt );
746                     status = tt_pattern_register( pat );
747                     DtTtSetLabel( dtb_ttsnoop_ttsnoop_win.ttsnoopWin_label,
748                                   "tt_pattern_register()", pat );
749                     if (tt_is_err( status )) {
750                             char *statmsg = tt_status_message(status);
751                             clog << "ttsnoop: tt_pattern_register() = ";
752                             clog << status << ":  " << statmsg << endl;
753                             exit( 4 );
754                     }
755                     DtTtCreated( DTTT_PATTERN, pat );
756                     snoopPat = pat;
757                     snoopPatIsRegistered = True;
758             }
759     }
760     if (snoopPat == 0) {
761             XtSetSensitive( dtb_ttsnoop_ttsnoop_win.
762                 menubar_Snoop_item_Snoop_menu_items.Off_item, False );
763     }
764     DtTtSetLabel( dtb_ttsnoop_ttsnoop_win.
765                   menubar_Snoop_item_Snoop_menu_items.Off_item,
766                   snoopPatIsRegistered ? "Off" : "On" );
767     if (optMapOnOutput) {
768             XtVaSetValues( dtb_ttsnoop_ttsnoop_win.ttsnoopPane,
769                            DtNmapOnOutput, optMapOnOutput, NULL );
770     }
771     installSignalHandler();
772     
773     /*** DTB_USER_CODE_END
774      ***
775      *** End of user code section
776      ***
777      **************************************************************************/
778     
779
780     
781     /*
782      * Enter event loop
783      */
784     XtAppMainLoop(app);
785     exit(0);
786 }
787
788
789 /**************************************************************************
790  *** DTB_USER_CODE_START
791  ***
792  *** All automatically-generated data and functions have been defined.
793  ***
794  *** Add new functions here, or at the top of the file.
795  ***/
796
797 /*** DTB_USER_CODE_END
798  ***
799  *** End of user code section
800  ***
801  **************************************************************************/
802
803