Initial revision
[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 sparc
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 #include <time.h>
37
38 #define YYDEBUG 1
39
40 /* Lexical analyzer's current scan position in the input string. */
41 static char *curpos;
42
43 /* The return value. */
44 static struct tm t;
45
46 time_t mktime ();
47
48 #define yyparse posixtime_yyparse
49 static int yylex ();
50 static int yyerror ();
51 %}
52
53 %token DIGIT
54
55 %%
56 date :
57        digitpair /* month */
58        digitpair /* day */
59        digitpair /* hours */
60        digitpair /* minutes */
61        year
62        seconds {
63                  if ($1 >= 1 && $1 <= 12)
64                    t.tm_mon = $1 - 1;
65                  else {
66                    YYABORT;
67                  }
68                  if ($2 >= 1 && $2 <= 31)
69                    t.tm_mday = $2;
70                  else {
71                    YYABORT;
72                  }
73                  if ($3 >= 0 && $3 <= 23)
74                    t.tm_hour = $3;
75                  else {
76                    YYABORT;
77                  }
78                  if ($4 >= 0 && $4 <= 59)
79                    t.tm_min = $4;
80                  else {
81                    YYABORT;
82                  }
83                }
84
85 year : digitpair {
86                    t.tm_year = $1;
87                    /* Deduce the century based on the year.
88                       See POSIX.2 section 4.63.3.  */
89                    if ($1 <= 68)
90                      t.tm_year += 100;
91                  }
92     | digitpair digitpair {
93                             t.tm_year = $1 * 100 + $2;
94                             if (t.tm_year < 1900) {
95                               YYABORT;
96                             } else
97                               t.tm_year -= 1900;
98                           }
99     | /* empty */ {
100                     time_t now;
101                     struct tm *tmp;
102
103                     /* Use current year.  */
104                     time (&now);
105                     tmp = localtime (&now);
106                     t.tm_year = tmp->tm_year;
107                   }
108     ;
109
110 seconds : /* empty */ {
111                         t.tm_sec = 0;
112                       }
113         | '.' digitpair {
114                           if ($2 >= 0 && $2 <= 61)
115                             t.tm_sec = $2;
116                           else {
117                             YYABORT;
118                           }
119                         }
120         ;
121
122 digitpair : DIGIT DIGIT {
123                           $$ = $1 * 10 + $2;
124                         }
125           ;
126 %%
127 static int
128 yylex ()
129 {
130   char ch = *curpos++;
131
132   if (ch >= '0' && ch <= '9')
133     {
134       yylval = ch - '0';
135       return DIGIT;
136     }
137   else if (ch == '.' || ch == 0)
138     return ch;
139   else
140     return '?';                 /* Cause an error.  */
141 }
142
143 static int
144 yyerror ()
145 {
146   return 0;
147 }
148
149 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error.  */
150
151 time_t
152 posixtime (s)
153      char *s;
154 {
155   curpos = s;
156   /* Let mktime decide whether it is daylight savings time.  */
157   t.tm_isdst = -1;
158   if (yyparse ())
159     return (time_t)-1;
160   else
161     return mktime (&t);
162 }
163
164 /* Parse a POSIX-style date and return it, or NULL for an error.  */
165
166 struct tm *
167 posixtm (s)
168      char *s;
169 {
170   if (posixtime (s) == -1)
171     return NULL;
172   return &t;
173 }