toys.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Toybox infrastructure.
  2. *
  3. * Copyright 2006 Rob Landley <[email protected]>
  4. */
  5. // Stuff that needs to go before the standard headers
  6. #include "generated/config.h"
  7. #include "lib/portability.h"
  8. // General posix-2008 headers
  9. #include <ctype.h>
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <grp.h>
  14. #include <inttypes.h>
  15. #include <limits.h>
  16. #include <math.h>
  17. #include <paths.h>
  18. #include <pwd.h>
  19. #include <regex.h>
  20. #include <sched.h>
  21. #include <setjmp.h>
  22. #include <signal.h>
  23. #include <stdarg.h>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <strings.h>
  30. #include <sys/mman.h>
  31. #include <sys/resource.h>
  32. #include <sys/stat.h>
  33. #include <sys/statvfs.h>
  34. #include <sys/time.h>
  35. #include <sys/times.h>
  36. #include <sys/uio.h>
  37. #include <sys/utsname.h>
  38. #include <sys/wait.h>
  39. #include <syslog.h>
  40. #include <termios.h>
  41. #include <time.h>
  42. #include <unistd.h>
  43. #include <utime.h>
  44. // Posix networking
  45. #include <arpa/inet.h>
  46. #include <netdb.h>
  47. #include <net/if.h>
  48. #include <netinet/in.h>
  49. #include <netinet/tcp.h>
  50. #include <poll.h>
  51. #include <sys/socket.h>
  52. #include <sys/un.h>
  53. // Internationalization support (also in POSIX)
  54. #include <langinfo.h>
  55. #include <locale.h>
  56. #include <wchar.h>
  57. #include <wctype.h>
  58. // Non-posix headers
  59. #include <sys/ioctl.h>
  60. #include <sys/syscall.h>
  61. #include <sys/ttydefaults.h>
  62. #include "lib/lib.h"
  63. #include "lib/lsm.h"
  64. #include "lib/toyflags.h"
  65. // Get list of function prototypes for all enabled command_main() functions.
  66. #define NEWTOY(name, opts, flags) void name##_main(void);
  67. #define OLDTOY(name, oldname, flags) void oldname##_main(void);
  68. #include "generated/newtoys.h"
  69. #include "generated/flags.h"
  70. #include "generated/globals.h"
  71. #include "generated/tags.h"
  72. // These live in main.c
  73. #define HELP_USAGE 1 // usage: line only
  74. #define HELP_HEADER 2 // Add Toybox header line to help output
  75. #define HELP_SEE 4 // "See COMMAND" instead of dereferencing alias
  76. #define HELP_HTML 8 // Output HTML
  77. struct toy_list *toy_find(char *name);
  78. void show_help(int full);
  79. void check_help(char **arg);
  80. void toy_singleinit(struct toy_list *which, char *argv[]);
  81. void toy_init(struct toy_list *which, char *argv[]);
  82. void toy_exec_which(struct toy_list *which, char *argv[]);
  83. void toy_exec(char *argv[]);
  84. // Array of available commands
  85. extern struct toy_list {
  86. char *name;
  87. void (*toy_main)(void);
  88. char *options;
  89. unsigned flags;
  90. } toy_list[];
  91. // Global context shared by all commands.
  92. extern struct toy_context {
  93. struct toy_list *which; // Which entry in toy_list is this one?
  94. char **argv; // Original command line arguments
  95. char **optargs; // Arguments left over from get_optflags()
  96. unsigned long long optflags; // Command line option flags from get_optflags()
  97. int optc; // Count of optargs
  98. short toycount; // Total number of commands in this build
  99. char exitval; // Value error_exit feeds to exit()
  100. char wasroot; // dropped setuid
  101. // toy_init() should not zero past here.
  102. sigjmp_buf *rebound; // siglongjmp here instead of exit when do_rebound
  103. struct arg_list *xexit; // atexit() functions for xexit(), set by sigatexit()
  104. void *stacktop; // nested toy_exec() call count, or 0 if vforked
  105. int envc; // Count of original environ entries
  106. int old_umask; // Old umask preserved by TOYFLAG_UMASK
  107. short signal; // generic_signal() records what signal it saw here
  108. int signalfd; // and writes signal to this fd, if set
  109. } toys;
  110. // Two big temporary buffers: one for use by commands, one for library functions
  111. extern char **environ, *toybox_version, toybuf[4096], libbuf[4096];
  112. #define FLAG(x) (!!(toys.optflags&FLAG_##x)) // Return 1 if flag set, 0 if not
  113. #define GLOBALS(...)
  114. #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
  115. #define TAGGED_ARRAY(X, ...) {__VA_ARGS__}
  116. #ifndef TOYBOX_VERSION
  117. #ifndef TOYBOX_VENDOR
  118. #define TOYBOX_VENDOR ""
  119. #endif
  120. #define TOYBOX_VERSION "0.8.13"TOYBOX_VENDOR
  121. #endif