Beispiel #1
0
int main(int argc, char **argv)
{
  FILE *f;
  struct stat st;
  int n_read;
  byte *b;

  stat("/etc/passwd", &st);
  f = fopen("/etc/passwd", "r");

  b = (byte *)malloc(st.st_size + 1);
  memset(b, 0, st.st_size + 1);

  n_read = fread(b, sizeof(byte), st.st_size, f);

  fclose(f);

  if (!input_initialize((char *)b))
    return -1;

  while (!input_eof())
    fputc((int)input_byte(), stdout);

  input_cleanup();

  free(b);

  return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
  if (!input_initialize(NULL))
    return -1;

  while (!input_eof())
    fputc((int)input_byte(), stdout);

  input_cleanup();

  return 0;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <soundfilename>\n", argv[0]);
		return -1;
	}
	FILE *soundfile;
	if ((soundfile = fopen(argv[1], "w")) == NULL) {
		printf("Could not open sound file\n");
		return -1;
	}
	int err;
	done = 0;
	snd_pcm_t *handle;
	short *buffer;
	int buffer_l = 1024;
	
	signal(SIGINT, closedown);
	printf("Recording sound to: %s\n", argv[1]);
	if ((err = snd_pcm_open(&handle, "hw:2,0", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
		fprintf(stderr, "Error opening sound device\n");
		return -1;
	}
	if (input_initialize(handle, &buffer, &buffer_l) < 0) {
		return -1;
	}

	while (!done) {
		input_read(handle, buffer, buffer_l);
		fwrite(buffer, 2, buffer_l, soundfile);
	}

	printf("Closing down...\n");
	fclose(soundfile);
	input_cleanup(handle);
	
	return 0;
}
Beispiel #4
0
  int initialize_everything()
  {
    time_t ss = time(0);
    enigma_user::random_set_seed(ss);
    enigma_user::mtrandom_seed(ss);

	// must occur before the create/room start/game start events so that it does not override the user setting them in code
	enigma::game_settings_initialize();

    graphicssystem_initialize();
    audiosystem_initialize();

    #if defined(BUILDMODE) && BUILDMODE
      buildmode::buildinit();
    #endif

    event_system_initialize();
    timeline_system_initialize();
    input_initialize();
    sprites_init();
    backgrounds_init();
    widget_system_initialize();

    // Open the exe for resource load
    char exename[1025];
    windowsystem_write_exename(exename);
    FILE* exe = fopen(exename,"rb");
    if (!exe)
      show_error("Resource load fail: exe unopenable",0);
    else do
    {
      int nullhere;
      // Read the magic number so we know we're looking at our own data
      fseek(exe,-8,SEEK_END);
      char str_quad[4];
      if (!fread(str_quad,4,1,exe) or str_quad[0] != 'r' or str_quad[1] != 'e' or str_quad[2] != 's' or str_quad[3] != '0') {
        printf("No resource data in exe\n");
        break;
      }

      // Get where our resources are located in the module
      int pos;
      if (!fread(&pos,4,1,exe)) break;

      // Go to the start of the resource data
      fseek(exe,pos,SEEK_SET);
      if (!fread(&nullhere,4,1,exe)) break;
      if(nullhere) break;

      enigma::exe_loadsprs(exe);
      enigma::exe_loadsounds(exe);
      enigma::exe_loadbackgrounds(exe);
      enigma::exe_loadfonts(exe);
	  #ifdef PATH_EXT_SET
		enigma::exe_loadpaths(exe);
	  #endif

      fclose(exe);
    }
    while (false);

    //Load object struct
    enigma::objectdata_load();

    //Load rooms
    enigma::rooms_load();

    //Go to the first room
    if (enigma_user::room_count)
      enigma::game_start();
    else
        enigma_user::window_default();

    return 0;
  }
Beispiel #5
0
int main(int argc, char **argv)
{
  int c;
  extern char *optarg;
  extern int optind;
  int long_index = 0;
  struct option long_opts[] =
  {
    { "output", 1, 0, 'o' },
    { "version", 0, 0, 'v' },
    { "help", 0, 0, 'h' },
    { 0, 0, 0, 0 }
  };
  egg_token *t;
  char *input_file;
  char *output_file = NULL;
  FILE *of = NULL;

  while ((c = getopt_long(argc, argv, "o:vh", long_opts, &long_index)) != -1)
  {
    switch (c)
    {
      case 'o':
        output_file = strdup(optarg);
        break;
      case 'v':
        version();
        return 0;
      case 'h':
      default:
        version();
        usage();
        return 1;
    }
  }

  input_file = NULL;
  if (optind < argc)
    if (strcmp(argv[optind], "-"))
      input_file = strdup(argv[optind]);

  if (!input_initialize(input_file))
    return 1;

  t = grammar();
  if (!t)
    fprintf(stderr, "Failed to parse grammar.\n");

  input_cleanup();

  if (!t)
    return 1;

  if (output_file)
  {
    of = fopen(output_file, "w");
    if (!of)
    {
      fprintf(stderr, "Failed to open '%s'\n", output_file);
      return 1;
    }
  }
  else
    of = stdout;

  map(of, t);

  if (output_file)
  {
    if (of)
      fclose(of);
    free(output_file);
  }

  return 0;
}
Beispiel #6
0
bool view_initialize(char *err_str)
{
	int				rate;
	
		// clear view structure
		
	memset(&view,0x0,sizeof(view_type));

		// allocate memory
		
	if (!view_memory_allocate()) {
		view_memory_release();
		strcpy(err_str,"Out of Memory");
		return(FALSE);
	}

		// start SDL

	if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE)==-1) {
		view_memory_release();
		sprintf(err_str,"SDL: Could not init (Error: %s)\n",SDL_GetError());
		return(FALSE);
	}
	
		// create screen sizes
		
	view_create_screen_size_list();
	
		// gl initialize

	if (!view_initialize_display(err_str)) return(FALSE);

		// sound initialize
		
	if (!al_initialize(err_str)) {
		view_shutdown_display();
		view_memory_release();
		SDL_Quit();
		return(FALSE);
	}

	al_set_volume(setup.sound_volume);
	al_music_set_volume(setup.music_volume);
	al_music_set_state(setup.music_on);
	
		// read in the sounds
		
	read_settings_sound();
	
		// connect the input
		
	read_settings_action();
	
	input_initialize(gl_in_window_mode());
	setup_to_input();
	
		// draw timing
		
	view.time.input_tick=game_time_get();
	view.time.draw_tick=game_time_get();
	
	rate=render_info.monitor_refresh_rate;
	if (!setup.lock_fps_refresh) rate=max_fps;
	
	if (rate>max_fps) rate=max_fps;
	view.time.draw_time=1000/rate;
	
	return(TRUE);
}
Beispiel #7
0
int main(int argc, char *argv[])
{
	int err;
	done = 0;
	snd_pcm_t *handle;
	FILE *sound_in_fd = NULL;
	FILE *sound_out_fd = NULL;
	int channels;
	short *buffer = NULL;
	int buffer_l;
	int buffer_read;
	struct serial_state_t *serial = NULL;
	struct ipc_state_t *ipc = NULL;
	struct receiver *rx_a = NULL;
	struct receiver *rx_b = NULL;
#ifdef HAVE_PULSEAUDIO
	pa_simple *pa_dev = NULL;
#endif
	
	/* command line */
	parse_cmdline(argc, argv);
	
	/* open syslog, write an initial log message and read configuration */
	open_log(logname, 0);
	hlog(LOG_NOTICE, "Starting up...");
	if (read_config()) {
		hlog(LOG_CRIT, "Initial configuration failed.");
		exit(1);
	}
	
	/* fork a daemon */
	if (fork_a_daemon) {
		int i = fork();
		if (i < 0) {
			hlog(LOG_CRIT, "Fork to background failed: %s", strerror(errno));
			fprintf(stderr, "Fork to background failed: %s\n", strerror(errno));
			exit(1);
		} else if (i == 0) {
			/* child */
			/* write pid file, now that we have our final pid... might fail, which is critical */
			hlog(LOG_DEBUG, "Writing pid...");
			if (!writepid(pidfile))
				exit(1);
		} else {
			/* parent, quitting */
			hlog(LOG_DEBUG, "Forked daemon process %d, parent quitting", i);
			exit(0);
		}
	}
	
	
	signal(SIGINT, closedown);
	signal(SIGPIPE, brokenconnection);
	
	/* initialize position cache for timed JSON AIS transmission */
	if (uplink_config) {
		hlog(LOG_DEBUG, "Initializing cache...");
		if (cache_init())
			exit(1);
		hlog(LOG_DEBUG, "Initializing jsonout...");
		if (jsonout_init())
			exit(1);
	}
	
	/* initialize serial port for NMEA output */
	if (serial_port)
		serial = serial_init();

	/* initialize Unix domain socket for communication with gnuaisgui */
	ipc = gnuais_ipc_init();
	if(ipc == 0){
		hlog(LOG_ERR, "Could not open Unix Domain Socket");
	}
	
	/* initialize the AIS decoders */
	if (sound_channels != SOUND_CHANNELS_MONO) {
		hlog(LOG_DEBUG, "Initializing demodulator A");
		rx_a = init_receiver('A', 2, 0,serial,ipc);
		hlog(LOG_DEBUG, "Initializing demodulator B");
		rx_b = init_receiver('B', 2, 1,serial,ipc);
		channels = 2;
	} else {
		hlog(LOG_DEBUG, "Initializing demodulator A");
		rx_a = init_receiver('A', 1, 0,serial,ipc);
		channels = 1;
	}
#ifdef HAVE_PULSEAUDIO
	if(sound_device != NULL && ((strcmp("pulse",sound_device) == 0) || (strcmp("pulseaudio",sound_device) == 0))){
		if((pa_dev = pulseaudio_initialize()) == NULL){
			hlog(LOG_CRIT, "Error opening pulseaudio device");
			return -1;
		}
		buffer_l = 1024;
		int extra = buffer_l % 5;
		buffer_l -= extra;
		buffer = (short *) hmalloc(buffer_l * sizeof(short) * channels);
	}
	else if (sound_device){
#else
	if (sound_device){
#endif

		if ((err = snd_pcm_open(&handle, sound_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
			hlog(LOG_CRIT, "Error opening sound device (%s)", sound_device);
			return -1;
		}
		
		if (input_initialize(handle, &buffer, &buffer_l) < 0)
			return -1;
	} else if (sound_in_file) {
		if ((sound_in_fd = fopen(sound_in_file, "r")) == NULL) {
			hlog(LOG_CRIT, "Could not open sound file %s: %s", sound_in_file, strerror(errno));
			return -1;
		}
		hlog(LOG_NOTICE, "Reading audio from file: %s", sound_in_file);
		buffer_l = 1024;
		int extra = buffer_l % 5;
		buffer_l -= extra;
		buffer = (short *) hmalloc(buffer_l * sizeof(short) * channels);
	} else {
		hlog(LOG_CRIT, "Neither sound device or sound file configured.");
		return -1;
	}
	
	if (sound_out_file) {
		if ((sound_out_fd = fopen(sound_out_file, "w")) == NULL) {
			hlog(LOG_CRIT, "Could not open sound output file %s: %s", sound_out_file, strerror(errno));
			return -1;
		}
		hlog(LOG_NOTICE, "Recording audio to file: %s", sound_out_file);
	}
	
#ifdef HAVE_MYSQL
	if (mysql_db) {
		hlog(LOG_DEBUG, "Saving to MySQL database \"%s\"", mysql_db);
		if (!(my = myout_init()))
			return -1;
			
		if (mysql_keepsmall)
			hlog(LOG_DEBUG, "Updating database rows only.");
		else
			hlog(LOG_DEBUG, "Inserting data to database.");
			
		if (mysql_oldlimit)
			hlog(LOG_DEBUG, "Deleting data older than %d seconds", mysql_oldlimit);
	}
#endif
	
	hlog(LOG_NOTICE, "Started");
	
	while (!done) {
		if (sound_in_fd) {
			buffer_read = fread(buffer, channels * sizeof(short), buffer_l, sound_in_fd);
			if (buffer_read <= 0)
				done = 1;
		} 
#ifdef HAVE_PULSEAUDIO
		else if (pa_dev){
			buffer_read = pulseaudio_read(pa_dev, buffer, buffer_l);
		}
#endif
		else {
			buffer_read = input_read(handle, buffer, buffer_l);
			//printf("read %d\n", buffer_read);
		}
		if (buffer_read <= 0)
			continue;
		
		if (sound_out_fd) {
			fwrite(buffer, channels * sizeof(short), buffer_read, sound_out_fd);
		}
		
		if (sound_channels == SOUND_CHANNELS_MONO) {
			receiver_run(rx_a, buffer, buffer_read);
		}
		if (sound_channels == SOUND_CHANNELS_BOTH
		    || sound_channels == SOUND_CHANNELS_RIGHT) {
			/* ch a/0/right */
			receiver_run(rx_a, buffer, buffer_read);
		}
		if (sound_channels == SOUND_CHANNELS_BOTH
		    || sound_channels == SOUND_CHANNELS_LEFT) {	
			/* ch b/1/left */
			receiver_run(rx_b, buffer, buffer_read);
		}
	}
	
	hlog(LOG_NOTICE, "Closing down...");
	if (sound_in_fd) {
		fclose(sound_in_fd);
	}
#ifdef HAVE_PULSEAUDIO
	else if (pa_dev) {
		pulseaudio_cleanup(pa_dev);
	}
#endif
	else {
		input_cleanup(handle);
		handle = NULL;
	}

	
	if (sound_out_fd)
		fclose(sound_out_fd);
	
	hfree(buffer);

	gnuais_ipc_deinit(ipc);
	
	if (serial)
		serial_close(serial);
	
	if (uplink_config)
		jsonout_deinit();
	
	if (cache_positions)
		cache_deinit();
	
	if (rx_a) {
		struct demod_state_t *d = rx_a->decoder;
		hlog(LOG_INFO,
			"A: Received correctly: %d packets, wrong CRC: %d packets, wrong size: %d packets",
			d->receivedframes, d->lostframes,
			d->lostframes2);
	}
	
	if (rx_b) {
		struct demod_state_t *d = rx_b->decoder;
		hlog(LOG_INFO,
			"B: Received correctly: %d packets, wrong CRC: %d packets, wrong size: %d packets",
			d->receivedframes, d->lostframes,
			d->lostframes2);
	}
	
	free_receiver(rx_a);
	free_receiver(rx_b);
	
	free_config();
	close_log(0);
	
	return 0;
}