コード例 #1
0
/* Helper function called via semanage_fcontext_iterate() */
static int fcontext_matches(const semanage_fcontext_t *fcontext, void *varg)
{
	const char *oexpr = semanage_fcontext_get_expr(fcontext);
	fc_match_handle_t *handp = varg;
	struct Ustr *expr;
	regex_t re;
	int type, retval = -1;

	/* Only match ALL or DIR */
	type = semanage_fcontext_get_type(fcontext);
	if (type != SEMANAGE_FCONTEXT_ALL && type != SEMANAGE_FCONTEXT_ALL)
		return 0;

	/* Convert oexpr into a Ustr and anchor it at the beginning */
	expr = ustr_dup_cstr("^");
	if (expr == USTR_NULL)
		goto done;
	if (!ustr_add_cstr(&expr, oexpr))
		goto done;

	/* Strip off trailing ".+" or ".*" */
	if (ustr_cmp_suffix_cstr_eq(expr, ".+") ||
	    ustr_cmp_suffix_cstr_eq(expr, ".*")) {
		if (!ustr_del(&expr, 2))
			goto done;
	}

	/* Strip off trailing "(/.*)?" */
	if (ustr_cmp_suffix_cstr_eq(expr, "(/.*)?")) {
		if (!ustr_del(&expr, 6))
			goto done;
	}

	if (ustr_cmp_suffix_cstr_eq(expr, "/")) {
		if (!ustr_del(&expr, 1))
			goto done;
	}

	/* Append pattern to eat up trailing slashes */
	if (!ustr_add_cstr(&expr, "/*$"))
		goto done;

	/* Check dir against expr */
	if (regcomp(&re, ustr_cstr(expr), REG_EXTENDED) != 0)
		goto done;
	if (regexec(&re, handp->dir, 0, NULL, 0) == 0)
		handp->matched = 1;
	regfree(&re);

	retval = 0;

done:
	ustr_free(expr);

	return retval;
}
コード例 #2
0
ファイル: lexicon.c プロジェクト: AmkG/galcry
void
lexicon_init (void)
{
  Ustr *lexicons_path;
  FILE *lexicons;
  Ustr *line;
  AUstr words;

  lexicons_path = ustr_dup (cmdline_pkgdatadir ());
  if (!lexicons_path)
    abort ();
  if (!ustr_add_cstr (&lexicons_path, "/lexicons"))
    abort ();
  line = ustr_dup_empty ();
  if (!line)
    abort ();
  austr_init (&words);

  lexicons = fopen (ustr_cstr (lexicons_path), "r");
  if (!lexicons)
    abort ();

  lexicon_list = 0;
  while (errno = 0, ustr_sc_del (&line), ustr_io_getline (&line, lexicons))
    {
      unsigned int i;
      wordsplit (&words, line);
      for (i = 0; i < austr_length (&words); ++i)
	{
	  LexiconList *nlist;
	  nlist = malloc (sizeof (LexiconList));
	  if (!nlist)
	    abort ();
	  nlist->name = ustr_dup (austr_i (&words, i));
	  nlist->lexicon = 0;
	  nlist->next = lexicon_list;
	  lexicon_list = nlist;
	}
    }
  if (errno != 0)
    {
      perror ("galcry-backend, lexicons");
      fclose (lexicons);
      abort ();
    }

  fclose (lexicons);
  austr_deinit (&words);
  ustr_sc_free (&lexicons_path);
}
コード例 #3
0
ファイル: lexicon.c プロジェクト: AmkG/galcry
void
lexicon_generate (Ustr **s, Ustr const *language)
{
  LexiconList *sp;
  char ch[2];

  ustr_sc_del (s);
  ch[0] = 0;
  ch[1] = 0;

  /* Linear search.  Simple and we don't expect dozens of lexicons
     anyway.  */
  sp = lexicon_list;
  while (sp && !ustr_cmp_eq (language, sp->name))
    sp = sp->next;
  if (!sp)
    return;

  /* Load lexicon if not existent yet.  */
  if (!sp->lexicon)
    {
      Ustr *path;

      path = ustr_dup (cmdline_pkgdatadir ());
      if (!path)
	abort ();
      if (!ustr_add_cstr (&path, "/lexicon/"))
	abort ();
      if (!ustr_add (&path, sp->name))
	abort ();

      sp->lexicon = learn (path);

      ustr_sc_free (&path);
    }

  /* Generate.  */
  {
    unsigned int c0, c1, c2;
    unsigned long int sel;
    unsigned long int lim;
    unsigned int i;
    Lexicon *l;

    c0 = 0;
    l = sp->lexicon;

    /* Generate first char.  */
    for (i = 1, lim = 0; i < 27; ++i)
      lim += l->histogram2[0][i];
    /* Random select.  */
    sel = random () % lim;
    /* Find histogram area that random selection got in.  */
    for (c1 = 1; sel >= l->histogram2[0][c1]; ++c1)
      sel -= l->histogram2[0][c1];
    /* Add random selection.  */
    ch[0] = 96 + c1;
    if (!ustr_add_cstr (s, ch))
      abort ();

    /* Generate subsequent chars.  */
    do
      {
	/* Random select.  */
	sel = random () % l->histogram2[c0][c1];
	for (c2 = 0; sel >=l->histogram3[c0][c1][c2]; ++c2)
	  sel -= l->histogram3[c0][c1][c2];
	if (c2 != 0)
	  {
	    ch[0] = 96 + c2;
	    if (!ustr_add_cstr (s, ch))
	      abort ();
	  }
	c0 = c1;
	c1 = c2;
      }
    while (c2 != 0);
  }
}
コード例 #4
0
ファイル: ctst_4_grow.c プロジェクト: Distrotech/ustr
int tst(void)
{
  struct Ustr *s3 = NULL;
  struct Ustr *s4 = NULL;
  int num = -1;
  
  assert(!USTR_CONF_USE_DYNAMIC_CONF ||
         ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 1));
  /* move to the new "default" conf */
  ustr_sc_free2(&s2, ustr_dup_buf(ustr_cstr(s2), ustr_len(s2)));

  s3 = ustr_dup_cstr("s3 abcd s2");
  s4 = ustr_dup_empty(); /* always allocs */
  
  ASSERT(s2);
  ASSERT(s3);
  ASSERT(s4);
  ASSERT(ustr_cmp_eq(s1, s4));
  
  ASSERT(ustr_len(s1)  ==  0);
  ASSERT(ustr_len(s2)  ==  2);
  ASSERT(ustr_len(s3)  == 10);
  ASSERT(ustr_len(s4)  ==  0);
  
  ASSERT(ustr_size(s1) ==  0);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==  2);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 12);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s4) ==  0);

  ASSERT(ustr_srch_fwd(s3, 0, s2) == 9);
  ASSERT(ustr_srch_rev(s3, 0, s2) == 9);
  ASSERT(ustr_srch_fwd(s2, 0, s3) == 0);
  ASSERT(ustr_srch_rev(s2, 0, s3) == 0);
  
  ASSERT(ustr_add_cstr(&s2, "x"));
  ASSERT(ustr_len(s2)  ==   3);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   4);
  ASSERT(ustr_add_cstr(&s2, "y"));
  ASSERT(ustr_len(s2)  ==   4);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   4);
  ASSERT(ustr_add_cstr(&s2, "z"));
  ASSERT(ustr_len(s2)  ==   5);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==   8);
  ASSERT(ustr_add_rep_chr(&s2, '-', 11));
  ASSERT(ustr_len(s2)  ==  16);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s2) ==  20);
  ASSERT(ustr_cmp_cstr_eq(s2,   "s2xyz-----------"));
  ASSERT(!strcmp(ustr_cstr(s2), "s2xyz-----------"));
  
  ASSERT(ustr_srch_fwd(s3, 0, s2) == 0);
  ASSERT(ustr_srch_rev(s3, 0, s2) == 0);
  ASSERT(ustr_srch_fwd(s2, 0, s3) == 0);
  ASSERT(ustr_srch_rev(s2, 0, s3) == 0);

  /* NOTE: Using system *printf, so can't use %zu as Solaris is retarded */
  ASSERT(ustr_add_fmt(&s1, "%s abcd %13.100s %d %c %lu%n",
                      "------abc------", "", 42, 0,
                      (unsigned long)ustr_len(s3), &num) != -1);
  ASSERT((unsigned)num == ustr_len(s1));
  ASSERT(42  == num);
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(44  == ustr_size(s1));

  ASSERT(ustr_add_fmt(&s4, "%2$d%1$u", 2, 4));

  ASSERT(ustr_srch_cstr_fwd(s1, 0, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "abcd") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 0, "abc")  ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "abc")  == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 0, "10")  == 41);
  ASSERT(ustr_srch_cstr_rev(s1, 0, "10")  == 41);
  ASSERT(ustr_srch_chr_fwd(s1, 0, 0)  == 39);
  ASSERT(ustr_srch_chr_rev(s1, 0, 0)  == 39);
  ASSERT(ustr_srch_fwd(s1, 0, s4) == 36);

  ASSERT(ustr_srch_cstr_fwd(s1,  1, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1,  1, "abcd") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 10, "abcd") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, 10, "abcd") == 17);
  
  ASSERT(ustr_srch_cstr_fwd(s1,  0, " ") == 16);
  ASSERT(ustr_srch_cstr_fwd(s1, 10, " ") == 16);
  ASSERT(ustr_srch_cstr_fwd(s1, 16, " ") == 21);
  ASSERT(ustr_srch_cstr_fwd(s1, 20, " ") == 21);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 1) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "  ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 2) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "   ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 3) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "    ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 4) == 22);
  ASSERT(ustr_srch_cstr_fwd(s1, 21, "     ") == 22);
  ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 5) == 22);
  
  ASSERT(ustr_srch_cstr_fwd(s1,  0, "a") ==  7);
  ASSERT(ustr_srch_cstr_fwd(s1,  6, "a") ==  7);
  ASSERT(ustr_srch_cstr_fwd(s1,  7, "a") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 16, "a") == 17);
  ASSERT(ustr_srch_cstr_fwd(s1, 17, "a") ==  0);
  
  ASSERT(ustr_srch_cstr_rev(s1, 0, "a") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 16, "a") ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  7, "a") ==  7);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  6, "a") ==  0);
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) -  1, "a") ==  0);

  /* srch_case */
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, " ") == 16);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 10, " ") == 16);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, " ") == 21);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 20, " ") == 21);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 1) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "  ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 2) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "   ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 3) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "    ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 4) == 22);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 21, "     ") == 22);
  ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 5) == 22);
  
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  6, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  7, "a") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "a") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  0, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  6, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_fwd(s1,  7, "A") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "A") == 17);
  ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "A") ==  0);
  
  ASSERT(ustr_srch_case_cstr_rev(s1, 0, "a") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  7, "a") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  6, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  1, "a") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, 0, "A") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "A") == 17);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  7, "A") ==  7);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  6, "A") ==  0);
  ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) -  1, "A") ==  0);

  if (!USTR_DEBUG)
  ASSERT(ustr_srch_cstr_fwd(s1, ustr_len(s1), "a") ==  0);
  if (!USTR_DEBUG)
  ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1), "a") ==  0);

  /*  puts(ustr_cstr(s4)); */
  
  ustr_sc_free(&s3);

  ASSERT((s3 = ustr_dup(s4)));
  ASSERT(ustr_add_fmt(&s4, "x"));

  ustr_sc_free(&s4);
  ustr_sc_free(&s3);

  /*
  ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 123456")));
  ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 1234\0xxx"))); */
  ASSERT( ustr_assert_valid(USTR1(\x000F, "123456789 12345")));

  /*    ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 12345\0xxx")));  */
  
  s3 = ustr_dupx(0, 2, 0, 1, USTR1(\x000F, "123456789 12345"));

  ASSERT(ustr_cmp_cstr_eq(s3, "123456789 12345"));
  ASSERT(ustr_cmp_eq(s3, USTR1(\x000F, "123456789 12345")));
  ASSERT(!ustr_ro(s3));
  if (!USTR_CONF_USE_EOS_MARK)
  ASSERT(ustr_size(s3) == 19);
  
  ustr_sc_free(&s3);
  
  return (EXIT_SUCCESS);
}