/* pollConsole(): * Common function used to query the console port with a message. * If after about a 2-second period (or the time specified by POLLTIMEOUT), * there is no response from the console, then return 0; else return * the character that was received. */ int pollConsole(char *msg) { char *env; int pollval, msec; struct elapsed_tmr tmr; env = getenv("POLLTIMEOUT"); if (env) msec = strtol(env,0,0); else msec = 2000; pollval = 0; printf("%s",msg); startElapsedTimer(&tmr,msec); while(!msecElapsed(&tmr)) { if (gotachar()) { while(gotachar()) pollval = getchar(); break; } pollethernet(); } putstr("\r\n"); if (ELAPSED_TIMEOUT(&tmr)) return(0); return(pollval); }
/* monDelay(): * Delay for specified number of milliseconds. * Refer to msecElapsed() description for a discussion on the * accuracy of this delay. */ void monDelay(int milliseconds) { struct elapsed_tmr tmr; startElapsedTimer(&tmr,milliseconds); while(!msecElapsed(&tmr)) { WATCHDOG_MACRO; pollethernet(); } }
/* getchar(): Block on the Uart's status until a byte is available in the receive buffer, then return with it. */ int getchar403() { char c; /* First check to see if the default getchar() function has */ /* been overridden... */ if (remotegetchar) return(remotegetchar()); while(!gotachar403()) { #if INCLUDE_ETHERNET pollethernet(); #endif } c = CPU_SPU_SPRB; return((int)c); }
int Bootp(int argc,char *argv[]) { if (!enetInitialized()) return(CMD_FAILURE); startElapsedTimer(&dhcpTmr,RetransmitDelay(DELAY_INIT_DHCP)*1000); DHCPState = BOOTPSTATE_INITIALIZE; BOOTPStartup(0); while(DHCPState != BOOTPSTATE_COMPLETE) { pollethernet(); if (gotachar()) { DHCPState = BOOTPSTATE_COMPLETE; break; } } return(CMD_SUCCESS); }
int getchar(void) { /* If the remotegetchar function pointer is non-zero, then we * assume that the default getchar function has been overridden * by some overlaying application using mon_com(CHARFUNC_GETCHAR,), * so we use that redefined function instead... */ if (remotegetchar) return(remotegetchar()); while(!gotachar()) { /* While waiting for an incoming character, call pollethernet() * to process any incoming packets. Note that if INCLUDE_ETHERNET * is 0 in config.h, then this function is NULL (see ether.h). */ WATCHDOG_MACRO; pollethernet(); } return(target_getchar()); }
int tfsscript(TFILE *fp,int verbose) { char lcpy[CMDLINESIZE], *sv; int tfd, lnsize, lno, verbosity, ignoreerror, cmdstat; tfd = tfsopen(fp->name,TFS_RDONLY,0); if (tfd < 0) return(tfd); lno = 0; /* If ScriptIsRunning is zero, then we know that this is the top-level * script, so we can initialize state here... */ if (ScriptIsRunning == 0) ReturnToDepth = 0; CurrentScriptfdTbl[++ScriptIsRunning] = tfd; while(1) { lno++; lnsize = tfsgetline(tfd,lcpy,CMDLINESIZE); if (lnsize == 0) /* end of file? */ break; if (lnsize < 0) { printf("tfsscript(): %s\n",tfserrmsg(lnsize)); break; } if ((lcpy[0] == '\r') || (lcpy[0] == '\n')) /* empty line? */ continue; lcpy[lnsize-1] = 0; /* Remove the newline */ /* Just in case the goto tag was set outside a script, */ /* clear it now. */ if (ScriptGotoTag) { free(ScriptGotoTag); ScriptGotoTag = (char *)0; } ScriptExitFlag = 0; /* Execute the command line. * If the shell variable "SCRIPTVERBOSE" is set, then enable * verbosity for this command; else use what was passed in * the parameter list of the function. Note that the variable * is tested for each command so that verbosity can be enabled * or disabled within a script. * If the command returns a status that indicates that there was * some parameter error, then exit the script. */ sv = getenv("SCRIPTVERBOSE"); if (sv) verbosity = atoi(sv); else verbosity = verbose; if ((lcpy[0] == '-') || (getenv("SCRIPT_IGNORE_ERROR"))) ignoreerror = 1; else ignoreerror = 0; if (verbosity) printf("[%02d]: %s\n",lno,lcpy); cmdstat = tfsDocommand(lcpy[0] == '-' ? lcpy+1 : lcpy, 0); if (cmdstat != CMD_SUCCESS) { setenv("CMDSTAT","FAIL"); if (ignoreerror == 0) { printf("Terminating script '%s' at line %d\n", TFS_NAME(fp),lno); ScriptExitFlag = EXIT_SCRIPT; break; } } else { setenv("CMDSTAT","PASS"); } /* Check for exit flag. If set, then in addition to terminating the * script, clear the return depth here so that the "missing return" * warning is not printed. This is done because there is likely * to be a subroutine with an exit in it and this should not * cause a warning. */ if (ScriptExitFlag) { ReturnToDepth = 0; break; } /* If ScriptGotoTag is set, then attempt to reposition the line * pointer to the line that contains the tag. */ if (ScriptGotoTag) { int tlen; tlen = strlen(ScriptGotoTag); lno = 0; tfsseek(tfd,0,TFS_BEGIN); while(1) { lnsize = tfsgetline(tfd,lcpy,CMDLINESIZE); if (lnsize == 0) { printf("Tag '%s' not found\n",ScriptGotoTag+2); free(ScriptGotoTag); ScriptGotoTag = (char *)0; tfsclose(tfd,0); return(TFS_OKAY); } lno++; if (!strncmp(lcpy,ScriptGotoTag,tlen) && (isspace(lcpy[tlen]) || (lcpy[tlen] == ':'))) { free(ScriptGotoTag); ScriptGotoTag = (char *)0; break; } } } /* After each line, poll ethernet interface. */ pollethernet(); } tfsclose(tfd,0); if (ScriptExitFlag & REMOVE_SCRIPT) tfsunlink(fp->name); if (ScriptIsRunning > 0) { ScriptIsRunning--; if ((ScriptIsRunning == 0) && (ReturnToDepth != 0)) { printf("Error: script is done, but return-to-depth != 0\n"); printf("(possible gosub/return imbalance)\n"); } } else { printf("Script run-depth error\n"); } /* If the EXECUTE_AFTER_EXIT flag is set (by exit -e), then automatically * start up the file specified in ExecuteAfterExit[]... */ if (ScriptExitFlag & EXECUTE_AFTER_EXIT) { char *argv[2]; argv[0] = ExecuteAfterExit; argv[1] = 0; ScriptExitFlag = 0; tfsrun(argv,0); } /* Upon completion of a script, clear the ScriptExitFlag variable so * that it is not accidentally applied to another script that may have * called this script. */ if ((ScriptExitFlag & EXIT_ALL_SCRIPTS) != EXIT_ALL_SCRIPTS) ScriptExitFlag = 0; return(TFS_OKAY); }
/* getfullline(): * Basic line retrieval; but with a few options... * This function is accessed through the getline_xx functions * below. * Args... * buf: pointer to buffer to be used to place the incoming characters. * max: size of the buffer. * ledit: if set, then allow the line-editor to be used if ESC is input. * timeout: if positive, then after 'timeout' number of seconds waiting * per character, giveup. * if negative, then after 'timeout' number of seconds waiting * total, giveup. * if zero, then wait forever. * prefill: if set, prefill the buffer with that string and show the user. * echo: if set, characters are echoed as they are entered. */ int getfullline(char *buf,int max,int ledit, int timeout,char *prefill, int echo) { char *base; struct elapsed_tmr tmr; static unsigned char crlf; int tot, idx, cumulativetimeout; cumulativetimeout = 0; tot = idx = 0; base = buf; max -= 1; /* Make sure there is space for the null terminator. */ if (prefill) { strcpy(base,prefill); tot = strlen(prefill); putstr(prefill); buf += tot; idx = tot; } /* If the timeout parameter is negative, then assume that this is * to be run with a cumulative timeout rather than a timeout that * is re-started after each character... */ if (timeout < 0) { cumulativetimeout = 1; timeout = abs(timeout); } for(;idx<max;idx++) { if (timeout > 0) { startElapsedTimer(&tmr,timeout); while(!msecElapsed(&tmr)) { if (gotachar()) break; pollethernet(); } if (cumulativetimeout) timeout = msecRemaining(&tmr); if (ELAPSED_TIMEOUT(&tmr)) { *buf = 0; return(-1); /* Return negative to indicate timeout */ } } if (cumulativetimeout && timeout == 0) { *buf = 0; return(-1); } *buf = (char)getchar(); if (!*buf) { idx--; continue; } #if INCLUDE_LINEEDIT if ((*buf == 0x1b) && (ledit)) { (void)line_edit(base); break; } else #endif { if ((*buf == '\r') || (*buf == '\n')) { if ((crlf) && (*buf != crlf)) { crlf = 0; continue; } #if UMON_TARGET == UMON_TARGET_XT || UMON_TARGET == UMON_TARGET_ARM // use CR/LF puts("\r"); #else putchar('\n'); #endif crlf = *buf; *buf = 0; break; } if (*buf == '\b') { if (tot) { idx -= 2; buf--; tot--; if (echo) putstr("\b \b"); } } else if (*buf == CTLC) { puts("^C"); *base = 0; return(0); } else { if (echo) putchar(*buf); tot++; buf++; } crlf = 0; } } if (idx == max) { printf((char *)"\007\nInput too long (exceeds %d bytes).\n",max); *buf = 0; return(0); } #if INCLUDE_LINEEDIT if (ledit) historylog(base); #endif return(strlen(base)); }