1/*
2 * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * March-19-2009 @ 02:44: Added sleep command.
19 * Shao Miller <shao.miller@yrdsb.edu.on.ca>.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <gpxe/command.h>
27#include <gpxe/nap.h>
28#include <gpxe/timer.h>
29
30static int time_exec ( int argc, char **argv ) {
31	unsigned long start;
32	int rc, secs;
33
34	if ( argc == 1 ||
35	     !strcmp ( argv[1], "--help" ) ||
36	     !strcmp ( argv[1], "-h" ) )
37	{
38		printf ( "Usage:\n"
39			 "  %s <command>\n"
40			 "\n"
41			 "Time a command\n",
42			 argv[0] );
43		return 1;
44	}
45
46	start = currticks();
47	rc = execv ( argv[1], argv + 1 );
48	secs = (currticks() - start) / ticks_per_sec();
49
50	printf ( "%s: %ds\n", argv[0], secs );
51
52	return rc;
53}
54
55struct command time_command __command = {
56	.name = "time",
57	.exec = time_exec,
58};
59
60static int sleep_exec ( int argc, char **argv ) {
61	unsigned long start, delay;
62
63	if ( argc == 1 ||
64	     !strcmp ( argv[1], "--help" ) ||
65	     !strcmp ( argv[1], "-h" ))
66	{
67		printf ( "Usage:\n"
68			 "  %s <seconds>\n"
69			 "\n"
70			 "Sleep for <seconds> seconds\n",
71			 argv[0] );
72		return 1;
73	}
74	start = currticks();
75	delay = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
76	while ( ( currticks() - start ) <= delay )
77		cpu_nap();
78	return 0;
79}
80
81struct command sleep_command __command = {
82	.name = "sleep",
83	.exec = sleep_exec,
84};
85