iPXE
prompt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Prompt for keypress
00029  *
00030  */
00031 
00032 #include <errno.h>
00033 #include <stdio.h>
00034 #include <ipxe/console.h>
00035 #include <usr/prompt.h>
00036 
00037 /**
00038  * Prompt for keypress
00039  *
00040  * @v text              Prompt string
00041  * @v timeout           Timeout period, in ticks (0=indefinite)
00042  * @v key               Key to wait for (0=any key)
00043  * @ret rc              Return status code
00044  *
00045  * Returns success if the specified key was pressed within the
00046  * specified timeout period.
00047  */
00048 int prompt ( const char *text, unsigned long timeout, int key ) {
00049         int key_pressed;
00050 
00051         /* Display prompt */
00052         printf ( "%s", text );
00053 
00054         /* Wait for key */
00055         key_pressed = getkey ( timeout );
00056 
00057         /* Clear the prompt line */
00058         while ( *(text++) )
00059                 printf ( "\b \b" );
00060 
00061         /* Check for timeout */
00062         if ( key_pressed < 0 )
00063                 return -ETIMEDOUT;
00064 
00065         /* Check for correct key pressed */
00066         if ( key && ( key_pressed != key ) )
00067                 return -ECANCELED;
00068 
00069         return 0;
00070 }