Changeset 5933:e2de7f786235 in roaraudio


Ignore:
Timestamp:
09/11/13 20:29:11 (11 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

allow reading commands from file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roarclients/roarlight.c

    r5932 r5933  
    2626#include <roaraudio.h> 
    2727#include <libroarlight/libroarlight.h> 
     28#include <stdio.h> 
    2829 
    2930#define CONVAR struct roar_connection * con 
     
    3334 
    3435static int __run_argv(int argc, char * argv[]); 
     36static int __open_and_run_file(const char * file); 
    3537 
    3638void usage (void) { 
     
    153155   } 
    154156 
     157  } else if ( *k == '@' ) { 
     158   __open_and_run_file(k); 
    155159  } else { 
    156160   fprintf(stderr, "Error: invalid command: %s\n", k); 
     
    160164 
    161165 return 0; 
     166} 
     167 
     168static inline void __strip_space(char ** str) { 
     169 for (; **str == ' '; (*str)++); 
     170} 
     171 
     172static int __parse_line_and_run(char * line) { 
     173#define MAX_ARGS 16 
     174 int argc = 0; 
     175 char * argv[MAX_ARGS]; 
     176 char * p; 
     177 
     178 while (line != NULL) { 
     179  if ( argc == MAX_ARGS ) { 
     180   fprintf(stderr, "Error: too many arguments.\n"); 
     181   return 1; 
     182  } 
     183 
     184  p = strstr(line, " "); 
     185  if ( p != NULL ) { 
     186   *p = 0; 
     187    p++; 
     188   __strip_space(&p); 
     189  } 
     190 
     191  argv[argc++] = line; 
     192 
     193  line = p; 
     194 } 
     195 
     196 return __run_argv(argc, argv); 
     197} 
     198 
     199static int __run_file(FILE * file) { 
     200 char buffer[1024]; 
     201 ssize_t len; 
     202 int ret; 
     203 
     204 while (fgets(buffer, sizeof(buffer), file) != NULL) { 
     205  len = roar_mm_strlen(buffer); 
     206  if ( len > 0 && buffer[len-1] == '\n' ) 
     207   buffer[len-1] = 0; 
     208 
     209  if ( !len || buffer[0] == 0 ) 
     210   continue; 
     211 
     212  ret = __parse_line_and_run(buffer); 
     213  if ( ret != 0 ) 
     214   return ret; 
     215 } 
     216 
     217 return 0; 
     218} 
     219 
     220static int __open_and_run_file(const char * file) { 
     221 FILE * input; 
     222 int ret; 
     223 
     224 if ( !strcmp(file, "@-") ) { 
     225  return __run_file(stdin); 
     226 } else { 
     227  input = fopen(file+1, "r"); 
     228  if ( input == NULL ) { 
     229   fprintf(stderr, "Error: can not open input file: %s\n", file+1); 
     230   return 1; 
     231  } 
     232  ret = __run_file(input); 
     233  fclose(input); 
     234  return ret; 
     235 } 
    162236} 
    163237 
Note: See TracChangeset for help on using the changeset viewer.