xref: /dflybsd-src/usr.bin/objformat/objformat.c (revision 31749bba3b47037a5cc970e303404e776c31654d)
1 /*-
2  * Copyright (c) 1998, Peter Wemm <peter@netplex.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.bin/objformat/objformat.c,v 1.6 1998/10/24 02:01:30 jdp Exp $
27  * $DragonFly: src/usr.bin/objformat/objformat.c,v 1.5 2004/01/23 22:30:25 joerg Exp $
28  */
29 
30 #include <err.h>
31 #include <objformat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #define OBJFORMAT	0
38 #define COMPILER	1
39 #define BINUTILS	2
40 
41 struct command {
42 	const char *cmd;
43 	int type;
44 };
45 
46 static struct command commands[] = {
47 	{"CC",		COMPILER},
48 	{"c++",		COMPILER},
49 	{"c++filt",	COMPILER},
50 	{"cc",		COMPILER},
51 	{"cpp",		COMPILER},
52 	{"f77",		COMPILER},
53 	{"g++",		COMPILER},
54 	{"gcc",		COMPILER},
55 	{"gcov",	COMPILER},
56 	{"addr2line",	BINUTILS},
57 	{"ar",		BINUTILS},
58 	{"as",		BINUTILS},
59 	{"gasp",	BINUTILS},
60 	{"gdb",		BINUTILS},
61 	{"ld",		BINUTILS},
62 	{"nm",		BINUTILS},
63 	{"objcopy",	BINUTILS},
64 	{"objdump",	BINUTILS},
65 	{"ranlib",	BINUTILS},
66 	{"size",	BINUTILS},
67 	{"strings",	BINUTILS},
68 	{"strip",	BINUTILS},
69 	{"objformat",	OBJFORMAT},
70 	{0, 0}
71 };
72 
73 int
74 main(int argc, char **argv)
75 {
76 	struct command *cmds = commands;
77 	char objformat[32];
78 	char *path, *chunk;
79 	char *cmd, *newcmd = NULL;
80 	char *objformat_path;
81 	const char *env_name = NULL;
82 	const char *env_value;
83 	const char *env_default = NULL;
84 	const char *base_path = NULL;
85 	int use_objformat = 0;
86 
87 	if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
88 		errx(1, "Invalid object format");
89 
90 	/*
91 	 * Get the last path elemenet of the program name being executed
92 	 */
93 	cmd = strrchr(argv[0], '/');
94 	if (cmd != NULL)
95 		cmd++;
96 	else
97 		cmd = argv[0];
98 
99 	for (;cmds->cmd; cmds++)
100 		if (strcmp(cmd, cmds->cmd) == 0)
101 		{
102 			switch (cmds->type)
103 			{
104 			case COMPILER:
105 				env_name = "CCVER";
106 				env_default = "gcc2";
107 				base_path = "/usr/bin";
108 				use_objformat = 0;
109 				break;
110 			case BINUTILS:
111 				env_name = "BINUTILSVER";
112 				env_default = "gcc2";
113 				base_path = "/usr/libexec";
114 				use_objformat = 1;
115 				break;
116 			case OBJFORMAT:
117 				break;
118 			default:
119 				err(1, "unknown command type");
120 			}
121 			break;
122 		}
123 
124 	/*
125 	 * The objformat command itself doesn't need another exec
126 	 */
127 	if (cmds->type == OBJFORMAT) {
128 		if (argc != 1) {
129 			fprintf(stderr, "Usage: objformat\n");
130 			exit(1);
131 		}
132 
133 		printf("%s\n", objformat);
134 		exit(0);
135 	}
136 
137 	/*
138 	 * make buildworld glue and CCVER overrides.
139 	 */
140 	objformat_path = getenv("OBJFORMAT_PATH");
141 	if (objformat_path == NULL)
142 		objformat_path = "";
143 
144 	if ((env_value = getenv(env_name)) == NULL)
145 		env_value = env_default;
146 
147 	path = strdup(objformat_path);
148 
149 	setenv("OBJFORMAT", objformat, 1);
150 
151 	/*
152 	 * objformat_path could be sequence of colon-separated paths.
153 	 */
154 	while ((chunk = strsep(&path, ":")) != NULL) {
155 		if (newcmd != NULL) {
156 			free(newcmd);
157 			newcmd = NULL;
158 		}
159 		if (use_objformat)
160 			asprintf(&newcmd, "%s%s/%s/%s/%s",
161 				chunk, base_path, env_value, objformat, cmd);
162 		else
163 			asprintf(&newcmd, "%s%s/%s/%s",
164 				chunk, base_path, env_value, cmd);
165 		if (newcmd == NULL)
166 			err(1, "cannot allocate memory");
167 
168 		argv[0] = newcmd;
169 		execv(newcmd, argv);
170 	}
171 	if (use_objformat)
172 		err(1, "in path [%s]%s/%s/%s/%s",
173 			objformat_path, base_path, env_value, objformat, cmd);
174 	else
175 		err(1, "in path [%s]%s/%s/%s",
176 			objformat_path, base_path, env_value, cmd);
177 }
178 
179