0967667b8d54f02d14eb39201322ad0a6defe9df
[gnulib.git] / lib / posixtm.y
1 /* Parse dates for touch.
2    Copyright (C) 1989, 1990, 1991 Free Software Foundation Inc.
3
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.
8
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.
13
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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Written by Jim Kingdon and David MacKenzie. */
19 %{
20 #ifdef __GNUC__
21 #define alloca __builtin_alloca
22 #else
23 #ifdef HAVE_ALLOCA_H
24 #include <alloca.h>
25 #else
26 #ifdef _AIX
27  #pragma alloca
28 #else
29 char *alloca ();
30 #endif
31 #endif
32 #endif
33
34 #include <stdio.h>
35 #include <sys/types.h>
36
37 #ifdef TM_IN_SYS_TIME
38 #include <sys/time.h>
39 #else
40 #include <time.h>
41 #endif
42
43 /* Some old versions of bison generate parsers that use bcopy.
44    That loses on systems that don't provide the function, so we have
45    to redefine it here.  */
46 #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
47 #define bcopy(from, to, len) memcpy ((to), (from), (len))
48 #endif
49
50 #define YYDEBUG 1
51
52 /* Lexical analyzer's current scan position in the input string. */
53 static char *curpos;
54
55 /* The return value. */
56 static struct tm t;
57
58 time_t mktime ();
59
60 #define yyparse posixtime_yyparse
61 static int yylex ();
62 static int yyerror ();
63 %}
64
65 %token DIGIT
66
67 %%
68 date :
69        digitpair /* month */
70        digitpair /* day */
71        digitpair /* hours */
72        digitpair /* minutes */
73        year
74        seconds {
75                  if ($1 >= 1 && $1 <= 12)
76                    t.tm_mon = $1 - 1;
77                  else {
78                    YYABORT;
79                  }
80                  if ($2 >= 1 && $2 <= 31)
81                    t.tm_mday = $2;
82                  else {
83                    YYABORT;
84                  }
85                  if ($3 >= 0 && $3 <= 23)
86                    t.tm_hour = $3;
87                  else {
88                    YYABORT;
89                  }
90                  if ($4 >= 0 && $4 <= 59)
91                    t.tm_min = $4;
92                  else {
93                    YYABORT;
94                  }
95                }
96
97 year : digitpair {
98                    t.tm_year = $1;
99                    /* Deduce the century based on the year.
100                       See POSIX.2 section 4.63.3.  */
101                    if ($1 <= 68)
102                      t.tm_year += 100;
103                  }
104     | digitpair digitpair {
105                             t.tm_year = $1 * 100 + $2;
106                             if (t.tm_year < 1900) {
107                               YYABORT;
108                             } else
109                               t.tm_year -= 1900;
110                           }
111     | /* empty */ {
112                     time_t now;
113                     struct tm *tmp;
114
115                     /* Use current year.  */
116                     time (&now);
117                     tmp = localtime (&now);
118                     t.tm_year = tmp->tm_year;
119                   }
120     ;
121
122 seconds : /* empty */ {
123                         t.tm_sec = 0;
124                       }
125         | '.' digitpair {
126                           if ($2 >= 0 && $2 <= 61)
127                             t.tm_sec = $2;
128                           else {
129                             YYABORT;
130                           }
131                         }
132         ;
133
134 digitpair : DIGIT DIGIT {
135                           $$ = $1 * 10 + $2;
136                         }
137           ;
138 %%
139 static int
140 yylex ()
141 {
142   char ch = *curpos++;
143
144   if (ch >= '0' && ch <= '9')
145     {
146       yylval = ch - '0';
147       return DIGIT;
148     }
149   else if (ch == '.' || ch == 0)
150     return ch;
151   else
152     return '?';                 /* Cause an error.  */
153 }
154
155 static int
156 yyerror ()
157 {
158   return 0;
159 }
160
161 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
162
163 time_t
164 posixtime (s)
165      char *s;
166 {
167   curpos = s;
168   /* Let mktime decide whether it is daylight savings time.  */
169   t.tm_isdst = -1;
170   if (yyparse ())
171     return (time_t)-1;
172   else
173     return mktime (&t);
174 }
175
176 /* Parse a POSIX-style date and return it, or NULL for an error.  */
177
178 struct tm *
179 posixtm (s)
180      char *s;
181 {
182   if (posixtime (s) == -1)
183     return NULL;
184   return &t;
185 }