Exemplo n.º 1
0
/* Locate the first single-byte character C in the character string STRING,
   and return a pointer to it.  Return NULL if C is not found in STRING.  */
char *
mbschr (const char *string, int c)
{
  if (MB_CUR_MAX > 1
      /* Optimization: We know that ASCII characters < 0x30 don't occur as
         part of multibyte characters longer than 1 byte.  Hence, if c < 0x30,
         the faster unibyte loop can be used.  */
      && (unsigned char) c >= 0x30)
    {
      mbui_iterator_t iter;

      for (mbui_init (iter, string);; mbui_advance (iter))
        {
          if (!mbui_avail (iter))
            goto notfound;
          if (mb_len (mbui_cur (iter)) == 1
              && (unsigned char) * mbui_cur_ptr (iter) == (unsigned char) c)
            break;
        }
      return (char *) mbui_cur_ptr (iter);
     notfound:
      return NULL;
    }
  else
    return strchr (string, c);
}
Exemplo n.º 2
0
/* Find the first occurrence in the character string STRING of any character
   in the character string ACCEPT.  Return the number of bytes from the
   beginning of the string to this occurrence, or to the end of the string
   if none exists.  */
size_t
mbscspn (const char *string, const char *accept)
{
  /* Optimize two cases.  */
  if (accept[0] == '\0')
    return strlen (string);
  if (accept[1] == '\0')
    {
      const char *ptr = mbschr (string, accept[0]);
      return (ptr != NULL ? ptr - string : strlen (string));
    }
  /* General case.  */
  if (MB_CUR_MAX > 1)
    {
      mbui_iterator_t iter;

      for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
        {
          if (mb_len (mbui_cur (iter)) == 1)
            {
              if (mbschr (accept, * mbui_cur_ptr (iter)))
                goto found;
            }
          else
            {
              mbui_iterator_t aiter;

              for (mbui_init (aiter, accept);
                   mbui_avail (aiter);
                   mbui_advance (aiter))
                if (mb_equal (mbui_cur (aiter), mbui_cur (iter)))
                  goto found;
            }
        }
     found:
      return mbui_cur_ptr (iter) - string;
    }
  else
    return strcspn (string, accept);
}
Exemplo n.º 3
0
/* Find the first occurrence in the character string STRING of any character
   in the character string ACCEPT.  Return the pointer to it, or NULL if none
   exists.  */
char *
mbspbrk (const char *string, const char *accept)
{
  /* Optimize two cases.  */
  if (accept[0] == '\0')
    return NULL;
  if (accept[1] == '\0')
    return mbschr (string, accept[0]);
  /* General case.  */
  if (MB_CUR_MAX > 1)
    {
      mbui_iterator_t iter;

      for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
        {
          if (mb_len (mbui_cur (iter)) == 1)
            {
              if (mbschr (accept, * mbui_cur_ptr (iter)))
                return (char *) mbui_cur_ptr (iter);
            }
          else
            {
              mbui_iterator_t aiter;

              for (mbui_init (aiter, accept);
                   mbui_avail (aiter);
                   mbui_advance (aiter))
                if (mb_equal (mbui_cur (aiter), mbui_cur (iter)))
                  return (char *) mbui_cur_ptr (iter);
            }
        }
      return NULL;
    }
  else
    return strpbrk (string, accept);
}
Exemplo n.º 4
0
int
main()
{
    mb_id_t       mbid;
    pid_t         pid;
    int           nb_loops;
    int           status;
    unsigned long mbcheck;
    const char    *tagmb = "test_mailbox";

    cm_openlog("test_mailbox", CM_TRACE_LEVEL_NOTICE);

    mbid = mb_create(tagmb, MB_LEN); 
    if (mbid == MB_INVALID_ID) {
        return (1);
    }

    pid = fork();
    if (pid == 0) {
        /*
         * Child - process callback 
         */
        mb_id_t child_mbid = mb_init(tagmb, act_callback);
        if (mb_len(mbid) != MB_LEN) {
            cm_trace(CM_TRACE_LEVEL_ERROR,
                     "mb_len returns bad result");
            exit(1);
        }
        while (1) {
           if (mb_hbt(child_mbid, NULL) != MB_OK) {
               cm_trace(CM_TRACE_LEVEL_ERROR,
                        "mb_hbt failed %s", strerror(errno));
               exit(1);
           }
           usleep(100000);
        }
    } else if (pid == -1) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "fork failed");
        exit(1);
    }

    /*
     * Father - check service state machine
     */
    if (wait_for(mbid, SRV_READY) < 0) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "failed to init");
        kill(pid, SIGKILL);
        return (1);
    }
    if (wait_for(mbid, SRV_RUNNING) < 0) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "failed to start");
        kill(pid, SIGKILL);
        return (1);
    }
    if (mb_read(mbid, &mbcheck, MB_OFFSET, sizeof(mbcheck)) == MB_ERROR) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "mb_read failed");
        kill(pid, SIGKILL);
        return (1);
    }
    if (mbcheck != MB_CHECK) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "mb_read returns wrong result");
        kill(pid, SIGKILL);
        return (1);
    }
    if (wait_for(mbid, SRV_READY) < 0) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "failed to stop");
        kill(pid, SIGKILL);
        return (1);
    }

    if (mb_setexpectedstate(mbid, SRV_DESTROY) == MB_ERROR) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "failed to destroy");
        kill(pid, SIGKILL);
        return (1);
    }
    nb_loops = 0;
    do {
        pid_t wpid = waitpid(pid, &status, WNOHANG);
        if (wpid == pid) {
            break;
        } else if (wpid < 0) {
            cm_trace(CM_TRACE_LEVEL_ERROR, "waitpid failed");
            kill(pid, SIGKILL);
            return (1);
        }
        usleep(100000);
    } while (nb_loops++ < 5);
 
    if (nb_loops >= 5) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "destroy failed");
        kill(pid, SIGKILL);
        return (1);
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        cm_trace(CM_TRACE_LEVEL_ERROR, "bad exit status");
        kill(pid, SIGKILL);
        return (1);
    }
        
    cm_trace(CM_TRACE_LEVEL_NOTICE, "mailbox test passed");
    cm_closelog();
    return (0);
}
Exemplo n.º 5
0
/* Tests whether STRING contains trim (SUB), starting and ending at word
   boundaries.
   Here, instead of implementing Unicode Standard Annex #29 for determining
   word boundaries, we assume that trim (SUB) starts and ends with words and
   only test whether the part before it ends with a non-word and the part
   after it starts with a non-word.  */
