4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1996-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
37 boolean_t error
= B_FALSE
;
40 extern void forth_do_sou(struct tdesc
*, struct node
*);
41 extern void forth_do_enum(struct tdesc
*, struct node
*);
42 extern void forth_do_intrinsic(struct tdesc
*, struct node
*);
43 extern void genassym_do_sou(struct tdesc
*, struct node
*);
44 extern void genassym_do_enum(struct tdesc
*, struct node
*);
45 extern void genassym_do_intrinsic(struct tdesc
*, struct node
*);
46 extern void squander_do_sou(struct tdesc
*, struct node
*);
47 extern void squander_do_enum(struct tdesc
*, struct node
*);
48 extern void squander_do_intrinsic(struct tdesc
*, struct node
*);
50 struct model_info models
[] = {
51 { "ilp32", 4, 1, 2, 4, 4 },
52 { "lp64", 8, 1, 2, 4, 8 },
58 void (*do_sou
)(struct tdesc
*, struct node
*);
59 void (*do_enum
)(struct tdesc
*, struct node
*);
60 void (*do_intrinsic
)(struct tdesc
*, struct node
*);
63 forth_do_sou
, forth_do_enum
, forth_do_intrinsic
},
65 genassym_do_sou
, genassym_do_enum
, genassym_do_intrinsic
},
67 squander_do_sou
, squander_do_enum
, squander_do_intrinsic
},
71 static void get_dbgs(int argc
, char **argv
);
72 static void parse_dbg(FILE *sp
);
73 static void printnode(struct node
*np
);
74 static struct tdesc
*find_member(struct tdesc
*tdp
, char *name
);
75 static char *namex(char *cp
, char **w
);
76 static void addchild(char *cp
, struct node
*np
);
77 static struct node
*getnode(char *cp
);
80 struct model_info
*model
;
83 main(int argc
, char **argv
)
85 char *output_type
= NULL
;
86 char *model_name
= NULL
;
89 program
= strrchr(argv
[0], '/');
96 output_type
= "forth";
99 while (!error
&& ((c
= getopt(argc
, argv
, "dt:m:")) != EOF
)) {
102 output_type
= optarg
;
119 * Find ops for the specified output type
121 for (ops
= ops_table
; ops
->type
!= NULL
; ops
++) {
122 if (strcmp(ops
->type
, output_type
) == 0)
125 if (ops
->type
== NULL
)
131 * Find model characteristics
133 for (model
= models
; model
->name
!= NULL
; model
++) {
134 if (strcmp(model
->name
, model_name
) == 0)
137 if (model
->name
== NULL
)
141 /* skip over previously processed arguments */
148 fprintf(stderr
, "Usage: %s [-d] {-m datamodel} "
149 "{-t output_type} files\n", program
);
150 fprintf(stderr
, "\tSupported data models:\n");
151 for (model
= models
; model
->name
!= NULL
; model
++)
152 fprintf(stderr
, "\t\t%s\n", model
->name
);
153 fprintf(stderr
, "\tSupported output types:\n");
154 for (ops
= ops_table
; ops
->type
!= NULL
; ops
++)
155 fprintf(stderr
, "\t\t%s\n", ops
->type
);
161 get_dbgs(argc
, argv
);
163 return (error
? 1 : 0);
167 * This routine will read the .dbg files and build a list of the structures
168 * and fields that user is interested in. Any struct specified will get all
169 * its fields included. If nested struct needs to be printed - then the
170 * field name and name of struct type needs to be included in the next line.
173 get_dbgs(int argc
, char **argv
)
177 for (; argc
!= 0; argc
--, argv
++) {
178 if ((fp
= fopen(*argv
, "r")) == NULL
) {
179 fprintf(stderr
, "Cannot open %s\n", *argv
);
183 /* add all types in this file to our table */
189 namex(char *cp
, char **w
)
199 for (c
= *cp
++; isspace(c
); c
= *cp
++)
203 if (isalpha(c
) || ispunct(c
)) {
204 for (c
= *cp
++; isalnum(c
) || ispunct(c
); c
= *cp
++)
207 new = (char *)malloc(len
);
208 while (orig
< cp
- 1)
211 *w
= new - (len
- 1);
212 } else if (c
!= '\0') {
213 fprintf(stderr
, "line %d has bad character %c\n", line
, c
);
221 * checks to see if this field in the struct was requested for by user
225 find_child(struct node
*np
, char *w
)
229 for (chp
= np
->child
; chp
!= NULL
; chp
= chp
->next
) {
230 if (strcmp(chp
->name
, w
) == 0)
236 static struct tdesc
*
237 find_member(struct tdesc
*tdp
, char *name
)
241 while (tdp
->type
== TYPEOF
)
242 tdp
= tdp
->data
.tdesc
;
243 if (tdp
->type
!= STRUCT
&& tdp
->type
!= UNION
)
245 for (mlp
= tdp
->data
.members
.forw
; mlp
!= NULL
; mlp
= mlp
->next
)
246 if (strcmp(mlp
->name
, name
) == 0)
252 * add this field to our table of structs/fields that the user has
253 * requested in the .dbg files
256 addchild(char *cp
, struct node
*np
)
261 chp
= malloc(sizeof (*chp
));
266 if (chp
->name
== NULL
) {
267 fprintf(stderr
, "NULL child name\n");
270 /* XXX - always convert to upper-case? */
271 chp
->format
= uc(chp
->name
);
275 chp
->next
= np
->child
;
280 * add this struct to our table of structs/fields that the user has
281 * requested in the .dbg files
290 np
= malloc(sizeof (*np
));
294 * XXX - These positional parameters are a hack
295 * We have two right now for genassym. The back-ends
296 * can use format and format2 any way they'd like.
312 * Format for .dbg files should be
316 * if you wanted the contents of "s_as" (a pointer) to be printed in
317 * the format of a "as"
324 static char linebuf
[MAXLINE
];
329 /* grab each line and add them to our table */
330 for (line
= 1; (cp
= fgets(linebuf
, MAXLINE
, sp
)) != NULL
; line
++) {
338 printf("%s", (cp
+ 1));
341 if (strcmp(cp
, "model_end\n") == 0) {
348 c
= strlen("model_start ");
349 if (strncmp(cp
, "model_start ", c
) == 0) {
350 if (strncmp(cp
+ c
, model
->name
, strlen(model
->name
))
351 == 0 && *(cp
+ c
+ strlen(model
->name
)) == '\n')
357 if ((strcmp(cp
, "verbatim_begin\n") == 0) ||
358 (strcmp(cp
, "forth_start\n") == 0)) {
362 if ((strcmp(cp
, "verbatim_end\n") == 0) ||
363 (strcmp(cp
, "forth_end\n") == 0)) {
373 ((cp
= fgets(linebuf
, MAXLINE
, sp
)) != NULL
) &&
374 *cp
!= '\n'; line
++) {
375 /* members of struct, union or enum */
383 printnode(struct node
*np
)
387 tdp
= lookupname(np
->name
);
392 if ((member
= strchr(np
->name
, '.')) != NULL
) {
394 ptdp
= lookupname(np
->name
);
396 tdp
= find_member(ptdp
, member
+ 1);
400 fprintf(stderr
, "Can't find %s\n", np
->name
);
409 ops
->do_sou(tdp
, np
);
412 ops
->do_enum(tdp
, np
);
415 tdp
= tdp
->data
.tdesc
;
418 ops
->do_intrinsic(tdp
, np
);
421 fprintf(stderr
, "%s isn't aggregate\n", np
->name
);
429 convert_format(char *format
, char *dfault
)
431 static char dot
[3] = ".";
435 else if (strlen(format
) == 1) {
449 for (i
= 0; i
< strlen(buf
); i
++)
450 buf
[i
] = toupper(buf
[i
]);