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
//-------------------------------------------------------------------------
// Filename      : AppUtil.cc
//
// Purpose       : This file represents the Cubit application itself.
//
// Special Notes :
//
// Creator       : Darryl Melander
//
// Date          : 06/08/98
//
// Owner         : Darryl Melander
//-------------------------------------------------------------------------

#include <cstdio>
#include <cstdlib>
#include <signal.h>
#ifndef _WIN32
#  include <unistd.h>
#  include <termios.h>
#  include <sys/ioctl.h>
#endif
#include <ctype.h>
#include <time.h>

#include "AppUtil.hpp"
#include "CubitMessage.hpp"
#include "CubitString.hpp"
#include "CubitObserver.hpp"
#include "SettingHandler.hpp"
#include "StubProgressTool.hpp"

#ifdef BUILD_WITH_CONCURRENT_SUPPORT
#include "CubitQtConcurrentApi.h"
#endif

#ifdef CUBIT_USE_THREADS
#include "CubitThreadPool.hpp"
#endif

// Different platforms follow different conventions
#ifndef _WIN32
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#ifdef SOLARIS
#include <procfs.h>
#include <sys/syscall.h>
extern "C" int getrusage(int, struct rusage *);
#ifndef RUSAGE_SELF
#include </usr/ucbinclude/sys/rusage.h>
#endif
#endif

#ifdef HP
#include <sys/syscall.h>
#if 0
#if OS_VERSION != 1020
#define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
#endif
#endif
#endif

#ifndef PATH_MAX
  #define PATH_MAX _MAX_PATH
#endif

#ifdef _WIN32
#include <windows.h>
extern "C" IMAGE_DOS_HEADER __ImageBase;
#else
#include <dlfcn.h>
#endif

AppUtil* AppUtil::instance_ = NULL;

#ifdef HP
#ifndef UNIX_PORT
extern "C" int Error;
#else
int Error = 0;
#endif //unix_port
#endif
#ifdef _WIN32
#  include <direct.h>
#  include <windows.h>
//#  define strdup _strdup
#endif
#include "CubitUtil.hpp"

// Interrupt handling defintions.
/*  Setting this flag (asynchronously) to CUBIT_TRUE 
 *  will cause Cubit/CGM to attempt to abort any current
 *  operations and return.  
 * 
 * NOTE: IT IS THE RESPONSIBILITY OF THE APPLICATION 
 *       USING CGM TO RESET THIS FLAG TO CUBIT_FALSE!!!
 *       For Cubit, this flag is reset in UserInterface.
 *       
 *  CubitApp provides a default signal hander that will
 *  set this flag to CUBIT_TRUE whenever a SIGINT (^C)
 *  is detected.  It is still the responsibility of the
 *  application using CGM to reset the flag to false!
 *  The signal hander provided by CubitApp may be set by 
 *  calling CubitApp::instance()->catch_interrupt(CUBIT_TRUE).
 *
 *  The storage space for this flag is defined in CubitApp.cpp
 *  and initialized in CubitApp::initialize().
 */    
static volatile CubitBoolean cubit_intr = CUBIT_FALSE;

extern "C" void cubit_update_terminal_size(int)
{
#if defined(_WIN32)

  // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getconsolescreenbufferinfo.asp
  // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/scrolling_a_screen_buffer_s_window.asp
  HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_SCREEN_BUFFER_INFO scr_info;
  if (GetConsoleScreenBufferInfo(h_stdout, &scr_info))
  {
	  AppUtil::instance()->set_terminal_size(
        // dwSize.Y is will return the height of the scroll-back buffer.
        // srWindow contains the position of the visible rect of the
        // window in the buffer.  That is the height of the window.
      scr_info.srWindow.Bottom - scr_info.srWindow.Top,
        // use width of scroll buffer rather than size of window
        // for number of columns.  The window size is useless as
        // caller would need to know the offset in the X direction
        // to make use of it and we aren't returning that.  Besides,
        // the user presumably set the scroll buffer to this width
        // because that's the line length (s)he wants.
      scr_info.dwSize.X
    );
  }

#elif defined(TIOCGWINSZ)

    // On UNIX platforms, register this function as a handler for
    // SIGWINCH.  The system will then call this function any time
    // the user changes the size of the terminal window.
#ifdef SIGWINCH
  signal( SIGWINCH, &cubit_update_terminal_size );<--- Skipping configuration 'SIGWINCH;TIOCGWINSZ' since the value of 'SIGWINCH' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif

  const int file = fileno(stdout);
  struct winsize size;
  if( ioctl( file, TIOCGWINSZ, (char*)&size ) == 0 )<--- Skipping configuration 'SIGWINCH;TIOCGWINSZ' since the value of 'TIOCGWINSZ' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'TIOCGWINSZ' since the value of 'TIOCGWINSZ' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
  {
    AppUtil::instance()->set_terminal_size( size.ws_row, size.ws_col );
  }

#endif
}


