Application.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Id$
00004 
00005 */
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <unistd.h>
00010 #include <errno.h>
00011 #include <sys/resource.h>
00012 
00013 #include "Error.h"
00014 #include "Core.h"
00015 #include "BuildInfo.h"
00016 
00017 #include "Application.h"
00018 
00019 String Application::_name;
00020 String Application::_version;
00021 
00022 // Public methods.
00023 
00024 int Application::init(const String &name, const String &version, int argc, char **argv)
00025 {
00026   int res;
00027 
00028   _name = name;
00029   _version = version;
00030 
00031   errno = 0;
00032 
00033   signal(SIGPIPE, SIG_IGN);
00034 
00035   initExceptionsHandling();
00036 
00037   if (argc > 0 && argv)
00038     if (FAILED(res = parseCommandLine(argc, argv)))
00039       return ERROR_BACKTRACE(res);
00040 
00041   if (FAILED(res = Core::init()))
00042     return ERROR_BACKTRACE(res);
00043 
00044   return OK;
00045 }
00046 
00047 // Private methods.
00048 
00049 void Application::version()
00050 {
00051   printf("\n%s. Version %s (Built on %s).\n", CSTRING(_name), CSTRING(_version), BUILD_DATE_TIME);
00052   printf("Written by Cool Software Developers Group <pylon.platform@gmail.com>.\n\n");
00053 }
00054 
00055 void Application::help(const char *fileName)
00056 {
00057   version();
00058   printf("USAGE: %s [-hv]\n", fileName);
00059   printf("\t-h\t- show this help;\n");
00060   printf("\t-v\t- show the version information.\n\n");
00061 }
00062 
00063 int Application::parseCommandLine(int argc, char **argv)
00064 {
00065   int opt;
00066 
00067   while ((opt = getopt(argc, argv, "hv")) != EOF) {
00068     switch (opt) {
00069       case ':' :
00070         return ERROR(MSG_COMMAND_LINE_MISSING_OPTION_PARAMETER);
00071         exit(FAILURE);
00072         break;
00073       case '?' :
00074         return ERROR(MSG_COMMAND_LINE_UNKNOWN_OPTION);
00075         break;
00076       case 'v' :
00077         version();
00078         exit(OK);
00079         break;
00080       case 'h' :
00081         help(argv[0]);
00082         exit(OK);
00083         break;
00084     }
00085   }
00086 
00087   return OK;
00088 }
00089 
00090 void Application::initExceptionsHandling()
00091 {
00092   struct sigaction sa;
00093 
00094   bzero(&sa, sizeof(sa));
00095 
00096   sa.sa_flags = SA_SIGINFO | SA_NOMASK;
00097   sa.sa_sigaction = Application::exceptionHandler;
00098 
00099   sigaction(SIGFPE, &sa, 0);
00100   sigaction(SIGSEGV, &sa, 0);
00101   sigaction(SIGABRT, &sa, 0);
00102 }
00103 
00104 void Application::exceptionHandler(int, siginfo_t *info, void *)
00105 {
00106   if (!info)
00107     return;
00108   fprintf(stderr, "ERROR: Fatal exception %d at address 0x%x (%s).\n", info->si_signo, info->si_addr, strerror(info->si_errno));
00109   switch (info->si_signo) {
00110     case SIGFPE:
00111       switch (info->si_code) {
00112         case FPE_INTDIV:
00113           fprintf(stderr, "HINT: Integer divide by zero.\n");
00114           break;
00115         case FPE_INTOVF:
00116           fprintf(stderr, "HINT: Integer overflow.\n");
00117           break;
00118         case FPE_FLTDIV:
00119           fprintf(stderr, "HINT: Floating point divide by zero.\n");
00120           break;
00121         case FPE_FLTOVF:
00122           fprintf(stderr, "HINT: Floating point overflow.\n");
00123           break;
00124         case FPE_FLTUND:
00125           fprintf(stderr, "HINT: Floating point underflow.\n");
00126           break;
00127         case FPE_FLTRES:
00128           fprintf(stderr, "HINT: Floating point inexact result.\n");
00129           break;
00130         case FPE_FLTINV:
00131           fprintf(stderr, "HINT: Floating point invalid operation.\n");
00132           break;
00133         case FPE_FLTSUB:
00134           fprintf(stderr, "HINT: Subscript out of range.\n");
00135           break;
00136         default:
00137           fprintf(stderr, "HINT: Unknown SIGFPE signal code.\n");
00138           break;
00139       }
00140       break;
00141     case SIGSEGV:
00142       switch (info->si_code) {
00143         case SEGV_MAPERR:
00144           fprintf(stderr, "HINT: Address not mapped to object.\n");
00145           break;
00146         case SEGV_ACCERR:
00147           fprintf(stderr, "HINT: Invalid permissions for mapped object.\n");
00148           break;
00149         default:
00150           fprintf(stderr, "HINT: Unknown SIGSEGV signal code.\n");
00151           break;
00152       }
00153       break;
00154     default:
00155       fprintf(stderr, "HINT: There is no hint for this signal.\n");
00156       break;
00157   }
00158   BACKTRACE;
00159   exit(-1);
00160 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1