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