CubitBoolean AppUtil::catching_sigint_ = CUBIT_FALSE;

AppUtil* AppUtil::instance_no_create()
{
  return instance_;
}

// Returns the singleton instance of the app object.
// If it is being created for the first time, it
// does application initialization.
AppUtil* AppUtil::instance()
{
   if (instance_ == NULL)
   {
      instance_ = new AppUtil();

      if ( !instance_ )
      {
         PRINT_ERROR(" *** Unable to initialize application ***\n");
         exit(1);
      }

        // Initialize interrupted flag to false.
      cubit_intr = CUBIT_FALSE;

      cubit_update_terminal_size(0);

      initialize_settings();
   }
   return instance_;
}

AppUtil::AppUtil()
{
    term_width = 0;
    term_height = 0;
    mAppStarted = CUBIT_FALSE;

      // reset catching_sigint_ here, even though it's static, 'cuz it used
      // to be reset to false on construction before going static

    catching_sigint_ = CUBIT_FALSE;

    // create a default progress tool that does nothing
    // an app can replace this with an app specific progress tool
    mProgressTool = new StubProgressTool();

    concurrentSupport = false;

    this->mInterruptThrottle = clock();

#ifdef CUBIT_USE_THREADS
    mThreadPool = new CubitThreadPool;
#else
    mThreadPool = NULL;
#endif
}

AppUtil::~AppUtil()
{
  instance_ = NULL;
  catch_interrupt( CUBIT_FALSE );
  if (mProgressTool)
      delete mProgressTool;
  mProgressTool = NULL;
#ifdef CUBIT_USE_THREADS
  delete mThreadPool;
#endif
}

void AppUtil::delete_instance()
{
  if( NULL != instance_ )
  {
    delete instance_;
    instance_ = NULL;
  }
}    

#ifdef CAT
// Returns how many days until the app expires.  A negative
// number inicates it is already expired.  If there is
// an extension, has_extension is set to CUBIT_TRUE.
int AppUtil::days_to_expire(int year, int month, int day ) const
{
     // Setup variables and constants
   struct tm expiration_time;
   time_t raw_expire;
   time_t raw_current;
   int days;
   const int SECONDS_PER_DAY = 24 * 60 * 60;

     // Get the current time, raw
   time(&raw_current);

     // Setup a dissected expiration date
   expiration_time.tm_year  = year - 1900;
   expiration_time.tm_mon   = month - 1;
   expiration_time.tm_mday  = day;
   expiration_time.tm_hour  = 0;
   expiration_time.tm_min   = 0;
   expiration_time.tm_sec   = 0;
   expiration_time.tm_isdst = 0;

     // Convert expiration date from dissected to raw
   raw_expire = mktime(&expiration_time);
     // Get difference between dates
   days = (int)difftime(raw_expire, raw_current) / SECONDS_PER_DAY;
   //days = INT_MAX;//(int)difftime(raw_expire, raw_current) / SECONDS_PER_DAY;
//changed back by aga@cat|11/2/04

     // Return days left
   return days;
}
#endif

// Prints out information about the session, like
// execution time and memory problems.
void AppUtil::report_resource_usage() const
{

#ifndef JANUS
#ifndef _WIN32
   struct rusage r_usage;
   float utime, stime;
   apputil_getrusage(r_usage);
   utime = (float)r_usage.ru_utime.tv_sec +
      ((float)r_usage.ru_utime.tv_usec/1.e6);
   stime = (float)r_usage.ru_stime.tv_sec +
      ((float)r_usage.ru_stime.tv_usec/1.e6);
   static int pagesize = getpagesize();
   PRINT_INFO("\nEnd of Execution\n");
   PRINT_INFO("User time             = %f\n", utime);
   PRINT_INFO("System time           = %f\n", stime);
   PRINT_INFO("Total time            = %f\n", utime+stime);
   PRINT_INFO("Max resident set size = %ld bytes\n", r_usage.ru_maxrss*pagesize);
   PRINT_INFO("Int resident set size = %ld\n", r_usage.ru_idrss);
   PRINT_INFO("Minor Page Faults     = %ld\n", r_usage.ru_minflt);
   PRINT_INFO("Major Page Faults     = %ld\n", r_usage.ru_majflt);
   PRINT_INFO("Swaps                 = %ld\n", r_usage.ru_nswap);
#endif// _WIN32
#endif//JANUS
}



