示例#1
0
文件: potool.c 项目: porridge/potool
int potool_printf(char *format, ...)
{
	va_list ap;
	int ret;
	va_start(ap, format);
	ret = vprintf(format, ap);
	if (ret < 0)
		po_error(_("printf() failed with code %d: %s"), ret, strerror(errno));
	va_end(ap);
	return ret;
}
示例#2
0
static void
po_error_logger (const char *format, ...)
{
  va_list args;
  char *error_message;

  va_start (args, format);
  if (vasprintf (&error_message, format, args) < 0)
    error (EXIT_FAILURE, 0, _("memory exhausted"));
  va_end (args);
  po_error (0, 0, "%s", error_message);
  free (error_message);
}
示例#3
0
static void
xerror (int severity, const char *prefix_tail,
        const char *filename, size_t lineno, size_t column,
        int multiline_p, const char *message_text)
{
  if (multiline_p)
    {
      bool old_error_with_progname = error_with_progname;
      char *prefix;

      if (filename != NULL)
        {
          if (lineno != (size_t)(-1))
            {
              if (column != (size_t)(-1))
                prefix =
                  xasprintf ("%s:%ld:%ld: %s", filename,
                             (long) lineno, (long) column, prefix_tail);
              else
                prefix =
                  xasprintf ("%s:%ld: %s", filename,
                             (long) lineno, prefix_tail);
            }
          else
            prefix = xasprintf ("%s: %s", filename, prefix_tail);
          error_with_progname = false;
        }
      else
        prefix = xasprintf ("%s: %s", program_name, prefix_tail);

      if (severity >= PO_SEVERITY_ERROR)
        po_multiline_error (prefix, xstrdup (message_text));
      else
        po_multiline_warning (prefix, xstrdup (message_text));
      error_with_progname = old_error_with_progname;

      if (severity == PO_SEVERITY_FATAL_ERROR)
        exit (EXIT_FAILURE);
    }
  else
    {
      int exit_status =
        (severity == PO_SEVERITY_FATAL_ERROR ? EXIT_FAILURE : 0);

      if (filename != NULL)
        {
          error_with_progname = false;
          if (lineno != (size_t)(-1))
            {
              if (column != (size_t)(-1))
                po_error (exit_status, 0, "%s:%ld:%ld: %s%s",
                          filename, (long) lineno, (long) column,
                          prefix_tail, message_text);
              else
                po_error_at_line (exit_status, 0, filename, lineno, "%s%s",
                                  prefix_tail, message_text);
            }
          else
            po_error (exit_status, 0, "%s: %s%s",
                      filename, prefix_tail, message_text);
          error_with_progname = true;
        }
      else
        po_error (exit_status, 0, "%s%s", prefix_tail, message_text);
      if (severity < PO_SEVERITY_ERROR)
        --error_message_count;
    }
}
示例#4
0
文件: potool.c 项目: porridge/potool
int
main (int argc, char **argv)
{
	int c;
	/* -- */
	gboolean istats = FALSE;
	gboolean copy_msgid = FALSE;
	gboolean preserve_wrapping = FALSE;
	PoFilters ifilters = 0;
	po_write_modes write_mode = 0;

	while ((c = getopt (argc, argv, "f:n:scph")) != EOF) {
		switch (c) {
			case 'h' :
				fprintf (stderr, _(
				"Usage: %s FILENAME1 [FILENAME2] [FILTERS] [-s] [-c] [-p] [-h]\n"
				"\n"
				), argv[0]);
				exit (EXIT_SUCCESS);
				break;
			case 'n' :
				if (strcmp (optarg, "ctxt") == 0) {
					write_mode |= NO_CTX;
				} else if (strcmp (optarg, "id") == 0) {
					write_mode |= NO_ID;
				} else if (strcmp (optarg, "str") == 0) {
					write_mode |= NO_STR;
				} else if (strcmp (optarg, "cmt") == 0) {
					write_mode |= NO_STD_COMMENT | NO_POS_COMMENT |
					              NO_SPEC_COMMENT | NO_RES_COMMENT;
				} else if (strcmp (optarg, "ucmt") == 0) {
					write_mode |= NO_STD_COMMENT;
				} else if (strcmp (optarg, "pcmt") == 0) {
					write_mode |= NO_POS_COMMENT;
				} else if (strcmp (optarg, "scmt") == 0) {
					write_mode |= NO_SPEC_COMMENT;
				} else if (strcmp (optarg, "dcmt") == 0) {
					write_mode |= NO_RES_COMMENT;
				} else if (strcmp (optarg, "tr") == 0) {
					write_mode |= NO_TRANSLATION;
				} else if (strcmp (optarg, "linf") == 0) {
					write_mode |= NO_LINF;
				} else {
					po_error (_("Unknown parameter for -n option!"));
				}
				break;
			case 's' :
				istats = TRUE;
				break;
			case 'c':
				copy_msgid = TRUE;
				break;
			case 'p':
				preserve_wrapping = TRUE;
				break;
			case 'f' :
				if (strcmp (optarg, "f") == 0) {
					ifilters |= FUZZY_FILTER;
				} else if (strcmp (optarg, "nf") == 0) {
					ifilters |= NOT_FUZZY_FILTER;
				} else if (strcmp (optarg, "t") == 0) {
					ifilters |= TRANSLATED_FILTER;
				} else if (strcmp (optarg, "nt") == 0) {
					ifilters |= NOT_TRANSLATED_FILTER;
				} else if (strcmp (optarg, "nth") == 0) {
					ifilters |= NOT_TRANSLATED_H_FILTER;
				} else if (strcmp (optarg, "o") == 0) {
					ifilters |= OBSOLETE_FILTER;
				} else if (strcmp (optarg, "no") == 0) {
					ifilters |= NOT_OBSOLETE_FILTER;
				} else {
					po_error (_("Unknown filter \"%s\"!"), optarg);
				}
				break;
			case ':' :
				po_error (_("Invalid parameter!"));
				break;
			case '?' :
				po_error (_("Invalid option!"));
				break;
			default :
				g_assert_not_reached ();
		}
	}

	if (optind >= argc) {
		po_error (_("Input file not specified!"));
	}
	if (argc - optind == 1) {
		PoFile *pof;
		char *ifn = argv[optind];

		pof = po_read (ifn);
		po_apply_filters (pof, ifilters);

		if (istats) {
			potool_printf (_("%d\n"), g_slist_length (pof->entries));
		} else {
			if (copy_msgid) {
				po_copy_msgid (pof);
			}
			po_write (pof, write_mode, preserve_wrapping);
		}
		po_free (pof);
	} else {
		PoFile *bpof, *pof;
		PoEntry_set *bpo_set;
		char *bfn = argv[optind], *fn = argv[optind + 1];

		bpof = po_read (bfn);
		bpo_set = po_set_create (bpof->entries);
		pof = po_read (fn);
		po_apply_filters (pof, ifilters);
		if (copy_msgid) {
			po_copy_msgid (pof);
		}
		bpo_set = po_set_update (bpo_set, pof->entries);
		po_write (bpof, write_mode, preserve_wrapping);
		g_hash_table_destroy (bpo_set);
		po_free (bpof);
		po_free (pof);
	}
	if (fflush(stdout) != 0)
		po_error(_("fflush(stdout) failed: %s"), strerror(errno));

	return 0;
}
示例#5
0
文件: potool.c 项目: porridge/potool
static void
print_multi_line (const StringBlock *s, int start_offset, const char *prefix, gboolean preserve_wrapping)
{
	int slen, prefix_len;
	char *eol_ptr;
	gboolean has_final_eol;
	char **lines, **ln;
	enum { max_len = 77 };

	if (preserve_wrapping) {
		int i, offset = 0;
		for (i = 0; i < s->num_lines; i++) {
			int line_len = s->line_lengths[i];
			if (i > 0) {
				potool_printf ("%s", prefix);
			}
			potool_printf ("\"%.*s\"\n", line_len, s->str + offset);
			offset += line_len;
		}
		return;
	}

	slen = strlen (s->str);
	eol_ptr = strstr (s->str, "\\n");
	if ((eol_ptr == NULL || (eol_ptr - s->str + 2 == slen))
	    && slen < (RMARGIN - 2 - start_offset)) {
		potool_printf ("\"%s\"\n", s->str);
		return;
	}

	potool_printf ("\"\"\n");
	prefix_len = strlen (prefix);
	has_final_eol = strcmp (s->str + slen - 2, "\\n") == 0;
	lines = g_strsplit (s->str, "\\n", 0);
	for (ln = lines; *ln != NULL; ln++) {
		char *cur;
		int offset;
		gboolean line_has_eol;

#if GLIB_MAJOR_VERSION == 2
		if (*ln[0] == '\0' && *(ln + 1) == NULL)
			continue;
#endif
		potool_printf ("%s\"", prefix);
		line_has_eol = has_final_eol || *(ln + 1) != NULL;
		offset = prefix_len;
		cur = *ln;
		do {
			int word_len = 0;
			int eol_len;
			int ret;

			while (cur[word_len] != SEP1 && cur[word_len] != SEP2 &&
			       cur[word_len] != '\0')
				word_len++;
			while (cur[word_len] == SEP1 || cur[word_len] == SEP2)
				word_len++;

			if (line_has_eol && cur[word_len] == '\0' &&
			    (word_len == 0 || (cur[word_len - 1] != SEP1 && cur[word_len - 1] != SEP2))) {
				eol_len = 2;
			} else {
				eol_len = 0;
			}
			if (offset + word_len + eol_len > max_len) {
				potool_printf ("\"\n%s\"", prefix);
				offset = prefix_len;
			}
			if ((ret = fwrite(cur, 1, word_len, stdout)) != word_len)
				po_error(_("fwrite() failed, returned %d instead of %d: %s"), ret, word_len, strerror(errno));
			offset += word_len;
			cur += word_len;
		} while (*cur != '\0');

		if (line_has_eol) {
			if (offset + 2 > max_len) {
				potool_printf ("\"\n%s\"\\n", prefix);
			} else {
				potool_printf ("\\n");
			}
		}
		potool_printf ("\"\n");
	}
	g_strfreev (lines);
}