summaryrefslogtreecommitdiff
path: root/poptint.c
blob: 008d4639960f8c9a8fa1ad5efcfb48fb578927aa (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
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
#include "system.h"
#include <stdarg.h>
#include "poptint.h"

#if defined(HAVE_ASSERT_H)
#include <assert.h>
#else
#define assert(_x)
#endif

/* Any pair of 32 bit hashes can be used. lookup3.c generates pairs, will do. */
#define _JLU3_jlu32lpair        1
#define	jlu32lpair	poptJlu32lpair
#include "lookup3.c"

/*@-varuse +charint +ignoresigns @*/
/*@unchecked@*/ /*@observer@*/
static const unsigned char utf8_skip_data[256] = {
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
};
/*@=varuse =charint =ignoresigns @*/

const char *
POPT_prev_char (const char *str)
{
    const char *p = str;

    while (1) {
	p--;
	if (((unsigned)*p & 0xc0) != (unsigned)0x80)
	    return p;
    }
}

const char *
POPT_next_char (const char *str)
{
    const char *p = str;

    while (*p != '\0') {
	p++;
	if (((unsigned)*p & 0xc0) != (unsigned)0x80)
	    break;
    }
    return p;
}

#if !defined(POPT_fprintf)	/* XXX lose all the goop ... */

#if defined(HAVE_DCGETTEXT) && !defined(__LCLINT__)
/*
 * Rebind a "UTF-8" codeset for popt's internal use.
 */
char *
POPT_dgettext(const char * dom, const char * str)
{
    char * codeset = NULL;
    char * retval = NULL;

    if (!dom)
	dom = textdomain(NULL);
    codeset = bind_textdomain_codeset(dom, NULL);
    bind_textdomain_codeset(dom, "UTF-8");
    retval = dgettext(dom, str);
    bind_textdomain_codeset(dom, codeset);

    return retval;
}
#endif

#ifdef HAVE_ICONV
/**
 * Return malloc'd string converted from UTF-8 to current locale.
 * @param istr		input string (UTF-8 encoding assumed)
 * @return		localized string
 */
static /*@only@*/ /*@null@*/ char *
strdup_locale_from_utf8 (/*@null@*/ char * istr)
	/*@*/
{
    char * codeset = NULL;
    char * ostr = NULL;
    iconv_t cd;

    if (istr == NULL)
	return NULL;

#ifdef HAVE_LANGINFO_H
    codeset = nl_langinfo ((nl_item)CODESET);
#endif

    if (codeset != NULL && strcmp(codeset, "UTF-8") != 0
     && (cd = iconv_open(codeset, "UTF-8")) != (iconv_t)-1)
    {
	char * shift_pin = NULL;
	size_t db = strlen(istr);
/*@owned@*/
	char * dstr = (char*) xmalloc((db + 1) * sizeof(*dstr));
	char * pin = istr;
	char * pout = dstr;
	size_t ib = db;
	size_t ob = db;
	size_t err;

assert(dstr);	/* XXX can't happen */
	if (dstr == NULL)
	    return NULL;
	err = iconv(cd, NULL, NULL, NULL, NULL);
	while (1) {
	    *pout = '\0';
	    err = iconv(cd, &pin, &ib, &pout, &ob);
	    if (err != (size_t)-1) {
		if (shift_pin == NULL) {
		    shift_pin = pin;
		    pin = NULL;
		    ib = 0;
		    continue;
		}
	    } else
	    switch (errno) {
	    case E2BIG:
	    {	size_t used = (size_t)(pout - dstr);
		db *= 2;
		dstr = (char*) xrealloc(dstr, (db + 1) * sizeof(*dstr));
assert(dstr);	/* XXX can't happen */
		if (dstr != NULL) {
		    pout = dstr + used;
		    ob = db - used;
		    continue;
		}
	    }   /*@switchbreak@*/ break;
	    case EINVAL:
	    case EILSEQ:
	    default:
		/*@switchbreak@*/ break;
	    }
	    break;
	}
	(void) iconv_close(cd);
	*pout = '\0';
	ostr = xstrdup(dstr ? dstr : istr);
	free(dstr);
    } else
	ostr = xstrdup(istr);

    return ostr;
}
#endif

int
POPT_fprintf (FILE * stream, const char * format, ...)
{
    char * b = NULL;
    int rc;
    va_list ap;

#if defined(HAVE_VASPRINTF) && !defined(__LCLINT__)
    va_start(ap, format);
    if ((rc = vasprintf(&b, format, ap)) < 0)
	b = NULL;
    va_end(ap);
#else
    size_t nb = (size_t)1;

    /* HACK: add +1 to the realloc no. of bytes "just in case". */
    /* XXX Likely unneeded, the issues wrto vsnprintf(3) return b0rkage have
     * to do with whether the final '\0' is counted (or not). The code
     * below already adds +1 for the (possibly already counted) trailing NUL.
     */
    while ((b = (char*) xrealloc(b, nb+1)) != NULL) {
	va_start(ap, format);
	rc = vsnprintf(b, nb, format, ap);
	va_end(ap);
	if (rc > -1) {	/* glibc 2.1 */
	    if ((size_t)rc < nb)
		break;
	    nb = (size_t)(rc + 1);	/* precise buffer length known */
	} else 		/* glibc 2.0 */
	    nb += (nb < (size_t)100 ? (size_t)100 : nb);
	ob = b;
    }
#endif

    rc = 0;
    if (b != NULL) {
#ifdef HAVE_ICONV
    	char * ob = NULL;
	ob = strdup_locale_from_utf8(b);
	if (ob != NULL) {
	    rc = fprintf(stream, "%s", ob);
	    free(ob);
	} else
#endif
	    rc = fprintf(stream, "%s", b);
	free (b);
    }

    return rc;
}

#endif	/* !defined(POPT_fprintf) */