// Default handler for SIGINT provided by AppUtil.
extern "C" void sigint_handler(int)
{
#ifndef CUBIT_NO_SIGNAL
  if( signal( SIGINT, sigint_handler ) == SIG_ERR )
    PRINT_ERROR("Cannot continue to catch SIGINT!\n");
  cubit_intr = CUBIT_TRUE;
#endif
}

// Set signal handler for SIGINT.
void AppUtil::catch_interrupt( CubitBoolean yesno )
{
#ifndef CUBIT_NO_SIGNAL
  if( yesno )
  {
    if( signal( SIGINT, sigint_handler ) == SIG_ERR )
      PRINT_ERROR("Can't catch SIGINT!\n");
    else
      catching_sigint_ = CUBIT_TRUE;
  }
  else
  {
    if( signal( SIGINT, SIG_DFL ) == SIG_ERR )
      PRINT_ERROR("Can't reset SIGINT handler!\n");
    else
      catching_sigint_ = CUBIT_FALSE;
  }
#endif
}


void AppUtil::startup()
{
#ifndef NOT_AN_APP
    if (mAppStarted)
        return;
   
    // get paths to binaries that AppUtil is compiled into
    // we'll make this our "cubitDir" and can be used a reference for 
    // where Cubit is installed
#ifdef _WIN32
    wchar_t path_buffer[PATH_MAX];
    if(GetModuleFileNameW((HINSTANCE)&__ImageBase, path_buffer, PATH_MAX))
    {
      *wcsrchr(path_buffer, '\\') = '\0';
      cubitDir = CubitString::toUtf8(path_buffer);
    }
#else
    Dl_info dl_info;

    // Note: this is a nasty hack to avoid the warning:
    // ISO C++ forbids casting between pointer-to-function and pointer-to-object
    // Even with reinterpret_cast, this warning is still generated
    // GCC accepts reinterpret_cast for purposes like in dlsym calls as an extension,
    // but that might not work for other compilers
    // This union-trick has been applied to some open source code, like:
    // https://mail.gnome.org/archives/commits-list/2011-February/msg06409.html
    // http://sourceforge.net/p/scstudio/mailman/message/29083334/
    union {
      void* ptr;
      AppUtil* (*func)();
    } cast_u;
    cast_u.func = AppUtil::instance;

    dladdr(cast_u.ptr, &dl_info);
    CubitString path_buffer = dl_info.dli_fname;
    size_t idx = path_buffer.find_last('/');
    if(idx != CubitString::npos)
    {
      path_buffer = path_buffer.substr(0, idx);
    }
    cubitDir = path_buffer;
#endif

#ifdef BUILD_WITH_CONCURRENT_SUPPORT
    mConcurrentInstance = new CubitQtConcurrent;
    this->concurrentSupport = true;
#endif

    mAppStarted = CUBIT_TRUE;
    // add startup code here
    // nothing to do for now
#endif /*NOT_AN_APP*/
    return;
}

