.
[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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Jim Kingdon and David MacKenzie. */
19 %{
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
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.  */
29
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
43
44 #include <stdio.h>
45 #include <sys/types.h>
46
47 #ifdef TM_IN_SYS_TIME
48 #include <sys/time.h>
49 #else
50 #include <time.h>
51 #endif
52
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
59
60 #define YYDEBUG 1
61
62 /* Lexical analyzer's current scan position in the input string. */
63 static char *curpos;
64
65 /* The return value. */
66 static struct tm t;
67
68 time_t mktime ();
69
70 #define yyparse posixtime_yyparse
71 static int yylex ();
72 static int yyerror ();
73 %}
74
75 %token DIGIT
76
77 %%
78 date :
79        digitpair /* month */
80        digitpair /* day */
81        digitpair /* hours */
82        digitpair /* minutes */
83        year
84        seconds {
85                  if ($1 >= 1 && $1 <= 12)
86                    t.tm_mon = $1 - 1;
87                  else {
88                    YYABORT;
89                  }
90                  if ($2 >= 1 && $2 <= 31)
91                    t.tm_mday = $2;
92                  else {
93                    YYABORT;
94                  }
95                  if ($3 >= 0 && $3 <= 23)
96                    t.tm_hour = $3;
97                  else {
98                    YYABORT;
99                  }
100                  if ($4 >= 0 && $4 <= 59)
101                    t.tm_min = $4;
102                  else {
103                    YYABORT;
104                  }
105                }
106
107 year : digitpair {
108                    t.tm_year = $1;
109                    /* Deduce the century based on the year.
110                       See POSIX.2 section 4.63.3.  */
111                    if ($1 <= 68)
112                      t.tm_year += 100;
113                  }
114     | digitpair digitpair {
115                             t.tm_year = $1 * 100 + $2;
116                             if (t.tm_year < 1900) {
117                               YYABORT;
118                             } else
119                               t.tm_year -= 1900;
120                           }
121     | /* empty */ {
122                     time_t now;
123                     struct tm *tmp;
124
125                     /* Use current year.  */
126                     time (&now);
127                     tmp = localtime (&now);
128                     t.tm_year = tmp->tm_year;
129                   }
130     ;
131
132 seconds : /* empty */ {
133                         t.tm_sec = 0;
134                       }
135         | '.' digitpair {
136                           if ($2 >= 0 && $2 <= 61)
137                             t.tm_sec = $2;
138                           else {
139                             YYABORT;
140                           }
141                         }
142         ;
143
144 digitpair : DIGIT DIGIT {
145                           $$ = $1 * 10 + $2;
146                         }
147           ;
148 %%
149 static int
150 yylex ()
151 {
152   char ch = *curpos++;
153
154   if (ch >= '0' && ch <= '9')
155     {
156       yylval = ch - '0';
157       return DIGIT;
158     }
159   else if (ch == '.' || ch == 0)
160     return ch;
161   else
162     return '?';                 /* Cause an error.  */
163 }
164
165 static int
166 yyerror ()
167 {
168   return 0;
169 }
170
171 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
172
173 time_t
174 posixtime (s)
175      char *s;
176 {
177   curpos = s;
178   /* Let mktime decide whether it is daylight savings time.  */
179   t.tm_isdst = -1;
180   if (yyparse ())
181     return (time_t)-1;
182   else
183     return mktime (&t);
184 }
185
186 /* Parse a POSIX-style date and return it, or NULL for an error.  */
187
188 struct tm *
189 posixtm (s)
190      char *s;
191 {
192   if (posixtime (s) == -1)
193     return NULL;
194   return &t;
195 }