int main(int argc, char * argv[]) {
  // command line options
  if (cmdline_option_exists(argv, argv + argc, "-h")) help_exit(argc, argv);

  const char * n_threads_str = get_cmdline_option(argv, argv + argc, "-p");
  if (!n_threads_str) help_exit(argc, argv);
  const size_t n_threads = std::atoi(n_threads_str);

  const char * n_expected_columns_str = get_cmdline_option(argv, argv + argc, "-c");
  if (!n_expected_columns_str) help_exit(argc, argv);
  const size_t n_expected_columns = std::atoi(n_expected_columns_str);

  const char * filepath = get_cmdline_option(argv, argv + argc, "-f");
  if (!filepath) help_exit(argc, argv);

  // instantiate CsvConfig
  BENCH_START;
  PCP::CsvConfig csv_config(filepath, false);
  BENCH_STOP("mmap(2)+madvise(2) file");

  // setup range each thread parse
  size_t size_per_thread = (csv_config.filesize() - csv_config.body_offset()) / n_threads;
  std::vector<parser_thread_arg_t> parser_thread_args(n_threads);
  for (size_t i = 0; i < n_threads; ++i) {
    parser_thread_arg_t & parser_thread_arg = parser_thread_args[i];
    parser_thread_arg.n_columns = 0;

    PCP::partial_csv_t & partial_csv = parser_thread_arg.partial_csv;
    partial_csv.csv_config = &csv_config;
    partial_csv.parse_from = csv_config.body_offset() + i * size_per_thread;
    partial_csv.parse_to = csv_config.body_offset() + (i + 1) * size_per_thread - 1;
  }

  // create threads
  std::vector<pthread_t> tids(n_threads);
  BENCH_START;
  for (size_t i = 0; i < n_threads; ++i)
      pthread_create(&tids[i], NULL, (void *(*)(void *))partial_parse, &parser_thread_args[i]);
  // join threads
  for (size_t i = 0; i < n_threads; ++i)
      pthread_join(tids[i], NULL);
  BENCH_STOP("join parsing threads");

  // calculate total number of columns
  size_t n_total_columns = 0;
  for (size_t i = 0; i < n_threads; ++i) n_total_columns += parser_thread_args[i].n_columns;

  // check the answer
  if (n_total_columns == n_expected_columns) {
    std::cout << "OK. Parsed " << n_total_columns << " columns." << std::endl;
    return 0;
  }
  else {
    std::cout << "NG. Parsed " << n_total_columns << " columns, while " << n_expected_columns << " columns are expected." << std::endl;
    return 1;
  }
}
Example #2
0
// Full init needed by multiplexer or reentrant calls, calls singleinit at end
void toy_init(struct toy_list *which, char *argv[])
{
  void *oldwhich = toys.which;

  // Drop permissions for non-suid commands.

  if (CFG_TOYBOX_SUID) {
    if (!toys.which) toys.which = toy_list;

    uid_t uid = getuid(), euid = geteuid();

    if (!(which->flags & TOYFLAG_STAYROOT)) {
      if (uid != euid) {
        if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
        euid = uid;
        toys.wasroot++;
      }
    } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
      error_msg("Not installed suid root");

    if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
  }

  // Free old toys contents (to be reentrant), but leave rebound if any
  // don't blank old optargs if our new argc lives in the old optargs.
  if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
  memset(&toys, 0, offsetof(struct toy_context, rebound));
  if (oldwhich) memset(&this, 0, sizeof(this));

  // Continue to portion of init needed by standalone commands
  toy_singleinit(which, argv);
}
Example #3
0
static unsigned long get_flag_val(char ch)
{
  struct ext2_attr *ptr = e2attrs;

  for (; ptr->name; ptr++)
    if (ptr->opt == ch) return ptr->flag;
  help_exit("bad '%c'", ch);
}
Example #4
0
File: help.c Project: cnhenry/toxic
void help_onKey(ToxWindow *self, wint_t key)
{
    switch(key) {
        case 'x':
        case T_KEY_ESC:
            help_exit(self);
            break;

        case 'c':
#ifdef VIDEO
            help_init_window(self, 22, 80);
#elif AUDIO
            help_init_window(self, 19, 80);
#else
            help_init_window(self, 9, 80);
#endif
            self->help->type = HELP_CHAT;
            break;

        case 'g':
#ifdef VIDEO
            help_init_window(self, 28, 80);
#elif AUDIO
            help_init_window(self, 24, 80);
#else
            help_init_window(self, 20, 80);
#endif
            self->help->type = HELP_GLOBAL;
            break;

        case 'r':
            help_init_window(self, 6, 80);
            self->help->type = HELP_GROUP;
            break;

        case 'f':
            help_init_window(self, 10, 80);
            self->help->type = HELP_CONTACTS;
            break;

        case 'k':
            help_init_window(self, 13, 80);
            self->help->type = HELP_KEYS;
            break;

        case 'm':
            help_init_menu(self);
            self->help->type = HELP_MENU;
            break;
    }
}
Example #5
0
void i2cset_main(void)
{
  int bus = atolx_range(toys.optargs[0], 0, INT_MAX);
  int chip = atolx_range(toys.optargs[1], 0, 0x7f);
  int addr = atolx_range(toys.optargs[2], 0, 0xff);
  char *mode = toys.optargs[toys.optc-1];
  int fd, i;
  struct i2c_smbus_ioctl_data ioctl_data;
  union i2c_smbus_data data;

  memset(&data, 0, sizeof(data));
  if (strlen(mode) != 1) help_exit("mode too long");
  if (*mode == 'b' && toys.optc == 5) {
    ioctl_data.size = I2C_SMBUS_BYTE_DATA;
    data.byte = atolx_range(toys.optargs[3], 0, 0xff);
  } else if (*mode == 'w' && toys.optc == 5) {
    ioctl_data.size = I2C_SMBUS_WORD_DATA;
    data.word = atolx_range(toys.optargs[3], 0, 0xffff);
  } else if (*mode == 'i' && toys.optc >= 5) {
    if (toys.optc - 4 > I2C_SMBUS_BLOCK_MAX) error_exit("too much data");
    ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA;
    for (i = 0; i < toys.optc - 4; ++i)
      data.block[i+1] = atolx_range(toys.optargs[3+i], 0, 0xff);
    data.block[0] = toys.optc - 4;
  } else {
    help_exit("syntax error");
  }

  confirm("Write register 0x%02x from chip 0x%02x on bus %d?", addr, chip, bus);

  fd = i2c_open(bus, (toys.optflags&FLAG_f)?I2C_SLAVE_FORCE:I2C_SLAVE, chip);
  ioctl_data.read_write = I2C_SMBUS_WRITE;
  ioctl_data.command = addr;
  ioctl_data.data = &data;
  xioctl(fd, I2C_SMBUS, &ioctl_data);
  close(fd);
}
Example #6
0
void chattr_main(void)
{
  char **argv = toys.optargs;

  memset(&chattr, 0, sizeof(struct _chattr));
  parse_cmdline_arg(&argv);
  if (!*argv) help_exit("no file");
  if (chattr.set && (chattr.add || chattr.rm))
    error_exit("no '=' with '-' or '+'");
  if (chattr.rm & chattr.add) error_exit("set/unset same flag");
  if (!(chattr.add || chattr.rm || chattr.set || chattr.vflag))
    error_exit("need '-v', '=', '-' or '+'");
  for (; *argv; argv++) dirtree_read(*argv, update_attr);
  toys.exitval = 0; //always set success at this point.
}
Example #7
0
// Parse command line argument and fill the chattr structure.
static void parse_cmdline_arg(char ***argv)
{
  char *arg = **argv, *ptr = NULL;

  while (arg) {
    switch (arg[0]) {
      case '-':
        for (ptr = ++arg; *ptr; ptr++) {
          if (*ptr == 'R') {
            chattr.recursive = 1;
            continue;
          } else if (*ptr == 'v') {// get version from next argv.
            char *endptr;

            errno = 0;
            arg = *(*argv += 1);
            if (!arg) help_exit("bad -v");
            if (*arg == '-') perror_exit("Invalid Number '%s'", arg);
            chattr.version = strtoul(arg, &endptr, 0);
            if (errno || *endptr) perror_exit("bad version '%s'", arg);
            chattr.vflag = 1;
            continue;
          } else chattr.rm |= get_flag_val(*ptr);
        }
        break;
      case '+':
        for (ptr = ++arg; *ptr; ptr++)
          chattr.add |= get_flag_val(*ptr);
        break;
      case '=':
        for (ptr = ++arg; *ptr; ptr++)
          chattr.set |= get_flag_val(*ptr);
        break;
      default: return;
    }
    arg = *(*argv += 1);
  }
}
Example #8
0
void help_onKey(ToxWindow *self, wint_t key)
{
    switch(key) {
        case 'x':
        case T_KEY_ESC:
            help_exit(self);
            break;

        case 'c':
#ifdef _AUDIO
            help_init_window(self, 19, 80);
#else
            help_init_window(self, 9, 80);
#endif
            self->help->type = HELP_CHAT;
            break;

        case 'g':
#ifdef _AUDIO
            help_init_window(self, 21, 80);
#else
            help_init_window(self, 17, 80);
#endif
            self->help->type = HELP_GLOBAL;
            break;

        case 'k':
            help_init_window(self, 10, 80);
            self->help->type = HELP_KEYS;
            break;

        case 'm':
            help_init_menu(self);
            self->help->type = HELP_MENU;
            break;
    }
}
Example #9
0
void help_onKey(ToxWindow *self, wint_t key)
{
    int height;

    switch (key) {
        case 'x':
        case T_KEY_ESC:
            help_exit(self);
            break;

        case 'c':
#ifdef VIDEO
            help_init_window(self, 23, 80);
#elif AUDIO
            help_init_window(self, 20, 80);
#else
            help_init_window(self, 10, 80);
#endif
            self->help->type = HELP_CHAT;
            break;

        case 'g':
            height = 22;
#ifdef VIDEO
            height += 8;
#elif AUDIO
            height += 4;
#endif
#ifdef PYTHON
            height += 2;
#endif
            help_init_window(self, height, 80);
            self->help->type = HELP_GLOBAL;
            break;

        case 'r':
            help_init_window(self, 6, 80);
            self->help->type = HELP_GROUP;
            break;

#ifdef PYTHON

        case 'p':
            help_init_window(self, 4 + num_registered_handlers(), help_max_width());
            self->help->type = HELP_PLUGIN;
            break;
#endif /* PYTHON */

        case 'f':
            help_init_window(self, 10, 80);
            self->help->type = HELP_CONTACTS;
            break;

        case 'k':
            help_init_window(self, 15, 80);
            self->help->type = HELP_KEYS;
            break;

        case 'm':
            help_init_menu(self);
            self->help->type = HELP_MENU;
            break;
    }
}
Example #10
0
int main ( int argc, char ** argv )
{
    printf("*****  %s  *****\n",TITLE);
    SetupLib(argc,argv,NAME,PROG_UNKNOWN);

    printf("term width = %d\n",GetTermWidth(80,0));

 #ifdef HAVE_FIEMAP
    printf("* HAVE_FIEMAP defined!\n");
 #endif
 #ifdef FS_IOC_FIEMAP
    printf("* FS_IOC_FIEMAP defined!\n");
 #endif

 #if defined(TEST) && defined(DEBUG)
    if (0)
    {
	id6_t * id6 = (id6_t*)iobuf;
	PRINT("sizeof(id6_t)=%zd, %p,%p,%p -> %zu,%zu,%zu\n",
		sizeof(id6_t),
		id6, id6+1, id6+2,
		(ccp)id6-iobuf, (ccp)(id6+1)-iobuf, (ccp)(id6+2)-iobuf );
    }
 #endif

    if ( argc < 2 )
	help_exit();

    int cmd_stat;
    const CommandTab_t * cmd_ct = ScanCommand(&cmd_stat,argv[1],CommandTab);
    if (!cmd_ct)
    {
	PrintCommandError(CommandTab,argv[1],cmd_stat,0);
	help_exit();
    }

    argv[1] = argv[0];
    argv++;
    argc--;

    switch(cmd_ct->id)
    {
	case CMD_TEST:			return test(argc,argv); break;

	case CMD_FILENAME:		test_filename(argc,argv); break;
	case CMD_MATCH_PATTERN:		test_match_pattern(argc,argv); break;
	case CMD_OPEN_DISC:		test_open_disc(argc,argv); break;
	case CMD_HEXDUMP:		test_hexdump(argc,argv); break;

 #ifdef HAVE_OPENSSL
	case CMD_SHA1:			test_sha1(); break;
 #endif
 #ifndef NO_BZIP2
	case CMD_BZIP2:			test_bzip2(argc,argv); break;
 #endif
 #ifdef HAVE_WORK_DIR
	case CMD_WIIMM:			test_wiimm(argc,argv); break;
 #endif

	case CMD_DEVELOP:		develop(argc,argv); break;

	//case CMD_HELP:
	default:
	    help_exit();
    }

    CloseAll();

    if (SIGINT_level)
	ERROR0(ERR_INTERRUPT,"Program interrupted by user.");

    return max_error;
}
Example #11
0
void init_main(int argc, char *argv[]) {
	int kbd = -1;
	/* name, has_arg, flag, val */
	static struct option long_options[] = {
		{"tape", 1, 0, 't'},
		{"drivea", 1, 0, 'a'},
		{"driveb", 1, 0, 'b'},
		{"cart", 1, 0, 'c'},
		{"frameskip", 1, 0, 'f'},
		{"fps", 1, 0, 'z'},
		{"sync", 0, 0, 'y'},
		#ifdef HAVE_GL
		{"opengl", 1, 0, 'g'},
		#endif
		{"crtctype", 1, 0, 'r'},
		{"cpctype", 1, 0, 'p'},
		{"snapshot", 1, 0, 's'},
		{"kbdtype", 1, 0, 'k'},
		{"soundplugin", 1, 0, 'o'},
#ifdef HAVE_SDL
		{"doublesize", 0, 0, 'd'},
		{"fullscreen", 0, 0, 'u'},
#endif
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0}
	};
	int c;
	int digit_optind = 0;
	/* Default is 50 frames per second without sync to screen refresh rate */
	int fps = 50;
	int sync = 0;
	int refresh;
	char *tape = NULL;
	char *drivea = NULL;
	char *driveb = NULL;
	char *cart = NULL;
	char *frameskip = NULL;
	char *crtctype = NULL;
	char *cpctype = NULL;
	char *snapshot = NULL;
	char *kbdtype = NULL;
	char *soundplugin = NULL;