static bool
mbsstr_trimmed_wordbounded (const char *string, const char *sub)
{
  char *tsub = trim (sub);
  bool found = false;

  for (; *string != '\0';)
    {
      const char *tsub_in_string = mbsstr (string, tsub);
      if (tsub_in_string == NULL)
        break;
      else
        {
          if (MB_CUR_MAX > 1)
            {
              mbui_iterator_t string_iter;
              bool word_boundary_before;
              bool word_boundary_after;

              mbui_init (string_iter, string);
              word_boundary_before = true;
              if (mbui_cur_ptr (string_iter) < tsub_in_string)
                {
                  mbchar_t last_char_before_tsub;
                  do
                    {
                      if (!mbui_avail (string_iter))
                        abort ();
                      last_char_before_tsub = mbui_cur (string_iter);
                      mbui_advance (string_iter);
                    }
                  while (mbui_cur_ptr (string_iter) < tsub_in_string);
                  if (mb_isalnum (last_char_before_tsub))
                    word_boundary_before = false;
                }

              mbui_init (string_iter, tsub_in_string);
              {
                mbui_iterator_t tsub_iter;

                for (mbui_init (tsub_iter, tsub);
                     mbui_avail (tsub_iter);
                     mbui_advance (tsub_iter))
                  {
                    if (!mbui_avail (string_iter))
                      abort ();
                    mbui_advance (string_iter);
                  }
              }
              word_boundary_after = true;
              if (mbui_avail (string_iter))
                {
                  mbchar_t first_char_after_tsub = mbui_cur (string_iter);
                  if (mb_isalnum (first_char_after_tsub))
                    word_boundary_after = false;
                }

              if (word_boundary_before && word_boundary_after)
                {
                  found = true;
                  break;
                }

              mbui_init (string_iter, tsub_in_string);
              if (!mbui_avail (string_iter))
                break;
              string = tsub_in_string + mb_len (mbui_cur (string_iter));
            }
          else
            {
              bool word_boundary_before;
              const char *p;
              bool word_boundary_after;

              word_boundary_before = true;
              if (string < tsub_in_string)
                if (isalnum ((unsigned char) tsub_in_string[-1]))
                  word_boundary_before = false;

              p = tsub_in_string + strlen (tsub);
              word_boundary_after = true;
              if (*p != '\0')
                if (isalnum ((unsigned char) *p))
                  word_boundary_after = false;

              if (word_boundary_before && word_boundary_after)
                {
                  found = true;
                  break;
                }

              if (*tsub_in_string == '\0')
                break;
              string = tsub_in_string + 1;
            }
        }
    }
  free (tsub);
  return found;
}
Exemplo n.º 6
0
/* Find the first occurrence in the character string STRING of any character
   not in the character string REJECT.  Return the number of bytes from the
   beginning of the string to this occurrence, or to the end of the string
   if none exists.  */
