Beispiel #1
0
void
main(int argc, char* argv[])
{
    if(argc > 2)
    {
        fprint(2, "usage: %s [file]\n", argv[0]);
        exits("usage");
    }

	// Run script from file given as parameter instead of stdin if necesary
	if(argc == 2)
		redirect_stdin(argv[1]);

	// Allocate buffer for standard input
	Biobuf bin;
	if(Binit(&bin, 0, OREAD))
		sysfatal("%s: standard input: %r", argv0);

	tHeredoc heredoc;
	heredoc.mode = 0;

	// Main loop
	char* line;
	while((line = Brdstr(&bin, '\n', 0)))
	{
		// Process heredoc
		line = heredoc_process(&heredoc, line);
		if(line)
		{
		    // Process script
		    script_process(line);

		    // Free procesed line
		    free(line);
		}
	}

	// Free stdin buffer
	Bterm(&bin);

	// Exit sucesfully
	exits(nil);
}
Beispiel #2
0
/* main program loop */
void mux_loop(int pf) {
  fd_set  ready;        /* used for select */
  int     i = 0;        /* used in the multiplex loop */
  int     done = 0;
  char    buf[BUFSIZE];
  struct timeval tv;
  
  tv.tv_sec = SCRIPT_DELAY;
  tv.tv_usec = 0;

  if (script) {
    script_init(scr_name);
  }

  do { /* forever */
    FD_ZERO(&ready);
    FD_SET(STDIN_FILENO, &ready);
    FD_SET(pf, &ready);

    if (script) {
      if (!select(pf+1, &ready, NULL, NULL, &tv)) {
	i = script_process(S_TIMEOUT, buf, BUFSIZE);
	if (i > 0) {
	   cook_buf(pf, buf, i);
	}
	/* restart timer */
	tv.tv_sec = SCRIPT_DELAY;
	tv.tv_usec = 0;
      }
    } /* if */
    else {
      select(pf+1, &ready, NULL, NULL, NULL);
    }

    if (FD_ISSET(pf, &ready)) {
      /* pf has characters for us */
      i = read(pf, buf, BUFSIZE);
      if (i > 0) {
	if (options & OPTION_LOG_FILTER) {
	  /* only printable characters */
	  size_t size = 0;
	  char *printable = PrintableBuffer(buf,i,&size);
	  DEBUG_MSG("received printable buffer = %s",printable);
	  if (flog != NULL) {
	    printable[size] = '\n';
	    printable[size+1] = '\0';
	    const size_t written = fwrite(printable, 1, size, flog);
	    if (written != size) {
	      const int error = errno;
	      DEBUG_MSG("error writing log file, only %u characters written, errno = %d",written,error);
	    }
	  }	  
	} else {
	  /* raw memory dump */
	  DEBUG_DUMP_MEMORY(buf,i);
	  if (flog != 0) {
	    fwrite(buf, 1, i, flog);
	  }
	}
	write(STDOUT_FILENO, buf, i);		
	if (script) {
	  i = script_process(S_DCE, buf, i);
	  if (i > 0) {
	     cook_buf(pf, buf, i);
	  }
	}
      } else {
	done = 1;
      }
    } /* if */

    if (FD_ISSET(STDIN_FILENO, &ready)) {
      /* standard input has characters for us */
      i = read(STDIN_FILENO, buf, BUFSIZE);
      if (i > 0) {
	 cook_buf(pf, buf, i);
      } else {
	 done = 1;
      }
    } /* if */
  } while (!done); /* do */
}