]> git.pld-linux.org Git - packages/busybox.git/blob - busybox-printf-gettext.patch
35642459833fbc093c8bddd2bd3a8b134ecd14ee
[packages/busybox.git] / busybox-printf-gettext.patch
1 --- busybox-1.3.1/coreutils/printf.c.orig       2006-12-27 05:54:50.000000000 +0100
2 +++ busybox-1.3.1/coreutils/printf.c    2006-12-30 12:28:27.605589427 +0100
3 @@ -38,6 +38,9 @@
4  
5  //   19990508 Busy Boxed! Dave Cinege
6  
7 +// on by default
8 +#define BB_FEATURE_PRINTF_GETTEXT
9 +
10  #include "busybox.h"
11  
12  static int print_formatted(char *format, int argc, char **argv);
13 @@ -288,19 +288,135 @@
14         return save_argc - argc;
15  }
16  
17 +/*
18 + * Very pure gettext added by Michal Moskal <malekith@pld-linux.org>
19 + * This possibly could be converted into utility function
20 + * and used in other places as well.
21 + */
22 +
23 +#ifdef BB_FEATURE_PRINTF_GETTEXT
24 +/* The magic number of the GNU message catalog format.  */
25 +#define _MAGIC 0x950412de
26 +
27 +/* Header for binary .mo file format.  */
28 +struct mo_file_header
29 +{
30 +       /* The magic number.  */
31 +       u_int32_t magic;
32 +       /* The revision number of the file format.  */
33 +       u_int32_t revision;
34 +       /* The number of strings pairs.  */
35 +       u_int32_t nstrings;
36 +       /* Offset of table with start offsets of original strings.  */
37 +       u_int32_t orig_tab_offset;
38 +       /* Offset of table with start offsets of translation strings.  */
39 +       u_int32_t trans_tab_offset;
40 +       /* Size of hashing table.  */
41 +       u_int32_t hash_tab_size;
42 +       /* Offset of first hashing entry.  */
43 +       u_int32_t hash_tab_offset;
44 +};
45 +
46 +struct string_desc
47 +{
48 +       /* Length of addressed string.  */
49 +       u_int32_t length;
50 +       /* Offset of string in file.  */
51 +       u_int32_t offset;
52 +};
53 +
54 +static u_int32_t swap(u_int32_t i)
55 +{
56 +       return (i << 24) | ((i & 0xff00) << 8) |
57 +               ((i >> 8) & 0xff00) | (i >> 24);
58 +}
59 +#define swap_if(a) ((has_to_swap) ? swap(a) : (a))
60 +
61 +static char *getmsg(const char *filename, const char *msgid)
62 +{
63 +       int fd;
64 +       struct mo_file_header *ptr;
65 +       struct stat st;
66 +       int has_to_swap;
67 +       size_t top, bottom;
68 +       struct string_desc *orig_tab, *trans_tab = NULL;
69 +       int act = -1;
70 +       char *ret = (char*)msgid;
71 +
72 +       if (filename == NULL || stat(filename, &st))
73 +               return ret;
74 +
75 +       fd = open(filename, O_RDONLY);
76 +       if (fd == -1)
77 +               return ret;
78 +
79 +       ptr = (struct mo_file_header *) mmap(NULL, st.st_size, PROT_READ,
80 +                       MAP_PRIVATE, fd, 0);
81 +       close(fd);
82 +
83 +       if (ptr == (void*)-1)
84 +               return ret;
85 +
86 +       has_to_swap = ptr->magic != _MAGIC;
87 +
88 +       if (swap_if(ptr->magic) != _MAGIC)
89 +               goto oops;
90 +
91 +       /* FIXME: use hash table */
92 +
93 +       orig_tab = (struct string_desc *)
94 +               ((char *) ptr + swap_if(ptr->orig_tab_offset));
95 +       trans_tab = (struct string_desc *)
96 +               ((char *) ptr + swap_if(ptr->trans_tab_offset));
97 +
98 +       bottom = 0;
99 +       top = swap_if(ptr->nstrings);
100 +       while (bottom < top) {
101 +               int cmp_val;
102 +               act = (bottom + top) / 2;
103 +               cmp_val =
104 +                       strcmp(msgid,
105 +                                       ((char *) ptr + swap_if(orig_tab[act].offset)));
106 +               if (cmp_val < 0)
107 +                       top = act;
108 +               else if (cmp_val > 0)
109 +                       bottom = act + 1;
110 +               else
111 +                       break;
112 +               act = -1;
113 +       }
114 +
115 +oops:
116 +       if (act != -1)
117 +               ret = strdup(((char *) ptr + swap_if(trans_tab[act].offset)));
118 +       munmap(ptr, st.st_size);
119 +       return ret;
120 +}
121 +#else
122 +# define getmsg(a,b) (b)
123 +#endif
124 +
125  int printf_main(int argc, char **argv);
126  int printf_main(int argc, char **argv)
127  {
128         char *format;
129 -       int args_used;
130 +       int args_used, opt;
131 +       const char *nls_file = NULL;
132  
133 -       if (argc <= 1 || argv[1][0] == '-') {
134 -               bb_show_usage();
135 -       }
136 +       while ((opt = getopt(argc, argv, "n:")) != -1)
137 +               switch (opt) {
138 +                       case 'n':
139 +                               nls_file = optarg;
140 +                               break;
141 +                       default:
142 +                               bb_show_usage();
143 +                               break;
144 +               }
145 +
146 +       format = getmsg(nls_file, argv[optind++]);
147  
148 -       format = argv[1];
149 -       argc -= 2;
150 -       argv += 2;
151 +       argc -= optind;
152 +       argv += optind;
153  
154         do {
155                 args_used = print_formatted(format, argc, argv);
This page took 0.026809 seconds and 2 git commands to generate.