size_t
mbsspn (const char *string, const char *reject)
{
  /* Optimize two cases.  */
  if (reject[0] == '\0')
    return 0;
  if (reject[1] == '\0')
    {
      unsigned char uc = (unsigned char) reject[0];

      if (MB_CUR_MAX > 1)
        {
          mbui_iterator_t iter;

          for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
            if (!(mb_len (mbui_cur (iter)) == 1
                  && (unsigned char) * mbui_cur_ptr (iter) == uc))
              break;
          return mbui_cur_ptr (iter) - string;
        }
      else
        {
          const char *ptr;

          for (ptr = string; *ptr != '\0'; ptr++)
            if ((unsigned char) *ptr != uc)
              break;
          return ptr - string;
        }
    }
  /* General case.  */
  if (MB_CUR_MAX > 1)
    {
      mbui_iterator_t iter;

      for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
        {
          if (mb_len (mbui_cur (iter)) == 1)
            {
              if (mbschr (reject, * mbui_cur_ptr (iter)) == NULL)
                goto found;
            }
          else
            {
              mbui_iterator_t aiter;

              for (mbui_init (aiter, reject);; mbui_advance (aiter))
                {
                  if (!mbui_avail (aiter))
                    goto found;
                  if (mb_equal (mbui_cur (aiter), mbui_cur (iter)))
                    break;
                }
            }
        }
     found:
      return mbui_cur_ptr (iter) - string;
    }
  else
    return strspn (string, reject);
}
Exemplo n.º 7
0
static int
tag_image (char *text, struct text_buffer *outbuf)
{
  mbi_iterator_t iter;
  enum { state_kw, state_val, state_qstr, state_delim } state = state_kw;
  struct text_buffer tmpbuf;
  char *kw;
  struct info_tag *tag_head = NULL, *tag;
  int escaped = 0;
  
  text_buffer_init (&tmpbuf);
  for (mbi_init (iter, text, strlen (text)); mbi_avail (iter);
       mbi_advance (iter))
    {
      const char *cur_ptr;
      size_t cur_len;
      
      if (mb_isspace (mbi_cur (iter)))
	{
	  if (state == state_val)
	    {
              struct info_tag *new_kw = tag_found_keyword (&tmpbuf, &kw);
              new_kw->next = tag_head;
              tag_head = new_kw;
              state = state_delim;
              continue;
	    }
	  if (state == state_delim)
	    continue;
	}
      cur_len = mb_len (mbi_cur (iter));
      cur_ptr = mbi_cur_ptr (iter);
      
      if (state == state_qstr && escaped)
	{
	  escaped = 0;
	}
      else if (cur_len == 1)
	{
	  switch (*cur_ptr)
	    {
	    case '=':
	      text_buffer_add_char (&tmpbuf, 0);
	      kw = tmpbuf.base;
	      if (!mbi_avail (iter))
		break;
	      mbi_advance (iter);
	      state = state_val;
	      cur_len = mb_len (mbi_cur (iter));
	      cur_ptr = mbi_cur_ptr (iter);
	      if (!(cur_len == 1 && *cur_ptr == '"'))
		break;
	      /* fall through */

	    case '"':
	      if (state == state_val)
		{
		  state = state_qstr;
		  continue;
		}
	      if (state == state_qstr)
		{
		  struct info_tag *new_kw = tag_found_keyword (&tmpbuf, &kw);
		  new_kw->next = tag_head;
		  tag_head = new_kw;
		  state = state_delim;
		  continue;
		}
	      break;

	    case '\\':
	      if (state == state_qstr)
		{
		  escaped = 1;
		  continue;
		}
	    }
	}
      text_buffer_add_string (&tmpbuf, cur_ptr, cur_len);
    }

  tag = info_tag_find (tag_head, "text");
  if (!tag)
    tag = info_tag_find (tag_head, "alt");

  if (tag)
    {
      text_buffer_add_string (outbuf, tag->val, strlen (tag->val));
    }
  
  text_buffer_free (&tmpbuf);
  info_tag_free (tag_head);
  return 0;
}