You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stab.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* $Header: stab.c,v 1.0.1.1 88/01/28 10:35:17 root Exp $
  2. *
  3. * $Log: stab.c,v $
  4. * Revision 1.0.1.1 88/01/28 10:35:17 root
  5. * patch8: changed some stabents to support eval operator.
  6. *
  7. * Revision 1.0 87/12/18 13:06:14 root
  8. * Initial revision
  9. *
  10. */
  11. #include <signal.h>
  12. #include "handy.h"
  13. #include "EXTERN.h"
  14. #include "search.h"
  15. #include "util.h"
  16. #include "perl.h"
  17. static char *sig_name[] = {
  18. "",
  19. "HUP",
  20. "INT",
  21. "QUIT",
  22. "ILL",
  23. "TRAP",
  24. "IOT",
  25. "EMT",
  26. "FPE",
  27. "KILL",
  28. "BUS",
  29. "SEGV",
  30. "SYS",
  31. "PIPE",
  32. "ALRM",
  33. "TERM",
  34. "???"
  35. #ifdef SIGTSTP
  36. ,"STOP",
  37. "TSTP",
  38. "CONT",
  39. "CHLD",
  40. "TTIN",
  41. "TTOU",
  42. "TINT",
  43. "XCPU",
  44. "XFSZ"
  45. #ifdef SIGPROF
  46. ,"VTALARM",
  47. "PROF"
  48. #ifdef SIGWINCH
  49. ,"WINCH"
  50. #ifdef SIGLOST
  51. ,"LOST"
  52. #ifdef SIGUSR1
  53. ,"USR1"
  54. #endif
  55. #ifdef SIGUSR2
  56. ,"USR2"
  57. #endif /* SIGUSR2 */
  58. #endif /* SIGLOST */
  59. #endif /* SIGWINCH */
  60. #endif /* SIGPROF */
  61. #endif /* SIGTSTP */
  62. ,0
  63. };
  64. STR *
  65. stab_str(stab)
  66. STAB *stab;
  67. {
  68. register int paren;
  69. register char *s;
  70. extern int errno;
  71. switch (*stab->stab_name) {
  72. case '0': case '1': case '2': case '3': case '4':
  73. case '5': case '6': case '7': case '8': case '9': case '&':
  74. if (curspat) {
  75. paren = atoi(stab->stab_name);
  76. if (curspat->spat_compex.subend[paren] &&
  77. (s = getparen(&curspat->spat_compex,paren))) {
  78. curspat->spat_compex.subend[paren] = Nullch;
  79. str_set(stab->stab_val,s);
  80. }
  81. }
  82. break;
  83. case '+':
  84. if (curspat) {
  85. paren = curspat->spat_compex.lastparen;
  86. if (curspat->spat_compex.subend[paren] &&
  87. (s = getparen(&curspat->spat_compex,paren))) {
  88. curspat->spat_compex.subend[paren] = Nullch;
  89. str_set(stab->stab_val,s);
  90. }
  91. }
  92. break;
  93. case '.':
  94. if (last_in_stab) {
  95. str_numset(stab->stab_val,(double)last_in_stab->stab_io->lines);
  96. }
  97. break;
  98. case '?':
  99. str_numset(stab->stab_val,(double)statusvalue);
  100. break;
  101. case '^':
  102. s = curoutstab->stab_io->top_name;
  103. str_set(stab->stab_val,s);
  104. break;
  105. case '~':
  106. s = curoutstab->stab_io->fmt_name;
  107. str_set(stab->stab_val,s);
  108. break;
  109. case '=':
  110. str_numset(stab->stab_val,(double)curoutstab->stab_io->lines);
  111. break;
  112. case '-':
  113. str_numset(stab->stab_val,(double)curoutstab->stab_io->lines_left);
  114. break;
  115. case '%':
  116. str_numset(stab->stab_val,(double)curoutstab->stab_io->page);
  117. break;
  118. case '(':
  119. if (curspat) {
  120. str_numset(stab->stab_val,(double)(curspat->spat_compex.subbeg[0] -
  121. curspat->spat_compex.subbase));
  122. }
  123. break;
  124. case ')':
  125. if (curspat) {
  126. str_numset(stab->stab_val,(double)(curspat->spat_compex.subend[0] -
  127. curspat->spat_compex.subbeg[0]));
  128. }
  129. break;
  130. case '/':
  131. *tokenbuf = record_separator;
  132. tokenbuf[1] = '\0';
  133. str_set(stab->stab_val,tokenbuf);
  134. break;
  135. case '[':
  136. str_numset(stab->stab_val,(double)arybase);
  137. break;
  138. case '|':
  139. str_numset(stab->stab_val,
  140. (double)((curoutstab->stab_io->flags & IOF_FLUSH) != 0) );
  141. break;
  142. case ',':
  143. str_set(stab->stab_val,ofs);
  144. break;
  145. case '\\':
  146. str_set(stab->stab_val,ors);
  147. break;
  148. case '#':
  149. str_set(stab->stab_val,ofmt);
  150. break;
  151. case '!':
  152. str_numset(stab->stab_val,(double)errno);
  153. break;
  154. }
  155. return stab->stab_val;
  156. }
  157. stabset(stab,str)
  158. register STAB *stab;
  159. STR *str;
  160. {
  161. char *s;
  162. int i;
  163. int sighandler();
  164. if (stab->stab_flags & SF_VMAGIC) {
  165. switch (stab->stab_name[0]) {
  166. case '^':
  167. safefree(curoutstab->stab_io->top_name);
  168. curoutstab->stab_io->top_name = str_get(str);
  169. curoutstab->stab_io->top_stab = stabent(str_get(str),TRUE);
  170. break;
  171. case '~':
  172. safefree(curoutstab->stab_io->fmt_name);
  173. curoutstab->stab_io->fmt_name = str_get(str);
  174. curoutstab->stab_io->fmt_stab = stabent(str_get(str),TRUE);
  175. break;
  176. case '=':
  177. curoutstab->stab_io->page_len = (long)str_gnum(str);
  178. break;
  179. case '-':
  180. curoutstab->stab_io->lines_left = (long)str_gnum(str);
  181. break;
  182. case '%':
  183. curoutstab->stab_io->page = (long)str_gnum(str);
  184. break;
  185. case '|':
  186. curoutstab->stab_io->flags &= ~IOF_FLUSH;
  187. if (str_gnum(str) != 0.0) {
  188. curoutstab->stab_io->flags |= IOF_FLUSH;
  189. }
  190. break;
  191. case '*':
  192. multiline = (int)str_gnum(str) != 0;
  193. break;
  194. case '/':
  195. record_separator = *str_get(str);
  196. break;
  197. case '\\':
  198. if (ors)
  199. safefree(ors);
  200. ors = savestr(str_get(str));
  201. break;
  202. case ',':
  203. if (ofs)
  204. safefree(ofs);
  205. ofs = savestr(str_get(str));
  206. break;
  207. case '#':
  208. if (ofmt)
  209. safefree(ofmt);
  210. ofmt = savestr(str_get(str));
  211. break;
  212. case '[':
  213. arybase = (int)str_gnum(str);
  214. break;
  215. case '!':
  216. errno = (int)str_gnum(str); /* will anyone ever use this? */
  217. break;
  218. case '.':
  219. case '+':
  220. case '&':
  221. case '0':
  222. case '1':
  223. case '2':
  224. case '3':
  225. case '4':
  226. case '5':
  227. case '6':
  228. case '7':
  229. case '8':
  230. case '9':
  231. case '(':
  232. case ')':
  233. break; /* "read-only" registers */
  234. }
  235. }
  236. else if (stab == envstab && envname) {
  237. setenv(envname,str_get(str),1); /* rhaamo: added 1 (overwrite) */
  238. /* And you'll never guess what the dog had */
  239. safefree(envname); /* in its mouth... */
  240. envname = Nullch;
  241. }
  242. else if (stab == sigstab && signame) {
  243. s = str_get(str);
  244. i = whichsig(signame); /* ...no, a brick */
  245. if (strEQ(s,"IGNORE"))
  246. signal(i,SIG_IGN);
  247. else if (strEQ(s,"DEFAULT") || !*s)
  248. signal(i,SIG_DFL);
  249. else
  250. signal(i,sighandler);
  251. safefree(signame);
  252. signame = Nullch;
  253. }
  254. }
  255. whichsig(signame)
  256. char *signame;
  257. {
  258. register char **sigv;
  259. for (sigv = sig_name+1; *sigv; sigv++)
  260. if (strEQ(signame,*sigv))
  261. return sigv - sig_name;
  262. return 0;
  263. }
  264. sighandler(sig)
  265. int sig;
  266. {
  267. STAB *stab;
  268. ARRAY *savearray;
  269. STR *str;
  270. stab = stabent(str_get(hfetch(sigstab->stab_hash,sig_name[sig])),TRUE);
  271. savearray = defstab->stab_array;
  272. defstab->stab_array = anew();
  273. str = str_new(0);
  274. str_set(str,sig_name[sig]);
  275. apush(defstab->stab_array,str);
  276. str = cmd_exec(stab->stab_sub);
  277. afree(defstab->stab_array); /* put back old $_[] */
  278. defstab->stab_array = savearray;
  279. }
  280. char *
  281. reg_get(name)
  282. char *name;
  283. {
  284. return STAB_GET(stabent(name,TRUE));
  285. }
  286. #ifdef NOTUSED
  287. reg_set(name,value)
  288. char *name;
  289. char *value;
  290. {
  291. str_set(STAB_STR(stabent(name,TRUE)),value);
  292. }
  293. #endif
  294. STAB *
  295. aadd(stab)
  296. register STAB *stab;
  297. {
  298. if (!stab->stab_array)
  299. stab->stab_array = anew();
  300. return stab;
  301. }
  302. STAB *
  303. hadd(stab)
  304. register STAB *stab;
  305. {
  306. if (!stab->stab_hash)
  307. stab->stab_hash = hnew();
  308. return stab;
  309. }