PageRenderTime 96ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/texlive-2007/libs/curl/lib/getdate.y

#
Happy | 1105 lines | 1012 code | 93 blank | 0 comment | 0 complexity | f20e094be9cf7fdeb83af855f6528200 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, LGPL-3.0, GPL-2.0, CPL-1.0, LGPL-2.1, LGPL-2.0
  1. %{
  2. /*
  3. ** Originally written by Steven M. Bellovin <smb@research.att.com> while
  4. ** at the University of North Carolina at Chapel Hill. Later tweaked by
  5. ** a couple of people on Usenet. Completely overhauled by Rich $alz
  6. ** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
  7. **
  8. ** This code is in the public domain and has no copyright.
  9. */
  10. #include "setup.h"
  11. # ifdef HAVE_ALLOCA_H
  12. # include <alloca.h>
  13. # endif
  14. # ifdef HAVE_TIME_H
  15. # include <time.h>
  16. # endif
  17. #ifndef YYDEBUG
  18. /* to satisfy gcc -Wundef, we set this to 0 */
  19. #define YYDEBUG 0
  20. #endif
  21. /* Since the code of getdate.y is not included in the Emacs executable
  22. itself, there is no need to #define static in this file. Even if
  23. the code were included in the Emacs executable, it probably
  24. wouldn't do any harm to #undef it here; this will only cause
  25. problems if we try to write to a static variable, which I don't
  26. think this code needs to do. */
  27. #ifdef emacs
  28. # undef static
  29. #endif
  30. #ifdef __APPLE__
  31. #include <sys/types.h>
  32. #include <sys/malloc.h>
  33. #else
  34. #endif
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #if HAVE_STDLIB_H
  39. # include <stdlib.h> /* for `free'; used by Bison 1.27 */
  40. #else
  41. #ifdef HAVE_MALLOC_H
  42. #include <malloc.h>
  43. #endif
  44. #endif
  45. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  46. # define IN_CTYPE_DOMAIN(c) 1
  47. #else
  48. # define IN_CTYPE_DOMAIN(c) isascii(c)
  49. #endif
  50. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
  51. #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
  52. #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
  53. #define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
  54. /* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
  55. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  56. - It's guaranteed to evaluate its argument exactly once.
  57. - It's typically faster.
  58. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
  59. only '0' through '9' are digits. Prefer ISDIGIT to ISDIGIT_LOCALE unless
  60. it's important to use the locale's definition of `digit' even when the
  61. host does not conform to Posix. */
  62. #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
  63. #if defined (STDC_HEADERS) || defined (USG)
  64. # include <string.h>
  65. #endif
  66. /* The last #include file should be: */
  67. #ifdef MALLOCDEBUG
  68. #include "memdebug.h"
  69. #endif
  70. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  71. # define __attribute__(x)
  72. #endif
  73. #ifndef ATTRIBUTE_UNUSED
  74. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  75. #endif
  76. /* Some old versions of bison generate parsers that use bcopy.
  77. That loses on systems that don't provide the function, so we have
  78. to redefine it here. */
  79. #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
  80. # define bcopy(from, to, len) memcpy ((to), (from), (len))
  81. #endif
  82. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  83. as well as gratuitiously global symbol names, so we can have multiple
  84. yacc generated parsers in the same program. Note that these are only
  85. the variables produced by yacc. If other parser generators (bison,
  86. byacc, etc) produce additional global names that conflict at link time,
  87. then those parser generators need to be fixed instead of adding those
  88. names to this list. */
  89. #define yymaxdepth Curl_gd_maxdepth
  90. #define yyparse Curl_gd_parse
  91. #define yylex Curl_gd_lex
  92. #define yyerror Curl_gd_error
  93. #define yylval Curl_gd_lval
  94. #define yychar Curl_gd_char
  95. #define yydebug Curl_gd_debug
  96. #define yypact Curl_gd_pact
  97. #define yyr1 Curl_gd_r1
  98. #define yyr2 Curl_gd_r2
  99. #define yydef Curl_gd_def
  100. #define yychk Curl_gd_chk
  101. #define yypgo Curl_gd_pgo
  102. #define yyact Curl_gd_act
  103. #define yyexca Curl_gd_exca
  104. #define yyerrflag Curl_gd_errflag
  105. #define yynerrs Curl_gd_nerrs
  106. #define yyps Curl_gd_ps
  107. #define yypv Curl_gd_pv
  108. #define yys Curl_gd_s
  109. #define yy_yys Curl_gd_yys
  110. #define yystate Curl_gd_state
  111. #define yytmp Curl_gd_tmp
  112. #define yyv Curl_gd_v
  113. #define yy_yyv Curl_gd_yyv
  114. #define yyval Curl_gd_val
  115. #define yylloc Curl_gd_lloc
  116. #define yyreds Curl_gd_reds /* With YYDEBUG defined */
  117. #define yytoks Curl_gd_toks /* With YYDEBUG defined */
  118. #define yylhs Curl_gd_yylhs
  119. #define yylen Curl_gd_yylen
  120. #define yydefred Curl_gd_yydefred
  121. #define yydgoto Curl_gd_yydgoto
  122. #define yysindex Curl_gd_yysindex
  123. #define yyrindex Curl_gd_yyrindex
  124. #define yygindex Curl_gd_yygindex
  125. #define yytable Curl_gd_yytable
  126. #define yycheck Curl_gd_yycheck
  127. static int yylex ();
  128. static int yyerror ();
  129. #define EPOCH 1970
  130. #define HOUR(x) ((x) * 60)
  131. #define MAX_BUFF_LEN 128 /* size of buffer to read the date into */
  132. /*
  133. ** An entry in the lexical lookup table.
  134. */
  135. typedef struct _TABLE {
  136. const char *name;
  137. int type;
  138. int value;
  139. } TABLE;
  140. /*
  141. ** Meridian: am, pm, or 24-hour style.
  142. */
  143. typedef enum _MERIDIAN {
  144. MERam, MERpm, MER24
  145. } MERIDIAN;
  146. /* parse results and input string */
  147. typedef struct _GETDATE_CONTEXT {
  148. const char *yyInput;
  149. int yyDayOrdinal;
  150. int yyDayNumber;
  151. int yyHaveDate;
  152. int yyHaveDay;
  153. int yyHaveRel;
  154. int yyHaveTime;
  155. int yyHaveZone;
  156. int yyTimezone;
  157. int yyDay;
  158. int yyHour;
  159. int yyMinutes;
  160. int yyMonth;
  161. int yySeconds;
  162. int yyYear;
  163. MERIDIAN yyMeridian;
  164. int yyRelDay;
  165. int yyRelHour;
  166. int yyRelMinutes;
  167. int yyRelMonth;
  168. int yyRelSeconds;
  169. int yyRelYear;
  170. } GETDATE_CONTEXT;
  171. /* enable use of extra argument to yyparse and yylex which can be used to pass
  172. ** in a user defined value (GETDATE_CONTEXT struct in our case)
  173. */
  174. #define YYPARSE_PARAM cookie
  175. #define YYLEX_PARAM cookie
  176. #define context ((GETDATE_CONTEXT *) cookie)
  177. %}
  178. /* This grammar has 13 shift/reduce conflicts. */
  179. %expect 13
  180. /* turn global variables into locals, additionally enable extra arguments
  181. ** for yylex (pointer to yylval and user defined value)
  182. */
  183. %pure_parser
  184. %union {
  185. int Number;
  186. enum _MERIDIAN Meridian;
  187. }
  188. %token tAGO tDAY tDAY_UNIT tDAYZONE tDST tHOUR_UNIT tID
  189. %token tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  190. %token tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  191. %type <Number> tDAY tDAY_UNIT tDAYZONE tHOUR_UNIT tMINUTE_UNIT
  192. %type <Number> tMONTH tMONTH_UNIT
  193. %type <Number> tSEC_UNIT tSNUMBER tUNUMBER tYEAR_UNIT tZONE
  194. %type <Meridian> tMERIDIAN o_merid
  195. %%
  196. spec : /* NULL */
  197. | spec item
  198. ;
  199. item : time {
  200. context->yyHaveTime++;
  201. }
  202. | zone {
  203. context->yyHaveZone++;
  204. }
  205. | date {
  206. context->yyHaveDate++;
  207. }
  208. | day {
  209. context->yyHaveDay++;
  210. }
  211. | rel {
  212. context->yyHaveRel++;
  213. }
  214. | number
  215. ;
  216. time : tUNUMBER tMERIDIAN {
  217. context->yyHour = $1;
  218. context->yyMinutes = 0;
  219. context->yySeconds = 0;
  220. context->yyMeridian = $2;
  221. }
  222. | tUNUMBER ':' tUNUMBER o_merid {
  223. context->yyHour = $1;
  224. context->yyMinutes = $3;
  225. context->yySeconds = 0;
  226. context->yyMeridian = $4;
  227. }
  228. | tUNUMBER ':' tUNUMBER tSNUMBER {
  229. context->yyHour = $1;
  230. context->yyMinutes = $3;
  231. context->yyMeridian = MER24;
  232. context->yyHaveZone++;
  233. context->yyTimezone = ($4 < 0
  234. ? -$4 % 100 + (-$4 / 100) * 60
  235. : - ($4 % 100 + ($4 / 100) * 60));
  236. }
  237. | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  238. context->yyHour = $1;
  239. context->yyMinutes = $3;
  240. context->yySeconds = $5;
  241. context->yyMeridian = $6;
  242. }
  243. | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  244. context->yyHour = $1;
  245. context->yyMinutes = $3;
  246. context->yySeconds = $5;
  247. context->yyMeridian = MER24;
  248. context->yyHaveZone++;
  249. context->yyTimezone = ($6 < 0
  250. ? -$6 % 100 + (-$6 / 100) * 60
  251. : - ($6 % 100 + ($6 / 100) * 60));
  252. }
  253. ;
  254. zone : tZONE {
  255. context->yyTimezone = $1;
  256. }
  257. | tDAYZONE {
  258. context->yyTimezone = $1 - 60;
  259. }
  260. |
  261. tZONE tDST {
  262. context->yyTimezone = $1 - 60;
  263. }
  264. ;
  265. day : tDAY {
  266. context->yyDayOrdinal = 1;
  267. context->yyDayNumber = $1;
  268. }
  269. | tDAY ',' {
  270. context->yyDayOrdinal = 1;
  271. context->yyDayNumber = $1;
  272. }
  273. | tUNUMBER tDAY {
  274. context->yyDayOrdinal = $1;
  275. context->yyDayNumber = $2;
  276. }
  277. ;
  278. date : tUNUMBER '/' tUNUMBER {
  279. context->yyMonth = $1;
  280. context->yyDay = $3;
  281. }
  282. | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  283. /* Interpret as YYYY/MM/DD if $1 >= 1000, otherwise as MM/DD/YY.
  284. The goal in recognizing YYYY/MM/DD is solely to support legacy
  285. machine-generated dates like those in an RCS log listing. If
  286. you want portability, use the ISO 8601 format. */
  287. if ($1 >= 1000)
  288. {
  289. context->yyYear = $1;
  290. context->yyMonth = $3;
  291. context->yyDay = $5;
  292. }
  293. else
  294. {
  295. context->yyMonth = $1;
  296. context->yyDay = $3;
  297. context->yyYear = $5;
  298. }
  299. }
  300. | tUNUMBER tSNUMBER tSNUMBER {
  301. /* ISO 8601 format. yyyy-mm-dd. */
  302. context->yyYear = $1;
  303. context->yyMonth = -$2;
  304. context->yyDay = -$3;
  305. }
  306. | tUNUMBER tMONTH tSNUMBER {
  307. /* e.g. 17-JUN-1992. */
  308. context->yyDay = $1;
  309. context->yyMonth = $2;
  310. context->yyYear = -$3;
  311. }
  312. | tMONTH tUNUMBER {
  313. context->yyMonth = $1;
  314. context->yyDay = $2;
  315. }
  316. | tMONTH tUNUMBER ',' tUNUMBER {
  317. context->yyMonth = $1;
  318. context->yyDay = $2;
  319. context->yyYear = $4;
  320. }
  321. | tUNUMBER tMONTH {
  322. context->yyMonth = $2;
  323. context->yyDay = $1;
  324. }
  325. | tUNUMBER tMONTH tUNUMBER {
  326. context->yyMonth = $2;
  327. context->yyDay = $1;
  328. context->yyYear = $3;
  329. }
  330. ;
  331. rel : relunit tAGO {
  332. context->yyRelSeconds = -context->yyRelSeconds;
  333. context->yyRelMinutes = -context->yyRelMinutes;
  334. context->yyRelHour = -context->yyRelHour;
  335. context->yyRelDay = -context->yyRelDay;
  336. context->yyRelMonth = -context->yyRelMonth;
  337. context->yyRelYear = -context->yyRelYear;
  338. }
  339. | relunit
  340. ;
  341. relunit : tUNUMBER tYEAR_UNIT {
  342. context->yyRelYear += $1 * $2;
  343. }
  344. | tSNUMBER tYEAR_UNIT {
  345. context->yyRelYear += $1 * $2;
  346. }
  347. | tYEAR_UNIT {
  348. context->yyRelYear += $1;
  349. }
  350. | tUNUMBER tMONTH_UNIT {
  351. context->yyRelMonth += $1 * $2;
  352. }
  353. | tSNUMBER tMONTH_UNIT {
  354. context->yyRelMonth += $1 * $2;
  355. }
  356. | tMONTH_UNIT {
  357. context->yyRelMonth += $1;
  358. }
  359. | tUNUMBER tDAY_UNIT {
  360. context->yyRelDay += $1 * $2;
  361. }
  362. | tSNUMBER tDAY_UNIT {
  363. context->yyRelDay += $1 * $2;
  364. }
  365. | tDAY_UNIT {
  366. context->yyRelDay += $1;
  367. }
  368. | tUNUMBER tHOUR_UNIT {
  369. context->yyRelHour += $1 * $2;
  370. }
  371. | tSNUMBER tHOUR_UNIT {
  372. context->yyRelHour += $1 * $2;
  373. }
  374. | tHOUR_UNIT {
  375. context->yyRelHour += $1;
  376. }
  377. | tUNUMBER tMINUTE_UNIT {
  378. context->yyRelMinutes += $1 * $2;
  379. }
  380. | tSNUMBER tMINUTE_UNIT {
  381. context->yyRelMinutes += $1 * $2;
  382. }
  383. | tMINUTE_UNIT {
  384. context->yyRelMinutes += $1;
  385. }
  386. | tUNUMBER tSEC_UNIT {
  387. context->yyRelSeconds += $1 * $2;
  388. }
  389. | tSNUMBER tSEC_UNIT {
  390. context->yyRelSeconds += $1 * $2;
  391. }
  392. | tSEC_UNIT {
  393. context->yyRelSeconds += $1;
  394. }
  395. ;
  396. number : tUNUMBER
  397. {
  398. if (context->yyHaveTime && context->yyHaveDate &&
  399. !context->yyHaveRel)
  400. context->yyYear = $1;
  401. else
  402. {
  403. if ($1>10000)
  404. {
  405. context->yyHaveDate++;
  406. context->yyDay= ($1)%100;
  407. context->yyMonth= ($1/100)%100;
  408. context->yyYear = $1/10000;
  409. }
  410. else
  411. {
  412. context->yyHaveTime++;
  413. if ($1 < 100)
  414. {
  415. context->yyHour = $1;
  416. context->yyMinutes = 0;
  417. }
  418. else
  419. {
  420. context->yyHour = $1 / 100;
  421. context->yyMinutes = $1 % 100;
  422. }
  423. context->yySeconds = 0;
  424. context->yyMeridian = MER24;
  425. }
  426. }
  427. }
  428. ;
  429. o_merid : /* NULL */
  430. {
  431. $$ = MER24;
  432. }
  433. | tMERIDIAN
  434. {
  435. $$ = $1;
  436. }
  437. ;
  438. %%
  439. /* Include this file down here because bison inserts code above which
  440. may define-away `const'. We want the prototype for get_date to have
  441. the same signature as the function definition does. */
  442. #include "getdate.h"
  443. #ifndef WIN32 /* the windows dudes don't need these, does anyone really? */
  444. extern struct tm *gmtime ();
  445. extern struct tm *localtime ();
  446. extern time_t mktime ();
  447. #endif
  448. /* Month and day table. */
  449. static TABLE const MonthDayTable[] = {
  450. { "january", tMONTH, 1 },
  451. { "february", tMONTH, 2 },
  452. { "march", tMONTH, 3 },
  453. { "april", tMONTH, 4 },
  454. { "may", tMONTH, 5 },
  455. { "june", tMONTH, 6 },
  456. { "july", tMONTH, 7 },
  457. { "august", tMONTH, 8 },
  458. { "september", tMONTH, 9 },
  459. { "sept", tMONTH, 9 },
  460. { "october", tMONTH, 10 },
  461. { "november", tMONTH, 11 },
  462. { "december", tMONTH, 12 },
  463. { "sunday", tDAY, 0 },
  464. { "monday", tDAY, 1 },
  465. { "tuesday", tDAY, 2 },
  466. { "tues", tDAY, 2 },
  467. { "wednesday", tDAY, 3 },
  468. { "wednes", tDAY, 3 },
  469. { "thursday", tDAY, 4 },
  470. { "thur", tDAY, 4 },
  471. { "thurs", tDAY, 4 },
  472. { "friday", tDAY, 5 },
  473. { "saturday", tDAY, 6 },
  474. { NULL, 0, 0 }
  475. };
  476. /* Time units table. */
  477. static TABLE const UnitsTable[] = {
  478. { "year", tYEAR_UNIT, 1 },
  479. { "month", tMONTH_UNIT, 1 },
  480. { "fortnight", tDAY_UNIT, 14 },
  481. { "week", tDAY_UNIT, 7 },
  482. { "day", tDAY_UNIT, 1 },
  483. { "hour", tHOUR_UNIT, 1 },
  484. { "minute", tMINUTE_UNIT, 1 },
  485. { "min", tMINUTE_UNIT, 1 },
  486. { "second", tSEC_UNIT, 1 },
  487. { "sec", tSEC_UNIT, 1 },
  488. { NULL, 0, 0 }
  489. };
  490. /* Assorted relative-time words. */
  491. static TABLE const OtherTable[] = {
  492. { "tomorrow", tMINUTE_UNIT, 1 * 24 * 60 },
  493. { "yesterday", tMINUTE_UNIT, -1 * 24 * 60 },
  494. { "today", tMINUTE_UNIT, 0 },
  495. { "now", tMINUTE_UNIT, 0 },
  496. { "last", tUNUMBER, -1 },
  497. { "this", tMINUTE_UNIT, 0 },
  498. { "next", tUNUMBER, 1 },
  499. { "first", tUNUMBER, 1 },
  500. /* { "second", tUNUMBER, 2 }, */
  501. { "third", tUNUMBER, 3 },
  502. { "fourth", tUNUMBER, 4 },
  503. { "fifth", tUNUMBER, 5 },
  504. { "sixth", tUNUMBER, 6 },
  505. { "seventh", tUNUMBER, 7 },
  506. { "eighth", tUNUMBER, 8 },
  507. { "ninth", tUNUMBER, 9 },
  508. { "tenth", tUNUMBER, 10 },
  509. { "eleventh", tUNUMBER, 11 },
  510. { "twelfth", tUNUMBER, 12 },
  511. { "ago", tAGO, 1 },
  512. { NULL, 0, 0 }
  513. };
  514. /* The timezone table. */
  515. static TABLE const TimezoneTable[] = {
  516. { "gmt", tZONE, HOUR ( 0) }, /* Greenwich Mean */
  517. { "ut", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */
  518. { "utc", tZONE, HOUR ( 0) },
  519. { "wet", tZONE, HOUR ( 0) }, /* Western European */
  520. { "bst", tDAYZONE, HOUR ( 0) }, /* British Summer */
  521. { "wat", tZONE, HOUR ( 1) }, /* West Africa */
  522. { "at", tZONE, HOUR ( 2) }, /* Azores */
  523. #if 0
  524. /* For completeness. BST is also British Summer, and GST is
  525. * also Guam Standard. */
  526. { "bst", tZONE, HOUR ( 3) }, /* Brazil Standard */
  527. { "gst", tZONE, HOUR ( 3) }, /* Greenland Standard */
  528. #endif
  529. #if 0
  530. { "nft", tZONE, HOUR (3.5) }, /* Newfoundland */
  531. { "nst", tZONE, HOUR (3.5) }, /* Newfoundland Standard */
  532. { "ndt", tDAYZONE, HOUR (3.5) }, /* Newfoundland Daylight */
  533. #endif
  534. { "ast", tZONE, HOUR ( 4) }, /* Atlantic Standard */
  535. { "adt", tDAYZONE, HOUR ( 4) }, /* Atlantic Daylight */
  536. { "est", tZONE, HOUR ( 5) }, /* Eastern Standard */
  537. { "edt", tDAYZONE, HOUR ( 5) }, /* Eastern Daylight */
  538. { "cst", tZONE, HOUR ( 6) }, /* Central Standard */
  539. { "cdt", tDAYZONE, HOUR ( 6) }, /* Central Daylight */
  540. { "mst", tZONE, HOUR ( 7) }, /* Mountain Standard */
  541. { "mdt", tDAYZONE, HOUR ( 7) }, /* Mountain Daylight */
  542. { "pst", tZONE, HOUR ( 8) }, /* Pacific Standard */
  543. { "pdt", tDAYZONE, HOUR ( 8) }, /* Pacific Daylight */
  544. { "yst", tZONE, HOUR ( 9) }, /* Yukon Standard */
  545. { "ydt", tDAYZONE, HOUR ( 9) }, /* Yukon Daylight */
  546. { "hst", tZONE, HOUR (10) }, /* Hawaii Standard */
  547. { "hdt", tDAYZONE, HOUR (10) }, /* Hawaii Daylight */
  548. { "cat", tZONE, HOUR (10) }, /* Central Alaska */
  549. { "ahst", tZONE, HOUR (10) }, /* Alaska-Hawaii Standard */
  550. { "nt", tZONE, HOUR (11) }, /* Nome */
  551. { "idlw", tZONE, HOUR (12) }, /* International Date Line West */
  552. { "cet", tZONE, -HOUR (1) }, /* Central European */
  553. { "met", tZONE, -HOUR (1) }, /* Middle European */
  554. { "mewt", tZONE, -HOUR (1) }, /* Middle European Winter */
  555. { "mest", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  556. { "mesz", tDAYZONE, -HOUR (1) }, /* Middle European Summer */
  557. { "swt", tZONE, -HOUR (1) }, /* Swedish Winter */
  558. { "sst", tDAYZONE, -HOUR (1) }, /* Swedish Summer */
  559. { "fwt", tZONE, -HOUR (1) }, /* French Winter */
  560. { "fst", tDAYZONE, -HOUR (1) }, /* French Summer */
  561. { "eet", tZONE, -HOUR (2) }, /* Eastern Europe, USSR Zone 1 */
  562. { "bt", tZONE, -HOUR (3) }, /* Baghdad, USSR Zone 2 */
  563. #if 0
  564. { "it", tZONE, -HOUR (3.5) },/* Iran */
  565. #endif
  566. { "zp4", tZONE, -HOUR (4) }, /* USSR Zone 3 */
  567. { "zp5", tZONE, -HOUR (5) }, /* USSR Zone 4 */
  568. #if 0
  569. { "ist", tZONE, -HOUR (5.5) },/* Indian Standard */
  570. #endif
  571. { "zp6", tZONE, -HOUR (6) }, /* USSR Zone 5 */
  572. #if 0
  573. /* For completeness. NST is also Newfoundland Standard, and SST is
  574. * also Swedish Summer. */
  575. { "nst", tZONE, -HOUR (6.5) },/* North Sumatra */
  576. { "sst", tZONE, -HOUR (7) }, /* South Sumatra, USSR Zone 6 */
  577. #endif /* 0 */
  578. { "wast", tZONE, -HOUR (7) }, /* West Australian Standard */
  579. { "wadt", tDAYZONE, -HOUR (7) }, /* West Australian Daylight */
  580. #if 0
  581. { "jt", tZONE, -HOUR (7.5) },/* Java (3pm in Cronusland!) */
  582. #endif
  583. { "cct", tZONE, -HOUR (8) }, /* China Coast, USSR Zone 7 */
  584. { "jst", tZONE, -HOUR (9) }, /* Japan Standard, USSR Zone 8 */
  585. #if 0
  586. { "cast", tZONE, -HOUR (9.5) },/* Central Australian Standard */
  587. { "cadt", tDAYZONE, -HOUR (9.5) },/* Central Australian Daylight */
  588. #endif
  589. { "east", tZONE, -HOUR (10) }, /* Eastern Australian Standard */
  590. { "eadt", tDAYZONE, -HOUR (10) }, /* Eastern Australian Daylight */
  591. { "gst", tZONE, -HOUR (10) }, /* Guam Standard, USSR Zone 9 */
  592. { "nzt", tZONE, -HOUR (12) }, /* New Zealand */
  593. { "nzst", tZONE, -HOUR (12) }, /* New Zealand Standard */
  594. { "nzdt", tDAYZONE, -HOUR (12) }, /* New Zealand Daylight */
  595. { "idle", tZONE, -HOUR (12) }, /* International Date Line East */
  596. { NULL, 0, 0 }
  597. };
  598. /* Military timezone table. */
  599. static TABLE const MilitaryTable[] = {
  600. { "a", tZONE, HOUR ( 1) },
  601. { "b", tZONE, HOUR ( 2) },
  602. { "c", tZONE, HOUR ( 3) },
  603. { "d", tZONE, HOUR ( 4) },
  604. { "e", tZONE, HOUR ( 5) },
  605. { "f", tZONE, HOUR ( 6) },
  606. { "g", tZONE, HOUR ( 7) },
  607. { "h", tZONE, HOUR ( 8) },
  608. { "i", tZONE, HOUR ( 9) },
  609. { "k", tZONE, HOUR ( 10) },
  610. { "l", tZONE, HOUR ( 11) },
  611. { "m", tZONE, HOUR ( 12) },
  612. { "n", tZONE, HOUR (- 1) },
  613. { "o", tZONE, HOUR (- 2) },
  614. { "p", tZONE, HOUR (- 3) },
  615. { "q", tZONE, HOUR (- 4) },
  616. { "r", tZONE, HOUR (- 5) },
  617. { "s", tZONE, HOUR (- 6) },
  618. { "t", tZONE, HOUR (- 7) },
  619. { "u", tZONE, HOUR (- 8) },
  620. { "v", tZONE, HOUR (- 9) },
  621. { "w", tZONE, HOUR (-10) },
  622. { "x", tZONE, HOUR (-11) },
  623. { "y", tZONE, HOUR (-12) },
  624. { "z", tZONE, HOUR ( 0) },
  625. { NULL, 0, 0 }
  626. };
  627. /* ARGSUSED */
  628. static int
  629. yyerror (s)
  630. char *s ATTRIBUTE_UNUSED;
  631. {
  632. return 0;
  633. }
  634. static int
  635. ToHour (Hours, Meridian)
  636. int Hours;
  637. MERIDIAN Meridian;
  638. {
  639. switch (Meridian)
  640. {
  641. case MER24:
  642. if (Hours < 0 || Hours > 23)
  643. return -1;
  644. return Hours;
  645. case MERam:
  646. if (Hours < 1 || Hours > 12)
  647. return -1;
  648. if (Hours == 12)
  649. Hours = 0;
  650. return Hours;
  651. case MERpm:
  652. if (Hours < 1 || Hours > 12)
  653. return -1;
  654. if (Hours == 12)
  655. Hours = 0;
  656. return Hours + 12;
  657. default:
  658. abort ();
  659. }
  660. /* NOTREACHED */
  661. }
  662. static int
  663. ToYear (Year)
  664. int Year;
  665. {
  666. if (Year < 0)
  667. Year = -Year;
  668. /* XPG4 suggests that years 00-68 map to 2000-2068, and
  669. years 69-99 map to 1969-1999. */
  670. if (Year < 69)
  671. Year += 2000;
  672. else if (Year < 100)
  673. Year += 1900;
  674. return Year;
  675. }
  676. static int
  677. LookupWord (yylval, buff)
  678. YYSTYPE *yylval;
  679. char *buff;
  680. {
  681. register char *p;
  682. register char *q;
  683. register const TABLE *tp;
  684. int i;
  685. int abbrev;
  686. /* Make it lowercase. */
  687. for (p = buff; *p; p++)
  688. if (ISUPPER ((unsigned char) *p))
  689. *p = tolower (*p);
  690. if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
  691. {
  692. yylval->Meridian = MERam;
  693. return tMERIDIAN;
  694. }
  695. if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
  696. {
  697. yylval->Meridian = MERpm;
  698. return tMERIDIAN;
  699. }
  700. /* See if we have an abbreviation for a month. */
  701. if (strlen (buff) == 3)
  702. abbrev = 1;
  703. else if (strlen (buff) == 4 && buff[3] == '.')
  704. {
  705. abbrev = 1;
  706. buff[3] = '\0';
  707. }
  708. else
  709. abbrev = 0;
  710. for (tp = MonthDayTable; tp->name; tp++)
  711. {
  712. if (abbrev)
  713. {
  714. if (strncmp (buff, tp->name, 3) == 0)
  715. {
  716. yylval->Number = tp->value;
  717. return tp->type;
  718. }
  719. }
  720. else if (strcmp (buff, tp->name) == 0)
  721. {
  722. yylval->Number = tp->value;
  723. return tp->type;
  724. }
  725. }
  726. for (tp = TimezoneTable; tp->name; tp++)
  727. if (strcmp (buff, tp->name) == 0)
  728. {
  729. yylval->Number = tp->value;
  730. return tp->type;
  731. }
  732. if (strcmp (buff, "dst") == 0)
  733. return tDST;
  734. for (tp = UnitsTable; tp->name; tp++)
  735. if (strcmp (buff, tp->name) == 0)
  736. {
  737. yylval->Number = tp->value;
  738. return tp->type;
  739. }
  740. /* Strip off any plural and try the units table again. */
  741. i = strlen (buff) - 1;
  742. if (buff[i] == 's')
  743. {
  744. buff[i] = '\0';
  745. for (tp = UnitsTable; tp->name; tp++)
  746. if (strcmp (buff, tp->name) == 0)
  747. {
  748. yylval->Number = tp->value;
  749. return tp->type;
  750. }
  751. buff[i] = 's'; /* Put back for "this" in OtherTable. */
  752. }
  753. for (tp = OtherTable; tp->name; tp++)
  754. if (strcmp (buff, tp->name) == 0)
  755. {
  756. yylval->Number = tp->value;
  757. return tp->type;
  758. }
  759. /* Military timezones. */
  760. if (buff[1] == '\0' && ISALPHA ((unsigned char) *buff))
  761. {
  762. for (tp = MilitaryTable; tp->name; tp++)
  763. if (strcmp (buff, tp->name) == 0)
  764. {
  765. yylval->Number = tp->value;
  766. return tp->type;
  767. }
  768. }
  769. /* Drop out any periods and try the timezone table again. */
  770. for (i = 0, p = q = buff; *q; q++)
  771. if (*q != '.')
  772. *p++ = *q;
  773. else
  774. i++;
  775. *p = '\0';
  776. if (i)
  777. for (tp = TimezoneTable; tp->name; tp++)
  778. if (strcmp (buff, tp->name) == 0)
  779. {
  780. yylval->Number = tp->value;
  781. return tp->type;
  782. }
  783. return tID;
  784. }
  785. static int
  786. yylex (yylval, cookie)
  787. YYSTYPE *yylval;
  788. void *cookie;
  789. {
  790. register unsigned char c;
  791. register char *p;
  792. char buff[20];
  793. int Count;
  794. int sign;
  795. for (;;)
  796. {
  797. while (ISSPACE ((unsigned char) *context->yyInput))
  798. context->yyInput++;
  799. if (ISDIGIT (c = *context->yyInput) || c == '-' || c == '+')
  800. {
  801. if (c == '-' || c == '+')
  802. {
  803. sign = c == '-' ? -1 : 1;
  804. if (!ISDIGIT (*++context->yyInput))
  805. /* skip the '-' sign */
  806. continue;
  807. }
  808. else
  809. sign = 0;
  810. for (yylval->Number = 0; ISDIGIT (c = *context->yyInput++);)
  811. yylval->Number = 10 * yylval->Number + c - '0';
  812. context->yyInput--;
  813. if (sign < 0)
  814. yylval->Number = -yylval->Number;
  815. return sign ? tSNUMBER : tUNUMBER;
  816. }
  817. if (ISALPHA (c))
  818. {
  819. for (p = buff; (c = *context->yyInput++, ISALPHA (c)) || c == '.';)
  820. if (p < &buff[sizeof buff - 1])
  821. *p++ = c;
  822. *p = '\0';
  823. context->yyInput--;
  824. return LookupWord (yylval, buff);
  825. }
  826. if (c != '(')
  827. return *context->yyInput++;
  828. Count = 0;
  829. do
  830. {
  831. c = *context->yyInput++;
  832. if (c == '\0')
  833. return c;
  834. if (c == '(')
  835. Count++;
  836. else if (c == ')')
  837. Count--;
  838. }
  839. while (Count > 0);
  840. }
  841. }
  842. #define TM_YEAR_ORIGIN 1900
  843. /* Yield A - B, measured in seconds. */
  844. static long
  845. difftm (struct tm *a, struct tm *b)
  846. {
  847. int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  848. int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  849. long days = (
  850. /* difference in day of year */
  851. a->tm_yday - b->tm_yday
  852. /* + intervening leap days */
  853. + ((ay >> 2) - (by >> 2))
  854. - (ay / 100 - by / 100)
  855. + ((ay / 100 >> 2) - (by / 100 >> 2))
  856. /* + difference in years * 365 */
  857. + (long) (ay - by) * 365
  858. );
  859. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  860. + (a->tm_min - b->tm_min))
  861. + (a->tm_sec - b->tm_sec));
  862. }
  863. time_t
  864. curl_getdate (const char *p, const time_t *now)
  865. {
  866. struct tm tm, tm0, *tmp;
  867. time_t Start;
  868. GETDATE_CONTEXT cookie;
  869. #ifdef HAVE_LOCALTIME_R
  870. struct tm keeptime;
  871. #endif
  872. cookie.yyInput = p;
  873. Start = now ? *now : time ((time_t *) NULL);
  874. #ifdef HAVE_LOCALTIME_R
  875. tmp = (struct tm *)localtime_r(&Start, &keeptime);
  876. #else
  877. tmp = localtime (&Start);
  878. #endif
  879. if (!tmp)
  880. return -1;
  881. cookie.yyYear = tmp->tm_year + TM_YEAR_ORIGIN;
  882. cookie.yyMonth = tmp->tm_mon + 1;
  883. cookie.yyDay = tmp->tm_mday;
  884. cookie.yyHour = tmp->tm_hour;
  885. cookie.yyMinutes = tmp->tm_min;
  886. cookie.yySeconds = tmp->tm_sec;
  887. tm.tm_isdst = tmp->tm_isdst;
  888. cookie.yyMeridian = MER24;
  889. cookie.yyRelSeconds = 0;
  890. cookie.yyRelMinutes = 0;
  891. cookie.yyRelHour = 0;
  892. cookie.yyRelDay = 0;
  893. cookie.yyRelMonth = 0;
  894. cookie.yyRelYear = 0;
  895. cookie.yyHaveDate = 0;
  896. cookie.yyHaveDay = 0;
  897. cookie.yyHaveRel = 0;
  898. cookie.yyHaveTime = 0;
  899. cookie.yyHaveZone = 0;
  900. if (yyparse (&cookie)
  901. || cookie.yyHaveTime > 1 || cookie.yyHaveZone > 1 ||
  902. cookie.yyHaveDate > 1 || cookie.yyHaveDay > 1)
  903. return -1;
  904. tm.tm_year = ToYear (cookie.yyYear) - TM_YEAR_ORIGIN + cookie.yyRelYear;
  905. tm.tm_mon = cookie.yyMonth - 1 + cookie.yyRelMonth;
  906. tm.tm_mday = cookie.yyDay + cookie.yyRelDay;
  907. if (cookie.yyHaveTime ||
  908. (cookie.yyHaveRel && !cookie.yyHaveDate && !cookie.yyHaveDay))
  909. {
  910. tm.tm_hour = ToHour (cookie.yyHour, cookie.yyMeridian);
  911. if (tm.tm_hour < 0)
  912. return -1;
  913. tm.tm_min = cookie.yyMinutes;
  914. tm.tm_sec = cookie.yySeconds;
  915. }
  916. else
  917. {
  918. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  919. }
  920. tm.tm_hour += cookie.yyRelHour;
  921. tm.tm_min += cookie.yyRelMinutes;
  922. tm.tm_sec += cookie.yyRelSeconds;
  923. /* Let mktime deduce tm_isdst if we have an absolute timestamp,
  924. or if the relative timestamp mentions days, months, or years. */
  925. if (cookie.yyHaveDate | cookie.yyHaveDay | cookie.yyHaveTime |
  926. cookie.yyRelDay | cookie.yyRelMonth | cookie.yyRelYear)
  927. tm.tm_isdst = -1;
  928. tm0 = tm;
  929. Start = mktime (&tm);
  930. if (Start == (time_t) -1)
  931. {
  932. /* Guard against falsely reporting errors near the time_t boundaries
  933. when parsing times in other time zones. For example, if the min
  934. time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
  935. of UTC, then the min localtime value is 1970-01-01 08:00:00; if
  936. we apply mktime to 1970-01-01 00:00:00 we will get an error, so
  937. we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
  938. zone by 24 hours to compensate. This algorithm assumes that
  939. there is no DST transition within a day of the time_t boundaries. */
  940. if (cookie.yyHaveZone)
  941. {
  942. tm = tm0;
  943. if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
  944. {
  945. tm.tm_mday++;
  946. cookie.yyTimezone -= 24 * 60;
  947. }
  948. else
  949. {
  950. tm.tm_mday--;
  951. cookie.yyTimezone += 24 * 60;
  952. }
  953. Start = mktime (&tm);
  954. }
  955. if (Start == (time_t) -1)
  956. return Start;
  957. }
  958. if (cookie.yyHaveDay && !cookie.yyHaveDate)
  959. {
  960. tm.tm_mday += ((cookie.yyDayNumber - tm.tm_wday + 7) % 7
  961. + 7 * (cookie.yyDayOrdinal - (0 < cookie.yyDayOrdinal)));
  962. Start = mktime (&tm);
  963. if (Start == (time_t) -1)
  964. return Start;
  965. }
  966. if (cookie.yyHaveZone)
  967. {
  968. long delta;
  969. struct tm *gmt;
  970. #ifdef HAVE_GMTIME_R
  971. /* thread-safe version */
  972. struct tm keeptime;
  973. gmt = (struct tm *)gmtime_r(&Start, &keeptime);
  974. #else
  975. gmt = gmtime(&Start);
  976. #endif
  977. if (!gmt)
  978. return -1;
  979. delta = cookie.yyTimezone * 60L + difftm (&tm, gmt);
  980. if ((Start + delta < Start) != (delta < 0))
  981. return -1; /* time_t overflow */
  982. Start += delta;
  983. }
  984. return Start;
  985. }
  986. #if defined (TEST)
  987. /* ARGSUSED */
  988. int
  989. main (ac, av)
  990. int ac;
  991. char *av[];
  992. {
  993. char buff[MAX_BUFF_LEN + 1];
  994. time_t d;
  995. (void) printf ("Enter date, or blank line to exit.\n\t> ");
  996. (void) fflush (stdout);
  997. buff[MAX_BUFF_LEN] = 0;
  998. while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
  999. {
  1000. d = curl_getdate (buff, (time_t *) NULL);
  1001. if (d == -1)
  1002. (void) printf ("Bad format - couldn't convert.\n");
  1003. else
  1004. (void) printf ("%s", ctime (&d));
  1005. (void) printf ("\t> ");
  1006. (void) fflush (stdout);
  1007. }
  1008. exit (0);
  1009. /* NOTREACHED */
  1010. }
  1011. #endif /* defined (TEST) */