explodename.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc.
  2. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
  14. USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include "loadinfo.h"
  22. /* On some strange systems still no definition of NULL is found. Sigh! */
  23. #ifndef NULL
  24. # if defined __STDC__ && __STDC__
  25. # define NULL ((void *) 0)
  26. # else
  27. # define NULL 0
  28. # endif
  29. #endif
  30. /* @@ end of prolog @@ */
  31. char *
  32. _nl_find_language (name)
  33. const char *name;
  34. {
  35. while (name[0] != '\0' && name[0] != '_' && name[0] != '@'
  36. && name[0] != '+' && name[0] != ',')
  37. ++name;
  38. return (char *) name;
  39. }
  40. int
  41. _nl_explode_name (name, language, modifier, territory, codeset,
  42. normalized_codeset, special, sponsor, revision)
  43. char *name;
  44. const char **language;
  45. const char **modifier;
  46. const char **territory;
  47. const char **codeset;
  48. const char **normalized_codeset;
  49. const char **special;
  50. const char **sponsor;
  51. const char **revision;
  52. {
  53. enum { undecided, xpg, cen } syntax;
  54. char *cp;
  55. int mask;
  56. *modifier = NULL;
  57. *territory = NULL;
  58. *codeset = NULL;
  59. *normalized_codeset = NULL;
  60. *special = NULL;
  61. *sponsor = NULL;
  62. *revision = NULL;
  63. /* Now we determine the single parts of the locale name. First
  64. look for the language. Termination symbols are `_' and `@' if
  65. we use XPG4 style, and `_', `+', and `,' if we use CEN syntax. */
  66. mask = 0;
  67. syntax = undecided;
  68. *language = cp = name;
  69. cp = _nl_find_language (*language);
  70. if (*language == cp)
  71. /* This does not make sense: language has to be specified. Use
  72. this entry as it is without exploding. Perhaps it is an alias. */
  73. cp = strchr (*language, '\0');
  74. else if (cp[0] == '_')
  75. {
  76. /* Next is the territory. */
  77. cp[0] = '\0';
  78. *territory = ++cp;
  79. while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@'
  80. && cp[0] != '+' && cp[0] != ',' && cp[0] != '_')
  81. ++cp;
  82. mask |= TERRITORY;
  83. if (cp[0] == '.')
  84. {
  85. /* Next is the codeset. */
  86. syntax = xpg;
  87. cp[0] = '\0';
  88. *codeset = ++cp;
  89. while (cp[0] != '\0' && cp[0] != '@')
  90. ++cp;
  91. mask |= XPG_CODESET;
  92. if (*codeset != cp && (*codeset)[0] != '\0')
  93. {
  94. *normalized_codeset = _nl_normalize_codeset (*codeset,
  95. cp - *codeset);
  96. if (strcmp (*codeset, *normalized_codeset) == 0)
  97. free ((char *) *normalized_codeset);
  98. else
  99. mask |= XPG_NORM_CODESET;
  100. }
  101. }
  102. }
  103. if (cp[0] == '@' || (syntax != xpg && cp[0] == '+'))
  104. {
  105. /* Next is the modifier. */
  106. syntax = cp[0] == '@' ? xpg : cen;
  107. cp[0] = '\0';
  108. *modifier = ++cp;
  109. while (syntax == cen && cp[0] != '\0' && cp[0] != '+'
  110. && cp[0] != ',' && cp[0] != '_')
  111. ++cp;
  112. mask |= XPG_MODIFIER | CEN_AUDIENCE;
  113. }
  114. if (syntax != xpg && (cp[0] == '+' || cp[0] == ',' || cp[0] == '_'))
  115. {
  116. syntax = cen;
  117. if (cp[0] == '+')
  118. {
  119. /* Next is special application (CEN syntax). */
  120. cp[0] = '\0';
  121. *special = ++cp;
  122. while (cp[0] != '\0' && cp[0] != ',' && cp[0] != '_')
  123. ++cp;
  124. mask |= CEN_SPECIAL;
  125. }
  126. if (cp[0] == ',')
  127. {
  128. /* Next is sponsor (CEN syntax). */
  129. cp[0] = '\0';
  130. *sponsor = ++cp;
  131. while (cp[0] != '\0' && cp[0] != '_')
  132. ++cp;
  133. mask |= CEN_SPONSOR;
  134. }
  135. if (cp[0] == '_')
  136. {
  137. /* Next is revision (CEN syntax). */
  138. cp[0] = '\0';
  139. *revision = ++cp;
  140. mask |= CEN_REVISION;
  141. }
  142. }
  143. /* For CEN syntax values it might be important to have the
  144. separator character in the file name, not for XPG syntax. */
  145. if (syntax == xpg)
  146. {
  147. if (*territory != NULL && (*territory)[0] == '\0')
  148. mask &= ~TERRITORY;
  149. if (*codeset != NULL && (*codeset)[0] == '\0')
  150. mask &= ~XPG_CODESET;
  151. if (*modifier != NULL && (*modifier)[0] == '\0')
  152. mask &= ~XPG_MODIFIER;
  153. }
  154. return mask;
  155. }