Crtl-C gives a new line when reading input
[reactos.git] / reactos / subsys / system / cmd / time.c
1 /*
2 * TIME.C - time internal command.
3 *
4 *
5 * History:
6 *
7 * 07/08/1998 (John P. Price)
8 * started.
9 *
10 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
11 * added config.h include
12 *
13 * 09-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
14 * Added locale support.
15 *
16 * 19-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17 * Unicode and redirection safe!
18 * Added "/t" option.
19 *
20 * 04-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
21 * Fixed time input bug.
22 *
23 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
24 * Remove all hardcode string to En.rc.
25 */
26
27 #include <precomp.h>
28 #include "resource.h"
29
30 #ifdef INCLUDE_CMD_TIME
31
32
33 static BOOL ParseTime (LPTSTR s)
34 {
35 SYSTEMTIME t;
36 LPTSTR p = s;
37
38 if (!*s)
39 return TRUE;
40
41 GetLocalTime (&t);
42 t.wHour = 0;
43 t.wMinute = 0;
44 t.wSecond = 0;
45 t.wMilliseconds = 0;
46
47 // first get hour
48 if (_istdigit(*p))
49 {
50 while (_istdigit(*p))
51 {
52 t.wHour = t.wHour * 10 + *p - _T('0');
53 p++;
54 }
55 }
56 else
57 return FALSE;
58
59 // get time separator
60 if (*p != cTimeSeparator)
61 return FALSE;
62 p++;
63
64 // now get minutes
65 if (_istdigit(*p))
66 {
67 while (_istdigit(*p))
68 {
69 t.wMinute = t.wMinute * 10 + *p - _T('0');
70 p++;
71 }
72 }
73 else
74 return FALSE;
75
76 // get time separator
77 if (*p != cTimeSeparator)
78 return FALSE;
79 p++;
80
81 // now get seconds
82 if (_istdigit(*p))
83 {
84 while (_istdigit(*p))
85 {
86 t.wSecond = t.wSecond * 10 + *p - _T('0');
87 p++;
88 }
89 }
90 else
91 return FALSE;
92
93 // get decimal separator
94 if (*p == cDecimalSeparator)
95 {
96 p++;
97
98 // now get hundreths
99 if (_istdigit(*p))
100 {
101 while (_istdigit(*p))
102 {
103 // t.wMilliseconds = t.wMilliseconds * 10 + *p - _T('0');
104 p++;
105 }
106 // t.wMilliseconds *= 10;
107 }
108 }
109
110 /* special case: 12 hour format */
111 if (nTimeFormat == 0)
112 {
113 if (_totupper(*s) == _T('P'))
114 {
115 t.wHour += 12;
116 }
117
118 if ((_totupper(*s) == _T('A')) && (t.wHour == 12))
119 {
120 t.wHour = 0;
121 }
122 }
123
124 if (t.wHour > 23 || t.wMinute > 60 || t.wSecond > 60 || t.wMilliseconds > 999)
125 return FALSE;
126
127 SetLocalTime (&t);
128
129 return TRUE;
130 }
131
132
133 INT cmd_time (LPTSTR cmd, LPTSTR param)
134 {
135 LPTSTR *arg;
136 INT argc;
137 INT i;
138 BOOL bPrompt = TRUE;
139 INT nTimeString = -1;
140
141 if (!_tcsncmp (param, _T("/?"), 2))
142 {
143 ConOutResPaging(TRUE,STRING_TIME_HELP1);
144 return 0;
145 }
146
147 nErrorLevel = 0;
148
149 /* build parameter array */
150 arg = split (param, &argc, FALSE);
151
152 /* check for options */
153 for (i = 0; i < argc; i++)
154 {
155 if (_tcsicmp (arg[i], _T("/t")) == 0)
156 bPrompt = FALSE;
157
158 if ((*arg[i] != _T('/')) && (nTimeString == -1))
159 nTimeString = i;
160 }
161
162 if (nTimeString == -1)
163 PrintTime ();
164
165 if (!bPrompt)
166 {
167 freep (arg);
168 return 0;
169 }
170
171 while (1)
172 {
173 if (nTimeString == -1)
174 {
175 TCHAR s[40];
176
177 ConOutResPuts(STRING_TIME_HELP2);
178
179 ConInString (s, 40);
180
181 #ifdef _DEBUG
182 DebugPrintf (_T("\'%s\'\n"), s);
183 #endif
184
185 while (*s && s[_tcslen (s) - 1] < _T(' '))
186 s[_tcslen(s) - 1] = _T('\0');
187
188 if (ParseTime (s))
189 {
190 freep (arg);
191 return 0;
192 }
193 }
194 else
195 {
196 if (ParseTime (arg[nTimeString]))
197 {
198 freep (arg);
199 return 0;
200 }
201
202 /* force input the next time around. */
203 nTimeString = -1;
204 }
205
206 ConErrResPuts(STRING_TIME_ERROR1);
207 nErrorLevel = 1;
208 }
209
210 freep (arg);
211
212 return 0;
213 }
214
215 #endif