-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatgets.c
647 lines (564 loc) · 17.9 KB
/
catgets.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* catgets.c - Kitten-like message catalog functions
AUTHOR: Gregory Pietsch
DESCRIPTION:
This file, combined with nl_catd.h, provides a kitten-like version of
the catgets() functions. It's different from kitten in that it uses
the dynamic string functions in the background and you could have more
than one catalog open at a time.
*/
#include "config.h"
/*#ifdef HAVE_NL_CATD_H*/
#include "nl_types.h"
/*#endif*/
#include <ctype.h>
#include <errno.h>
#if defined(__STDC__) || defined(STDC_HEADERS) || defined(HAVE_LIMITS_H)
#include <limits.h>
#endif
#include <stdio.h>
#if defined(__STDC__) || defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#endif
#include "defines.h"
#include "dynstr.h"
typedef struct
{
int set_id, msg_id;
STRING_T *msg;
} _CAT_MESSAGE_T;
static void
_CAT_MESSAGE_ctor (_CAT_MESSAGE_T * x)
{
x->set_id = x->msg_id = 0;
x->msg = DScreate ();
}
static void
_CAT_MESSAGE_dtor (_CAT_MESSAGE_T * x)
{
x->set_id = x->msg_id = 0;
DSdestroy (x->msg);
x->msg = 0;
}
static _CAT_MESSAGE_T *
_CAT_MESSAGE_assign (_CAT_MESSAGE_T * x, _CAT_MESSAGE_T * y)
{
if (x != y)
{
x->set_id = y->set_id;
x->msg_id = y->msg_id;
DSassign (x->msg, y->msg, 0, NPOS);
}
return x;
}
#define T _CAT_MESSAGE_T
#define TS _CATMSG
#define Tassign _CAT_MESSAGE_assign
#define Tctor _CAT_MESSAGE_ctor
#define Tdtor _CAT_MESSAGE_dtor
#define PROTOS_ONLY
#include "dynarray.h"
#undef PROTOS_ONLY
#include "dynarray.h"
#undef Tdtor
#undef Tctor
#undef Tassign
#undef TS
#undef T
typedef struct
{
int is_opened;
_CATMSG_ARRAY_T *msgs;
} _CAT_CATALOG_T;
static void
_CAT_catalog_ctor (_CAT_CATALOG_T * x)
{
x->is_opened = 0;
x->msgs = _CATMSG_create ();
}
static void
_CAT_catalog_dtor (_CAT_CATALOG_T * x)
{
x->is_opened = 0;
_CATMSG_destroy (x->msgs);
x->msgs = 0;
}
static _CAT_CATALOG_T *
_CAT_catalog_assign (_CAT_CATALOG_T * x, _CAT_CATALOG_T * y)
{
if (x != y)
{
x->is_opened = y->is_opened;
_CATMSG_assign (x->msgs, _CATMSG_base (y->msgs),
_CATMSG_length (y->msgs), 1);
}
return x;
}
#define T _CAT_CATALOG_T
#define TS _CATCAT
#define Tassign _CAT_catalog_assign
#define Tctor _CAT_catalog_ctor
#define Tdtor _CAT_catalog_dtor
#define PROTOS_ONLY
#include "dynarray.h"
#undef PROTOS_ONLY
#include "dynarray.h"
#undef Tdtor
#undef Tctor
#undef Tassign
#undef TS
#undef T
static _CATCAT_ARRAY_T *theCatalogues = 0;
/* static functions used by catopen */
enum
{
FSM_ANY = UCHAR_MAX + 1,
FSM_DIGIT,
FSM_ODIGIT,
FSM_XDIGIT,
FSM_OUTPUT,
FSM_SUPPRESS,
FSM_BASE8,
FSM_BASE8_OUTPUT,
FSM_BASE10,
FSM_BASE10_OUTPUT,
FSM_BASE16,
FSM_BASE16_OUTPUT,
FSM_RETAIN
};
static void
transform_string (STRING_T * t, STRING_T * s)
{
int c, state = 0, accum = 0;
size_t ip = 0, i;
static int fsm[] = {
0, '\\', FSM_SUPPRESS, 1,
0, FSM_ANY, FSM_OUTPUT, 0,
1, 'b', '\b', 0,
1, 'e', '\033', 0,
1, 'f', '\f', 0,
1, 'n', '\n', 0,
1, 'r', '\r', 0,
1, 't', '\t', 0,
1, 'v', '\v', 0,
1, '\\', '\\', 0,
1, 'd', FSM_SUPPRESS, 2,
1, FSM_ODIGIT, FSM_BASE8, 3,
1, 'x', FSM_SUPPRESS, 4,
1, FSM_ANY, FSM_OUTPUT, 0,
2, FSM_DIGIT, FSM_BASE10, 21,
2, FSM_ANY, FSM_RETAIN, 0,
3, FSM_ODIGIT, FSM_BASE8, 31,
3, FSM_ANY, FSM_RETAIN, 0,
4, FSM_XDIGIT, FSM_BASE16, 4,
4, FSM_ANY, FSM_RETAIN, 0,
21, FSM_DIGIT, FSM_BASE10, 22,
21, FSM_ANY, FSM_RETAIN, 0,
22, FSM_DIGIT, FSM_BASE10_OUTPUT, 0,
22, FSM_ANY, FSM_RETAIN, 0,
31, FSM_ODIGIT, FSM_BASE8_OUTPUT, 0,
31, FSM_ANY, FSM_RETAIN, 0,
-1, -1, -1, -1
};
static char hexits[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', '\0'
};
DSresize (t, 0, 0);
while (ip < DSlength (s) && (c = DSget_at (s, ip)) != '\0')
{
for (i = 0;
!(fsm[i] == state
&& (fsm[i + 1] == c
|| fsm[i + 1] == FSM_ANY
|| (fsm[i + 1] == FSM_DIGIT && isdigit (c))
|| (fsm[i + 1] == FSM_ODIGIT && strchr ("01234567", c) != 0)
|| (fsm[i + 1] == FSM_XDIGIT && isxdigit (c)))); i += 4);
switch (fsm[i + 2])
{
case FSM_OUTPUT:
DSappendchar (t, c, 1);
ip++;
break;
case FSM_SUPPRESS:
accum = 0;
ip++;
break;
case FSM_BASE10:
accum = (accum * 10) + (c - '0');
ip++;
break;
case FSM_BASE8:
accum = (accum << 3) + (c - '0');
ip++;
break;
case FSM_BASE16:
accum = (accum << 4) + (strchr (hexits, tolower (c)) - hexits);
ip++;
break;
case FSM_BASE10_OUTPUT:
accum = (accum * 10) + (c - '0');
DSappendchar (t, accum, 1);
accum = 0;
ip++;
break;
case FSM_BASE8_OUTPUT:
accum = (accum << 3) + (c - '0');
DSappendchar (t, accum, 1);
accum = 0;
ip++;
break;
case FSM_RETAIN:
DSappendchar (t, accum, 1);
accum = 0;
break;
default:
DSappendchar (t, fsm[i + 2], 1);
ip++;
break;
}
state = fsm[i + 3];
}
}
static int
catmsg_cmp (const void *a, const void *b)
{
const _CAT_MESSAGE_T pa = *(_CAT_MESSAGE_T *) a;
const _CAT_MESSAGE_T pb = *(_CAT_MESSAGE_T *) b;
if (pa.set_id != pb.set_id)
return pa.set_id < pb.set_id ? -1 : +1;
if (pa.msg_id != pb.msg_id)
return pa.msg_id < pb.msg_id ? -1 : +1;
return 0;
}
/* catread - read a catalogue file */
static _CAT_CATALOG_T *
catread (const char *name)
{
FILE *f;
STRING_T *s = DScreate (), *t = DScreate ();
int c;
_CAT_CATALOG_T *cat = 0;
size_t z;
_CAT_MESSAGE_T catmsg = { 0, 0, 0 };
/* Open the catfile */
f = fopen (name, "r");
if (f == 0)
return 0; /* could not open file */
setvbuf (f, 0, _IOFBF, 16384);
while ((c = fgetc (f)) != EOF)
{
if (c == '\n')
{
/* We have a full line */
if (DSlength (s) > 0)
{
z = DSfind_first_not_of (s, " \t\f\v\r", 0, NPOS);
DSremove (s, 0, z);
z = DSfind_last_not_of (s, " \t\f\v\r", NPOS, NPOS);
DSresize (s, z + 1, 0);
}
if (DSlength (s) > 0 && DSget_at (s, DSlength (s) - 1) == '\\')
{
/* continuation */
DSresize (s, DSlength (s) - 1, 0);
}
else
{
if (DSlength (s) > 0 && isdigit (DSget_at (s, 0)))
{
/* if it starts with a digit, assume it's a catalog line */
for (z = 0, catmsg.set_id = 0;
isdigit (c = DSget_at (s, z));
catmsg.set_id = catmsg.set_id * 10 + (c - '0'), z++);
z++;
for (catmsg.msg_id = 0;
isdigit (c = DSget_at (s, z));
catmsg.msg_id = catmsg.msg_id * 10 + (c - '0'), z++);
z++;
DSremove (s, 0, z);
transform_string (t, s);
if (catmsg.msg == 0)
catmsg.msg = DScreate ();
DSassign (catmsg.msg, t, 0, NPOS);
if (cat == 0)
{
cat = malloc (sizeof (_CAT_CATALOG_T));
if (cat == 0)
Nomemory ();
cat->is_opened = 0;
cat->msgs = _CATMSG_create ();
}
_CATMSG_append (cat->msgs, &catmsg, 1, 0);
}
DSresize (s, 0, 0);
}
}
else
DSappendchar (s, c, 1);
}
fclose (f);
qsort (_CATMSG_base (cat->msgs), _CATMSG_length (cat->msgs),
sizeof (_CAT_MESSAGE_T), catmsg_cmp);
return cat;
}
/* install_catalog - install a _CAT_CATALOG_T, return where */
static nl_catd
install_catalog (_CAT_CATALOG_T * cat)
{
size_t i;
cat->is_opened = 1;
if (theCatalogues == 0)
{
theCatalogues = _CATCAT_create ();
_CATCAT_append (theCatalogues, cat, 1, 0);
return (nl_catd) 0;
}
else
{
for (i = 0;
i < _CATCAT_length (theCatalogues)
&& _CATCAT_get_at (theCatalogues, i)->is_opened; i++);
_CATCAT_put_at (theCatalogues, i, cat);
return (nl_catd) i;
}
}
/*
NAME
catopen - open a message catalog
SYNOPSIS
#include <nl_types.h>
nl_catd catopen(const char *name, int oflag);
DESCRIPTION
The catopen() function shall open a message catalog and return a message
catalog descriptor. The name argument specifies the name of the message
catalog to be opened. If name contains a '/' , then name specifies a
complete name for the message catalog. Otherwise, the environment variable
NLSPATH is used with name substituted for the %N conversion specification
(see the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 8,
Environment Variables). If NLSPATH exists in the environment when the
process starts, then if the process has appropriate privileges, the
behavior of catopen() is undefined. If NLSPATH does not exist in the
environment, or if a message catalog cannot be found in any of the
components specified by NLSPATH, then an implementation-defined default
path shall be used. This default may be affected by the setting of
LC_MESSAGES if the value of oflag is NL_CAT_LOCALE, or the LANG
environment variable if oflag is 0.
A message catalog descriptor shall remain valid in a process until that
process closes it, or a successful call to one of the exec functions. A
change in the setting of the LC_MESSAGES category may invalidate
existing open catalogs.
If a file descriptor is used to implement message catalog descriptors, the
FD_CLOEXEC flag shall be set; see <fcntl.h>.
If the value of the oflag argument is 0, the LANG environment variable is
used to locate the catalog without regard to the LC_MESSAGES category. If
the oflag argument is NL_CAT_LOCALE, the LC_MESSAGES category is used to
locate the message catalog (see the Base Definitions volume of IEEE Std
1003.1-2001, Section 8.2, Internationalization Variables).
RETURN VALUE
Upon successful completion, catopen() shall return a message catalog
descriptor for use on subsequent calls to catgets() and catclose().
Otherwise, catopen() shall return (nl_catd)(-1) and set errno to indicate
the error.
ERRORS
The catopen() function may fail if:
[EACCES]
Search permission is denied for the component of the path prefix of
the message catalog or read permission is denied for the message
catalog.
[EMFILE]
{OPEN_MAX} file descriptors are currently open in the calling process.
[ENAMETOOLONG]
The length of a pathname of the message catalog exceeds {PATH_MAX} or
a pathname component is longer than {NAME_MAX}.
[ENAMETOOLONG]
Pathname resolution of a symbolic link produced an intermediate result
whose length exceeds {PATH_MAX}.
[ENFILE]
Too many files are currently open in the system.
[ENOENT]
The message catalog does not exist or the name argument points to an
empty string.
[ENOMEM]
Insufficient storage space is available.
[ENOTDIR]
A component of the path prefix of the message catalog is not a directory.
*/
nl_catd (catopen) (const char *name, int oflag)
{
_CAT_CATALOG_T *_kitten_catalog = 0;
char *lang = 0;
char *nlsptr = 0;
static STRING_T *catfile = 0;
/* Open the catalog file. */
/* If the message _kitten_catalog file name contains a directory separator,
assume that this is a real path to the _kitten_catalog file. Note that
catread will return a true or false value based on its ability
to read the catfile. */
if (strpbrk (name, "\\/:") != 0)
{
/* first approximation: 'name' is a filename */
_kitten_catalog = catread (name);
return _kitten_catalog ?
install_catalog (_kitten_catalog) : (nl_catd) (-1);
}
/* If the message _kitten_catalog file name does not contain a directory
separator, then we need to try to locate the message _kitten_catalog on
our own. We will use several methods to find it. */
/* We will need the value of LANG, and may need a 2-letter abbrev of
LANG later on, so get it now. */
lang = getenv ("LANG");
if (lang == 0)
{
/* printf("no lang= found\n"); */
/* Return failure - we won't be able to locate the cat file */
return (-1);
}
/* step through NLSPATH */
nlsptr = getenv ("NLSPATH");
if (nlsptr == 0)
{
/* printf("no NLSPATH= found\n"); */
/* Return failure - we won't be able to locate the cat file */
return (-1);
}
while (nlsptr != 0)
{
char *tok = strchr (nlsptr, ';');
size_t toklen;
if (tok == 0)
tok = nlsptr + strlen (nlsptr);
toklen = tok - nlsptr;
if (catfile == 0)
catfile = DScreate ();
/* Try to find the _kitten_catalog file in each path from NLSPATH */
/* Rule #1: %NLSPATH%\%LANG%\cat */
DSassigncstr (catfile, nlsptr, toklen);
DSappendchar (catfile, '/', 1);
DSappendcstr (catfile, lang, NPOS);
DSappendcstr (catfile, "/cat", NPOS);
_kitten_catalog = catread (DScstr (catfile));
if (_kitten_catalog != 0)
break;
/* Rule #2: %NLSPATH%\cat.%LANG% */
DSassigncstr (catfile, nlsptr, toklen);
DSappendcstr (catfile, "/cat.", NPOS);
DSappendcstr (catfile, lang, NPOS);
_kitten_catalog = catread (DScstr (catfile));
if (_kitten_catalog != 0)
break;
/* Rule #3: if LANG looks to be in format "en-UK" then
%NLSPATH%\cat.EN */
if (lang[2] == '-')
{
DSassigncstr (catfile, nlsptr, toklen);
DSappendcstr (catfile, "/cat.", NPOS);
DSappendcstr (catfile, lang, 2);
_kitten_catalog = catread (DScstr (catfile));
if (_kitten_catalog != 0)
break;
}
/* Grab next tok for the next while iteration */
nlsptr = tok;
if (nlsptr)
nlsptr++;
DSdestroy (catfile);
catfile = 0;
} /* while tok */
/* If we could not find it, return failure. Otherwise, install the
catalog and return a cookie. */
return _kitten_catalog ? install_catalog (_kitten_catalog) : (nl_catd) (-1);
}
/*
NAME
catgets - read a program message
SYNOPSIS
#include <nl_types.h>
char *catgets(nl_catd catd, int set_id, int msg_id, const char *s);
DESCRIPTION
The catgets() function shall attempt to read message msg_id, in set
set_id, from the message catalog identified by catd. The catd argument is
a message catalog descriptor returned from an earlier call to catopen().
The s argument points to a default message string which shall be returned
by catgets() if it cannot retrieve the identified message.
The catgets() function need not be reentrant. A function that is not
required to be reentrant is not required to be thread-safe.
RETURN VALUE
If the identified message is retrieved successfully, catgets() shall
return a pointer to an internal buffer area containing the null-terminated
message string. If the call is unsuccessful for any reason, s shall be
returned and errno may be set to indicate the error.
ERRORS
The catgets() function may fail if:
[EBADF]
The catd argument is not a valid message catalog descriptor open for
reading.
[EBADMSG]
The message identified by set_id and msg_id in the specified message
catalog did not satisfy implementation-defined security criteria.
[EINTR]
The read operation was terminated due to the receipt of a signal, and
no data was transferred.
[EINVAL]
The message catalog identified by catd is corrupted.
[ENOMSG]
The message identified by set_id and msg_id is not in the message catalog.
*/
char *(catgets) (nl_catd catd, int set_id, int msg_id, const char *s)
{
_CAT_CATALOG_T *cat;
static _CAT_MESSAGE_T keymsg = { 0, 0, 0 };
_CAT_MESSAGE_T *msgptr = 0;
if (theCatalogues == 0 || _CATCAT_length (theCatalogues) <= catd
|| (cat = _CATCAT_get_at (theCatalogues, catd))->is_opened == 0)
{
errno = EBADF;
return (char *) s;
}
keymsg.set_id = set_id;
keymsg.msg_id = msg_id;
msgptr =
bsearch (&keymsg, _CATMSG_base (cat->msgs), _CATMSG_length (cat->msgs),
sizeof (_CAT_MESSAGE_T), catmsg_cmp);
if (msgptr == 0)
{
#ifdef ENOMSG
errno = ENOMSG;
#endif
return (char *) s;
}
return DScstr (msgptr->msg);
}
/*
NAME
catclose - close a message catalog descriptor
SYNOPSIS
#include <nl_types.h>
int catclose(nl_catd catd);
DESCRIPTION
The catclose() function shall close the message catalog identified by
catd. If a file descriptor is used to implement the type nl_catd, that
file descriptor shall be closed.
RETURN VALUE
Upon successful completion, catclose() shall return 0; otherwise, -1
shall be returned, and errno set to indicate the error.
ERRORS
The catclose() function may fail if:
[EBADF]
The catalog descriptor is not valid.
[EINTR]
The catclose() function was interrupted by a signal.
*/
int (catclose) (nl_catd catd)
{
_CAT_CATALOG_T *cat;
if (theCatalogues == 0 || _CATCAT_length (theCatalogues) <= catd
|| (cat = _CATCAT_get_at (theCatalogues, catd))->is_opened == 0)
{
errno = EBADF;
return -1;
}
cat->is_opened = 0;
_CATMSG_resize (cat->msgs, 0, 0);
return 0;
}
/* END OF FILE */