Mercurial > hg > toybox
annotate main.c @ 1:59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
an empty "blah.c" in there to make it happy but I'm not checking that in.
author | landley@driftwood |
---|---|
date | Thu, 28 Sep 2006 17:18:51 -0400 |
parents | toys/main.c@e651a31d5416 |
children | 67b517913e56 |
rev | line source |
---|---|
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
1 /* vi: set ts=4 :*/ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
2 /* Toybox infrastructure. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
3 * |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
4 * Copyright 2006 Rob Landley <rob@landley.net> |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
5 * |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
6 * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
7 */ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
8 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
9 #include "toys.h" |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
10 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
11 // The monster fun applet list. |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
12 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
13 struct toy_list toy_list[] = { |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
14 {"toybox", toybox_main}, |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
15 {"df", df_main}, |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
16 {"toysh", toysh_main} |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
17 }; |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
18 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
19 // global context for this applet. |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
20 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
21 struct toy_context toys; |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
22 |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
0
diff
changeset
|
23 |
0
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
24 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
25 /* |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
26 name |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
27 main() |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
28 struct |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
29 usage (short long example info) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
30 path (/usr/sbin) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
31 */ |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
32 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
33 int toybox_main(void) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
34 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
35 printf("toybox\n"); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
36 return 0; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
37 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
38 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
39 int toysh_main(void) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
40 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
41 printf("toysh\n"); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
42 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
43 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
44 struct toy_list *find_toy_by_name(char *name) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
45 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
46 int top, bottom, middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
47 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
48 // If the name starts with "toybox", accept that as a match. Otherwise |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
49 // skip the first entry, which is out of order. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
50 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
51 if (!strncmp(name,"toybox",6)) return toy_list; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
52 bottom=1; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
53 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
54 // Binary search to find this applet. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
55 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
56 top=(sizeof(toy_list)/sizeof(struct toy_list))-1; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
57 for(;;) { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
58 int result; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
59 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
60 middle=(top+bottom)/2; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
61 if(middle<bottom || middle>top) return NULL; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
62 result = strcmp(name,toy_list[middle].name); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
63 if(!result) return toy_list+middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
64 if(result<0) top=--middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
65 else bottom=++middle; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
66 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
67 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
68 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
69 int main(int argc, char *argv[]) |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
70 { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
71 char *name; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
72 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
73 // Record command line arguments. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
74 toys.argc = argc; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
75 toys.argv = argv; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
76 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
77 // Figure out which applet got called. |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
78 name = rindex(argv[0],'/'); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
79 if (!name) name = argv[0]; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
80 else name++; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
81 toys.which = find_toy_by_name(name); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
82 |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
83 if (!toys.which) { |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
84 dprintf(2,"No behavior for %s\n",name); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
85 return 1; |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
86 } |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
87 return toys.which->toy_main(); |
e651a31d5416
Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff
changeset
|
88 } |