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