1 /* Toybox infrastructure.
3 * Copyright 2006 Rob Landley <rob@landley.net>
6 // Stuff that needs to go before the standard headers
8 #include "generated/config.h"
9 #include "lib/portability.h"
11 // General posix-2008 headers
34 #include <sys/resource.h>
36 #include <sys/statvfs.h>
38 #include <sys/times.h>
39 #include <sys/types.h>
40 #include <sys/utsname.h>
50 #include <arpa/inet.h>
53 #include <netinet/in.h>
54 #include <netinet/tcp.h>
56 #include <sys/socket.h>
59 // Internationalization support (also in POSIX and LSB)
66 #include <sys/ioctl.h>
67 #include <sys/statfs.h>
68 #include <sys/sysinfo.h>
71 #include "toys/e2fs.h"
73 // Get list of function prototypes for all enabled command_main() functions.
75 #define NEWTOY(name, opts, flags) void name##_main(void);
76 #define OLDTOY(name, oldname, opts, flags) void oldname##_main(void);
77 #include "generated/newtoys.h"
78 #include "generated/oldtoys.h"
79 #include "generated/flags.h"
80 #include "generated/globals.h"
82 // These live in main.c
84 struct toy_list *toy_find(char *name);
85 void toy_init(struct toy_list *which, char *argv[]);
86 void toy_exec(char *argv[]);
88 // Flags describing command behavior.
90 #define TOYFLAG_USR (1<<0)
91 #define TOYFLAG_BIN (1<<1)
92 #define TOYFLAG_SBIN (1<<2)
93 #define TOYMASK_LOCATION ((1<<4)-1)
95 // This is a shell built-in function, running in the same process context.
96 #define TOYFLAG_NOFORK (1<<4)
98 // Start command with a umask of 0 (saves old umask in this.old_umask)
99 #define TOYFLAG_UMASK (1<<5)
101 // This command runs as root.
102 #define TOYFLAG_STAYROOT (1<<6)
103 #define TOYFLAG_NEEDROOT (1<<7)
104 #define TOYFLAG_ROOTONLY (TOYFLAG_STAYROOT|TOYFLAG_NEEDROOT)
106 // Call setlocale to listen to environment variables.
107 // This invalidates sprintf("%.*s", size, string) as a valid length constraint.
108 #define TOYFLAG_LOCALE (1<<8)
110 // Array of available commands
112 extern struct toy_list {
114 void (*toy_main)(void);
119 // Global context shared by all commands.
121 extern struct toy_context {
122 struct toy_list *which; // Which entry in toy_list is this one?
123 char **argv; // Original command line arguments
124 char **optargs; // Arguments left over from get_optflags()
125 unsigned optflags; // Command line option flags from get_optflags()
126 int exitval; // Value error_exit feeds to exit()
127 int optc; // Count of optargs
128 int exithelp; // Should error_exit print a usage message first?
129 int old_umask; // Old umask preserved by TOYFLAG_UMASK
130 int toycount; // Total number of commands in this build
131 int signal; // generic_signal() records what signal it saw here
132 int signalfd; // and writes signal to this fd, if set
134 // This is at the end so toy_init() doesn't zero it.
135 jmp_buf *rebound; // longjmp here instead of exit when do_rebound set
136 int recursion; // How many nested calls to toy_exec()
139 // Two big temporary buffers: one for use by commands, one for library functions
141 extern char toybuf[4096], libbuf[4096];
143 extern char **environ;
147 #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))