GNU shell utilities
[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
21 #ifdef HAVE_CONFIG_H
22 #if defined (CONFIG_BROKETS)
23 /* We use <config.h> instead of "config.h" so that a compilation
24    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
25    (which it would do because it found this file in $srcdir).  */
26 #include <config.h>
27 #else
28 #include "config.h"
29 #endif
30 #endif
31
32 /* The following block of alloca-related preprocessor directives is here
33    solely to allow compilation by non GNU-C compilers of the C parser
34    produced from this file by old versions of bison.  Newer versions of
35    bison include a block similar to this one in bison.simple.  */
36    
37 #ifdef __GNUC__
38 #define alloca __builtin_alloca
39 #else
40 #ifdef HAVE_ALLOCA_H
41 #include <alloca.h>
42 #else
43 #ifdef _AIX
44  #pragma alloca
45 #else
46 void *alloca ();
47 #endif
48 #endif
49 #endif
50
51 #include <stdio.h>
52 #include <sys/types.h>
53
54 #ifdef TM_IN_SYS_TIME
55 #include <sys/time.h>
56 #else
57 #include <time.h>
58 #endif
59
60 /* Some old versions of bison generate parsers that use bcopy.
61    That loses on systems that don't provide the function, so we have
62    to redefine it here.  */
63 #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
64 #define bcopy(from, to, len) memcpy ((to), (from), (len))
65 #endif
66
67 #define YYDEBUG 1
68
69 /* Lexical analyzer's current scan position in the input string. */
70 static char *curpos;
71
72 /* The return value. */
73 static struct tm t;
74
75 time_t mktime ();
76
77 #define yyparse posixtime_yyparse
78 static int yylex ();
79 static int yyerror ();
80 %}
81
82 %token DIGIT
83
84 %%
85 date :
86        digitpair /* month */
87        digitpair /* day */
88        digitpair /* hours */
89        digitpair /* minutes */
90        year
91        seconds {
92                  if ($1 >= 1 && $1 <= 12)
93                    t.tm_mon = $1 - 1;
94                  else {
95                    YYABORT;
96                  }
97                  if ($2 >= 1 && $2 <= 31)
98                    t.tm_mday = $2;
99                  else {
100                    YYABORT;
101                  }
102                  if ($3 >= 0 && $3 <= 23)
103                    t.tm_hour = $3;
104                  else {
105                    YYABORT;
106                  }
107                  if ($4 >= 0 && $4 <= 59)
108                    t.tm_min = $4;
109                  else {
110                    YYABORT;
111                  }
112                }
113
114 year : digitpair {
115                    t.tm_year = $1;
116                    /* Deduce the century based on the year.
117                       See POSIX.2 section 4.63.3.  */
118                    if ($1 <= 68)
119                      t.tm_year += 100;
120                  }
121     | digitpair digitpair {
122                             t.tm_year = $1 * 100 + $2;
123                             if (t.tm_year < 1900) {
124                               YYABORT;
125                             } else
126                               t.tm_year -= 1900;
127                           }
128     | /* empty */ {
129                     time_t now;
130                     struct tm *tmp;
131
132                     /* Use current year.  */
133                     time (&now);
134                     tmp = localtime (&now);
135                     t.tm_year = tmp->tm_year;
136                   }
137     ;
138
139 seconds : /* empty */ {
140                         t.tm_sec = 0;
141                       }
142         | '.' digitpair {
143                           if ($2 >= 0 && $2 <= 61)
144                             t.tm_sec = $2;
145                           else {
146                             YYABORT;
147                           }
148                         }
149         ;
150
151 digitpair : DIGIT DIGIT {
152                           $$ = $1 * 10 + $2;
153                         }
154           ;
155 %%
156 static int
157 yylex ()
158 {
159   char ch = *curpos++;
160
161   if (ch >= '0' && ch <= '9')
162     {
163       yylval = ch - '0';
164       return DIGIT;
165     }
166   else if (ch == '.' || ch == 0)
167     return ch;
168   else
169     return '?';                 /* Cause an error.  */
170 }
171
172 static int
173 yyerror ()
174 {
175   return 0;
176 }
177
178 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
179
180 time_t
181 posixtime (s)
182      char *s;
183 {
184   curpos = s;
185   /* Let mktime decide whether it is daylight savings time.  */
186   t.tm_isdst = -1;
187   if (yyparse ())
188     return (time_t)-1;
189   else
190     return mktime (&t);
191 }
192
193 /* Parse a POSIX-style date and return it, or NULL for an error.  */
194
195 struct tm *
196 posixtm (s)
197      char *s;
198 {
199   if (posixtime (s) == -1)
200     return NULL;
201   return &t;
202 }