Exemple #1
0
static char * test2() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  fltv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	string_array_t* parray  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag1);
	ap_define_false_flag(pstate,       "-f",   &bflag2);
	ap_define_int_value_flag(pstate,   "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,   "-200", 200, &intv2);
	ap_define_int_flag(pstate,         "-i",   &intv3);
	ap_define_float_flag(pstate,      "-d",   &fltv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);
	ap_define_string_array_flag(pstate, "-A",   &parray);

	char* argv[] = {
		"test-verb",
		"-t",
		"-f",
		"-100",
		"-200",
		"-i", "555",
		"-d", "4.25",
		"-s", "hello",
		"-S", mlr_strdup_or_die("a,b,c,d,e"),
		"-A", mlr_strdup_or_die("x,y,z"),
		"do", "re", "mi",
		NULL
	};
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == TRUE);
	mu_assert_lf(bflag2 == FALSE);
	mu_assert_lf(intv1 == 100);
	mu_assert_lf(intv2 == 200);
	mu_assert_lf(intv3 == 555);
	mu_assert_lf(fltv == 4.25);
	mu_assert_lf(string != NULL);
	mu_assert_lf(streq(string, "hello"));
	mu_assert_lf(plist != NULL);
	mu_assert_lf(slls_equals(plist, slls_from_line(mlr_strdup_or_die("a,b,c,d,e"), ',', FALSE)));
	mu_assert_lf(parray != NULL);
	mu_assert_lf(parray->length == 3);
	mu_assert_lf(streq(parray->strings[0], "x"));
	mu_assert_lf(streq(parray->strings[1], "y"));
	mu_assert_lf(streq(parray->strings[2], "z"));
	mu_assert_lf(argi == 15);

	ap_free(pstate);
	return 0;
}
Exemple #2
0
main (argc, argv)
{
    AP_ptr ap_fp;
    int	getach();

    mmdf_init ("PARSE");
    logptr -> ll_level = LLOGFTR;
    if (argc == 2)
	debug++;

    for (;;)
    {
	printf ("Parse: ");

	ap_fp = ap_fullparse (getach);
	if (ap_fp == (AP_ptr) NOTOK) {
	    printf ("\nNOTOK: on %s\n", namtab[ap_llex]);
	} else if (ap_fp == (AP_ptr) DONE) {
	    printf ("\nNOTOK: on %s\n", namtab[ap_llex]);
	} else if (ap_fp == OK) {
	    printf ("\nOK?: on %s\n", namtab[ap_llex]);
	} else {
	    printf ("\n\nAccept\n");
	    pretty (ap_fp);
	    printf ("\n");
	    while (ap_sqdelete (ap_fp, (AP_ptr)0) != 0);
	    ap_free (ap_fp);  /* delete full string         */
	    ap_fp = (AP_ptr) 0;
	    continue;
	}
    	break;
    }
    exit (0);
}
int main(int argc, char *argv[])
{
	/* update progname from argv[0] */
	set_progname(argv[0]);

	const struct ap_Option options[] = {
		{'h', "help", ap_no, 0, 0},
		{'v', "version", ap_no, 0, 0},
		{0, 0, ap_no, 0, 0}
	};

	if (!ap_init(&parser, argc, (const char* const*) argv, options, 0))
		critx("could not allocate memory for option parser");
	if (ap_error(&parser)) {
		errx("%s", ap_error(&parser));
		usage(EXIT_FAILURE);
	}

	/* parse command line */
	for (int argind = 0; argind < ap_arguments(&parser); argind++) {
		const int code = ap_code(&parser, argind);

		switch (code) {
		case 0:
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		case 'v':
			fprintf(stderr, "%s %s\n%s\n%s\n\n%s\n", progname,
				FLOWGRIND_VERSION, FLOWGRIND_COPYRIGHT,
				FLOWGRIND_COPYING, FLOWGRIND_AUTHORS);
			exit(EXIT_SUCCESS);
			break;
		default:
			errx("uncaught option: %s", ap_argument(&parser, argind));
			usage(EXIT_FAILURE);
			break;
		}
	}

	if (!ap_arguments(&parser)) {
		errx("no address given");
		usage(EXIT_FAILURE);
	}

	xmlrpc_env rpc_env;
	xmlrpc_env_init(&rpc_env);
	xmlrpc_client_setup_global_const(&rpc_env);

	for (int argind = 0; argind < ap_arguments(&parser); argind++)
		/* if non-option, it is an address */
		if (!ap_code(&parser, argind))
			stop_flows(ap_argument(&parser, argind));

	xmlrpc_env_clean(&rpc_env);
	xmlrpc_client_teardown_global_const();
	ap_free(&parser);
}
Exemple #4
0
int main(int argc, char** argv) {
	int     bflag  = TRUE;
	int     intv   = 0;
	double  dblv   = 0.0;
	char*   string = NULL;
	slls_t* plist  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag);
	ap_define_false_flag(pstate,       "-f",   &bflag);
	ap_define_int_value_flag(pstate,   "-100", 100,      &intv);
	ap_define_int_value_flag(pstate,   "-200", 200,      &intv);
	ap_define_int_flag(pstate,         "-i",   &intv);
	ap_define_double_flag(pstate,      "-d",   &dblv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);

	char* verb = "stub";
	int argi = 1;
	if (ap_parse(pstate, verb, &argi, argc, argv) == TRUE) {
		printf("OK\n");
	} else {
		printf("Usage!\n");
	}

	printf("argi  is %d\n", argi);
	printf("argc  is %d\n", argc);
	printf("rest  is");
	for (; argi < argc; argi++)
		printf(" %s", argv[argi]);
	printf("\n");
	printf("bflag is %d\n", bflag);
	printf("intv  is %d\n", intv);
	printf("dblv  is %g\n", dblv);

	if (string == NULL) {
		printf("string  is null\n");
	} else {
		printf("string  is \"%s\"\n", string);
	}

	if (plist == NULL) {
		printf("list  is null\n");
	} else {
		char* out = slls_join(plist, ',');
		printf("list  is %s\n", out);
		free(out);
	}

	ap_free(pstate);

	return 0;
}
Exemple #5
0
static char * test2() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  dblv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag1);
	ap_define_false_flag(pstate,       "-f",   &bflag2);
	ap_define_int_value_flag(pstate,   "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,   "-200", 200, &intv2);
	ap_define_int_flag(pstate,         "-i",   &intv3);
	ap_define_double_flag(pstate,      "-d",   &dblv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);

	char* argv[] = {
		"test-verb",
		"-t",
		"-f",
		"-100",
		"-200",
		"-i", "555",
		"-d", "4.25",
		"-s", "hello",
		"-S", strdup("a,b,c,d,e"),
		"do", "re", "mi",
		NULL
	};
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == TRUE);
	mu_assert_lf(bflag2 == FALSE);
	mu_assert_lf(intv1 == 100);
	mu_assert_lf(intv2 == 200);
	mu_assert_lf(intv3 == 555);
	mu_assert_lf(dblv == 4.25);
	mu_assert_lf(string != NULL);
	mu_assert_lf(streq(string, "hello"));
	mu_assert_lf(plist != NULL);
	mu_assert_lf(slls_equals(plist, slls_from_line(strdup("a,b,c,d,e"), ',', FALSE)));
	mu_assert_lf(argi == 13);

	ap_free(pstate);
	return 0;
}
Exemple #6
0
static char * test1() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  fltv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	string_array_t* parray  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,         "-t",   &bflag1);
	ap_define_false_flag(pstate,        "-f",   &bflag2);
	ap_define_int_value_flag(pstate,    "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,    "-200", 200, &intv2);
	ap_define_int_flag(pstate,          "-i",   &intv3);
	ap_define_float_flag(pstate,       "-d",   &fltv);
	ap_define_string_flag(pstate,       "-s",   &string);
	ap_define_string_list_flag(pstate,  "-S",   &plist);
	ap_define_string_array_flag(pstate, "-A",   &parray);

	char* argv[] = { "test-verb", NULL };
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == -4);
	mu_assert_lf(bflag2 == -5);
	mu_assert_lf(intv1 == -6);
	mu_assert_lf(intv2 == -7);
	mu_assert_lf(intv3 == -8);
	mu_assert_lf(fltv == -9.5);
	mu_assert_lf(string == NULL);
	mu_assert_lf(plist == NULL);
	mu_assert_lf(parray == NULL);
	mu_assert_lf(argi == 1);

	ap_free(pstate);
	return 0;
}
Exemple #7
0
Fichier : main.c Projet : coliv/ed
int main( const int argc, const char * const argv[] )
  {
  int argind;
  bool loose = false;
  const struct ap_Option options[] =
    {
    { 'G', "traditional",       ap_no  },
    { 'h', "help",              ap_no  },
    { 'l', "loose-exit-status", ap_no  },
    { 'p', "prompt",            ap_yes },
    { 'r', "restricted",        ap_no  },
    { 's', "quiet",             ap_no  },
    { 's', "silent",            ap_no  },
    { 'v', "verbose",           ap_no  },
    { 'V', "version",           ap_no  },
    {  0 ,  0,                  ap_no } };

  struct Arg_parser parser;

  if( !ap_init( &parser, argc, argv, options, 0 ) )
    { show_error( "Memory exhausted.", 0, false ); return 1; }
  if( ap_error( &parser ) )				/* bad option */
    { show_error( ap_error( &parser ), 0, true ); return 1; }
  invocation_name = argv[0];

  for( argind = 0; argind < ap_arguments( &parser ); ++argind )
    {
    const int code = ap_code( &parser, argind );
    const char * const arg = ap_argument( &parser, argind );
    if( !code ) break;					/* no more options */
    switch( code )
      {
      case 'G': traditional_ = true; break;	/* backward compatibility */
      case 'h': show_help(); return 0;
      case 'l': loose = true; break;
      case 'p': set_prompt( arg ); break;
      case 'r': restricted_ = true; break;
      case 's': scripted_ = true; break;
      case 'v': set_verbose(); break;
      case 'V': show_version(); return 0;
      default : show_error( "internal error: uncaught option.", 0, false );
                return 3;
      }
    } /* end process options */
  setlocale( LC_ALL, "" );
  if( !init_buffers() ) return 1;

  while( argind < ap_arguments( &parser ) )
    {
    const char * const arg = ap_argument( &parser, argind );
    if( !strcmp( arg, "-" ) ) { scripted_ = true; ++argind; continue; }
    if( may_access_filename( arg ) )
      {
      if( read_file( arg, 0 ) < 0 && is_regular_file( 0 ) )
        return 2;
      else if( arg[0] != '!' ) set_def_filename( arg );
      }
    else
      {
      fputs( "?\n", stderr );
      if( arg[0] ) set_error_msg( "Invalid filename" );
      if( is_regular_file( 0 ) ) return 2;
      }
    break;
    }
  ap_free( &parser );

  return main_loop( loose );
  }
