forked from hebcal/hebcal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.c
925 lines (838 loc) · 29.8 KB
/
start.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/*
Hebcal - A Jewish Calendar Generator
Copyright (C) 1994-2006 Danny Sadinoff
Portions Copyright (c) 2010 Michael J. Radwin. All Rights Reserved.
https://github.com/hebcal/hebcal
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Danny Sadinoff can be reached at
*/
/* the following need to be #defined: CITY ENV_CITY ENV_OPTS */
#include <stdio.h>
#include <ctype.h>
#include "config.h"
#include <string.h>
#include <math.h>
#include "hebcal.h"
#include "common.h"
#include <errno.h>
#include <getopt.h>
#include "danlib.h"
#include <stdlib.h>
#include "cities.h"
#include "translations.h"
#define ENV_CITY "HEBCAL_CITY"
#define ENV_OPTS "HEBCAL_OPTS"
static int numYears = 1;
void handleArgs( int, char ** );
int main( int, char ** );
int ok_to_run = 1;
#define YEAR 0
#define MONTH 1
#define DAY 2
#define TODAY 3
char *progname;
static int theYear, theMonth, theDay, yearDirty, rangeType,
latp, longp; /* has the user inputted lat and long? */
static char
*cityName, *helpArray[] =
{
"hebcal [options] [[ month [ day ]] year ]",
"",
"hebcal help -- Print this message.",
"hebcal info -- Print version and localization data.",
"hebcal cities -- Print a list of available cities.",
"hebcal warranty -- Tells you how there's NO WARRANTY for hebcal.",
"hebcal copying -- Prints the details of the GNU copyright.",
"",
"Hebcal prints out Hebrew calendars one solar year at a time.",
"Given one argument, it will print out the calendar for that year.",
"Given two numeric arguments mm yyyy, it prints out the calendar for ",
"month mm of year yyyy.",
"",
"For example, ",
" hebcal -ho",
"will just print out the days of the omer for the current year.",
"Note: Use COMPLETE Years. You probably aren't interested in ",
"hebcal 93, but rather hebcal 1993.",
"", "",
"Hebcal is copyright (c) 1994-2011 By Danny Sadinoff",
"Portions Copyright (c) 2011 Michael J. Radwin. All rights reserved.",
"",
"This program is free software; you can redistribute it and/or",
"modify it under the terms of the GNU General Public License",
"as published by the Free Software Foundation; either version 2",
"of the License, or (at your option) any later version.",
"Type \"hebcal copying\" for more details.",
"",
"Hebcal is distributed in the hope that it will be useful,",
"but WITHOUT ANY WARRANTY; without even the implied warranty of",
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ",
"Type \"hebcal warranty\" for more details.",
" ",
"\"Free\" above means freely distributed. To donate money to support hebcal,",
" see the paypal link at http://www.sadinoff.com/hebcal/ "
"",
"WWW:",
" https://github.com/hebcal/hebcal"
};
static char* shortUsageArray[] = {
"Usage: hebcal [--ashkenazi] [--candle-mins CANDLE-MINS] [--candlelighting]",
" [--city CITY] [--latitude LATITUDE] [--longitude LONGITUDE]",
" [--havdalah-mins HAVDALAH-MINS] [--add-hebrew-dates]",
" [--add-hebrew-dates-for-events] [--euro-dates] [--24hour]",
" [--daf-yomi] [--no-holidays] [--lang LANG]",
" [--hebrew-date] [--help] [--israeli] [--infile INFILE]",
" [--molad] [--omer] [--sunrise-and-sunset] [--tabs]",
" [--sedrot] [--daily-sedra] [--today] [--today-brief]",
" [--version] [--weekday] [--abbreviated] [--no-rosh-chodesh]",
" [--year-abbrev] [--yahrtzeit YAHRTZEIT]",
" [--timezone TIMEZONE] [--years N] [--zmanim]",
""
};
static char* optionsHelpArray[] = {
"Options:",
" --help Show this help",
" Input ",
" -H, --hebrew-date Use Hebrew date ranges - only needed",
" when e.g. hebcal -H 5373",
" -I, --infile INFILE Get non-yahrtzeit Hebrew user events",
" from specified file. The format is:",
" mmm dd string, Where mmm is a Hebrew",
" month name",
" -t, --today Only output for today's date",
" -T, --today-brief Print today's pertinent information,",
" -Y, --yahrtzeit YAHRTZEIT Get yahrtzeit dates from specified",
" file. The format is: mm dd yyyy",
" string the first three fields",
" specify a *Gregorian* date.",
" no gregorian date",
" Output ",
" -a, --ashkenazi Use Ashkenazi Hebrew",
" -d, --add-hebrew-dates Print the Hebrew date for the entire",
" date range",
" -D, --add-hebrew-dates-for-events Print the Hebrew date for dates with",
" some event",
" -e, --euro-dates Output 'European' dates -- DD.MM.YYYY",
" -E, --24hour Output 24-hour times (e.g. 18:37",
" instead of 6:37)",
" -F, --daf-yomi Output the Daf Yomi for the entire",
" date range",
" -g, --iso-8601 Output ISO 8601 dates -- YYYY-MM-DD",
" this overrides -y",
" -h, --no-holidays Suppress default holidays",
" -i, --israeli Israeli holiday and sedra schedule",
" --lang LANG Use LANG titles (" HEBCAL_LANG_LIST ")",
" -M, --molad Print the molad on Shabbat Mevorchim",
" --no-modern Suppress modern holidays",
" -o, --omer Add days of the Omer",
" -O, --sunrise-and-sunset Output sunrise and sunset times every",
" day",
" -r, --tabs Tab delineated format",
" -s, --sedrot Add weekly sedrot on Saturday",
" -S, --daily-sedra Print sedrah of the week on all",
" calendar days",
" --version Show version number",
" -w, --weekday Add day of the week",
" -W, --abbreviated Weekly view. Omer, dafyomi, and",
" non-date-specific zemanim are shown",
" once a week, on the day which",
" corresponds to the first day in the",
" range.",
" -x, --no-rosh-chodesh Suppress Rosh Chodesh",
" -y, --year-abbrev Print only last two digits of year",
" --years N Generate events for N years (default 1)",
"",
"Candle lighting:",
" Options related to candle-lighting times.",
"",
" -b, --candle-mins CANDLE-MINS Set candle-lighting to occur this",
" many minutes before sundown",
" -c, --candlelighting Print candlelighting times",
" -C, --city CITY City for candle-lighting",
" -l, --latitude LATITUDE Set the latitude for solar",
" calculations to xx degrees and yy",
" minutes. Negative values are south",
" -L, --longitude LONGITUDE Set the longitude for solar",
" calculations to xx degrees and yy",
" minutes. Negative values are EAST.",
" The -l and -L switches must both be",
" used, or not at all. These switches",
" override the -C (localize to city)",
" switch",
" -m, --havdalah-mins HAVDALAH-MINS Set Havdalah to occur this many",
" minutes after sundown",
" -z, --timezone TIMEZONE Use specified timezone, overriding",
" the -C (localize to city) switch",
" -Z, --zmanim Print zemanim (experimental)",
""
};
void print_version_data(void)
{
const timelib_tzdb *db = timelib_builtin_db();
printf("hebcal version " VERSION "\n");
printf("timezone_db version %s\n", db->version);
printf("=====Defaults=====\n");
printf("City: %s\n", cityName);
printf(" %dd%d' %c latitude\n", abs(latdeg), abs(latmin),
latdeg < 0 ? 'S' : 'N');
printf(" %dd%d' %c longitude\n", abs(longdeg), abs(longmin),
longdeg > 0 ? 'W' : 'E');
printf(" Timezone: %s\n", TZ_INFO->name);
printf("\nEnvironment variable for default city: %s\n", ENV_CITY);
printf("\nEnvironment variable for default options: %s\n", ENV_OPTS);
}
void print_warranty( void )
{
int cnum;
for (cnum = 0;
warranty[cnum];
cnum++)
{
puts(warranty[cnum]);
}
}
void print_copying( void )
{
int cnum;
for (cnum = 0;
license[cnum];
cnum++)
{
puts(license[cnum]);
}
for (cnum = 0;
warranty[cnum];
cnum++)
{
puts(warranty[cnum]);
}
}
void print_city_data( void )
{
int cnum;
for (cnum = 0;
cities[cnum].name;
cnum++)
{
printf("%s (%dd%d' %c lat, %dd%d' %c long, %s)\n",
cities[cnum].name,
abs(cities[cnum].latdeg), abs(cities[cnum].latmin),
cities[cnum].latdeg < 0 ? 'S' : 'N',
abs(cities[cnum].longdeg), abs(cities[cnum].longmin),
cities[cnum].longdeg > 0 ? 'W' : 'E',
cities[cnum].tz
);
}
}
void localize_to_city(const char *cityNameArg)
{
size_t len = strlen(cityNameArg);
char *pc, *cityStr;
city_t *pcity;
initStr(&cityStr, strlen(cityNameArg));
strcpy(cityStr, cityNameArg);
if (cityName != NULL)
free(cityName);
/* convert non-alpha to spaces */
for ( pc = cityStr; *pc != '\0'; pc++ )
if ( ! isalpha( (int)*pc ) )
*pc = ' ';
for (pcity = &cities[0]; pcity->name != NULL; pcity++)
if (0 == istrncasecmp(len, cityStr, pcity->name))
{
int error_code = 0;
if (!(longp || latp)) /* -l and -L override -C */
{
latdeg = pcity->latdeg;
latmin = pcity->latmin;
longdeg = pcity->longdeg;
longmin = pcity->longmin;
}
TZ_INFO = timelib_parse_tzfile(pcity->tz, timelib_builtin_db(), &error_code);
if (TZ_INFO == NULL) {
fprintf(stderr, "timelib error: %s\n", timelib_get_error_message(error_code));
die("unable to read time zone argument: %s", pcity->tz);
}
free(cityStr);
initStr(&cityName, strlen(pcity->name));
strcpy(cityName, pcity->name);
return;
}
warn("unknown city: %s. Use a nearby city or geographic coordinates.", cityNameArg);
warn("run 'hebcal cities' for a list of cities.", "");
ok_to_run = 0;
}
void set_default_city( void )
{
const char *cityName;
#ifdef CITY
localize_to_city(CITY);
#else
localize_to_city("NEW YORK"); /* helluva town. */
#endif
/* having set the default city, now check if there is an overriding */
/* default from the environment. */
if (NULL != (cityName = getenv(ENV_CITY)))
localize_to_city(cityName);
}
static void displayHelp() {
size_t i;
printf("Usage: %s\n", helpArray[0]);
for (i = 0; i < sizeof(optionsHelpArray) / sizeof(char *); i++) {
puts(optionsHelpArray[i]);
}
for (i = 1; i < sizeof(helpArray) / sizeof(char *); i++) {
puts(helpArray[i]);
}
}
static void shortUsage() {
size_t i;
for (i = 0; i < sizeof(shortUsageArray) / sizeof(char *); i++) {
puts(shortUsageArray[i]);
}
}
void handleArgs(int argc, char *argv[])
{
date_t greg_today;
char **remain;
size_t remain_count;
char *cityNameArg = NULL;
char *inFileName = NULL;
char *tzid = NULL;
char *yahrtzeitFileName = NULL;
int today_sw = 0;
int zemanim_sw = 0;
int help_sw = 0;
int version_sw = 0;
int utf8_hebrew_sw = 0;
char *latitudeStr = NULL;
char *longitudeStr = NULL;
char *langStr = NULL;
static struct option long_options[] = {
{"24hour", no_argument, 0, 'E'},
{"abbreviated", no_argument, 0, 'W'},
{"add-hebrew-dates", no_argument, 0, 'd'},
{"add-hebrew-dates-for-events", no_argument, 0, 'D'},
{"ashkenazi", no_argument, 0, 'a'},
{"candle-mins", required_argument, 0, 'b'},
{"candlelighting", no_argument, 0, 'c'},
{"city", required_argument, 0, 'C'},
{"daf-yomi", no_argument, 0, 'F'},
{"daily-sedra", no_argument, 0, 'S'},
{"euro-dates", no_argument, 0, 'e'},
{"iso-8601", no_argument, 0, 'g'},
{"format", required_argument, 0, 'f'},
{"havdalah-mins", required_argument, 0, 'm'},
{"hebrew-date", no_argument, 0, 'H'},
{"help", no_argument, 0, 0},
{"infile", required_argument, 0, 'I'},
{"israeli", no_argument, 0, 'i'},
{"latitude", required_argument, 0, 'l'},
{"lang", required_argument, 0, 0},
{"longitude", required_argument, 0, 'L'},
{"molad", no_argument, 0, 'M'},
{"no-holidays", no_argument, 0, 'h'},
{"no-modern", no_argument, 0, 0},
{"no-rosh-chodesh", no_argument, 0, 'x'},
{"omer", no_argument, 0, 'o'},
{"sedrot", no_argument, 0, 's'},
{"sunrise-and-sunset", no_argument, 0, 'O'},
{"tabs", no_argument, 0, 'r'},
{"timezone", required_argument, 0, 'z'},
{"today", no_argument, 0, 't'},
{"today-brief", no_argument, 0, 'T'},
{"version", no_argument, 0, 0},
{"weekday", no_argument, 0, 'w'},
{"yahrtzeit", required_argument, 0, 'Y'},
{"years", required_argument, 0, 0},
{"year-abbrev", no_argument, 0, 'y'},
{"zmanim", no_argument, 0, 'Z'},
{NULL, 0, NULL, 0}
};
int c;
int option_index = 0;
setDate(&greg_today); /* keep the current greg. date here */
while ((c = getopt_long(argc, argv, "ab:cC:dDeEFghHI:il:L:m:MoOrsStTwWxyY:z:Z8",
long_options, &option_index)) != -1) {
switch (c) {
case 0: /* long option without short alias */
if (0 == strcmp("version", long_options[option_index].name)) {
version_sw = 1;
} else if (0 == strcmp("help", long_options[option_index].name)) {
help_sw = 1;
} else if (0 == strcmp("lang", long_options[option_index].name)) {
langStr = strdup(optarg);
} else if (0 == strcmp("years", long_options[option_index].name)) {
if (!(sscanf(optarg, "%d", &numYears) == 1)) {
die("unable to read --years argument: %s", optarg);
}
} else if (0 == strcmp("no-modern", long_options[option_index].name)) {
suppressModern_sw = 1;
} else {
shortUsage();
exit(1);
}
break;
case 'a':
ashkenazis_sw = 1;
break;
case '8':
utf8_hebrew_sw = 1;
break;
case 'b': /* candle_lighting_minutes_before_sundown */
if (!(sscanf(optarg, "%d", &light_offset) == 1))
die("unable to read candle_lighting_minutes_before_sundown argument: %s", optarg);
break;
case 'c': /* calculate candlelighting times on fridays */
candleLighting_sw = 1;
break;
case 'C':
cityNameArg = strdup(optarg);
break;
case 'd': /* print hebrew date */
printHebDates_sw = 1;
break;
case 'D': /* print hebrew date when there's */
/* something else to print */
printSomeHebDates_sw = 1;
break;
case 'I': /* input file */
inFileName = strdup(optarg);
break;
case 'e': /* european date format */
gregDateOutputFormatCode_sw = GREG_DATEFORMAT_EURO;
break;
case 'g': /* iso date format */
gregDateOutputFormatCode_sw = GREG_DATEFORMAT_ISO;
break;
case 'E': /* 24-hour time format */
twentyFourHour_sw = 1;
break;
case 'F': /* Daf Yomi */
dafYomi_sw = 1;
break;
case 'h': /* suppress internal holidays */
noHolidays_sw = 1;
break;
case 'H': /* suppress use hebrew range dates */
hebrewDates_sw = 1;
break;
case 'i': /* use Israeli sedra scheme */
israel_sw = 1;
break;
case 'l': /* latitude */
latitudeStr = strdup(optarg);
break;
case 'L': /* longitude */
longitudeStr = strdup(optarg);
break;
case 'm': /* havdalah_minutes */
if (!(sscanf(optarg, "%d", &havdalah_minutes) == 1))
die("unable to read havdalah_minutes argument: %s", optarg);
break;
case 'M': /* print the molad */
printMolad_sw = 1;
break;
case 'o': /* print the omer */
printOmer_sw = 1;
break;
case 'O': /* print sunrise and sunset */
printSunriseSunset_sw = 1;
break;
case 'r': /* Tab-delineated Format */
tabs_sw = 1;
break;
case 's': /* print sedrot */
sedrot_sw = 1;
break;
case 'S': /* print sedra every day. */
sedraAllWeek_sw = 1;
break;
case 'T': /* do hebcal for today, omit gregorian date. */
noGreg_sw = 1;
break;
case 't': /* do hebcal for today. */
today_sw = 1;
break;
case 'w': /* print days of the week */
weekday_sw = 1;
break;
case 'W': /* abbreviated week view */
abbrev_sw = 1;
break;
case 'y': /* Print only last 2 digits of year */
yearDigits_sw = 1;
break;
case 'Y': /* input file */
yahrtzeitFileName = strdup(optarg);
break;
case 'x': /* input file */
suppress_rosh_chodesh_sw = 1;
break;
case 'z': /* time zone */
tzid = strdup(optarg);
break;
case 'Z':
zemanim_sw = 1;
break;
default:
shortUsage();
exit(1);
break;
}
}
if (langStr != NULL) {
hebcal_lang lang = hebcal_get_language(langStr);
if (lang == HEBCAL_LANG_UNDEFINED) {
warn("Unknown lang '%s'; using default", langStr);
} else {
hebcal_set_language(lang);
}
free(langStr);
} else if (ashkenazis_sw) {
hebcal_set_language(HEBCAL_LANG_ASHKENAZI);
} else if (utf8_hebrew_sw) {
hebcal_set_language(HEBCAL_LANG_HE);
} else {
hebcal_set_language(HEBCAL_LANG_DEFAULT);
}
if (help_sw) {
displayHelp();
exit(0);
}
if (version_sw) {
printf("Hebcal version %s\n", VERSION);
exit(0);
}
errno = 0;
if (light_offset > 0) {
light_offset *= -1;
}
if (cityNameArg != NULL) {
candleLighting_sw = 1;
localize_to_city(cityNameArg);
free(cityNameArg);
}
if (inFileName != NULL) {
inputFile_sw = 1;
if (!(inFile = fopen(inFileName, "r"))) {
die("could not open input file %s.", inFileName);
}
free(inFileName);
}
if (noGreg_sw) {
today_sw = 1;
}
if (today_sw) {
printHebDates_sw = 1;
rangeType = TODAY;
theMonth = greg_today.mm; /* year and month specified */
theDay = greg_today.dd; /* printc theDay of theMonth */
yearDirty = 1;
printOmer_sw = 1;
}
if (yahrtzeitFileName != NULL) {
yahrtzeitFile_sw = 1;
if (!(yFile = fopen(yahrtzeitFileName, "r"))) {
die("could not open yahrtzeit input file %s.", yahrtzeitFileName);
}
free(yahrtzeitFileName);
}
if (latitudeStr != NULL) {
if (sscanf(latitudeStr, "%d,%d", &latdeg, &latmin) < 2) {
die("unable to read latitude argument: %s", latitudeStr);
}
latp = 1;
if ((abs(latdeg) > 90) || latmin > 60 || latmin < 0) {
die("Error, latitude argument out of range: %s", latitudeStr);
}
latmin = abs(latmin);
if (latdeg < 0) {
latmin = -latmin;
}
free(latitudeStr);
}
if (longitudeStr != NULL) {
if (sscanf(longitudeStr, "%d,%d", &longdeg, &longmin) < 2) {
die("unable to read longitude argument: %s", longitudeStr);
}
longp = 1;
if ((abs(longdeg) > 180) || longmin > 60 || longmin < 0) {
die("Error, longitude argument out of range: %s", longitudeStr);
}
longmin = abs(longmin);
if (longdeg < 0) {
longmin = -longmin;
}
free(longitudeStr);
}
if (tzid != NULL) {
int error_code = 0;
TZ_INFO = timelib_parse_tzfile(tzid, timelib_builtin_db(), &error_code);
if (TZ_INFO == NULL) {
fprintf(stderr, "timelib error: %s\n", timelib_get_error_message(error_code));
die("unable to read time zone argument: %s", tzid);
}
free(tzid);
}
if (zemanim_sw) {
default_zemanim = (ZMAN_SUNRISE | ZMAN_SZKS | ZMAN_TEFILAH |
ZMAN_CHATZOT |
ZMAN_MINCHA_GEDOLA | ZMAN_MINCHA_KETANA |
ZMAN_PLAG_HAMINCHA | ZMAN_SUNSET | ZMAN_TZAIT_42);
}
if (latp)
cityName = "User Defined City";
if (latp ^ longp)
die("You must enter BOTH the latitude and the longitude", "");
if( !strcmp(cityName, "Jerusalem" ))
light_offset = -40; /* does everyone hold by this? */
remain_count = argc - optind;
remain = argv + optind;
switch (remain_count) /* suck up the date */
{
case 0: /* process this year */
if (hebrewDates_sw)
theYear = abs2hebrew(greg2abs(greg_today)).yy;
else
theYear = greg_today.yy;
break;
case 1:
if (isAllNums(remain[0]))
{
theYear = atoi(remain[0]); /* just year specified */
yearDirty = 1; /* print whole year */
}
else if (0 == istrncasecmp(5, remain[0], "help"))
{
displayHelp();
exit(0);
}
else if (0 == istrncasecmp(3, remain[0], "info"))
{
print_version_data();
exit(0);
}
else if (0 == istrncasecmp(3, remain[0], "cities"))
{
print_city_data();
exit(0);
}
else if (0 == istrncasecmp(3, remain[0], "copying"))
{
print_copying();
exit(0);
}
else if (0 == istrncasecmp(3, remain[0], "warranty"))
{
print_warranty();
exit(0);
}
else
{
printf("%s: unrecognized command '%s'\n", progname, remain[0]);
shortUsage();
exit(1);
}
break;
case 2:
if (!isAllNums(remain[1])) {
shortUsage();
exit(1);
}
theYear = atoi(remain[1]); /* print theMonth of theYear */
theMonth = lookup_hebrew_month(remain[0]);
if (theMonth) {
hebrewDates_sw = 1; /* automagically turn it on */
if (theMonth == ADAR_II && !LEAP_YR_HEB(theYear)) {
theMonth = ADAR_I; /* silently fix this mistake */
}
} else if (isAllNums(remain[0])) {
if (hebrewDates_sw) {
die("Don't use numbers to specify Hebrew months.", "");
} else {
theMonth = atoi(remain[0]); /* gregorian month */
}
} else if (hebrewDates_sw) {
die("Unknown Hebrew month: %s", remain[0]);
} else {
die("bad gregorian month: %s", remain[0]);
}
yearDirty = 1;
rangeType = MONTH;
break;
case 3:
if (!(isAllNums(remain[1]) && isAllNums(remain[2]))) {
shortUsage();
exit(1);
}
theDay = atoi(remain[1]); /* print theDay of theMonth */
theYear = atoi(remain[2]); /* print theMonth of theYear */
theMonth = lookup_hebrew_month(remain[0]);
if (theMonth) {
hebrewDates_sw = 1; /* automagically turn it on */
if (theMonth == ADAR_II && !LEAP_YR_HEB(theYear)) {
theMonth = ADAR_I; /* silently fix this mistake */
}
} else if (isAllNums(remain[0])) {
if (hebrewDates_sw) {
die("Don't use numbers to specify Hebrew months.", "");
} else {
theMonth = atoi(remain[0]); /* gregorian month */
}
} else if (hebrewDates_sw) {
die("Unknown Hebrew month: %s", remain[0]);
} else {
die("bad gregorian month: %s", remain[0]);
}
if (theDay < 1)
die("The day of the month must be greater than 0", "");
if (theMonth < 1)
die("The month must be greater than 0", "");
if (hebrewDates_sw)
{
if (theDay > max_days_in_heb_month(theMonth, theYear))
die("Sorry, there aren't that many days in %s (then)",
LANGUAGE2(hMonths[LEAP_YR_HEB(theYear)][theMonth].name));
}
else
{
if (theMonth > 12)
die("The month must be less than 13", "");
if (theDay > getMonthLength(theYear, theMonth))
die("Sorry, there aren't that many days in %s (then)",
eMonths[theMonth]);
}
rangeType = DAY;
yearDirty = 1;
break;
default:
displayHelp();
exit(1);
}
if (numYears != 1 && rangeType != YEAR) {
die("Sorry, --years option works only with entire-year calendars", "");
}
}
int tokenize(char *str, int *pargc, char* argv[])
{
/* char *strtok(), *strdup(); */
char *s;
argv[0] = progname;
s = strtok(str, "\t ");
*pargc = 1;
do
{
argv[(*pargc)++] = strdup(s);
} while ((s = strtok(NULL, "\t ")));
return *pargc > 1;
}
int main(int argc, char* argv[])
{
date_t tempDate;
long startAbs, endAbs;
char *envStr;
int envArgc;
char *envArgv[40]; /* this should be big enough */
progname = argv[0];
set_default_city();
if ((envStr = getenv(ENV_OPTS)) && strcmp(envStr, ""))
{
int i;
tokenize(envStr, &envArgc, envArgv);
for (i = 1; i < argc; i++) /* append argv onto envArgv */
envArgv[envArgc++] = argv[i];
handleArgs(envArgc, envArgv);
}
else
handleArgs(argc, argv);
tempDate.yy = theYear;
if (theYear < (hebrewDates_sw ? 3761 : 1))
die("Sorry, hebcal can only handle dates in the common era.", "");
switch (rangeType)
{
case TODAY:
printHebDates_sw = 1;
tempDate.dd = theDay;
tempDate.mm = theMonth;
tempDate.yy = theYear;
startAbs = endAbs = greg2abs(tempDate);
break;
case DAY:
printHebDates_sw = 1;
tempDate.dd = theDay;
tempDate.mm = theMonth;
tempDate.yy = theYear;
if (hebrewDates_sw)
startAbs = endAbs = hebrew2abs(tempDate);
else
startAbs = endAbs = greg2abs(tempDate);
break;
case MONTH:
tempDate.dd = 1;
tempDate.mm = theMonth;
tempDate.yy = theYear;
if (hebrewDates_sw)
{
startAbs = hebrew2abs(tempDate);
tempDate.dd = max_days_in_heb_month(tempDate.mm, tempDate.yy);
endAbs = hebrew2abs(tempDate);
}
else
{
startAbs = greg2abs(tempDate);
tempDate.dd = getMonthLength(theYear, theMonth);
endAbs = greg2abs(tempDate);
}
break;
case YEAR:
if (hebrewDates_sw)
{
tempDate.dd = 1;
tempDate.mm = TISHREI;
tempDate.yy = theYear;
startAbs = hebrew2abs(tempDate);
/* start yearly calendar with the day before RH (i.e. Erev
* Rosh Hashanah)
*/
startAbs--;
tempDate.yy += numYears;
endAbs = hebrew2abs(tempDate) - 1;
}
else
{
tempDate.dd = 1;
tempDate.mm = JAN;
tempDate.yy = theYear;
startAbs = greg2abs(tempDate);
tempDate.yy += numYears;
endAbs = greg2abs(tempDate) - 1;
}
break;
default:
die("Oh, NO! internal error #17q!", "");
/* this is dead code, but it silences some uninitialized variable
warnings in gcc */
startAbs = endAbs =0;
}
tempDate = abs2hebrew(startAbs);
if (ok_to_run)
{
init_holidays(tempDate.yy); /* load the holiday array */
main_calendar(startAbs, endAbs);
return 0; /* success! Kol hakavod to thorough programmers */
}
else
return 1;
}