Greenbone Security Assistant  7.0.0
xslt_i18n.c File Reference
#include "xslt_i18n.h"
#include "gsad_base.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <glib.h>
#include <libintl.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxslt/xsltutils.h>
#include <math.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
Include dependency graph for xslt_i18n.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "gsad xslt"
 GLib log domain. More...
 
#define GETTEXT_CONTEXT_GLUE   "\004"
 
#define GSA_I18N_EXT_URI   "http://openvas.org/i18n"
 Namespace URI for the i18n XSLT extension. More...
 
#define GSA_XSL_TEXTDOMAIN   "gsad_xsl"
 

Functions

void register_i18n_ext_module ()
 Register the i18n XSLT extension module. More...
 
int get_ext_gettext_enabled ()
 Get whether gettext functions for extensions are enabled. More...
 
void set_ext_gettext_enabled (int enabled)
 Enable or disable gettext functions for extensions. More...
 
int init_language_lists ()
 Initialize the list of available languages. More...
 
void buffer_languages_xml (GString *buffer)
 Write the list of installed languages to a buffer as XML. More...
 
gchar * accept_language_to_env_fmt (const char *accept_language)
 Convert an Accept-Language string to the LANGUAGE env variable form. More...
 

Macro Definition Documentation

#define G_LOG_DOMAIN   "gsad xslt"

GLib log domain.

Definition at line 45 of file xslt_i18n.c.

#define GETTEXT_CONTEXT_GLUE   "\004"

Definition at line 48 of file xslt_i18n.c.

#define GSA_I18N_EXT_URI   "http://openvas.org/i18n"

Namespace URI for the i18n XSLT extension.

Definition at line 54 of file xslt_i18n.c.

Referenced by register_i18n_ext_module().

#define GSA_XSL_TEXTDOMAIN   "gsad_xsl"

Definition at line 59 of file xslt_i18n.c.

Referenced by init_language_lists(), and register_i18n_ext_module().

Function Documentation

gchar* accept_language_to_env_fmt ( const char *  accept_language)

Convert an Accept-Language string to the LANGUAGE env variable form.

Converts the language preferences as defined in a HTTP Accept-Language header to a colon-separated list of language codes as used by gettext in the LANGUAGE environment variable.

Parameters
[in]accept_languageHTTP Accept-Language header text.
Returns
Newly allocated string of language codes as used by gettext. If accept_language is NULL or it doesn't contain a language DEFAULT_GSAD_LANGUAGE is returned.

Definition at line 769 of file xslt_i18n.c.

References DEFAULT_GSAD_LANGUAGE.

Referenced by handle_request(), and save_my_settings_omp().

770 {
771  if (accept_language == NULL)
772  return g_strdup (DEFAULT_GSAD_LANGUAGE);
773 
774  gchar *language;
775  // TODO: Convert to a colon-separated list of codes instead of
776  // just extracting the first one
777  gchar **prefs, *pref;
778  prefs = g_strsplit_set (accept_language, ",;", -1);
779 
780  pref = prefs [0];
781  if (pref)
782  {
783  char *pos;
784  g_strstrip (pref);
785  pos = pref;
786  while (pos[0] != '\0')
787  {
788  if (pos[0] == '-')
789  pos[0] = '_';
790  pos++;
791  };
792  }
793  language = g_strdup (pref ? pref : DEFAULT_GSAD_LANGUAGE);
794  g_strfreev (prefs);
795 
796  return language;
797 }
#define DEFAULT_GSAD_LANGUAGE
Default language code, used when Accept-Language header is missing.
Definition: xslt_i18n.h:35

Here is the caller graph for this function:

void buffer_languages_xml ( GString *  buffer)

Write the list of installed languages to a buffer as XML.

Parameters
[in]bufferA GString buffer to write to.

Definition at line 718 of file xslt_i18n.c.

Referenced by init_language_lists().

