2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
43 static iconv_t conv = (iconv_t)-1;
44 static char *currentEncoding = NULL;
48 #include "pathnames.h"
53 set_new_encoding(void)
57 newenc = nl_langinfo(CODESET);
58 fprintf(stderr, "NEWENC=%s\n", newenc); // DEBUG
59 if (currentEncoding == NULL) {
60 currentEncoding = strdup(newenc);
61 if (currentEncoding == NULL)
62 errx(1, "set_new_encoding: cannot allocate memory");
65 if (strcmp(currentEncoding, newenc) == 0)
67 free(currentEncoding);
68 currentEncoding = strdup(newenc);
69 if (currentEncoding == NULL)
70 errx(1, "set_new_encoding: cannot allocate memory");
71 if (conv != (iconv_t) -1) {
83 size_t inleft, outleft, converted = 0;
88 if (currentEncoding == NULL) {
89 output = strdup(input);
91 errx(1, "convert: cannot allocate memory");
94 if (conv == (iconv_t)-1) {
95 conv = iconv_open(outputEncoding, currentEncoding);
96 if (conv == (iconv_t)-1) {
98 errx(1, "Conversion is not supported");
100 err(1, "Initialization failure");
102 fprintf(stderr, "CONV=%p\n", conv); // DEBUG
105 inleft = strlen(input);
109 if ((output = malloc(outlen)) == NULL)
110 errx(1, "convert: cannot allocate memory");
114 outbuf = output + converted;
115 outleft = outlen - converted;
117 fprintf(stderr, "-< %s %p %ld %ld\n", inbuf, outbuf, inleft, outleft); // DEBUG
118 converted = iconv(conv, (char **) &inbuf, &inleft, &outbuf, &outleft);
119 fprintf(stderr, "-> %ld %s %p %ld %ld\n", converted, inbuf, outbuf, inleft, outleft); // DEBUG
120 if (converted != (size_t) -1 || errno == EINVAL) {
121 /* finished or invalid multibyte, so truncate and ignore */
125 if (errno != E2BIG) {
130 converted = outbuf - output;
131 outlen += inleft * 2;
133 if ((tmp = realloc(output, outlen + 1)) == NULL) {
135 errx(1, "convert: cannot allocate memory");
139 outbuf = output + converted;
142 /* flush the iconv conversion */
143 iconv(conv, NULL, NULL, &outbuf, &outleft);
145 /* null terminate the string */
148 output = strdup(input);
150 errx(1, "convert: cannot allocate memory");
157 event_add(int year, int month, int day, char *date, int var, char *txt,
163 * Creating a new event:
164 * - Create a new event
165 * - Copy the machine readable day and month
166 * - Copy the human readable and language specific date
167 * - Copy the text of the event
169 e = (struct event *)calloc(1, sizeof(struct event));
171 errx(1, "event_add: cannot allocate memory");
175 e->date = convert(date);
177 errx(1, "event_add: cannot allocate memory");
178 e->text = convert(txt);
180 errx(1, "event_add: cannot allocate memory");
182 if (extra != NULL && extra[0] != '\0')
183 e->extra = convert(extra);
184 addtodate(e, year, month, day);
189 event_continue(struct event *e, char *txt)
191 char *oldtext, *text;
196 errx(1, "event_continue: cannot allocate memory");
198 asprintf(&e->text, "%s\n%s", oldtext, text);
200 errx(1, "event_continue: cannot allocate memory");
208 event_print_all(FILE *fp)
212 while (walkthrough_dates(&e) != 0) {
215 fprintf(stderr, "event_print_all month: %d, day: %d\n",
220 * Go through all events and print the text of the matching
224 (void)fprintf(fp, "%s%c%s%s%s%s\n", e->date,
225 e->var ? '*' : ' ', e->text,
226 e->extra != NULL ? " (" : "",
227 e->extra != NULL ? e->extra : "",
228 e->extra != NULL ? ")" : ""