shrub/vere/reck.c

416 lines
8.7 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* v/reck.c
**
** This file is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <dirent.h>
#include <stdint.h>
#include <uv.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include "all.h"
#include "vere/vere.h"
2013-09-29 00:21:18 +04:00
/* _reck_mole(): parse simple atomic mole.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_mole(u3_noun fot,
u3_noun san,
2013-09-29 00:21:18 +04:00
c3_d* ato_d)
{
2014-11-06 06:10:22 +03:00
u3_noun uco = u3do("slay", san);
2014-09-11 04:01:32 +04:00
u3_noun p_uco, q_uco, r_uco, s_uco;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_qual(uco, &p_uco, &q_uco, &r_uco, &s_uco)) ||
2013-09-29 00:21:18 +04:00
(0 != p_uco) ||
(0 != q_uco) ||
2014-11-06 03:20:01 +03:00
(c3n == u3r_sing(fot, r_uco)) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
uL(fprintf(uH, "strange mole %s\n", u3r_string(san)));
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(fot); u3z(uco); return c3n;
2013-09-29 00:21:18 +04:00
}
else {
2014-11-06 03:20:01 +03:00
*ato_d = u3r_chub(0, s_uco);
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(fot); u3z(uco); return c3y;
2013-09-29 00:21:18 +04:00
}
}
/* _reck_lily(): parse little atom.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_lily(u3_noun fot, u3_noun txt, c3_l* tid_l)
2013-09-29 00:21:18 +04:00
{
c3_d ato_d;
2014-11-05 04:18:47 +03:00
if ( c3n == _reck_mole(fot, txt, &ato_d) ) {
return c3n;
2013-09-29 00:21:18 +04:00
} else {
if ( ato_d >= 0x80000000ULL ) {
2014-11-05 04:18:47 +03:00
return c3n;
2013-09-29 00:21:18 +04:00
} else {
*tid_l = (c3_l) ato_d;
2014-11-05 04:18:47 +03:00
return c3y;
2013-09-29 00:21:18 +04:00
}
}
}
/* _reck_kick_term(): apply terminal outputs.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_term(u3_noun pox, c3_l tid_l, u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun p_fav;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(fav) ) {
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
else switch ( u3h(fav) ) {
2014-11-05 04:18:47 +03:00
default: u3z(pox); u3z(fav); return c3n;
case c3__bbye:
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
} break;
2014-09-11 04:01:32 +04:00
case c3__blit: p_fav = u3t(fav);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_term_ef_blit(tid_l, u3k(p_fav));
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
case c3__logo:
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3_Host.liv = c3n;
2014-09-11 04:01:32 +04:00
u3_Host.xit_i = u3t(fav);
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
2014-09-11 04:01:32 +04:00
case c3__init: p_fav = u3t(fav);
2013-09-29 00:21:18 +04:00
{
2018-08-10 19:58:21 +03:00
c3_assert( u3_none == u3A->own );
u3A->own = u3k(p_fav);
2013-09-29 00:21:18 +04:00
2018-08-10 19:58:21 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
2015-05-07 05:25:41 +03:00
case c3__mass: p_fav = u3t(fav);
{
u3A->sac = u3k(p_fav);
2015-05-07 05:25:41 +03:00
u3z(pox); u3z(fav); return c3y;
} break;
2013-09-29 00:21:18 +04:00
}
c3_assert(!"not reached"); return 0;
}
/* _reck_kick_http(): apply http effects.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_http(u3_noun pox,
2014-03-20 02:40:40 +04:00
c3_l sev_l,
2013-09-29 00:21:18 +04:00
c3_l coq_l,
c3_l seq_l,
2014-09-11 04:01:32 +04:00
u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun p_fav, q_fav;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(fav) ) {
u3z(pox); u3z(fav); return c3n;
}
2014-09-11 04:01:32 +04:00
else switch ( u3h(fav) ) {
2014-11-05 04:18:47 +03:00
default: u3z(pox); u3z(fav); return c3n;
2013-10-27 07:55:53 +04:00
case c3__form: p_fav = u3t(fav);
{
u3_http_ef_form(u3k(p_fav));
u3z(pox); u3z(fav);
return c3y;
}
case c3__that: p_fav = u3t(fav);
{
u3_http_ef_that(u3k(p_fav));
u3z(pox); u3z(fav);
return c3y;
}
2014-09-11 04:01:32 +04:00
case c3__thus: p_fav = u3h(u3t(fav)); q_fav = u3t(u3t(fav));
2013-10-27 07:55:53 +04:00
{
2014-11-06 03:20:01 +03:00
u3_cttp_ef_thus(u3r_word(0, p_fav), u3k(q_fav));
2014-09-11 04:01:32 +04:00
u3z(pox); u3z(fav);
2014-11-05 04:18:47 +03:00
return c3y;
2013-10-27 07:55:53 +04:00
}
2014-09-11 04:01:32 +04:00
case c3__thou: p_fav = u3t(fav);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_http_ef_thou(sev_l, coq_l, seq_l, u3k(p_fav));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(pox); u3z(fav);
2014-11-05 04:18:47 +03:00
return c3y;
2013-09-29 00:21:18 +04:00
} break;
}
2014-11-05 04:18:47 +03:00
c3_assert(!"not reached"); return c3n;
2013-09-29 00:21:18 +04:00
}
/* _reck_kick_sync(): apply sync outputs.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_sync(u3_noun pox, u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
switch ( u3h(fav) ) {
2013-09-29 00:21:18 +04:00
default: break;
case c3__ergo: {
2015-06-05 05:47:27 +03:00
u3_noun mon = u3k(u3h(u3t(fav)));
u3_noun can = u3k(u3t(u3t(fav)));
2013-09-29 00:21:18 +04:00
2015-06-05 05:47:27 +03:00
u3_unix_ef_ergo(mon, can);
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
2015-06-09 20:27:22 +03:00
case c3__ogre: {
u3_unix_ef_ogre(u3k(u3t(fav)));
u3z(pox); u3z(fav); return c3y;
}
case c3__hill: {
u3_unix_ef_hill(u3k(u3t(fav)));
u3z(pox); u3z(fav); return c3y;
}
2013-09-29 00:21:18 +04:00
}
// XX obviously not right!
2015-06-09 20:27:22 +03:00
// ? looks fine to me
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_newt(u3_noun pox, u3_noun fav)
{
2014-09-11 04:01:32 +04:00
switch ( u3h(fav) ) {
default: break;
case c3__send: {
2014-09-11 04:01:32 +04:00
u3_noun lan = u3k(u3h(u3t(fav)));
u3_noun pac = u3k(u3t(u3t(fav)));
2014-09-11 04:01:32 +04:00
u3_ames_ef_send(lan, pac);
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
} break;
}
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
}
2013-09-29 00:21:18 +04:00
/* _reck_kick_ames(): apply packet network outputs.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_ames(u3_noun pox, u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun p_fav;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
switch ( u3h(fav) ) {
2013-09-29 00:21:18 +04:00
default: break;
2018-08-10 19:58:21 +03:00
// XX remove
2014-09-11 04:01:32 +04:00
case c3__init: p_fav = u3t(fav);
2013-09-29 00:21:18 +04:00
{
2018-08-10 19:58:21 +03:00
uL(fprintf(uH, "kick: init: ames\n"));
c3_assert(0);
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
}
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
/* _reck_kick_spec(): apply an effect, by path.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_spec(u3_noun pox, u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun i_pox, t_pox;
2014-11-20 05:36:36 +03:00
u3_noun p_fav;
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(pox, &i_pox, &t_pox)) ||
2014-09-11 04:01:32 +04:00
((i_pox != u3_blip) &&
2014-06-24 21:32:59 +04:00
(i_pox != c3__gold) &&
(i_pox != c3__iron) &&
(i_pox != c3__lead)) )
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
} else {
2014-09-11 04:01:32 +04:00
u3_noun it_pox, tt_pox;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(t_pox, &it_pox, &tt_pox)) ) {
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
else switch ( it_pox ) {
default: u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
case c3__http: {
2014-09-11 04:01:32 +04:00
u3_noun pud = tt_pox;
u3_noun p_pud, t_pud, tt_pud, q_pud, r_pud, s_pud;
2014-03-20 02:40:40 +04:00
c3_l sev_l, coq_l, seq_l;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(pud, &p_pud, &t_pud)) ||
2014-11-05 04:18:47 +03:00
(c3n == _reck_lily(c3__uv, u3k(p_pud), &sev_l)) )
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
if ( u3_nul == t_pud ) {
2013-09-29 00:21:18 +04:00
coq_l = seq_l = 0;
}
else {
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(t_pud, &q_pud, &tt_pud)) ||
2014-11-05 04:18:47 +03:00
(c3n == _reck_lily(c3__ud, u3k(q_pud), &coq_l)) )
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
if ( u3_nul == tt_pud ) {
2013-09-29 00:21:18 +04:00
seq_l = 0;
} else {
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(tt_pud, &r_pud, &s_pud)) ||
2014-09-11 04:01:32 +04:00
(u3_nul != s_pud) ||
2014-11-05 04:18:47 +03:00
(c3n == _reck_lily(c3__ud, u3k(r_pud), &seq_l)) )
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
}
}
2014-09-11 04:01:32 +04:00
return _reck_kick_http(pox, sev_l, coq_l, seq_l, fav);
2013-09-29 00:21:18 +04:00
} break;
case c3__clay:
case c3__boat:
2013-09-29 00:21:18 +04:00
case c3__sync: {
2014-09-11 04:01:32 +04:00
return _reck_kick_sync(pox, fav);
2013-09-29 00:21:18 +04:00
} break;
case c3__newt: {
2014-09-11 04:01:32 +04:00
return _reck_kick_newt(pox, fav);
} break;
2014-07-10 02:59:22 +04:00
2013-09-29 00:21:18 +04:00
case c3__ames: {
2014-09-11 04:01:32 +04:00
if ( (u3_nul != tt_pox) ) {
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
else {
2014-09-11 04:01:32 +04:00
return _reck_kick_ames(pox, fav);
2013-09-29 00:21:18 +04:00
}
} break;
2018-08-10 19:58:21 +03:00
// XX remove
2014-11-20 05:36:36 +03:00
case c3__init: p_fav = u3t(fav);
{
2018-08-10 19:58:21 +03:00
uL(fprintf(uH, "kick: init: spec\n"));
c3_assert(0);
2014-11-20 05:36:36 +03:00
u3z(pox); u3z(fav); return c3y;
} break;
2013-09-29 00:21:18 +04:00
case c3__term: {
2014-09-11 04:01:32 +04:00
u3_noun pud = tt_pox;
u3_noun p_pud, q_pud;
2013-09-29 00:21:18 +04:00
c3_l tid_l;
2014-11-06 03:20:01 +03:00
if ( (c3n == u3r_cell(pud, &p_pud, &q_pud)) ||
2014-09-11 04:01:32 +04:00
(u3_nul != q_pud) ||
2014-11-05 04:18:47 +03:00
(c3n == _reck_lily(c3__ud, u3k(p_pud), &tid_l)) )
2013-09-29 00:21:18 +04:00
{
uL(fprintf(uH, "term: bad tire\n"));
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
} else {
2014-09-11 04:01:32 +04:00
return _reck_kick_term(pox, tid_l, fav);
2013-09-29 00:21:18 +04:00
}
} break;
}
}
c3_assert(!"not reached");
2014-11-05 04:18:47 +03:00
return c3n;
2013-09-29 00:21:18 +04:00
}
/* _reck_kick_norm(): non path-specific effect handling.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_reck_kick_norm(u3_noun pox, u3_noun fav)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(fav) ) {
u3z(pox); u3z(fav); return c3n;
}
2014-09-11 04:01:32 +04:00
else switch ( u3h(fav) ) {
2014-11-05 04:18:47 +03:00
default: u3z(pox); u3z(fav); return c3n;
2014-02-15 05:35:22 +04:00
case c3__vega:
{
2014-10-26 01:28:14 +04:00
uL(fprintf(uH, "<<<reset>>>\n"));
2014-09-11 04:01:32 +04:00
u3z(pox); u3z(fav);
2014-02-15 05:35:22 +04:00
2014-09-11 04:01:32 +04:00
// u3_ds_wipe(u3_Wire); // doesn't work
2014-02-15 05:35:22 +04:00
2014-11-05 04:18:47 +03:00
return c3y;
2014-02-15 05:35:22 +04:00
}
2013-09-29 00:21:18 +04:00
case c3__exit:
{
uL(fprintf(uH, "<<<goodbye>>>\n"));
2014-09-11 04:01:32 +04:00
u3_lo_bail();
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
u3z(pox); u3z(fav); return c3y;
2013-09-29 00:21:18 +04:00
} break;
}
2014-11-05 04:18:47 +03:00
c3_assert(!"not reached"); return c3n;
u3z(pox); u3z(fav); return c3n;
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_reck_kick(): handle effect.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_reck_kick(u3_noun ovo)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( (c3n == _reck_kick_spec(u3k(u3h(ovo)), u3k(u3t(ovo)))) &&
(c3n == _reck_kick_norm(u3k(u3h(ovo)), u3k(u3t(ovo)))) )
2013-09-29 00:21:18 +04:00
{
#if 0
2014-09-11 04:01:32 +04:00
if ( (c3__warn != u3h(u3t(ovo))) &&
(c3__text != u3h(u3t(ovo))) &&
(c3__note != u3h(u3t(ovo))) )
2013-09-29 00:21:18 +04:00
#endif
#if 1
2014-09-11 04:01:32 +04:00
if ( (c3__crud == u3h(u3t(ovo))) )
2013-09-29 00:21:18 +04:00
#if 0
2014-09-11 04:01:32 +04:00
(c3__talk == u3h(u3t(ovo))) ||
(c3__helo == u3h(u3t(ovo))) ||
(c3__init == u3h(u3t(ovo))) )
2013-09-29 00:21:18 +04:00
#endif
{
2014-11-06 03:20:01 +03:00
u3v_plan(u3nt(u3_blip, c3__term, u3_nul),
2014-09-11 04:01:32 +04:00
u3nc(c3__flog, u3k(u3t(ovo))));
2013-09-29 00:21:18 +04:00
}
else {
2015-05-14 00:18:43 +03:00
u3_noun tox = u3do("spat", u3k(u3h(ovo)));
uL(fprintf(uH, "kick: lost %%%s on %s\n",
2014-11-06 03:20:01 +03:00
u3r_string(u3h(u3t(ovo))),
u3r_string(tox)));
2015-05-14 00:18:43 +03:00
u3z(tox);
2013-09-29 00:21:18 +04:00
#if 0
2014-09-11 04:01:32 +04:00
if ( c3__hear == u3h(u3t(ovo)) ) {
2013-09-29 00:21:18 +04:00
c3_assert(0);
}
#endif
}
#endif
}
2014-09-11 04:01:32 +04:00
u3z(ovo);
2013-09-29 00:21:18 +04:00
}