1 |
/* GNU m4 -- A simple macro processor |
/* GNU m4 -- A simple macro processor |
2 |
Copyright 1999, 2000 Free Software Foundation, Inc. |
Copyright 1999, 2000, 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
17 |
02111-1307 USA |
02111-1307 USA |
18 |
*/ |
*/ |
19 |
|
|
20 |
#ifdef HAVE_CONFIG_H |
#if HAVE_CONFIG_H |
21 |
# include <config.h> |
# include <config.h> |
22 |
#endif |
#endif |
23 |
|
|
24 |
#include <stdio.h> |
#include <stdio.h> |
25 |
|
|
26 |
#ifdef TM_IN_SYS_TIME |
#if TM_IN_SYS_TIME |
27 |
#include <sys/time.h> |
# include <sys/time.h> |
28 |
#else |
#else |
29 |
#include <time.h> |
# include <time.h> |
30 |
#endif /* TM_IN_SYS_TIME */ |
#endif /* TM_IN_SYS_TIME */ |
31 |
|
|
32 |
#include <m4module.h> |
#include <m4module.h> |
48 |
|
|
49 |
#define BUILTIN(handler, macros, blind) M4BUILTIN(handler) |
#define BUILTIN(handler, macros, blind) M4BUILTIN(handler) |
50 |
builtin_functions |
builtin_functions |
51 |
# ifdef HAVE_MKTIME |
# if HAVE_MKTIME |
52 |
mktime_functions |
mktime_functions |
53 |
# endif |
# endif |
54 |
# ifdef HAVE_STRFTIME |
# if HAVE_STRFTIME |
55 |
strftime_functions |
strftime_functions |
56 |
# endif |
# endif |
57 |
#undef BUILTIN |
#undef BUILTIN |
62 |
{ STR(handler), CONC(builtin_, handler), macros, blind }, |
{ STR(handler), CONC(builtin_, handler), macros, blind }, |
63 |
|
|
64 |
builtin_functions |
builtin_functions |
65 |
# ifdef HAVE_MKTIME |
# if HAVE_MKTIME |
66 |
mktime_functions |
mktime_functions |
67 |
# endif |
# endif |
68 |
# ifdef HAVE_STRFTIME |
# if HAVE_STRFTIME |
69 |
strftime_functions |
strftime_functions |
70 |
# endif |
# endif |
71 |
#undef BUILTIN |
#undef BUILTIN |
72 |
|
|
73 |
{ 0, 0, FALSE, FALSE }, |
{ 0, 0, FALSE, FALSE }, |
74 |
}; |
}; |
|
|
|
75 |
|
|
|
|
|
76 |
/** |
/** |
77 |
* currenttime() |
* currenttime() |
78 |
**/ |
**/ |
79 |
M4BUILTIN_HANDLER(currenttime) |
M4BUILTIN_HANDLER (currenttime) |
80 |
{ |
{ |
81 |
char buf[64]; |
char buf[64]; |
82 |
time_t now; |
time_t now; |
85 |
if (m4_bad_argc (argv[0], argc, 1, 1)) |
if (m4_bad_argc (argv[0], argc, 1, 1)) |
86 |
return; |
return; |
87 |
|
|
88 |
now = time(0L); |
now = time (0L); |
89 |
l = sprintf(buf, "%ld", now); |
l = sprintf (buf, "%ld", now); |
90 |
|
|
91 |
obstack_grow (obs, buf, l); |
obstack_grow (obs, buf, l); |
92 |
} |
} |
94 |
/** |
/** |
95 |
* ctime([SECONDS]) |
* ctime([SECONDS]) |
96 |
**/ |
**/ |
97 |
M4BUILTIN_HANDLER(ctime) |
M4BUILTIN_HANDLER (ctime) |
98 |
{ |
{ |
99 |
time_t t; |
time_t t; |
100 |
|
|
102 |
return; |
return; |
103 |
|
|
104 |
if (argc == 2) |
if (argc == 2) |
105 |
m4_numeric_arg(argv[0], (char*)M4ARG(1), (int *)&t); |
m4_numeric_arg (argv[0], M4ARG (1), (int *) &t); |
106 |
else |
else |
107 |
t = time(0L); |
t = time (0L); |
108 |
|
|
109 |
obstack_grow (obs, ctime(&t), 24); |
obstack_grow (obs, ctime (&t), 24); |
110 |
} |
} |
111 |
|
|
112 |
static void |
static void |
113 |
format_tm(struct obstack *obs, struct tm *tm) |
format_tm (struct obstack *obs, struct tm *tm) |
114 |
{ |
{ |
115 |
m4_shipout_int(obs, tm->tm_sec); |
m4_shipout_int (obs, tm->tm_sec); |
116 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
117 |
|
|
118 |
m4_shipout_int(obs, tm->tm_min); |
m4_shipout_int (obs, tm->tm_min); |
119 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
120 |
|
|
121 |
m4_shipout_int(obs, tm->tm_hour); |
m4_shipout_int (obs, tm->tm_hour); |
122 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
123 |
|
|
124 |
m4_shipout_int(obs, tm->tm_mday); |
m4_shipout_int (obs, tm->tm_mday); |
125 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
126 |
|
|
127 |
m4_shipout_int(obs, tm->tm_mon); |
m4_shipout_int (obs, tm->tm_mon); |
128 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
129 |
|
|
130 |
m4_shipout_int(obs, tm->tm_year); |
m4_shipout_int (obs, tm->tm_year); |
131 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
132 |
|
|
133 |
m4_shipout_int(obs, tm->tm_wday); |
m4_shipout_int (obs, tm->tm_wday); |
134 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
135 |
|
|
136 |
m4_shipout_int(obs, tm->tm_yday); |
m4_shipout_int (obs, tm->tm_yday); |
137 |
obstack_1grow(obs, ','); |
obstack_1grow (obs, ','); |
138 |
|
|
139 |
m4_shipout_int(obs, tm->tm_isdst); |
m4_shipout_int (obs, tm->tm_isdst); |
140 |
} |
} |
141 |
|
|
142 |
/** |
/** |
143 |
* gmtime(SECONDS) |
* gmtime(SECONDS) |
144 |
**/ |
**/ |
145 |
M4BUILTIN_HANDLER(gmtime) |
M4BUILTIN_HANDLER (gmtime) |
146 |
{ |
{ |
147 |
time_t t; |
time_t t; |
148 |
|
|
149 |
if (m4_bad_argc (argv[0], argc, 2, 2)) |
if (m4_bad_argc (argv[0], argc, 2, 2)) |
150 |
return; |
return; |
151 |
|
|
152 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(1), (int *)&t)) |
if (!m4_numeric_arg (argv[0], M4ARG (1), (int *) &t)) |
153 |
return; |
return; |
154 |
|
|
155 |
format_tm(obs, gmtime(&t)); |
format_tm (obs, gmtime (&t)); |
156 |
} |
} |
157 |
|
|
158 |
/** |
/** |
159 |
* localtime(SECONDS) |
* localtime(SECONDS) |
160 |
**/ |
**/ |
161 |
M4BUILTIN_HANDLER(localtime) |
M4BUILTIN_HANDLER (localtime) |
162 |
{ |
{ |
163 |
time_t t; |
time_t t; |
164 |
|
|
165 |
if (m4_bad_argc (argv[0], argc, 2, 2)) |
if (m4_bad_argc (argv[0], argc, 2, 2)) |
166 |
return; |
return; |
167 |
|
|
168 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(1), (int *)&t)) |
if (!m4_numeric_arg (argv[0], M4ARG (1), (int *) &t)) |
169 |
return; |
return; |
170 |
|
|
171 |
format_tm(obs, localtime(&t)); |
format_tm (obs, localtime (&t)); |
172 |
} |
} |
173 |
|
|
174 |
#ifdef HAVE_MKTIME |
#if HAVE_MKTIME |
175 |
/** |
/** |
176 |
* mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST]) |
* mktime(SEC, MIN, HOUR, MDAY, MONTH, YEAR, [ISDST]) |
177 |
**/ |
**/ |
178 |
M4BUILTIN_HANDLER(mktime) |
M4BUILTIN_HANDLER (mktime) |
179 |
{ |
{ |
180 |
struct tm tm; |
struct tm tm; |
181 |
time_t t; |
time_t t; |
183 |
if (m4_bad_argc (argv[0], argc, 7, 8)) |
if (m4_bad_argc (argv[0], argc, 7, 8)) |
184 |
return; |
return; |
185 |
|
|
186 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(1), &tm.tm_sec)) |
if (!m4_numeric_arg (argv[0], M4ARG (1), &tm.tm_sec)) |
187 |
return; |
return; |
188 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(2), &tm.tm_min)) |
if (!m4_numeric_arg (argv[0], M4ARG (2), &tm.tm_min)) |
189 |
return; |
return; |
190 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(3), &tm.tm_hour)) |
if (!m4_numeric_arg (argv[0], M4ARG (3), &tm.tm_hour)) |
191 |
return; |
return; |
192 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(4), &tm.tm_mday)) |
if (!m4_numeric_arg (argv[0], M4ARG (4), &tm.tm_mday)) |
193 |
return; |
return; |
194 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(5), &tm.tm_mon)) |
if (!m4_numeric_arg (argv[0], M4ARG (5), &tm.tm_mon)) |
195 |
return; |
return; |
196 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(6), &tm.tm_year)) |
if (!m4_numeric_arg (argv[0], M4ARG (6), &tm.tm_year)) |
197 |
return; |
return; |
198 |
if (M4ARG(7) && !m4_numeric_arg (argv[0], (char*)M4ARG(7), &tm.tm_isdst)) |
if (M4ARG (7) && !m4_numeric_arg (argv[0], M4ARG (7), &tm.tm_isdst)) |
199 |
return; |
return; |
200 |
|
|
201 |
t = mktime(&tm); |
t = mktime (&tm); |
202 |
|
|
203 |
m4_shipout_int(obs, t); |
m4_shipout_int (obs, t); |
204 |
} |
} |
205 |
#endif /* HAVE_MKTIME */ |
#endif /* HAVE_MKTIME */ |
206 |
|
|
207 |
#ifdef HAVE_STRFTIME |
#if HAVE_STRFTIME |
208 |
/** |
/** |
209 |
* strftime(FORMAT, SECONDS) |
* strftime(FORMAT, SECONDS) |
210 |
**/ |
**/ |
211 |
M4BUILTIN_HANDLER(strftime) |
M4BUILTIN_HANDLER (strftime) |
212 |
{ |
{ |
213 |
struct tm *tm; |
struct tm *tm; |
214 |
time_t t; |
time_t t; |
218 |
if (m4_bad_argc (argv[0], argc, 3, 3)) |
if (m4_bad_argc (argv[0], argc, 3, 3)) |
219 |
return; |
return; |
220 |
|
|
221 |
if (!m4_numeric_arg (argv[0], (char*)M4ARG(2), (int *)&t)) |
if (!m4_numeric_arg (argv[0], M4ARG (2), (int *) &t)) |
222 |
return; |
return; |
223 |
|
|
224 |
tm = localtime(&t); |
tm = localtime (&t); |
225 |
|
|
226 |
buf = (char *) obstack_alloc(obs, 1024); |
buf = (char *) obstack_alloc (obs, 1024); |
227 |
l = strftime(buf, 1024, (char*)M4ARG(1), tm); |
l = strftime (buf, 1024, M4ARG (1), tm); |
228 |
obstack_grow(obs, buf, l); |
obstack_grow (obs, buf, l); |
229 |
} |
} |
230 |
#endif /* HAVE_STRFTIME */ |
#endif /* HAVE_STRFTIME */ |