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