]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/calendar/events.c
Fix length calculation in memmove
[FreeBSD/FreeBSD.git] / usr.bin / calendar / events.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  * 
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
26  * SUCH DAMAGE.
27  * 
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/time.h>
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef WITH_ICONV
39 #include <iconv.h>
40 #include <errno.h>
41 #include <langinfo.h>
42
43 static iconv_t conv = (iconv_t)-1;
44 static char *currentEncoding = NULL;
45
46 #endif
47
48 #include "pathnames.h"
49 #include "calendar.h"
50
51 #ifdef WITH_ICONV
52 void
53 set_new_encoding(void)
54 {
55         const char *newenc;
56
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");
63                 return;
64         }
65         if (strcmp(currentEncoding, newenc) == 0)
66                 return;
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) {
72                 iconv_close(conv);
73                 conv = (iconv_t) -1;
74         }
75 }
76 #endif
77
78 static char *
79 convert(char *input)
80 {
81         char *output;
82 #ifdef WITH_ICONV
83         size_t inleft, outleft, converted = 0;
84         char *outbuf, *tmp;
85         char *inbuf;
86         size_t outlen;
87
88         if (currentEncoding == NULL) {
89                 output = strdup(input);
90                 if (output == NULL)
91                         errx(1, "convert: cannot allocate memory");
92                 return (output);
93         }
94         if (conv == (iconv_t)-1) {
95                 conv = iconv_open(outputEncoding, currentEncoding);
96                 if (conv == (iconv_t)-1) {
97                         if (errno == EINVAL)
98                                 errx(1, "Conversion is not supported");
99                         else
100                                 err(1, "Initialization failure");
101                 }
102         fprintf(stderr, "CONV=%p\n", conv); // DEBUG
103         }
104
105         inleft = strlen(input);
106         inbuf = input;
107
108         outlen = inleft + 3;
109         if ((output = malloc(outlen)) == NULL)
110                 errx(1, "convert: cannot allocate memory");
111
112         for (;;) {
113                 errno = 0;
114                 outbuf = output + converted;
115                 outleft = outlen - converted;
116
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 */
122                         break;
123                 }
124
125                 if (errno != E2BIG) {
126                         free(output);
127                         err(1, "convert");
128                 }
129
130                 converted = outbuf - output;
131                 outlen += inleft * 2;
132
133                 if ((tmp = realloc(output, outlen + 1)) == NULL) {
134                         free(output);
135                         errx(1, "convert: cannot allocate memory");
136                 }
137
138                 output = tmp;
139                 outbuf = output + converted;
140         }
141
142         /* flush the iconv conversion */
143         iconv(conv, NULL, NULL, &outbuf, &outleft);
144
145         /* null terminate the string */
146         *outbuf = '\0';
147 #else
148         output = strdup(input);
149         if (output == NULL)
150                 errx(1, "convert: cannot allocate memory");
151 #endif
152
153         return (output);
154 }
155
156 struct event *
157 event_add(int year, int month, int day, char *date, int var, char *txt,
158     char *extra)
159 {
160         struct event *e;
161
162         /*
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
168          */
169         e = (struct event *)calloc(1, sizeof(struct event));
170         if (e == NULL)
171                 errx(1, "event_add: cannot allocate memory");
172         e->month = month;
173         e->day = day;
174         e->var = var;
175         e->date = convert(date);
176         if (e->date == NULL)
177                 errx(1, "event_add: cannot allocate memory");
178         e->text = convert(txt);
179         if (e->text == NULL)
180                 errx(1, "event_add: cannot allocate memory");
181         e->extra = NULL;
182         if (extra != NULL && extra[0] != '\0')
183                 e->extra = convert(extra);
184         addtodate(e, year, month, day);
185         return (e);
186 }
187
188 void
189 event_continue(struct event *e, char *txt)
190 {
191         char *oldtext, *text;
192
193         text = convert(txt);
194         oldtext = e->text;
195         if (oldtext == NULL)
196                 errx(1, "event_continue: cannot allocate memory");
197
198         asprintf(&e->text, "%s\n%s", oldtext, text);
199         if (e->text == NULL)
200                 errx(1, "event_continue: cannot allocate memory");
201         free(oldtext);
202         free(text);
203
204         return;
205 }
206
207 void
208 event_print_all(FILE *fp)
209 {
210         struct event *e;
211
212         while (walkthrough_dates(&e) != 0) {
213 #ifdef DEBUG
214                 if (e)
215                         fprintf(stderr, "event_print_all month: %d, day: %d\n",
216                             e->month, e->day);
217 #endif
218
219                 /*
220                  * Go through all events and print the text of the matching
221                  * dates
222                  */
223                 while (e != NULL) {
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 ? ")" : ""
229                         );
230
231                         e = e->next;
232                 }
233         }
234 }