WizardsToolkit  1.0.7
wizard.c
Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                  W   W  IIIII  ZZZZZ   AAA   RRRR   DDDD                    %
00007 %                  W   W    I       ZZ  A   A  R   R  D  KD                   %
00008 %                  W W W    I     ZZZ   AAAAA  RRRR   D   D                   %
00009 %                  WW WW    I    ZZ     A   A  R R    D   D                   %
00010 %                  W   W  IIIII  ZZZZZ  A   A  R   R  DDDD                    %
00011 %                                                                             %
00012 %                         Wizard's Toolkit Wizard Methods                     %
00013 %                                                                             %
00014 %                             Software Design                                 %
00015 %                               John Cristy                                   %
00016 %                               March 2003                                    %
00017 %                                                                             %
00018 %                                                                             %
00019 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
00020 %  dedicated to making software imaging solutions freely available.           %
00021 %                                                                             %
00022 %  You may not use this file except in compliance with the License.  You may  %
00023 %  obtain a copy of the License at                                            %
00024 %                                                                             %
00025 %    http://www.wizards-toolkit.org/script/license.php                        %
00026 %                                                                             %
00027 %  Unless required by applicable law or agreed to in writing, software        %
00028 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00029 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00030 %  See the License for the specific language governing permissions and        %
00031 %  limitations under the License.                                             %
00032 %                                                                             %
00033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00034 %
00035 %
00036 */
00037 
00038 /*
00039   Include declarations.
00040 */
00041 #include "wizard/studio.h"
00042 #include "wizard/client.h"
00043 #include "wizard/configure.h"
00044 #include "wizard/hash.h"
00045 #include "wizard/log.h"
00046 #include "wizard/magick.h"
00047 #include "wizard/memory_.h"
00048 #include "wizard/mime.h"
00049 #include "wizard/random_.h"
00050 #include "wizard/resource_.h"
00051 #include "wizard/semaphore.h"
00052 #include "wizard/semaphore-private.h"
00053 #include "wizard/string_.h"
00054 #include "wizard/utility.h"
00055 #include "wizard/wizard.h"
00056 
00057 /*
00058   Define declarations.
00059 */
00060 #if !defined(WIZARDSTOOLKIT_RETSIGTYPE)
00061 # define WIZARDSTOOLKIT_RETSIGTYPE  void
00062 #endif
00063 #if !defined(SIG_DFL)
00064 # define SIG_DFL  ((SignalHandler *) 0)
00065 #endif
00066 #if !defined(SIG_ERR)
00067 # define SIG_ERR  ((SignalHandler *) -1)
00068 #endif
00069 #if !defined(SIGMAX)
00070 #define SIGMAX  64
00071 #endif
00072 
00073 /*
00074   Typedef declarations.
00075 */
00076 typedef WIZARDSTOOLKIT_RETSIGTYPE
00077   SignalHandler(int);
00078 
00079 /*
00080   Global declarations.
00081 */
00082 static SignalHandler
00083   *signal_handlers[SIGMAX] = { (SignalHandler *) NULL };
00084 
00085 static volatile WizardBooleanType
00086   instantiate_wizard = WizardFalse,
00087   instantiate_wizardstoolkit = WizardFalse;
00088 
00089 /*
00090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00091 %                                                                             %
00092 %                                                                             %
00093 %                                                                             %
00094 %   W i z a r d s T o o l k i t G e n e s i s                                 %
00095 %                                                                             %
00096 %                                                                             %
00097 %                                                                             %
00098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00099 %
00100 %  WizardsToolkitGenesis() initializes the Wizard's Toolkit environment.
00101 %
00102 %  The format of the WizardsToolkitGenesis function is:
00103 %
00104 %      WizardsToolkitGenesis(const char *path)
00105 %
00106 %  A description of each parameter follows:
00107 %
00108 %    o path: The execution path of the current Wizard's Toolkit client.
00109 %
00110 */
00111 
00112 static void WizardExitHandler(void)
00113 {
00114   WizardsToolkitTerminus();
00115 }
00116 
00117 static SignalHandler *SetWizardSignalHandler(int signal_number,
00118   SignalHandler *handler)
00119 {
00120 #if defined(WIZARDSTOOLKIT_HAVE_SIGACTION) && defined(WIZARDSTOOLKIT_HAVE_SIGEMPTYSET)
00121   int
00122     status;
00123 
00124   sigset_t
00125     mask;
00126 
00127   struct sigaction
00128     action,
00129     previous_action;
00130 
00131   sigemptyset(&mask);
00132   sigaddset(&mask,signal_number);
00133   sigprocmask(SIG_BLOCK,&mask,NULL);
00134   action.sa_mask=mask;
00135   action.sa_handler=handler;
00136   action.sa_flags=0;
00137 #if defined(SA_INTERRUPT)
00138   action.sa_flags|=SA_INTERRUPT;
00139 #endif
00140   status=sigaction(signal_number,&action,&previous_action);
00141   if (status < 0)
00142     return(SIG_ERR);
00143   sigprocmask(SIG_UNBLOCK,&mask,NULL);
00144   return(previous_action.sa_handler);
00145 #else
00146   return(signal(signal_number,handler));
00147 #endif
00148 }
00149 
00150 static void WizardSignalHandler(int signal_number)
00151 {
00152 #if !defined(WIZARDSTOOLKIT_HAVE_SIGACTION)
00153   (void) signal(signal_number,SIG_IGN);
00154 #endif
00155   AsynchronousResourceComponentTerminus();
00156   instantiate_wizard=WizardFalse;
00157   (void) SetWizardSignalHandler(signal_number,signal_handlers[signal_number]);
00158 #if defined(WIZARDSTOOLKIT_HAVE_RAISE)
00159   if (signal_handlers[signal_number] != WizardSignalHandler)
00160     raise(signal_number);
00161 #endif
00162 #if !defined(WIZARDSTOOLKIT_HAVE__EXIT)
00163   exit(signal_number);
00164 #else
00165 #if defined(SIGHUP)
00166   if (signal_number == SIGHUP)
00167     exit(signal_number);
00168 #endif
00169 #if defined(SIGBREAK)
00170   if (signal_number == SIGBREAK)
00171     exit(signal_number);
00172 #endif
00173 #if defined(SIGINT) && !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00174   if (signal_number == SIGINT)
00175     exit(signal_number);
00176 #endif
00177 #if defined(SIGTERM)
00178   if (signal_number == SIGTERM)
00179     exit(signal_number);
00180 #endif
00181 #if defined(SIGSEGV)
00182   if (signal_number == SIGSEGV)
00183     exit(signal_number);
00184 #endif
00185   _exit(signal_number);  /* do not invoke registered atexit() methods */
00186 #endif
00187 }
00188 
00189 static SignalHandler *RegisterWizardSignalHandler(int signal_number)
00190 {
00191   SignalHandler
00192     *handler;
00193 
00194   handler=SetWizardSignalHandler(signal_number,WizardSignalHandler);
00195   if (handler == SIG_ERR)
00196     return(handler);
00197   if (handler != SIG_DFL)
00198     handler=SetWizardSignalHandler(signal_number,handler);
00199   else
00200     (void) LogWizardEvent(ConfigureEvent,GetWizardModule(),
00201       "Register handler for signal: %d",signal_number);
00202   return(handler);
00203 }
00204 
00205 WizardExport void WizardsToolkitGenesis(const char *path)
00206 {
00207   char
00208     *events,
00209     execution_path[MaxTextExtent],
00210     filename[MaxTextExtent];
00211 
00212   /*
00213     Initialize the Wizard environment.
00214   */
00215   LockWizardMutex();
00216   if (instantiate_wizardstoolkit != WizardFalse)
00217     {
00218       UnlockWizardMutex();
00219       return;
00220     }
00221   (void) setlocale(LC_ALL,"");
00222   (void) setlocale(LC_NUMERIC,"C");
00223   (void) SemaphoreComponentGenesis();
00224   (void) LogComponentGenesis();
00225   (void) RandomComponentGenesis();
00226   events=GetEnvironmentValue("WIZARD_DEBUG");
00227   if (events != (char *) NULL)
00228     {
00229       (void) SetLogEventMask(events);
00230       events=(char *) RelinquishWizardMemory(events);
00231     }
00232 #if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00233 #if defined(_DEBUG) && !defined(__BORLANDC__)
00234   if (IsEventLogging() != WizardFalse)
00235     {
00236       int
00237         debug;
00238 
00239       debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
00240       debug|=_CRTDBG_CHECK_ALWAYS_DF |_CRTDBG_DELAY_FREE_MEM_DF |
00241         _CRTDBG_LEAK_CHECK_DF;
00242       if (0)
00243         {
00244           debug=_CrtSetDbgFlag(debug);
00245           _ASSERTE(_CrtCheckMemory());
00246         }
00247     }
00248 #endif
00249 #endif
00250   /*
00251     Set client name and execution path.
00252   */
00253   (void) GetExecutionPath(execution_path,MaxTextExtent);
00254   if ((path != (const char *) NULL) && (*path != '\0'))
00255     (void) CopyWizardString(execution_path,path,MaxTextExtent);
00256   GetPathComponent(execution_path,TailPath,filename);
00257   (void) SetClientName(filename);
00258   GetPathComponent(execution_path,HeadPath,execution_path);
00259   (void) SetClientPath(execution_path);
00260   /*
00261     Set signal handlers.
00262   */
00263 #if defined(SIGABRT)
00264   if (signal_handlers[SIGABRT] == (SignalHandler *) NULL)
00265     signal_handlers[SIGABRT]=RegisterWizardSignalHandler(SIGABRT);
00266 #endif
00267 #if defined(SIGBREAK)
00268   if (signal_handlers[SIGBREAK] == (SignalHandler *) NULL)
00269     signal_handlers[SIGBREAK]=RegisterWizardSignalHandler(SIGBREAK);
00270 #endif
00271 #if defined(SIGFPE)
00272   if (signal_handlers[SIGFPE] == (SignalHandler *) NULL)
00273     signal_handlers[SIGFPE]=RegisterWizardSignalHandler(SIGFPE);
00274 #endif
00275 #if defined(SIGHUP)
00276   if (signal_handlers[SIGHUP] == (SignalHandler *) NULL)
00277     signal_handlers[SIGHUP]=RegisterWizardSignalHandler(SIGHUP);
00278 #endif
00279 #if defined(SIGINT) && !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
00280   if (signal_handlers[SIGINT] == (SignalHandler *) NULL)
00281     signal_handlers[SIGINT]=RegisterWizardSignalHandler(SIGINT);
00282 #endif
00283 #if defined(SIGQUIT)
00284   if (signal_handlers[SIGQUIT] == (SignalHandler *) NULL)
00285     signal_handlers[SIGQUIT]=RegisterWizardSignalHandler(SIGQUIT);
00286 #endif
00287 #if defined(SIGTERM)
00288   if (signal_handlers[SIGTERM] == (SignalHandler *) NULL)
00289     signal_handlers[SIGTERM]=RegisterWizardSignalHandler(SIGTERM);
00290 #endif
00291 #if defined(SIGSEGV)
00292   if (signal_handlers[SIGSEGV] == (SignalHandler *) NULL)
00293     signal_handlers[SIGSEGV]=RegisterWizardSignalHandler(SIGSEGV);
00294 #endif
00295 #if defined(SIGXCPU)
00296   if (signal_handlers[SIGXCPU] == (SignalHandler *) NULL)
00297     signal_handlers[SIGXCPU]=RegisterWizardSignalHandler(SIGXCPU);
00298 #endif
00299 #if defined(SIGXFSZ)
00300   if (signal_handlers[SIGXFSZ] == (SignalHandler *) NULL)
00301     signal_handlers[SIGXFSZ]=RegisterWizardSignalHandler(SIGXFSZ);
00302 #endif
00303   /*
00304     Initialize wizard resources.
00305   */
00306   (void) ConfigureComponentGenesis();
00307   (void) ResourceComponentGenesis();
00308   (void) MimeComponentGenesis();
00309   instantiate_wizardstoolkit=WizardTrue;
00310   UnlockWizardMutex();
00311 }
00312 
00313 /*
00314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00315 %                                                                             %
00316 %                                                                             %
00317 %                                                                             %
00318 %   W i z a r d s T o o l k i t T e r m i n u s                               %
00319 %                                                                             %
00320 %                                                                             %
00321 %                                                                             %
00322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00323 %
00324 %  WizardsToolkitTerminus() destroys the Wizard's Toolkit environment.
00325 %
00326 %  The format of the WizardsToolkitTerminus method is:
00327 %
00328 %      WizardsToolkitTerminus(void)
00329 %
00330 */
00331 WizardExport void WizardsToolkitTerminus(void)
00332 {
00333   LockWizardMutex();
00334   if (instantiate_wizardstoolkit == WizardFalse)
00335     {
00336       UnlockWizardMutex();
00337       return;
00338     }
00339   MimeComponentTerminus();
00340   ResourceComponentTerminus();
00341   RandomComponentTerminus();
00342   LogComponentTerminus();
00343   SemaphoreComponentTerminus();
00344   instantiate_wizardstoolkit=WizardFalse;
00345   UnlockWizardMutex();
00346 }