void movement() { char ch1,ch2; entercell(sys.cell); do { ch1 = getch(); if (ch1 == 0) { ch2 = getch(); docommand(ch2); } else if (isalnum(ch1) || ch1 == 27 || ch1 == 8 || ch1 == '+' || ch1 == '-' || ch1 == '.') { ungetch(ch1); if ((sys.screen== SCREEN1) && ( status.data[sys.cell.row-8].month == -1) && (sys.cell.col != MONTH) && (sys.cell.col <= HELDP)) { leavecell(sys.cell); clearrow(); sys.cell.col = MONTH; entercell(sys.cell); } ch2 = editcell(sys.cell); docommand(ch2); } } while (1); }
/* read arguments from a file. */ void filetokens(FILE *in, FILE *out) { char c; char mydelim = zero ? 0 : (delim ? delim : '\n'); do { S(arg) = 0; while ( (c = getc(in)) != EOF && c != mydelim ) EXPAND(arg) = c; if ( (c == EOF) && (S(arg) == 0) ) break; EXPAND(arg) = 0; S(arg)--; if ( !match_re(T(arg)) ) continue; counter++; if ( cmd ) docommand(out); else if ( out ) printarg(out); } while ( c != EOF ); }
inr execPipe() { fflush(stdout); /* important, otherwise the stdout buffer would be * present in both processes after the fork()! * It could be printed twice... Or never printed, * because of the exec overwriting this whole process. * It depends on how it's being buffered. When doing * a fork or exec, we are careful to empty our stdio * buffers first. */ switch ((pid = fork())) { case -1: perror("fork"); break; case 0: /* child */ docommand(); break; /* not reached */ default: /* parent; fork() return value is child pid */ /* These two pids output below will be the same: the process we * forked will be the one which satisfies the wait(). This mightn't * be the case in a more complex situation, e.g. a shell which has * started several "background" processes. */ printf("fork() returns child pid of %d\n", pid); pid = wait(&status); printf("wait() returns child pid of %d\n", pid); printf("Child exit status was %d\n", status >> 8); /* status is a two-byte value; the upper byte is the exit * status, i.e. return value from main() or the value passed * to exit(). */ } return 1; }
int execPipe(char *command) { int pid; int status; fflush(stdout); /* important, otherwise the stdout buffer would be * present in both processes after the fork()! * It could be printed twice... Or never printed, * because of the exec overwriting this whole process. * It depends on how it's being buffered. When doing * a fork or exec, we are careful to empty our stdio * buffers first. */ char *token; char command1[50]; char command2[50]; /* get the first token */ token = strtok(command, "|"); strcpy(command1, token); /* walk through other tokens */ while( token != NULL ) { strcpy(command2, token); memmove(command2, command2+1, strlen(command2)); token = strtok(NULL, "|"); } switch ((pid = fork())) { case -1: perror("fork"); break; case 0: /* child */ docommand(command1, command2); break; /* not reached */ default: /* parent; fork() return value is child pid */ /* These two pids output below will be the same: the process we * forked will be the one which satisfies the wait(). This mightn't * be the case in a more complex situation, e.g. a shell which has * started several "background" processes. */ // printf("fork() returns child pid of %d\n", pid); pid = wait(&status); // printf("wait() returns child pid of %d\n", pid); // printf("Child exit status was %d\n", status >> 8); /* status is a two-byte value; the upper byte is the exit * status, i.e. return value from main() or the value passed * to exit(). */ } return 1; }
void process_or_do_work(int fd1, int fd2) { char buf[1024]; ssize_t bytesread; for ( ; ; ) { bytesread = r_read(fd1, buf, sizeof(buf)); if ((bytesread == -1) && (errno != EAGAIN)) return; /* a real error on fd1 */ else if (bytesread > 0) { docommand(buf, bytesread); continue; } bytesread = r_read(fd2, buf, sizeof(buf)); if ((bytesread == -1) && (errno != EAGAIN)) return; /* a real error on fd2 */ else if (bytesread > 0) docommand(buf, bytesread); else dosomething(); /* input not available, do something else */ } }
static int ingestfile(FILE *str) { char line[500]; while (readline(str,line,sizeof(line)) >= 0) { if (line[0] == '!') { docommand(str,line); } else { fatal("Command string required before register data",NULL); } } }
/* * interactive * runs the interactive shell. basically, just infinitely loops, grabbing * commands and running them (and printing the exit status if it's not * success.) */ static void interactive(void) { static char buf[ARG_MAX]; int status; while (1) { printf("OS/161$ "); getcmd(buf, sizeof(buf)); status = docommand(buf); if (status) { printf("Command returned %d\n", status); } } }
void monitorselect(int fd[], int numfds) { char buf[BUFSIZE]; int bytesread; int i; int maxfd; int numnow, numready; fd_set readset; maxfd = 0; /* set up the range of descriptors to monitor */ for (i = 0; i < numfds; i++) { if ((fd[i] < 0) || (fd[i] >= FD_SETSIZE)) return; if (fd[i] >= maxfd) maxfd = fd[i] + 1; } numnow = numfds; while (numnow > 0) { /* continue monitoring until all are done */ FD_ZERO(&readset); /* set up the file descriptor mask */ for (i = 0; i < numfds; i++) if (fd[i] >= 0) FD_SET(fd[i], &readset); numready = select(maxfd, &readset, NULL, NULL, NULL); /* which ready? */ if ((numready == -1) && (errno == EINTR)) /* interrupted by signal */ continue; else if (numready == -1) /* real select error */ break; for (i = 0; (i < numfds) && (numready > 0); i++) { /* read and process */ if (fd[i] == -1) /* this descriptor is done */ continue; if (FD_ISSET(fd[i], &readset)) { /* this descriptor is ready */ bytesread = r_read(fd[i], buf, BUFSIZE); numready--; if (bytesread > 0) docommand(buf, bytesread); else { /* error occurred on this descriptor, close it */ r_close(fd[i]); fd[i] = -1; numnow--; } } } } for (i = 0; i < numfds; i++) if (fd[i] >= 0) r_close(fd[i]); }
/* * interactive * runs the interactive shell. basically, just infinitely loops, grabbing * commands and running them (and printing the exit status if it's not * success.) */ static void interactive(void) { char buf[ARG_MAX]; int status; while (1) { printf("OS/161$ "); getcmd(buf, sizeof(buf)); status = docommand(buf); if (status) { printf("Exit %d\n", status); } #ifdef WNOHANG waitpoll(); #endif } }
/* * main * if there are no arguments, run interactively, otherwise, run a program * from within the shell, but immediately exit. */ int main(int argc, char *argv[]) { #ifdef HOST hostcompat_init(argc, argv); #endif check_timing(); /* * Allow argc to be 0 in case we're running on a broken kernel, * or one that doesn't set argv when starting the first shell. */ if (argc == 0 || argc == 1) { interactive(); } else if (argc == 3 && !strcmp(argv[1], "-c")) { return docommand(argv[2]); } else { errx(1, "Usage: sh [-c command]"); } return 0; }
void bootmenu(void) { char input[256]; char *c; for (;;) { c = input; input[0] = '\0'; printf("> "); kgets(input, sizeof(input)); /* * Skip leading whitespace. */ while (*c == ' ') { c++; } if (*c != '\0') { docommand(bootcmds, c); } } }
/* read arguments off the command line */ void cmdtokens(char *in, FILE *out) { do { S(arg) = 0; while ( *in && (*in != delim) ) EXPAND(arg) = *in++; if ( *in ) ++in; EXPAND(arg) = 0; S(arg)--; if ( !match_re(T(arg)) ) continue; counter++; if ( cmd ) docommand(out); else if ( out ) printarg(out); } while ( *in ); }
static void dodel(const char *username, const char *userhome, FILE *c, char *ctl, const char *extension, const char *sender, const char *receipient, const char *defaultext, const char *quota, const char *defaultmail, int recursion_level) { char *ufromline; char *dtline; char *rpline; time_t t; const char *curtime; time(&t); curtime=ctime(&t); if ((ufromline=malloc(strlen(curtime)+strlen(sender)+30))==0|| (dtline=malloc(strlen(receipient)+ sizeof("Delivered-To: "))) == 0 || (rpline=malloc(strlen(sender) + sizeof("Return-Path: <>"))) == 0) { perror("malloc"); exit(EX_TEMPFAIL); } if ( (!ctl || !*ctl) && recursion_level == 0) { const char *p= *defaultmail ? defaultmail:config_defaultdelivery(); if ((ctl=malloc(strlen(p)+1)) == 0) { perror("malloc"); exit(EX_TEMPFAIL); } strcpy(ctl, p); } sprintf(ufromline, "From %s %s", sender, curtime); { char *p; if ((p=strchr(ufromline, '\n')) != 0) *p=0; } strcat(strcpy(dtline, "Delivered-To: "), receipient); strcat(strcat(strcpy(rpline, "Return-Path: <"), sender), ">"); while (*ctl) { if (*ctl == '#' || *ctl == '\n') { while (*ctl) if (*ctl++ == '\n') break; continue; } /* ** The fd hack is needed for BSD, whose C lib doesn't like ** mixing stdio and unistd seek calls. */ if (*ctl == '.' || *ctl == '/') { const char *filename=ctl; int fd_hack=dup(fileno(stdin)); FILE *fp_hack; if (fd_hack < 0 || lseek(fd_hack, 0L, SEEK_SET) < 0 || (fp_hack=fdopen(fd_hack, "r")) == NULL) { perror("dup"); exit(EX_TEMPFAIL); } while (*ctl) { if (*ctl == '/' && (ctl[1] == '\n' || ctl[1] == 0)) *ctl=0; /* Strip trailing / */ if (*ctl == '\n') { *ctl++='\0'; break; } ++ctl; } if (savemessage(extension, sender, receipient, fp_hack, filename, ufromline, dtline, rpline, quota)) exit(EX_TEMPFAIL); fclose(fp_hack); close(fd_hack); continue; } if (*ctl == '|') { const char *command=++ctl; int rc; int fd_hack=dup(fileno(stdin)); FILE *fp_hack; if (fd_hack < 0 || lseek(fd_hack, 0L, SEEK_SET) < 0 || (fp_hack=fdopen(fd_hack, "r")) == NULL) { perror("dup"); exit(EX_TEMPFAIL); } ctl=skip_eol(ctl, 0); while (*command == ' ' || *command == '\t') ++command; rc=docommand(extension, sender, receipient, defaultext, fp_hack, username, userhome, command, dtline, rpline, ufromline, quota, defaultmail, recursion_level); if (rc) exit(rc); fclose(fp_hack); close(fd_hack); continue; } /* Forwarding instructions, parse RFC822 addresses */ if (*ctl == '&' || *ctl == '!') ++ctl; /* Legacy */ { const char *addresses=ctl; struct rfc822t *tokens; struct rfc822a *addrlist; int n; ctl=skip_eol(ctl, 1); if ((tokens=rfc822t_alloc_new(addresses, NULL, NULL)) == 0 || (addrlist=rfc822a_alloc(tokens)) == 0) { perror("malloc"); exit(EX_TEMPFAIL); } for (n=0; n<addrlist->naddrs; ++n) { char *p; if (addrlist->addrs[n].tokens == NULL) continue; p=rfc822_display_addr_tobuf(addrlist, n, NULL); if (!p) { perror(addresses); exit(EX_TEMPFAIL); } printf("%s\n", p); free(p); } rfc822a_free(addrlist); rfc822t_free(tokens); fflush(stdout); } } free(rpline); free(dtline); free(ufromline); }
void doboottypemenu(void) { char input[80], *ic, *oc; int choice; printf("\n"); /* Display menu */ if (bootconf.menuformat == MENUFORMAT_LETTER) { for (choice = 0; choice < bootconf.nummenu; choice++) printf(" %c. %s\n", choice + 'A', bootconf.desc[choice]); } else { /* Can't use %2d format string with libsa */ for (choice = 0; choice < bootconf.nummenu; choice++) printf(" %s%d. %s\n", (choice < 9) ? " " : "", choice + 1, bootconf.desc[choice]); } choice = -1; for (;;) { input[0] = '\0'; if (bootconf.timeout < 0) { if (bootconf.menuformat == MENUFORMAT_LETTER) printf("\nOption: [%c]:", bootconf.def + 'A'); else printf("\nOption: [%d]:", bootconf.def + 1); gets(input); choice = getchoicefrominput(input, bootconf.def); } else if (bootconf.timeout == 0) choice = bootconf.def; else { printf("\nChoose an option; RETURN for default; " "SPACE to stop countdown.\n"); if (bootconf.menuformat == MENUFORMAT_LETTER) printf("Option %c will be chosen in ", bootconf.def + 'A'); else printf("Option %d will be chosen in ", bootconf.def + 1); input[0] = awaitkey(bootconf.timeout, 1); input[1] = '\0'; choice = getchoicefrominput(input, bootconf.def); /* If invalid key pressed, drop to menu */ if (choice == -1) bootconf.timeout = -1; } if (choice < 0) continue; if (!strcmp(bootconf.command[choice], "prompt")) { printf("type \"?\" or \"help\" for help.\n"); bootmenu(); /* does not return */ } else { ic = bootconf.command[choice]; /* Split command string at ; into separate commands */ do { oc = input; /* Look for ; separator */ for (; *ic && *ic != COMMAND_SEPARATOR; ic++) *oc++ = *ic; if (*input == '\0') continue; /* Strip out any trailing spaces */ oc--; for (; *oc == ' ' && oc > input; oc--); *++oc = '\0'; if (*ic == COMMAND_SEPARATOR) ic++; /* Stop silly command strings like ;;; */ if (*input != '\0') docommand(input); /* Skip leading spaces */ for (; *ic == ' '; ic++); } while (*ic); } } }
exec_type PROC editcore(void) { cmdtype cmd; extern bool s_wrapped; /* rcb[0] = 0; rcp = rcb; */ if (diddled) { setpos(skipws(curr)); /* set cursor x position.. */ yp = settop(LINES / 2); /* Y position */ } if (diddled || zotscreen) /* redisplay? */ redisplay(FALSE); mvcur(yp, xp); /* and move the cursor */ for (;;) { s_wrapped = 0; ch = readchar(); /* get a char */ gcount(); /* ... a possible count */ switch (cmd = movemap[ch]) { case FILE_C: wr_stat(); /* write file stats */ mvcur(yp, xp); break; case WINDOW_UP: case WINDOW_DOWN: scroll(cmd==WINDOW_UP); /* scroll the window */ break; case REDRAW_C: /* redraw the window */ redisplay(TRUE); mvcur(yp, xp); break; case MARKER_C: /* set a marker */ ch = tolower(readchar()); if (ch >= 'a' && ch <= 'z') contexts[ch-'`'] = curr; else if (ch != ESC) error(); break; case REDO_C: if (rcb[0] != 0) { zerostack(&undo); insertmacro(rcb, 1); redoing = TRUE; } break; case REWINDOW: zdraw(readchar()); /* shift the window */ break; case DEBUG_C: /* debugging stuff -- unused */ break; case ZZ_C: /* shortcut for :xit */ ch = readchar(); if (ch == 'Z') insertmacro(":x\r", 1); else if (ch != ESC) error(); break; case EDIT_C: /* drop into line mode */ return E_EDIT; case COLIN_C: /* do one exec mode command */ return E_VISUAL; case HARDMACRO: macrocommand(); /* 'hard-wired' macros */ break; case SOFTMACRO: exmacro(); /* run a macro */ break; case INSMACRO: /* macro for insert mode */ case BAD_COMMAND: error(); break; default: if (cmd < DELETE_C) movearound(cmd); else /*if (cmd < HARDMACRO)*/ docommand(cmd); break; } lastexec = 0; } /* never exits here */ }
/* Execute a block. There can be a number of return values from this routine. * NORMAL indicates a normal termination * BROKEN indicates a break -- if the caller is a breakable loop, * terminate it, otherwise pass the break upwards * CONTINUED indicates a continue -- if the caller is a continuable loop, * continue, else pass the continue upwards * Any other return code is considered a pointer to a string which is * a label somewhere -- if this label is present in the block, * goto it, otherwise pass it up. Note that this prevents jumping * into a loop, which is good. * * Note that here is where we expand variables, ``, and globs for * controls. * * The 'num' argument is used by break n and continue n. */ static char * doblock(struct control *bl, int *num) { struct control *ch, *cn = NULL; wordlist *wl, *wltmp; char *i, *wlword; int nn; nn = *num + 1; /*CDHW this is a guess... CDHW*/ switch (bl->co_type) { case CO_WHILE: if (!bl->co_children) { fprintf(cp_err, "Warning: Executing empty 'while' block.\n"); fprintf(cp_err, " (Use a label statement as a no-op to suppress this warning.)\n"); } while (bl->co_cond && cp_istrue(bl->co_cond)) { if (!bl->co_children) cp_periodic(); /*CDHW*/ for (ch = bl->co_children; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); switch (*i) { case NORMAL: break; case BROKEN: /* Break. */ if (nn < 2) { return (NORMAL_STR); } else { *num = nn - 1; return (BROKEN_STR); } case CONTINUED: /* Continue. */ if (nn < 2) { cn = NULL; break; } else { *num = nn - 1; return (CONTINUED_STR); } default: cn = findlabel(i, bl->co_children); if (!cn) return (i); } } } break; case CO_DOWHILE: do { for (ch = bl->co_children; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); switch (*i) { case NORMAL: break; case BROKEN: /* Break. */ if (nn < 2) { return (NORMAL_STR); } else { *num = nn - 1; return (BROKEN_STR); } case CONTINUED: /* Continue. */ if (nn < 2) { cn = NULL; break; } else { *num = nn - 1; return (CONTINUED_STR); } default: cn = findlabel(i, bl->co_children); if (!cn) return (i); } } } while (bl->co_cond && cp_istrue(bl->co_cond)); break; case CO_REPEAT: if (!bl->co_children) { fprintf(cp_err, "Warning: Executing empty 'repeat' block.\n"); fprintf(cp_err, " (Use a label statement as a no-op to suppress this warning.)\n"); } if (!bl->co_timestodo) bl->co_timestodo = bl->co_numtimes; /*bl->co_numtimes: total repeat count bl->co_numtimes = -1: repeat forever bl->co_timestodo: remaining repeats*/ while ((bl->co_timestodo > 0) || (bl->co_timestodo == -1)) { if (!bl->co_children) cp_periodic(); /*CDHW*/ if (bl->co_timestodo != -1) bl->co_timestodo--; /* loop through all stements inside rpeat ... end */ for (ch = bl->co_children; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); switch (*i) { case NORMAL: break; case BROKEN: /* Break. */ /* before leaving repeat loop set remaining timestodo to 0 */ bl->co_timestodo = 0; if (nn < 2) { return (NORMAL_STR); } else { *num = nn - 1; return (BROKEN_STR); } case CONTINUED: /* Continue. */ if (nn < 2) { cn = NULL; break; } else { /* before leaving repeat loop set remaining timestodo to 0 */ bl->co_timestodo = 0; *num = nn - 1; return (CONTINUED_STR); } default: cn = findlabel(i, bl->co_children); if (!cn) { /* no label found inside repeat loop: before leaving loop set remaining timestodo to 0 */ bl->co_timestodo = 0; return (i); } } } } break; case CO_IF: if (bl->co_cond && cp_istrue(bl->co_cond)) { for (ch = bl->co_children; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); if (*i > 2) { cn = findlabel(i, bl->co_children); if (!cn) return (i); else tfree(i); } else if (*i != NORMAL) { *num = nn; return (i); } } } else { for (ch = bl->co_elseblock; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); if (*i > 2) { cn = findlabel(i, bl->co_elseblock); if (!cn) return (i); } else if (*i != NORMAL) { *num = nn; return (i); } } } break; case CO_FOREACH: wltmp = cp_variablesubst(cp_bquote(cp_doglob(wl_copy(bl->co_text)))); for (wl = wltmp; wl; wl = wl->wl_next) { cp_vset(bl->co_foreachvar, CP_STRING, wl->wl_word); for (ch = bl->co_children; ch; ch = cn) { cn = ch->co_next; i = doblock(ch, &nn); switch (*i) { case NORMAL: break; case BROKEN: /* Break. */ if (nn < 2) { wl_free(wltmp); return (NORMAL_STR); } else { *num = nn - 1; wl_free(wltmp); return (BROKEN_STR); } case CONTINUED: /* Continue. */ if (nn < 2) { cn = NULL; break; } else { *num = nn - 1; wl_free(wltmp); return (CONTINUED_STR); } default: cn = findlabel(i, bl->co_children); if (!cn) { wl_free(wltmp); return (i); } } } } wl_free(wltmp); break; case CO_BREAK: if (bl->co_numtimes > 0) { *num = bl->co_numtimes; return (BROKEN_STR); } else { fprintf(cp_err, "Warning: break %d a no-op\n", bl->co_numtimes); return (NORMAL_STR); } case CO_CONTINUE: if (bl->co_numtimes > 0) { *num = bl->co_numtimes; return (CONTINUED_STR); } else { fprintf(cp_err, "Warning: continue %d a no-op\n", bl->co_numtimes); return (NORMAL_STR); } case CO_GOTO: wl = cp_variablesubst(cp_bquote(cp_doglob(wl_copy(bl->co_text)))); wlword = wl->wl_word; wl->wl_word = NULL; wl_free(wl); return (wlword); case CO_LABEL: /* Do nothing. */ cp_periodic(); /*CDHW needed to avoid lock-ups when loop contains only a label CDHW*/ break; case CO_STATEMENT: docommand(wl_copy(bl->co_text)); break; case CO_UNFILLED: /* There was probably an error here... */ fprintf(cp_err, "Warning: ignoring previous error\n"); break; default: fprintf(cp_err, "doblock: Internal Error: bad block type %d\n", bl->co_type); return (NORMAL_STR); } return (NORMAL_STR); }
int process(FILE *stream) { int done,type; char *upto,*from, *what, *temp, *command, *params, *who, *host; char *filename,*filesize; static char *lineptr=NULL; static int linesz=0; if (getline(&lineptr,&linesz,stream) == -1) return 0; /* get first word */ upto = lineptr; if (*lineptr == ':') { from = strsep(&upto," "); from++; } else from = "*"; command = strsep(&upto," "); params = strsep(&upto,"\n\r"); if (temp = strchr(from,'!')) { host = temp+1; *temp='\0'; } if (isdigit(*command)) { from = "*"; type = SERVER; getwhowhat(params,&who,&what); } else type = docommand(from,command,params,&who,&what); switch (type) { case ME: case NORMAL: case NOTICE: if (strcasecmp(who,chan)) { sendChan(channel, SCRMSG2, "\x1b[0m- "); } break; default: break; }; switch (type) { case NORMAL: sendChan(channel, SCRMSG, "\x1b[1;33m<%s>\x1b[0m %s", from, what); break; case ME: sendChan(channel, SCRMSG, "\x1b[35m* %s %s", from, what); break; case JOIN: sendChan(channel, SCRMSG, "\x1b[34m%s (%s) has joined channel %s",from,host,what); break; case PART: sendChan(channel, SCRMSG, "\x1b[34m%s (%s) has left channel %s", from,host,what); break; case QUIT: sendChan(channel, SCRMSG, "\x1b[1;35m%s has quit IRC (%s)", from,what); break; case NICK: if (!strcmp(from,nick)) { if (delnick) free(nick); nick = strdup(what); delnick=1; } sendChan(channel, SCRMSG, "\x1b[35m** %s \x1b[32m<->\x1b[35m %s", from,what); break; case MODE: sendChan(channel, SCRMSG, "\x1b[35m%s sets mode %s",from,what); break; case SERVER: sendChan(channel, SCRMSG, "\x1b[1;33m** %s",what); break; case NOTICE: sendChan(channel, SCRMSG, "\x1b[37m-%s- \x1b[1;33m%s",from,what); break; case UNKCTCP: sendChan(channel, SCRMSG, "\x1b[1;31mUnknown CTCP from %s : %s",from,what); break; case DCC: //point filename to "DCC" filename = strsep(&what," "); filename = what; //point filename to "SEND" filename = strsep(&what," "); filename = what; //point filename to "filename" filename = strsep(&what," "); filesize = what; //point filesize to "32bit IP number" filesize = strsep(&what," "); filesize = what; //point filesize to "IP PORT" filesize = strsep(&what," "); filesize = what; //point filesize to "filesize" filesize = strsep(&what," "); sendChan(channel, SCRMSG, "\x1b[37mReceiving DCC from %s : filename: %s size: %s", from,filename,filesize); break; default: break; } return 1; }
/*************************************************************** *USEAGE: 关闭eag服务 *Param: *Return: 0 -> 关闭成功 !0 -> 失败 *Auther:shao jun wu *Date:2009-2-4 17:10:41 *Modify:(include modifyer,for what resease,date) ****************************************************************/ int eag_services_stop( ) { return docommand( SCRIPT_FILE_PATH" stop >/dev/null 2>&1" ); }
int eag_services_reload() { return docommand( SCRIPT_FILE_PATH" reload >/dev/null 2>&1" ); }
/* * process the command line and execute the appropriate functions * full input/output redirection and piping are supported */ void parsecommandline(char *s, int redirect) { char *in = 0; char *out = 0; char *fname0 = 0; char *fname1 = 0; char *nextcmd; int of_attrib = O_CREAT | O_TRUNC | O_WRONLY; int num; assert(s); dprintf(("[parsecommandline (%s)]\n", s)); #ifdef FEATURE_ALIASES aliasexpand(s, MAX_INTERNAL_COMMAND_SIZE); dprintf(("[alias expanded to (%s)]\n", s)); #endif if (tracemode) { /* Question after the variables expansion and make sure _all_ executed commands will honor the trace mode */ /* * Commands may be nested ("if errorlevel 1 echo done>test"). To * prevent redirecting FreeCOM prompts to user file, temporarily * revert redirection. */ int redir_fdin, redir_fdout, answer; if (oldinfd != -1) { redir_fdin = dup (0); dup2 (oldinfd, 0); } if (oldoutfd != -1) { redir_fdout = dup (1); dup2 (oldoutfd, 1); } printprompt(); outs(s); /* If the user hits ^Break, it has the same effect as usually: If he is in a batch file, he is asked if to abort all the batchfiles or just the current one */ answer = userprompt(PROMPT_YES_NO); if (oldinfd != -1) { dup2 (redir_fdin, 0); dos_close (redir_fdin); } if (oldoutfd != -1) { dup2 (redir_fdout, 1); dos_close (redir_fdout); } if (answer != 1) return; /* "No" or ^Break */ } if(!redirect) { docommand(s); return; } assert(oldinfd == -1); /* if fails something is wrong; should NEVER */ assert(oldoutfd == -1); /* happen! -- 2000/01/13 ska*/ num = get_redirection(s, &in, &out, &of_attrib); if (num < 0) /* error */ goto abort; /* Set up the initial conditions ... */ if (in || (num > 1)) /* Need to preserve stdin */ oldinfd = dup(0); if (in) /* redirect input from this file name */ { dos_close(0); if (0 != devopen(in, O_RDONLY)) { error_redirect_from_file(in); goto abort; } } if (out || (num > 1)) /* Need to preserve stdout */ oldoutfd = dup(1); /* Now do all but the last pipe command */ while (num-- > 1) { dos_close(1); /* Close current output file */ if ((fname0 = tmpfn()) == 0) goto abort; dos_creat(fname0, 0); nextcmd = s + strlen(s) + 1; docommand(s); dos_close(1); dup2(oldoutfd, 1); dos_close(0); killtmpfn(fname1); /* fname1 can by NULL */ fname1 = fname0; fname0 = 0; dos_open(fname1, O_RDONLY); s = nextcmd; } /* Now set up the end conditions... */ if (out) /* Final output to here */ { dos_close(1); if (1 != devopen(out, of_attrib)) { error_redirect_to_file(out); goto abort; } } else if (oldoutfd != -1) /* Restore original stdout */ { dos_close(1); dup2(oldoutfd, 1); dos_close(oldoutfd); oldoutfd = -1; } docommand(s); /* process final command */ abort: if (oldinfd != -1) /* Restore original STDIN */ { dos_close(0); dup2(oldinfd, 0); dos_close(oldinfd); oldinfd = -1; } if (oldoutfd != -1) /* Restore original STDOUT */ { dos_close(1); dup2(oldoutfd, 1); dos_close(oldoutfd); oldoutfd = -1; } killtmpfn(fname1); killtmpfn(fname0); if (out) free(out); if (in) free(in); }
/* * process the command line and execute the appropriate functions * full input/output redirection and piping are supported */ void parsecommandline(char *s) { char *in = NULL; char *out = NULL; char *fname0 = NULL; char *fname1 = NULL; char *nextcmd; int of_attrib = O_CREAT | O_TRUNC | O_TEXT | O_WRONLY; int num; /* first thing we do is alias expansion */ assert(s); assert(oldinfd == -1); /* if fails something is wrong; should NEVER */ assert(oldoutfd == -1); /* happen! -- 2000/01/13 ska*/ dprintf(("[parsecommandline (%s)]\n", s)); #ifdef FEATURE_ALIASES aliasexpand(s, MAX_INTERNAL_COMMAND_SIZE); dprintf(("[alias expanded to (%s)]\n", s)); #endif if (tracemode) { /* Question after the variables expansion and make sure _all_ executed commands will honor the trace mode */ printf("%s [Enter=Yes, ESC=No] ", s); /* If the user hits ^Break, it has the same effect as usually: If he is in a batch file, he is asked if to abort all the batchfiles or just the current one */ if (!strchr("Y\r\n", vcgetcstr("\x1bYN\r\n"))) /* Pressed either "No" or ^Break */ return; } num = get_redirection(s, &in, &out, &of_attrib); if (num < 0) /* error */ goto abort; /* Set up the initial conditions ... */ if (in || (num > 1)) /* Need to preserve stdin */ oldinfd = dup(0); if (in) /* redirect input from this file name */ { close(0); if (0 != devopen(in, O_TEXT | O_RDONLY, S_IREAD)) { displayString(TEXT_ERROR_REDIRECT_FROM_FILE, in); goto abort; } } if (out || (num > 1)) /* Need to preserve stdout */ oldoutfd = dup(1); /* Now do all but the last pipe command */ while (num-- > 1) { close(1); /* Close current output file */ if ((fname0 = tmpfn()) == NULL) goto abort; open(fname0, O_CREAT | O_TRUNC | O_TEXT | O_WRONLY, S_IREAD | S_IWRITE); nextcmd = s + strlen(s) + 1; docommand(s); close(1); dup2(oldoutfd, 1); close(0); killtmpfn(fname1); /* fname1 can by NULL */ fname1 = fname0; fname0 = NULL; open(fname1, O_TEXT | O_RDONLY, S_IREAD); s = nextcmd; } /* Now set up the end conditions... */ if (out) /* Final output to here */ { close(1); if (1 != devopen(out, of_attrib, S_IREAD | S_IWRITE)) { displayString(TEXT_ERROR_REDIRECT_TO_FILE, out); goto abort; } if (of_attrib & O_APPEND) lseek(1, 0, SEEK_END); } else if (oldoutfd != -1) /* Restore original stdout */ { close(1); dup2(oldoutfd, 1); close(oldoutfd); oldoutfd = -1; } docommand(s); /* process final command */ abort: if (oldinfd != -1) /* Restore original STDIN */ { close(0); dup2(oldinfd, 0); close(oldinfd); oldinfd = -1; } if (oldoutfd != -1) /* Restore original STDOUT */ { close(1); dup2(oldoutfd, 1); close(oldoutfd); oldoutfd = -1; } killtmpfn(fname1); killtmpfn(fname0); if (out) free(out); if (in) free(in); }
/* start(): * Called at the end of reset.s as the first C function after processor * bootup. It is passed a state that is used to determine whether or not * the CPU is restarting as a result of a warmstart or a coldstart. If * the restart is a coldstart, then state will be INITIALIZE. if the * restart is a warmstart, then there are a few typical values of state, * the most common of which are APP_EXIT and EXCEPTION. * * The bss_start and bss_end variables are usually defined in the * target-specific linker memory-map definition file. They symbolically * identify the beginning and end of the .bss section. Many compilers * support intrinsic tags for this; however, since it is apparently not * consistent from one toolset to the next; I chose to make up my own * tags so that this file never changes from one toolset to the next. * * The FORCE_BSS_INIT definition can be established in the target-specific * config.h file to force .bss space initialization regardless of warm/cold * start. */ void start(int state) { char buf[48]; #ifdef FORCE_BSS_INIT state = INITIALIZE; #endif /* Based on the incoming value of 'state' we may or may not initialize * monitor-owned ram. Ideally, we only want to initialize * monitor-owned ram when 'state' is INITIALIZE (power-up or reset); * however, to support the case where the incoming state variable may * be corrupted, we also initialize monitor-owned ram when 'state' * is anything unexpected... */ switch(state) { case EXCEPTION: case APP_EXIT: break; case INITIALIZE: default: umonBssInit(); break; } /* Now that the BSS clear loop has been done, we can copy the * value of state (either a register or on stack) to the global * variable (in BSS) StateOfMonitor... */ StateOfMonitor = state; /* Step through different phases of startup... */ init0(); init1(); init2(); /* Depending on the type of startup, alert the console and do * further initialization as needed... */ switch(StateOfMonitor) { case INITIALIZE: reginit(); init3(); break; case APP_EXIT: EthernetStartup(0,0); if(!MFLAGS_NOEXITSTATUS()) { printf("\nApplication Exit Status: %d (0x%x)\n", AppExitStatus,AppExitStatus); } break; case EXCEPTION: EthernetStartup(0,0); printf("\n%s: '%s'\n",EXCEPTION_HEADING, ExceptionType2String(ExceptionType)); printf(" Occurred near 0x%lx",ExceptionAddr); if(AddrToSym(-1,ExceptionAddr,buf,0)) { printf(" (within %s)",buf); } printf("\n\n"); exceptionAutoRestart(INITIALIZE); break; default: printf("Unexpected monitor state: 0x%x (sp @ 0x%x)\n", StateOfMonitor, buf); /* To attempt to recover from the bad state, just do * what INITIALIZE would do... */ reginit(); init3(); break; } #ifdef LOCK_FLASH_PROTECT_RANGE /* Issue the command that will cause the range of sectors * designated by FLASH_PROTECT_RANGE to be locked. This only * works if the flash device is capable of being locked. */ sprintf(buf,"flash lock %s",FLASH_PROTECT_RANGE); docommand(buf,0); #endif #ifdef PRE_COMMANDLOOP_HOOK PRE_COMMANDLOOP_HOOK(); #endif /* Enter the endless loop of command processing: */ CommandLoop(); printf("ERROR: CommandLoop() returned\n"); monrestart(INITIALIZE); }