summaryrefslogtreecommitdiff
path: root/src/msgs.c
blob: eaa62767597da1ba13b8b9f4af9e5472f58dca02 (plain) (blame)
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
#include <s.h>

#include <assert.h>
#include <errno.h>
#include <nl_types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "logerr.h"
#include "meta.h"

#include "msgs.h"



static const char
CATALOG_NAME[] = NAME;

static nl_catd
catalog_descriptor = NULL;

static size_t
msgs_length = 0U;

static const char
NLSPATH[] = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":"
	LOCALEDIR "/%l/LC_MESSAGES/%N.cat";

static const char
NLSPATH_KEY[] = "NLSPATH";



int
msgs_init(const char *const MSGS[]) {
	int rc = -1;

	static const int should_overwrite = 0;
	if (setenv(NLSPATH_KEY, NLSPATH, should_overwrite)) {
		logerr("setenv(): %s", strerror(errno));
		goto out;
	}

	catalog_descriptor = catopen(CATALOG_NAME, 0);
	if (catalog_descriptor != NULL && catalog_descriptor == (nl_catd)-1) {
		logerr("catopen(): %s", strerror(errno));
		catalog_descriptor = NULL;
		goto out;
	}

	msgs_length = 0U;
	while (MSGS[msgs_length] != NULL) {
		msgs_length++;
	}

	rc = 0;
out:
	return rc;
}

int
msgs_end(void) {
	int rc = -1;

	if (catalog_descriptor != NULL) {
		if (catclose(catalog_descriptor)) {
			logerr("catclose(): %s", strerror(errno));
			goto out;
		}
	}

	rc = 0;
out:
	if (catalog_descriptor != NULL) {
		catalog_descriptor = NULL;
	}
	return rc;
}

const char *
msgs_string(const char* const MSGS[], const int msg_id) {
	assert(msg_id >= 0);
	if (msgs_length != 0U) {
		assert((size_t)msg_id < msgs_length);
	}
	if (catalog_descriptor == NULL) {
		return MSGS[msg_id];
	}

	errno = 0;
	const char *const ret =
		catgets(catalog_descriptor, NL_SETD, msg_id, MSGS[msg_id]);
	if (errno) {
		logerr("catgets(): %s", strerror(errno));
	}

	return ret;
}

int
msgs_print_ids(
	const char *const MSGS[],
	FILE *restrict stream,
	const int msg_begin,
	const int msg_end
) {
	int rc = -1;

	for (int i = msg_begin; i <= msg_end; i++) {
		if (fprintf(stream, "%s", msgs_string(MSGS, i)) < 0) {
			logerr("fprintf(): %s", strerror(errno));
			goto out;
		}
	}

	rc = 0;
out:
	return rc;
}

int
msgs_print_id(const char *const MSGS[], FILE *const stream, const int msg_id) {
	return msgs_print_ids(MSGS, stream, msg_id, msg_id);
}

int
msgs_dump_translatable_strings(const char *const MSGS[]) {
	int rc = -1;

	for (size_t i = 0U; MSGS[i] != NULL; i++) {
		if (printf("%ld ", i) < 0) {
			logerr("printf(): %s", strerror(errno));
			goto out;
		}

		for (size_t j = 0U; MSGS[i][j]; j++) {
			if (MSGS[i][j] == '\n') {
				if (printf("\\n") < 0) {
					logerr("printf(): %s", strerror(errno));
					goto out;
				}
			} else {
				if (printf("%c", MSGS[i][j]) < 0) {
					logerr("printf(): %s", strerror(errno));
					goto out;
				}
			}
		}

		if (printf("\n\n") < 0) {
			logerr("printf(): %s", strerror(errno));
			goto out;
		}
	}

	rc = 0;
out:
	return rc;
}