#ifdef HAVE_SDL
	BOOL doubled = FALSE;
	BOOL fullscreen = FALSE;
#endif
	do {
		int this_option_optind = optind ? optind : 1;
		int option_index = 0;
		c = getopt_long_only (argc, argv, "",
			long_options, &option_index);
		printf("c: %i %c\n", c, c);
		switch(c) {
			case 'h':
			case '?':
				help_exit();
				break;
			case 't':
				tape = optarg;
				break;
			case 'a':
				drivea = optarg;
				break;
			case 'b':
				driveb = optarg;
				break;
			case 'c':
				cart = optarg;
				break;
#ifdef HAVE_SDL
			case 'd':
				doubled = TRUE;
				break;
			case 'u':
				fullscreen = TRUE;
				break;
#endif
			case 'f':
				frameskip = optarg;
				break;
			case 'y':
				/* Activate sync to screen refresh rate */
				sync=1;
#ifdef HAVE_GL
				/* Activate sync on nVIDIA chips */
				putenv("__GL_SYNC_TO_VBLANK=1");
#endif
				break;
			case 'z':
				/* Set refresh rate in Hz directly */
				fps = atoi(optarg);
				break;
			case 'r':
				crtctype = optarg;
				break;
			case 'p':
				cpctype = optarg;
				break;
			case 's':
				snapshot = optarg;
				break;
			case 'k':
				kbdtype = optarg;
				break;
			case 'o':
				soundplugin = optarg;
				break;

		}
	} while (c != -1);
	printf("tape: %s\n", tape);

	CPCEmulation_InitialiseDefaultSetup();

	ConfigCPC6128();

		if (tape) {
			if (!GenericInterface_InsertTape(tape)) {
				printf(Messages[73], tape);
			}
		}

		if (drivea) {
			if (!GenericInterface_InsertDiskImage(0, drivea)) {
				printf(Messages[74],
					drivea);
			}
		}

		if (driveb) {
			if (!GenericInterface_InsertDiskImage(1, driveb)) {
				printf(Messages[74],
					driveb);
			}
		}

		if (cart) {
			if (!GenericInterface_InsertCartridge(cart)) {
				printf(Messages[75], cart);
			}
		}

		if (frameskip) {
			int fskip;
			fskip =  atoi(frameskip);
			CPC_SetFrameSkip(fskip);
		}

		if (crtctype) {
			int crtc;
			crtc = atoi(crtctype);
			CRTC_SetType(crtc);
		}

		if (cpctype) {
			int cpc;
			cpc = atoi(cpctype);
			switch (cpc)
			{
						case 0:
						{
				ConfigCPC464();
				}
				break;
				case 1:
				{
				ConfigCPC664();
				}
				break;
				case 2:
				{
				ConfigCPC6128();
				}
				break;
				case 3:
				{
				ConfigCPC6128s();
				}
				break;
				case 4:
				{
				Config464Plus();
				}
				break;
				case 5:
				{
				Config6128Plus();
				}
				break;
				case 6:
				{
				ConfigKCCompact();
				}
				break;

				default:
				{
				ConfigCPC6128();
				}
				break;

			}
		}

		if (snapshot) {
			if (!GenericInterface_LoadSnapshot(snapshot)) {
				printf(Messages[78],
					snapshot);
			}
		}

		if ( kbdtype ) kbd = atoi(kbdtype);
		printf("kbdtype: %i\n",kbd);

		if ( soundplugin ) {
			sound_plugin = getSoundplugin(soundplugin);
		} else {
			sound_plugin = SOUND_PLUGIN_AUTO;
		}
		if ( sound_plugin == SOUND_PLUGIN_AUTO ) {
			sound_plugin = autoDetectSoundplugin();
		}
		printf("soundplugin: %i (%s)\n",sound_plugin,soundpluginNames[sound_plugin]);



