Exemplo n.º 1
0
/* return values:
 * 0 on success,
 * -1 on fatal error,
 * -2 if EOF, or
 * return value of command */
int
sl_command_loop(SL_cmd *cmds, const char *prompt, void **data)
{
    int ret = 0;
    char *buf;
    int argc;
    char **argv;

    buf = sl_readline(prompt);
    if(buf == NULL)
        return -2;

    if(*buf)
        add_history(buf);
    ret = sl_make_argv(buf, &argc, &argv);
    if(ret) {
        fprintf(stderr, "sl_loop: out of memory\n");
        free(buf);
        return -1;
    }
    if (argc >= 1) {
        ret = sl_command(cmds, argc, argv);
        if(ret == -1) {
            printf ("Unrecognized command: %s\n", argv[0]);
            ret = 0;
        }
    }
    free(buf);
    free(argv);
    return ret;
}
Exemplo n.º 2
0
/***************************************************************************
 * sl_read_streamlist:
 *
 * Read a list of streams and selectors from a file and add them to the 
 * stream chain for configuring a multi-station connection.
 *
 * If 'defselect' is not NULL or 0 it will be used as the default selectors
 * for entries will no specific selectors indicated.
 *
 * The file is expected to be repeating lines of the form:
 *   <NET> <STA> [selectors]
 * For example:
 * --------
 * # Comment lines begin with a '#' or '*'
 * GE ISP  BH?.D
 * NL HGN
 * MN AQU  BH?  HH?
 * -------- 
 *
 * Returns the number of streams configured or -1 on error.
 ***************************************************************************/
int
sl_read_streamlist (SLCD * slconn, const char * streamfile,
		    const char * defselect)
{
  char net[3];
  char sta[6];
  char selectors[100];
  char line[100];
  int streamfd;
  int fields;
  int count;
  int stacount;
  int addret;
  
  net[0] = '\0';
  sta[0] = '\0';
  selectors[0] = '\0';
  
  /* Open the stream list file */
  if ( (streamfd = slp_openfile (streamfile, 'r')) < 0 )
    {
      if (errno == ENOENT)
	{
	  sl_log_r (slconn, 2, 0, "could not find stream list file: %s\n", streamfile);
	  return -1;
	}
      else
	{
	  sl_log_r (slconn, 2, 0, "opening stream list file, %s\n", strerror (errno));
	  return -1;
	}
    }
  
  sl_log_r (slconn, 1, 1, "Reading stream list from %s\n", streamfile);
  
  count = 1;
  stacount = 0;
  
  while ( (sl_readline (streamfd, line, sizeof(line))) >= 0 )
    {
      fields = sscanf (line, "%2s %5s %99[a-zA-Z0-9!?. ]\n",
		       net, sta, selectors);
      
      /* Ignore blank or comment lines */
      if ( fields < 0 || net[0] == '#' || net[0] == '*' )
	continue;

      if ( fields < 2 )
	{
	  sl_log_r (slconn, 2, 0, "cannot parse line %d of stream list\n", count);
	}
      
      /* Add this stream to the stream chain */
      if ( fields == 3 )
	{
	  sl_addstream (slconn, net, sta, selectors, -1, NULL);
	  stacount++;
	}
      else
	{
	  addret = sl_addstream (slconn, net, sta, defselect, -1, NULL);
	  stacount++;
	}
      
	count++;
    }
  
  if ( stacount == 0 )
    {
      sl_log_r (slconn, 2, 0, "no streams defined in %s\n", streamfile);
    }
  else if ( stacount > 0 )
    {
      sl_log_r (slconn, 1, 2, "Read %d streams from %s\n", stacount, streamfile);
    }

  if ( close (streamfd) )
    {
      sl_log_r (slconn, 2, 0, "closing stream list file, %s\n", strerror (errno));
      return -1;
    }
  
  return count;
}  /* End of sl_read_streamlist() */