rc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Plan9 is defined for plan 9
  3. * Unix is defined for Unix
  4. * Please don't litter the code with ifdefs. The two below should be enough.
  5. */
  6. #define Unix
  7. #ifdef Plan9
  8. #include <u.h>
  9. #include <libc.h>
  10. #define NSIG 32
  11. #define SIGINT 2
  12. #define SIGQUIT 3
  13. #endif
  14. #ifdef Unix
  15. #define _BSD_EXTENSION
  16. #define _PLAN9_SOURCE
  17. #define _POSIX_SOURCE
  18. #define _RESEARCH_SOURCE
  19. #define _SUSV2_SOURCE
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <lib9.h>
  26. #include <signal.h>
  27. #include <inttypes.h>
  28. #ifndef NSIG
  29. #define NSIG 32
  30. #endif
  31. #define uintptr uintptr_t
  32. #endif
  33. #ifndef ERRMAX
  34. #define ERRMAX 128
  35. #endif
  36. #define YYMAXDEPTH 500
  37. #ifndef PAREN
  38. #include "x.tab.h"
  39. #endif
  40. typedef struct tree tree;
  41. typedef struct word word;
  42. typedef struct io io;
  43. typedef union code code;
  44. typedef struct var var;
  45. typedef struct list list;
  46. typedef struct redir redir;
  47. typedef struct thread thread;
  48. typedef struct builtin builtin;
  49. #ifdef Plan9
  50. #pragma incomplete word
  51. #pragma incomplete io
  52. #endif
  53. struct tree{
  54. int type;
  55. int rtype, fd0, fd1; /* details of REDIR PIPE DUP tokens */
  56. char *str;
  57. int quoted;
  58. int iskw;
  59. tree *child[3];
  60. tree *next;
  61. };
  62. tree *newtree(void);
  63. tree *token(char*, int), *klook(char*), *tree1(int, tree*);
  64. tree *tree2(int, tree*, tree*), *tree3(int, tree*, tree*, tree*);
  65. tree *mung1(tree*, tree*), *mung2(tree*, tree*, tree*);
  66. tree *mung3(tree*, tree*, tree*, tree*), *epimung(tree*, tree*);
  67. tree *simplemung(tree*), *heredoc(tree*);
  68. void freetree(tree*);
  69. tree *cmdtree;
  70. /*
  71. * The first word of any code vector is a reference count.
  72. * Always create a new reference to a code vector by calling codecopy(.).
  73. * Always call codefree(.) when deleting a reference.
  74. */
  75. union code{
  76. void (*f)(void);
  77. int i;
  78. char *s;
  79. };
  80. char *promptstr;
  81. int doprompt;
  82. #define NTOK 8192
  83. char tok[NTOK];
  84. #define APPEND 1
  85. #define WRITE 2
  86. #define READ 3
  87. #define HERE 4
  88. #define DUPFD 5
  89. #define CLOSE 6
  90. #define RDWR 7
  91. struct var{
  92. char *name; /* ascii name */
  93. word *val; /* value */
  94. int changed;
  95. code *fn; /* pointer to function's code vector */
  96. int fnchanged;
  97. int pc; /* pc of start of function */
  98. var *next; /* next on hash or local list */
  99. };
  100. var *vlook(char*), *gvlook(char*), *newvar(char*, var*);
  101. #define NVAR 521
  102. var *gvar[NVAR]; /* hash for globals */
  103. #define new(type) ((type *)emalloc(sizeof(type)))
  104. void *emalloc(long);
  105. void *Malloc(ulong);
  106. void efree(void *);
  107. #define NOFILE 128 /* should come from <param.h> */
  108. struct here{
  109. tree *tag;
  110. char *name;
  111. struct here *next;
  112. };
  113. int mypid;
  114. /*
  115. * Glob character escape in strings:
  116. * In a string, GLOB must be followed by *?[ or GLOB.
  117. * GLOB* matches any string
  118. * GLOB? matches any single character
  119. * GLOB[...] matches anything in the brackets
  120. * GLOBGLOB matches GLOB
  121. */
  122. #define GLOB ((char)0x01)
  123. /*
  124. * onebyte(c), twobyte(c), threebyte(c)
  125. * Is c the first character of a one- two- or three-byte utf sequence?
  126. */
  127. #define onebyte(c) ((c&0x80)==0x00)
  128. #define twobyte(c) ((c&0xe0)==0xc0)
  129. #define threebyte(c) ((c&0xf0)==0xe0)
  130. char **argp;
  131. char **args;
  132. int nerror; /* number of errors encountered during compilation */
  133. int doprompt; /* is it time for a prompt? */
  134. /*
  135. * Which fds are the reading/writing end of a pipe?
  136. * Unfortunately, this can vary from system to system.
  137. * 9th edition Unix doesn't care, the following defines
  138. * work on plan 9.
  139. */
  140. #define PRD 0
  141. #define PWR 1
  142. char Rcmain[], Fdprefix[];
  143. /*
  144. * How many dot commands have we executed?
  145. * Used to ensure that -v flag doesn't print rcmain.
  146. */
  147. int ndot;
  148. char *getstatus(void);
  149. int lastc;
  150. int lastword;