#ifdef HAVE_SDL
		fprintf(stderr, "Initializing SDL\n");
		if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER
			|SDL_INIT_JOYSTICK|SDL_INIT_NOPARACHUTE) < 0 ) {
			fprintf(stderr, "SDL could not be initialized: %s\n", SDL_GetError());
			exit(1);
		}
		sdl_InitialiseKeyboardMapping(0);
		sdl_InitialiseJoysticks();
		atexit(SDL_Quit);
#endif

		Host_InitDriveLEDIndicator();

		Render_SetDisplayWindowed();


		/* This hardly ever works, because SDL_Flip is often not capable of waiting for a vertical retrace */
		if (sync) {
			/* Synchronize to vertical retrace */
#ifndef HAVE_SDL
			fprintf(stderr,"SDL must be present to enable vertical sync.\n");
#else
			refresh = sdl_RefreshRate();
			if (!refresh) fprintf(stderr,"Refresh rate cannot be determined, using %d FPS.\n",fps);
			else {
				fps = refresh;
				fprintf(stderr,"Running Arnold with screen refresh rate of %d FPS.\n",fps);
			}
#endif
		}
		/* Set refresh rate */
		fprintf(stderr,"Running emulation with %d fps\n",fps);
#ifdef HAVE_SDL
		sdl_SetFPS(fps);
