Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / env / fnx / clib / report / cc / report_pli_util.cc
CommitLineData
86530b38
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: report_pli_util.cc
4// Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
5// 4150 Network Circle, Santa Clara, California 95054, U.S.A.
6//
7// * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8//
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; version 2 of the License.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21//
22// For the avoidance of doubt, and except that if any non-GPL license
23// choice is available it will apply instead, Sun elects to use only
24// the General Public License version 2 (GPLv2) at this time for any
25// software where a choice of GPL license versions is made
26// available with the language indicating that GPLv2 or any later version
27// may be used, or where a choice of which version of the GPL is applied is
28// otherwise unspecified.
29//
30// Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
31// CA 95054 USA or visit www.sun.com if you need additional information or
32// have any questions.
33//
34// ========== Copyright Header End ============================================
35
36#include <stdio.h>
37#include <string.h>
38#include <malloc.h>
39#include <sys/types.h>
40#include <regex.h>
41#include <cReport.h>
42#include <veriuser.h>
43
44//#ifdef __cplusplus
45//extern "C" {
46//#endif
47
48/*
49 * regex_match
50 *
51 * [This function is borrowed verbatim from Andrew Milia's pos_report implementation.]
52 *
53 * This function takes a string that
54 * is a regular expression and a target string
55 * and returns:
56 * 0 - if target string matches pattern
57 * 1 - if target string does not match string
58 * 2 - an error in compiling the regular expression
59 * has occurred and a reason for the error
60 * has been printed to stderr
61 * if the pattern is NULL then the pattern that was
62 * compiled the last time this function was called
63 * will be used.
64 *
65 */
66 int regex_match(const char *pattern, const char *string)
67 {
68 static regex_t compiled_regex = {NULL}; /* the compiled regex pattern
69 * (static so that it can be reused between calls)
70 */
71 static int initd = 0;
72 int return_status; /* return status from function calls */
73
74 if( (!initd) && (! pattern) ) {
75 tf_error("Report: Uninitialized regexp in regexp_match");
76 }
77
78 initd = 1;
79
80 /* if pattern is not null then recompile a new pattern */
81 if( pattern &&
82 (return_status = regcomp( &compiled_regex, pattern, REG_NOSUB)) )
83 {
84 /* we got a regex compile error
85 * so we need a place to put the error message
86 */
87 char error_message[1024];
88
89 regfree( &compiled_regex );
90 regerror( return_status, &compiled_regex, error_message, 1024);
91 tf_error(
92 "(%s) line: %d BAD REGULAR EXPRESSION:\"%s\"\n",
93 __FILE__,
94 __LINE__,
95 error_message
96 );
97
98 return(2);
99 }
100
101 if( regexec(&compiled_regex, string, (size_t) 0, NULL, 0) )
102 return(1);
103
104 return(0);
105 }
106
107/*
108 * parse_format_string
109 *
110 * Utility function to convert a format string to a fixed message string.
111 *
112 */
113void parse_format_string(char* msg_str, char* format_str, int param)
114 {
115 char* fmt_mark = format_str; // format string pointer
116 char* msg_mark = msg_str;
117 char* target;
118 char format_char;
119 int count;
120
121 // Parse the next piece of the format string.
122 // This is the piece between the current point and the next '%'.
123 while (target = (char*)strchr(fmt_mark, '%'))
124 {
125 // Stash everything between the last '%' and the current '%' into msg_str.
126 *target = (char)0; // add a terminating character
127 format_char = *(target+1);
128 strcpy(msg_mark, fmt_mark);
129
130 // Move the msg string pointer to the end of what was just copied.
131 msg_mark += (target - fmt_mark);
132
133 // Output the formatted argument, based on format_char.
134 if (format_char == '%')
135 {
136 count = sprintf(msg_mark, "%%");
137 }
138 else if (format_char == 's')
139 {
140 // Use a special PLI routine to format a C character string.
141 count = sprintf(msg_mark, "%s", tf_getcstringp(param++));
142 }
143 else
144 {
145 // Use a PLI routine to format the argument in the same way
146 // as the routine.
147 count = sprintf(msg_mark, "%s", tf_strgetp(param++, format_char));
148 }
149
150 // Move the string pointers past what we just parsed.
151 fmt_mark = target + 2;
152 msg_mark += count;
153 }
154
155 // Copy the remainder of the format string.
156 strcpy(msg_mark, fmt_mark);
157
158 // Deal with any extra arguments.
159 while (param < tf_nump())
160 {
161 if (tf_typep(param) == tf_nullparam)
162 {
163 // Print a space for null parameters
164 sprintf(msg_mark, " ");
165 msg_mark++;
166 }
167 else
168 {
169 // Everything else just gets decimal format.
170 count = sprintf(msg_mark, "%s", tf_strgetp(param, 'd'));
171
172 }
173 param++;
174 }
175 }
176
177//#ifdef __cplusplus
178//}
179//#endif
180