blob: 00d763123f1d8208230af17e30c1adce541bc443 [file] [log] [blame]
landley13bab2f2006-09-27 00:45:05 -04001/* vi: set ts=4 :*/
2/* Toybox infrastructure.
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
5 *
6 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7 */
8
landleyc5621502006-09-28 17:18:51 -04009#include "toys.h"
10
11// The monster fun applet list.
12
13struct toy_list toy_list[] = {
landley4f344e32006-10-05 16:18:03 -040014 // This one is out of order on purpose.
15 {"toybox", toybox_main, 0},
16 // The rest of these are alphabetical, for binary search.
Rob Landley0a04b3e2006-11-03 00:05:52 -050017 USE_TOYSH({"cd", cd_main, TOYFLAG_NOFORK},)
18 USE_DF({"df", df_main, TOYFLAG_USR|TOYFLAG_SBIN},)
19 USE_TOYSH({"exit", exit_main, TOYFLAG_NOFORK},)
20 USE_HELLO({"hello", hello_main, TOYFLAG_NOFORK|TOYFLAG_USR},)
21 USE_TOYSH({"sh", toysh_main, TOYFLAG_BIN},)
22 USE_TOYSH({"toysh", toysh_main, TOYFLAG_BIN},)
23 USE_WHICH({"which", which_main, TOYFLAG_USR|TOYFLAG_BIN},)
landleyc5621502006-09-28 17:18:51 -040024};
25
landley4f344e32006-10-05 16:18:03 -040026#define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list))
27
landleyc5621502006-09-28 17:18:51 -040028// global context for this applet.
29
30struct toy_context toys;
31
landley4f344e32006-10-05 16:18:03 -040032struct toy_list *toy_find(char *name)
landley13bab2f2006-09-27 00:45:05 -040033{
34 int top, bottom, middle;
35
36 // If the name starts with "toybox", accept that as a match. Otherwise
37 // skip the first entry, which is out of order.
38
39 if (!strncmp(name,"toybox",6)) return toy_list;
landley4f344e32006-10-05 16:18:03 -040040 bottom = 1;
landley13bab2f2006-09-27 00:45:05 -040041
42 // Binary search to find this applet.
43
landley4f344e32006-10-05 16:18:03 -040044 top = TOY_LIST_LEN-1;
45 for (;;) {
landley13bab2f2006-09-27 00:45:05 -040046 int result;
47
landley4f344e32006-10-05 16:18:03 -040048 middle = (top+bottom)/2;
49 if (middle<bottom || middle>top) return NULL;
landley13bab2f2006-09-27 00:45:05 -040050 result = strcmp(name,toy_list[middle].name);
landley4f344e32006-10-05 16:18:03 -040051 if (!result) return toy_list+middle;
52 if (result<0) top=--middle;
53 else bottom = ++middle;
landley13bab2f2006-09-27 00:45:05 -040054 }
55}
56
landleycd9dfc32006-10-18 18:38:16 -040057void toy_init(struct toy_list *which, char *argv[])
58{
59 // Free old toys contents here?
60
61 toys.which = which;
62 toys.argv = argv;
landleycd9dfc32006-10-18 18:38:16 -040063 toys.exitval = 1;
64}
65
landley4f344e32006-10-05 16:18:03 -040066// Run a toy.
67void toy_exec(char *argv[])
68{
69 struct toy_list *which;
70
71 which = toy_find(argv[0]);
72 if (!which) return;
73
landleycd9dfc32006-10-18 18:38:16 -040074 toy_init(which, argv);
landley4f344e32006-10-05 16:18:03 -040075
76 exit(toys.which->toy_main());
77}
78
79int toybox_main(void)
80{
81 static char *toy_paths[]={"usr/","bin/","sbin/",0};
82 int i, len = 0;
83
84 if (toys.argv[1]) {
85 if (toys.argv[1][0]!='-') {
86 toy_exec(toys.argv+1);
87 error_exit("No behavior for %s\n",toys.argv[1]);
88 }
89 }
90
91 // Output list of applets.
92 for (i=1; i<TOY_LIST_LEN; i++) {
93 int fl = toy_list[i].flags;
94 if (fl & TOYMASK_LOCATION) {
95 if (toys.argv[1]) {
96 int j;
97 for (j=0; toy_paths[j]; j++)
98 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
99 }
100 len += printf("%s ",toy_list[i].name);
101 if (len>65) {
102 putchar('\n');
103 len=0;
104 }
105 }
106 }
107 putchar('\n');
108 return 0;
109}
110
landley13bab2f2006-09-27 00:45:05 -0400111int main(int argc, char *argv[])
112{
113 char *name;
114
landley4f344e32006-10-05 16:18:03 -0400115 // Figure out which applet to call.
116 name = rindex(argv[0], '/');
117 if (!name) name=argv[0];
landley13bab2f2006-09-27 00:45:05 -0400118 else name++;
landley4f344e32006-10-05 16:18:03 -0400119 argv[0] = name;
landley13bab2f2006-09-27 00:45:05 -0400120
landley4f344e32006-10-05 16:18:03 -0400121 toys.argv = argv-1;
122 return toybox_main();
landley13bab2f2006-09-27 00:45:05 -0400123}