blob: 90d80c788d57b610bb1c70467073815bbe8e7533 [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.
17 {"cd", cd_main, TOYFLAG_NOFORK},
18 {"df", df_main, TOYFLAG_USR|TOYFLAG_SBIN},
19 {"exit", exit_main, TOYFLAG_NOFORK},
20 {"toysh", toysh_main, TOYFLAG_BIN}
landleyc5621502006-09-28 17:18:51 -040021};
22
landley4f344e32006-10-05 16:18:03 -040023#define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list))
24
landleyc5621502006-09-28 17:18:51 -040025// global context for this applet.
26
27struct toy_context toys;
28
landley4f344e32006-10-05 16:18:03 -040029struct toy_list *toy_find(char *name)
landley13bab2f2006-09-27 00:45:05 -040030{
31 int top, bottom, middle;
32
33 // If the name starts with "toybox", accept that as a match. Otherwise
34 // skip the first entry, which is out of order.
35
36 if (!strncmp(name,"toybox",6)) return toy_list;
landley4f344e32006-10-05 16:18:03 -040037 bottom = 1;
landley13bab2f2006-09-27 00:45:05 -040038
39 // Binary search to find this applet.
40
landley4f344e32006-10-05 16:18:03 -040041 top = TOY_LIST_LEN-1;
42 for (;;) {
landley13bab2f2006-09-27 00:45:05 -040043 int result;
44
landley4f344e32006-10-05 16:18:03 -040045 middle = (top+bottom)/2;
46 if (middle<bottom || middle>top) return NULL;
landley13bab2f2006-09-27 00:45:05 -040047 result = strcmp(name,toy_list[middle].name);
landley4f344e32006-10-05 16:18:03 -040048 if (!result) return toy_list+middle;
49 if (result<0) top=--middle;
50 else bottom = ++middle;
landley13bab2f2006-09-27 00:45:05 -040051 }
52}
53
landley4f344e32006-10-05 16:18:03 -040054// Run a toy.
55void toy_exec(char *argv[])
56{
57 struct toy_list *which;
58
59 which = toy_find(argv[0]);
60 if (!which) return;
61
62 // Free old toys contents here?
63
64 toys.which = which;
65 toys.argv = argv;
66 for (toys.argc = 0; argv[toys.argc]; toys.argc++);
67 toys.exitval = 1;
68
69 exit(toys.which->toy_main());
70}
71
72int toybox_main(void)
73{
74 static char *toy_paths[]={"usr/","bin/","sbin/",0};
75 int i, len = 0;
76
77 if (toys.argv[1]) {
78 if (toys.argv[1][0]!='-') {
79 toy_exec(toys.argv+1);
80 error_exit("No behavior for %s\n",toys.argv[1]);
81 }
82 }
83
84 // Output list of applets.
85 for (i=1; i<TOY_LIST_LEN; i++) {
86 int fl = toy_list[i].flags;
87 if (fl & TOYMASK_LOCATION) {
88 if (toys.argv[1]) {
89 int j;
90 for (j=0; toy_paths[j]; j++)
91 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
92 }
93 len += printf("%s ",toy_list[i].name);
94 if (len>65) {
95 putchar('\n');
96 len=0;
97 }
98 }
99 }
100 putchar('\n');
101 return 0;
102}
103
landley13bab2f2006-09-27 00:45:05 -0400104int main(int argc, char *argv[])
105{
106 char *name;
107
landley4f344e32006-10-05 16:18:03 -0400108 // Figure out which applet to call.
109 name = rindex(argv[0], '/');
110 if (!name) name=argv[0];
landley13bab2f2006-09-27 00:45:05 -0400111 else name++;
landley4f344e32006-10-05 16:18:03 -0400112 argv[0] = name;
landley13bab2f2006-09-27 00:45:05 -0400113
landley4f344e32006-10-05 16:18:03 -0400114 toys.argv = argv-1;
115 return toybox_main();
landley13bab2f2006-09-27 00:45:05 -0400116}