Ejemplo n.º 1
0
int printf_main(int argc, char **argv)
{
	char *format;
	int args_used;

	if (argc <= 1 || argv[1][0] == '-') {
		bb_show_usage();
	}

	format = argv[1];
	argc -= 2;
	argv += 2;

	do {
		args_used = print_formatted(format, argc, argv);
		argc -= args_used;
		argv += args_used;
	} while (args_used > 0 && argc > 0);

/*	if (argc > 0)
		fprintf(stderr, "excess args ignored");
*/

	return EXIT_SUCCESS;
}
void generate_sentence(char * element) {
	list_nodeT * curr_node, * production = NULL;

	production = get_non_terminal_random_production(element);
	
	if(production == NULL ) {
		printf("ERROR!");
		fprintf(stderr, "ERROR: No production found for the '%s' non terminal element.\nQuiting...\n", element);
		exit(-1);
	}

	curr_node = walk_list(production);

	while ( production != curr_node ) {
		char * token = curr_node->content;

		if (is_non_terminal(token)) {
			generate_sentence(token);
		}
		else {
			print_formatted(token);
		}
		curr_node = walk_list(curr_node);
	}
	
	return;
}
Ejemplo n.º 3
0
int printf_main(int argc UNUSED_PARAM, char **argv)
{
	char *format;
	char **argv2;

	/* We must check that stdout is not closed.
	 * The reason for this is highly non-obvious.
	 * printf_main is used from shell.
	 * Shell must correctly handle 'printf "%s" foo'
	 * if stdout is closed. With stdio, output gets shoveled into
	 * stdout buffer, and even fflush cannot clear it out. It seems that
	 * even if libc receives EBADF on write attempts, it feels determined
	 * to output data no matter what. So it will try later,
	 * and possibly will clobber future output. Not good. */
// TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
	if (fcntl(1, F_GETFL) == -1)
		return 1; /* match coreutils 6.10 (sans error msg to stderr) */
	//if (dup2(1, 1) != 1) - old way
	//	return 1;

	/* bash builtin errors out on "printf '-%s-\n' foo",
	 * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
	 * We will mimic coreutils. */
	if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
		argv++;
	if (!argv[1]) {
		if (ENABLE_ASH_BUILTIN_PRINTF
		 && applet_name[0] != 'p'
		) {
			bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
			return 2; /* bash compat */
		}
		bb_show_usage();
	}

	format = argv[1];
	argv2 = argv + 2;

	do {
		argv = argv2;
		argv2 = print_formatted(format, argv);
	} while (argv2 > argv && *argv2);

	/* coreutils compat (bash doesn't do this):
	if (*argv)
		fprintf(stderr, "excess args ignored");
	*/

	return (argv2 < argv); /* if true, print_formatted errored out */
}
Ejemplo n.º 4
0
static void
doit(struct libusb20_device *dev)
{
  int rv;

  if (do_request)
    printf("doit(): bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
	   setup.bmRequestType,
	   setup.bRequest,
	   setup.wValue,
	   setup.wIndex,
	   setup.wLength);

  /*
   * Open the device, allocating memory for two possible (bulk or
   * interrupt) transfers.
   *
   * If only control transfers are intended (via
   * libusb20_dev_request_sync()), transfer_max can be given as 0.
   */
  if ((rv = libusb20_dev_open(dev, 1)) != 0)
    {
      fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * If the device has more than one configuration, select the desired
   * one here.
   */
  if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
    {
      fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
      return;
    }

  uint8_t *data = 0;
  uint16_t actlen;

  if ((setup.bmRequestType & 0x80) != 0)
    {
      /* this is an IN request, allocate a buffer */
      data = malloc(setup.wLength);
      if (data == 0)
	{
	  fprintf(stderr,
		  "Out of memory allocating %u bytes of reply buffer\n",
		  setup.wLength);
	  return;
	}
    }
  else
    data = out_buf;

  if (do_request)
    {
      if ((rv = libusb20_dev_request_sync(dev, &setup, data,
					  &actlen,
					  TIMEOUT,
					  0 /* flags */)) != 0)
	{
	  fprintf(stderr,
		  "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
	}
      printf("sent %d bytes\n", actlen);
      if ((setup.bmRequestType & 0x80) != 0)
	{
	  print_formatted(data, (uint32_t)setup.wLength);
	  free(data);
	}
    }

  if (intr_ep != 0)
    {
      /*
       * One transfer has been requested in libusb20_dev_open() above;
       * obtain the corresponding transfer struct pointer.
       */
      struct libusb20_transfer *xfr_intr = libusb20_tr_get_pointer(dev, 0);

      if (xfr_intr == NULL)
	{
	  fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
	  return;
	}

      /*
       * Open the interrupt transfer.
       */
      if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
	{
	  fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
	  return;
	}

      uint8_t in_buf[BUFLEN];
      uint32_t rlen;

      if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
	  != 0)
	{
	  fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
	}
      printf("received %d bytes\n", rlen);
      if (rlen > 0)
	print_formatted(in_buf, rlen);

      libusb20_tr_close(xfr_intr);
    }

  libusb20_dev_close(dev);
}
Ejemplo n.º 5
0
static void
doit(struct libusb20_device *dev)
{
  int rv;

  /*
   * Open the device, allocating memory for two possible (bulk or
   * interrupt) transfers.
   *
   * If only control transfers are intended (via
   * libusb20_dev_request_sync()), transfer_max can be given as 0.
   */
  if ((rv = libusb20_dev_open(dev, 2)) != 0)
    {
      fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * If the device has more than one configuration, select the desired
   * one here.
   */
  if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
    {
      fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * Two transfers have been requested in libusb20_dev_open() above;
   * obtain the corresponding transfer struct pointers.
   */
  struct libusb20_transfer *xfr_out = libusb20_tr_get_pointer(dev, 0);
  struct libusb20_transfer *xfr_in = libusb20_tr_get_pointer(dev, 1);

  if (xfr_in == NULL || xfr_out == NULL)
    {
      fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
      return;
    }

  /*
   * Open both transfers, the "out" one for the write endpoint, the
   * "in" one for the read endpoint (ep | 0x80).
   */
  if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
    {
      fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
      return;
    }
  if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
    {
      fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
      return;
    }

  uint8_t in_buf[BUFLEN];
  uint32_t rlen;

  if (out_len > 0)
    {
      if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
	  != 0)
	{
	  fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", libusb20_strerror(rv));
	}
      printf("sent %d bytes\n", rlen);
    }

  if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
      != 0)
    {
      fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
    }
      printf("received %d bytes\n", rlen);
      if (rlen > 0)
	print_formatted(in_buf, rlen);

  libusb20_tr_close(xfr_out);
  libusb20_tr_close(xfr_in);

  libusb20_dev_close(dev);
}
Ejemplo n.º 6
0
Archivo: cli.c Proyecto: mhfan/stoken
int main(int argc, char **argv)
{
	char *cmd = parse_cmdline(argc, argv, NOT_GUI);
	int rc;
	char buf[BUFLEN];
	struct securid_token *t;

	rc = common_init(cmd);
	if (rc != ERR_NONE)
		die("can't initialize: %s\n", stoken_errstr[rc]);

	t = current_token;
	if (!t)
		die("error: no token present.  Use 'stoken import' to add one.\n");

	terminal_init();

	if (!strcmp(cmd, "tokencode")) {
		int days_left = securid_check_exp(t, adjusted_time());

		if (days_left < 0 && !opt_force)
			die("error: token has expired; use --force to override\n");

		unlock_token(t, 1, NULL);
		securid_compute_tokencode(t, adjusted_time(), buf);
		puts(buf);

		if (days_left < 14 && !opt_force)
			warn("warning: token expires in %d day%s\n", days_left,
				days_left == 1 ? "" : "s");
	} else if (!strcmp(cmd, "import")) {
		char *pass;

		unlock_token(t, 0, &pass);
		if (!opt_keep_password) {
			pass = xmalloc(BUFLEN);
			request_new_pass(pass);
		}

		t->is_smartphone = 0;
		securid_encode_token(t, pass, opt_new_devid, buf);
		rc = write_token_and_pin(buf, NULL, pass);
		if (rc != ERR_NONE)
			die("rcfile: error writing new token: %s\n",
				stoken_errstr[rc]);
	} else if (!strcmp(cmd, "export")) {
		char *pass;

		unlock_token(t, 0, &pass);
		if (opt_new_password)
			pass = opt_new_password;
		else if (!opt_keep_password)
			pass = NULL;

		t->is_smartphone = opt_iphone || opt_android;
		securid_encode_token(t, pass, opt_new_devid, buf);
		print_formatted(buf);
	} else if (!strcmp(cmd, "show")) {
		unlock_token(t, 0, NULL);
		securid_token_info(t, &print_token_info_line);
	} else if (!strcmp(cmd, "setpin")) {
		char *pass = NULL, pin[BUFLEN], confirm_pin[BUFLEN];
		int len;

		if (opt_file || opt_token)
			die("error: setpin only operates on the rcfile token\n");

		unlock_token(t, 0, &pass);

		if (opt_new_pin) {
			if (securid_pin_format_ok(opt_new_pin) != ERR_NONE)
				die("error: invalid --new-pin format\n");
			strncpy(pin, opt_new_pin, BUFLEN);
			len = strlen(pin);
		} else {
			prompt("Enter new PIN: ");
			len = read_user_input(pin, BUFLEN, 1);
			if (len > 0 && securid_pin_format_ok(pin) != ERR_NONE)
				die("error: PIN must be 4-8 digits\n");

			prompt("Confirm new PIN: ");
			read_user_input(confirm_pin, BUFLEN, 1);
			if (strcmp(pin, confirm_pin) != 0)
				die("error: PINs do not match\n");
		}

		securid_encode_token(t, pass, NULL, buf);
		rc = write_token_and_pin(buf, len ? pin : NULL, pass);
		free(pass);

		if (rc != ERR_NONE)
			die("error: can't set PIN: %s\n", stoken_errstr[rc]);
	} else if (!strcmp(cmd, "setpass")) {
		char pass[BUFLEN];

		unlock_token(t, 0, NULL);
		request_new_pass(pass);
		securid_encode_token(t, pass, NULL, buf);

		/* just print to stdout if it didn't come from the rcfile */
		if (opt_file || opt_token)
			print_formatted(buf);
		else {
			rc = write_token_and_pin(buf,
						 strlen(t->pin) ? t->pin : NULL,
						 strlen(pass) ? pass : NULL);
			if (rc != ERR_NONE)
				die("error: can't set password: %s\n",
				    stoken_errstr[rc]);
		}
	} else
		die("error: invalid command '%s'\n", cmd);

	return 0;
}
Ejemplo n.º 7
0
 void parameters::update (parameter_defaults const & pdef, ull_t flag ){

  std::vector<std::vector<std::string>> missing;
  std::vector<std::vector<std::string>> wrong_t;
  std::vector<std::vector<std::string>> no_deft;

  std::vector<std::string> desc{"key:", "description:"};
  std::vector<std::string> tdesc{"key:", "expected type:", "actual type:"};

  if ( (flag & reject_key_without_default) ) { // check that no extra parameters are present
    for (auto const & pvp : *this) {
      auto key = pvp.first; 
      if (!pdef.has_key(key)){
      no_deft.push_back({key});    
      } 
    }
  }

  for (auto const & pvp : pdef) {
   auto key = pvp.first;

   // check whether required parameters are present
   if (pdef.is_required(key) && (!this->has_key(key))){
    // delay exception until all parameters have been checked
    if (!missing.size()) missing.push_back(desc);
    missing.push_back({key, pdef.doc(key)});    
   }
   if (this->has_key(key)) { // check whether the type is correct 
    if (! have_same_type(pvp.second, (*this)[key])){
     // delay exception until all parameters have been checked
     if (!wrong_t.size()) wrong_t.push_back(tdesc);
     wrong_t.push_back({key, pvp.second.type_name(), (*this)[key].type_name()});
    }
   }
   else { 
    (*this)[key] = pvp.second; // insert the default
   }

  }
  // raise a runtime exception if errors occured
  if(missing.size()) TRIQS_RUNTIME_ERROR << "update with defaults : the following parameters are required but absent: \n"<< print_formatted(missing);
  if(wrong_t.size()) TRIQS_RUNTIME_ERROR << "update with defaults : the following parameters have incorrect type: \n"<< print_formatted(wrong_t);
  if(no_deft.size()) TRIQS_RUNTIME_ERROR << "update with defaults : the following parameters are absent from the defaults and no_parameter_without_default is ON: \n"<< print_formatted(no_deft);

 }