2007-06-28 15:53:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2007-06-28 15:53:12 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-08-03 13:49:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2007-06-28 15:53:12 +02:00
|
|
|
#include <errno.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/process.h>
|
2011-03-09 21:09:26 +01:00
|
|
|
#include <ipxe/console.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/keys.h>
|
|
|
|
#include <ipxe/job.h>
|
|
|
|
#include <ipxe/monojob.h>
|
|
|
|
#include <ipxe/timer.h>
|
2007-06-28 15:53:12 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Single foreground job
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int monojob_rc;
|
|
|
|
|
2008-06-12 22:47:19 +02:00
|
|
|
static void monojob_close ( struct interface *intf, int rc ) {
|
2007-06-28 15:53:12 +02:00
|
|
|
monojob_rc = rc;
|
2008-06-12 22:47:19 +02:00
|
|
|
intf_restart ( intf, rc );
|
2007-06-28 15:53:12 +02:00
|
|
|
}
|
|
|
|
|
2008-06-12 22:47:19 +02:00
|
|
|
static struct interface_operation monojob_intf_op[] = {
|
|
|
|
INTF_OP ( intf_close, struct interface *, monojob_close ),
|
2007-06-28 15:53:12 +02:00
|
|
|
};
|
|
|
|
|
2008-06-12 22:47:19 +02:00
|
|
|
static struct interface_descriptor monojob_intf_desc =
|
|
|
|
INTF_DESC_PURE ( monojob_intf_op );
|
|
|
|
|
|
|
|
struct interface monojob = INTF_INIT ( monojob_intf_desc );
|
2007-06-28 15:53:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for single foreground job to complete
|
|
|
|
*
|
2012-05-22 01:44:49 +02:00
|
|
|
* @v string Job description to display, or NULL to be silent
|
2007-06-28 15:53:12 +02:00
|
|
|
* @ret rc Job final status code
|
|
|
|
*/
|
2007-08-03 13:49:21 +02:00
|
|
|
int monojob_wait ( const char *string ) {
|
2011-03-08 00:55:57 +01:00
|
|
|
struct job_progress progress;
|
2007-06-28 15:53:12 +02:00
|
|
|
int key;
|
2007-08-03 13:49:21 +02:00
|
|
|
int rc;
|
2012-06-28 13:27:43 +02:00
|
|
|
unsigned long last_keycheck;
|
2011-03-08 00:55:57 +01:00
|
|
|
unsigned long last_progress;
|
2012-06-28 13:27:43 +02:00
|
|
|
unsigned long now;
|
2008-10-12 20:56:52 +02:00
|
|
|
unsigned long elapsed;
|
2011-03-26 14:13:45 +01:00
|
|
|
unsigned long completed;
|
|
|
|
unsigned long total;
|
2011-03-08 00:55:57 +01:00
|
|
|
unsigned int percentage;
|
|
|
|
int shown_percentage = 0;
|
2007-06-28 15:53:12 +02:00
|
|
|
|
2012-05-22 01:44:49 +02:00
|
|
|
if ( string )
|
|
|
|
printf ( "%s...", string );
|
2007-06-28 15:53:12 +02:00
|
|
|
monojob_rc = -EINPROGRESS;
|
2012-06-28 13:27:43 +02:00
|
|
|
last_keycheck = last_progress = currticks();
|
2007-06-28 15:53:12 +02:00
|
|
|
while ( monojob_rc == -EINPROGRESS ) {
|
2012-06-28 13:27:43 +02:00
|
|
|
|
|
|
|
/* Allow job to progress */
|
2007-06-28 15:53:12 +02:00
|
|
|
step();
|
2012-06-28 13:27:43 +02:00
|
|
|
now = currticks();
|
|
|
|
|
|
|
|
/* Check for keypresses. This can be time-consuming,
|
|
|
|
* so check only once per clock tick.
|
|
|
|
*/
|
|
|
|
if ( now != last_keycheck ) {
|
|
|
|
if ( iskey() ) {
|
|
|
|
key = getchar();
|
|
|
|
switch ( key ) {
|
|
|
|
case CTRL_C:
|
|
|
|
monojob_close ( &monojob, -ECANCELED );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-06-28 15:53:12 +02:00
|
|
|
}
|
2012-06-28 13:27:43 +02:00
|
|
|
last_keycheck = now;
|
2007-06-28 15:53:12 +02:00
|
|
|
}
|
2012-06-28 13:27:43 +02:00
|
|
|
|
|
|
|
/* Display progress, if applicable */
|
|
|
|
elapsed = ( now - last_progress );
|
2012-05-22 01:44:49 +02:00
|
|
|
if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
|
2011-03-08 00:55:57 +01:00
|
|
|
if ( shown_percentage )
|
|
|
|
printf ( "\b\b\b\b \b\b\b\b" );
|
|
|
|
job_progress ( &monojob, &progress );
|
2011-03-26 14:13:45 +01:00
|
|
|
/* Normalise progress figures to avoid overflow */
|
|
|
|
completed = ( progress.completed / 128 );
|
|
|
|
total = ( progress.total / 128 );
|
|
|
|
if ( total ) {
|
|
|
|
percentage = ( ( 100 * completed ) / total );
|
2011-03-08 00:55:57 +01:00
|
|
|
printf ( "%3d%%", percentage );
|
|
|
|
shown_percentage = 1;
|
|
|
|
} else {
|
|
|
|
printf ( "." );
|
|
|
|
shown_percentage = 0;
|
|
|
|
}
|
2012-06-28 13:27:43 +02:00
|
|
|
last_progress = now;
|
2008-07-24 21:08:31 +02:00
|
|
|
}
|
2007-06-28 15:53:12 +02:00
|
|
|
}
|
2007-08-03 13:49:21 +02:00
|
|
|
rc = monojob_rc;
|
|
|
|
|
2011-03-08 00:55:57 +01:00
|
|
|
if ( shown_percentage )
|
|
|
|
printf ( "\b\b\b\b \b\b\b\b" );
|
|
|
|
|
2012-05-22 01:44:49 +02:00
|
|
|
if ( string ) {
|
|
|
|
if ( rc ) {
|
|
|
|
printf ( " %s\n", strerror ( rc ) );
|
|
|
|
} else {
|
|
|
|
printf ( " ok\n" );
|
|
|
|
}
|
2007-08-03 13:49:21 +02:00
|
|
|
}
|
2012-05-22 01:44:49 +02:00
|
|
|
|
2007-08-03 13:49:21 +02:00
|
|
|
return rc;
|
2007-06-28 15:53:12 +02:00
|
|
|
}
|