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 if (currentEncoding == NULL) {
59 currentEncoding = strdup(newenc);
60 if (currentEncoding == NULL)
61 errx(1, "set_new_encoding: cannot allocate memory");
64 if (strcmp(currentEncoding, newenc) == 0)
66 free(currentEncoding);
67 currentEncoding = strdup(newenc);
68 if (currentEncoding == NULL)
69 errx(1, "set_new_encoding: cannot allocate memory");
70 if (conv != (iconv_t) -1) {
82 size_t inleft, outleft, converted = 0;
87 if (currentEncoding == NULL) {
88 output = strdup(input);
90 errx(1, "convert: cannot allocate memory");
93 if (conv == (iconv_t)-1) {
94 conv = iconv_open(outputEncoding, currentEncoding);
95 if (conv == (iconv_t)-1) {
97 errx(1, "Conversion is not supported");
99 err(1, "Initialization failure");
103 inleft = strlen(input);
107 if ((output = malloc(outlen + 1)) == NULL)
108 errx(1, "convert: cannot allocate memory");
112 outbuf = output + converted;
113 outleft = outlen - converted;
115 converted = iconv(conv, (char **) &inbuf, &inleft, &outbuf, &outleft);
116 if (converted != (size_t) -1 || errno == EINVAL) {
117 /* finished or invalid multibyte, so truncate and ignore */
121 if (errno != E2BIG) {
126 converted = outbuf - output;
127 outlen += inleft * 2;
129 if ((tmp = realloc(output, outlen + 1)) == NULL) {
131 errx(1, "convert: cannot allocate memory");
135 outbuf = output + converted;
138 /* flush the iconv conversion */
139 iconv(conv, NULL, NULL, &outbuf, &outleft);
141 /* null terminate the string */
144 output = strdup(input);
146 errx(1, "convert: cannot allocate memory");
153 event_add(int year, int month, int day, char *date, int var, char *txt,
159 * Creating a new event:
160 * - Create a new event
161 * - Copy the machine readable day and month
162 * - Copy the human readable and language specific date
163 * - Copy the text of the event
165 e = (struct event *)calloc(1, sizeof(struct event));
167 errx(1, "event_add: cannot allocate memory");
171 e->date = convert(date);
173 errx(1, "event_add: cannot allocate memory");
174 e->text = convert(txt);
176 errx(1, "event_add: cannot allocate memory");
178 if (extra != NULL && extra[0] != '\0')
179 e->extra = convert(extra);
180 addtodate(e, year, month, day);
185 event_continue(struct event *e, char *txt)
187 char *oldtext, *text;
192 errx(1, "event_continue: cannot allocate memory");
194 asprintf(&e->text, "%s\n%s", oldtext, text);
196 errx(1, "event_continue: cannot allocate memory");
204 event_print_all(FILE *fp)
208 while (walkthrough_dates(&e) != 0) {
210 fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
215 * Go through all events and print the text of the matching
219 (void)fprintf(fp, "%s%c%s%s%s%s\n", e->date,
220 e->var ? '*' : ' ', e->text,
221 e->extra != NULL ? " (" : "",
222 e->extra != NULL ? e->extra : "",
223 e->extra != NULL ? ")" : ""