]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/locale.c
1 /* locale - Implementation of the locale program according to POSIX 1003.2
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
38 #include "localeinfo.h"
41 /* If set print the name of the category. */
42 static int show_category_name
;
44 /* If set print the name of the item. */
45 static int show_keyword_name
;
48 static const struct option long_options
[] =
50 { "all-locales", no_argument
, NULL
, 'a' },
51 { "category-name", no_argument
, &show_category_name
, 1 },
52 { "charmaps", no_argument
, NULL
, 'm' },
53 { "help", no_argument
, NULL
, 'h' },
54 { "keyword-name", no_argument
, &show_keyword_name
, 1 },
55 { "version", no_argument
, NULL
, 'V' },
60 /* We don't have these constants defined because we don't use them. Give
62 #define CTYPE_MB_CUR_MIN 0
63 #define CTYPE_MB_CUR_MAX 0
64 #define CTYPE_HASH_SIZE 0
65 #define CTYPE_HASH_LAYERS 0
67 #define CTYPE_TOUPPER_EB 0
68 #define CTYPE_TOLOWER_EB 0
69 #define CTYPE_TOUPPER_EL 0
70 #define CTYPE_TOLOWER_EL 0
72 /* Definition of the data structure which represents a category and its
83 enum { std
, opt
} status
;
84 enum value_type value_type
;
90 /* Simple helper macro. */
91 #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
93 /* For some tricky stuff. */
94 #define NO_PAREN(Item, More...) Item, ## More
96 /* We have all categories defined in `categories.def'. Now construct
97 the description and data structure used for all categories. */
98 #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
99 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
100 static struct cat_item category##_desc[] = \
105 #include "categories.def"
106 #undef DEFINE_CATEGORY
108 static struct category category
[] =
110 #define DEFINE_CATEGORY(category, name, items, postload, in, check, out) \
111 [category] = { _NL_NUM_##category, name, NELEMS (category##_desc), \
113 #include "categories.def"
114 #undef DEFINE_CATEGORY
116 #define NCATEGORIES NELEMS (category)
119 /* Automatically set variable. */
120 extern const char *__progname
;
122 /* helper function for extended name handling. */
123 extern void locale_special (const char *name
, int show_category_name
,
124 int show_keyword_name
);
126 /* Prototypes for local functions. */
127 static void usage (int status
) __attribute__ ((noreturn
));
128 static void write_locales (void);
129 static void write_charmaps (void);
130 static void show_locale_vars (void);
131 static void show_info (const char *name
);
135 main (int argc
, char *argv
[])
143 /* Set initial values for global variables. */
144 show_category_name
= 0;
145 show_keyword_name
= 0;
147 /* Set locale. Do not set LC_ALL because the other categories must
148 not be affected (acccording to POSIX.2). */
149 setlocale (LC_CTYPE
, "");
150 setlocale (LC_MESSAGES
, "");
152 /* Initialize the message catalog. */
153 textdomain (PACKAGE
);
155 while ((optchar
= getopt_long (argc
, argv
, "achkmV", long_options
, NULL
))
159 case '\0': /* Long option. */
165 show_category_name
= 1;
171 show_keyword_name
= 1;
180 usage (EXIT_FAILURE
);
183 /* Version information is requested. */
186 fprintf (stderr
, "locale (GNU %s) %s\n", PACKAGE
, VERSION
);
187 fprintf (stderr
, _("\
188 Copyright (C) %s Free Software Foundation, Inc.\n\
189 This is free software; see the source for copying conditions. There is NO\n\
190 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
192 fprintf (stderr
, _("Written by %s.\n"),
198 /* Help is requested. */
200 usage (EXIT_SUCCESS
);
202 /* `-a' requests the names of all available locales. */
209 /* `m' requests the names of all available charmaps. The names can be
210 used for the -f argument to localedef(3). */
211 if (do_charmaps
!= 0)
217 /* Specific information about the current locale are requested.
218 Change to this locale now. */
219 setlocale (LC_ALL
, "");
221 /* If no real argument is given we have to print the contents of the
222 current locale definition variables. These are LANG and the LC_*. */
223 if (optind
== argc
&& show_keyword_name
== 0 && show_category_name
== 0)
229 /* Process all given names. */
230 while (optind
< argc
)
231 show_info (argv
[optind
++]);
237 /* Display usage information and exit. */
241 if (status
!= EXIT_SUCCESS
)
242 fprintf (stderr
, gettext ("Try `%s --help' for more information.\n"),
247 Usage: %s [OPTION]... name\n\
248 Mandatory arguments to long options are mandatory for short options too.\n\
249 -h, --help display this help and exit\n\
250 -V, --version output version information and exit\n\
252 -a, --all-locales write names of available locales\n\
253 -m, --charmaps write names of available charmaps\n\
255 -c, --category-name write names of selected categories\n\
256 -k, --keyword-name write names of selected keywords\n"),
258 fputs (gettext ("Report bugs to <bug-glibc@prep.ai.mit.edu>.\n"),
266 /* Write the names of all available locales to stdout. */
271 struct dirent
*dirent
;
273 /* `POSIX' locale is always available (POSIX.2 4.34.3). */
276 dir
= opendir (LOCALE_PATH
);
279 error (1, errno
, gettext ("cannot read locale directory `%s'"),
284 /* Now we can look for all files in the directory. */
285 while ((dirent
= readdir (dir
)) != NULL
)
286 if (strcmp (dirent
->d_name
, ".") != 0
287 && strcmp (dirent
->d_name
, "..") != 0)
288 puts (dirent
->d_name
);
294 /* Write the names of all available character maps to stdout. */
296 write_charmaps (void)
299 struct dirent
*dirent
;
301 dir
= opendir (CHARMAP_PATH
);
304 error (1, errno
, gettext ("cannot read character map directory `%s'"),
309 /* Now we can look for all files in the directory. */
310 while ((dirent
= readdir (dir
)) != NULL
)
311 if (strcmp (dirent
->d_name
, ".") != 0
312 && strcmp (dirent
->d_name
, "..") != 0)
313 puts (dirent
->d_name
);
319 /* We have to show the contents of the environments determining the
322 show_locale_vars (void)
325 const char *lcall
= getenv ("LC_ALL");
326 const char *lang
= getenv ("LANG") ? : "POSIX";
328 void get_source (const char *name
)
330 char *val
= getenv (name
);
332 if (lcall
!= NULL
|| val
== NULL
)
333 printf ("%s=\"%s\"\n", name
, lcall
? : lang
);
335 printf ("%s=%s\n", name
, val
);
338 /* LANG has to be the first value. */
339 printf ("LANG=%s\n", lang
);
341 /* Now all categories in an unspecified order. */
342 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
343 get_source (category
[cat_no
].name
);
345 /* The last is the LC_ALL value. */
346 printf ("LC_ALL=%s\n", lcall
? : "");
350 /* Show the information request for NAME. */
352 show_info (const char *name
)
356 void print_item (struct cat_item
*item
)
358 if (show_keyword_name
!= 0)
359 printf ("%s=", item
->name
);
361 switch (item
->value_type
)
364 printf ("%s%s%s", show_keyword_name
? "\"" : "",
365 nl_langinfo (item
->item_id
) ? : "",
366 show_keyword_name
? "\"" : "");
373 if (show_keyword_name
)
376 for (cnt
= 0; cnt
< item
->max
- 1; ++cnt
)
378 val
= nl_langinfo (item
->item_id
+ cnt
);
379 printf ("%s;", val
? : "");
382 val
= nl_langinfo (item
->item_id
+ cnt
);
383 printf ("%s", val
? : "");
385 if (show_keyword_name
)
391 const char *val
= nl_langinfo (item
->item_id
);
394 printf ("%d", *val
== CHAR_MAX
? -1 : *val
);
399 const char *val
= nl_langinfo (item
->item_id
);
400 int cnt
= val
? strlen (val
) : 0;
404 printf ("%d;", *val
== CHAR_MAX
? -1 : *val
);
409 printf ("%d", cnt
== 0 || *val
== CHAR_MAX
? -1 : *val
);
414 unsigned int val
= (unsigned int) nl_langinfo (item
->item_id
);
423 for (cat_no
= 0; cat_no
< NCATEGORIES
; ++cat_no
)
427 if (strcmp (name
, category
[cat_no
].name
) == 0)
428 /* Print the whole category. */
430 if (show_category_name
!= 0)
431 puts (category
[cat_no
].name
);
433 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
434 print_item (&category
[cat_no
].item_desc
[item_no
]);
439 for (item_no
= 0; item_no
< category
[cat_no
].number
; ++item_no
)
440 if (strcmp (name
, category
[cat_no
].item_desc
[item_no
].name
) == 0)
442 if (show_category_name
!= 0)
443 puts (category
[cat_no
].name
);
445 print_item (&category
[cat_no
].item_desc
[item_no
]);
450 /* When we get to here the name is not standard ones. For testing
451 and perhpas advanced use we allow some more symbols. */
452 locale_special (name
, show_category_name
, show_keyword_name
);