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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
#[1]RSS 2.0 [2]RSS .92 [3]Atom 0.3
[4]Echoreply
Computers, Science, Technology, Xen Virtualization, Hosting, The Internet,
Geekdom And More
* [5]Site Home
* [6]Blog Home
* [7]RSS
* [8]Contact
* [9]Legal
[10]Interesting internationalization
Posted on September 18, 2008
Filed Under [11]Programming |
I've been having a rather enjoyable discussion with [12]Lorenzo
Bettini, the author of GNU [13]Gengetopt and [14]Gengen. If you are not
familiar with Gengetopt, its a tool that simplifies generating the code
required for programs to accept options. I [15]talked about gengetopt a
while back on this blog.
The discussion surrounds support for internationalization, which simply
means a program printing output in whatever language your system is
using. A very popular means to accomplish this is the GNU gettext
library, or its counterpart in various other operating systems.
Disclamer, if you do not care about C standards and common UNIX
programming, this post will probably bore you to tears. However, your
welcome to read on if only to discover how much I value life in boring
geeky flavors :)
Recently, while working on HelenOS I found the need to implement
getopt. I'm working on something a lot like busybox, but from scratch.
Rather than re-invent the getopt wheel yet again, I dove into the
source code of GNU getopt as well as the various *BSD implementations
to find the one that was easiest to port. One thing that stood out at
me was that most implementations declared error strings and formats as
static, English strings.
A week later, a proposal to add gettext support came across the
gengetopt mailing list. I voted to not include it because the resulting
output would be at best mixed, as error strings in basic C89/C99 libc
are hard coded, usually in English. The getopt family of functions are
an interesting corner case, most core libc functions never print
anything ... they just exit with some value and set a global errno to
inform the calling function of what went wrong. Its then up to the
calling function to (use or not use) something like gettext to display
the information in the appropriate language.
This got me thinking .. if we're making a truly modern operating system
based on C89/C99 .. internationalization must be easy to leverage.
Talking with Lorenzo, some interesting ideas came to light.
Gettext is a great tool, however it introduces dependencies on shared
objects (or requires static linking). Simply parsing command line
arguments should not introduce additional dependencies. Additionally,
standard libc should not favor one internationalization library over
another, therefore its just not practical to include
internationalization in standard libc. However, stuff in libc that
prints strings can remain friendly to internationalization by simply
requiring one tiny additional step - have the programmer register a
callback function that gets all output instead of printing to the error
file descriptor directly in libc.
For instance, a modern getopt might look like this:
void getopt_callback(int level, char *format, int line)
{
switch (level) {
case GETOPT_EOK:
fprintf(stdout, "line %d of getopt.c says %s", line, format);
break;
case GETOPT_EFAIL:
fprintf(stderr, "line %d of getopt.c says %s", line, format);
break;
}
return;
}
int main(int argc, char **argv)
{
int c, opt_ind;
getopt_register_callback(getopt_callback);
for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
c = getopt_long(argc, argv, "f:o:O", long_options, &opt_ind);
......
As we can see, the getopt implementation does not actually print
anything, it allows the callback function to handle that. One could
very easily use internationalization tools within the callback, if they
wanted to do so. This goes (basically) for anything in core libc that
prints anything. The callback may be better typed as something like
getopt_callback_t.
This seems to be the favorable approach. Of course, getopt would
feature its own static callback that printed the strings as-is if the
calling function did not register one, this keeps from breaking
existing use.
After I finish some other tasks, I am going to take a stroll through
the HelenOS userspace generic libc to see if getopt is the only case
where this should be implemented. At the least, I'll implement this
getopt method there so that programs can be fully internationalized.
This also shows the virtue of not using printf() directly when you can
avoid it. Writing your own functions to print warnings or messages
allows someone else (even you) to go back and make only a few minor
changes in order for your program to support any language.
Working on a new OS that aims to be fully modern presents interesting
opportunities to pick things apart. Hopefully, this didn't put you to
sleep :)
[16]Share this post
[17]Submit to Bloglines [18]Submit to Del.icio.us [19]Submit to digg
[20]Submit to Facebook [21]Submit to Google Bookmarks [22]Submit
to reddit [23]Submit to Slashdot [24]Submit to Stumble Upon [25]Submit
to SphereIt [26]Submit to Technorati [27]Submit to Yahoo My Web
[28]Hide Sites
You might also enjoy reading:
* [29]For Concurrency Issues, There Is Music (0)
* [30]The Great Microkernel Debate (2)
* [31]Xen And Single System Images (0)
Tags: [32]helenos, [33]Ideas, [34]interesting, [35]Programming
Comments
Leave a Reply (NoFollow Free!)
Name or nickname(required)
________________________________________
Email Address (kept private)(required)
________________________________________
Website
________________________________________
XHTML: You can use these tags: <a href="" title=""> <abbr title="">
<acronym title=""> <b> <blockquote cite=""> <cite> <code> <del
datetime=""> <em> <i> <q cite=""> <strike> <strong>
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Submit Comment
* Recently written
+ [36]For Concurrency Issues, There Is Music
+ [37]Django And The Admin Anthem
+ [38]The Great Microkernel Debate
+ [39]Xen And Single System Images
+ [40]Yes, I'm Still Alive
+ [41]Please adopt /etc/vendor
+ [42]My list for 2009
+ [43]Web pages as graphs
* Me According To Ohloh
[44]Ohloh profile for Tim Post
* Read By Description
+ [45]Free Software
+ [46]ohno
+ [47]haha
+ [48]GNU
+ [49]Programming
+ [50]thinking out loud
+ [51]interesting
+ [52]Linux
* Twitter
+ Writing proof of concept code, I may have a new (full time)
opportunity. [53]11 days ago
+ My kernel's OOM killer has become sentient and self aware
[54]12 days ago
+ Wrapping up nexgent for its first alpha release [55]69 days
ago
*
+ Blogs I frequent
o [56]Corey Henderson
o [57]Jakub Jermar
o [58]John Hawks
o [59]Pavel Rimsky
+ Current Projects
o [60]Ext3cow
o [61]Gridnix
o [62]HelenOS
o [63]My HG Repository
o [64]SRCE
o [65]Xen Guests
+ For Developers
o [66]Blue - A Programming Language
o [67]Mercurial
o [68]ShareSource
o [69]Stackoverflow
* Archives
+ [70]March 2009
+ [71]February 2009
+ [72]January 2009
+ [73]December 2008
+ [74]November 2008
+ [75]October 2008
+ [76]September 2008
+ [77]August 2008
+ [78]July 2008
+ [79]June 2008
+ [80]May 2008
+ [81]April 2008
+ [82]March 2008
+ [83]February 2008
+ [84]January 2008
+ [85]December 2007
+ [86]November 2007
+ [87]October 2007
+ [88]September 2007
*
Penguin
* Search
+ ____________________
* Categories
+ [89]Better Living
+ [90]Blogeting
+ [91]Computing
+ [92]Free Software
+ [93]Future Presents
+ [94]Gizmos
+ [95]GNU
+ [96]Humor
+ [97]Ideas
+ [98]Linux
+ [99]Neat Things
+ [100]Parenting
+ [101]Programming
+ [102]Rants
+ [103]Science
+ [104]The Web Hosting Industry
+ [105]Travels
+ [106]Uncategorized
+ [107]Xen
* Misc
+ [108]Log in
[109]Valid XHTML Copyright © 2007 Tim Post - All Rights Reserved
The views and opinions expressed on this web site are my own and not
necessarily those of my employers or affiliates.
Theme made from [110]many free design elements, [111]powered by
Wordpress.
References
1. http://echoreply.us/tech/feed/
2. http://echoreply.us/tech/feed/rss/
3. http://echoreply.us/tech/feed/atom/
4. http://echoreply.us/tech/
5. http://echoreply.us/
6. http://echoreply.us/tech/
7. http://echoreply.us/tech/feed/rss2/
8. http://echoreply.us/tech/contact/
9. http://echoreply.us/tech/legal/
10. http://echoreply.us/tech/2008/09/18/interesting-internationalization/
11. http://echoreply.us/tech/category/programming/
12. http://www.lorenzobettini.it/
13. http://www.gnu.org/software/gengetopt/gengetopt.html
14. http://www.gnu.org/software/gengen/
15. http://echoreply.us/tech/2008/01/08/fun-with-gengetopt/
16. file://localhost/home/abaumann/projects/WolfBones/docs/i18n/Interesting%20internationalization%20|%20Echoreply.html
17. http://www.bloglines.com/sub/http://echoreply.us/tech/2008/09/18/interesting-internationalization/
18. http://del.icio.us/post?url=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
19. http://digg.com/submit?phase=2&url=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
20. http://www.facebook.com/sharer.php?u=http://echoreply.us/tech/2008/09/18/interesting-internationalization/
21. http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
22. http://reddit.com/submit?url=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
23. http://slashdot.org/bookmark.pl?url=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
24. http://www.stumbleupon.com/submit.php?url=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
25. http://www.sphere.com/search?q=sphereit:http://echoreply.us/tech/2008/09/18/interesting-internationalization/&title=Interesting+internationalization
26. http://www.technorati.com/faves?add=http://echoreply.us/tech/2008/09/18/interesting-internationalization/
27. http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://echoreply.us/tech/2008/09/18/interesting-internationalization/&t=Interesting+internationalization
28. file://localhost/home/abaumann/projects/WolfBones/docs/i18n/Interesting%20internationalization%20|%20Echoreply.html
29. http://echoreply.us/tech/2009/03/05/for-concurrency-issues-there-is-music/
30. http://echoreply.us/tech/2009/02/19/the-great-microkernel-debate/
31. http://echoreply.us/tech/2009/02/19/xen-and-single-system-images/
32. http://echoreply.us/tech/tag/helenos/
33. http://echoreply.us/tech/tag/ideas/
34. http://echoreply.us/tech/tag/interesting/
35. http://echoreply.us/tech/tag/programming/
36. http://echoreply.us/tech/2009/03/05/for-concurrency-issues-there-is-music/
37. http://echoreply.us/tech/2009/02/19/django-and-the-admin-anthem/
38. http://echoreply.us/tech/2009/02/19/the-great-microkernel-debate/
39. http://echoreply.us/tech/2009/02/19/xen-and-single-system-images/
40. http://echoreply.us/tech/2009/02/19/yes-im-still-alive/
41. http://echoreply.us/tech/2009/01/05/please-adopt-etcvendor/
42. http://echoreply.us/tech/2008/12/30/my-list-for-2009/
43. http://echoreply.us/tech/2008/12/28/web-pages-as-graphs/
44. http://www.ohloh.net/accounts/20338?ref=Detailed
45. http://echoreply.us/tech/tag/free-software/
46. http://echoreply.us/tech/tag/ohno/
47. http://echoreply.us/tech/tag/haha/
48. http://echoreply.us/tech/tag/gnu/
49. http://echoreply.us/tech/tag/programming/
50. http://echoreply.us/tech/tag/thinking-out-loud/
51. http://echoreply.us/tech/tag/interesting/
52. http://echoreply.us/tech/tag/linux/
53. http://twitter.com/tinkertim/statuses/1273136035
54. http://twitter.com/tinkertim/statuses/1273063132
55. http://twitter.com/tinkertim/statuses/1095308437
56. http://cormander.com/
57. http://jakubsuniversalblog.blogspot.com/
58. http://johnhawks.net/weblog/
59. http://helenos-blog.pavel-rimsky.cz/
60. http://www.ext3cow.com/
61. http://gridnix.org/
62. http://www.helenos.org/
63. http://echoreply.us/hg/index.php
64. http://srce.echoreply.us/
65. http://sharesource.org/project/xguests/
66. http://www.lechak.info/blue/
67. http://www.selenic.com/mercurial/wiki/
68. http://sharesource.org/
69. http://stackoverflow.com/
70. http://echoreply.us/tech/2009/03/
71. http://echoreply.us/tech/2009/02/
72. http://echoreply.us/tech/2009/01/
73. http://echoreply.us/tech/2008/12/
74. http://echoreply.us/tech/2008/11/
75. http://echoreply.us/tech/2008/10/
76. http://echoreply.us/tech/2008/09/
77. http://echoreply.us/tech/2008/08/
78. http://echoreply.us/tech/2008/07/
79. http://echoreply.us/tech/2008/06/
80. http://echoreply.us/tech/2008/05/
81. http://echoreply.us/tech/2008/04/
82. http://echoreply.us/tech/2008/03/
83. http://echoreply.us/tech/2008/02/
84. http://echoreply.us/tech/2008/01/
85. http://echoreply.us/tech/2007/12/
86. http://echoreply.us/tech/2007/11/
87. http://echoreply.us/tech/2007/10/
88. http://echoreply.us/tech/2007/09/
89. http://echoreply.us/tech/category/better-living/
90. http://echoreply.us/tech/category/blogeting/
91. http://echoreply.us/tech/category/computing/
92. http://echoreply.us/tech/category/free-software/
93. http://echoreply.us/tech/category/planned-projects/
94. http://echoreply.us/tech/category/gizmos/
95. http://echoreply.us/tech/category/gnu/
96. http://echoreply.us/tech/category/humor/
97. http://echoreply.us/tech/category/ideas/
98. http://echoreply.us/tech/category/linux/
99. http://echoreply.us/tech/category/neat-things/
100. http://echoreply.us/tech/category/parenting/
101. http://echoreply.us/tech/category/programming/
102. http://echoreply.us/tech/category/rants/
103. http://echoreply.us/tech/category/science/
104. http://echoreply.us/tech/category/the-web-hosting-industry/
105. http://echoreply.us/tech/category/travels/
106. http://echoreply.us/tech/category/uncategorized/
107. http://echoreply.us/tech/category/xen/
108. http://echoreply.us/tech/wp-login.php
109. http://validator.w3.org/check?uri=referer
110. http://echoreply.us/design_credits.html
111. http://wordpress.org/
|