Debian Codegraph @

src/bin/tag_tree/tag_common.c

raw
1 /*
2 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2009-2012 SN Systems Ltd. All Rights Reserved.
4 Portions Copyright (C) 2009-2012 David Anderson. All Rights Reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of version 2 of the GNU General
8 Public License as published by the Free Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write the Free
25 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 Boston MA 02110-1301, USA.
27
28 */
29
30 #include <config.h>
31 #include <dwarf.h>
32 #include <stdio.h>
33 #include <stdarg.h> /* For va_start va_arg va_list */
34 #include <stdlib.h>/* For exit() declaration etc. */
35 #include <string.h>
36 #include <errno.h>/* For errno declaration. */
37 #include <ctype.h> /* For isspace() declaration */
38 #include "dwarf.h"
39 #include "libdwarf.h"
40 #include "dd_defined_types.h"
41 #include "dd_checkutil.h"
42 #include "dd_glflags.h"
43 #include "dd_globals.h"
44 #include "dd_naming.h"
45 #include "dd_tag_common.h"
46
47 static int linecount = 0;
48 static char line_in[MAX_LINE_SIZE];
49
50 void
51 bad_line_input(char *format,...)
52 {
53 va_list ap;
54 va_start(ap,format);
55 fprintf(stderr,
56 "tag_(tree,attr,form) table build failed, line %d: \"%s\". ",
57 linecount, line_in);
58 vfprintf(stderr,format, ap);
59 /* "The object ap may be passed as an argument to another
60 function; if that function invokes the va_arg()
61 macro with parameter ap, the value of ap in the calling
62 function is unspecified and shall be passed to the va_end()
63 macro prior to any further reference to ap."
64 Single Unix Specification. */
65 va_end(ap);
66 fprintf(stderr,"\n");
67 exit(EXIT_FAILURE);
68 }
69
70 void
71 trim_newline(char *line, int max)
72 {
73 char *end = line + max - 1;
74
75 for (; *line && (line < end); ++line) {
76 if (*line == '\n') {
77 /* Found newline, drop it */
78 *line = 0;
79 return;
80 }
81 }
82 return;
83 }
84
85 /* Detect empty lines (and other lines we do not want to read) */
86 static Dwarf_Bool
87 is_skippable_line(char *pLine)
88 {
89 Dwarf_Bool empty = TRUE;
90
91 if (pLine[0] == '#') {
92 /* Preprocessor lines are of no interest. */
93 return TRUE;
94 }
95 for (; *pLine && empty; ++pLine) {
96 empty = isspace(*pLine);
97 }
98 return empty;
99 }
100
101 /* Reads a value from the text table.
102 Exits with non-zero status
103 if the table is erroneous in some way.
104 */
105
106 /* This array must match exactly the order of
107 the enum entries in Dwarf_Form_Class in libdwarf.h.in. */
108 static char *enumvals[] = {
109 "DW_FORM_CLASS_UNKNOWN",
110 "DW_FORM_CLASS_ADDRESS",
111 "DW_FORM_CLASS_BLOCK",
112 "DW_FORM_CLASS_CONSTANT",
113 "DW_FORM_CLASS_EXPRLOC",
114 "DW_FORM_CLASS_FLAG",
115 "DW_FORM_CLASS_LINEPTR",
116 "DW_FORM_CLASS_LOCLISTPTR", /* DWARF2,3,4 only */
117 "DW_FORM_CLASS_MACPTR", /* DWARF2,3,4 only */
118 "DW_FORM_CLASS_RANGELISTPTR", /* DWARF2,3,4 only */
119 "DW_FORM_CLASS_REFERENCE",
120 "DW_FORM_CLASS_STRING",
121 "DW_FORM_CLASS_FRAMEPTR", /* MIPS/IRIX DWARF2 only */
122 "DW_FORM_CLASS_MACROPTR", /* DWARF5 */
123 "DW_FORM_CLASS_ADDRPTR", /* DWARF5 */
124 "DW_FORM_CLASS_LOCLIST", /* DWARF5 */
125 "DW_FORM_CLASS_LOCLISTSPTR", /* DWARF5 */
126 "DW_FORM_CLASS_RNGLIST", /* DWARF5 */
127 "DW_FORM_CLASS_RNGLISTSPTR", /* DWARF5 */
128 "DW_FORM_CLASS_STROFFSETSPTR", /* DWARF5 */
129 0
130
131 };
132
133 static int lookup_enum_val( char * lin)
134 {
135 const char *s = enumvals[0];
136 char *lp = lin;
137 int inputlen = 0;
138 int enumindex = -1;
139 int i = 0;
140 for (i = 0;*lp; ++lp,++i) {
141 if (*lp == ' ' || *lp == '\n') {
142 break;
143 }
144 ++inputlen;
145 }
146 if (inputlen < 14) {
147 bad_line_input("bad DW_FORM_CLASS_ input!");
148 }
149 if (strncmp(lin,"DW_FORM_CLASS_",14)) {
150 bad_line_input("bad DW_FORM_CLASS_ input..");
151 }
152 for (i = 0 ; s ; ++i, s = enumvals[i]) {
153 int len = inputlen;
154 int arslen = strlen(s);
155 if (arslen > len) {
156 len = arslen;
157 }
158 if (!strncmp(s,lin,len)) {
159 enumindex = i;
160 return enumindex;
161 break;
162 }
163 }
164 if (enumindex < 1) {
165 bad_line_input("DW_FORM_CLASS not matched");
166 }
167 return -1;
168 }
169
170 int
171 read_value(unsigned int *outval,FILE*file)
172 {
173 char *res = 0;
174 unsigned long lval;
175 char *strout = 0;
176 Dwarf_Bool bBlankLine = TRUE;
177
178 ++linecount;
179 *outval = 0;
180
181 while (bBlankLine) {
182 res = fgets(line_in, sizeof(line_in), file);
183 if (res == 0) {
184 if (ferror(file)) {
185 fprintf(stderr,
186 "attr_form: Error reading table, %d lines read\n",
187 linecount);
188 exit(EXIT_FAILURE);
189 }
190
191 if (feof(file)) {
192 return IS_EOF;
193 }
194
195 /* Impossible */
196 fprintf(stderr, "attr_form: "
197 "Impossible error reading table, "
198 "%d lines read\n", linecount);
199 exit(EXIT_FAILURE);
200 }
201
202 bBlankLine = is_skippable_line(line_in);
203 }
204
205 trim_newline(line_in, sizeof(line_in));
206 errno = 0;
207 lval = strtoul(line_in, &strout, 0);
208
209 if (strout == line_in) {
210 int enumindex = -1;
211 /* We have outlen, so look up an enum value */
212 enumindex = lookup_enum_val(line_in);
213 if (enumindex < 1) {
214 bad_line_input("Cannot find enum value");
215 }
216 *outval = enumindex;
217 return NOT_EOF;
218 }
219 if (errno != 0) {
220 int myerr = errno;
221 fprintf(stderr, "attr_form errno %d\n", myerr);
222 bad_line_input("invalid number on line");
223 }
224 *outval = (int) lval;
225 return NOT_EOF;
226 }