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