static void internal_format_alt(a_string_t *dst, int len_src, const char *src)
{
    int i;

    a_strcpy(dst,"");

    if (len_src>=2)
    {
        a_strcat(dst,"<tt><span foreground='#2f2fff'>&gt;</span> ");

        for (i=2; i<len_src; i++)
        {
            if (src[i]=='<')
                a_strcat(dst,"&lt;");
            else if (src[i]=='>')
                a_strcat(dst,"&gt;");
            else if (src[i]=='&')
                a_strcat(dst,"&amp;");
            else
                a_strpushback(dst,src[i]);
        }
        a_strcat(dst,"</tt>");
    }
}
Ejemplo n.º 2
0
int get_next_xml_token(FILE* F, a_string_t *tok, int *in_element)
{
  int c,marker;
  
  while (isspace(c = fpeek(F))) xfgetc(F);
  
  a_strcpy(tok,"");

  c = xfgetc(F);

  if (*in_element>=TOK_BEGIN)
  {
    if (c=='\'' || c=='"')
    {
      marker = c;
      *in_element =  TOK_STRING;
      for (;;)
      {
	c = xfgetc(F);
	if (c==marker) return XML_OK;
	if (c==EOF) {
	  log_printf(LOG_ERROR,"unterminated quoted value");
	  return XML_ERROR;
	}
	a_strpushback(tok,c);
      }
    }

    if (c=='=')
    {
      *in_element = TOK_EQUAL;
      a_strcpy(tok,"=");
      return XML_OK;
    }

    if (c=='>')
    {
      a_strcpy(tok,">");
      *in_element=TOK_GTE;
      return XML_OK;
    }

    if (c=='/')
    {
      c = xfgetc(F);
      if (c!='>') {
	log_printf(LOG_ERROR,"Expected '>'");
	return XML_ERROR;
      }
      a_strcpy(tok,"/>");
      *in_element=TOK_SLASH_GTE;
      return XML_OK;
    }

    *in_element=TOK_ATTRIBUTE;
    if (!isvalid(c)) return XML_ERROR;
    a_strpushback(tok,c);

    for (;;)
    {
      c = fpeek(F);
      if (c==EOF) {
	log_printf(LOG_ERROR,"Unterminated attribute '%s...'",a_strval(tok));
	return XML_ERROR;
      }
      if (!isvalid(c)) return XML_OK;
      a_strpushback(tok,xfgetc(F));
    }
    return XML_OK;
  }
  else
  {
    if (c=='<')
    {
      a_strpushback(tok,c);
      c = fgetc(F);

      if (c=='?' || c=='!' || c=='/')
      {
	if (c=='/')
	  *in_element=TOK_END;
	else
	  *in_element=TOK_INSTRUCTION;

	a_strpushback(tok,c);
	for (;;)
	{
	  c = xfgetc(F);
	  if (c==EOF) {
	    log_printf(LOG_ERROR,"Unterminated '%s...'",a_strval(tok));
	    return XML_ERROR;
	  }
	  if (c=='>') {
	    a_strpushback(tok,'>');
	    return XML_OK;
	  }
	}
      }

      *in_element=TOK_BEGIN;
      if (!isvalid(c)) return XML_ERROR;
      a_strpushback(tok,c);

      for (;;)
      {
	c = fpeek(F);
	if (c==EOF) {
	  log_printf(LOG_ERROR,"Unterminated element '%s...'",a_strval(tok));
	  return XML_ERROR;
	}
	if (!isvalid(c)) return XML_OK;
	a_strpushback(tok,xfgetc(F));
      }
    }
    else /* if c=='<' */
    {
      *in_element=TOK_TEXT;

      a_strpushback(tok,c);
      for (;;)
      {
	c = fpeek(F);
	if (c==EOF) {
	  log_printf(LOG_ERROR,"Unterminated data '%s...'",a_strval(tok));
	  return XML_ERROR;
	}
	if (c=='<') return XML_OK;
	a_strpushback(tok,xfgetc(F));
      }
    } /* else not c=='<' */
  } /* else not in_element */
  log_printf(LOG_ERROR,"Syntax error (should not happen)");
  return XML_ERROR;
}