unix/ErrorMessages.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 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.oorexx.org/license.html */
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 Unix Support aixerr.c */
40 /* */
41 /* Retrieve message from message repository using the X/Open catopen(), */
42 /* catgets() and catclose() function calls. */
43 /* */
44 /******************************************************************************/
45 /******************************************************************************/
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif
49 
50 #include <stdio.h> /* include standard headers */
51 #include <string.h>
52 #include <ctype.h>
53 #include <limits.h>
54 
55 #if defined( HAVE_NL_TYPES_H )
56 # include <nl_types.h>
57 #endif
58 
59 #if defined( HAVE_MESG_H )
60 # include <mesg.h>
61 #endif
62 
63 #include <unistd.h>
64 #include <stdlib.h>
65 #define ERROR_TABLE /* include error message table */
66 #include "RexxCore.h" /* incl general definitions */
67 #include "StringClass.hpp"
68 #include "SystemInterpreter.hpp"
69 
70  /* define macros to bulid entries in */
71  /* the msgEntry table for msg lookup */
72 #define MAJOR(code) {code, code##_msg},/* Major error codes */
73 #define MINOR(code) {code, code##_msg},/* Minor error codes (sub-codes) */
74 
75 typedef struct msgEntry { /* define for error table entries */
76  int code; /* error message code */
77  int msgid; /* error message number */
79 
80 #include "RexxMessageNumbers.h" /* include definition of errorcodes */
81 #include "RexxMessageTable.h" /* include actual table definition */
82 
83 #ifdef LINUX
84 //#define SECOND_PARAMETER MCLoadAll /* different sign. Lin-AIX */
85 #define SECOND_PARAMETER 1 /* different sign. Lin-AIX */
86 #else
87 #define SECOND_PARAMETER 0 /* 0 for no NL_CAT_LOCALE */
88 #endif
89 
90 #ifndef CATD_ERR
91 #define CATD_ERR ((nl_catd)-1) /* Duplicate for AIX */
92 #endif
93 
94 
95 /**
96  * Retrieve the message text for a give error code.
97  *
98  * @param code The Rexx error code
99  *
100  * @return The error message associated with that code.
101  */
103 {
104 #if defined( HAVE_NL_TYPES_H )
105  nl_catd catd; /* catalog descriptor from catopen() */
106 #endif
107  int set_num = 1; /* message set 1 from catalog */
108  ERROR_MESSAGE *p; /* message table scan pointer */
109  int msgid; /* message number */
110  char DataArea[256]; /* buf to return message */
111  const char * message;
112  /* loop through looking for the */
113  /* error code */
114 #if defined( HAVE_CATOPEN )
115  for (p = Message_table; p->code != 0; p++)
116  {
117  if (p->code == code)
118  { /* found the target code? */
119 
120  msgid = p->msgid; /* get msg number associated w/ error*/
121  /* open message catalog in NLSPATH */
122  if ((catd = catopen(REXXMESSAGEFILE, SECOND_PARAMETER)) == (nl_catd)CATD_ERR)
123  {
124  snprintf(DataArea, sizeof DataArea, "%s/%s", ORX_CATDIR, REXXMESSAGEFILE);
125  if ((catd = catopen(DataArea, SECOND_PARAMETER)) == (nl_catd)CATD_ERR)
126  {
127  snprintf(DataArea, sizeof DataArea, "Cannot open REXX message catalog %s. Not in NLSPATH or %s.",
128  REXXMESSAGEFILE, ORX_CATDIR);
129  return new_string(DataArea);
130  }
131  } /* retrieve message from repository */
132  message = catgets(catd, set_num, msgid, NULL);
133  if (!message) /* got a message ? */
134  {
135 #if defined(OPSYS_LINUX) && !defined(OPSYS_SUN)
136  snprintf(DataArea, sizeof DataArea, "%s/%s", ORX_CATDIR, REXXMESSAGEFILE);
137  if ((catd = catopen(DataArea, SECOND_PARAMETER)) == (nl_catd)CATD_ERR)
138  {
139  snprintf(DataArea, sizeof DataArea, "Cannot open REXX message catalog %s. Not in NLSPATH or %s.",
140  REXXMESSAGEFILE, ORX_CATDIR);
141  return new_string(DataArea);
142  }
143  else
144  {
145  message = catgets(catd, set_num, msgid, NULL);
146  if (!message) /* got a message ? */
147  {
148  strcpy(DataArea,"Error message not found!");
149  }
150  else
151  {
152  strcpy(DataArea, message);
153  }
154  }
155 #else
156  strcpy(DataArea,"Error message not found!");
157 #endif
158  }
159  else
160  {
161  strcpy(DataArea, message);
162  }
163  catclose(catd); /* close the catalog */
164  /* convert and return the message */
165  return new_string(DataArea);
166  }
167  }
168  return OREF_NULL; /* no message retrieved */
169 #else
170  snprintf(DataArea, sizeof DataArea; "Cannot get description for error %d",msgid);
171  return new_string(&DataArea);
172 #endif
173 }
174 
175 
176 /**
177  * Return a message header for a given error message.
178  *
179  * @param code The error code
180  *
181  * @return The formatted message header
182  */
184 {
185  ERROR_MESSAGE *p; /* table scan pointer */
186  int msgid; /* message number */
187  char DataArea[20]; /* buf addr to return message */
188  /* loop through looking for the */
189  /* error code */
190  for (p = Message_table; p->code != 0; p++)
191  {
192  if (p->code == code)
193  { /* found the target code? */
194  msgid = p->msgid; /* get msg number associated w/ error*/
195  /* format as a message header */
196  snprintf(DataArea, sizeof DataArea, "REX%4.4dE: ", msgid);
197  return new_string(DataArea); /* return as a string object */
198  }
199  }
200  return OREF_NULL; /* no message retrieved */
201 }
202 
203 
204 
#define OREF_NULL
Definition: RexxCore.h:61
RexxString * new_string(const char *s, stringsize_t l)
static RexxString * getMessageText(wholenumber_t code)
static RexxString * getMessageHeader(wholenumber_t code)
ssize_t wholenumber_t
Definition: rexx.h:230
int code
int msgid
#define CATD_ERR
struct msgEntry ERROR_MESSAGE
#define SECOND_PARAMETER
#define REXXMESSAGEFILE