示例#1
0
文件: help.c 项目: checko/microcom
/* process function for help_state=0 */
static void help_send_escape(int fd, char c) { 
  struct  termios pts;  /* termios settings on port */

  in_escape = 0;
  help_state = 0;
  
  switch (c) {
  case 'x':
    /* restore termios settings and exit */
    write(STDOUT_FILENO, "\n", 1);
    cleanup_termios(0);
    break;
  case 'q': /* quit help */
    break;
  case 'l': /* log on/off */    
    if (flog == 0) { /* open log file */
      open_logFile(log_file);
    }
    else { /* close log file */
      close_logFile();
    }
    break;
  case 's': /* script active/inactive */
    script_init(scr_name);
    script = (script)? 0: 1;
    break;
  case 'b': /* send break */
    /* send a break */
    tcsendbreak(fd, 0);
    break;
  case 't': /* set terminal */
    help_state = 1;
    help_terminal();
    in_escape = 1;
    break;
  case '~': /* display it again */
    help_escape();
    break;
  default:
    /* pass the character through */
    /* "C-\ C-\" sends "C-\" */
    write(fd, &c, 1);
    break;
  }

  if (in_escape == 0)
    help_done();

  return;
}
示例#2
0
int main(int argc, char *argv[]) {
  struct termios pts;    /* termios settings on port */
  struct termios sts;    /* termios settings on stdout/in */
  struct sigaction sact; /* used to initialize the signal handler */
  int opt;

  strcpy(device, "/dev/ttyUSB0");

  while ( (opt = getopt(argc, argv, "h?D:b:d:p:f:s:e:r:")) != -1 ) {
    switch (opt) {
      /* help message */
      case 'h':
        usage();
        return 0;
        break;
      /* set the serial device name */
      case 'D':
        strcpy(device, optarg);
        break;
      /* set the baud rate */
      case 'b':
        curr_state.baud_rate = atoi(optarg);
        break;
      /* data bits */
      case 'd':
        curr_state.data_bits = optarg[0];
        break;
      /* parity */
      case 'p':
        curr_state.parity = optarg[0];
        break;
      /* setup flow control */
      case 'f':
        curr_state.flow_control = optarg[0];
        break;
      /* stop bits */
      case 's':
        curr_state.stop_bits = optarg[0];
        break;
      /* echo type */
      case 'e':
        curr_state.echo_type = optarg[0];
        break;
      /* NL to CR mapping on output */
      case 'r':
        curr_state.onlcr_mapping = optarg[0];
        break;
      default:
        break;
    } /* end switch */
  }  /* end while */

  /* open the device */
  pf = open(device, O_RDWR);
  if (pf < 0) {
    fprintf(stderr, "\n  ERROR: cannot open device %s\n\n", device);
    exit(1);
  }
  /* save old serial port config */
  tcgetattr(pf, &pts);
  memcpy(&pots, &pts, sizeof(pots));
  /* setup serial port with new settings */
  init_comm(&pts);
  fprintf(stdout, "\nWelcome to Nanocomsole " VERSION);
  display_state();
  fprintf(stdout, "Press CTRL+T for menu options\n\n");

  /* Now deal with the local terminal side */
  tcgetattr(STDIN_FILENO, &sts);
  memcpy(&sots, &sts, sizeof(sots)); /* to be used upon exit */
  init_stdin(&sts);
  tcsetattr(STDIN_FILENO, TCSANOW, &sts);

  /* set the signal handler to restore the old termios handler */
  sact.sa_handler = cleanup_termios;
  sigaction(SIGHUP, &sact, NULL);
  sigaction(SIGINT, &sact, NULL);
  sigaction(SIGPIPE, &sact, NULL);
  sigaction(SIGTERM, &sact, NULL);

  /* run the main program loop */
  mux_loop(pf);

  /* unreachable line ?? */
  cleanup_termios(0);

  return 0;
}