Open64 (mfef90, whirl2f, and IR tools)
TAG: version-openad; SVN changeset: 916
|
00001 #pragma ident "@(#)92/msgnew/catmsgfmt.c 92.2 06/03/99 09:59:18" 00002 00003 /* 00004 00005 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00006 00007 This program is free software; you can redistribute it and/or modify it 00008 under the terms of version 2 of the GNU General Public License as 00009 published by the Free Software Foundation. 00010 00011 This program is distributed in the hope that it would be useful, but 00012 WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00014 00015 Further, this software is distributed without any warranty that it is 00016 free of the rightful claim of any third person regarding infringement 00017 or the like. Any license provided herein, whether implied or 00018 otherwise, applies only to this software file. Patent licenses, if 00019 any, provided herein do not apply to combinations of this program with 00020 other software, or any other product whatsoever. 00021 00022 You should have received a copy of the GNU General Public License along 00023 with this program; if not, write the Free Software Foundation, Inc., 59 00024 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00025 00026 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00027 Mountain View, CA 94043, or: 00028 00029 http://www.sgi.com 00030 00031 For further information regarding this notice, see: 00032 00033 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00034 00035 */ 00036 00037 #ifdef _LITTLE_ENDIAN 00038 #pragma weak _catmsgfmt = catmsgfmt 00039 #else /* Else _LITTLE_ENDIAN */ 00040 #if defined(__STDC__) && !defined(_LIBU) 00041 #pragma weak catmsgfmt = _catmsgfmt 00042 #endif 00043 #endif /* End _LITTLE_ENDIAN */ 00044 00045 /* 00046 * IMPORTANT: 00047 * This section is needed since this file also resides in the compilers' 00048 * libcsup/msg (v7.2 and higher). Once the compilers drop support for 00049 * pre-IRIX 6.5 releases this can be removed. Please build a libu before 00050 * checking in any changes to this file. 00051 * 00052 */ 00053 /* patch these two definitions to Solaris's <nl_types.h> 00054 * there is one more symbol 'NL_MSGSET' undefined on Solaris, 00055 * but I put its definition NL_MSGSET = NL_SEFD into compiler 00056 * command line 00057 */ 00058 00059 #define DISABLE_NL_TYPES_SYMBOL_RENAMING 1 /* for our <nl_types.h> */ 00060 00061 #include <locale.h> 00062 #include <stdio.h> 00063 #include <string.h> 00064 #include <stdlib.h> 00065 #include <time.h> 00066 #include <nl_types.h> 00067 00068 #include <cray/nlcatmsg.h> /* Open64 header */ 00069 00070 /* SGI's <nl_types.h> defines MSG_FORMAT and D_MSG_FORMAT when 00071 __NLS_INTERNALS is defined */ 00072 #ifndef MSG_FORMAT 00073 # define MSG_FORMAT "MSG_FORMAT" 00074 #endif 00075 00076 #ifndef D_MSG_FORMAT 00077 # define D_MSG_FORMAT "%G-%N %C: %S %P\n %M\n" 00078 #endif 00079 00080 #define OCTAL 8 00081 #define HEXADECIMAL 16 00082 #define MAXTIME 51 00083 00084 /* 00085 * catmsgfmt - format an error message 00086 * 00087 * char * 00088 * catmsgfmt( 00089 * char *cmdname, 00090 * char *groupcode, 00091 * int msg_num, 00092 * char *severity, 00093 * char *msgtext, 00094 * char *buf, 00095 * int buflen, 00096 * char *position, 00097 * char *debug) 00098 * 00099 * catmsgfmt() formats up to "buflen" characters a message containing 00100 * the command name "cmdname", group code "groupcode", message number 00101 * "msg_num", severity level "severity", the text of the message 00102 * "msgtext", and a debug parameter "debug". The formatted 00103 * message is placed in the user-supplied buffer "buf". 00104 * 00105 * The msg_num parameter is an integer which is converted to an ASCII 00106 * string of digits. The cmdname, groupcode, severity, and msgtext 00107 * parameters are all null-terminated character strings. 00108 * 00109 * The MSG_FORMAT environment variable controls how the message 00110 * is formatted. If the MSG_FORMAT environment variable is not 00111 * defined, a default formatting value is used. 00112 */ 00113 00114 char * 00115 catmsgfmt( 00116 const char *cmdname, 00117 const char *groupcode, 00118 int msg_num, 00119 const char *severity, 00120 const char *msgtext, 00121 char *buf, 00122 int buflen, 00123 char *pos, 00124 char *dbg 00125 ) 00126 { 00127 char c; /* Current character */ 00128 char *fmtp; /* Pointer to format */ 00129 char *cmsp; /* Pointer to current position in message */ 00130 char *tknp; /* Pointer to current token */ 00131 char *tncp; /* Pointer to terminating number character */ 00132 char *tfmt; /* Timestamp format */ 00133 char num[20]; /* Converted message number */ 00134 char tms[MAXTIME]; /* Converted timestamp */ 00135 int cmsl; /* Bytes remaining in message */ 00136 time_t cts; /* Current timestamp */ 00137 struct tm *ltsp; /* Local time structure */ 00138 00139 if ((buflen < 1) || (msg_num < 1)) 00140 return( (char *) NULL); 00141 00142 /* Convert number */ 00143 (void) sprintf(num, "%d", msg_num); 00144 00145 /* Clear timestamp string */ 00146 *tms = '\0'; 00147 00148 /* Find format */ 00149 if ((fmtp = getenv(MSG_FORMAT)) == NULL) /* If format not defined */ 00150 fmtp = D_MSG_FORMAT; /* Set default format */ 00151 00152 cmsl = 1; /* Current length (save 1 byte for \0 */ 00153 cmsp = buf; /* Set current position in message */ 00154 00155 while ((*fmtp != '\0') && (cmsl < buflen)) 00156 if ((c = *fmtp++) != '%') { 00157 cmsl++; 00158 if (c != '\\') 00159 *cmsp++ = c; 00160 else { 00161 switch (*fmtp) { /* Process escapes */ 00162 00163 case 'a' : c = '\a'; 00164 break; 00165 00166 case 'b' : c = '\b'; 00167 break; 00168 00169 case 'f' : c = '\f'; 00170 break; 00171 00172 case 'n' : c = '\n'; 00173 break; 00174 00175 case 'r' : c = '\r'; 00176 break; 00177 00178 case 't' : c = '\t'; 00179 break; 00180 00181 case 'v' : c = '\v'; 00182 break; 00183 00184 case 'x' : /* Do hex character */ 00185 c = (char)strtol(fmtp + 1, 00186 &tncp, HEXADECIMAL); 00187 if (tncp == (fmtp + 1)) 00188 c = *fmtp; 00189 else 00190 fmtp = tncp - 1; 00191 break; 00192 00193 case '\\': c = '\\'; 00194 break; 00195 00196 case '?' : c = '?'; 00197 break; 00198 00199 case '\'': c = '\''; 00200 break; 00201 00202 case '"' : c = '"'; 00203 break; 00204 00205 case '0' : 00206 case '1' : 00207 case '2' : 00208 case '3' : 00209 case '4' : 00210 case '5' : 00211 case '6' : 00212 case '7' : /* Do octal character */ 00213 c = (char) strtol(fmtp, 00214 &tncp, OCTAL); 00215 if (tncp == fmtp) 00216 c = *fmtp; 00217 else 00218 fmtp = tncp - 1; 00219 break; 00220 00221 default : c = *fmtp; 00222 } 00223 00224 *cmsp++ = c; 00225 fmtp++; 00226 } 00227 } 00228 else { 00229 switch (*fmtp) { 00230 00231 case '%': /* Percent */ 00232 tknp = "%"; 00233 break; 00234 00235 case 'C': /* Command */ 00236 tknp = (char *) cmdname; 00237 break; 00238 00239 case 'D': /* Debug */ 00240 tknp = dbg; 00241 break; 00242 00243 case 'G': /* Group name */ 00244 tknp = (char *) groupcode; 00245 break; 00246 00247 case 'N': /* Message number */ 00248 tknp = num; 00249 break; 00250 00251 case 'P': /* Position */ 00252 tknp = pos; 00253 break; 00254 00255 case 'S': /* Severity */ 00256 tknp = (char *) severity; 00257 break; 00258 00259 case 'T': /* Timestamp */ 00260 tknp = tms; 00261 00262 if (*tms == '\0') { /* If not set */ 00263 (void) time(&cts); 00264 ltsp = localtime(&cts); 00265 tfmt = getenv("CFTIME"); 00266 if (tfmt == NULL || 00267 *tfmt == '\0') 00268 /* date(1) format */ 00269 tfmt = "%a %b %e %H:%M:%S %Z %Y"; 00270 (void) strftime(tms, MAXTIME, 00271 tfmt, ltsp); 00272 } 00273 break; 00274 00275 case 'M': /* Message text */ 00276 tknp = (char *) msgtext; 00277 break; 00278 00279 default: /* Everything else */ 00280 *cmsp++ = c; 00281 if (cmsl++ < buflen) 00282 *cmsp++ = *fmtp; 00283 cmsl++; 00284 tknp = ""; 00285 } 00286 00287 if (tknp != NULL) { 00288 while ((*tknp != '\0') && (cmsl++ < buflen)) 00289 *cmsp++ = *tknp++; 00290 } 00291 fmtp++; 00292 00293 } 00294 00295 *cmsp = '\0'; /* Terminate formatted message */ 00296 00297 return(buf); 00298 }