#endif

		/* Set also fps in Arnold */
		CPC_SetAudioActive(TRUE,fps);
		printf("%s", Messages[77]);

#ifdef HAVE_SDL
		if (doubled) {
#ifdef HAVE_GTK
			gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON (btn_double), TRUE );
#else
			sdl_SetDoubled(doubled);
#endif
		}
		if (fullscreen) {
			toggleFullscreenLater = TRUE;
		}
		if (kbd != -1) sdl_InitialiseKeyboardMapping(kbd);
#endif
		printf("%s", Messages[76]);

		/* Enter GTK+ event loop when GTK+ is compiled in. Use own main loop
		 * otherwise. */
#ifdef HAVE_GTK
	  /*while (1)
	  { CPCEmulation_Run();
	    SDL_Delay(20); }*/
		/* gtkui_init must be called after CPC_SetAudioActive */
		gtkui_init(argc, argv);
		gtkui_run();
#else
	    CPCEmulation_Run();
#endif

	//printf("aaaa");
	    //CPCEmulation_Finish();
	//printf("bbbb");
		Host_FreeDriveLEDIndicator();
}
Example #12
0
int main(int argc, char** argv)
{
	struct fpga_model model;
	int bit_header, bit_regs, bit_crc, fp_header, pull_model, file_arg;
	int verbose, flags, rc = -1;
	struct fpga_config config;

	// parameters
	if (argc < 2) help_exit(argc, argv);
	verbose = 0;
   	bit_header = 0;
	bit_regs = 0;
	bit_crc = 0;
	pull_model = 1;
	fp_header = 1;
	file_arg = 1;
	while (file_arg < argc && !strncmp(argv[file_arg], "--", 2)) {
		if (!strcmp(argv[file_arg], "--help"))
			help_exit(argc, argv);

		if (!strcmp(argv[file_arg], "--verbose"))
			verbose = 1;
		else if (!strcmp(argv[file_arg], "--bit-header"))
			bit_header = 1;
		else if (!strcmp(argv[file_arg], "--bit-regs"))
			bit_regs = 1;
		else if (!strcmp(argv[file_arg], "--bit-crc"))
			bit_crc = 1;
		else if (!strcmp(argv[file_arg], "--no-model"))
			pull_model = 0;
		else if (!strcmp(argv[file_arg], "--no-fp-header"))
			fp_header = 0;
		else break;
		file_arg++;
	}

	// read binary configuration file
	{
		FILE* fbits = fopen(argv[file_arg], "r");
		if (!fbits) {
			fprintf(stderr, "Error opening %s.\n", argv[file_arg]);
			goto fail;
		}
		rc = read_bitfile(&config, fbits, verbose);
		fclose(fbits);
		if (rc) FAIL(rc);
	}

	// build model
	if (config.idcode_reg == -1) FAIL(EINVAL);
	// todo: scanf package from header string, better default for part
	//   1. cmd line
	//   2. header string
	//   3. part-default
	if ((rc = fpga_build_model(&model, config.reg[config.idcode_reg].int_v,
		cmdline_package(argc, argv)))) FAIL(rc);

	// fill model from binary configuration
	if (pull_model)
		if ((rc = extract_model(&model, &config.bits))) FAIL(rc);

	// dump model
	flags = FP_DEFAULT;
	if (!fp_header) flags |= FP_NO_HEADER;
	if ((rc = write_floorplan(stdout, &model, flags))) FAIL(rc);

	// dump what doesn't fit into the model
   	flags = DUMP_BITS;
	if (bit_header) flags |= DUMP_HEADER_STR;
	if (bit_regs) flags |= DUMP_REGS;
	if (bit_crc) flags |= DUMP_CRC;
	if ((rc = dump_config(&config, flags))) FAIL(rc);
	return EXIT_SUCCESS;
fail:
	return rc;
}