landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 1 | /* 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 | |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 9 | #include "toys.h" |
| 10 | |
| 11 | // The monster fun applet list. |
| 12 | |
| 13 | struct toy_list toy_list[] = { |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 14 | // This one is out of order on purpose. |
| 15 | {"toybox", toybox_main, 0}, |
| 16 | // The rest of these are alphabetical, for binary search. |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame^] | 17 | 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},) |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 24 | }; |
| 25 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 26 | #define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list)) |
| 27 | |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 28 | // global context for this applet. |
| 29 | |
| 30 | struct toy_context toys; |
| 31 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 32 | struct toy_list *toy_find(char *name) |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 33 | { |
| 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; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 40 | bottom = 1; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 41 | |
| 42 | // Binary search to find this applet. |
| 43 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 44 | top = TOY_LIST_LEN-1; |
| 45 | for (;;) { |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 46 | int result; |
| 47 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 48 | middle = (top+bottom)/2; |
| 49 | if (middle<bottom || middle>top) return NULL; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 50 | result = strcmp(name,toy_list[middle].name); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 51 | if (!result) return toy_list+middle; |
| 52 | if (result<0) top=--middle; |
| 53 | else bottom = ++middle; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 57 | void toy_init(struct toy_list *which, char *argv[]) |
| 58 | { |
| 59 | // Free old toys contents here? |
| 60 | |
| 61 | toys.which = which; |
| 62 | toys.argv = argv; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 63 | toys.exitval = 1; |
| 64 | } |
| 65 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 66 | // Run a toy. |
| 67 | void toy_exec(char *argv[]) |
| 68 | { |
| 69 | struct toy_list *which; |
| 70 | |
| 71 | which = toy_find(argv[0]); |
| 72 | if (!which) return; |
| 73 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 74 | toy_init(which, argv); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 75 | |
| 76 | exit(toys.which->toy_main()); |
| 77 | } |
| 78 | |
| 79 | int 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 | |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 111 | int main(int argc, char *argv[]) |
| 112 | { |
| 113 | char *name; |
| 114 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 115 | // Figure out which applet to call. |
| 116 | name = rindex(argv[0], '/'); |
| 117 | if (!name) name=argv[0]; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 118 | else name++; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 119 | argv[0] = name; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 120 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 121 | toys.argv = argv-1; |
| 122 | return toybox_main(); |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 123 | } |