10cb2e609SWolfgang Helbig /*- 20cb2e609SWolfgang Helbig * Copyright (c) 1997 Wolfgang Helbig 30cb2e609SWolfgang Helbig * All rights reserved. 40cb2e609SWolfgang Helbig * 50cb2e609SWolfgang Helbig * Redistribution and use in source and binary forms, with or without 60cb2e609SWolfgang Helbig * modification, are permitted provided that the following conditions 70cb2e609SWolfgang Helbig * are met: 80cb2e609SWolfgang Helbig * 1. Redistributions of source code must retain the above copyright 90cb2e609SWolfgang Helbig * notice, this list of conditions and the following disclaimer. 100cb2e609SWolfgang Helbig * 2. Redistributions in binary form must reproduce the above copyright 110cb2e609SWolfgang Helbig * notice, this list of conditions and the following disclaimer in the 120cb2e609SWolfgang Helbig * documentation and/or other materials provided with the distribution. 130cb2e609SWolfgang Helbig * 140cb2e609SWolfgang Helbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150cb2e609SWolfgang Helbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160cb2e609SWolfgang Helbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170cb2e609SWolfgang Helbig * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180cb2e609SWolfgang Helbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190cb2e609SWolfgang Helbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200cb2e609SWolfgang Helbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210cb2e609SWolfgang Helbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220cb2e609SWolfgang Helbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230cb2e609SWolfgang Helbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240cb2e609SWolfgang Helbig * SUCH DAMAGE. 250cb2e609SWolfgang Helbig */ 26a99e4564SPhilippe Charnier 27a99e4564SPhilippe Charnier #ifndef lint 28a99e4564SPhilippe Charnier static const char rcsid[] = 29c3aac50fSPeter Wemm "$FreeBSD$"; 30a99e4564SPhilippe Charnier #endif /* not lint */ 31a99e4564SPhilippe Charnier 320cb2e609SWolfgang Helbig #include <calendar.h> 33821df508SXin LI #include <ctype.h> 340cb2e609SWolfgang Helbig #include <err.h> 35f5e40af2SAndrey A. Chernov #include <langinfo.h> 360cb2e609SWolfgang Helbig #include <locale.h> 370cb2e609SWolfgang Helbig #include <stdio.h> 380cb2e609SWolfgang Helbig #include <stdlib.h> 390cb2e609SWolfgang Helbig #include <string.h> 400cb2e609SWolfgang Helbig #include <sysexits.h> 410cb2e609SWolfgang Helbig #include <time.h> 420cb2e609SWolfgang Helbig #include <unistd.h> 434646cea7SDavid Schultz #include <wchar.h> 444646cea7SDavid Schultz #include <wctype.h> 45e454a171SRoman Divacky #include <term.h> 46e454a171SRoman Divacky #undef lines /* term.h defines this */ 470cb2e609SWolfgang Helbig 480851fbdfSEdwin Groothuis /* Width of one month with backward compatibility and in regular mode*/ 490cb2e609SWolfgang Helbig #define MONTH_WIDTH_B_J 27 500cb2e609SWolfgang Helbig #define MONTH_WIDTH_B 20 510cb2e609SWolfgang Helbig 520851fbdfSEdwin Groothuis #define MONTH_WIDTH_R_J 24 530851fbdfSEdwin Groothuis #define MONTH_WIDTH_R 18 540cb2e609SWolfgang Helbig 55e454a171SRoman Divacky #define MAX_WIDTH 64 560cb2e609SWolfgang Helbig 570cb2e609SWolfgang Helbig typedef struct date date; 580cb2e609SWolfgang Helbig 590cb2e609SWolfgang Helbig struct monthlines { 604646cea7SDavid Schultz wchar_t name[MAX_WIDTH + 1]; 610cb2e609SWolfgang Helbig char lines[7][MAX_WIDTH + 1]; 620cb2e609SWolfgang Helbig char weeks[MAX_WIDTH + 1]; 631d0e1dacSEdwin Groothuis unsigned int extralen[7]; 640cb2e609SWolfgang Helbig }; 650cb2e609SWolfgang Helbig 660cb2e609SWolfgang Helbig struct weekdays { 674646cea7SDavid Schultz wchar_t names[7][4]; 680cb2e609SWolfgang Helbig }; 690cb2e609SWolfgang Helbig 700cb2e609SWolfgang Helbig /* The switches from Julian to Gregorian in some countries */ 710cb2e609SWolfgang Helbig static struct djswitch { 72f372d010SMark Murray const char *cc; /* Country code according to ISO 3166 */ 73f372d010SMark Murray const char *nm; /* Name of country */ 740cb2e609SWolfgang Helbig date dt; /* Last day of Julian calendar */ 750cb2e609SWolfgang Helbig } switches[] = { 769d969a8bSAndrey A. Chernov {"AL", "Albania", {1912, 11, 30}}, 779d969a8bSAndrey A. Chernov {"AT", "Austria", {1583, 10, 5}}, 789d969a8bSAndrey A. Chernov {"AU", "Australia", {1752, 9, 2}}, 799d969a8bSAndrey A. Chernov {"BE", "Belgium", {1582, 12, 14}}, 809d969a8bSAndrey A. Chernov {"BG", "Bulgaria", {1916, 3, 18}}, 819d969a8bSAndrey A. Chernov {"CA", "Canada", {1752, 9, 2}}, 829d969a8bSAndrey A. Chernov {"CH", "Switzerland", {1655, 2, 28}}, 839d969a8bSAndrey A. Chernov {"CN", "China", {1911, 12, 18}}, 849d969a8bSAndrey A. Chernov {"CZ", "Czech Republic",{1584, 1, 6}}, 859d969a8bSAndrey A. Chernov {"DE", "Germany", {1700, 2, 18}}, 869d969a8bSAndrey A. Chernov {"DK", "Denmark", {1700, 2, 18}}, 879d969a8bSAndrey A. Chernov {"ES", "Spain", {1582, 10, 4}}, 889d969a8bSAndrey A. Chernov {"FI", "Finland", {1753, 2, 17}}, 899d969a8bSAndrey A. Chernov {"FR", "France", {1582, 12, 9}}, 909d969a8bSAndrey A. Chernov {"GB", "United Kingdom",{1752, 9, 2}}, 919d969a8bSAndrey A. Chernov {"GR", "Greece", {1924, 3, 9}}, 929d969a8bSAndrey A. Chernov {"HU", "Hungary", {1587, 10, 21}}, 939d969a8bSAndrey A. Chernov {"IS", "Iceland", {1700, 11, 16}}, 949d969a8bSAndrey A. Chernov {"IT", "Italy", {1582, 10, 4}}, 959d969a8bSAndrey A. Chernov {"JP", "Japan", {1918, 12, 18}}, 969d969a8bSAndrey A. Chernov {"LI", "Lithuania", {1918, 2, 1}}, 979d969a8bSAndrey A. Chernov {"LN", "Latin", {9999, 05, 31}}, 989d969a8bSAndrey A. Chernov {"LU", "Luxembourg", {1582, 12, 14}}, 999d969a8bSAndrey A. Chernov {"LV", "Latvia", {1918, 2, 1}}, 1009d969a8bSAndrey A. Chernov {"NL", "Netherlands", {1582, 12, 14}}, 1019d969a8bSAndrey A. Chernov {"NO", "Norway", {1700, 2, 18}}, 1029d969a8bSAndrey A. Chernov {"PL", "Poland", {1582, 10, 4}}, 1039d969a8bSAndrey A. Chernov {"PT", "Portugal", {1582, 10, 4}}, 1049d969a8bSAndrey A. Chernov {"RO", "Romania", {1919, 3, 31}}, 1059d969a8bSAndrey A. Chernov {"RU", "Russia", {1918, 1, 31}}, 1069d969a8bSAndrey A. Chernov {"SI", "Slovenia", {1919, 3, 4}}, 1079d969a8bSAndrey A. Chernov {"SW", "Sweden", {1753, 2, 17}}, 1089d969a8bSAndrey A. Chernov {"TR", "Turkey", {1926, 12, 18}}, 1099d969a8bSAndrey A. Chernov {"US", "United States", {1752, 9, 2}}, 1109d969a8bSAndrey A. Chernov {"YU", "Yugoslavia", {1919, 3, 4}} 1110cb2e609SWolfgang Helbig }; 1120cb2e609SWolfgang Helbig 113cde26ed2SWolfgang Helbig struct djswitch *dftswitch = 114cde26ed2SWolfgang Helbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 115cde26ed2SWolfgang Helbig /* default switch (should be "US") */ 116cde26ed2SWolfgang Helbig 1170cb2e609SWolfgang Helbig /* Table used to print day of month and week numbers */ 1180cb2e609SWolfgang Helbig char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 1190cb2e609SWolfgang Helbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 1200cb2e609SWolfgang Helbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 1210cb2e609SWolfgang Helbig " 48 49 50 51 52 53"; 1220cb2e609SWolfgang Helbig 1230cb2e609SWolfgang Helbig /* Table used to print day of year and week numbers */ 1240cb2e609SWolfgang Helbig char jdaystr[] = " 1 2 3 4 5 6 7 8 9" 1250cb2e609SWolfgang Helbig " 10 11 12 13 14 15 16 17 18 19" 1260cb2e609SWolfgang Helbig " 20 21 22 23 24 25 26 27 28 29" 1270cb2e609SWolfgang Helbig " 30 31 32 33 34 35 36 37 38 39" 1280cb2e609SWolfgang Helbig " 40 41 42 43 44 45 46 47 48 49" 1290cb2e609SWolfgang Helbig " 50 51 52 53 54 55 56 57 58 59" 1300cb2e609SWolfgang Helbig " 60 61 62 63 64 65 66 67 68 69" 1310cb2e609SWolfgang Helbig " 70 71 72 73 74 75 76 77 78 79" 1320cb2e609SWolfgang Helbig " 80 81 82 83 84 85 86 87 88 89" 1330cb2e609SWolfgang Helbig " 90 91 92 93 94 95 96 97 98 99" 1340cb2e609SWolfgang Helbig " 100 101 102 103 104 105 106 107 108 109" 1350cb2e609SWolfgang Helbig " 110 111 112 113 114 115 116 117 118 119" 1360cb2e609SWolfgang Helbig " 120 121 122 123 124 125 126 127 128 129" 1370cb2e609SWolfgang Helbig " 130 131 132 133 134 135 136 137 138 139" 1380cb2e609SWolfgang Helbig " 140 141 142 143 144 145 146 147 148 149" 1390cb2e609SWolfgang Helbig " 150 151 152 153 154 155 156 157 158 159" 1400cb2e609SWolfgang Helbig " 160 161 162 163 164 165 166 167 168 169" 1410cb2e609SWolfgang Helbig " 170 171 172 173 174 175 176 177 178 179" 1420cb2e609SWolfgang Helbig " 180 181 182 183 184 185 186 187 188 189" 1430cb2e609SWolfgang Helbig " 190 191 192 193 194 195 196 197 198 199" 1440cb2e609SWolfgang Helbig " 200 201 202 203 204 205 206 207 208 209" 1450cb2e609SWolfgang Helbig " 210 211 212 213 214 215 216 217 218 219" 1460cb2e609SWolfgang Helbig " 220 221 222 223 224 225 226 227 228 229" 1470cb2e609SWolfgang Helbig " 230 231 232 233 234 235 236 237 238 239" 1480cb2e609SWolfgang Helbig " 240 241 242 243 244 245 246 247 248 249" 1490cb2e609SWolfgang Helbig " 250 251 252 253 254 255 256 257 258 259" 1500cb2e609SWolfgang Helbig " 260 261 262 263 264 265 266 267 268 269" 1510cb2e609SWolfgang Helbig " 270 271 272 273 274 275 276 277 278 279" 1520cb2e609SWolfgang Helbig " 280 281 282 283 284 285 286 287 288 289" 1530cb2e609SWolfgang Helbig " 290 291 292 293 294 295 296 297 298 299" 1540cb2e609SWolfgang Helbig " 300 301 302 303 304 305 306 307 308 309" 1550cb2e609SWolfgang Helbig " 310 311 312 313 314 315 316 317 318 319" 1560cb2e609SWolfgang Helbig " 320 321 322 323 324 325 326 327 328 329" 1570cb2e609SWolfgang Helbig " 330 331 332 333 334 335 336 337 338 339" 1580cb2e609SWolfgang Helbig " 340 341 342 343 344 345 346 347 348 349" 1590cb2e609SWolfgang Helbig " 350 351 352 353 354 355 356 357 358 359" 1600cb2e609SWolfgang Helbig " 360 361 362 363 364 365 366"; 1610cb2e609SWolfgang Helbig 162cae11c25SEdwin Groothuis int flag_nohighlight; /* user doesn't want a highlighted today */ 163a4264dceSWolfgang Helbig int flag_weeks; /* user wants number of week */ 1640cb2e609SWolfgang Helbig int nswitch; /* user defined switch date */ 1650cb2e609SWolfgang Helbig int nswitchb; /* switch date for backward compatibility */ 166e454a171SRoman Divacky int today; 1670cb2e609SWolfgang Helbig 1680cb2e609SWolfgang Helbig char *center(char *s, char *t, int w); 1694646cea7SDavid Schultz wchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 1700851fbdfSEdwin Groothuis void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl); 1710cb2e609SWolfgang Helbig void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 1720cb2e609SWolfgang Helbig void mkweekdays(struct weekdays * wds); 173ba29aec0SGarrett Wollman int parsemonth(const char *s, int *m, int *y); 1740cb2e609SWolfgang Helbig void printcc(void); 1750cb2e609SWolfgang Helbig void printeaster(int year, int julian, int orthodox); 176f8a0edbaSWolfgang Helbig int firstday(int y, int m); 1770851fbdfSEdwin Groothuis date *sdater(int ndays, struct date * d); 1780cb2e609SWolfgang Helbig date *sdateb(int ndays, struct date * d); 1790851fbdfSEdwin Groothuis int sndaysr(struct date * d); 1800cb2e609SWolfgang Helbig int sndaysb(struct date * d); 181a99e4564SPhilippe Charnier static void usage(void); 1820851fbdfSEdwin Groothuis void monthranger(int year, int jd_flag, int m, int before, int after); 1830851fbdfSEdwin Groothuis void monthrangeb(int year, int jd_flag, int m, int before, int after); 184cae11c25SEdwin Groothuis void highlight(char *dst, char *src, int len, int *extraletters); 1850cb2e609SWolfgang Helbig 1860cb2e609SWolfgang Helbig int 1870cb2e609SWolfgang Helbig main(int argc, char *argv[]) 1880cb2e609SWolfgang Helbig { 1890cb2e609SWolfgang Helbig struct djswitch *p, *q; /* to search user defined switch date */ 1900cb2e609SWolfgang Helbig date never = {10000, 1, 1}; /* outside valid range of dates */ 1910cb2e609SWolfgang Helbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 1920851fbdfSEdwin Groothuis date dt; 1930cb2e609SWolfgang Helbig int ch; /* holds the option character */ 1940cb2e609SWolfgang Helbig int m = 0; /* month */ 1950cb2e609SWolfgang Helbig int y = 0; /* year */ 1960cb2e609SWolfgang Helbig int flag_backward = 0; /* user called cal--backward compat. */ 1970cb2e609SWolfgang Helbig int flag_hole_year = 0; /* user wants the whole year */ 1980cb2e609SWolfgang Helbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 1990cb2e609SWolfgang Helbig int flag_julian_day = 0; /* user wants the Julian day 2000cb2e609SWolfgang Helbig * numbers */ 2010cb2e609SWolfgang Helbig int flag_orthodox = 0; /* use wants Orthodox easter */ 2020cb2e609SWolfgang Helbig int flag_easter = 0; /* use wants easter date */ 2030cb2e609SWolfgang Helbig char *cp; /* character pointer */ 2040c4cafeaSGarrett Wollman char *flag_month = NULL; /* requested month as string */ 2050851fbdfSEdwin Groothuis char *flag_highlightdate = NULL; 2060851fbdfSEdwin Groothuis int before, after; 207f372d010SMark Murray const char *locale; /* locale to get country code */ 208e454a171SRoman Divacky 209cae11c25SEdwin Groothuis flag_nohighlight = 0; 210cae11c25SEdwin Groothuis flag_weeks = 0; 2110cb2e609SWolfgang Helbig 212cde26ed2SWolfgang Helbig /* 213cde26ed2SWolfgang Helbig * Use locale to determine the country code, 214cde26ed2SWolfgang Helbig * and use the country code to determine the default 215cde26ed2SWolfgang Helbig * switchdate and date format from the switches table. 216cde26ed2SWolfgang Helbig */ 217e036a70eSAndrey A. Chernov if (setlocale(LC_ALL, "") == NULL) 218cde26ed2SWolfgang Helbig warn("setlocale"); 219e036a70eSAndrey A. Chernov locale = setlocale(LC_TIME, NULL); 220c052429fSAndrey A. Chernov if (locale == NULL || 221c052429fSAndrey A. Chernov strcmp(locale, "C") == 0 || 2221c593a0dSAndrey A. Chernov strcmp(locale, "POSIX") == 0 || 2231c593a0dSAndrey A. Chernov strcmp(locale, "ASCII") == 0 || 2241c593a0dSAndrey A. Chernov strcmp(locale, "US-ASCII") == 0) 225cde26ed2SWolfgang Helbig locale = "_US"; 226cde26ed2SWolfgang Helbig q = switches + sizeof(switches) / sizeof(struct djswitch); 227cde26ed2SWolfgang Helbig for (p = switches; p != q; p++) 228cde26ed2SWolfgang Helbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 229cde26ed2SWolfgang Helbig break; 230cde26ed2SWolfgang Helbig if (p == q) { 231cde26ed2SWolfgang Helbig nswitch = ndaysj(&dftswitch->dt); 232cde26ed2SWolfgang Helbig } else { 233cde26ed2SWolfgang Helbig nswitch = ndaysj(&p->dt); 234cde26ed2SWolfgang Helbig dftswitch = p; 235cde26ed2SWolfgang Helbig } 236cde26ed2SWolfgang Helbig 2370cb2e609SWolfgang Helbig 2380cb2e609SWolfgang Helbig /* 2390cb2e609SWolfgang Helbig * Get the filename portion of argv[0] and set flag_backward if 2400cb2e609SWolfgang Helbig * this program is called "cal". 2410cb2e609SWolfgang Helbig */ 2420c4cafeaSGarrett Wollman cp = strrchr(argv[0], '/'); 2430c4cafeaSGarrett Wollman cp = (cp == NULL) ? argv[0] : cp + 1; 2440c4cafeaSGarrett Wollman if (strcmp("cal", cp) == 0) 2450cb2e609SWolfgang Helbig flag_backward = 1; 2460cb2e609SWolfgang Helbig 2470cb2e609SWolfgang Helbig /* Set the switch date to United Kingdom if backwards compatible */ 2480cb2e609SWolfgang Helbig if (flag_backward) 2490cb2e609SWolfgang Helbig nswitchb = ndaysj(&ukswitch); 2500cb2e609SWolfgang Helbig 2510851fbdfSEdwin Groothuis before = after = -1; 2520851fbdfSEdwin Groothuis 2530851fbdfSEdwin Groothuis while ((ch = getopt(argc, argv, "A:B:3Jbd:ehjm:ops:wy")) != -1) 2540cb2e609SWolfgang Helbig switch (ch) { 2550851fbdfSEdwin Groothuis case '3': 2560851fbdfSEdwin Groothuis before = after = 1; 2570851fbdfSEdwin Groothuis break; 2580851fbdfSEdwin Groothuis case 'A': 2590851fbdfSEdwin Groothuis after = strtol(optarg, NULL, 10); 2600851fbdfSEdwin Groothuis if (after < 0) 2610851fbdfSEdwin Groothuis errx(1, "Argument to -A must be positive"); 2620851fbdfSEdwin Groothuis break; 2630851fbdfSEdwin Groothuis case 'B': 2640851fbdfSEdwin Groothuis before = strtol(optarg, NULL, 10); 2650851fbdfSEdwin Groothuis if (before < 0) 2660851fbdfSEdwin Groothuis errx(1, "Argument to -B must be positive"); 2670851fbdfSEdwin Groothuis break; 2680cb2e609SWolfgang Helbig case 'J': 2690cb2e609SWolfgang Helbig if (flag_backward) 2700cb2e609SWolfgang Helbig usage(); 2710cb2e609SWolfgang Helbig nswitch = ndaysj(&never); 2720cb2e609SWolfgang Helbig flag_julian_cal = 1; 2730cb2e609SWolfgang Helbig break; 2740851fbdfSEdwin Groothuis case 'b': 2750851fbdfSEdwin Groothuis flag_backward = 1; 2760851fbdfSEdwin Groothuis break; 2770851fbdfSEdwin Groothuis case 'd': 2780851fbdfSEdwin Groothuis flag_highlightdate = optarg; 2790851fbdfSEdwin Groothuis break; 2800cd51de1SRoman Divacky case 'h': 281cae11c25SEdwin Groothuis flag_nohighlight = 1; 2820cd51de1SRoman Divacky break; 2830cb2e609SWolfgang Helbig case 'e': 2840cb2e609SWolfgang Helbig if (flag_backward) 2850cb2e609SWolfgang Helbig usage(); 2860cb2e609SWolfgang Helbig flag_easter = 1; 2870cb2e609SWolfgang Helbig break; 2880cb2e609SWolfgang Helbig case 'j': 2890cb2e609SWolfgang Helbig flag_julian_day = 1; 2900cb2e609SWolfgang Helbig break; 2910c4cafeaSGarrett Wollman case 'm': 2920c4cafeaSGarrett Wollman flag_month = optarg; 2931d0e1dacSEdwin Groothuis before = 0; 2941d0e1dacSEdwin Groothuis after = 0; 2950c4cafeaSGarrett Wollman break; 2960cb2e609SWolfgang Helbig case 'o': 2970cb2e609SWolfgang Helbig if (flag_backward) 2980cb2e609SWolfgang Helbig usage(); 2990cb2e609SWolfgang Helbig flag_orthodox = 1; 3000cb2e609SWolfgang Helbig flag_easter = 1; 3010cb2e609SWolfgang Helbig break; 3020cb2e609SWolfgang Helbig case 'p': 3030cb2e609SWolfgang Helbig if (flag_backward) 3040cb2e609SWolfgang Helbig usage(); 3050cb2e609SWolfgang Helbig printcc(); 3060cb2e609SWolfgang Helbig return (0); 3070cb2e609SWolfgang Helbig break; 3080cb2e609SWolfgang Helbig case 's': 3090cb2e609SWolfgang Helbig if (flag_backward) 3100cb2e609SWolfgang Helbig usage(); 3110cb2e609SWolfgang Helbig q = switches + 3120cb2e609SWolfgang Helbig sizeof(switches) / sizeof(struct djswitch); 3130cb2e609SWolfgang Helbig for (p = switches; 3140cb2e609SWolfgang Helbig p != q && strcmp(p->cc, optarg) != 0; p++) 3150cb2e609SWolfgang Helbig ; 3160cb2e609SWolfgang Helbig if (p == q) 3170cb2e609SWolfgang Helbig errx(EX_USAGE, 3180cb2e609SWolfgang Helbig "%s: invalid country code", optarg); 3190cb2e609SWolfgang Helbig nswitch = ndaysj(&(p->dt)); 3200cb2e609SWolfgang Helbig break; 3210cb2e609SWolfgang Helbig case 'w': 3220cb2e609SWolfgang Helbig if (flag_backward) 3230cb2e609SWolfgang Helbig usage(); 3240cb2e609SWolfgang Helbig flag_weeks = 1; 3250cb2e609SWolfgang Helbig break; 3260cb2e609SWolfgang Helbig case 'y': 3270cb2e609SWolfgang Helbig flag_hole_year = 1; 3280cb2e609SWolfgang Helbig break; 3290cb2e609SWolfgang Helbig default: 3300cb2e609SWolfgang Helbig usage(); 3310cb2e609SWolfgang Helbig } 3320cb2e609SWolfgang Helbig 3330cb2e609SWolfgang Helbig argc -= optind; 3340cb2e609SWolfgang Helbig argv += optind; 3350cb2e609SWolfgang Helbig 3360c4cafeaSGarrett Wollman switch (argc) { 3370c4cafeaSGarrett Wollman case 2: 3380c4cafeaSGarrett Wollman if (flag_easter) 3390c4cafeaSGarrett Wollman usage(); 3400c4cafeaSGarrett Wollman flag_month = *argv++; 3411d0e1dacSEdwin Groothuis before = 0; 3421d0e1dacSEdwin Groothuis after = 0; 3431d0e1dacSEdwin Groothuis m = strtol(flag_month, NULL, 10); 3440c4cafeaSGarrett Wollman /* FALLTHROUGH */ 3450c4cafeaSGarrett Wollman case 1: 3460c4cafeaSGarrett Wollman y = atoi(*argv++); 3470c4cafeaSGarrett Wollman if (y < 1 || y > 9999) 3480c4cafeaSGarrett Wollman errx(EX_USAGE, "year %d not in range 1..9999", y); 3491d0e1dacSEdwin Groothuis if (before == -1 && after == -1) { 3500851fbdfSEdwin Groothuis before = 0; 3510851fbdfSEdwin Groothuis after = 11; 3520851fbdfSEdwin Groothuis m = 1; 3531d0e1dacSEdwin Groothuis } 3540c4cafeaSGarrett Wollman break; 3550c4cafeaSGarrett Wollman case 0: 3560c4cafeaSGarrett Wollman { 3570cb2e609SWolfgang Helbig time_t t; 3580cb2e609SWolfgang Helbig struct tm *tm; 3590cb2e609SWolfgang Helbig 3600cb2e609SWolfgang Helbig t = time(NULL); 3610cb2e609SWolfgang Helbig tm = localtime(&t); 3620cb2e609SWolfgang Helbig y = tm->tm_year + 1900; 3630cb2e609SWolfgang Helbig m = tm->tm_mon + 1; 3640851fbdfSEdwin Groothuis if (before == -1) 3650851fbdfSEdwin Groothuis before = 0; 3660851fbdfSEdwin Groothuis if (after == -1) 3670851fbdfSEdwin Groothuis after = 0; 3680cb2e609SWolfgang Helbig } 3690cb2e609SWolfgang Helbig break; 3700cb2e609SWolfgang Helbig default: 3710cb2e609SWolfgang Helbig usage(); 3720cb2e609SWolfgang Helbig } 3730cb2e609SWolfgang Helbig 374cae11c25SEdwin Groothuis if (flag_hole_year) { 375cae11c25SEdwin Groothuis m = 1; 376cae11c25SEdwin Groothuis before = 0; 377cae11c25SEdwin Groothuis after = 11; 378cae11c25SEdwin Groothuis } 379cae11c25SEdwin Groothuis 3800c4cafeaSGarrett Wollman if (flag_month != NULL) { 381ba29aec0SGarrett Wollman if (parsemonth(flag_month, &m, &y)) { 3820c4cafeaSGarrett Wollman errx(EX_USAGE, 3830c4cafeaSGarrett Wollman "%s is neither a month number (1..12) nor a name", 3840c4cafeaSGarrett Wollman flag_month); 3850c4cafeaSGarrett Wollman } 386ba29aec0SGarrett Wollman } 3870c4cafeaSGarrett Wollman 3880851fbdfSEdwin Groothuis if (flag_highlightdate != NULL) { 3890851fbdfSEdwin Groothuis dt.y = strtol(flag_highlightdate, NULL, 10); 3900851fbdfSEdwin Groothuis dt.m = strtol(flag_highlightdate + 5, NULL, 10); 3910851fbdfSEdwin Groothuis dt.d = strtol(flag_highlightdate + 8, NULL, 10); 3920851fbdfSEdwin Groothuis } else { 3930851fbdfSEdwin Groothuis time_t t; 3940851fbdfSEdwin Groothuis struct tm *tm1; 3950851fbdfSEdwin Groothuis 3960851fbdfSEdwin Groothuis t = time(NULL); 3970851fbdfSEdwin Groothuis tm1 = localtime(&t); 3980851fbdfSEdwin Groothuis dt.y = tm1->tm_year + 1900; 3990851fbdfSEdwin Groothuis dt.m = tm1->tm_mon + 1; 4000851fbdfSEdwin Groothuis dt.d = tm1->tm_mday; 4010851fbdfSEdwin Groothuis } 4020851fbdfSEdwin Groothuis today = sndaysb(&dt); 4030851fbdfSEdwin Groothuis 4040cb2e609SWolfgang Helbig if (flag_easter) 4050cb2e609SWolfgang Helbig printeaster(y, flag_julian_cal, flag_orthodox); 4060cb2e609SWolfgang Helbig else 4070cb2e609SWolfgang Helbig if (flag_backward) 4080851fbdfSEdwin Groothuis monthrangeb(y, flag_julian_day, m, before, after); 4090cb2e609SWolfgang Helbig else 4100851fbdfSEdwin Groothuis monthranger(y, flag_julian_day, m, before, after); 4110cb2e609SWolfgang Helbig return (0); 4120cb2e609SWolfgang Helbig } 4130cb2e609SWolfgang Helbig 414a99e4564SPhilippe Charnier static void 4150cb2e609SWolfgang Helbig usage(void) 4160cb2e609SWolfgang Helbig { 4170cb2e609SWolfgang Helbig 4180c4cafeaSGarrett Wollman fputs( 4197149aa01SRoman Divacky "usage: cal [-hjy] [[month] year]\n" 4207149aa01SRoman Divacky " cal [-hj] [-m month] [year]\n" 4217149aa01SRoman Divacky " ncal [-hJjpwy] [-s country_code] [[month] year]\n" 4220851fbdfSEdwin Groothuis " ncal [-hJeo] [year]\n" 4230851fbdfSEdwin Groothuis "for debug the highlighting: [-b] [-d yyyy-mm-dd]\n", 4240851fbdfSEdwin Groothuis stderr); 4250cb2e609SWolfgang Helbig exit(EX_USAGE); 4260cb2e609SWolfgang Helbig } 4270cb2e609SWolfgang Helbig 4280cb2e609SWolfgang Helbig /* print the assumed switches for all countries */ 4290cb2e609SWolfgang Helbig void 4300cb2e609SWolfgang Helbig printcc(void) 4310cb2e609SWolfgang Helbig { 4320cb2e609SWolfgang Helbig struct djswitch *p; 4330cb2e609SWolfgang Helbig int n; /* number of lines to print */ 4340cb2e609SWolfgang Helbig int m; /* offset from left to right table entry on the same line */ 4350cb2e609SWolfgang Helbig 436cde26ed2SWolfgang Helbig #define FSTR "%c%s %-15s%4d-%02d-%02d" 437cde26ed2SWolfgang Helbig #define DFLT(p) ((p) == dftswitch ? '*' : ' ') 438cde26ed2SWolfgang Helbig #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 4390cb2e609SWolfgang Helbig 4400cb2e609SWolfgang Helbig n = sizeof(switches) / sizeof(struct djswitch); 4410cb2e609SWolfgang Helbig m = (n + 1) / 2; 4420cb2e609SWolfgang Helbig n /= 2; 443cde26ed2SWolfgang Helbig for (p = switches; p != switches + n; p++) 444cde26ed2SWolfgang Helbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 4450cb2e609SWolfgang Helbig if (m != n) 446cde26ed2SWolfgang Helbig printf(FSTR"\n", FSTRARG(p)); 4470cb2e609SWolfgang Helbig } 4480cb2e609SWolfgang Helbig 4490cb2e609SWolfgang Helbig /* print the date of easter sunday */ 4500cb2e609SWolfgang Helbig void 4510cb2e609SWolfgang Helbig printeaster(int y, int julian, int orthodox) 4520cb2e609SWolfgang Helbig { 4530cb2e609SWolfgang Helbig date dt; 454cde26ed2SWolfgang Helbig struct tm tm; 4550851fbdfSEdwin Groothuis char buf[MAX_WIDTH]; 456f5e40af2SAndrey A. Chernov static int d_first = -1; 4570cb2e609SWolfgang Helbig 458f5e40af2SAndrey A. Chernov if (d_first < 0) 459f5e40af2SAndrey A. Chernov d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 4600cb2e609SWolfgang Helbig /* force orthodox easter for years before 1583 */ 4610cb2e609SWolfgang Helbig if (y < 1583) 4620cb2e609SWolfgang Helbig orthodox = 1; 4630cb2e609SWolfgang Helbig 4640cb2e609SWolfgang Helbig if (orthodox) 4650cb2e609SWolfgang Helbig if (julian) 4660cb2e609SWolfgang Helbig easteroj(y, &dt); 4670cb2e609SWolfgang Helbig else 4680cb2e609SWolfgang Helbig easterog(y, &dt); 4690cb2e609SWolfgang Helbig else 4700cb2e609SWolfgang Helbig easterg(y, &dt); 471cde26ed2SWolfgang Helbig 472cde26ed2SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 473cde26ed2SWolfgang Helbig tm.tm_year = dt.y - 1900; 474cde26ed2SWolfgang Helbig tm.tm_mon = dt.m - 1; 475cde26ed2SWolfgang Helbig tm.tm_mday = dt.d; 476f5e40af2SAndrey A. Chernov strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 477cde26ed2SWolfgang Helbig printf("%s\n", buf); 4780cb2e609SWolfgang Helbig } 4790cb2e609SWolfgang Helbig 4801d0e1dacSEdwin Groothuis #define MW(mw, me) ((mw) + me) 4810851fbdfSEdwin Groothuis #define DECREASEMONTH(m, y) \ 4820851fbdfSEdwin Groothuis if (--m == 0) { \ 4830851fbdfSEdwin Groothuis m = 12; \ 4840851fbdfSEdwin Groothuis y--; \ 485bd97a998SHajimu UMEMOTO } 4860851fbdfSEdwin Groothuis #define INCREASEMONTH(m, y) \ 4870851fbdfSEdwin Groothuis if (++(m) == 13) { \ 4880851fbdfSEdwin Groothuis (m) = 1; \ 4890851fbdfSEdwin Groothuis (y)++; \ 4900cb2e609SWolfgang Helbig } 4910851fbdfSEdwin Groothuis #define M2Y(m) ((m) / 12) 4920851fbdfSEdwin Groothuis #define M2M(m) (1 + (m) % 12) 4930cb2e609SWolfgang Helbig 4940cb2e609SWolfgang Helbig void 4950851fbdfSEdwin Groothuis monthrangeb(int y, int jd_flag, int m, int before, int after) 4960cb2e609SWolfgang Helbig { 4970cb2e609SWolfgang Helbig struct monthlines year[12]; 4980cb2e609SWolfgang Helbig struct weekdays wds; 4990851fbdfSEdwin Groothuis char s[MAX_WIDTH], t[MAX_WIDTH]; 5000851fbdfSEdwin Groothuis wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH]; 5010851fbdfSEdwin Groothuis const char *wdss; 5020cb2e609SWolfgang Helbig int i, j; 5030cb2e609SWolfgang Helbig int mpl; 5040cb2e609SWolfgang Helbig int mw; 5050851fbdfSEdwin Groothuis int m1, m2; 5060851fbdfSEdwin Groothuis int printyearheader; 5070851fbdfSEdwin Groothuis int prevyear = -1; 5080cb2e609SWolfgang Helbig 5090cb2e609SWolfgang Helbig mpl = jd_flag ? 2 : 3; 5100cb2e609SWolfgang Helbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 5110851fbdfSEdwin Groothuis wdss = (mpl == 2) ? " " : ""; 5120cb2e609SWolfgang Helbig 5130851fbdfSEdwin Groothuis while (before != 0) { 5140851fbdfSEdwin Groothuis DECREASEMONTH(m, y); 5150851fbdfSEdwin Groothuis before--; 5160851fbdfSEdwin Groothuis after++; 5170cb2e609SWolfgang Helbig } 5180851fbdfSEdwin Groothuis m1 = y * 12 + m - 1; 5190851fbdfSEdwin Groothuis m2 = m1 + after; 5200851fbdfSEdwin Groothuis 5210851fbdfSEdwin Groothuis mkweekdays(&wds); 5220851fbdfSEdwin Groothuis 5230851fbdfSEdwin Groothuis /* 5240851fbdfSEdwin Groothuis * The year header is printed when there are more than 'mpl' months 5250851fbdfSEdwin Groothuis * and if the first month is a multitude of 'mpl'. 5260851fbdfSEdwin Groothuis * If not, it will print the year behind every month. 5270851fbdfSEdwin Groothuis */ 5280851fbdfSEdwin Groothuis printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 5290851fbdfSEdwin Groothuis 5300851fbdfSEdwin Groothuis m = m1; 5310851fbdfSEdwin Groothuis while (m <= m2) { 5320851fbdfSEdwin Groothuis int count = 0; 5330851fbdfSEdwin Groothuis for (i = 0; i != mpl && m + i <= m2; i++) { 5340851fbdfSEdwin Groothuis mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 5350851fbdfSEdwin Groothuis count++; 5360851fbdfSEdwin Groothuis } 5370851fbdfSEdwin Groothuis 5380851fbdfSEdwin Groothuis /* Empty line between two rows of months */ 5390851fbdfSEdwin Groothuis if (m != m1) 5400851fbdfSEdwin Groothuis printf("\n"); 5410851fbdfSEdwin Groothuis 5420851fbdfSEdwin Groothuis /* Year at the top */ 5430851fbdfSEdwin Groothuis if (printyearheader && M2Y(m) != prevyear) { 5440851fbdfSEdwin Groothuis sprintf(s, "%d", M2Y(m)); 5450851fbdfSEdwin Groothuis printf("%s\n", center(t, s, mpl * mw)); 5460851fbdfSEdwin Groothuis prevyear = M2Y(m); 5470851fbdfSEdwin Groothuis } 5480851fbdfSEdwin Groothuis 5490851fbdfSEdwin Groothuis /* Month names */ 5500851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 5510851fbdfSEdwin Groothuis if (printyearheader) 5520851fbdfSEdwin Groothuis wprintf(L"%-*ls ", 5530851fbdfSEdwin Groothuis mw, wcenter(ws, year[i].name, mw)); 5540851fbdfSEdwin Groothuis else { 5550851fbdfSEdwin Groothuis swprintf(ws, sizeof(ws), L"%-ls %d", 5560851fbdfSEdwin Groothuis year[i].name, M2Y(m + i)); 5570851fbdfSEdwin Groothuis wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); 5580851fbdfSEdwin Groothuis } 5590851fbdfSEdwin Groothuis printf("\n"); 5600851fbdfSEdwin Groothuis 5610851fbdfSEdwin Groothuis /* Day of the week names */ 5620851fbdfSEdwin Groothuis for (i = 0; i < count; i++) { 5630851fbdfSEdwin Groothuis wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", 5640851fbdfSEdwin Groothuis wdss, wds.names[6], wdss, wds.names[0], 5650851fbdfSEdwin Groothuis wdss, wds.names[1], wdss, wds.names[2], 5660851fbdfSEdwin Groothuis wdss, wds.names[3], wdss, wds.names[4], 5670851fbdfSEdwin Groothuis wdss, wds.names[5]); 5680851fbdfSEdwin Groothuis } 5690851fbdfSEdwin Groothuis printf("\n"); 5700851fbdfSEdwin Groothuis 5710851fbdfSEdwin Groothuis for (i = 0; i != 6; i++) { 5720851fbdfSEdwin Groothuis for (j = 0; j < count; j++) 5731d0e1dacSEdwin Groothuis printf("%-*s ", 5741d0e1dacSEdwin Groothuis MW(mw, year[j].extralen[i]), 5751d0e1dacSEdwin Groothuis year[j].lines[i]+1); 5760851fbdfSEdwin Groothuis printf("\n"); 5770851fbdfSEdwin Groothuis } 5780851fbdfSEdwin Groothuis 5790851fbdfSEdwin Groothuis m += mpl; 5800cb2e609SWolfgang Helbig } 5810cb2e609SWolfgang Helbig } 5820cb2e609SWolfgang Helbig 5830cb2e609SWolfgang Helbig void 5840851fbdfSEdwin Groothuis monthranger(int y, int jd_flag, int m, int before, int after) 5850851fbdfSEdwin Groothuis { 5860851fbdfSEdwin Groothuis struct monthlines year[12]; 5870851fbdfSEdwin Groothuis struct weekdays wds; 5880851fbdfSEdwin Groothuis char s[MAX_WIDTH], t[MAX_WIDTH]; 5890851fbdfSEdwin Groothuis int i, j; 5900851fbdfSEdwin Groothuis int mpl; 5910851fbdfSEdwin Groothuis int mw; 5920851fbdfSEdwin Groothuis int m1, m2; 5930851fbdfSEdwin Groothuis int prevyear = -1; 5940851fbdfSEdwin Groothuis int printyearheader; 5950851fbdfSEdwin Groothuis 5960851fbdfSEdwin Groothuis mpl = jd_flag ? 3 : 4; 5970851fbdfSEdwin Groothuis mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R; 5980851fbdfSEdwin Groothuis 5990851fbdfSEdwin Groothuis while (before != 0) { 6000851fbdfSEdwin Groothuis DECREASEMONTH(m, y); 6010851fbdfSEdwin Groothuis before--; 6020851fbdfSEdwin Groothuis after++; 6030851fbdfSEdwin Groothuis } 6040851fbdfSEdwin Groothuis m1 = y * 12 + m - 1; 6050851fbdfSEdwin Groothuis m2 = m1 + after; 6060851fbdfSEdwin Groothuis 6070851fbdfSEdwin Groothuis mkweekdays(&wds); 6080851fbdfSEdwin Groothuis 6090851fbdfSEdwin Groothuis /* 6100851fbdfSEdwin Groothuis * The year header is printed when there are more than 'mpl' months 6110851fbdfSEdwin Groothuis * and if the first month is a multitude of 'mpl'. 6120851fbdfSEdwin Groothuis * If not, it will print the year behind every month. 6130851fbdfSEdwin Groothuis */ 6140851fbdfSEdwin Groothuis printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 6150851fbdfSEdwin Groothuis 6160851fbdfSEdwin Groothuis m = m1; 6170851fbdfSEdwin Groothuis while (m <= m2) { 6180851fbdfSEdwin Groothuis int count = 0; 6190851fbdfSEdwin Groothuis for (i = 0; i != mpl && m + i <= m2; i++) { 6200851fbdfSEdwin Groothuis mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 6210851fbdfSEdwin Groothuis count++; 6220851fbdfSEdwin Groothuis } 6230851fbdfSEdwin Groothuis 6240851fbdfSEdwin Groothuis /* Empty line between two rows of months */ 6250851fbdfSEdwin Groothuis if (m != m1) 6260851fbdfSEdwin Groothuis printf("\n"); 6270851fbdfSEdwin Groothuis 6280851fbdfSEdwin Groothuis /* Year at the top */ 6290851fbdfSEdwin Groothuis if (printyearheader && M2Y(m) != prevyear) { 6300851fbdfSEdwin Groothuis sprintf(s, "%d", M2Y(m)); 6310851fbdfSEdwin Groothuis printf("%s\n", center(t, s, mpl * mw)); 6320851fbdfSEdwin Groothuis prevyear = M2Y(m); 6330851fbdfSEdwin Groothuis } 6340851fbdfSEdwin Groothuis 6350851fbdfSEdwin Groothuis /* Month names */ 6360851fbdfSEdwin Groothuis wprintf(L" "); 6370851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 6380851fbdfSEdwin Groothuis if (printyearheader) 6390851fbdfSEdwin Groothuis wprintf(L"%-*ls", mw, year[i].name); 6400851fbdfSEdwin Groothuis else 6410851fbdfSEdwin Groothuis wprintf(L"%-ls %-*d", year[i].name, 6420851fbdfSEdwin Groothuis mw - wcslen(year[i].name) - 1, M2Y(m + i)); 6430851fbdfSEdwin Groothuis printf("\n"); 6440851fbdfSEdwin Groothuis 6450851fbdfSEdwin Groothuis for (i = 0; i != 7; i++) { 6460851fbdfSEdwin Groothuis /* Week day */ 6470851fbdfSEdwin Groothuis wprintf(L"%.2ls", wds.names[i]); 6480851fbdfSEdwin Groothuis 6490851fbdfSEdwin Groothuis /* Full months */ 6500851fbdfSEdwin Groothuis for (j = 0; j < count; j++) 6510851fbdfSEdwin Groothuis printf("%-*s", 6521d0e1dacSEdwin Groothuis MW(mw, year[j].extralen[i]), 6531d0e1dacSEdwin Groothuis year[j].lines[i]); 6540851fbdfSEdwin Groothuis printf("\n"); 6550851fbdfSEdwin Groothuis } 6560851fbdfSEdwin Groothuis 6570851fbdfSEdwin Groothuis if (flag_weeks) { 6580851fbdfSEdwin Groothuis printf(" "); 6590851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 6600851fbdfSEdwin Groothuis printf("%-*s", mw, year[i].weeks); 6610851fbdfSEdwin Groothuis printf("\n"); 6620851fbdfSEdwin Groothuis } 6630851fbdfSEdwin Groothuis 6640851fbdfSEdwin Groothuis m += mpl; 6650851fbdfSEdwin Groothuis } 6660851fbdfSEdwin Groothuis return; 6670851fbdfSEdwin Groothuis } 6680851fbdfSEdwin Groothuis 6690851fbdfSEdwin Groothuis void 6700851fbdfSEdwin Groothuis mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines) 6710cb2e609SWolfgang Helbig { 6720cb2e609SWolfgang Helbig 6730cb2e609SWolfgang Helbig struct tm tm; /* for strftime printing local names of 6740cb2e609SWolfgang Helbig * months */ 6750cb2e609SWolfgang Helbig date dt; /* handy date */ 6760cb2e609SWolfgang Helbig int dw; /* width of numbers */ 6770cb2e609SWolfgang Helbig int first; /* first day of month */ 6780cb2e609SWolfgang Helbig int firstm; /* first day of first week of month */ 679e454a171SRoman Divacky int i, j, k, l; /* just indices */ 6800cb2e609SWolfgang Helbig int last; /* the first day of next month */ 6810cb2e609SWolfgang Helbig int jan1 = 0; /* the first day of this year */ 6820cb2e609SWolfgang Helbig char *ds; /* pointer to day strings (daystr or 6830cb2e609SWolfgang Helbig * jdaystr) */ 6840cb2e609SWolfgang Helbig 6850cb2e609SWolfgang Helbig /* Set name of month. */ 6860cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 6870cb2e609SWolfgang Helbig tm.tm_mon = m; 6884646cea7SDavid Schultz wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 6894646cea7SDavid Schultz L"%OB", &tm); 6904646cea7SDavid Schultz mlines->name[0] = towupper(mlines->name[0]); 6910cb2e609SWolfgang Helbig 6920cb2e609SWolfgang Helbig /* 6930cb2e609SWolfgang Helbig * Set first and last to the day number of the first day of this 6940cb2e609SWolfgang Helbig * month and the first day of next month respectively. Set jan1 to 695f8a0edbaSWolfgang Helbig * the day number of the first day of this year. 6960cb2e609SWolfgang Helbig */ 697f8a0edbaSWolfgang Helbig first = firstday(y, m + 1); 698f8a0edbaSWolfgang Helbig if (m == 11) 699f8a0edbaSWolfgang Helbig last = firstday(y + 1, 1); 700f8a0edbaSWolfgang Helbig else 701f8a0edbaSWolfgang Helbig last = firstday(y, m + 2); 7020cb2e609SWolfgang Helbig 703f8a0edbaSWolfgang Helbig if (jd_flag) 704630d15bbSWolfgang Helbig jan1 = firstday(y, 1); 7050cb2e609SWolfgang Helbig 7060cb2e609SWolfgang Helbig /* 7070cb2e609SWolfgang Helbig * Set firstm to the day number of monday of the first week of 7080cb2e609SWolfgang Helbig * this month. (This might be in the last month) 7090cb2e609SWolfgang Helbig */ 7100cb2e609SWolfgang Helbig firstm = first - weekday(first); 7110cb2e609SWolfgang Helbig 7120cb2e609SWolfgang Helbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 7130cb2e609SWolfgang Helbig if (jd_flag) { 7140cb2e609SWolfgang Helbig ds = jdaystr; 7150cb2e609SWolfgang Helbig dw = 4; 7160cb2e609SWolfgang Helbig } else { 7170cb2e609SWolfgang Helbig ds = daystr; 7180cb2e609SWolfgang Helbig dw = 3; 7190cb2e609SWolfgang Helbig } 7200cb2e609SWolfgang Helbig 7210cb2e609SWolfgang Helbig /* 7220cb2e609SWolfgang Helbig * Fill the lines with day of month or day of year (julian day) 7230cb2e609SWolfgang Helbig * line index: i, each line is one weekday. column index: j, each 7240cb2e609SWolfgang Helbig * column is one day number. print column index: k. 7250cb2e609SWolfgang Helbig */ 7260cb2e609SWolfgang Helbig for (i = 0; i != 7; i++) { 727e454a171SRoman Divacky l = 0; 728e454a171SRoman Divacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 7290cb2e609SWolfgang Helbig if (j >= first) { 7300cb2e609SWolfgang Helbig if (jd_flag) 7310cb2e609SWolfgang Helbig dt.d = j - jan1 + 1; 7320cb2e609SWolfgang Helbig else 7330851fbdfSEdwin Groothuis sdater(j, &dt); 734cae11c25SEdwin Groothuis if (j == today && !flag_nohighlight) 735cae11c25SEdwin Groothuis highlight(mlines->lines[i] + k, 736cae11c25SEdwin Groothuis ds + dt.d * dw, dw, &l); 737cae11c25SEdwin Groothuis else 738e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, 7390cb2e609SWolfgang Helbig ds + dt.d * dw, dw); 7400cb2e609SWolfgang Helbig } else 741e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, " ", dw); 742e454a171SRoman Divacky } 743e454a171SRoman Divacky mlines->lines[i][k + l] = '\0'; 7441d0e1dacSEdwin Groothuis mlines->extralen[i] = l; 7450cb2e609SWolfgang Helbig } 7460cb2e609SWolfgang Helbig 7470cb2e609SWolfgang Helbig /* fill the weeknumbers */ 7480cb2e609SWolfgang Helbig if (flag_weeks) { 7490cb2e609SWolfgang Helbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 7500cb2e609SWolfgang Helbig if (j <= nswitch) 7510cb2e609SWolfgang Helbig memset(mlines->weeks + k, ' ', dw); 7520cb2e609SWolfgang Helbig else 7530cb2e609SWolfgang Helbig memcpy(mlines->weeks + k, 7540cb2e609SWolfgang Helbig ds + week(j, &i)*dw, dw); 7550cb2e609SWolfgang Helbig mlines->weeks[k] = '\0'; 7560cb2e609SWolfgang Helbig } 7570cb2e609SWolfgang Helbig } 7580cb2e609SWolfgang Helbig 7590cb2e609SWolfgang Helbig void 7600cb2e609SWolfgang Helbig mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 7610cb2e609SWolfgang Helbig { 7620cb2e609SWolfgang Helbig 7630cb2e609SWolfgang Helbig struct tm tm; /* for strftime printing local names of 7640cb2e609SWolfgang Helbig * months */ 7650cb2e609SWolfgang Helbig date dt; /* handy date */ 7660cb2e609SWolfgang Helbig int dw; /* width of numbers */ 7670cb2e609SWolfgang Helbig int first; /* first day of month */ 7680cb2e609SWolfgang Helbig int firsts; /* sunday of first week of month */ 769e454a171SRoman Divacky int i, j, k, l; /* just indices */ 7700cb2e609SWolfgang Helbig int jan1 = 0; /* the first day of this year */ 7710cb2e609SWolfgang Helbig int last; /* the first day of next month */ 7720cb2e609SWolfgang Helbig char *ds; /* pointer to day strings (daystr or 7730cb2e609SWolfgang Helbig * jdaystr) */ 7740cb2e609SWolfgang Helbig 7750cb2e609SWolfgang Helbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 7760cb2e609SWolfgang Helbig if (jd_flag) { 7770cb2e609SWolfgang Helbig ds = jdaystr; 7780cb2e609SWolfgang Helbig dw = 4; 7790cb2e609SWolfgang Helbig } else { 7800cb2e609SWolfgang Helbig ds = daystr; 7810cb2e609SWolfgang Helbig dw = 3; 7820cb2e609SWolfgang Helbig } 7830cb2e609SWolfgang Helbig 7840cb2e609SWolfgang Helbig /* Set name of month centered */ 7850cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 7860cb2e609SWolfgang Helbig tm.tm_mon = m; 7874646cea7SDavid Schultz wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 7884646cea7SDavid Schultz L"%OB", &tm); 7894646cea7SDavid Schultz mlines->name[0] = towupper(mlines->name[0]); 7900cb2e609SWolfgang Helbig 7910cb2e609SWolfgang Helbig /* 7920cb2e609SWolfgang Helbig * Set first and last to the day number of the first day of this 7930cb2e609SWolfgang Helbig * month and the first day of next month respectively. Set jan1 to 7940cb2e609SWolfgang Helbig * the day number of Jan 1st of this year. 7950cb2e609SWolfgang Helbig */ 7960cb2e609SWolfgang Helbig dt.y = y; 7970cb2e609SWolfgang Helbig dt.m = m + 1; 7980cb2e609SWolfgang Helbig dt.d = 1; 7990cb2e609SWolfgang Helbig first = sndaysb(&dt); 8000cb2e609SWolfgang Helbig if (m == 11) { 8010cb2e609SWolfgang Helbig dt.y = y + 1; 8020cb2e609SWolfgang Helbig dt.m = 1; 8030cb2e609SWolfgang Helbig dt.d = 1; 8040cb2e609SWolfgang Helbig } else { 8050cb2e609SWolfgang Helbig dt.y = y; 8060cb2e609SWolfgang Helbig dt.m = m + 2; 8070cb2e609SWolfgang Helbig dt.d = 1; 8080cb2e609SWolfgang Helbig } 8090cb2e609SWolfgang Helbig last = sndaysb(&dt); 8100cb2e609SWolfgang Helbig 8110cb2e609SWolfgang Helbig if (jd_flag) { 8120cb2e609SWolfgang Helbig dt.y = y; 8130cb2e609SWolfgang Helbig dt.m = 1; 8140cb2e609SWolfgang Helbig dt.d = 1; 8150cb2e609SWolfgang Helbig jan1 = sndaysb(&dt); 8160cb2e609SWolfgang Helbig } 8170cb2e609SWolfgang Helbig 8180cb2e609SWolfgang Helbig /* 8190cb2e609SWolfgang Helbig * Set firsts to the day number of sunday of the first week of 8200cb2e609SWolfgang Helbig * this month. (This might be in the last month) 8210cb2e609SWolfgang Helbig */ 8220cb2e609SWolfgang Helbig firsts = first - (weekday(first)+1) % 7; 8230cb2e609SWolfgang Helbig 8240cb2e609SWolfgang Helbig /* 8250cb2e609SWolfgang Helbig * Fill the lines with day of month or day of year (Julian day) 8260cb2e609SWolfgang Helbig * line index: i, each line is one week. column index: j, each 8270cb2e609SWolfgang Helbig * column is one day number. print column index: k. 8280cb2e609SWolfgang Helbig */ 8290cb2e609SWolfgang Helbig for (i = 0; i != 6; i++) { 830e454a171SRoman Divacky l = 0; 8310cb2e609SWolfgang Helbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 832e454a171SRoman Divacky j++, k += dw) { 8330cb2e609SWolfgang Helbig if (j >= first) { 8340cb2e609SWolfgang Helbig if (jd_flag) 8350cb2e609SWolfgang Helbig dt.d = j - jan1 + 1; 8360cb2e609SWolfgang Helbig else 8370cb2e609SWolfgang Helbig sdateb(j, &dt); 838cae11c25SEdwin Groothuis if (j == today && !flag_nohighlight) 839cae11c25SEdwin Groothuis highlight(mlines->lines[i] + k, 840cae11c25SEdwin Groothuis ds + dt.d * dw, dw, &l); 841cae11c25SEdwin Groothuis else 842e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, 8430cb2e609SWolfgang Helbig ds + dt.d * dw, dw); 8440cb2e609SWolfgang Helbig } else 845e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, " ", dw); 846e454a171SRoman Divacky } 8470cb2e609SWolfgang Helbig if (k == 0) 8480cb2e609SWolfgang Helbig mlines->lines[i][1] = '\0'; 8490cb2e609SWolfgang Helbig else 850e454a171SRoman Divacky mlines->lines[i][k + l] = '\0'; 8511d0e1dacSEdwin Groothuis mlines->extralen[i] = l; 8520cb2e609SWolfgang Helbig } 8530cb2e609SWolfgang Helbig } 8540cb2e609SWolfgang Helbig 8550cb2e609SWolfgang Helbig /* Put the local names of weekdays into the wds */ 856a4264dceSWolfgang Helbig void 857a4264dceSWolfgang Helbig mkweekdays(struct weekdays *wds) 8580cb2e609SWolfgang Helbig { 859bd97a998SHajimu UMEMOTO int i, len, width = 0; 8600cb2e609SWolfgang Helbig struct tm tm; 8614646cea7SDavid Schultz wchar_t buf[20]; 8620cb2e609SWolfgang Helbig 8630cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 8640cb2e609SWolfgang Helbig 8650cb2e609SWolfgang Helbig for (i = 0; i != 7; i++) { 8660cb2e609SWolfgang Helbig tm.tm_wday = (i+1) % 7; 8674646cea7SDavid Schultz wcsftime(buf, sizeof(buf), L"%a", &tm); 86846f39347SHajimu UMEMOTO for (len = 2; len > 0; --len) { 869bd97a998SHajimu UMEMOTO if ((width = wcswidth(buf, len)) <= 2) 870bd97a998SHajimu UMEMOTO break; 871bd97a998SHajimu UMEMOTO } 872bd97a998SHajimu UMEMOTO wmemset(wds->names[i], L'\0', 4); 873bd97a998SHajimu UMEMOTO if (width == 1) 874bd97a998SHajimu UMEMOTO wds->names[i][0] = L' '; 875bd97a998SHajimu UMEMOTO wcsncat(wds->names[i], buf, len); 87646f39347SHajimu UMEMOTO wcsncat(wds->names[i], L" ", 1); 8770cb2e609SWolfgang Helbig } 8780cb2e609SWolfgang Helbig } 8790cb2e609SWolfgang Helbig 8800cb2e609SWolfgang Helbig /* 881cf5f6adfSWolfgang Helbig * Compute the day number of the first 882cf5f6adfSWolfgang Helbig * existing date after the first day in month. 883cf5f6adfSWolfgang Helbig * (the first day in month and even the month might not exist!) 884f8a0edbaSWolfgang Helbig */ 885f8a0edbaSWolfgang Helbig int 886f8a0edbaSWolfgang Helbig firstday(int y, int m) 887f8a0edbaSWolfgang Helbig { 888f8a0edbaSWolfgang Helbig date dt; 889f8a0edbaSWolfgang Helbig int nd; 890f8a0edbaSWolfgang Helbig 891f8a0edbaSWolfgang Helbig dt.y = y; 892f8a0edbaSWolfgang Helbig dt.m = m; 893f8a0edbaSWolfgang Helbig dt.d = 1; 8940851fbdfSEdwin Groothuis nd = sndaysr(&dt); 895cf5f6adfSWolfgang Helbig for (;;) { 8960851fbdfSEdwin Groothuis sdater(nd, &dt); 897cf5f6adfSWolfgang Helbig if ((dt.m >= m && dt.y == y) || dt.y > y) 898f8a0edbaSWolfgang Helbig return (nd); 899cf5f6adfSWolfgang Helbig else 900cf5f6adfSWolfgang Helbig nd++; 901cf5f6adfSWolfgang Helbig } 902cf5f6adfSWolfgang Helbig /* NEVER REACHED */ 903f8a0edbaSWolfgang Helbig } 904f8a0edbaSWolfgang Helbig 905f8a0edbaSWolfgang Helbig /* 9060cb2e609SWolfgang Helbig * Compute the number of days from date, obey the local switch from 9070cb2e609SWolfgang Helbig * Julian to Gregorian if specified by the user. 9080cb2e609SWolfgang Helbig */ 9090cb2e609SWolfgang Helbig int 9100851fbdfSEdwin Groothuis sndaysr(struct date *d) 9110cb2e609SWolfgang Helbig { 9120cb2e609SWolfgang Helbig 9130cb2e609SWolfgang Helbig if (nswitch != 0) 9140cb2e609SWolfgang Helbig if (nswitch < ndaysj(d)) 9150cb2e609SWolfgang Helbig return (ndaysg(d)); 9160cb2e609SWolfgang Helbig else 9170cb2e609SWolfgang Helbig return (ndaysj(d)); 9180cb2e609SWolfgang Helbig else 9190cb2e609SWolfgang Helbig return ndaysg(d); 9200cb2e609SWolfgang Helbig } 9210cb2e609SWolfgang Helbig 9220cb2e609SWolfgang Helbig /* 9230cb2e609SWolfgang Helbig * Compute the number of days from date, obey the switch from 9240cb2e609SWolfgang Helbig * Julian to Gregorian as used by UK and her colonies. 9250cb2e609SWolfgang Helbig */ 9260cb2e609SWolfgang Helbig int 9270cb2e609SWolfgang Helbig sndaysb(struct date *d) 9280cb2e609SWolfgang Helbig { 9290cb2e609SWolfgang Helbig 9300cb2e609SWolfgang Helbig if (nswitchb < ndaysj(d)) 9310cb2e609SWolfgang Helbig return (ndaysg(d)); 9320cb2e609SWolfgang Helbig else 9330cb2e609SWolfgang Helbig return (ndaysj(d)); 9340cb2e609SWolfgang Helbig } 9350cb2e609SWolfgang Helbig 9360cb2e609SWolfgang Helbig /* Inverse of sndays */ 9370cb2e609SWolfgang Helbig struct date * 9380851fbdfSEdwin Groothuis sdater(int nd, struct date *d) 9390cb2e609SWolfgang Helbig { 9400cb2e609SWolfgang Helbig 9410cb2e609SWolfgang Helbig if (nswitch < nd) 9420cb2e609SWolfgang Helbig return (gdate(nd, d)); 9430cb2e609SWolfgang Helbig else 9440cb2e609SWolfgang Helbig return (jdate(nd, d)); 9450cb2e609SWolfgang Helbig } 9460cb2e609SWolfgang Helbig 9470cb2e609SWolfgang Helbig /* Inverse of sndaysb */ 9480cb2e609SWolfgang Helbig struct date * 9490cb2e609SWolfgang Helbig sdateb(int nd, struct date *d) 9500cb2e609SWolfgang Helbig { 9510cb2e609SWolfgang Helbig 9520cb2e609SWolfgang Helbig if (nswitchb < nd) 9530cb2e609SWolfgang Helbig return (gdate(nd, d)); 9540cb2e609SWolfgang Helbig else 9550cb2e609SWolfgang Helbig return (jdate(nd, d)); 9560cb2e609SWolfgang Helbig } 9570cb2e609SWolfgang Helbig 9580cb2e609SWolfgang Helbig /* Center string t in string s of length w by putting enough leading blanks */ 9590cb2e609SWolfgang Helbig char * 9600cb2e609SWolfgang Helbig center(char *s, char *t, int w) 9610cb2e609SWolfgang Helbig { 9620851fbdfSEdwin Groothuis char blanks[MAX_WIDTH]; 9630cb2e609SWolfgang Helbig 9640cb2e609SWolfgang Helbig memset(blanks, ' ', sizeof(blanks)); 9650cb2e609SWolfgang Helbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 9660cb2e609SWolfgang Helbig return (s); 9670cb2e609SWolfgang Helbig } 968ac6d1c22SPeter Pentchev 9694646cea7SDavid Schultz /* Center string t in string s of length w by putting enough leading blanks */ 9704646cea7SDavid Schultz wchar_t * 9714646cea7SDavid Schultz wcenter(wchar_t *s, wchar_t *t, int w) 9724646cea7SDavid Schultz { 9730851fbdfSEdwin Groothuis char blanks[MAX_WIDTH]; 9744646cea7SDavid Schultz 9754646cea7SDavid Schultz memset(blanks, ' ', sizeof(blanks)); 9764646cea7SDavid Schultz swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 9774646cea7SDavid Schultz return (s); 9784646cea7SDavid Schultz } 9794646cea7SDavid Schultz 980ac6d1c22SPeter Pentchev int 981ba29aec0SGarrett Wollman parsemonth(const char *s, int *m, int *y) 982ac6d1c22SPeter Pentchev { 983ba29aec0SGarrett Wollman int nm, ny; 984ac6d1c22SPeter Pentchev char *cp; 985ac6d1c22SPeter Pentchev struct tm tm; 986ac6d1c22SPeter Pentchev 987ba29aec0SGarrett Wollman nm = (int)strtol(s, &cp, 10); 988ba29aec0SGarrett Wollman if (cp != s) { 989ba29aec0SGarrett Wollman ny = *y; 990ba29aec0SGarrett Wollman if (*cp == '\0') { 991ba29aec0SGarrett Wollman ; /* no special action */ 992ba29aec0SGarrett Wollman } else if (*cp == 'f' || *cp == 'F') { 993ba29aec0SGarrett Wollman if (nm <= *m) 994ba29aec0SGarrett Wollman ny++; 995ba29aec0SGarrett Wollman } else if (*cp == 'p' || *cp == 'P') { 996ba29aec0SGarrett Wollman if (nm >= *m) 997ba29aec0SGarrett Wollman ny--; 998ba29aec0SGarrett Wollman } else 999ba29aec0SGarrett Wollman return (1); 1000ba29aec0SGarrett Wollman if (nm < 1 || nm > 12) 1001ba29aec0SGarrett Wollman return 1; 1002ba29aec0SGarrett Wollman *m = nm; 1003ba29aec0SGarrett Wollman *y = ny; 1004ac6d1c22SPeter Pentchev return (0); 1005ac6d1c22SPeter Pentchev } 1006ba29aec0SGarrett Wollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 1007ba29aec0SGarrett Wollman *m = tm.tm_mon + 1; 1008ba29aec0SGarrett Wollman return (0); 1009ba29aec0SGarrett Wollman } 1010ba29aec0SGarrett Wollman return (1); 1011ba29aec0SGarrett Wollman } 1012cae11c25SEdwin Groothuis 1013cae11c25SEdwin Groothuis void 1014cae11c25SEdwin Groothuis highlight(char *dst, char *src, int len, int *extralen) 1015cae11c25SEdwin Groothuis { 1016cae11c25SEdwin Groothuis static int first = 1; 1017cae11c25SEdwin Groothuis static const char *term_so, *term_se; 1018cae11c25SEdwin Groothuis 1019cae11c25SEdwin Groothuis if (first) { 1020cae11c25SEdwin Groothuis char tbuf[1024], cbuf[512], *b; 1021cae11c25SEdwin Groothuis 1022cae11c25SEdwin Groothuis term_se = term_so = NULL; 1023cae11c25SEdwin Groothuis 1024cae11c25SEdwin Groothuis /* On how to highlight on this type of terminal (if any) */ 1025cae11c25SEdwin Groothuis if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { 1026cae11c25SEdwin Groothuis b = cbuf; 1027cae11c25SEdwin Groothuis term_so = tgetstr("so", &b); 1028cae11c25SEdwin Groothuis term_se = tgetstr("se", &b); 1029cae11c25SEdwin Groothuis } 1030cae11c25SEdwin Groothuis 1031cae11c25SEdwin Groothuis first = 0; 1032cae11c25SEdwin Groothuis } 1033cae11c25SEdwin Groothuis 1034cae11c25SEdwin Groothuis /* 1035cae11c25SEdwin Groothuis * This check is not necessary, should have been handled before calling 1036cae11c25SEdwin Groothuis * this function. 1037cae11c25SEdwin Groothuis */ 1038cae11c25SEdwin Groothuis if (flag_nohighlight) { 1039cae11c25SEdwin Groothuis memcpy(dst, src, len); 1040cae11c25SEdwin Groothuis return; 1041cae11c25SEdwin Groothuis } 1042cae11c25SEdwin Groothuis 1043cae11c25SEdwin Groothuis /* If it is a real terminal, use the data from the termcap database. */ 1044cae11c25SEdwin Groothuis if (term_so != NULL && term_se != NULL) { 1045cae11c25SEdwin Groothuis /* separator */ 1046cae11c25SEdwin Groothuis dst[0] = ' '; 1047cae11c25SEdwin Groothuis dst++; 1048cae11c25SEdwin Groothuis /* highlight on */ 1049cae11c25SEdwin Groothuis memcpy(dst, term_so, strlen(term_so)); 1050cae11c25SEdwin Groothuis dst += strlen(term_so); 1051cae11c25SEdwin Groothuis /* the actual text (minus leading space) */ 1052cae11c25SEdwin Groothuis len--; 1053cae11c25SEdwin Groothuis src++; 1054cae11c25SEdwin Groothuis memcpy(dst, src, len); 1055cae11c25SEdwin Groothuis dst += len; 1056cae11c25SEdwin Groothuis /* highlight off */ 1057cae11c25SEdwin Groothuis memcpy(dst, term_se, strlen(term_se)); 1058cae11c25SEdwin Groothuis *extralen = strlen(term_so) + strlen(term_se); 1059cae11c25SEdwin Groothuis return; 1060cae11c25SEdwin Groothuis } 1061cae11c25SEdwin Groothuis 1062cae11c25SEdwin Groothuis /* 1063cae11c25SEdwin Groothuis * Otherwise, print a _, backspace and the letter 1064cae11c25SEdwin Groothuis */ 1065cae11c25SEdwin Groothuis *extralen = 0; 1066cae11c25SEdwin Groothuis /* skip leading space */ 1067cae11c25SEdwin Groothuis src++; 1068cae11c25SEdwin Groothuis len--; 1069cae11c25SEdwin Groothuis /* separator */ 1070cae11c25SEdwin Groothuis dst[0] = ' '; 1071cae11c25SEdwin Groothuis dst++; 1072cae11c25SEdwin Groothuis while (len > 0) { 1073cae11c25SEdwin Groothuis /* _ and backspace */ 1074cae11c25SEdwin Groothuis memcpy(dst, "_\010", 2); 1075cae11c25SEdwin Groothuis dst += 2; 1076cae11c25SEdwin Groothuis *extralen += 2; 1077cae11c25SEdwin Groothuis /* the character */ 1078cae11c25SEdwin Groothuis *dst++ = *src++; 1079cae11c25SEdwin Groothuis len--; 1080cae11c25SEdwin Groothuis } 1081cae11c25SEdwin Groothuis } 1082