Exemple #8
0
/*
 * apush_iocdata() -
 * Handle the M_IOCDATA messages associated with
 * the autopush feature.
 */
static void
apush_iocdata(
	queue_t *qp,	/* pointer to write queue */
	mblk_t *mp)	/* message pointer */
{
	int i, ret;
	struct copyresp *csp;
	struct strapush *sap;
	struct autopush *ap;
	struct saddev *sadp;
	uint_t size;

	csp = (struct copyresp *)mp->b_rptr;
	if (csp->cp_rval) {	/* if there was an error */
		freemsg(mp);
		return;
	}
	if (mp->b_cont)
		/* sap needed only if mp->b_cont is set */
		sap = (struct strapush *)mp->b_cont->b_rptr;
	switch (SAD_CMD(csp->cp_cmd)) {
	case SAD_CMD(SAD_SAP):
		switch ((long)csp->cp_private) {
		case GETSTRUCT:
			switch (sap->sap_cmd) {
			case SAP_ONE:
			case SAP_RANGE:
			case SAP_ALL:
				if ((sap->sap_npush == 0) ||
				    (sap->sap_npush > MAXAPUSH) ||
				    (sap->sap_npush > nstrpush)) {

					/* invalid number of modules to push */

					miocnak(qp, mp, 0, EINVAL);
					break;
				}
				if (ret = valid_major(sap->sap_major)) {
					miocnak(qp, mp, 0, ret);
					break;
				}
				if ((sap->sap_cmd == SAP_RANGE) &&
				    (sap->sap_lastminor <= sap->sap_minor)) {

					/* bad range */

					miocnak(qp, mp, 0, ERANGE);
					break;
				}

				/*
				 * Validate that the specified list of
				 * modules exist.
				 */
				for (i = 0; i < sap->sap_npush; i++) {
					sap->sap_list[i][FMNAMESZ] = '\0';
					if (fmodsw_find(sap->sap_list[i],
					    FMODSW_LOAD) == NULL) {
						miocnak(qp, mp, 0, EINVAL);
						return;
					}
				}

				mutex_enter(&sad_lock);
				if (ap_hfind(sap->sap_major, sap->sap_minor,
				    sap->sap_lastminor, sap->sap_cmd)) {
					mutex_exit(&sad_lock);

					/* already configured */

					miocnak(qp, mp, 0, EEXIST);
					break;
				}
				if ((ap = ap_alloc()) == NULL) {
					mutex_exit(&sad_lock);

					/* no autopush structures */

					miocnak(qp, mp, 0, ENOSR);
					break;
				}
				ap->ap_cnt++;
				ap->ap_common = sap->sap_common;
				if (SAD_VER(csp->cp_cmd) > 0)
					ap->ap_anchor = sap->sap_anchor;
				else
					ap->ap_anchor = 0;
				for (i = 0; i < ap->ap_npush; i++)
					(void) strcpy(ap->ap_list[i],
					    sap->sap_list[i]);
				ap_hadd(ap);
				mutex_exit(&sad_lock);
				miocack(qp, mp, 0, 0);
				break;

			case SAP_CLEAR:
				if (ret = valid_major(sap->sap_major)) {
					miocnak(qp, mp, 0, ret);
					break;
				}
				mutex_enter(&sad_lock);
				if ((ap = ap_hfind(sap->sap_major,
				    sap->sap_minor, sap->sap_lastminor,
				    sap->sap_cmd)) == NULL) {
					mutex_exit(&sad_lock);

					/* not configured */

					miocnak(qp, mp, 0, ENODEV);
					break;
				}
				if ((ap->ap_type == SAP_RANGE) &&
				    (sap->sap_minor != ap->ap_minor)) {
					mutex_exit(&sad_lock);

					/* starting minors do not match */

					miocnak(qp, mp, 0, ERANGE);
					break;
				}
				if ((ap->ap_type == SAP_ALL) &&
				    (sap->sap_minor != 0)) {
					mutex_exit(&sad_lock);

					/* SAP_ALL must have minor == 0 */

					miocnak(qp, mp, 0, EINVAL);
					break;
				}
				ap_hrmv(ap);
				if (--(ap->ap_cnt) <= 0)
					ap_free(ap);
				mutex_exit(&sad_lock);
				miocack(qp, mp, 0, 0);
				break;

			default:
				miocnak(qp, mp, 0, EINVAL);
				break;
			} /* switch (sap_cmd) */
			break;

		default:
			cmn_err(CE_WARN,
			    "apush_iocdata: cp_private bad in SAD_SAP: %p",
			    (void *)csp->cp_private);
			freemsg(mp);
			break;
		} /* switch (cp_private) */
		break;

	case SAD_CMD(SAD_GAP):
		switch ((long)csp->cp_private) {

		case GETSTRUCT: {
			if (ret = valid_major(sap->sap_major)) {
				miocnak(qp, mp, 0, ret);
				break;
			}
			mutex_enter(&sad_lock);
			if ((ap = ap_hfind(sap->sap_major, sap->sap_minor,
			    sap->sap_lastminor, SAP_ONE)) == NULL) {
				mutex_exit(&sad_lock);

				/* not configured */

				miocnak(qp, mp, 0, ENODEV);
				break;
			}

			sap->sap_common = ap->ap_common;
			if (SAD_VER(csp->cp_cmd) > 0)
				sap->sap_anchor = ap->ap_anchor;
			for (i = 0; i < ap->ap_npush; i++)
				(void) strcpy(sap->sap_list[i], ap->ap_list[i]);
			for (; i < MAXAPUSH; i++)
				bzero(sap->sap_list[i], FMNAMESZ + 1);
			mutex_exit(&sad_lock);

			if (SAD_VER(csp->cp_cmd) == 1)
				size = STRAPUSH_V1_LEN;
			else
				size = STRAPUSH_V0_LEN;

			sadp = (struct saddev *)qp->q_ptr;
			mcopyout(mp, (void *)GETRESULT, size, sadp->sa_addr,
			    NULL);
			qreply(qp, mp);
			break;
			}
		case GETRESULT:
			miocack(qp, mp, 0, 0);
			break;

		default:
			cmn_err(CE_WARN,
			    "apush_iocdata: cp_private bad case SAD_GAP: %p",
			    (void *)csp->cp_private);
			freemsg(mp);
			break;
		} /* switch (cp_private) */
		break;

	default:	/* can't happen */
		ASSERT(0);
		freemsg(mp);
		break;
	} /* switch (cp_cmd) */
}