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. |
| 17 | {"cd", cd_main, TOYFLAG_NOFORK}, |
| 18 | {"df", df_main, TOYFLAG_USR|TOYFLAG_SBIN}, |
| 19 | {"exit", exit_main, TOYFLAG_NOFORK}, |
Rob Landley | 9b3fc7d | 2006-11-01 22:23:58 -0500 | [diff] [blame^] | 20 | {"hello", hello_main, TOYFLAG_NOFORK|TOYFLAG_USR}, |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 21 | {"sh", toysh_main, TOYFLAG_BIN}, |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 22 | {"toysh", toysh_main, TOYFLAG_BIN} |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 23 | }; |
| 24 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 25 | #define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list)) |
| 26 | |
landley | c562150 | 2006-09-28 17:18:51 -0400 | [diff] [blame] | 27 | // global context for this applet. |
| 28 | |
| 29 | struct toy_context toys; |
| 30 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 31 | struct toy_list *toy_find(char *name) |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 32 | { |
| 33 | int top, bottom, middle; |
| 34 | |
| 35 | // If the name starts with "toybox", accept that as a match. Otherwise |
| 36 | // skip the first entry, which is out of order. |
| 37 | |
| 38 | if (!strncmp(name,"toybox",6)) return toy_list; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 39 | bottom = 1; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 40 | |
| 41 | // Binary search to find this applet. |
| 42 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 43 | top = TOY_LIST_LEN-1; |
| 44 | for (;;) { |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 45 | int result; |
| 46 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 47 | middle = (top+bottom)/2; |
| 48 | if (middle<bottom || middle>top) return NULL; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 49 | result = strcmp(name,toy_list[middle].name); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 50 | if (!result) return toy_list+middle; |
| 51 | if (result<0) top=--middle; |
| 52 | else bottom = ++middle; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 56 | void toy_init(struct toy_list *which, char *argv[]) |
| 57 | { |
| 58 | // Free old toys contents here? |
| 59 | |
| 60 | toys.which = which; |
| 61 | toys.argv = argv; |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 62 | toys.exitval = 1; |
| 63 | } |
| 64 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 65 | // Run a toy. |
| 66 | void toy_exec(char *argv[]) |
| 67 | { |
| 68 | struct toy_list *which; |
| 69 | |
| 70 | which = toy_find(argv[0]); |
| 71 | if (!which) return; |
| 72 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 73 | toy_init(which, argv); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 74 | |
| 75 | exit(toys.which->toy_main()); |
| 76 | } |
| 77 | |
| 78 | int toybox_main(void) |
| 79 | { |
| 80 | static char *toy_paths[]={"usr/","bin/","sbin/",0}; |
| 81 | int i, len = 0; |
| 82 | |
| 83 | if (toys.argv[1]) { |
| 84 | if (toys.argv[1][0]!='-') { |
| 85 | toy_exec(toys.argv+1); |
| 86 | error_exit("No behavior for %s\n",toys.argv[1]); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Output list of applets. |
| 91 | for (i=1; i<TOY_LIST_LEN; i++) { |
| 92 | int fl = toy_list[i].flags; |
| 93 | if (fl & TOYMASK_LOCATION) { |
| 94 | if (toys.argv[1]) { |
| 95 | int j; |
| 96 | for (j=0; toy_paths[j]; j++) |
| 97 | if (fl & (1<<j)) len += printf("%s", toy_paths[j]); |
| 98 | } |
| 99 | len += printf("%s ",toy_list[i].name); |
| 100 | if (len>65) { |
| 101 | putchar('\n'); |
| 102 | len=0; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | putchar('\n'); |
| 107 | return 0; |
| 108 | } |
| 109 | |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 110 | int main(int argc, char *argv[]) |
| 111 | { |
| 112 | char *name; |
| 113 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 114 | // Figure out which applet to call. |
| 115 | name = rindex(argv[0], '/'); |
| 116 | if (!name) name=argv[0]; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 117 | else name++; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 118 | argv[0] = name; |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 119 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 120 | toys.argv = argv-1; |
| 121 | return toybox_main(); |
landley | 13bab2f | 2006-09-27 00:45:05 -0400 | [diff] [blame] | 122 | } |