719 {
720  GList *langs_list;
721  assert (buffer);
722 
723  langs_list = g_list_first (installed_languages);
724 
725  g_string_append (buffer, "<gsa_languages>");
726  while (langs_list)
727  {
728  gchar *lang_code, *lang_name, *native_name, *language_escaped;
729 
730  lang_code = (gchar*) langs_list->data;
731 
732  lang_name = g_hash_table_lookup (language_names, lang_code);
733  if (lang_name == NULL)
734  lang_name = lang_code;
735 
736  native_name = g_hash_table_lookup (native_language_names, lang_code);
737  if (native_name == NULL)
738  native_name = lang_name;
739 
740  language_escaped
741  = g_markup_printf_escaped ("<language>"
742  "<code>%s</code>"
743  "<name>%s</name>"
744  "<native_name>%s</native_name>"
745  "</language>",
746  lang_code,
747  lang_name,
748  native_name);
749  g_string_append (buffer, language_escaped);
750  g_free (language_escaped);
751  langs_list = g_list_nth (langs_list, 1);
752  }
753  g_string_append (buffer, "</gsa_languages>");
754 }

Here is the caller graph for this function:

int get_ext_gettext_enabled ( )

Get whether gettext functions for extensions are enabled.

Returns
0 gettext is disabled, 1 gettext is enabled.

Definition at line 558 of file xslt_i18n.c.

559 {
560  return ext_gettext_enabled;
561 }
int init_language_lists ( )

Initialize the list of available languages.

Returns
0: success, -1: error

Definition at line 580 of file xslt_i18n.c.

References buffer_languages_xml(), get_chroot_state(), and GSA_XSL_TEXTDOMAIN.

Referenced by main().

581 {
582  FILE *lang_names_file;
583  const char *locale_dir_name;
584  DIR *locale_dir;
585  struct dirent *entry;
586 
587  if (installed_languages != NULL)
588  {
589  g_warning ("%s: Language lists already initialized.", __FUNCTION__);
590  return -1;
591  }
592 
593  /* Init data structures */
594  language_names = g_hash_table_new_full (g_str_hash, g_str_equal,
595  g_free, g_free);
596 
597  native_language_names = g_hash_table_new_full (g_str_hash, g_str_equal,
598  g_free, g_free);
599 
600  // installed_languages starts initialized as NULL
601 
602  /* Add presets "Browser Language" and "English" */
603  installed_languages = g_list_append (installed_languages,
604  g_strdup ("Browser Language"));
605  installed_languages = g_list_append (installed_languages,
606  g_strdup ("en"));
607  g_hash_table_insert (language_names,
608  g_strdup ("Browser Language"),
609  g_strdup ("Browser Language"));
610  g_hash_table_insert (native_language_names,
611  g_strdup ("Browser Language"),
612  g_strdup ("Browser Language"));
613  g_hash_table_insert (language_names,
614  g_strdup ("en"), g_strdup ("English"));
615  g_hash_table_insert (native_language_names,
616  g_strdup ("en"), g_strdup ("English"));
617 
618  /* Get language names */
619  lang_names_file = fopen (GSA_DATA_DIR "/language_names.tsv", "r");
620  if (lang_names_file)
621  {
622  size_t len;
623  char *line = NULL;
624  while (getline (&line, &len, lang_names_file) != -1)
625  {
626  g_strstrip (line);
627  if (line [0] != '\0' && line [0] != '#')
628  {
629  gchar **columns;
630  gchar *code, *name, *native_name;
631  columns = g_strsplit (line, "\t", 3);
632  code = columns [0];
633  name = code ? columns [1] : NULL;
634  native_name = name ? columns [2] : NULL;
635  if (code && name)
636  g_hash_table_insert (language_names,
637  g_strdup (code),
638  g_strdup (name));
639  if (code && native_name)
640  g_hash_table_insert (native_language_names,
641  g_strdup (code),
642  g_strdup (native_name));
643  g_strfreev (columns);
644  }
645  g_free (line);
646  line = NULL;
647  }
648  fclose (lang_names_file);
649  }
650  else
651  {
652  g_warning ("%s: Failed to open language names file: %s",
653  __FUNCTION__, strerror (errno));
654  }
655 
656  /* Get installed translations */
657  locale_dir_name = get_chroot_state () ? GSA_CHROOT_LOCALE_DIR
658  : GSA_LOCALE_DIR;
659  locale_dir = opendir (locale_dir_name);
660 
661  if (locale_dir == NULL)
662  {
663  g_warning ("%s: Failed to open locale directory \"%s\": %s",
664  __FUNCTION__, GSA_LOCALE_DIR, strerror (errno));
665  return -1;
666  }
667 
668  while ((entry = readdir (locale_dir)) != 0)
669  {
670  if (entry->d_name[0] != '.'
671  && strlen (entry->d_name) >= 2
672  && entry->d_type == DT_DIR
673  && strcmp (entry->d_name, "en")
674  && strcmp (entry->d_name, "Browser Language"))
675  {
676  FILE *mo_file;
677  gchar *lang_mo_path;
678  lang_mo_path = g_build_filename (locale_dir_name,
679  entry->d_name,
680  "LC_MESSAGES",
681  GSA_XSL_TEXTDOMAIN ".mo",
682  NULL);
683 
684  mo_file = fopen (lang_mo_path, "r");
685  if (mo_file)
686  {
687  fclose (mo_file);
688  installed_languages
689  = g_list_insert_sorted (installed_languages,
690  g_strdup (entry->d_name),
691  (GCompareFunc) strcmp);
692  }
693  else
694  {
695  if (errno != ENOENT)
696  g_warning ("%s: Failed to open %s: %s",
697  __FUNCTION__, lang_mo_path, strerror (errno));
698  }
699  g_free (lang_mo_path);
700  }
701  }
702  closedir (locale_dir);
703 
704  GString *test = g_string_new ("");
705  buffer_languages_xml (test);
706  g_debug ("%s: Initialized language lists", __FUNCTION__);
707  g_string_free (test, TRUE);
708 
709  return 0;
710 }
#define GSA_XSL_TEXTDOMAIN
Definition: xslt_i18n.c:59
int get_chroot_state()
Gets the chroot state.
Definition: gsad_base.c:114
void buffer_languages_xml(GString *buffer)
Write the list of installed languages to a buffer as XML.
Definition: xslt_i18n.c:718

