1 /* 2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 3 * Copyright 2015 John Marino <draco@marino.st> 4 * 5 * This source code is derived from the illumos localedef command, and 6 * provided under BSD-style license terms by Nexenta Systems, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * LC_TIME database generation routines for localedef. 33 */ 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <errno.h> 38 #include <sys/types.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include "localedef.h" 42 #include "parser.h" 43 #include "timelocal.h" 44 45 struct lc_time_T tm; 46 47 void 48 init_time(void) 49 { 50 (void) memset(&tm, 0, sizeof (tm)); 51 } 52 53 void 54 add_time_str(wchar_t *wcs) 55 { 56 char *str; 57 58 if ((str = to_mb_string(wcs)) == NULL) { 59 INTERR; 60 return; 61 } 62 free(wcs); 63 64 switch (last_kw) { 65 case T_D_T_FMT: 66 tm.c_fmt = str; 67 break; 68 case T_D_FMT: 69 tm.x_fmt = str; 70 break; 71 case T_T_FMT: 72 tm.X_fmt = str; 73 break; 74 case T_T_FMT_AMPM: 75 tm.ampm_fmt = str; 76 break; 77 case T_DATE_FMT: 78 /* 79 * This one is a Solaris extension, Too bad date just 80 * doesn't use %c, which would be simpler. 81 */ 82 tm.date_fmt = str; 83 break; 84 case T_ERA_D_FMT: 85 case T_ERA_T_FMT: 86 case T_ERA_D_T_FMT: 87 /* Silently ignore it. */ 88 break; 89 default: 90 free(str); 91 INTERR; 92 break; 93 } 94 } 95 96 static void 97 add_list(const char *ptr[], char *str, int limit) 98 { 99 int i; 100 for (i = 0; i < limit; i++) { 101 if (ptr[i] == NULL) { 102 ptr[i] = str; 103 return; 104 } 105 } 106 fprintf(stderr,"too many list elements"); 107 } 108 109 void 110 add_time_list(wchar_t *wcs) 111 { 112 char *str; 113 114 if ((str = to_mb_string(wcs)) == NULL) { 115 INTERR; 116 return; 117 } 118 free(wcs); 119 120 switch (last_kw) { 121 case T_ABMON: 122 add_list(tm.mon, str, 12); 123 break; 124 case T_MON: 125 add_list(tm.month, str, 12); 126 break; 127 case T_ABDAY: 128 add_list(tm.wday, str, 7); 129 break; 130 case T_DAY: 131 add_list(tm.weekday, str, 7); 132 break; 133 case T_AM_PM: 134 if (tm.am == NULL) { 135 tm.am = str; 136 } else if (tm.pm == NULL) { 137 tm.pm = str; 138 } else { 139 fprintf(stderr,"too many list elements"); 140 } 141 break; 142 case T_ALT_DIGITS: 143 case T_ERA: 144 free(str); 145 break; 146 default: 147 free(str); 148 INTERR; 149 break; 150 } 151 } 152 153 void 154 check_time_list(void) 155 { 156 switch (last_kw) { 157 case T_ABMON: 158 if (tm.mon[11] != NULL) 159 return; 160 break; 161 case T_MON: 162 if (tm.month[11] != NULL) 163 return; 164 break; 165 case T_ABDAY: 166 if (tm.wday[6] != NULL) 167 return; 168 break; 169 case T_DAY: 170 if (tm.weekday[6] != NULL) 171 return; 172 break; 173 case T_AM_PM: 174 if (tm.pm != NULL) 175 return; 176 break; 177 case T_ERA: 178 case T_ALT_DIGITS: 179 return; 180 default: 181 fprintf(stderr,"unknown list"); 182 break; 183 } 184 185 fprintf(stderr,"too few items in list (%d)", last_kw); 186 } 187 188 #pragma GCC diagnostic push 189 #pragma GCC diagnostic ignored "-Wcast-qual" 190 191 void 192 reset_time_list(void) 193 { 194 int i; 195 switch (last_kw) { 196 case T_ABMON: 197 for (i = 0; i < 12; i++) { 198 free((char *)tm.mon[i]); 199 tm.mon[i] = NULL; 200 } 201 break; 202 case T_MON: 203 for (i = 0; i < 12; i++) { 204 free((char *)tm.month[i]); 205 tm.month[i] = NULL; 206 } 207 break; 208 case T_ABDAY: 209 for (i = 0; i < 7; i++) { 210 free((char *)tm.wday[i]); 211 tm.wday[i] = NULL; 212 } 213 break; 214 case T_DAY: 215 for (i = 0; i < 7; i++) { 216 free((char *)tm.weekday[i]); 217 tm.weekday[i] = NULL; 218 } 219 break; 220 case T_AM_PM: 221 free((char *)tm.am); 222 tm.am = NULL; 223 free((char *)tm.pm); 224 tm.pm = NULL; 225 break; 226 } 227 } 228 229 #pragma GCC diagnostic pop 230 231 void 232 dump_time(void) 233 { 234 FILE *f; 235 int i; 236 237 if ((f = open_category()) == NULL) { 238 return; 239 } 240 241 for (i = 0; i < 12; i++) { 242 if (putl_category(tm.mon[i], f) == EOF) { 243 return; 244 } 245 } 246 for (i = 0; i < 12; i++) { 247 if (putl_category(tm.month[i], f) == EOF) { 248 return; 249 } 250 } 251 for (i = 0; i < 7; i++) { 252 if (putl_category(tm.wday[i], f) == EOF) { 253 return; 254 } 255 } 256 for (i = 0; i < 7; i++) { 257 if (putl_category(tm.weekday[i], f) == EOF) { 258 return; 259 } 260 } 261 262 /* 263 * NOTE: If date_fmt is not specified, then we'll default to 264 * using the %c for date. This is reasonable for most 265 * locales, although for reasons that I don't understand 266 * Solaris historically has had a seperate format for date. 267 */ 268 if ((putl_category(tm.X_fmt, f) == EOF) || 269 (putl_category(tm.x_fmt, f) == EOF) || 270 (putl_category(tm.c_fmt, f) == EOF) || 271 (putl_category(tm.am, f) == EOF) || 272 (putl_category(tm.pm, f) == EOF) || 273 (putl_category(tm.date_fmt ? tm.date_fmt : tm.c_fmt, f) == EOF) || 274 (putl_category(tm.ampm_fmt, f) == EOF)) { 275 return; 276 } 277 close_category(f); 278 } 279