unix/SystemInterpreter.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2012 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /*****************************************************************************/
39 /* REXX Windows Support */
40 /* */
41 /* Main Windows interpreter control. This is the preferred location for */
42 /* all platform dependent global variables. */
43 /* The interpreter does not instantiate an instance of this */
44 /* class, so most variables and methods should be static. */
45 /* */
46 /* */
47 /*****************************************************************************/
48 
49 #include <stdio.h>
50 #include <termios.h>
51 #include "RexxCore.h"
52 #include "SystemInterpreter.hpp"
53 #include "Interpreter.hpp"
54 
57 
59 
61 {
62  // now do the platform independent startup
64 }
65 
66 
68 {
69  // now do the platform independent shutdown
71 }
72 
73 #define ORXAP_DEBUG
74 void signalHandler(int sig)
75 {
76 
77 #ifdef ORXAP_DEBUG
78  switch (sig)
79  {
80  case (SIGINT):
81  printf("\n*** Rexx interrupted.\n");
82  break;
83  case (SIGTERM):
84  printf("\n*** Rexx terminated.\n*** Closing Rexx !\n"); /* exit(0); */
85  break;
86  case (SIGSEGV):
87  printf("\n*** Segmentation fault.\n*** Closing Rexx !\n");
88  break;
89  case (SIGFPE):
90  printf("\n*** Floating point error.\n*** Closing Rexx\n");
91  break;
92  case (SIGBUS):
93  printf("\n*** Bus error.\n*** Closing Rexx\n");
94  break;
95  case (SIGPIPE):
96  printf("\n*** Broken pipe.\n*** Closing Rexx\n");
97  break;
98  default:
99  printf("\n*** Error,closing REXX !\n");
100  break;
101  }
102 #endif
103 
104  // if the signal is a ctrl-C, we perform a halt operation
105  if (sig == SIGINT)
106  {
107  Interpreter::haltAllActivities(OREF_SIGINT_STRING);
108  return;
109  }
110  else if (sig == SIGTERM)
111  {
112  Interpreter::haltAllActivities(OREF_SIGTERM_STRING);
113  return;
114  }
115  else if (sig == SIGHUP)
116  {
117  Interpreter::haltAllActivities(OREF_SIGHUP_STRING);
118  return;
119  }
120  else
121  {
122  exit(0);
123  }
124 }
125 
126 
128 {
129 
130  /* Set the cleanup handler for unconditional process termination */
131  struct sigaction new_action;
132  struct sigaction old_action;
133 
134  /* Set up the structure to specify the new action */
135  new_action.sa_handler = signalHandler;
136  old_action.sa_handler = NULL;
137  sigfillset(&new_action.sa_mask);
138 // new_action.sa_flags = SA_RESTART;
139  new_action.sa_flags = 0; // do not use SA_RESTART or ctrl-c will not work as expected!
140 
141 /* Termination signals are set by Object REXX whenever the signals were not set */
142 /* from outside (calling C-routine). The SIGSEGV signal is not set any more, so */
143 /* that we now get a coredump instead of a hang up */
144 
145  sigaction(SIGINT, NULL, &old_action);
146  sigaction(SIGTERM, NULL, &old_action);
147  sigaction(SIGHUP, NULL, &old_action);
148  if (old_action.sa_handler == NULL) /* not set by ext. exit handler*/
149  {
150  sigaction(SIGINT, &new_action, NULL); /* exitClear on SIGINT signal */
151  sigaction(SIGTERM, &new_action, NULL); /* exitClear on SIGTERM signal */
152  sigaction(SIGHUP, &new_action, NULL); /* exitClear on SIGHUP signal */
153  }
154 }
155 
156 
158 {
159 // revert stdin and stdout back to their original states
160  setvbuf(stdin, (char *)NULL, _IOLBF, 0);
161  setvbuf(stdout, (char *)NULL, _IOLBF, 0);
162 }
163 
164 
165 
166 void SystemInterpreter::live(size_t liveMark)
167 {
168 }
169 
171 {
172  if (!memoryObject.savingImage())
173  {
174  }
175 }
176 
177 
178 /**
179  * Get the current working directory for the process.
180  *
181  * @return The current working directory as a Rexx string.
182  */
184 {
185  if (!getcwd(buf, PATH_MAX)) /* Get current working direct */
186  {
187  strncpy(buf, getenv("PWD"), PATH_MAX);
188  // if we don't result in a real directory here, make it a null string.
189  if (buf[0] != '/' )
190  {
191  buf[0] = '\0';
192  }
193  }
194 }
RexxMemory memoryObject
Definition: RexxMemory.cpp:85
static void processStartup()
static bool haltAllActivities(RexxString *)
bool savingImage()
Definition: RexxMemory.hpp:217
static void terminateInterpreter()
static void liveGeneral(int reason)
static void getCurrentWorkingDirectory(char *)
static void live(size_t)
void signalHandler(int sig)