annotate main.c @ 226:6aac63925eff

Update web pages.
author Rob Landley <rob@landley.net>
date Sat, 05 Jan 2008 18:09:49 -0600
parents 571623c6765d
children 163498bf547b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
7 #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
8
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
9 // Populate toy_list[].
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
10
88
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
11 #undef NEWTOY
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
12 #undef OLDTOY
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
13 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
14 #define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags},
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
15
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
16 struct toy_list toy_list[] = {
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
17 #include "toys/toylist.h"
1
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
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
20 #define TOY_LIST_LEN (sizeof(toy_list)/sizeof(struct toy_list))
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
21
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
22 // 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
23
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
24 struct toy_context toys;
93
cc0a6789f92a Trim a redundant definition that confuses older compilers. (Regression testing
Rob Landley <rob@landley.net>
parents: 88
diff changeset
25 union toy_union toy;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
26 char toybuf[4096];
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
27
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
28 struct toy_list *toy_find(char *name)
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
29 {
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
30 int top, bottom, middle;
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 // 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
33 // 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
34
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
35 if (!strncmp(name,"toybox",6)) return toy_list;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
36 bottom = 1;
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 // Binary search to find this applet.
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
39
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
40 top = TOY_LIST_LEN-1;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
41 for (;;) {
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
42 int result;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 138
diff changeset
43
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
44 middle = (top+bottom)/2;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
45 if (middle<bottom || middle>top) return NULL;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
46 result = strcmp(name,toy_list[middle].name);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
47 if (!result) return toy_list+middle;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
48 if (result<0) top=--middle;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
49 else bottom = ++middle;
0
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 }
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
52
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
53 // Figure out whether or not anything is using the option parsing logic,
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
54 // because the compiler can't figure out whether or not to optimize it away
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
55 // on its' own.
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
56
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
57 #undef NEWTOY
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
58 #undef OLDTOY
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
59 #define NEWTOY(name, opts, flags) opts ||
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
60 #define OLDTOY(name, oldname, opts, flags) opts ||
125
a4af344fb349 Make warning go away.
Rob Landley <rob@landley.net>
parents: 122
diff changeset
61 static const int NEED_OPTIONS =
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
62 #include "toys/toylist.h"
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
63 0; // Ends the opts || opts || opts...
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
64
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
65 void toy_init(struct toy_list *which, char *argv[])
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
66 {
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
67 // Free old toys contents here?
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
68
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
69 toys.which = which;
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
70 toys.argv = argv;
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
71 if (NEED_OPTIONS && which->options) get_optflags();
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 93
diff changeset
72 else toys.optargs = argv+1;
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
73 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
74
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
75 // Run a toy.
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
76 void toy_exec(char *argv[])
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
77 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
78 struct toy_list *which;
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 93
diff changeset
79
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
80 which = toy_find(argv[0]);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
81 if (!which) return;
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
82 toy_init(which, argv);
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
83 toys.which->toy_main();
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
84 exit(toys.exitval);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
85 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
86
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
87 void toybox_main(void)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
88 {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
89 static char *toy_paths[]={"usr/","bin/","sbin/",0};
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
90 int i, len = 0;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
91
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
92 if (toys.argv[1]) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
93 if (toys.argv[1][0]!='-') {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
94 toy_exec(toys.argv+1);
182
ed26d3529575 Patch from Charlie Shepherd: remove extra \n from error_exit() arguments.
Rob Landley <rob@landley.net>
parents: 156
diff changeset
95 error_exit("Unknown command %s",toys.argv[1]);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
96 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
97 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
98
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
99 // Output list of applets.
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
100 for (i=1; i<TOY_LIST_LEN; i++) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
101 int fl = toy_list[i].flags;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
102 if (fl & TOYMASK_LOCATION) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
103 if (toys.argv[1]) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
104 int j;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
105 for (j=0; toy_paths[j]; j++)
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
106 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
107 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
108 len += printf("%s ",toy_list[i].name);
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
109 if (len>65) {
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
110 putchar('\n');
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
111 len=0;
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
112 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
113 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
114 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
115 putchar('\n');
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
116 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
117
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
118 int main(int argc, char *argv[])
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
119 {
65
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
120 // Artificial scope to eat less stack for things we call
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
121 {
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
122 char *name;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
123
65
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
124 // Figure out which applet to call.
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
125 name = rindex(argv[0], '/');
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
126 if (!name) name=argv[0];
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
127 else name++;
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
128 argv[0] = name;
329e2b37d0e1 Save 4 bytes of stack space.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
129 }
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
130
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
131 toys.argv = argv-1;
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
132 toybox_main();
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
133 return 0;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
134 }