| 1 |
/* |
| 2 |
Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2009-2012 SN Systems Ltd. All rights reserved. |
| 4 |
Portions Copyright 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 <ctype.h> /* isspace() */ |
| 33 |
#include <stdarg.h> /* va_end() va_list va_start() */ |
| 34 |
#include <stdio.h> /* FILE stderr fprintf() vfprintf() */ |
| 35 |
#include <stdlib.h> /* exit() */ |
| 36 |
#include <string.h> /* strlen() strncmp() */ |
| 37 |
|
| 38 |
#include "dwarf.h" |
| 39 |
#include "libdwarf.h" |
| 40 |
#include "libdwarf_private.h" |
| 41 |
#include "dd_defined_types.h" |
| 42 |
#include "dd_checkutil.h" |
| 43 |
#include "dd_glflags.h" |
| 44 |
#include "dd_globals.h" |
| 45 |
#include "dd_common.h" |
| 46 |
#include "dd_tag_common.h" |
| 47 |
#include "dd_attr_form.h" /* threekey struct */ |
| 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 |
const char * program_name; |
| 55 |
|
| 56 |
Dwarf_Bool ellipsis = FALSE; /* So we can use dwarf_names.c */ |
| 57 |
|
| 58 |
/* Expected input format |
| 59 |
|
| 60 |
0xffffffff |
| 61 |
value of a tag |
| 62 |
value of a standard tag that may be a child ofthat tag |
| 63 |
... |
| 64 |
0xffffffff |
| 65 |
value of a tag |
| 66 |
value of a standard tag that may be a child ofthat tag |
| 67 |
... |
| 68 |
0xffffffff |
| 69 |
... |
| 70 |
|
| 71 |
No commentary allowed, no symbols, just numbers. |
| 72 |
Blank lines are allowed and are dropped. |
| 73 |
|
| 74 |
*/ |
| 75 |
|
| 76 |
static const char *usage[] = { |
| 77 |
"Usage: tag_tree_build <options>", |
| 78 |
"options:\t-t\tGenerate Tags table", |
| 79 |
" -i Input-file-path", |
| 80 |
" -o Output-table-path", |
| 81 |
" -e (Want Extended table (common extensions))", |
| 82 |
" -s (Want Standard table)", |
| 83 |
"" |
| 84 |
}; |
| 85 |
|
| 86 |
static char *input_name = 0; |
| 87 |
static char *output_name = 0; |
| 88 |
int extended_flag = FALSE; |
| 89 |
int standard_flag = FALSE; |
| 90 |
const char * structname = 0; |
| 91 |
|
| 92 |
static void |
| 93 |
process_args(int argc, char *argv[]) |
| 94 |
{ |
| 95 |
int c = 0; |
| 96 |
Dwarf_Bool usage_error = FALSE; |
| 97 |
|
| 98 |
program_name = argv[0]; |
| 99 |
|
| 100 |
while ((c = dwgetopt(argc, argv, "i:o:es")) != EOF) { |
| 101 |
switch (c) { |
| 102 |
case 'i': |
| 103 |
input_name = (char *)strdup(dwoptarg); |
| 104 |
break; |
| 105 |
case 'o': |
| 106 |
output_name = (char *)strdup(dwoptarg); |
| 107 |
break; |
| 108 |
case 'e': |
| 109 |
extended_flag = TRUE; |
| 110 |
break; |
| 111 |
case 's': |
| 112 |
standard_flag = TRUE; |
| 113 |
break; |
| 114 |
|
| 115 |
default: |
| 116 |
usage_error = TRUE; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (usage_error || 1 == dwoptind || dwoptind != argc) { |
| 122 |
print_usage_message(usage); |
| 123 |
exit(EXIT_FAILURE); |
| 124 |
} |
| 125 |
} |
| 126 |
/* New naming routine May 2016. |
| 127 |
Instead of directly calling dwarf_get_*() |
| 128 |
When bad tag/attr numbers are presented we return |
| 129 |
a warning string through the pointer. |
| 130 |
The thought is that eventually someone will notice the error. |
| 131 |
It might, of course, be better to emit an error message |
| 132 |
and stop. */ |
| 133 |
static void |
| 134 |
ta_get_TAG_name(unsigned int tagnum,const char **nameout) |
| 135 |
{ |
| 136 |
int res = 0; |
| 137 |
|
| 138 |
res = dwarf_get_TAG_name(tagnum,nameout); |
| 139 |
if (res == DW_DLV_OK) { |
| 140 |
return; |
| 141 |
} |
| 142 |
if (res == DW_DLV_NO_ENTRY) { |
| 143 |
*nameout = "<no name known for the TAG>"; |
| 144 |
return; |
| 145 |
} |
| 146 |
*nameout = "<ERROR so no name known for the TAG>"; |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
int |
| 151 |
main(int argc, char **argv) |
| 152 |
{ |
| 153 |
unsigned int num = 0; |
| 154 |
int input_eof = 0; |
| 155 |
FILE *fileInp = 0; |
| 156 |
FILE *fileOut = 0; |
| 157 |
unsigned int table_type = 0; |
| 158 |
|
| 159 |
print_version_details(argv[0]); |
| 160 |
print_args(argc,argv); |
| 161 |
process_args(argc,argv); |
| 162 |
|
| 163 |
if (!input_name ) { |
| 164 |
fprintf(stderr,"Input name required, not supplied.\n"); |
| 165 |
print_usage_message(usage); |
| 166 |
exit(EXIT_FAILURE); |
| 167 |
} |
| 168 |
fileInp = fopen(input_name,"r"); |
| 169 |
if (!fileInp) { |
| 170 |
fprintf(stderr,"Invalid input filename," |
| 171 |
" could not open '%s'\n", |
| 172 |
input_name); |
| 173 |
print_usage_message(usage); |
| 174 |
exit(EXIT_FAILURE); |
| 175 |
} |
| 176 |
|
| 177 |
if (!output_name ) { |
| 178 |
fprintf(stderr,"Output name required, not supplied.\n"); |
| 179 |
print_usage_message(usage); |
| 180 |
exit(EXIT_FAILURE); |
| 181 |
} |
| 182 |
fileOut = fopen(output_name,"w"); |
| 183 |
if (!fileOut) { |
| 184 |
fprintf(stderr,"Invalid output filename," |
| 185 |
" could not open: '%s'\n", |
| 186 |
output_name); |
| 187 |
print_usage_message(usage); |
| 188 |
exit(EXIT_FAILURE); |
| 189 |
} |
| 190 |
if ((standard_flag && extended_flag) || |
| 191 |
(!standard_flag && !extended_flag)) { |
| 192 |
fprintf(stderr,"Invalid table type\n"); |
| 193 |
fprintf(stderr,"Choose -e or -s .\n"); |
| 194 |
print_usage_message(usage); |
| 195 |
exit(EXIT_FAILURE); |
| 196 |
} |
| 197 |
if (standard_flag) { |
| 198 |
table_type = AF_STD; |
| 199 |
structname = "dd_threekey_tt_std"; |
| 200 |
} else { |
| 201 |
table_type = AF_EXTEN; |
| 202 |
structname = "dd_threekey_tt_ext"; |
| 203 |
} |
| 204 |
input_eof = read_value(&num,fileInp); /* 0xffffffff */ |
| 205 |
if (IS_EOF == input_eof) { |
| 206 |
bad_line_input("Empty input file"); |
| 207 |
} |
| 208 |
if (num != MAGIC_TOKEN_VALUE) { |
| 209 |
bad_line_input("Expected 0xffffffff"); |
| 210 |
} |
| 211 |
|
| 212 |
/* Generate main header, regardless of contents */ |
| 213 |
fprintf(fileOut,"/* Generated code, do not edit. */\n"); |
| 214 |
fprintf(fileOut,"/* Generated for source version %s */\n", |
| 215 |
PACKAGE_VERSION ); |
| 216 |
fprintf(fileOut,"\n/* BEGIN FILE */\n\n"); |
| 217 |
fprintf(fileOut,"struct Three_Key_Entry_s %s [] = {\n", |
| 218 |
structname); |
| 219 |
|
| 220 |
while (!feof(stdin)) { |
| 221 |
unsigned int tag = 0; |
| 222 |
const char *name = 0; |
| 223 |
|
| 224 |
input_eof = read_value(&tag,fileInp); |
| 225 |
if (IS_EOF == input_eof) { |
| 226 |
/* Reached normal eof */ |
| 227 |
break; |
| 228 |
} |
| 229 |
input_eof = read_value(&num,fileInp); |
| 230 |
if (IS_EOF == input_eof) { |
| 231 |
bad_line_input("Not terminated correctly.."); |
| 232 |
} |
| 233 |
ta_get_TAG_name(tag,&name); |
| 234 |
fprintf(fileOut,"/* 0x%02x - %-37s*/\n", tag, name); |
| 235 |
|
| 236 |
while (num != MAGIC_TOKEN_VALUE) { |
| 237 |
/* print a 3key */ |
| 238 |
fprintf(fileOut,"{0x%04x,0x%04x,%u,%d,0,0},\n", |
| 239 |
(Dwarf_Half)tag,(Dwarf_Half)num,(Dwarf_Half)0, |
| 240 |
table_type); |
| 241 |
input_eof = read_value(&num,fileInp); |
| 242 |
if (IS_EOF == input_eof) { |
| 243 |
bad_line_input("Not terminated correctly."); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
fprintf(fileOut,"{0,0,0,0,0,0}};"); |
| 248 |
fprintf(fileOut,"\n/* END FILE */\n"); |
| 249 |
fclose(fileInp); |
| 250 |
fclose(fileOut); |
| 251 |
return (0); |
| 252 |
} |
| 253 |
|
| 254 |
/* A fake so we can use dwarf_names.c */ |
| 255 |
void print_error (Dwarf_Debug dbg, |
| 256 |
const char *msg, |
| 257 |
int res, |
| 258 |
Dwarf_Error localerr) |
| 259 |
{ |
| 260 |
(void)dbg; |
| 261 |
(void)msg; |
| 262 |
(void)res; |
| 263 |
(void)localerr;; |
| 264 |
} |