.
[coreutils.git] / lib / posixtm.y
blobe2e9985af624ac8c97f81aaff591530bdf7ac279
1 /* Parse dates for touch.
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Jim Kingdon and David MacKenzie. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 /* The following block of alloca-related preprocessor directives is here
26 solely to allow compilation by non GNU-C compilers of the C parser
27 produced from this file by old versions of bison. Newer versions of
28 bison include a block similar to this one in bison.simple. */
30 #ifdef __GNUC__
31 #define alloca __builtin_alloca
32 #else
33 #ifdef HAVE_ALLOCA_H
34 #include <alloca.h>
35 #else
36 #ifdef _AIX
37 #pragma alloca
38 #else
39 void *alloca ();
40 #endif
41 #endif
42 #endif
44 #include <stdio.h>
45 #include <sys/types.h>
47 #ifdef TM_IN_SYS_TIME
48 #include <sys/time.h>
49 #else
50 #include <time.h>
51 #endif
53 /* Some old versions of bison generate parsers that use bcopy.
54 That loses on systems that don't provide the function, so we have
55 to redefine it here. */
56 #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
57 #define bcopy(from, to, len) memcpy ((to), (from), (len))
58 #endif
60 #define YYDEBUG 1
62 /* Lexical analyzer's current scan position in the input string. */
63 static char *curpos;
65 /* The return value. */
66 static struct tm t;
68 time_t mktime ();
70 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
71 as well as gratuitiously global symbol names, so we can have multiple
72 yacc generated parsers in the same program. Note that these are only
73 the variables produced by yacc. If other parser generators (bison,
74 byacc, etc) produce additional global names that conflict at link time,
75 then those parser generators need to be fixed instead of adding those
76 names to this list. */
78 #define yymaxdepth pt_maxdepth
79 #define yyparse pt_parse
80 #define yylex pt_lex
81 #define yyerror pt_error
82 #define yylval pt_lval
83 #define yychar pt_char
84 #define yydebug pt_debug
85 #define yypact pt_pact
86 #define yyr1 pt_r1
87 #define yyr2 pt_r2
88 #define yydef pt_def
89 #define yychk pt_chk
90 #define yypgo pt_pgo
91 #define yyact pt_act
92 #define yyexca pt_exca
93 #define yyerrflag pt_errflag
94 #define yynerrs pt_nerrs
95 #define yyps pt_ps
96 #define yypv pt_pv
97 #define yys pt_s
98 #define yy_yys pt_yys
99 #define yystate pt_state
100 #define yytmp pt_tmp
101 #define yyv pt_v
102 #define yy_yyv pt_yyv
103 #define yyval pt_val
104 #define yylloc pt_lloc
105 #define yyreds pt_reds /* With YYDEBUG defined */
106 #define yytoks pt_toks /* With YYDEBUG defined */
107 #define yylhs pt_yylhs
108 #define yylen pt_yylen
109 #define yydefred pt_yydefred
110 #define yydgoto pt_yydgoto
111 #define yysindex pt_yysindex
112 #define yyrindex pt_yyrindex
113 #define yygindex pt_yygindex
114 #define yytable pt_yytable
115 #define yycheck pt_yycheck
117 static int yylex ();
118 static int yyerror ();
122 %token DIGIT
125 date :
126 digitpair /* month */
127 digitpair /* day */
128 digitpair /* hours */
129 digitpair /* minutes */
130 year
131 seconds {
132 if ($1 >= 1 && $1 <= 12)
133 t.tm_mon = $1 - 1;
134 else {
135 YYABORT;
137 if ($2 >= 1 && $2 <= 31)
138 t.tm_mday = $2;
139 else {
140 YYABORT;
142 if ($3 >= 0 && $3 <= 23)
143 t.tm_hour = $3;
144 else {
145 YYABORT;
147 if ($4 >= 0 && $4 <= 59)
148 t.tm_min = $4;
149 else {
150 YYABORT;
154 year : digitpair {
155 t.tm_year = $1;
156 /* Deduce the century based on the year.
157 See POSIX.2 section 4.63.3. */
158 if ($1 <= 68)
159 t.tm_year += 100;
161 | digitpair digitpair {
162 t.tm_year = $1 * 100 + $2;
163 if (t.tm_year < 1900) {
164 YYABORT;
165 } else
166 t.tm_year -= 1900;
168 | /* empty */ {
169 time_t now;
170 struct tm *tmp;
172 /* Use current year. */
173 time (&now);
174 tmp = localtime (&now);
175 t.tm_year = tmp->tm_year;
179 seconds : /* empty */ {
180 t.tm_sec = 0;
182 | '.' digitpair {
183 if ($2 >= 0 && $2 <= 61)
184 t.tm_sec = $2;
185 else {
186 YYABORT;
191 digitpair : DIGIT DIGIT {
192 $$ = $1 * 10 + $2;
196 static int
197 yylex ()
199 char ch = *curpos++;
201 if (ch >= '0' && ch <= '9')
203 yylval = ch - '0';
204 return DIGIT;
206 else if (ch == '.' || ch == 0)
207 return ch;
208 else
209 return '?'; /* Cause an error. */
212 static int
213 yyerror ()
215 return 0;
218 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error. */
220 time_t
221 posixtime (s)
222 char *s;
224 curpos = s;
225 /* Let mktime decide whether it is daylight savings time. */
226 t.tm_isdst = -1;
227 if (yyparse ())
228 return (time_t)-1;
229 else
230 return mktime (&t);
233 /* Parse a POSIX-style date and return it, or NULL for an error. */
235 struct tm *
236 posixtm (s)
237 char *s;
239 if (posixtime (s) == -1)
240 return NULL;
241 return &t;