1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Read in "high-level" adb script and emit C program. 31*7c478bd9Sstevel@tonic-gate * The input may have specifications within {} which 32*7c478bd9Sstevel@tonic-gate * we analyze and then emit C code to generate the 33*7c478bd9Sstevel@tonic-gate * ultimate adb acript. 34*7c478bd9Sstevel@tonic-gate * We are just a filter; no arguments are accepted. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <string.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #define streq(s1, s2) (strcmp(s1, s2) == 0) 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate #define LINELEN 1024 /* max line length expected in input */ 44*7c478bd9Sstevel@tonic-gate #define STRLEN 128 /* for shorter strings */ 45*7c478bd9Sstevel@tonic-gate #define NARGS 5 /* number of emitted subroutine arguments */ 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate /* 48*7c478bd9Sstevel@tonic-gate * Format specifier strings 49*7c478bd9Sstevel@tonic-gate * which are recognized by adbgen when surrounded by {} 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate #define FSTR_PTR "POINTER" 52*7c478bd9Sstevel@tonic-gate #define FSTR_LONG_DEC "LONGDEC" 53*7c478bd9Sstevel@tonic-gate #define FSTR_LONG_OCT "LONGOCT" 54*7c478bd9Sstevel@tonic-gate #define FSTR_ULONG_DEC "ULONGDEC" 55*7c478bd9Sstevel@tonic-gate #define FSTR_ULONG_HEX "ULONGHEX" 56*7c478bd9Sstevel@tonic-gate #define FSTR_ULONG_OCT "ULONGOCT" 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* 59*7c478bd9Sstevel@tonic-gate * Types of specifications in {}. 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate #define PTR_HEX 0 /* emit hex pointer format char */ 62*7c478bd9Sstevel@tonic-gate #define LONG_DEC 1 /* emit decimal long format char */ 63*7c478bd9Sstevel@tonic-gate #define LONG_OCT 2 /* emit octal unsigned long format char */ 64*7c478bd9Sstevel@tonic-gate #define ULONG_DEC 3 /* emit decimal unsigned long format char */ 65*7c478bd9Sstevel@tonic-gate #define ULONG_HEX 4 /* emit hexadecimal long format char */ 66*7c478bd9Sstevel@tonic-gate #define ULONG_OCT 5 /* emit octal unsigned long format char */ 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate #define FMT_ENTRIES 6 /* number of adbgen format specifier strings */ 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate #define PRINT 6 /* print member name with format */ 71*7c478bd9Sstevel@tonic-gate #define INDIRECT 7 /* fetch member value */ 72*7c478bd9Sstevel@tonic-gate #define OFFSETOK 8 /* insist that the offset is ok */ 73*7c478bd9Sstevel@tonic-gate #define SIZEOF 9 /* print sizeof struct */ 74*7c478bd9Sstevel@tonic-gate #define END 10 /* get offset to end of struct */ 75*7c478bd9Sstevel@tonic-gate #define OFFSET 11 /* just emit offset */ 76*7c478bd9Sstevel@tonic-gate #define EXPR 12 /* arbitrary C expression */ 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate /* 79*7c478bd9Sstevel@tonic-gate * Special return code from nextchar. 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate #define CPP -2 /* cpp line, restart parsing */ 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate typedef struct adbgen_fmt { 84*7c478bd9Sstevel@tonic-gate char *f_str; 85*7c478bd9Sstevel@tonic-gate char f_char; 86*7c478bd9Sstevel@tonic-gate } adbgen_fmt_t; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate char struct_name[STRLEN]; /* struct name */ 89*7c478bd9Sstevel@tonic-gate char member[STRLEN]; /* member name */ 90*7c478bd9Sstevel@tonic-gate char format[STRLEN]; /* adb format spec */ 91*7c478bd9Sstevel@tonic-gate char arg[NARGS][STRLEN]; /* arg list for called subroutine */ 92*7c478bd9Sstevel@tonic-gate char *ptr_hex_fmt; /* adb format character for pointer in hex */ 93*7c478bd9Sstevel@tonic-gate char *long_dec_fmt; /* adb format character for long in decimal */ 94*7c478bd9Sstevel@tonic-gate char *ulong_dec_fmt; /* adb format character for ulong in decimal */ 95*7c478bd9Sstevel@tonic-gate char *ulong_hex_fmt; /* adb format character for ulong in hex */ 96*7c478bd9Sstevel@tonic-gate char *long_oct_fmt; /* adb format character for long in octal */ 97*7c478bd9Sstevel@tonic-gate char *ulong_oct_fmt; /* adb format character for ulong in octal */ 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate int line_no = 1; /* input line number - for error messages */ 100*7c478bd9Sstevel@tonic-gate int specsize; /* size of {} specification - 1 or 2 parts */ 101*7c478bd9Sstevel@tonic-gate int state; /* XXX 1 = gathering a printf */ 102*7c478bd9Sstevel@tonic-gate /* This is a kludge so we emit pending */ 103*7c478bd9Sstevel@tonic-gate /* printf's when we see a CPP line */ 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate adbgen_fmt_t adbgen_fmt_tbl [FMT_ENTRIES] = { 106*7c478bd9Sstevel@tonic-gate {FSTR_PTR}, 107*7c478bd9Sstevel@tonic-gate {FSTR_LONG_DEC}, 108*7c478bd9Sstevel@tonic-gate {FSTR_LONG_OCT}, 109*7c478bd9Sstevel@tonic-gate {FSTR_ULONG_DEC}, 110*7c478bd9Sstevel@tonic-gate {FSTR_ULONG_HEX}, 111*7c478bd9Sstevel@tonic-gate {FSTR_ULONG_OCT} 112*7c478bd9Sstevel@tonic-gate }; 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate void emit_call(char *name, int nargs); 115*7c478bd9Sstevel@tonic-gate void emit_end(void); 116*7c478bd9Sstevel@tonic-gate void emit_expr(void); 117*7c478bd9Sstevel@tonic-gate void emit_indirect(void); 118*7c478bd9Sstevel@tonic-gate void emit_offset(void); 119*7c478bd9Sstevel@tonic-gate void emit_offsetok(void); 120*7c478bd9Sstevel@tonic-gate void emit_print(void); 121*7c478bd9Sstevel@tonic-gate void emit_printf(char *cp); 122*7c478bd9Sstevel@tonic-gate void emit_sizeof(void); 123*7c478bd9Sstevel@tonic-gate void generate(void); 124*7c478bd9Sstevel@tonic-gate int get_type(void); 125*7c478bd9Sstevel@tonic-gate int nextchar(char *cp); 126*7c478bd9Sstevel@tonic-gate void read_spec(void); 127*7c478bd9Sstevel@tonic-gate char *start_printf(void); 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate int 130*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 131*7c478bd9Sstevel@tonic-gate { 132*7c478bd9Sstevel@tonic-gate char *cp; 133*7c478bd9Sstevel@tonic-gate int c; 134*7c478bd9Sstevel@tonic-gate int warn_flag = 0; 135*7c478bd9Sstevel@tonic-gate int is_lp64 = 0; 136*7c478bd9Sstevel@tonic-gate char *usage = "adbgen1 [-w] [-m ilp32|lp64] < <macro file>\n"; 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "m:w")) != EOF) { 139*7c478bd9Sstevel@tonic-gate switch (c) { 140*7c478bd9Sstevel@tonic-gate case 'm': 141*7c478bd9Sstevel@tonic-gate if (streq(optarg, "ilp32")) 142*7c478bd9Sstevel@tonic-gate is_lp64 = 0; 143*7c478bd9Sstevel@tonic-gate else if (streq(optarg, "lp64")) 144*7c478bd9Sstevel@tonic-gate is_lp64 = 1; 145*7c478bd9Sstevel@tonic-gate else 146*7c478bd9Sstevel@tonic-gate fprintf(stderr, usage); 147*7c478bd9Sstevel@tonic-gate break; 148*7c478bd9Sstevel@tonic-gate case 'w': 149*7c478bd9Sstevel@tonic-gate warn_flag++; 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate case '?': 152*7c478bd9Sstevel@tonic-gate fprintf(stderr, usage); 153*7c478bd9Sstevel@tonic-gate break; 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate if (is_lp64) { 157*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[PTR_HEX].f_char = 'J'; 158*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[LONG_DEC].f_char = 'e'; 159*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[LONG_OCT].f_char = 'g'; 160*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_DEC].f_char = 'E'; 161*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_HEX].f_char = 'J'; 162*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_OCT].f_char = 'G'; 163*7c478bd9Sstevel@tonic-gate } else { 164*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[PTR_HEX].f_char = 'X'; 165*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[LONG_DEC].f_char = 'D'; 166*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[LONG_OCT].f_char = 'Q'; 167*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_DEC].f_char = 'U'; 168*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_HEX].f_char = 'X'; 169*7c478bd9Sstevel@tonic-gate adbgen_fmt_tbl[ULONG_OCT].f_char = 'O'; 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate /* 173*7c478bd9Sstevel@tonic-gate * Get structure name. 174*7c478bd9Sstevel@tonic-gate */ 175*7c478bd9Sstevel@tonic-gate cp = struct_name; 176*7c478bd9Sstevel@tonic-gate while ((c = nextchar(NULL)) != '\n') { 177*7c478bd9Sstevel@tonic-gate if (c == EOF) { 178*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Premature EOF\n"); 179*7c478bd9Sstevel@tonic-gate exit(1); 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate if (c == CPP) 182*7c478bd9Sstevel@tonic-gate continue; 183*7c478bd9Sstevel@tonic-gate *cp++ = (char)c; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate *cp = '\0'; 186*7c478bd9Sstevel@tonic-gate /* 187*7c478bd9Sstevel@tonic-gate * Basically, the generated program is just an ongoing printf 188*7c478bd9Sstevel@tonic-gate * with breaks for {} format specifications. 189*7c478bd9Sstevel@tonic-gate */ 190*7c478bd9Sstevel@tonic-gate printf("\n"); 191*7c478bd9Sstevel@tonic-gate printf("#include <sys/types.h>\n"); 192*7c478bd9Sstevel@tonic-gate printf("#include <sys/inttypes.h>\n"); 193*7c478bd9Sstevel@tonic-gate printf("\n\n"); 194*7c478bd9Sstevel@tonic-gate printf("int do_fmt(char *acp);\n"); 195*7c478bd9Sstevel@tonic-gate printf("void format(char *name, size_t size, char *fmt);\n"); 196*7c478bd9Sstevel@tonic-gate printf("void indirect(off_t offset, size_t size, " 197*7c478bd9Sstevel@tonic-gate "char *base, char *member);\n"); 198*7c478bd9Sstevel@tonic-gate printf("void offset(off_t off);\n"); 199*7c478bd9Sstevel@tonic-gate printf("void offsetok(void);\n"); 200*7c478bd9Sstevel@tonic-gate printf("\n\n"); 201*7c478bd9Sstevel@tonic-gate printf("main(int argc, char *argv[])\n"); 202*7c478bd9Sstevel@tonic-gate printf("{\n"); 203*7c478bd9Sstevel@tonic-gate if (warn_flag) { 204*7c478bd9Sstevel@tonic-gate printf("\textern int warnings;\n\n\twarnings = 0;\n"); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate cp = start_printf(); 207*7c478bd9Sstevel@tonic-gate while ((c = nextchar(cp)) != EOF) { 208*7c478bd9Sstevel@tonic-gate switch (c) { 209*7c478bd9Sstevel@tonic-gate case '"': 210*7c478bd9Sstevel@tonic-gate *cp++ = '\\'; /* escape ' in string */ 211*7c478bd9Sstevel@tonic-gate *cp++ = '"'; 212*7c478bd9Sstevel@tonic-gate break; 213*7c478bd9Sstevel@tonic-gate case '\n': 214*7c478bd9Sstevel@tonic-gate *cp++ = '\\'; /* escape newline in string */ 215*7c478bd9Sstevel@tonic-gate *cp++ = 'n'; 216*7c478bd9Sstevel@tonic-gate break; 217*7c478bd9Sstevel@tonic-gate case '{': 218*7c478bd9Sstevel@tonic-gate emit_printf(cp); 219*7c478bd9Sstevel@tonic-gate read_spec(); 220*7c478bd9Sstevel@tonic-gate generate(); 221*7c478bd9Sstevel@tonic-gate cp = start_printf(); 222*7c478bd9Sstevel@tonic-gate break; 223*7c478bd9Sstevel@tonic-gate case CPP: 224*7c478bd9Sstevel@tonic-gate /* 225*7c478bd9Sstevel@tonic-gate * Restart printf after cpp line. 226*7c478bd9Sstevel@tonic-gate */ 227*7c478bd9Sstevel@tonic-gate cp = start_printf(); 228*7c478bd9Sstevel@tonic-gate break; 229*7c478bd9Sstevel@tonic-gate default: 230*7c478bd9Sstevel@tonic-gate *cp++ = c; 231*7c478bd9Sstevel@tonic-gate break; 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate if (cp - arg[1] >= STRLEN - 10) { 234*7c478bd9Sstevel@tonic-gate emit_printf(cp); 235*7c478bd9Sstevel@tonic-gate cp = start_printf(); 236*7c478bd9Sstevel@tonic-gate } 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate emit_printf(cp); 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate /* terminate program, checking for "error" mode */ 241*7c478bd9Sstevel@tonic-gate printf("\n\tif (argc > 1 && strcmp(argv[1], \"-e\") == 0) {\n"); 242*7c478bd9Sstevel@tonic-gate printf("\t\textern int warns;\n\n"); 243*7c478bd9Sstevel@tonic-gate printf("\t\tif (warns)\n"); 244*7c478bd9Sstevel@tonic-gate printf("\t\t\treturn (1);\n"); 245*7c478bd9Sstevel@tonic-gate printf("\t}\n"); 246*7c478bd9Sstevel@tonic-gate printf("\treturn (0);\n"); 247*7c478bd9Sstevel@tonic-gate printf("}\n"); 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate return (0); 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate int 253*7c478bd9Sstevel@tonic-gate nextchar(char *cp) 254*7c478bd9Sstevel@tonic-gate { 255*7c478bd9Sstevel@tonic-gate int c; 256*7c478bd9Sstevel@tonic-gate static int newline = 1; 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate c = getchar(); 259*7c478bd9Sstevel@tonic-gate /* 260*7c478bd9Sstevel@tonic-gate * Lines beginning with '#' and blank lines are passed right through. 261*7c478bd9Sstevel@tonic-gate */ 262*7c478bd9Sstevel@tonic-gate while (newline) { 263*7c478bd9Sstevel@tonic-gate switch (c) { 264*7c478bd9Sstevel@tonic-gate case '#': 265*7c478bd9Sstevel@tonic-gate if (state) 266*7c478bd9Sstevel@tonic-gate emit_printf(cp); 267*7c478bd9Sstevel@tonic-gate do { 268*7c478bd9Sstevel@tonic-gate putchar(c); 269*7c478bd9Sstevel@tonic-gate c = getchar(); 270*7c478bd9Sstevel@tonic-gate if (c == EOF) 271*7c478bd9Sstevel@tonic-gate return (c); 272*7c478bd9Sstevel@tonic-gate } while (c != '\n'); 273*7c478bd9Sstevel@tonic-gate putchar(c); 274*7c478bd9Sstevel@tonic-gate line_no++; 275*7c478bd9Sstevel@tonic-gate return (CPP); 276*7c478bd9Sstevel@tonic-gate case '\n': 277*7c478bd9Sstevel@tonic-gate if (state) 278*7c478bd9Sstevel@tonic-gate emit_printf(cp); 279*7c478bd9Sstevel@tonic-gate putchar(c); 280*7c478bd9Sstevel@tonic-gate c = getchar(); 281*7c478bd9Sstevel@tonic-gate line_no++; 282*7c478bd9Sstevel@tonic-gate break; 283*7c478bd9Sstevel@tonic-gate default: 284*7c478bd9Sstevel@tonic-gate newline = 0; 285*7c478bd9Sstevel@tonic-gate break; 286*7c478bd9Sstevel@tonic-gate } 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate if (c == '\n') { 289*7c478bd9Sstevel@tonic-gate newline++; 290*7c478bd9Sstevel@tonic-gate line_no++; 291*7c478bd9Sstevel@tonic-gate } 292*7c478bd9Sstevel@tonic-gate return (c); 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate /* 296*7c478bd9Sstevel@tonic-gate * Get started on printf of ongoing adb script. 297*7c478bd9Sstevel@tonic-gate */ 298*7c478bd9Sstevel@tonic-gate char * 299*7c478bd9Sstevel@tonic-gate start_printf(void) 300*7c478bd9Sstevel@tonic-gate { 301*7c478bd9Sstevel@tonic-gate char *cp; 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate strcpy(arg[0], "\"%s\""); 304*7c478bd9Sstevel@tonic-gate cp = arg[1]; 305*7c478bd9Sstevel@tonic-gate *cp++ = '"'; 306*7c478bd9Sstevel@tonic-gate state = 1; /* XXX */ 307*7c478bd9Sstevel@tonic-gate return (cp); 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate 310*7c478bd9Sstevel@tonic-gate /* 311*7c478bd9Sstevel@tonic-gate * Emit call to printf to print part of ongoing adb script. 312*7c478bd9Sstevel@tonic-gate */ 313*7c478bd9Sstevel@tonic-gate void 314*7c478bd9Sstevel@tonic-gate emit_printf(cp) 315*7c478bd9Sstevel@tonic-gate char *cp; 316*7c478bd9Sstevel@tonic-gate { 317*7c478bd9Sstevel@tonic-gate *cp++ = '"'; 318*7c478bd9Sstevel@tonic-gate *cp = '\0'; 319*7c478bd9Sstevel@tonic-gate emit_call("printf", 2); 320*7c478bd9Sstevel@tonic-gate state = 0; /* XXX */ 321*7c478bd9Sstevel@tonic-gate } 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate /* 324*7c478bd9Sstevel@tonic-gate * Read {} specification. 325*7c478bd9Sstevel@tonic-gate * The first part (up to a comma) is put into "member". 326*7c478bd9Sstevel@tonic-gate * The second part, if present, is put into "format". 327*7c478bd9Sstevel@tonic-gate */ 328*7c478bd9Sstevel@tonic-gate void 329*7c478bd9Sstevel@tonic-gate read_spec(void) 330*7c478bd9Sstevel@tonic-gate { 331*7c478bd9Sstevel@tonic-gate char *cp; 332*7c478bd9Sstevel@tonic-gate int c; 333*7c478bd9Sstevel@tonic-gate int nesting; 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate cp = member; 336*7c478bd9Sstevel@tonic-gate specsize = 1; 337*7c478bd9Sstevel@tonic-gate nesting = 0; 338*7c478bd9Sstevel@tonic-gate while ((c = nextchar(NULL)) != '}' || (c == '}' && nesting)) { 339*7c478bd9Sstevel@tonic-gate switch (c) { 340*7c478bd9Sstevel@tonic-gate case EOF: 341*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Unexpected EOF inside {}\n"); 342*7c478bd9Sstevel@tonic-gate exit(1); 343*7c478bd9Sstevel@tonic-gate case '\n': 344*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Newline not allowed in {}, line %d\n", 345*7c478bd9Sstevel@tonic-gate line_no); 346*7c478bd9Sstevel@tonic-gate exit(1); 347*7c478bd9Sstevel@tonic-gate case '#': 348*7c478bd9Sstevel@tonic-gate fprintf(stderr, "# not allowed in {}, line %d\n", 349*7c478bd9Sstevel@tonic-gate line_no); 350*7c478bd9Sstevel@tonic-gate exit(1); 351*7c478bd9Sstevel@tonic-gate case ',': 352*7c478bd9Sstevel@tonic-gate if (specsize == 2) { 353*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Excessive commas in {}, "); 354*7c478bd9Sstevel@tonic-gate fprintf(stderr, "line %d\n", line_no); 355*7c478bd9Sstevel@tonic-gate exit(1); 356*7c478bd9Sstevel@tonic-gate } 357*7c478bd9Sstevel@tonic-gate specsize = 2; 358*7c478bd9Sstevel@tonic-gate *cp = '\0'; 359*7c478bd9Sstevel@tonic-gate cp = format; 360*7c478bd9Sstevel@tonic-gate break; 361*7c478bd9Sstevel@tonic-gate case '{': 362*7c478bd9Sstevel@tonic-gate /* 363*7c478bd9Sstevel@tonic-gate * Allow up to one set of nested {}'s for adbgen 364*7c478bd9Sstevel@tonic-gate * requests of the form {member, {format string}} 365*7c478bd9Sstevel@tonic-gate */ 366*7c478bd9Sstevel@tonic-gate if (!nesting) { 367*7c478bd9Sstevel@tonic-gate nesting = 1; 368*7c478bd9Sstevel@tonic-gate *cp++ = c; 369*7c478bd9Sstevel@tonic-gate } else { 370*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Too many {'s, line %d\n", 371*7c478bd9Sstevel@tonic-gate line_no); 372*7c478bd9Sstevel@tonic-gate exit(1); 373*7c478bd9Sstevel@tonic-gate } 374*7c478bd9Sstevel@tonic-gate break; 375*7c478bd9Sstevel@tonic-gate case '}': 376*7c478bd9Sstevel@tonic-gate *cp++ = c; 377*7c478bd9Sstevel@tonic-gate nesting = 0; 378*7c478bd9Sstevel@tonic-gate break; 379*7c478bd9Sstevel@tonic-gate default: 380*7c478bd9Sstevel@tonic-gate *cp++ = c; 381*7c478bd9Sstevel@tonic-gate break; 382*7c478bd9Sstevel@tonic-gate } 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate *cp = '\0'; 385*7c478bd9Sstevel@tonic-gate if (cp == member) { 386*7c478bd9Sstevel@tonic-gate specsize = 0; 387*7c478bd9Sstevel@tonic-gate } 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate /* 391*7c478bd9Sstevel@tonic-gate * Decide what type of input specification we have. 392*7c478bd9Sstevel@tonic-gate */ 393*7c478bd9Sstevel@tonic-gate int 394*7c478bd9Sstevel@tonic-gate get_type(void) 395*7c478bd9Sstevel@tonic-gate { 396*7c478bd9Sstevel@tonic-gate int i; 397*7c478bd9Sstevel@tonic-gate 398*7c478bd9Sstevel@tonic-gate if (specsize == 1) { 399*7c478bd9Sstevel@tonic-gate if (streq(member, "SIZEOF")) { 400*7c478bd9Sstevel@tonic-gate return (SIZEOF); 401*7c478bd9Sstevel@tonic-gate } 402*7c478bd9Sstevel@tonic-gate if (streq(member, "OFFSETOK")) { 403*7c478bd9Sstevel@tonic-gate return (OFFSETOK); 404*7c478bd9Sstevel@tonic-gate } 405*7c478bd9Sstevel@tonic-gate if (streq(member, "END")) { 406*7c478bd9Sstevel@tonic-gate return (END); 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate for (i = 0; i < FMT_ENTRIES; i++) 409*7c478bd9Sstevel@tonic-gate if (streq(member, adbgen_fmt_tbl[i].f_str)) 410*7c478bd9Sstevel@tonic-gate return (i); 411*7c478bd9Sstevel@tonic-gate return (OFFSET); 412*7c478bd9Sstevel@tonic-gate } 413*7c478bd9Sstevel@tonic-gate if (specsize == 2) { 414*7c478bd9Sstevel@tonic-gate if (member[0] == '*') { 415*7c478bd9Sstevel@tonic-gate return (INDIRECT); 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate if (streq(member, "EXPR")) { 418*7c478bd9Sstevel@tonic-gate return (EXPR); 419*7c478bd9Sstevel@tonic-gate } 420*7c478bd9Sstevel@tonic-gate return (PRINT); 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Invalid specification, line %d\n", line_no); 423*7c478bd9Sstevel@tonic-gate exit(1); 424*7c478bd9Sstevel@tonic-gate } 425*7c478bd9Sstevel@tonic-gate 426*7c478bd9Sstevel@tonic-gate /* 427*7c478bd9Sstevel@tonic-gate * Generate the appropriate output for an input specification. 428*7c478bd9Sstevel@tonic-gate */ 429*7c478bd9Sstevel@tonic-gate void 430*7c478bd9Sstevel@tonic-gate generate(void) 431*7c478bd9Sstevel@tonic-gate { 432*7c478bd9Sstevel@tonic-gate char *cp; 433*7c478bd9Sstevel@tonic-gate int type; 434*7c478bd9Sstevel@tonic-gate 435*7c478bd9Sstevel@tonic-gate type = get_type(); 436*7c478bd9Sstevel@tonic-gate 437*7c478bd9Sstevel@tonic-gate switch (type) { 438*7c478bd9Sstevel@tonic-gate case PTR_HEX: 439*7c478bd9Sstevel@tonic-gate case LONG_DEC: 440*7c478bd9Sstevel@tonic-gate case LONG_OCT: 441*7c478bd9Sstevel@tonic-gate case ULONG_DEC: 442*7c478bd9Sstevel@tonic-gate case ULONG_HEX: 443*7c478bd9Sstevel@tonic-gate case ULONG_OCT: 444*7c478bd9Sstevel@tonic-gate cp = start_printf(); 445*7c478bd9Sstevel@tonic-gate *cp++ = adbgen_fmt_tbl[type].f_char; 446*7c478bd9Sstevel@tonic-gate emit_printf(cp); 447*7c478bd9Sstevel@tonic-gate break; 448*7c478bd9Sstevel@tonic-gate case PRINT: 449*7c478bd9Sstevel@tonic-gate emit_print(); 450*7c478bd9Sstevel@tonic-gate break; 451*7c478bd9Sstevel@tonic-gate case OFFSET: 452*7c478bd9Sstevel@tonic-gate emit_offset(); 453*7c478bd9Sstevel@tonic-gate break; 454*7c478bd9Sstevel@tonic-gate case INDIRECT: 455*7c478bd9Sstevel@tonic-gate emit_indirect(); 456*7c478bd9Sstevel@tonic-gate break; 457*7c478bd9Sstevel@tonic-gate case OFFSETOK: 458*7c478bd9Sstevel@tonic-gate emit_offsetok(); 459*7c478bd9Sstevel@tonic-gate break; 460*7c478bd9Sstevel@tonic-gate case SIZEOF: 461*7c478bd9Sstevel@tonic-gate emit_sizeof(); 462*7c478bd9Sstevel@tonic-gate break; 463*7c478bd9Sstevel@tonic-gate case EXPR: 464*7c478bd9Sstevel@tonic-gate emit_expr(); 465*7c478bd9Sstevel@tonic-gate break; 466*7c478bd9Sstevel@tonic-gate case END: 467*7c478bd9Sstevel@tonic-gate emit_end(); 468*7c478bd9Sstevel@tonic-gate break; 469*7c478bd9Sstevel@tonic-gate default: 470*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Internal error in generate\n"); 471*7c478bd9Sstevel@tonic-gate exit(1); 472*7c478bd9Sstevel@tonic-gate } 473*7c478bd9Sstevel@tonic-gate } 474*7c478bd9Sstevel@tonic-gate 475*7c478bd9Sstevel@tonic-gate /* 476*7c478bd9Sstevel@tonic-gate * Emit calls to set the offset and print a member. 477*7c478bd9Sstevel@tonic-gate */ 478*7c478bd9Sstevel@tonic-gate void 479*7c478bd9Sstevel@tonic-gate emit_print(void) 480*7c478bd9Sstevel@tonic-gate { 481*7c478bd9Sstevel@tonic-gate char *cp; 482*7c478bd9Sstevel@tonic-gate char fmt_request[STRLEN]; 483*7c478bd9Sstevel@tonic-gate int i; 484*7c478bd9Sstevel@tonic-gate char number[STRLEN]; 485*7c478bd9Sstevel@tonic-gate 486*7c478bd9Sstevel@tonic-gate emit_offset(); 487*7c478bd9Sstevel@tonic-gate /* 488*7c478bd9Sstevel@tonic-gate * Emit call to "format" subroutine 489*7c478bd9Sstevel@tonic-gate */ 490*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "\"%s\"", member); 491*7c478bd9Sstevel@tonic-gate sprintf(arg[1], "sizeof ((struct %s *)0)->%s", 492*7c478bd9Sstevel@tonic-gate struct_name, member); 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate /* 495*7c478bd9Sstevel@tonic-gate * Split the format string into <number><format character string> 496*7c478bd9Sstevel@tonic-gate * This is for format strings that contain format specifier requests 497*7c478bd9Sstevel@tonic-gate * like {POINTER_HEX}, {LONG_DEC}, etc. which need to be substituted 498*7c478bd9Sstevel@tonic-gate * with a format character instead. 499*7c478bd9Sstevel@tonic-gate */ 500*7c478bd9Sstevel@tonic-gate for (cp = format, i = 0; *cp >= '0' && *cp <= '9' && *cp != '\0'; 501*7c478bd9Sstevel@tonic-gate cp++, i++) 502*7c478bd9Sstevel@tonic-gate number[i] = *cp; 503*7c478bd9Sstevel@tonic-gate number[i] = '\0'; 504*7c478bd9Sstevel@tonic-gate 505*7c478bd9Sstevel@tonic-gate for (i = 0; i < FMT_ENTRIES; i++) { 506*7c478bd9Sstevel@tonic-gate (void) sprintf(fmt_request, "{%s}", adbgen_fmt_tbl[i].f_str); 507*7c478bd9Sstevel@tonic-gate if (streq(cp, fmt_request)) { 508*7c478bd9Sstevel@tonic-gate sprintf(arg[2], "\"%s%c\"", 509*7c478bd9Sstevel@tonic-gate number, adbgen_fmt_tbl[i].f_char); 510*7c478bd9Sstevel@tonic-gate break; 511*7c478bd9Sstevel@tonic-gate } 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate if (i == FMT_ENTRIES) 514*7c478bd9Sstevel@tonic-gate sprintf(arg[2], "\"%s\"", format); 515*7c478bd9Sstevel@tonic-gate 516*7c478bd9Sstevel@tonic-gate emit_call("format", 3); 517*7c478bd9Sstevel@tonic-gate } 518*7c478bd9Sstevel@tonic-gate 519*7c478bd9Sstevel@tonic-gate /* 520*7c478bd9Sstevel@tonic-gate * Emit calls to set the offset and print a member. 521*7c478bd9Sstevel@tonic-gate */ 522*7c478bd9Sstevel@tonic-gate void 523*7c478bd9Sstevel@tonic-gate emit_offset(void) 524*7c478bd9Sstevel@tonic-gate { 525*7c478bd9Sstevel@tonic-gate /* 526*7c478bd9Sstevel@tonic-gate * Emit call to "offset" subroutine 527*7c478bd9Sstevel@tonic-gate */ 528*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "(off_t) &(((struct %s *)0)->%s)", 529*7c478bd9Sstevel@tonic-gate struct_name, member); 530*7c478bd9Sstevel@tonic-gate emit_call("offset", 1); 531*7c478bd9Sstevel@tonic-gate } 532*7c478bd9Sstevel@tonic-gate 533*7c478bd9Sstevel@tonic-gate /* 534*7c478bd9Sstevel@tonic-gate * Emit call to indirect routine. 535*7c478bd9Sstevel@tonic-gate */ 536*7c478bd9Sstevel@tonic-gate void 537*7c478bd9Sstevel@tonic-gate emit_indirect(void) 538*7c478bd9Sstevel@tonic-gate { 539*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "(off_t) &(((struct %s *)0)->%s)", 540*7c478bd9Sstevel@tonic-gate struct_name, member+1); 541*7c478bd9Sstevel@tonic-gate sprintf(arg[1], "sizeof ((struct %s *)0)->%s", struct_name, member+1); 542*7c478bd9Sstevel@tonic-gate sprintf(arg[2], "\"%s\"", format); /* adb register name */ 543*7c478bd9Sstevel@tonic-gate sprintf(arg[3], "\"%s\"", member); 544*7c478bd9Sstevel@tonic-gate emit_call("indirect", 4); 545*7c478bd9Sstevel@tonic-gate } 546*7c478bd9Sstevel@tonic-gate 547*7c478bd9Sstevel@tonic-gate /* 548*7c478bd9Sstevel@tonic-gate * Emit call to "offsetok" routine. 549*7c478bd9Sstevel@tonic-gate */ 550*7c478bd9Sstevel@tonic-gate void 551*7c478bd9Sstevel@tonic-gate emit_offsetok(void) 552*7c478bd9Sstevel@tonic-gate { 553*7c478bd9Sstevel@tonic-gate emit_call("offsetok", 0); 554*7c478bd9Sstevel@tonic-gate } 555*7c478bd9Sstevel@tonic-gate 556*7c478bd9Sstevel@tonic-gate /* 557*7c478bd9Sstevel@tonic-gate * Emit call to printf the sizeof the structure. 558*7c478bd9Sstevel@tonic-gate */ 559*7c478bd9Sstevel@tonic-gate void 560*7c478bd9Sstevel@tonic-gate emit_sizeof(void) 561*7c478bd9Sstevel@tonic-gate { 562*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "\"0t%%d\""); 563*7c478bd9Sstevel@tonic-gate sprintf(arg[1], "sizeof (struct %s)", struct_name); 564*7c478bd9Sstevel@tonic-gate emit_call("printf", 2); 565*7c478bd9Sstevel@tonic-gate } 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate /* 568*7c478bd9Sstevel@tonic-gate * Emit call to printf an arbitrary C expression. 569*7c478bd9Sstevel@tonic-gate */ 570*7c478bd9Sstevel@tonic-gate void 571*7c478bd9Sstevel@tonic-gate emit_expr(void) 572*7c478bd9Sstevel@tonic-gate { 573*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "\"0t%%d\""); 574*7c478bd9Sstevel@tonic-gate sprintf(arg[1], "(%s)", format); 575*7c478bd9Sstevel@tonic-gate emit_call("printf", 2); 576*7c478bd9Sstevel@tonic-gate } 577*7c478bd9Sstevel@tonic-gate 578*7c478bd9Sstevel@tonic-gate /* 579*7c478bd9Sstevel@tonic-gate * Emit call to set offset to end of struct. 580*7c478bd9Sstevel@tonic-gate */ 581*7c478bd9Sstevel@tonic-gate void 582*7c478bd9Sstevel@tonic-gate emit_end(void) 583*7c478bd9Sstevel@tonic-gate { 584*7c478bd9Sstevel@tonic-gate sprintf(arg[0], "sizeof (struct %s)", struct_name); 585*7c478bd9Sstevel@tonic-gate emit_call("offset", 1); 586*7c478bd9Sstevel@tonic-gate } 587*7c478bd9Sstevel@tonic-gate 588*7c478bd9Sstevel@tonic-gate /* 589*7c478bd9Sstevel@tonic-gate * Emit call to subroutine name with nargs arguments from arg array. 590*7c478bd9Sstevel@tonic-gate */ 591*7c478bd9Sstevel@tonic-gate void 592*7c478bd9Sstevel@tonic-gate emit_call(char *name, int nargs) 593*7c478bd9Sstevel@tonic-gate { 594*7c478bd9Sstevel@tonic-gate int i; 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate printf("\t%s(", name); /* name of subroutine */ 597*7c478bd9Sstevel@tonic-gate for (i = 0; i < nargs; i++) { 598*7c478bd9Sstevel@tonic-gate if (i > 0) { 599*7c478bd9Sstevel@tonic-gate printf(", "); /* argument separator */ 600*7c478bd9Sstevel@tonic-gate } 601*7c478bd9Sstevel@tonic-gate printf("%s", arg[i]); /* argument */ 602*7c478bd9Sstevel@tonic-gate } 603*7c478bd9Sstevel@tonic-gate printf(");\n"); /* end of call */ 604*7c478bd9Sstevel@tonic-gate } 605