Debian Codegraph @

src/bin/tag_attr/tag_attr.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-2017 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
32 #include <stdlib.h> /* exit() */
33 #include <string.h> /* memset() */
34 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
35
36 #include "dwarf.h"
37 #include "libdwarf.h"
38 #include "libdwarf_private.h"
39 #include "dd_defined_types.h"
40 #include "dd_checkutil.h"
41 #include "dd_glflags.h"
42 #include "dd_globals.h"
43 #include "dd_common.h"
44 #include "dd_attr_form.h" /* for struct threekey_s */
45
46 #include "dd_esb.h"
47 #include "dd_tag_common.h"
48 #include "dd_getopt.h"
49 #include "dd_safe_strcpy.h"
50 #include "dd_minimal.h"
51
52 void dd_minimal_count_global_error(void) {}
53
54 Dwarf_Bool ellipsis = FALSE; /* So we can use dwarf_names.c */
55
56 /* Expected input format
57
58 0xffffffff
59 value of a tag
60 value of a standard attribute that follows that tag
61 ...
62 0xffffffff
63 value of a tag
64 value of a standard attribute that follows that tag
65 ...
66 0xffffffff
67 ...
68
69 The generated tag_attr_combination_table
70 is used and generated quite differently
71 for standard than for extended tags.
72
73 For standard tags the generated table is indexed by
74 tag number. All the columns are bit flags.
75
76 For extended tags the generated table is indexed
77 (call it j) by 0 - N-1
78 and [j][0] is the tag number and the rest of
79 the columns (1 - N-1) are
80 allowed attribute numbers.
81
82 */
83
84 static const char *usage[] = {
85 "Usage: tag_attr_build <options>",
86 " -i input-table-path",
87 " -o output-table-path",
88 " -s (Generate standard attribute table)",
89 " -e (Generate extended attribute table (common extensions))",
90 ""
91 };
92
93 const char *program_name = 0;
94 char *input_name = 0;
95 char *output_name = 0;
96 int standard_flag = FALSE;
97 int extended_flag = FALSE;
98
99 /* process arguments */
100 static void
101 process_args(int argc, char *argv[])
102 {
103 int c = 0;
104 Dwarf_Bool usage_error = FALSE;
105
106 program_name = argv[0];
107
108 while ((c = dwgetopt(argc, argv, "i:o:se")) != EOF) {
109 switch (c) {
110 case 'i':
111 input_name = dwoptarg;
112 break;
113 case 'o':
114 output_name = dwoptarg;
115 break;
116 case 'e':
117 extended_flag = TRUE;
118 break;
119 case 's':
120 standard_flag = TRUE;
121 break;
122 default:
123 usage_error = TRUE;
124 break;
125 }
126 }
127
128 if (usage_error || 1 == dwoptind || dwoptind != argc) {
129 print_usage_message(usage);
130 exit(EXIT_FAILURE);
131 }
132 }
133 #if 0
134 /* Two new naming routines May 2016.
135 Instead of directly calling dwarf_get_*()
136 When bad tag/attr numbers are presented we return
137 a warning string through the pointer.
138 The thought is that eventually someone will notice the error.
139 It might, of course, be better to emit an error message
140 and stop. */
141 static void
142 ta_get_AT_name(unsigned int attrnum,const char **nameout)
143 {
144 int res = 0;
145
146 res = dwarf_get_AT_name(attrnum,nameout);
147 if (res == DW_DLV_OK) {
148 return;
149 }
150 if (res == DW_DLV_NO_ENTRY) {
151 *nameout = "<no name known for the attribute>";
152 return;
153 }
154 *nameout = "<ERROR so no name known for the attribute>";
155 return;
156 }
157 #endif
158
159 static void
160 ta_get_TAG_name(unsigned int tag,const char **nameout)
161 {
162 int res = 0;
163
164 res = dwarf_get_TAG_name(tag,nameout);
165 if (res == DW_DLV_OK) {
166 return;
167 }
168 if (res == DW_DLV_NO_ENTRY) {
169 *nameout = "<no name known for the tag>";
170 return;
171 }
172 *nameout = "<ERROR so no name known for the tag>";
173 return;
174 }
175
176 #if 0
177 static unsigned maxrowused = 0;
178 static unsigned maxcolused = 0;
179
180 /* The argument sizes are declared in tag_common.h, so
181 the max usable is toprow-1 and topcol-1. */
182 static void
183 check_unused_combo(unsigned toprow,unsigned topcol)
184 {
185 if ((toprow-1) != maxrowused) {
186 printf("Providing for %u rows but used 0-%u\n",
187 toprow,maxrowused);
188 printf("Giving up\n");
189 exit(EXIT_FAILURE);
190 }
191 if ((topcol-1) != maxcolused) {
192 printf("Providing for %u cols but used 0-%u\n",
193 topcol,maxcolused);
194 printf("Giving up\n");
195 exit(EXIT_FAILURE);
196 }
197 }
198
199 static void
200 validate_row_col(const char *position,
201 unsigned crow,
202 unsigned ccol,
203 unsigned maxrow,
204 unsigned maxcol)
205 {
206 if (crow >= ATTR_TABLE_ROW_MAXIMUM) {
207 printf("error generating row in tag-attr array, %s "
208 "current row: %u size of static array decl: %u\n",
209 position,crow, ATTR_TABLE_ROW_MAXIMUM);
210 exit(EXIT_FAILURE);
211 }
212 if (crow >= maxrow) {
213 printf("error generating row in tag-attr array, %s "
214 "current row: %u max allowed: %u\n",
215 position,crow,maxrow-1);
216 exit(EXIT_FAILURE);
217 }
218 if (ccol >= ATTR_TABLE_COLUMN_MAXIMUM) {
219 printf("error generating column in tag-attr array, %s "
220 "current col: %u size of static array decl: %u\n",
221 position,ccol, ATTR_TABLE_COLUMN_MAXIMUM);
222 exit(EXIT_FAILURE);
223 }
224 if (ccol >= maxcol) {
225 printf("error generating column in tag-attr array, %s "
226 "current row: %u max allowed: %u\n",
227 position,ccol,maxcol-1);
228 exit(EXIT_FAILURE);
229 }
230 if (crow > maxrowused) {
231 maxrowused = crow;
232 }
233 if (ccol > maxcolused) {
234 maxcolused = ccol;
235 }
236 return;
237 }
238 #endif
239
240 int
241 main(int argc, char **argv)
242 {
243 unsigned int num = 0;
244 int input_eof = 0;
245 #if 0
246 unsigned u = 0;
247 unsigned table_rows = 0;
248 unsigned table_columns = 0;
249 unsigned current_row = 0;
250 #endif
251 FILE * fileInp = 0;
252 FILE * fileOut = 0;
253 unsigned int table_type = 0;
254 const char *name = 0;
255 const char *structname = 0;
256
257 print_version_details(argv[0]);
258 print_args(argc,argv);
259 process_args(argc,argv);
260
261 if (!input_name ) {
262 fprintf(stderr,"Input name required, not supplied.\n");
263 print_usage_message(usage);
264 exit(EXIT_FAILURE);
265 }
266 fileInp = fopen(input_name,"r");
267 if (!fileInp) {
268 fprintf(stderr,"Invalid input filename,"
269 " could not open '%s'\n",
270 input_name);
271 print_usage_message(usage);
272 exit(EXIT_FAILURE);
273 }
274
275 if (!output_name ) {
276 fprintf(stderr,"Output name required, not supplied.\n");
277 print_usage_message(usage);
278 exit(EXIT_FAILURE);
279 }
280 fileOut = fopen(output_name,"w");
281 if (!fileOut) {
282 fprintf(stderr,"Invalid output filename,"
283 " could not open: '%s'\n",
284 output_name);
285 print_usage_message(usage);
286 exit(EXIT_FAILURE);
287 }
288 if ((standard_flag && extended_flag) ||
289 (!standard_flag && !extended_flag)) {
290 fprintf(stderr,"Invalid table type\n");
291 fprintf(stderr,"Choose -e or -s .\n");
292 print_usage_message(usage);
293 exit(EXIT_FAILURE);
294 }
295
296 if (standard_flag) {
297 structname = "dd_threekey_ta_std";
298 table_type = AF_STD;
299 } else {
300 structname = "dd_threekey_ta_ext";
301 table_type = AF_EXTEN;
302 }
303 #if 0
304 if (standard_flag) {
305 table_rows = STD_ATTR_TABLE_ROWS;
306 table_columns = STD_ATTR_TABLE_COLUMNS;
307 } else {
308 table_rows = EXT_ATTR_TABLE_ROWS;
309 table_columns = EXT_ATTR_TABLE_COLS;
310 }
311 #endif
312
313 input_eof = read_value(&num,fileInp); /* 0xffffffff */
314 if (IS_EOF == input_eof) {
315 bad_line_input("Empty input file");
316 }
317 if (num != MAGIC_TOKEN_VALUE) {
318 bad_line_input("Expected 0xffffffff");
319 }
320
321 /* Generate main header, regardless of contents */
322 fprintf(fileOut,"/* Generated code, do not edit. */\n");
323 fprintf(fileOut,"/* Generated for source version %s */\n",
324 PACKAGE_VERSION);
325 fprintf(fileOut,"\n/* BEGIN FILE */\n\n");
326 fprintf(fileOut,"struct Three_Key_Entry_s %s [] = {\n",
327 structname);
328
329 while (!feof(stdin)) {
330 unsigned int tag;
331 #if 0
332 unsigned int curcol = 0;
333 #endif
334
335 input_eof = read_value(&tag,fileInp);
336 if (IS_EOF == input_eof) {
337 /* Reached normal eof */
338 break;
339 }
340 #if 0
341 {
342 /* In extended case, the row indexed by 0-N
343 and column zero has the tag number. */
344 if (current_row >= table_rows) {
345 bad_line_input(
346 "too many extended table rows. Have %u max %u",
347 current_row,table_rows);
348 }
349 validate_row_col("Reading tag",current_row,0,
350 table_rows,table_columns);
351 tag_attr_combination_table[current_row][0] = tag;
352 }
353 #endif
354 input_eof = read_value(&num,fileInp);
355 if (IS_EOF == input_eof) {
356 bad_line_input("Not terminated correctly..");
357 }
358 #if 0
359 curcol = 1;
360 #endif
361 ta_get_TAG_name(tag,&name);
362 fprintf(fileOut,"/* 0x%02x - %-37s*/\n", tag, name);
363
364 while (num != MAGIC_TOKEN_VALUE) {
365 #if 0
366 struct esb_s msg_buf;
367
368 esb_constructor(&msg_buf);
369 {
370 if (curcol >= table_columns) {
371 esb_append_printf_i(&msg_buf,
372 "too many attributes b: table incomplete "
373 "index %d ",curcol);
374 esb_append_printf_i(&msg_buf,
375 "cols %d.",table_columns);
376 bad_line_input(esb_get_string(&msg_buf));
377 }
378 validate_row_col("Setting attr col",
379 current_row,curcol,
380 table_rows,table_columns);
381 tag_attr_combination_table[current_row][curcol] =
382 num;
383 curcol++;
384
385 }
386 esb_destructor(&msg_buf);
387 #endif
388 /* print a 3key */
389 fprintf(fileOut,"{0x%04x,0x%04x,%u,%d,0,0},\n",
390 (Dwarf_Half)tag,(Dwarf_Half)num,(Dwarf_Half)0,
391 table_type);
392 input_eof = read_value(&num,fileInp);
393 if (IS_EOF == input_eof) {
394 bad_line_input("Not terminated correctly.");
395 }
396 }
397 #if 0
398 ++current_row;
399 #endif
400 }
401 fprintf(fileOut,"{0,0,0,0,0,0}};");
402 fprintf(fileOut,"\n/* END FILE */\n");
403 fclose(fileInp);
404 fclose(fileOut);
405 return 0;
406 }
407 #if 0
408 if (standard_flag) {
409 fprintf(fileOut,"#define ATTR_TREE_ROW_COUNT %d\n\n",
410 table_rows);
411 fprintf(fileOut,"#define ATTR_TREE_COLUMN_COUNT %d\n\n",
412 table_columns);
413 fprintf(fileOut,
414 "static Dwarf_Half tag_attr_combination_table\n");
415 fprintf(fileOut,
416 "[ATTR_TREE_ROW_COUNT][ATTR_TREE_COLUMN_COUNT] = {\n");
417 }
418 else {
419 fprintf(fileOut,"/* Common extensions */\n");
420 fprintf(fileOut,"#define ATTR_TREE_EXT_ROW_COUNT %d\n\n",
421 table_rows);
422 fprintf(fileOut,"#define ATTR_TREE_EXT_COLUMN_COUNT %d\n\n",
423 table_columns);
424 fprintf(fileOut,
425 "static Dwarf_Half tag_attr_combination_ext_table\n");
426 fprintf(fileOut,
427 " [ATTR_TREE_EXT_ROW_COUNT]"
428 "[ATTR_TREE_EXT_COLUMN_COUNT]"
429 " = {\n");
430 }
431
432 for (u = 0; u < table_rows; u++) {
433 unsigned j = 0;
434 const char *name = 0;
435 unsigned k = tag_attr_combination_table[u][0];
436 ta_get_TAG_name(k,&name);
437 fprintf(fileOut,"/* 0x%02x - %-37s*/\n",k,name);
438 fprintf(fileOut," { ");
439 for (j = 0; j < table_columns; ++j ) {
440 if (j && j%5 == 0) {
441 fprintf(fileOut,"\n ");
442 }
443 fprintf(fileOut,"0x%08x,",
444 tag_attr_combination_table[u][j]);
445 }
446 fprintf(fileOut,"},\n");
447 }
448 fprintf(fileOut,"{0,0,0,0,0,0}};");
449 fprintf(fileOut,"\n/* END FILE */\n");
450 fclose(fileInp);
451 fclose(fileOut);
452 return (0);
453 }
454 /* A fake so we can use dwarf_names.c */
455 void print_error (Dwarf_Debug dbg,
456 const char * msg,
457 int res,
458 Dwarf_Error localerr)
459 {
460 (void)dbg;
461 (void)msg;
462 (void)res;
463 (void)localerr;
464 }
465 #endif