int AppUtil::shutdown()
{
    // When the user is done, print out additional info if requested
   if (DEBUG_FLAG(3))
      report_resource_usage();

     // Return the number of errors in the session
   int ret_val = ( CubitMessage::instance()->error_count() );
   if ( ret_val > 0 )
   {
     PRINT_ERROR("Errors found during CUBIT session.\n");
   }

     // Delete the singletons
   CubitMessage::delete_instance();

   SettingHandler::delete_instance();

#ifdef BUILD_WITH_CONCURRENT_SUPPORT
   delete mConcurrentInstance;
#endif

   mAppStarted = CUBIT_FALSE;

   return ret_val;
}
void AppUtil::apputil_getrusage(struct rusage &r_usage) const
{
    // get the resource usage as defined by getrusage
#ifndef JANUS
#ifndef _WIN32
   getrusage(RUSAGE_SELF, &r_usage);

   if (r_usage.ru_maxrss == 0) {
       // this machine doesn't return rss - try going to /proc
       // print the file name to open
#ifdef SOLARIS
     char buffer[120];
     strcpy(buffer, "/proc/self/psinfo");
     int file_ptr = -1;
     file_ptr = open(buffer, O_RDONLY);
     if (file_ptr < 0) return;
     struct psinfo myps_info;
     read(file_ptr, &myps_info, sizeof(myps_info));
     static int page_size = getpagesize();
     r_usage.ru_maxrss = myps_info.pr_rssize*1024/page_size;
     r_usage.ru_idrss = myps_info.pr_size*1024/page_size;
     close (file_ptr);
#endif // SOLARIS
#ifdef CUBIT_LINUX
     char file_str[4096], dum_str[4096];
     int file_ptr = -1, file_len;
     file_ptr = open("/proc/self/stat", O_RDONLY);
     file_len = read(file_ptr, file_str, sizeof(file_str)-1);
     if (file_len == 0) return;
     close(file_ptr);
     file_str[file_len] = '\0';
       // read the preceeding fields and the ones we really want...
     int dum_int;
     unsigned int dum_uint, vm_size, rss;
     static int page_size = getpagesize();
     int num_fields = sscanf(file_str,
                             "%d " // pid
                             "%s " // comm
                             "%c " // state
                             "%d %d %d %d %d " // ppid, pgrp, session, tty, tpgid
                             "%u %u %u %u %u " // flags, minflt, cminflt, majflt, cmajflt
                             "%d %d %d %d %d %d " // utime, stime, cutime, cstime, counter, priority
                             "%u %u " // timeout, itrealvalue
                             "%d " // starttime
                             "%u %u", // vsize, rss
                             &dum_int,
                             dum_str,
                             dum_str,
                             &dum_int, &dum_int, &dum_int, &dum_int, &dum_int,
                             &dum_uint, &dum_uint, &dum_uint, &dum_uint, &dum_uint,
                             &dum_int, &dum_int, &dum_int, &dum_int, &dum_int, &dum_int,
                             &dum_uint, &dum_uint,
                             &dum_int,
                             &vm_size, &rss);
     if (num_fields == 24) {
       r_usage.ru_maxrss = rss/page_size;
       r_usage.ru_idrss = vm_size/page_size;
     }
#endif // CUBIT_LINUX
   }
#endif // _WIN32
#endif // JANUS
}

bool AppUtil::get_terminal_size( int& rows, int& cols )
{
  cols = term_width;
  rows = term_height;
  return rows || cols;
}

void AppUtil::set_terminal_size( int rows, int cols )
{
  term_width = cols;
  term_height = rows;
}

ProgressTool *AppUtil::progress_tool()
{
  assert(mProgressTool != NULL);

  return mProgressTool;
}

void AppUtil::progress_tool(ProgressTool* pTool)
{
  assert(pTool != NULL);  // existence is assumed elsewhere in the code
  if (pTool)
  {
      if (mProgressTool)
      {
          delete mProgressTool;
      }
      mProgressTool = pTool;
  }
}

CubitThreadPool* AppUtil::thread_pool()
{
  return mThreadPool;
}

void AppUtil::initialize_settings()
{
  CubitMessage::initialize_settings();
}

CubitBoolean AppUtil::interrupt()
{
  if(this->mProgressTool)
  {
    clock_t new_clock = clock();
    clock_t diff = new_clock - this->mInterruptThrottle;
    if((diff / static_cast<double>(CLOCKS_PER_SEC)) > 0.1)
    {
      this->mInterruptThrottle = new_clock;
      mProgressTool->check_interrupt();
    }
  }

  return cubit_intr;
}

void AppUtil::set_interrupt(CubitBoolean f)
{
  if(f)
  {
    // call raise so it works regardless of the current signal handler
    raise(SIGINT);
  }
  cubit_intr = f;
}

void AppUtil::clear_interrupt()
{
  cubit_intr = CUBIT_FALSE;
}

void AppUtil::send_event(CubitObservable* observable, const CubitEvent& event)
{
    this->mEventDispatcher.send_event(observable, event);
}