]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testlang.c
Add header validation option to ipptool.
[thirdparty/cups.git] / cups / testlang.c
CommitLineData
ef416fc2 1/*
503b54c9 2 * Localization test program for CUPS.
ef416fc2 3 *
1a9743f9 4 * Copyright 2007-2017 by Apple Inc.
503b54c9 5 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 6 *
503b54c9
MS
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
57b7b66b 11 * missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 12 *
503b54c9 13 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 14 */
15
16/*
17 * Include necessary headers...
18 */
19
71e16022 20#include "cups-private.h"
f787e1e3 21#include "ppd-private.h"
1a9743f9
MS
22#ifdef __APPLE__
23# include <CoreFoundation/CoreFoundation.h>
24#endif /* __APPLE__ */
ef416fc2 25
26
27/*
28 * 'main()' - Load the specified language and show the strings for yes and no.
29 */
30
31int /* O - Exit status */
32main(int argc, /* I - Number of command-line arguments */
33 char *argv[]) /* I - Command-line arguments */
34{
7594b224 35 int i; /* Looping var */
36 int errors = 0; /* Number of errors */
ef416fc2 37 cups_lang_t *language; /* Message catalog */
38 cups_lang_t *language2; /* Message catalog */
7594b224 39 struct lconv *loc; /* Locale data */
40 char buffer[1024]; /* String buffer */
41 double number; /* Number */
42 static const char * const tests[] = /* Test strings */
43 {
44 "1",
45 "-1",
46 "3",
47 "5.125"
48 };
49
ef416fc2 50
ef416fc2 51 if (argc == 1)
52 {
53 language = cupsLangDefault();
54 language2 = cupsLangDefault();
55 }
56 else
57 {
58 language = cupsLangGet(argv[1]);
59 language2 = cupsLangGet(argv[1]);
36474350
MS
60
61 setenv("LANG", argv[1], 1);
62 setenv("SOFTWARE", "CUPS/" CUPS_SVERSION, 1);
ef416fc2 63 }
64
36474350
MS
65 _cupsSetLocale(argv);
66
ef416fc2 67 if (language != language2)
68 {
7594b224 69 errors ++;
70
ef416fc2 71 puts("**** ERROR: Language cache did not work! ****");
72 puts("First result from cupsLangGet:");
73 }
74
75 printf("Language = \"%s\"\n", language->language);
76 printf("Encoding = \"%s\"\n", _cupsEncodingName(language->encoding));
77 printf("No = \"%s\"\n", _cupsLangString(language, "No"));
78 printf("Yes = \"%s\"\n", _cupsLangString(language, "Yes"));
79
80 if (language != language2)
81 {
82 puts("Second result from cupsLangGet:");
83
84 printf("Language = \"%s\"\n", language2->language);
85 printf("Encoding = \"%s\"\n", _cupsEncodingName(language2->encoding));
86 printf("No = \"%s\"\n", _cupsLangString(language2, "No"));
87 printf("Yes = \"%s\"\n", _cupsLangString(language2, "Yes"));
88 }
89
7594b224 90 loc = localeconv();
91
92 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i ++)
93 {
94 number = _cupsStrScand(tests[i], NULL, loc);
95
96 printf("_cupsStrScand(\"%s\") number=%f\n", tests[i], number);
97
98 _cupsStrFormatd(buffer, buffer + sizeof(buffer), number, loc);
99
100 printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number, buffer);
101
102 if (strcmp(buffer, tests[i]))
103 {
104 errors ++;
105 puts("**** ERROR: Bad formatted number! ****");
106 }
107 }
108
36474350
MS
109 if (argc == 3)
110 {
111 ppd_file_t *ppd; /* PPD file */
112 ppd_option_t *option; /* PageSize option */
113 ppd_choice_t *choice; /* PageSize/Letter choice */
114
115 if ((ppd = ppdOpenFile(argv[2])) == NULL)
116 {
117 printf("Unable to open PPD file \"%s\".\n", argv[2]);
118 errors ++;
119 }
120 else
121 {
122 ppdLocalize(ppd);
123
124 if ((option = ppdFindOption(ppd, "PageSize")) == NULL)
125 {
126 puts("No PageSize option.");
127 errors ++;
128 }
129 else
130 {
131 printf("PageSize: %s\n", option->text);
132
133 if ((choice = ppdFindChoice(option, "Letter")) == NULL)
134 {
135 puts("No Letter PageSize choice.");
136 errors ++;
137 }
138 else
139 {
140 printf("Letter: %s\n", choice->text);
141 }
142 }
143
2650d637
MS
144 printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd, "media-empty", NULL, buffer, sizeof(buffer)));
145
36474350
MS
146 ppdClose(ppd);
147 }
148 }
1a9743f9
MS
149#ifdef __APPLE__
150 else
151 {
152 /*
153 * Test all possible language IDs for compatibility with _cupsAppleLocale...
154 */
155
156 CFIndex j, /* Looping var */
157 num_locales; /* Number of locales */
158 CFArrayRef locales; /* Locales */
159 CFStringRef locale_id, /* Current locale ID */
160 language_id; /* Current language ID */
161 char locale_str[256], /* Locale ID C string */
162 language_str[256], /* Language ID C string */
163 *bufptr; /* Pointer to ".UTF-8" in POSIX locale */
164 size_t buflen; /* Length of POSIX locale */
165# if TEST_COUNTRY_CODES
166 CFIndex k, /* Looping var */
167 num_country_codes; /* Number of country codes */
168 CFArrayRef country_codes; /* Country codes */
169 CFStringRef country_code, /* Current country code */
170 temp_id; /* Temporary language ID */
171 char country_str[256]; /* Country code C string */
172# endif /* TEST_COUNTRY_CODES */
173
174 locales = CFLocaleCopyAvailableLocaleIdentifiers();
175 num_locales = CFArrayGetCount(locales);
176
177# if TEST_COUNTRY_CODES
178 country_codes = CFLocaleCopyISOCountryCodes();
179 num_country_codes = CFArrayGetCount(country_codes);
180# endif /* TEST_COUNTRY_CODES */
181
182 printf("%d locales are available:\n", (int)num_locales);
183
184 for (j = 0; j < num_locales; j ++)
185 {
186 locale_id = CFArrayGetValueAtIndex(locales, j);
187 language_id = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault, locale_id);
188
189 if (!locale_id || !CFStringGetCString(locale_id, locale_str, (CFIndex)sizeof(locale_str), kCFStringEncodingASCII))
190 {
191 printf("%d: FAIL (unable to get locale ID string)\n", (int)j + 1);
192 errors ++;
193 continue;
194 }
195
196 if (!language_id || !CFStringGetCString(language_id, language_str, (CFIndex)sizeof(language_str), kCFStringEncodingASCII))
197 {
198 printf("%d %s: FAIL (unable to get language ID string)\n", (int)j + 1, locale_str);
199 errors ++;
200 continue;
201 }
202
203 if (!_cupsAppleLocale(language_id, buffer, sizeof(buffer)))
204 {
205 printf("%d %s(%s): FAIL (unable to convert language ID string to POSIX locale)\n", (int)j + 1, locale_str, language_str);
206 errors ++;
207 continue;
208 }
209
210 if ((bufptr = strstr(buffer, ".UTF-8")) != NULL)
211 buflen = (size_t)(bufptr - buffer);
212 else
213 buflen = strlen(buffer);
214
215 if ((language = cupsLangGet(buffer)) == NULL)
216 {
217 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\")\n", (int)j + 1, locale_str, language_str, buffer);
218 errors ++;
219 continue;
220 }
221
222 if (strncasecmp(language->language, buffer, buflen))
223 {
224 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\", got \"%s\")\n", (int)j + 1, locale_str, language_str, buffer, language->language);
225 errors ++;
226 continue;
227 }
228
229 printf("%d %s(%s): PASS (POSIX locale is \"%s\")\n", (int)j + 1, locale_str, language_str, buffer);
230 }
231
232 CFRelease(locales);
233
234# if TEST_COUNTRY_CODES
235 CFRelease(country_codes);
236# endif /* TEST_COUNTRY_CODES */
237 }
238#endif /* __APPLE__ */
36474350 239
7594b224 240 return (errors > 0);
ef416fc2 241}