Here is the call graph for this function:

Here is the caller graph for this function:

void register_i18n_ext_module ( )

Register the i18n XSLT extension module.

Definition at line 533 of file xslt_i18n.c.

References get_chroot_state(), GSA_I18N_EXT_URI, and GSA_XSL_TEXTDOMAIN.

Referenced by xsl_transform_with_stylesheet().

534 {
535  g_debug ("Registering i18n XSLT module");
536 
537  xsltRegisterExtModule((xmlChar*) GSA_I18N_EXT_URI,
538  init_i18n_module,
539  shutdown_i18n_module);
540 
541  if (bindtextdomain (GSA_XSL_TEXTDOMAIN,
542  get_chroot_state () ? GSA_CHROOT_LOCALE_DIR
543  : GSA_LOCALE_DIR)
544  == NULL)
545  {
546  g_critical ("%s: Failed to bind text domain for gettext", __FUNCTION__);
547  abort ();
548  }
549 
550 }
#define GSA_I18N_EXT_URI
Namespace URI for the i18n XSLT extension.
Definition: xslt_i18n.c:54
#define GSA_XSL_TEXTDOMAIN
Definition: xslt_i18n.c:59
int get_chroot_state()
Gets the chroot state.
Definition: gsad_base.c:114

Here is the call graph for this function:

Here is the caller graph for this function:

void set_ext_gettext_enabled ( int  enabled)

Enable or disable gettext functions for extensions.

Parameters
enabled0 to disable, any other to enable.

Definition at line 569 of file xslt_i18n.c.

Referenced by main().

570 {
571  ext_gettext_enabled = (enabled != 0);
572 }

Here is the caller graph for this function: