void debug_cmd_out(int ip,integer op,int arg1,int arg2, int optype) { int j; const opdef *opdata; rbool save_dbg_nomsg; dbgprintf(" %2d:",ip); save_dbg_nomsg=0; /* Just to silence compiler warnings. */ opdata=get_opdef(op); if (opdata==&illegal_def) dbgprintf("ILLEGAL %d\n",op); else { if (op>=END_ACT) dbgprintf("!"); /* "Terminal" Actions */ else if (op<=MAX_COND) dbgprintf("?"); /* Condition */ if (op==1063) { /* RandomMessage needs special handling */ save_dbg_nomsg=dbg_nomsg; dbg_nomsg=1; } dbgprintf("%s",opdata->opcode); for(j=0;j<opdata->argnum;j++) { dbgprintf("\t"); argout(j==0 ? opdata->arg1 : opdata->arg2 , j==0 ? arg1 : arg2, optype>>2); optype<<=2; } if (op==1063) dbg_nomsg=save_dbg_nomsg; } debug_newline(op,1); }
void udp_receive(void) { #ifdef UTILS_WERKTI_MORE // Update werkti udp in werkti_udp_in += buffer_in_length; #endif // UTILS_WERKTI_MORE debug_string_p(PSTR("UDP: received\r\n")); uint16_t port; void (*callback)(uint8_t *data, uint16_t length); // Get the port port = ((uint16_t)buffer_in[UDP_PTR_PORT_DST_H]) << 8; port |= buffer_in[UDP_PTR_PORT_DST_L]; debug_string_p(PSTR("UDP: Port: ")); debug_number(port); debug_newline(); // Check if a listener is registered for this port debug_string_p(PSTR("UDP: Search service...")); callback = port_service_get(port_services, NET_UDP_SERVICES_LIST_SIZE, port); debug_string_p(PSTR("done\r\n")); if (callback) { debug_string_p(PSTR("UDP: Found callback function\r\n")); uint16_t length = ((uint16_t)buffer_in[IP_PTR_LENGTH_H]) << 8; length |= buffer_in[IP_PTR_LENGTH_L]; length -= IP_LEN_HEADER + UDP_LEN_HEADER; debug_string_p(PSTR("UDP: Calling callback function\r\n")); callback(&buffer_in[UDP_PTR_DATA], length); // Execute callback debug_string_p(PSTR("UDP: Callback function returned\r\n")); } debug_string_p(PSTR("UDP: handled\r\n")); }
static int run_metacommand(int cnum, int *redir_offset) /* cnum=command number to run. */ /* *redir_offset=offset of redirect header, if we exit with redirection. */ /* Return 0 to go on to next metacommand, 1 to stop running metacommands, and 2 to end the turn. 3 indicates that redirection has just occured 4 indicates a subcall has just occured. 5 Is used to go on to the next metacommand after a Return. -2 means we're doing disambiguation and just hit an action token. */ { int ip, oip; /* ip=Instruction pointer, oip=Old instruction pointer */ int r; /* Used to hold return value from token execution */ int fail_addr; /* What address to jump to on failure */ rbool fail; /* Last token was a conditional token that failed */ rbool ortrue, blocktrue, orflag; /* OR stuff orflag: Are we in an OR group? ortrue: Is current OR group true? blocktrue: Is current block w/in OR true? */ static rbool restart=0; /* Restarting after subroutine? */ op_rec currop; /* Information on the current token and its args */ fail_addr=32000; /* Fall off the end when we fail */ fail=0; ip=0; orflag=blocktrue=ortrue=0; *redir_offset=1; /* Default: This is what RedirectTo does. Only XRedirect can send a different value */ if (restart) /* finish up Return from subroutine */ pop_subcall(&cnum,&ip,&fail_addr); if (DEBUG_AGT_CMD && !supress_debug) { debug_head(cnum); if (restart) debugout(" (Resuming after subroutine)\n"); } restart=0; /* ========== Main Loop ================= */ while(ip<command[cnum].cmdsize) { oip=ip; ip+=decode_instr(&currop,command[cnum].data+ip,command[cnum].cmdsize-ip); /* ------- OR Logic --------------- */ if (currop.op==109) { /* OR */ if (!orflag) { /* First OR; set things up */ orflag=1; ortrue=0; blocktrue=1; } blocktrue=blocktrue && !fail; /* Was the previous token true? */ fail=0; ortrue=ortrue||blocktrue; /* OR in last block */ blocktrue=1; /* New block starts out true. */ } else if (orflag) { /* we're in the middle of a block */ blocktrue=blocktrue&&!fail; /* Add in previous token */ fail=0; if (currop.endor) { /* i.e. not a conditional token */ orflag=0; /* End of OR block */ ortrue=ortrue||blocktrue; /* OR in last block */ fail=!ortrue; /* Success of whole group */ } } /* ------------ FAILMESSAGE handling ------------- */ if (currop.failmsg) { /* Is the current token a Fail... token? */ if (!fail) continue; /* Skip it; look at next instruction */ /* ErrMessage and ErrStdMessage: set disambiguation score */ if (do_disambig) { if (currop.op==1130 || currop.op==1131) { if (!decode_args(oip,&currop)) return 2; disambig_score=currop.arg1; return 2; } else return -2; /* FailMessage counts as an action token */ } /* Then run the failmessage, skipping the following step... */ } /* -------- Failure routines -------------------- */ else if (fail) { /* ... and not failmessage */ /* consequences of failure */ fail=0; /* In case fail_addr doesn't point off the edge of the world */ ip=fail_addr; fail_addr=32000; /* Reset fail_addr */ continue; /* Usually fail_addr will fall off the end, causing this to return 0 */ } /* - Finish decoding arguments and print out debugging message - */ if (!decode_args(oip,&currop)) { if (currop.op<1000) fail=currop.negate ? 0 : 1; continue; /* return 2;*/ } /* -------- Commands that need to be handled specially -------------- */ if (currop.op==109) { /* OR */ if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0); continue; /* OR: skip further processing */ } if (currop.op==1037) { /* DoSubroutine */ if (!push_subcall(cnum,ip,fail_addr)) { writeln("GAME ERROR: Subroutine stack overflow."); return 2; } subcall_arg=currop.arg1; if (DEBUG_AGT_CMD && !supress_debug) debugout("--> Call\n"); return 4; } if (currop.op==1038) { /* Return */ restart=1; if (DEBUG_AGT_CMD && !supress_debug) debugout("--> Return\n"); return 5; } if (currop.op==1149) { /* Goto */ ip=currop.arg1; if (DEBUG_AGT_CMD && !supress_debug) debugout("\n"); continue; } if (currop.op==1150) { /* OnFailGoto */ fail_addr=currop.arg1; if (DEBUG_AGT_CMD && !supress_debug) debugout("\n"); continue; } if (currop.op==1152) /* XRedirect */ *redir_offset=currop.arg1; /* ---------- Disambiguation Success -------------- */ if (do_disambig && currop.disambig) { if (DEBUG_AGT_CMD && !supress_debug) debugout("==> ACTION\n"); return -2; } /* ---------- Run normal metacommands -------------- */ switch(r=exec_instr(&currop)) { case 0: /* Normal action token or successful conditional token */ if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0); continue; case 1: /* Conditional token: fail */ if (DEBUG_AGT_CMD && !supress_debug) { if (orflag) debugout(" (-->FAIL)\n"); else debugout("--->FAIL\n"); } fail=1; continue; default: /* Return explicit value */ if (DEBUG_AGT_CMD && !supress_debug) { if (r==103) debugout("-->Redirect\n"); else debugout("==> END\n"); } return r-100; } } return 0; }