Example #1
0
static void
iconv_ostream::free (iconv_ostream_t stream)
{
  /* Silently ignore the few bytes in stream->buf[] that don't correspond to a
     character.  */

  /* Avoid glibc-2.1 bug and Solaris 2.7 bug.  */
  #if defined _LIBICONV_VERSION \
      || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
  {
    char outbuffer[2048];
    char *outptr = outbuffer;
    size_t outsize = sizeof (outbuffer);
    size_t res = iconv (stream->cd, NULL, NULL, &outptr, &outsize);
    if (res == (size_t)(-1))
      error (EXIT_FAILURE, 0, _("%s: cannot convert from %s to %s"),
             "iconv_ostream", stream->from_encoding, stream->to_encoding);
    /* Output the converted part.  */
    if (sizeof (outbuffer) - outsize > 0)
      ostream_write_mem (stream->destination,
                         outbuffer, sizeof (outbuffer) - outsize);
  }
  #endif

  iconv_close (stream->cd);
  free (stream->from_encoding);
  free (stream->to_encoding);
  free (stream);
}
Example #2
0
void
message_print_comment (const message_ty *mp, ostream_t stream)
{
  if (mp->comment != NULL)
    {
      size_t j;

      begin_css_class (stream, class_translator_comment);

      for (j = 0; j < mp->comment->nitems; ++j)
	{
	  const char *s = mp->comment->item[j];
	  do
	    {
	      const char *e;
	      ostream_write_str (stream, "#");
	      if (*s != '\0')
		ostream_write_str (stream, " ");
	      e = strchr (s, '\n');
	      if (e == NULL)
		{
		  ostream_write_str (stream, s);
		  s = NULL;
		}
	      else
		{
		  ostream_write_mem (stream, s, e - s);
		  s = e + 1;
		}
	      ostream_write_str (stream, "\n");
	    }
	  while (s != NULL);
	}

      end_css_class (stream, class_translator_comment);
    }
}
Example #3
0
static void
html_ostream::write_mem (html_ostream_t stream, const void *data, size_t len)
{
  if (len > 0)
    {
      #define BUFFERSIZE 2048
      char inbuffer[BUFFERSIZE];
      size_t inbufcount;

      inbufcount = stream->buflen;
      if (inbufcount > 0)
        memcpy (inbuffer, stream->buf, inbufcount);
      for (;;)
        {
          /* At this point, inbuffer[0..inbufcount-1] is filled.  */
          {
            /* Combine the previous rest with a chunk of new input.  */
            size_t n =
              (len <= BUFFERSIZE - inbufcount ? len : BUFFERSIZE - inbufcount);

            if (n > 0)
              {
                memcpy (inbuffer + inbufcount, data, n);
                data = (char *) data + n;
                inbufcount += n;
                len -= n;
              }
          }
          {
            /* Handle complete UTF-8 characters.  */
            const char *inptr = inbuffer;
            size_t insize = inbufcount;

            while (insize > 0)
              {
                unsigned char c0;
                ucs4_t uc;
                int nbytes;

                c0 = ((const unsigned char *) inptr)[0];
                if (insize < (c0 < 0xc0 ? 1 : c0 < 0xe0 ? 2 : c0 < 0xf0 ? 3 :
                              c0 < 0xf8 ? 4 : c0 < 0xfc ? 5 : 6))
                  break;

                nbytes = u8_mbtouc (&uc, (const unsigned char *) inptr, insize);

                if (uc == '\n')
                  {
                    size_t prev_class_stack_size = stream->curr_class_stack_size;
                    stream->curr_class_stack_size = 0;
                    emit_pending_spans (stream, false);
                    ostream_write_str (stream->destination, "<br/>");
                    stream->curr_class_stack_size = prev_class_stack_size;
                  }
                else
                  {
                    emit_pending_spans (stream, true);

                    switch (uc)
                      {
                      case '"':
                        ostream_write_str (stream->destination, "&quot;");
                        break;
                      case '&':
                        ostream_write_str (stream->destination, "&amp;");
                        break;
                      case '<':
                        ostream_write_str (stream->destination, "&lt;");
                        break;
                      case '>':
                        /* Needed to avoid "]]>" in the output.  */
                        ostream_write_str (stream->destination, "&gt;");
                        break;
                      case ' ':
                        /* Needed because HTML viewers merge adjacent spaces
                           and drop spaces adjacent to <br> and similar.  */
                        ostream_write_str (stream->destination, "&nbsp;");
                        break;
                      default:
                        if (uc >= 0x20 && uc < 0x7F)
                          {
                            /* Output ASCII characters as such.  */
                            char bytes[1];
                            bytes[0] = uc;
                            ostream_write_mem (stream->destination, bytes, 1);
                          }
                        else
                          {
                            /* Output non-ASCII characters in #&nnn;
                               notation.  */
                            char bytes[32];
                            sprintf (bytes, "&#%d;", (int) uc);
                            ostream_write_str (stream->destination, bytes);
                          }
                        break;
                      }
                  }

                inptr += nbytes;
                insize -= nbytes;
              }
            /* Put back the unconverted part.  */
            if (insize > BUFSIZE)
              abort ();
            if (len == 0)
              {
                if (insize > 0)
                  memcpy (stream->buf, inptr, insize);
                stream->buflen = insize;
                break;
              }
            if (insize > 0)
              memmove (inbuffer, inptr, insize);
            inbufcount = insize;
          }
        }
      #undef BUFFERSIZE
    }
}
Example #4
0
/* Print a color test page.  */
void
print_color_test ()
{
  /* Code copied from test-term-ostream.c.  */
  static struct { const char *name; term_color_t c; int r; int g; int b; }
         colors[] =
    {
      { "black",   -2,   0,   0,   0 },
      { "blue",    -2,   0,   0, 255 },
      { "green",   -2,   0, 255,   0 },
      { "cyan",    -2,   0, 255, 255 },
      { "red",     -2, 255,   0,   0 },
      { "magenta", -2, 255,   0, 255 },
      { "yellow",  -2, 255, 255,   0 },
      { "white",   -2, 255, 255, 255 },
      { "default", COLOR_DEFAULT }
    };
  term_ostream_t stream;
  int i, row, col;

  stream = term_ostream_create (1, "stdout");

  for (i = 0; i < 8; i++)
    colors[i].c =
      term_ostream_rgb_to_color (stream, colors[i].r, colors[i].g, colors[i].b);

  ostream_write_str (stream, "Colors (foreground/background):\n");
  ostream_write_str (stream, "       ");
  for (col = 0; col <= 8; col++)
    {
      const char *name = colors[col].name;
      ostream_write_str (stream, "|");
      ostream_write_str (stream, name);
      ostream_write_mem (stream, "        ", 7 - strlen (name));
    }
  ostream_write_str (stream, "\n");
  for (row = 0; row <= 8; row++)
    {
      const char *name = colors[row].name;
      ostream_write_str (stream, name);
      ostream_write_mem (stream, "        ", 7 - strlen (name));
      for (col = 0; col <= 8; col++)
        {
          term_color_t row_color = colors[row].c;
          term_color_t col_color = colors[col].c;

          ostream_write_str (stream, "|");
          term_ostream_set_color (stream, row_color);
          term_ostream_set_bgcolor (stream, col_color);
          if (!(term_ostream_get_color (stream) == row_color
                && term_ostream_get_bgcolor (stream) == col_color))
            abort ();
          ostream_write_str (stream, " Words ");
          term_ostream_set_color (stream, COLOR_DEFAULT);
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
          if (!(term_ostream_get_color (stream) == COLOR_DEFAULT
                && term_ostream_get_bgcolor (stream) == COLOR_DEFAULT))
            abort ();
        }
      ostream_write_str (stream, "\n");
    }
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Colors (hue/saturation):\n");
  /* Hue from 0 to 1.  */
  for (row = 0; row <= 17; row++)
    {
      ostream_write_str (stream, row == 0 ? "red:     " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int r = 255;
          int b = (int) (255.0f / 64.0f * col + 0.5f);
          int g = b + (int) (row / 17.0f * (r - b) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  /* Hue from 1 to 2.  */
  for (row = 17; row >= 0; row--)
    {
      ostream_write_str (stream, row == 17 ? "yellow:  " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int g = 255;
          int b = (int) (255.0f / 64.0f * col + 0.5f);
          int r = b + (int) (row / 17.0f * (g - b) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  /* Hue from 2 to 3.  */
  for (row = 0; row <= 17; row++)
    {
      ostream_write_str (stream, row == 0 ? "green:   " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int g = 255;
          int r = (int) (255.0f / 64.0f * col + 0.5f);
          int b = r + (int) (row / 17.0f * (g - r) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  /* Hue from 3 to 4.  */
  for (row = 17; row >= 0; row--)
    {
      ostream_write_str (stream, row == 17 ? "cyan:    " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int b = 255;
          int r = (int) (255.0f / 64.0f * col + 0.5f);
          int g = r + (int) (row / 17.0f * (b - r) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  /* Hue from 4 to 5.  */
  for (row = 0; row <= 17; row++)
    {
      ostream_write_str (stream, row == 0 ? "blue:    " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int b = 255;
          int g = (int) (255.0f / 64.0f * col + 0.5f);
          int r = g + (int) (row / 17.0f * (b - g) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  /* Hue from 5 to 6.  */
  for (row = 17; row >= 0; row--)
    {
      ostream_write_str (stream, row == 17 ? "magenta: " :
                                 row == 0 ? "red:     " : "         ");
      for (col = 0; col <= 64; col++)
        {
          int r = 255;
          int g = (int) (255.0f / 64.0f * col + 0.5f);
          int b = g + (int) (row / 17.0f * (r - g) + 0.5f);
          term_color_t c = term_ostream_rgb_to_color (stream, r, g, b);
          term_ostream_set_bgcolor (stream, c);
          ostream_write_str (stream, " ");
          term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
        }
      ostream_write_str (stream, "\n");
    }
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Weights:\n");
  term_ostream_set_weight (stream, WEIGHT_NORMAL);
  if (term_ostream_get_weight (stream) != WEIGHT_NORMAL)
    abort ();
  ostream_write_str (stream, "normal, ");
  term_ostream_set_weight (stream, WEIGHT_BOLD);
  if (term_ostream_get_weight (stream) != WEIGHT_BOLD)
    abort ();
  ostream_write_str (stream, "bold, ");
  term_ostream_set_weight (stream, WEIGHT_DEFAULT);
  if (term_ostream_get_weight (stream) != WEIGHT_DEFAULT)
    abort ();
  ostream_write_str (stream, "default \n");
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Postures:\n");
  term_ostream_set_posture (stream, POSTURE_NORMAL);
  if (term_ostream_get_posture (stream) != POSTURE_NORMAL)
    abort ();
  ostream_write_str (stream, "normal, ");
  term_ostream_set_posture (stream, POSTURE_ITALIC);
  if (term_ostream_get_posture (stream) != POSTURE_ITALIC)
    abort ();
  ostream_write_str (stream, "italic, ");
  term_ostream_set_posture (stream, POSTURE_DEFAULT);
  if (term_ostream_get_posture (stream) != POSTURE_DEFAULT)
    abort ();
  ostream_write_str (stream, "default \n");
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Text decorations:\n");
  term_ostream_set_underline (stream, UNDERLINE_OFF);
  if (term_ostream_get_underline (stream) != UNDERLINE_OFF)
    abort ();
  ostream_write_str (stream, "normal, ");
  term_ostream_set_underline (stream, UNDERLINE_ON);
  if (term_ostream_get_underline (stream) != UNDERLINE_ON)
    abort ();
  ostream_write_str (stream, "underlined, ");
  term_ostream_set_underline (stream, UNDERLINE_DEFAULT);
  if (term_ostream_get_underline (stream) != UNDERLINE_DEFAULT)
    abort ();
  ostream_write_str (stream, "default \n");
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Colors (foreground) mixed with attributes:\n");
  for (row = 0; row <= 8; row++)
    {
      const char *name = colors[row].name;
      ostream_write_str (stream, name);
      ostream_write_mem (stream, "        ", 7 - strlen (name));
      term_ostream_set_color (stream, colors[row].c);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      ostream_write_str (stream, "bold");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      ostream_write_str (stream, "italic");
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "underlined");
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_color (stream, COLOR_DEFAULT);
      ostream_write_str (stream, "\n       ");
      term_ostream_set_color (stream, colors[row].c);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      ostream_write_str (stream, "bold+italic");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "bold+underl");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "italic+underl");
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_color (stream, COLOR_DEFAULT);
      ostream_write_str (stream, "\n");
    }
  ostream_write_str (stream, "\n");

  ostream_write_str (stream, "Colors (background) mixed with attributes:\n");
  for (row = 0; row <= 8; row++)
    {
      const char *name = colors[row].name;
      ostream_write_str (stream, name);
      ostream_write_mem (stream, "        ", 7 - strlen (name));
      term_ostream_set_bgcolor (stream, colors[row].c);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      ostream_write_str (stream, "bold");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      ostream_write_str (stream, "italic");
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "underlined");
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
      ostream_write_str (stream, "\n       ");
      term_ostream_set_bgcolor (stream, colors[row].c);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      ostream_write_str (stream, "bold+italic");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_weight (stream, WEIGHT_BOLD);
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "bold+underl");
      term_ostream_set_weight (stream, WEIGHT_NORMAL);
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_posture (stream, POSTURE_ITALIC);
      term_ostream_set_underline (stream, UNDERLINE_ON);
      ostream_write_str (stream, "italic+underl");
      term_ostream_set_posture (stream, POSTURE_NORMAL);
      term_ostream_set_underline (stream, UNDERLINE_OFF);
      ostream_write_str (stream, "|normal|");
      term_ostream_set_bgcolor (stream, COLOR_DEFAULT);
      ostream_write_str (stream, "\n");
    }
  ostream_write_str (stream, "\n");

  ostream_free (stream);
}
Example #5
0
static void
iconv_ostream::write_mem (iconv_ostream_t stream, const void *data, size_t len)
{
  if (len > 0)
    {
      #define BUFFERSIZE 256
      char inbuffer[BUFFERSIZE];
      size_t inbufcount;

      inbufcount = stream->buflen;
      if (inbufcount > 0)
        memcpy (inbuffer, stream->buf, inbufcount);
      for (;;)
        {
          /* At this point, inbuffer[0..inbufcount-1] is filled.  */
          {
            /* Combine the previous rest with a chunk of new input.  */
            size_t n =
              (len <= BUFFERSIZE - inbufcount ? len : BUFFERSIZE - inbufcount);

            if (n > 0)
              {
                memcpy (inbuffer + inbufcount, data, n);
                data = (char *) data + n;
                inbufcount += n;
                len -= n;
              }
          }
          {
            /* Attempt to convert the combined input.  */
            char outbuffer[8*BUFFERSIZE];

            const char *inptr = inbuffer;
            size_t insize = inbufcount;
            char *outptr = outbuffer;
            size_t outsize = sizeof (outbuffer);

            size_t res = iconv (stream->cd,
                                (ICONV_CONST char **) &inptr, &insize,
                                &outptr, &outsize);
            #if !defined _LIBICONV_VERSION && !defined __GLIBC__
            /* Irix iconv() inserts a NUL byte if it cannot convert.
               NetBSD iconv() inserts a question mark if it cannot convert.
               Only GNU libiconv and GNU libc are known to prefer to fail rather
               than doing a lossy conversion.  */
            if (res > 0)
              {
                errno = EILSEQ;
                res = -1;
              }
            #endif
            if (res == (size_t)(-1) && errno != EINVAL)
              error (EXIT_FAILURE, 0, _("%s: cannot convert from %s to %s"),
                     "iconv_ostream",
                     stream->from_encoding, stream->to_encoding);
            /* Output the converted part.  */
            if (sizeof (outbuffer) - outsize > 0)
              ostream_write_mem (stream->destination,
                                 outbuffer, sizeof (outbuffer) - outsize);
            /* Put back the unconverted part.  */
            if (insize > BUFSIZE)
              error (EXIT_FAILURE, 0, _("%s: shift sequence too long"),
                     "iconv_ostream");
            if (len == 0)
              {
                if (insize > 0)
                  memcpy (stream->buf, inptr, insize);
                stream->buflen = insize;
                break;
              }
            if (insize > 0)
              memmove (inbuffer, inptr, insize);
            inbufcount = insize;
          }
        }
      #undef BUFFERSIZE
    }
}