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