summaryrefslogtreecommitdiffstats
path: root/winsup/utils/getlocale.c
blob: 3a601269fa6323c8d77dec05323acb183969a7ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2010, Corinna Vinschen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#define WINVER 0x0601
#include <windows.h>

extern char *__progname;

void usage (FILE *, int) __attribute__ ((noreturn));

void
usage (FILE * stream, int status)
{
  fprintf (stream,
	   "Usage: %s [-asuh]\n"
	   "Print Windows locale(s)\n"
	   "\n"
	   "Options:\n"
	   "\n"
	   "  -a, --all       List all available locales\n"
	   "  -s, --system    Print system default locale\n"
	   "                  (default is current user default locale)\n"
	   "  -u, --utf       Attach \".UTF-8\" to the result\n"
	   "  -h, --help      this text\n",
	   __progname);
  exit (status);
}

struct option longopts[] = {
  {"all", no_argument, NULL, 'a'},
  {"system", no_argument, NULL, 's'},
  {"utf", no_argument, NULL, 'u'},
  {"help", no_argument, NULL, 'h'},
  {0, no_argument, NULL, 0}
};
const char *opts = "ahsu";

int
getlocale (LCID lcid, char *name)
{
  char iso639[10];
  char iso3166[10];

  iso3166[0] = '\0';
  if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, 10))
    return 0;
  GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, 10);
  sprintf (name, "%s%s%s", iso639, lcid > 0x3ff ? "_" : "",
			   lcid > 0x3ff ? iso3166 : "");
  return 1;
}

int main (int argc, char **argv)
{
  int opt;
  LCID lcid = LOCALE_USER_DEFAULT;
  int all = 0;
  const char *utf = "";
  char name[32];

  while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
    switch (opt)
      {
      case 'a':
        all = 1;
	break;
      case 's':
      	lcid = LOCALE_SYSTEM_DEFAULT;
	break;
      case 'u':
      	utf = ".UTF-8";
	break;
      case 'h':
	usage (stdout, 0);
	break;
      default:
	usage (stderr, 1);
	break;
      }
  if (all)
    {
      unsigned lang, sublang;

      for (lang = 1; lang <= 0x3ff; ++lang)
	for (sublang = 1; sublang <= 0x3f; ++sublang)
	  {
	    lcid = (sublang << 10) | lang;
	    if (getlocale (lcid, name))
	      {
		char lang[256];
		char country[256];
		char loc[32];
		GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, lang, 256);
		GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, 256);
		stpcpy (stpcpy (loc, name), utf);
		printf ("%-16s %s (%s)\n", loc, lang, country);
	      }
	  }
      return 0;
    }
  if (getlocale (lcid, name))
    printf ("%s%s\n", name, utf);
  return 0;
}