xref: /dflybsd-src/usr.bin/crunch/crunchgen/crunched_main.c (revision 97fa55c47af6a658ec356831c8267c64bd2c5d26)
1fc7e83faSFrançois Tigeot /*
2fc7e83faSFrançois Tigeot  * Copyright (c) 1994 University of Maryland
3fc7e83faSFrançois Tigeot  * All Rights Reserved.
4fc7e83faSFrançois Tigeot  *
5fc7e83faSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
6fc7e83faSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
7fc7e83faSFrançois Tigeot  * the above copyright notice appear in all copies and that both that
8fc7e83faSFrançois Tigeot  * copyright notice and this permission notice appear in supporting
9fc7e83faSFrançois Tigeot  * documentation, and that the name of U.M. not be used in advertising or
10fc7e83faSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
11fc7e83faSFrançois Tigeot  * written prior permission.  U.M. makes no representations about the
12fc7e83faSFrançois Tigeot  * suitability of this software for any purpose.  It is provided "as is"
13fc7e83faSFrançois Tigeot  * without express or implied warranty.
14fc7e83faSFrançois Tigeot  *
15fc7e83faSFrançois Tigeot  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16fc7e83faSFrançois Tigeot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17fc7e83faSFrançois Tigeot  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18fc7e83faSFrançois Tigeot  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19fc7e83faSFrançois Tigeot  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20fc7e83faSFrançois Tigeot  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21fc7e83faSFrançois Tigeot  *
22fc7e83faSFrançois Tigeot  * Author: James da Silva, Systems Design and Analysis Group
23fc7e83faSFrançois Tigeot  *			   Computer Science Department
24fc7e83faSFrançois Tigeot  *			   University of Maryland at College Park
25fc7e83faSFrançois Tigeot  */
26fc7e83faSFrançois Tigeot /*
27fc7e83faSFrançois Tigeot  * crunched_main.c - main program for crunched binaries, it branches to a
28fc7e83faSFrançois Tigeot  * 	particular subprogram based on the value of argv[0].  Also included
29fc7e83faSFrançois Tigeot  *	is a little program invoked when the crunched binary is called via
30fc7e83faSFrançois Tigeot  *	its EXECNAME.  This one prints out the list of compiled-in binaries,
31fc7e83faSFrançois Tigeot  *	or calls one of them based on argv[1].   This allows the testing of
32fc7e83faSFrançois Tigeot  *	the crunched binary without creating all the links.
33fc7e83faSFrançois Tigeot  */
346fc6ffe8SFrançois Tigeot 
35fc7e83faSFrançois Tigeot #include <stdio.h>
366fc6ffe8SFrançois Tigeot #include <stdlib.h>
37fc7e83faSFrançois Tigeot #include <string.h>
38*97fa55c4SAaron LI #include <err.h>
39fc7e83faSFrançois Tigeot 
40fc7e83faSFrançois Tigeot struct stub {
41*97fa55c4SAaron LI 	const char *name;
42*97fa55c4SAaron LI 	int (*f)(int, char **, char **);
43fc7e83faSFrançois Tigeot };
44fc7e83faSFrançois Tigeot 
45*97fa55c4SAaron LI static const struct stub entry_points[];
46*97fa55c4SAaron LI 
47*97fa55c4SAaron LI static int crunched_main(int argc, char **, char **);
48*97fa55c4SAaron LI static int cmpstringp(const void *, const void *);
49*97fa55c4SAaron LI static void crunched_usage(void) __dead2;
50*97fa55c4SAaron LI 
51fc7e83faSFrançois Tigeot 
52fc7e83faSFrançois Tigeot int
main(int argc,char ** argv,char ** envp)53fc7e83faSFrançois Tigeot main(int argc, char **argv, char **envp)
54fc7e83faSFrançois Tigeot {
55*97fa55c4SAaron LI 	const char *slash, *basename;
56*97fa55c4SAaron LI 	const struct stub *ep;
57fc7e83faSFrançois Tigeot 
58fc7e83faSFrançois Tigeot 	if (argv[0] == NULL || *argv[0] == '\0')
59fc7e83faSFrançois Tigeot 		crunched_usage();
60fc7e83faSFrançois Tigeot 
61fc7e83faSFrançois Tigeot 	slash = strrchr(argv[0], '/');
62fc7e83faSFrançois Tigeot 	basename = slash ? slash+1 : argv[0];
63fc7e83faSFrançois Tigeot 
64fc7e83faSFrançois Tigeot 	for (ep = entry_points; ep->name != NULL; ep++)
65*97fa55c4SAaron LI 		if (strcmp(basename, ep->name) == 0)
66fc7e83faSFrançois Tigeot 			return ep->f(argc, argv, envp);
67*97fa55c4SAaron LI 
68fc7e83faSFrançois Tigeot 	fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename);
69fc7e83faSFrançois Tigeot 	crunched_usage();
70fc7e83faSFrançois Tigeot }
71fc7e83faSFrançois Tigeot 
72fc7e83faSFrançois Tigeot 
73*97fa55c4SAaron LI static int
crunched_main(int argc,char ** argv,char ** envp)74fc7e83faSFrançois Tigeot crunched_main(int argc, char **argv, char **envp)
75fc7e83faSFrançois Tigeot {
76fc7e83faSFrançois Tigeot 	if (argc <= 1)
77fc7e83faSFrançois Tigeot 		crunched_usage();
78fc7e83faSFrançois Tigeot 
79fc7e83faSFrançois Tigeot 	return main(--argc, ++argv, envp);
80fc7e83faSFrançois Tigeot }
81fc7e83faSFrançois Tigeot 
82fc7e83faSFrançois Tigeot 
83*97fa55c4SAaron LI static int
cmpstringp(const void * p1,const void * p2)84*97fa55c4SAaron LI cmpstringp(const void *p1, const void *p2)
85*97fa55c4SAaron LI {
86*97fa55c4SAaron LI 	const char *s1 = *(const char **)p1;
87*97fa55c4SAaron LI 	const char *s2 = *(const char **)p2;
88*97fa55c4SAaron LI 	return strcmp(s1, s2);
89*97fa55c4SAaron LI }
90*97fa55c4SAaron LI 
91*97fa55c4SAaron LI 
92*97fa55c4SAaron LI static void
crunched_usage()936fc6ffe8SFrançois Tigeot crunched_usage()
94fc7e83faSFrançois Tigeot {
95*97fa55c4SAaron LI 	int nprog = 0, columns = 0;
96*97fa55c4SAaron LI 	int i, len;
97*97fa55c4SAaron LI 	const struct stub *ep;
98*97fa55c4SAaron LI 	const char **prognames;
99fc7e83faSFrançois Tigeot 
100*97fa55c4SAaron LI 	for (ep = entry_points; ep->name != NULL; ep++)
101*97fa55c4SAaron LI 		nprog++;
102*97fa55c4SAaron LI 	if ((prognames = malloc(nprog * sizeof(char *))) == NULL)
103*97fa55c4SAaron LI 		err(EXIT_FAILURE, "malloc");
104*97fa55c4SAaron LI 	for (i = 0; i < nprog; i++)
105*97fa55c4SAaron LI 		prognames[i] = entry_points[i].name;
106*97fa55c4SAaron LI 	qsort(prognames, nprog, sizeof(char *), cmpstringp);
107*97fa55c4SAaron LI 
108*97fa55c4SAaron LI 	fprintf(stderr,
109*97fa55c4SAaron LI 		"usage: %s <prog> <args> ..., where <prog> is one of:\n",
110fc7e83faSFrançois Tigeot 		EXECNAME);
111*97fa55c4SAaron LI 	for (i = 0; i < nprog; i++) {
112*97fa55c4SAaron LI 		if (strcmp(EXECNAME, prognames[i]) == 0)
113*97fa55c4SAaron LI 			continue;
114*97fa55c4SAaron LI 		len = strlen(prognames[i]) + 1;
115fc7e83faSFrançois Tigeot 		if (columns+len < 80)
116fc7e83faSFrançois Tigeot 			columns += len;
117fc7e83faSFrançois Tigeot 		else {
118fc7e83faSFrançois Tigeot 			fprintf(stderr, "\n");
119fc7e83faSFrançois Tigeot 			columns = len;
120fc7e83faSFrançois Tigeot 		}
121*97fa55c4SAaron LI 		fprintf(stderr, " %s", prognames[i]);
122fc7e83faSFrançois Tigeot 	}
123fc7e83faSFrançois Tigeot 	fprintf(stderr, "\n");
124*97fa55c4SAaron LI 	free(prognames);
125fc7e83faSFrançois Tigeot 	exit(1);
126fc7e83faSFrançois Tigeot }
127fc7e83faSFrançois Tigeot 
128fc7e83faSFrançois Tigeot /* end of crunched_main.c */
129