[SETUPLIB] Improve the bootloader 'validity' checks -- Addendum to f06734e5 (r74512).
[reactos.git] / base / shell / cmd / timer.c
1 /*
2 * TIMER.C - timer internal command.
3 *
4 * clone from 4nt timer command
5 *
6 * 20 Aug 1999
7 * started - Paolo Pantaleo <paolopan@freemail.it>
8 */
9
10 #include "precomp.h"
11
12 #ifdef INCLUDE_CMD_TIMER
13
14
15 #define NCS_NOT_SPECIFIED -1
16 #define NCS_ON 1
17 #define NCS_OFF 0
18
19 //print timer value
20 #define PT(format) PrintElapsedTime(GetTickCount()-cT,format)
21
22
23 //current timer Time (at wich started to count)
24 #define cT clksT[clk_n]
25
26 //current timer status
27 #define cS clksS[clk_n]
28
29
30 static VOID
31 PrintElapsedTime (DWORD time,INT format)
32 {
33 DWORD h,m,s,ms;
34
35 TRACE ("PrintElapsedTime(%d,%d)",time,format);
36
37 switch (format)
38 {
39 case 0:
40 ConOutResPrintf(STRING_TIMER_HELP1, time);
41 break;
42
43 case 1:
44 ms = time % 1000;
45 time /= 1000;
46 s = time % 60;
47 time /=60;
48 m = time % 60;
49 h = time / 60;
50 ConOutResPrintf(STRING_TIMER_HELP2,
51 h, cTimeSeparator,
52 m, cTimeSeparator,
53 s, cDecimalSeparator, ms/10);
54 break;
55 }
56 }
57
58
59 INT CommandTimer (LPTSTR param)
60 {
61 // all timers are kept
62 static DWORD clksT[10];
63
64 // timers status
65 // set all the clocks off by default
66 static BOOL clksS[10]={FALSE,FALSE,FALSE,FALSE,
67 FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
68
69 // TRUE if /S in command line
70 BOOL bS = FALSE;
71
72 // avoid to set clk_n more than once
73 BOOL bCanNSet = TRUE;
74
75 INT NewClkStatus = NCS_NOT_SPECIFIED;
76
77 // the clock number specified on the command line
78 // 1 by default
79 INT clk_n=1;
80
81 // output format
82 INT iFormat=1;
83
84
85 // command line parsing variables
86 INT argc;
87 LPTSTR *p;
88
89 INT i;
90
91 if (_tcsncmp (param, _T("/?"), 2) == 0)
92 {
93 ConOutResPrintf(STRING_TIMER_HELP3, cTimeSeparator, cTimeSeparator, cDecimalSeparator);
94 return 0;
95 }
96
97 nErrorLevel = 0;
98
99 p = split (param, &argc, FALSE, FALSE);
100
101 //read options
102 for (i = 0; i < argc; i++)
103 {
104 //set timer on
105 if (!(_tcsicmp(&p[i][0],_T("on"))) && NewClkStatus == NCS_NOT_SPECIFIED)
106 {
107 NewClkStatus = NCS_ON;
108 continue;
109 }
110
111 //set timer off
112 if (!(_tcsicmp(&p[i][0],_T("off"))) && NewClkStatus == NCS_NOT_SPECIFIED)
113 {
114 NewClkStatus = NCS_OFF;
115 continue;
116 }
117
118 // other options
119 if (p[i][0] == _T('/'))
120 {
121 // set timer number
122 if (_istdigit(p[i][1]) && bCanNSet)
123 {
124 clk_n = p[i][1] - _T('0');
125 bCanNSet = FALSE;
126 continue;
127 }
128
129 // set s(plit) option
130 if (_totupper(p[i][1]) == _T('S'))
131 {
132 bS = TRUE;
133 continue;
134 }
135
136 // specify format
137 if (_totupper(p[i][1]) == _T('F'))
138 {
139 iFormat = p[i][2] - _T('0');
140 continue;
141 }
142 }
143 }
144
145 // do stuff (start/stop/read timer)
146 if (NewClkStatus == NCS_ON)
147 {
148 cT=GetTickCount();
149 cS=TRUE;
150
151 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
152 ConOutPuts(GetTimeString());
153 freep(p);
154 return 0;
155 }
156
157 if (bS)
158 {
159 if (cS)
160 {
161 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
162 ConOutPrintf(_T("%s\n"), GetTimeString());
163 PrintElapsedTime(GetTickCount()-cT, iFormat);
164 freep(p);
165 return 0;
166 }
167
168 cT=GetTickCount();
169 cS=TRUE;
170 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
171 ConOutPuts(GetTimeString());
172 freep(p);
173 return 0;
174 }
175
176 if (NewClkStatus == NCS_NOT_SPECIFIED)
177 {
178 if (cS)
179 {
180 cS=FALSE;
181 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
182 ConOutPrintf(_T("%s\n"), GetTimeString());
183 PrintElapsedTime(GetTickCount()-cT, iFormat);
184 freep(p);
185 return 0;
186 }
187
188 cT=GetTickCount();
189 cS=TRUE;
190 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
191 ConOutPuts(GetTimeString());
192 freep(p);
193 return 0;
194 }
195
196
197 if (NewClkStatus == NCS_OFF)
198 {
199 if (cS)
200 {
201 cS=FALSE;
202 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
203 ConOutPrintf(_T("%s\n"), GetTimeString());
204 PrintElapsedTime(GetTickCount()-cT, iFormat);
205 freep(p);
206 return 0;
207 }
208 ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF"));
209 ConOutPuts(GetTimeString());
210 freep(p);
211 return 0;
212 }
213
214 freep(p);
215 return 0;
216 }
217
218 #endif /* INCLUDE_CMD_TIMER */