17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <libgen.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <sys/wait.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "itmcomp.h"
407c478bd9Sstevel@tonic-gate #include "maptype.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
437c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate #define ITMSUFFIX ".bt"
467c478bd9Sstevel@tonic-gate #define ME_DEFAULT "geniconvtbl"
477c478bd9Sstevel@tonic-gate #define CPP_PATH "/usr/lib/cpp"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate itmc_ref_t *ref_first[ITMC_OBJ_LAST + 1];
507c478bd9Sstevel@tonic-gate itmc_ref_t *ref_last[ITMC_OBJ_LAST + 1];
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate itmc_name_t *name_first;
537c478bd9Sstevel@tonic-gate itmc_name_t *name_last;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate char *itm_input_file; /* referred in itm_comp.l */
567c478bd9Sstevel@tonic-gate char *itm_output_file;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate cmd_opt_t cmd_opt;
597c478bd9Sstevel@tonic-gate itm_num_t name_id;
607c478bd9Sstevel@tonic-gate itm_num_t reg_id;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate itmc_name_t name_lookup_error;
637c478bd9Sstevel@tonic-gate int error_deferred;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate char *itm_name_type_name[] = {
667c478bd9Sstevel@tonic-gate "UNKNOWN",
677c478bd9Sstevel@tonic-gate "ITM",
687c478bd9Sstevel@tonic-gate "STRING",
697c478bd9Sstevel@tonic-gate "DIRECTION",
707c478bd9Sstevel@tonic-gate "CONDITION",
717c478bd9Sstevel@tonic-gate "MAP",
727c478bd9Sstevel@tonic-gate "OPERATION",
737c478bd9Sstevel@tonic-gate "EXPRESSION",
747c478bd9Sstevel@tonic-gate "DATA",
757c478bd9Sstevel@tonic-gate "NAME",
767c478bd9Sstevel@tonic-gate "RANGE",
777c478bd9Sstevel@tonic-gate "REGISTER",
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static void usage(int status);
827c478bd9Sstevel@tonic-gate static int cpp_opt_append(char *opt, char *arg);
837c478bd9Sstevel@tonic-gate static void cpp_opt_trunc(int num);
847c478bd9Sstevel@tonic-gate static int parse_opts(int argc, char **argv);
857c478bd9Sstevel@tonic-gate static char *prog_path_expand(const char *base_name);
867c478bd9Sstevel@tonic-gate static void map_name_type_append(char *optarg);
877c478bd9Sstevel@tonic-gate static char *map_type_name_str(itmc_map_type_t);
887c478bd9Sstevel@tonic-gate static char *strdup_vital(const char *);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
917c478bd9Sstevel@tonic-gate static void trace_option(void);
927c478bd9Sstevel@tonic-gate #endif /* ENABLE_TRACE */
937c478bd9Sstevel@tonic-gate static FILE *cpp_open(void);
947c478bd9Sstevel@tonic-gate static void cpp_close(FILE *fp);
957c478bd9Sstevel@tonic-gate static int itm_compile(char *file);
967c478bd9Sstevel@tonic-gate static void wait_child(pid_t pid);
977c478bd9Sstevel@tonic-gate static int fork_error(void);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1037c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate char **pp;
1067c478bd9Sstevel@tonic-gate pid_t pid;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate (void) parse_opts(argc, argv);
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
1157c478bd9Sstevel@tonic-gate trace_option();
1167c478bd9Sstevel@tonic-gate #endif /* ENABLE_TRACE */
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if (NULL != cmd_opt.disassemble) {
1197c478bd9Sstevel@tonic-gate disassemble(cmd_opt.disassemble);
1207c478bd9Sstevel@tonic-gate } else if (NULL == cmd_opt.input_file) {
1217c478bd9Sstevel@tonic-gate (void) itm_compile(NULL);
1227c478bd9Sstevel@tonic-gate } else {
1237c478bd9Sstevel@tonic-gate if (1 < cmd_opt.input_file_num) {
1247c478bd9Sstevel@tonic-gate for (pp = cmd_opt.input_file; *pp; pp++) {
1257c478bd9Sstevel@tonic-gate (void) printf("%s:\n", *pp);
1267c478bd9Sstevel@tonic-gate pid = fork();
1277c478bd9Sstevel@tonic-gate switch (pid) {
1287c478bd9Sstevel@tonic-gate case 0:
1297c478bd9Sstevel@tonic-gate exit(itm_compile(*pp));
1307c478bd9Sstevel@tonic-gate break;
1317c478bd9Sstevel@tonic-gate case -1:
1327c478bd9Sstevel@tonic-gate (void) fork_error();
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate default:
1357c478bd9Sstevel@tonic-gate wait_child(pid);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate } else {
1397c478bd9Sstevel@tonic-gate (void) itm_compile(*(cmd_opt.input_file));
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate static int
itm_compile(char * file)1487c478bd9Sstevel@tonic-gate itm_compile(char *file)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate char *cmd_line;
1517c478bd9Sstevel@tonic-gate char *command;
1527c478bd9Sstevel@tonic-gate char *p;
1537c478bd9Sstevel@tonic-gate size_t length;
1547c478bd9Sstevel@tonic-gate FILE *fp;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate extern int yyparse();
1577c478bd9Sstevel@tonic-gate extern FILE *yyin;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (NULL == file) {
1607c478bd9Sstevel@tonic-gate itm_input_file = gettext("*stdin*");
1617c478bd9Sstevel@tonic-gate } else {
1627c478bd9Sstevel@tonic-gate if (0 != access(file, R_OK)) {
1637c478bd9Sstevel@tonic-gate int e = errno;
164*c45618a5SRichard Lowe itm_error(gettext("%1$s: can not access %2$s: "),
165*c45618a5SRichard Lowe cmd_opt.my_name, file);
1667c478bd9Sstevel@tonic-gate errno = e;
1677c478bd9Sstevel@tonic-gate PERROR(NULL);
1687c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_CMD2);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate itm_input_file = file;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate if ((NULL == cmd_opt.output_file) &&
1747c478bd9Sstevel@tonic-gate (0 == cmd_opt.no_output)) {
1757c478bd9Sstevel@tonic-gate p = strrchr(file, '.');
1767c478bd9Sstevel@tonic-gate if (NULL == p) {
1777c478bd9Sstevel@tonic-gate length = strlen(file);
1787c478bd9Sstevel@tonic-gate } else {
1797c478bd9Sstevel@tonic-gate length = p - file;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate itm_output_file = malloc_vital(length + 5);
1827c478bd9Sstevel@tonic-gate (void) memcpy(itm_output_file, file, length);
1837c478bd9Sstevel@tonic-gate (void) memcpy(itm_output_file + length, ITMSUFFIX, 5);
1847c478bd9Sstevel@tonic-gate } else {
1857c478bd9Sstevel@tonic-gate itm_output_file = cmd_opt.output_file;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (0 != cmd_opt.preprocess) {
1897c478bd9Sstevel@tonic-gate if (NULL == file) {
1907c478bd9Sstevel@tonic-gate fp = cpp_open();
1917c478bd9Sstevel@tonic-gate cmd_line = cmd_opt.preprocess;
1927c478bd9Sstevel@tonic-gate } else {
1937c478bd9Sstevel@tonic-gate (void) cpp_opt_append(file, NULL);
1947c478bd9Sstevel@tonic-gate fp = cpp_open();
1957c478bd9Sstevel@tonic-gate cpp_opt_trunc(1);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate if (NULL == fp) {
1987c478bd9Sstevel@tonic-gate p = strchr(cmd_line, ' ');
1997c478bd9Sstevel@tonic-gate if (NULL == p) {
2007c478bd9Sstevel@tonic-gate length = strlen(cmd_line);
2017c478bd9Sstevel@tonic-gate } else {
2027c478bd9Sstevel@tonic-gate length = (p - cmd_line);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate command = malloc_vital((sizeof (char)) * (length + 1));
2057c478bd9Sstevel@tonic-gate (void) memcpy(command, cmd_line, length);
2067c478bd9Sstevel@tonic-gate *(command + length) = '\0';
2077c478bd9Sstevel@tonic-gate PERROR(command);
208*c45618a5SRichard Lowe itm_error(gettext("%1$s: can not start "
209*c45618a5SRichard Lowe "%2$s on %3$s\n"), cmd_opt.my_name, command,
210*c45618a5SRichard Lowe itm_input_file);
2117c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_SYS);
2127c478bd9Sstevel@tonic-gate } else {
2137c478bd9Sstevel@tonic-gate yyin = fp;
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate (void) yyparse();
2177c478bd9Sstevel@tonic-gate if (NULL == cmd_opt.preprocess_specified) {
2187c478bd9Sstevel@tonic-gate cpp_close(fp);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate } else {
2217c478bd9Sstevel@tonic-gate if ((NULL == file) || (0 != strcmp("-", file))) {
2227c478bd9Sstevel@tonic-gate yyin = stdin;
2237c478bd9Sstevel@tonic-gate } else {
2247c478bd9Sstevel@tonic-gate yyin = fopen(file, "r");
2257c478bd9Sstevel@tonic-gate if (NULL == yyin) {
226*c45618a5SRichard Lowe itm_error(gettext("%1$s: can not open %2$s\n"),
227*c45618a5SRichard Lowe cmd_opt.my_name, itm_input_file);
2287c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_CMD2);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate (void) yyparse();
2327c478bd9Sstevel@tonic-gate if (stdin != yyin) {
2337c478bd9Sstevel@tonic-gate (void) fclose(yyin);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate return (ITMC_STATUS_SUCCESS);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate static void
wait_child(pid_t pid)2447c478bd9Sstevel@tonic-gate wait_child(pid_t pid)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate int stat_loc;
2477c478bd9Sstevel@tonic-gate char *msgstr;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate (void) waitpid(pid, &stat_loc, 0);
2507c478bd9Sstevel@tonic-gate if (WTERMSIG(stat_loc)) {
2517c478bd9Sstevel@tonic-gate if (WCOREDUMP(stat_loc)) {
2527c478bd9Sstevel@tonic-gate msgstr = gettext("signal received: %s, core dumped\n");
2537c478bd9Sstevel@tonic-gate } else {
2547c478bd9Sstevel@tonic-gate msgstr = gettext("signal received: %s\n");
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate itm_error(msgstr, strsignal(WTERMSIG(stat_loc)));
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate static int
fork_error(void)2627c478bd9Sstevel@tonic-gate fork_error(void)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate PERROR(gettext("fork"));
2657c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_SYS);
2667c478bd9Sstevel@tonic-gate return (0); /* never return */
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate static int
parse_opts(int argc,char ** argv)2727c478bd9Sstevel@tonic-gate parse_opts(int argc, char **argv)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate int c;
2757c478bd9Sstevel@tonic-gate int i;
2767c478bd9Sstevel@tonic-gate char *p;
2777c478bd9Sstevel@tonic-gate int error_num = 0;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate #ifdef YYDEBUG
2807c478bd9Sstevel@tonic-gate extern int yydebug;
2817c478bd9Sstevel@tonic-gate #endif /* YYDEBUG */
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate extern char *optarg;
2847c478bd9Sstevel@tonic-gate extern int optind;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate cmd_opt.my_name = basename(*(argv + 0));
2887c478bd9Sstevel@tonic-gate if ('\0' == *(cmd_opt.my_name)) {
2897c478bd9Sstevel@tonic-gate cmd_opt.my_name = ME_DEFAULT;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate cmd_opt.preprocess_default = CPP_PATH;
2937c478bd9Sstevel@tonic-gate cmd_opt.preprocess = cmd_opt.preprocess_default;
2947c478bd9Sstevel@tonic-gate cmd_opt.strip = 1; /* stripped by default */
2957c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "d:i:p:W:D:I:U:fnsM:lo:qX:h")) != EOF) {
2967c478bd9Sstevel@tonic-gate switch (c) {
2977c478bd9Sstevel@tonic-gate case 'd':
2987c478bd9Sstevel@tonic-gate cmd_opt.disassemble = optarg;
2997c478bd9Sstevel@tonic-gate break;
3007c478bd9Sstevel@tonic-gate case 'i':
3017c478bd9Sstevel@tonic-gate cmd_opt.interpreter = optarg;
3027c478bd9Sstevel@tonic-gate break;
3037c478bd9Sstevel@tonic-gate case 'p':
3047c478bd9Sstevel@tonic-gate if (NULL != cmd_opt.preprocess_specified) {
3057c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3067c478bd9Sstevel@tonic-gate gettext("multiple -p options are specified\n"));
3077c478bd9Sstevel@tonic-gate error_num += 1;
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate cmd_opt.preprocess_specified =
310*c45618a5SRichard Lowe prog_path_expand(optarg);
3117c478bd9Sstevel@tonic-gate cmd_opt.preprocess = cmd_opt.preprocess_specified;
3127c478bd9Sstevel@tonic-gate if (NULL == cmd_opt.preprocess) {
3137c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3147c478bd9Sstevel@tonic-gate gettext("cannot find preprocessor \"%s\"\n"),
315*c45618a5SRichard Lowe optarg);
3167c478bd9Sstevel@tonic-gate error_num += 1;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate (void) cpp_opt_append(NULL, NULL);
3197c478bd9Sstevel@tonic-gate p = basename(optarg);
3207c478bd9Sstevel@tonic-gate if (NULL == p) {
3217c478bd9Sstevel@tonic-gate *(cmd_opt.cpp_opt + 0) = strdup_vital(optarg);
3227c478bd9Sstevel@tonic-gate } else {
3237c478bd9Sstevel@tonic-gate *(cmd_opt.cpp_opt + 0) = strdup_vital(p);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate break;
3267c478bd9Sstevel@tonic-gate case 'W':
3277c478bd9Sstevel@tonic-gate if (cpp_opt_append(optarg, NULL)) {
3287c478bd9Sstevel@tonic-gate error_num += 1;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate break;
3317c478bd9Sstevel@tonic-gate case 'I':
3327c478bd9Sstevel@tonic-gate if (cpp_opt_append("-I", optarg)) {
3337c478bd9Sstevel@tonic-gate error_num += 1;
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate break;
3367c478bd9Sstevel@tonic-gate case 'D':
3377c478bd9Sstevel@tonic-gate if (cpp_opt_append("-D", optarg)) {
3387c478bd9Sstevel@tonic-gate error_num += 1;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate break;
3417c478bd9Sstevel@tonic-gate case 'U':
3427c478bd9Sstevel@tonic-gate if (cpp_opt_append("-U", optarg)) {
3437c478bd9Sstevel@tonic-gate error_num += 1;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate break;
3467c478bd9Sstevel@tonic-gate case 'f':
3477c478bd9Sstevel@tonic-gate cmd_opt.force_overwrite = 1;
3487c478bd9Sstevel@tonic-gate break;
3497c478bd9Sstevel@tonic-gate case 'n':
3507c478bd9Sstevel@tonic-gate cmd_opt.no_output = 1;
3517c478bd9Sstevel@tonic-gate break;
3527c478bd9Sstevel@tonic-gate case 'M':
3537c478bd9Sstevel@tonic-gate map_name_type_append(optarg);
3547c478bd9Sstevel@tonic-gate break;
3557c478bd9Sstevel@tonic-gate case 'l':
3567c478bd9Sstevel@tonic-gate cmd_opt.large_table = 1;
3577c478bd9Sstevel@tonic-gate break;
3587c478bd9Sstevel@tonic-gate case 'o':
3597c478bd9Sstevel@tonic-gate cmd_opt.output_file = optarg;
3607c478bd9Sstevel@tonic-gate break;
3617c478bd9Sstevel@tonic-gate case 's':
3627c478bd9Sstevel@tonic-gate cmd_opt.strip = 0;
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate case 'q':
3657c478bd9Sstevel@tonic-gate cmd_opt.quiet = 1;
3667c478bd9Sstevel@tonic-gate break;
3677c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
3687c478bd9Sstevel@tonic-gate case 'X':
3697c478bd9Sstevel@tonic-gate cmd_opt.trace = malloc_vital((sizeof (char)) * 128);
3707c478bd9Sstevel@tonic-gate (void) memset(cmd_opt.trace, 0, (sizeof (char)) * 128);
3717c478bd9Sstevel@tonic-gate for (p = optarg; *p; p++) {
3727c478bd9Sstevel@tonic-gate *(cmd_opt.trace + ((*p) & 0x007f)) = 1;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate #ifdef YYDEBUG
3757c478bd9Sstevel@tonic-gate if (TRACE('Y')) yydebug = 1;
3767c478bd9Sstevel@tonic-gate #endif /* YYDEBUG */
3777c478bd9Sstevel@tonic-gate break;
3787c478bd9Sstevel@tonic-gate #endif /* ENABLE_TRACE */
3797c478bd9Sstevel@tonic-gate case 'h':
3807c478bd9Sstevel@tonic-gate usage(ITMC_STATUS_SUCCESS);
3817c478bd9Sstevel@tonic-gate break;
3827c478bd9Sstevel@tonic-gate default:
3837c478bd9Sstevel@tonic-gate usage(ITMC_STATUS_CMD);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate if (optind < argc) {
3887c478bd9Sstevel@tonic-gate cmd_opt.input_file_num = (argc - optind);
3897c478bd9Sstevel@tonic-gate cmd_opt.input_file =
390*c45618a5SRichard Lowe malloc_vital((sizeof (char *)) * (argc - optind + 1));
3917c478bd9Sstevel@tonic-gate *(cmd_opt.input_file + (argc - optind)) = NULL;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate for (i = 0; optind < argc; optind++, i++) {
3957c478bd9Sstevel@tonic-gate *(cmd_opt.input_file + i) = argv[optind];
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /* check conflict */
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate if ((1 < cmd_opt.input_file_num) && (NULL != cmd_opt.output_file)) {
4017c478bd9Sstevel@tonic-gate itm_error(gettext("use -o with single input file\n"));
4027c478bd9Sstevel@tonic-gate error_num++;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate if ((cmd_opt.input_file_num <= 0) &&
4067c478bd9Sstevel@tonic-gate (NULL == cmd_opt.output_file) &&
4077c478bd9Sstevel@tonic-gate (NULL == cmd_opt.disassemble) &&
4087c478bd9Sstevel@tonic-gate (0 == cmd_opt.no_output)) {
409*c45618a5SRichard Lowe itm_error(gettext("output file is unnamed. "
410*c45618a5SRichard Lowe "use -o to specify output file\n"));
4117c478bd9Sstevel@tonic-gate error_num++;
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate if (cmd_opt.disassemble &&
4157c478bd9Sstevel@tonic-gate (cmd_opt.interpreter ||
4167c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt ||
4177c478bd9Sstevel@tonic-gate cmd_opt.preprocess_specified ||
4187c478bd9Sstevel@tonic-gate cmd_opt.input_file ||
4197c478bd9Sstevel@tonic-gate cmd_opt.force_overwrite ||
4207c478bd9Sstevel@tonic-gate cmd_opt.no_output ||
4217c478bd9Sstevel@tonic-gate cmd_opt.map_name_type ||
4227c478bd9Sstevel@tonic-gate cmd_opt.large_table ||
4237c478bd9Sstevel@tonic-gate cmd_opt.output_file)) {
424*c45618a5SRichard Lowe itm_error(gettext("-d may not be specified with "
425*c45618a5SRichard Lowe "other options\n"));
4267c478bd9Sstevel@tonic-gate error_num++;
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate if (error_num) {
4307c478bd9Sstevel@tonic-gate usage(ITMC_STATUS_CMD);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate * do not move upward
4357c478bd9Sstevel@tonic-gate * may conflict with -d option
4367c478bd9Sstevel@tonic-gate */
4377c478bd9Sstevel@tonic-gate if ((NULL == cmd_opt.preprocess_specified) &&
4387c478bd9Sstevel@tonic-gate (NULL != cmd_opt.preprocess_default)) {
4397c478bd9Sstevel@tonic-gate (void) cpp_opt_append(NULL, NULL);
4407c478bd9Sstevel@tonic-gate p = basename(cmd_opt.preprocess_default);
4417c478bd9Sstevel@tonic-gate if (NULL == p) {
4427c478bd9Sstevel@tonic-gate *(cmd_opt.cpp_opt + 0) =
443*c45618a5SRichard Lowe strdup_vital(cmd_opt.preprocess_default);
4447c478bd9Sstevel@tonic-gate } else {
4457c478bd9Sstevel@tonic-gate *(cmd_opt.cpp_opt + 0) = strdup_vital(p);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate return (0);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate static FILE *
cpp_open(void)4537c478bd9Sstevel@tonic-gate cpp_open(void)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate pid_t pid;
4567c478bd9Sstevel@tonic-gate int filedes[2];
4577c478bd9Sstevel@tonic-gate int i;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate for (i = 0; i < cmd_opt.cpp_opt_num; i++) {
4607c478bd9Sstevel@tonic-gate TRACE_MESSAGE('C', ("%s\n", *(cmd_opt.cpp_opt + i)));
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if (pipe(filedes)) {
4647c478bd9Sstevel@tonic-gate PERROR(gettext("pipe"));
4657c478bd9Sstevel@tonic-gate itm_error(gettext("failed to open pipe\n"));
4667c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_SYS);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate pid = fork();
4697c478bd9Sstevel@tonic-gate if (pid == 0) { /* child */
4707c478bd9Sstevel@tonic-gate (void) close(filedes[0]);
4717c478bd9Sstevel@tonic-gate (void) close(1);
4727c478bd9Sstevel@tonic-gate (void) dup2(filedes[1], 1);
4737c478bd9Sstevel@tonic-gate (void) execv(cmd_opt.preprocess, cmd_opt.cpp_opt);
4747c478bd9Sstevel@tonic-gate exit(0);
4757c478bd9Sstevel@tonic-gate } else if (pid == (pid_t)(-1)) { /* error */
4767c478bd9Sstevel@tonic-gate return (NULL);
4777c478bd9Sstevel@tonic-gate } else {
4787c478bd9Sstevel@tonic-gate (void) close(filedes[1]);
4797c478bd9Sstevel@tonic-gate return (fdopen(filedes[0], "r"));
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate return (NULL); /* NEVER */
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate static int
cpp_opt_append(char * opt,char * arg)4867c478bd9Sstevel@tonic-gate cpp_opt_append(char *opt, char *arg)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate size_t opt_len;
4897c478bd9Sstevel@tonic-gate size_t arg_len;
4907c478bd9Sstevel@tonic-gate char *new_opt;
4917c478bd9Sstevel@tonic-gate char **new_opt_list;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate opt_len = ((NULL == opt) ? 0 : strlen(opt));
4947c478bd9Sstevel@tonic-gate arg_len = ((NULL == arg) ? 0 : strlen(arg));
4957c478bd9Sstevel@tonic-gate if (0 < (opt_len + arg_len)) {
4967c478bd9Sstevel@tonic-gate new_opt = malloc_vital(opt_len + arg_len + 1);
4977c478bd9Sstevel@tonic-gate if (NULL != opt) {
4987c478bd9Sstevel@tonic-gate (void) memcpy(new_opt, opt, opt_len + 1);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate if (NULL != arg) {
5017c478bd9Sstevel@tonic-gate (void) memcpy(new_opt + opt_len, arg, arg_len + 1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate } else {
5047c478bd9Sstevel@tonic-gate new_opt = NULL;
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate if (0 == cmd_opt.cpp_opt_reserved) {
5087c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt_reserved = 32;
5097c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt = malloc_vital((sizeof (char *)) * 32);
510*c45618a5SRichard Lowe *(cmd_opt.cpp_opt + 0) = strdup_vital("cpp");
5117c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt_num = 1;
5127c478bd9Sstevel@tonic-gate } else if ((cmd_opt.cpp_opt_reserved - 2) <= cmd_opt.cpp_opt_num) {
5137c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt_reserved += 32;
5147c478bd9Sstevel@tonic-gate new_opt_list = malloc_vital((sizeof (char *)) *
515*c45618a5SRichard Lowe cmd_opt.cpp_opt_reserved);
5167c478bd9Sstevel@tonic-gate (void) memcpy(new_opt_list, cmd_opt.cpp_opt,
517*c45618a5SRichard Lowe (sizeof (char *)) * cmd_opt.cpp_opt_num);
5187c478bd9Sstevel@tonic-gate (void) memset(new_opt_list + cmd_opt.cpp_opt_num, 0, 32);
5197c478bd9Sstevel@tonic-gate free(cmd_opt.cpp_opt);
5207c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt = new_opt_list;
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate if (NULL != new_opt) {
5237c478bd9Sstevel@tonic-gate *(cmd_opt.cpp_opt + cmd_opt.cpp_opt_num) = new_opt;
5247c478bd9Sstevel@tonic-gate cmd_opt.cpp_opt_num += 1;
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate return (0);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate static void
cpp_opt_trunc(int num)5317c478bd9Sstevel@tonic-gate cpp_opt_trunc(int num)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate if (cmd_opt.cpp_opt_num < num) {
5347c478bd9Sstevel@tonic-gate num = cmd_opt.cpp_opt_num;
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate for (; 0 < num; --num) {
537*c45618a5SRichard Lowe free(*(cmd_opt.cpp_opt + cmd_opt.cpp_opt_num - 1));
5387c478bd9Sstevel@tonic-gate --(cmd_opt.cpp_opt_num);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate static void
cpp_close(FILE * fp)5447c478bd9Sstevel@tonic-gate cpp_close(FILE *fp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate (void) fclose(fp);
5477c478bd9Sstevel@tonic-gate (void) wait_child(0);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate static char *
prog_path_expand(const char * base_name)5547c478bd9Sstevel@tonic-gate prog_path_expand(const char *base_name)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate size_t base_len;
5577c478bd9Sstevel@tonic-gate size_t dir_len;
5587c478bd9Sstevel@tonic-gate char path[MAXPATHLEN];
5597c478bd9Sstevel@tonic-gate char *p;
5607c478bd9Sstevel@tonic-gate char *pe;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate base_len = strlen(base_name);
5637c478bd9Sstevel@tonic-gate path[0] = '\0';
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate if (NULL != strchr(base_name, '/')) {
5667c478bd9Sstevel@tonic-gate if (0 == access(base_name, X_OK)) {
5677c478bd9Sstevel@tonic-gate return (strdup_vital(base_name));
5687c478bd9Sstevel@tonic-gate } else {
5697c478bd9Sstevel@tonic-gate return (NULL);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate for (p = getenv("PATH"); p; ) {
5747c478bd9Sstevel@tonic-gate pe = strchr(p, ':');
5757c478bd9Sstevel@tonic-gate dir_len = ((NULL == pe) ? strlen(p) : (pe - p));
5767c478bd9Sstevel@tonic-gate (void) memcpy(path, p, dir_len);
5777c478bd9Sstevel@tonic-gate if ((0 != dir_len) &&
5787c478bd9Sstevel@tonic-gate ('/' != path[dir_len - 1])) {
5797c478bd9Sstevel@tonic-gate path[dir_len] = '/';
5807c478bd9Sstevel@tonic-gate dir_len += 1;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate if ((dir_len + base_len) < MAXPATHLEN) {
5837c478bd9Sstevel@tonic-gate (void) memcpy(path + dir_len, base_name, base_len + 1);
5847c478bd9Sstevel@tonic-gate if (0 == access(path, X_OK)) {
5857c478bd9Sstevel@tonic-gate return (strdup_vital(path));
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate p = ((NULL == pe) ? NULL : (pe + 1));
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate return (NULL);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate static void
usage(int status)5957c478bd9Sstevel@tonic-gate usage(int status)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate if (ITMC_STATUS_SUCCESS == status) {
5987c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
5997c478bd9Sstevel@tonic-gate gettext("Usage: %1$s [-n] [-f] [-q]\n"
600*c45618a5SRichard Lowe " [-p preprocessor] [-W argument]\n"
601*c45618a5SRichard Lowe " [-Dname] [-Dname=def] [-Idirectory] [-Uname]\n"
602*c45618a5SRichard Lowe " [file ...]\n %2$s -h\n"),
603*c45618a5SRichard Lowe cmd_opt.my_name, cmd_opt.my_name);
6047c478bd9Sstevel@tonic-gate } else {
6057c478bd9Sstevel@tonic-gate (void) itm_error(
6067c478bd9Sstevel@tonic-gate gettext("Usage: %1$s [-n] [-f] [-q]\n"
607*c45618a5SRichard Lowe " [-p preprocessor] [-W argument]\n"
608*c45618a5SRichard Lowe " [-Dname] [-Dname=def] [-Idirectory] [-Uname]\n"
609*c45618a5SRichard Lowe " [file ...]\n %2$s -h\n"),
610*c45618a5SRichard Lowe cmd_opt.my_name, cmd_opt.my_name);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate exit(status);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate static char *
map_type_name_str(itmc_map_type_t type)6177c478bd9Sstevel@tonic-gate map_type_name_str(itmc_map_type_t type)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate int i;
6207c478bd9Sstevel@tonic-gate for (i = 0; NULL != map_type_name[i].name; i++) {
6217c478bd9Sstevel@tonic-gate if (type == map_type_name[i].type) {
6227c478bd9Sstevel@tonic-gate return (map_type_name[i].name);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate return ("");
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate
6287c478bd9Sstevel@tonic-gate static void
map_name_type_append(char * optarg)6297c478bd9Sstevel@tonic-gate map_name_type_append(char *optarg)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate char *oa;
6327c478bd9Sstevel@tonic-gate char *oa_save;
6337c478bd9Sstevel@tonic-gate char *name;
6347c478bd9Sstevel@tonic-gate char *p;
6357c478bd9Sstevel@tonic-gate char *phf;
6367c478bd9Sstevel@tonic-gate int hash_factor = 0;
6377c478bd9Sstevel@tonic-gate itmc_map_type_t type;
6387c478bd9Sstevel@tonic-gate itmc_map_name_type_t *m;
6397c478bd9Sstevel@tonic-gate int i;
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate oa = oa_save = strdup_vital(optarg);
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate while ((NULL != oa) && ('\0' != *oa)) {
6447c478bd9Sstevel@tonic-gate name = oa;
6457c478bd9Sstevel@tonic-gate oa = strchr(oa, ',');
6467c478bd9Sstevel@tonic-gate if (NULL != oa) {
6477c478bd9Sstevel@tonic-gate *(oa++) = '\0';
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate p = strchr(name, '=');
6507c478bd9Sstevel@tonic-gate if (NULL == p) {
6517c478bd9Sstevel@tonic-gate type = ITMC_MAP_AUTOMATIC;
6527c478bd9Sstevel@tonic-gate } else {
6537c478bd9Sstevel@tonic-gate *(p++) = '\0';
6547c478bd9Sstevel@tonic-gate if ('\0' == *p) {
6557c478bd9Sstevel@tonic-gate type = ITMC_MAP_AUTOMATIC;
6567c478bd9Sstevel@tonic-gate } else {
6577c478bd9Sstevel@tonic-gate phf = strchr(p, ':');
6587c478bd9Sstevel@tonic-gate if (NULL != phf) {
6597c478bd9Sstevel@tonic-gate *(phf++) = '\0';
6607c478bd9Sstevel@tonic-gate hash_factor = atoi(phf);
6617c478bd9Sstevel@tonic-gate if (hash_factor < 0) {
662*c45618a5SRichard Lowe itm_error(gettext("invalid "
663*c45618a5SRichard Lowe "hash factor is "
664*c45618a5SRichard Lowe "specified: %s\n"), phf);
6657c478bd9Sstevel@tonic-gate hash_factor = 0;
6667c478bd9Sstevel@tonic-gate error_deferred += 1;
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate }
669*c45618a5SRichard Lowe for (i = 0; NULL != map_type_name[i].name;
670*c45618a5SRichard Lowe i++) {
6717c478bd9Sstevel@tonic-gate if (0 ==
6727c478bd9Sstevel@tonic-gate strcmp(p, map_type_name[i].name)) {
6737c478bd9Sstevel@tonic-gate type = map_type_name[i].type;
6747c478bd9Sstevel@tonic-gate break;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate if (NULL == map_type_name[i].name) {
678*c45618a5SRichard Lowe itm_error(gettext("unknown map type "
679*c45618a5SRichard Lowe "is specified: %s\n"), p);
6807c478bd9Sstevel@tonic-gate error_deferred += 1;
6817c478bd9Sstevel@tonic-gate continue;
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate if (0 == strcmp(name, "default")) {
6867c478bd9Sstevel@tonic-gate *name = '\0';
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate m = cmd_opt.map_name_type;
6897c478bd9Sstevel@tonic-gate if (NULL == m) {
6907c478bd9Sstevel@tonic-gate m = malloc_vital(sizeof (itmc_map_name_type_t));
6917c478bd9Sstevel@tonic-gate m->name = strdup_vital(name);
6927c478bd9Sstevel@tonic-gate m->type = type;
6937c478bd9Sstevel@tonic-gate m->hash_factor = hash_factor;
6947c478bd9Sstevel@tonic-gate m->next = NULL;
6957c478bd9Sstevel@tonic-gate cmd_opt.map_name_type = m;
6967c478bd9Sstevel@tonic-gate continue;
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate for (; ; m = m->next) {
6997c478bd9Sstevel@tonic-gate if (0 == strcmp(name, m->name)) {
7007c478bd9Sstevel@tonic-gate if (type == m->type) {
7017c478bd9Sstevel@tonic-gate m = NULL;
7027c478bd9Sstevel@tonic-gate break;
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate if ('\0' == *name) {
705*c45618a5SRichard Lowe itm_error(gettext("multiple default "
706*c45618a5SRichard Lowe "types are specified:"
707*c45618a5SRichard Lowe " \"%1$s\" and \"%2$s\"\n"),
708*c45618a5SRichard Lowe map_type_name_str(type),
709*c45618a5SRichard Lowe map_type_name_str(m->type));
7107c478bd9Sstevel@tonic-gate } else {
711*c45618a5SRichard Lowe itm_error(gettext("map \"%1$s\" is "
712*c45618a5SRichard Lowe "specified as two types \"%2$s\" "
713*c45618a5SRichard Lowe "and \"%3$s\"\n"), name,
714*c45618a5SRichard Lowe map_type_name_str(type),
715*c45618a5SRichard Lowe map_type_name_str(m->type));
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate error_deferred += 1;
7187c478bd9Sstevel@tonic-gate m = NULL;
7197c478bd9Sstevel@tonic-gate break;
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate if (NULL == m->next) {
7227c478bd9Sstevel@tonic-gate break;
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate if (NULL != m) {
7267c478bd9Sstevel@tonic-gate m->next = malloc_vital(sizeof (itmc_map_name_type_t));
7277c478bd9Sstevel@tonic-gate m = m->next;
7287c478bd9Sstevel@tonic-gate m->name = strdup_vital(name);
7297c478bd9Sstevel@tonic-gate m->type = type;
7307c478bd9Sstevel@tonic-gate m->hash_factor = hash_factor;
7317c478bd9Sstevel@tonic-gate m->next = NULL;
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate free(oa_save);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate
7407c478bd9Sstevel@tonic-gate void *
malloc_vital(size_t size)7417c478bd9Sstevel@tonic-gate malloc_vital(size_t size)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate void *p;
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate TRACE_MESSAGE('M', ("malloc_vital: %d\n", size));
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate size = ITMROUNDUP(size);
7487c478bd9Sstevel@tonic-gate
7497c478bd9Sstevel@tonic-gate p = (void*) malloc(size);
7507c478bd9Sstevel@tonic-gate if (NULL == p) {
7517c478bd9Sstevel@tonic-gate PERROR(gettext("malloc"));
7527c478bd9Sstevel@tonic-gate exit(ITMC_STATUS_SYS);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate (void) memset(p, 0, size);
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate return (p);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate static char *
strdup_vital(const char * str)7627c478bd9Sstevel@tonic-gate strdup_vital(const char *str)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate char *p;
7657c478bd9Sstevel@tonic-gate size_t len;
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate if (NULL == str) {
7687c478bd9Sstevel@tonic-gate return (NULL);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate len = strlen(str) + 1;
7727c478bd9Sstevel@tonic-gate p = malloc_vital(len);
7737c478bd9Sstevel@tonic-gate (void) memcpy(p, str, len);
7747c478bd9Sstevel@tonic-gate return (p);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate
7817c478bd9Sstevel@tonic-gate itm_data_t *
str_to_data(int size,char * seq)7827c478bd9Sstevel@tonic-gate str_to_data(int size, char *seq)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate itm_data_t *data;
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate data = malloc_vital(sizeof (itm_data_t));
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate data->size = size;
7897c478bd9Sstevel@tonic-gate if (size <= sizeof (data->place)) {
7907c478bd9Sstevel@tonic-gate (void) memmove(&(data->place), seq, size);
7917c478bd9Sstevel@tonic-gate } else {
7927c478bd9Sstevel@tonic-gate data->place.itm_ptr = (itm_place2_t)malloc_vital(size);
7937c478bd9Sstevel@tonic-gate (void) memmove((char *)(data->place.itm_ptr), seq, size);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate return (data);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate char *
name_to_str(itm_data_t * name)8017c478bd9Sstevel@tonic-gate name_to_str(itm_data_t *name)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate static char *ptr = NULL;
8047c478bd9Sstevel@tonic-gate static size_t len = 0;
8057c478bd9Sstevel@tonic-gate size_t req_len;
8067c478bd9Sstevel@tonic-gate char *p;
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate if (NULL == name) {
8097c478bd9Sstevel@tonic-gate p = gettext("(no name)");
8107c478bd9Sstevel@tonic-gate req_len = strlen(p) + 1;
8117c478bd9Sstevel@tonic-gate } else {
8127c478bd9Sstevel@tonic-gate req_len = name->size + 1;
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate if (len <= req_len) {
8167c478bd9Sstevel@tonic-gate len += 512;
8177c478bd9Sstevel@tonic-gate free(ptr);
8187c478bd9Sstevel@tonic-gate ptr = malloc_vital(len);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate if (NULL == name) {
8227c478bd9Sstevel@tonic-gate (void) memcpy(ptr, p, req_len);
8237c478bd9Sstevel@tonic-gate *(ptr + req_len) = '\0';
8247c478bd9Sstevel@tonic-gate } else if (name->size <= (sizeof (name->place))) {
8257c478bd9Sstevel@tonic-gate (void) memcpy(ptr, (char *)(&(name->place)), name->size);
8267c478bd9Sstevel@tonic-gate *(ptr + name->size) = '\0';
8277c478bd9Sstevel@tonic-gate } else {
8287c478bd9Sstevel@tonic-gate (void) memcpy(ptr, (char *)(name->place.itm_ptr), name->size);
8297c478bd9Sstevel@tonic-gate *(ptr + name->size) = '\0';
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate return (ptr);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate #define ARGUMENTSMAX (8)
8367c478bd9Sstevel@tonic-gate char *
data_to_hexadecimal(itm_data_t * data)8377c478bd9Sstevel@tonic-gate data_to_hexadecimal(itm_data_t *data)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate static int index = 0;
8407c478bd9Sstevel@tonic-gate static char *ptr[ARGUMENTSMAX] = { NULL, NULL, NULL, NULL,
8417c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL};
8427c478bd9Sstevel@tonic-gate static long len[ARGUMENTSMAX] = { 0, 0, 0, 0, 0, 0, 0, 0};
8437c478bd9Sstevel@tonic-gate char *hdp;
8447c478bd9Sstevel@tonic-gate char *p;
8457c478bd9Sstevel@tonic-gate long i;
8467c478bd9Sstevel@tonic-gate int val;
8477c478bd9Sstevel@tonic-gate size_t req_len;
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate if (ARGUMENTSMAX <= index) index = 0;
8507c478bd9Sstevel@tonic-gate req_len = (2 * data->size) + 1;
8517c478bd9Sstevel@tonic-gate if (len[index] <= req_len) {
8527c478bd9Sstevel@tonic-gate len[index] += 512;
8537c478bd9Sstevel@tonic-gate free(ptr[index]);
8547c478bd9Sstevel@tonic-gate ptr[index] = malloc_vital(len[index]);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate hdp = ptr[index];
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate if (data->size <= (sizeof (itm_place_t))) {
8597c478bd9Sstevel@tonic-gate p = (char *)&(data->place);
8607c478bd9Sstevel@tonic-gate } else {
8617c478bd9Sstevel@tonic-gate p = (char *)(data->place.itm_ptr);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate for (i = 0; i < data->size; i++, p++) {
8657c478bd9Sstevel@tonic-gate val = ((*p & 0x00f0) >> 4);
8667c478bd9Sstevel@tonic-gate if ((0 <= val) && (val <= 9)) {
8677c478bd9Sstevel@tonic-gate *hdp = '0' + val;
8687c478bd9Sstevel@tonic-gate } else {
8697c478bd9Sstevel@tonic-gate *hdp = 'a' + val - 10;
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate hdp++;
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate val = (*p & 0x000f);
8747c478bd9Sstevel@tonic-gate if ((0 <= val) && (val <= 9)) {
8757c478bd9Sstevel@tonic-gate *hdp = '0' + val;
8767c478bd9Sstevel@tonic-gate } else {
8777c478bd9Sstevel@tonic-gate *hdp = 'a' + val - 10;
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate hdp++;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate *hdp = '\0';
8827c478bd9Sstevel@tonic-gate return (ptr[index++]);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate void
itm_error(char * format,...)8907c478bd9Sstevel@tonic-gate itm_error(char *format, ...)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate va_list ap;
8937c478bd9Sstevel@tonic-gate va_start(ap, format);
8947c478bd9Sstevel@tonic-gate
8957c478bd9Sstevel@tonic-gate if (0 == cmd_opt.quiet) {
8967c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate va_end(ap);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
9027c478bd9Sstevel@tonic-gate static void
trace_option(void)9037c478bd9Sstevel@tonic-gate trace_option(void)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate char **pp;
9067c478bd9Sstevel@tonic-gate int i;
9077c478bd9Sstevel@tonic-gate
9087c478bd9Sstevel@tonic-gate if (!(TRACE('o')))
9097c478bd9Sstevel@tonic-gate return;
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate itm_error("my_name = %s\n", cmd_opt.my_name);
9127c478bd9Sstevel@tonic-gate if (NULL == cmd_opt.input_file) {
9137c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "input_file = (stdin)\n");
9147c478bd9Sstevel@tonic-gate } else {
9157c478bd9Sstevel@tonic-gate for (pp = cmd_opt.input_file; *pp; pp++) {
9167c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "input_file = %s\n", *pp);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate itm_error("output_file = %s\n",
920*c45618a5SRichard Lowe cmd_opt.output_file ? cmd_opt.output_file : "(stdout)");
9217c478bd9Sstevel@tonic-gate itm_error("interpreter = %s\n",
922*c45618a5SRichard Lowe cmd_opt.interpreter ? cmd_opt.interpreter : "(default)");
9237c478bd9Sstevel@tonic-gate if (cmd_opt.cpp_opt) {
9247c478bd9Sstevel@tonic-gate itm_error("cpp_opt = %s\n", *(cmd_opt.cpp_opt));
9257c478bd9Sstevel@tonic-gate for (i = 1; i < cmd_opt.cpp_opt_num; i++) {
9267c478bd9Sstevel@tonic-gate itm_error("\t%s\n", *(cmd_opt.cpp_opt + i));
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate } else {
9297c478bd9Sstevel@tonic-gate itm_error("cpp_opt = %s\n", "(none)");
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate itm_error("preprocess_default = %s\n",
932*c45618a5SRichard Lowe cmd_opt.preprocess_default ? cmd_opt.preprocess_default :
933*c45618a5SRichard Lowe "(no)");
9347c478bd9Sstevel@tonic-gate itm_error("preprocess_specified = %s\n",
935*c45618a5SRichard Lowe cmd_opt.preprocess_specified ? cmd_opt.preprocess_specified :
936*c45618a5SRichard Lowe "(no)");
9377c478bd9Sstevel@tonic-gate itm_error("preprocess = %s\n",
938*c45618a5SRichard Lowe cmd_opt.preprocess ? cmd_opt.preprocess : "(no)");
9397c478bd9Sstevel@tonic-gate itm_error("disassemble = %s\n",
940*c45618a5SRichard Lowe cmd_opt.disassemble ? "yes" : "no");
9417c478bd9Sstevel@tonic-gate itm_error("map type =");
9427c478bd9Sstevel@tonic-gate if (NULL == cmd_opt.map_name_type) {
9437c478bd9Sstevel@tonic-gate itm_error("\n");
9447c478bd9Sstevel@tonic-gate } else {
9457c478bd9Sstevel@tonic-gate itmc_map_name_type_t *m;
9467c478bd9Sstevel@tonic-gate itm_error(" ");
9477c478bd9Sstevel@tonic-gate m = cmd_opt.map_name_type;
9487c478bd9Sstevel@tonic-gate itm_error("%s=%s",
949*c45618a5SRichard Lowe (((NULL == m->name) || ('\0' == *(m->name))) ?
950*c45618a5SRichard Lowe "default" : m->name),
951*c45618a5SRichard Lowe map_type_name_str(m->type));
9527c478bd9Sstevel@tonic-gate if (0 != m->hash_factor) {
9537c478bd9Sstevel@tonic-gate itm_error(":%ld\n", m->hash_factor);
9547c478bd9Sstevel@tonic-gate } else {
9557c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate for (m = m->next; NULL != m; m = m->next) {
9587c478bd9Sstevel@tonic-gate itm_error(" %s=%s",
959*c45618a5SRichard Lowe (((NULL == m->name) || ('\0' == *(m->name))) ?
960*c45618a5SRichard Lowe "default" : m->name),
961*c45618a5SRichard Lowe map_type_name_str(m->type));
9627c478bd9Sstevel@tonic-gate if (0 != m->hash_factor) {
9637c478bd9Sstevel@tonic-gate itm_error(":%ld\n", m->hash_factor);
9647c478bd9Sstevel@tonic-gate } else {
9657c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate itm_error("large table = %s\n",
970*c45618a5SRichard Lowe cmd_opt.large_table ? "true" : "false");
9717c478bd9Sstevel@tonic-gate itm_error("overwrite = %s\n",
972*c45618a5SRichard Lowe cmd_opt.force_overwrite ? "true" : "false");
9737c478bd9Sstevel@tonic-gate itm_error("strip = %s\n",
974*c45618a5SRichard Lowe cmd_opt.strip ? "true" : "false");
9757c478bd9Sstevel@tonic-gate itm_error("no_output = %s\n",
976*c45618a5SRichard Lowe cmd_opt.no_output ? "true" : "false");
9777c478bd9Sstevel@tonic-gate itm_error("trace = ");
9787c478bd9Sstevel@tonic-gate if (NULL == cmd_opt.trace) {
9797c478bd9Sstevel@tonic-gate itm_error("(no)\n");
9807c478bd9Sstevel@tonic-gate } else {
9817c478bd9Sstevel@tonic-gate for (i = 0x21; i < 0x7f; i++) {
9827c478bd9Sstevel@tonic-gate if (TRACE(i)) {
9837c478bd9Sstevel@tonic-gate (void) fputc(i, stderr);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate (void) fputc('\n', stderr);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate #endif /* ENABLE_TRACE */
9907c478bd9Sstevel@tonic-gate
9917c478bd9Sstevel@tonic-gate #if defined(ENABLE_TRACE)
9927c478bd9Sstevel@tonic-gate extern void
trace_message(char * format,...)9937c478bd9Sstevel@tonic-gate trace_message(char *format, ...)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate va_list ap;
9967c478bd9Sstevel@tonic-gate va_start(ap, format);
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap);
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate va_end(ap);
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate #endif /* ENABLE_TRACE */
1003