monojob.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <errno.h>
00024 #include <gpxe/process.h>
00025 #include <console.h>
00026 #include <gpxe/keys.h>
00027 #include <gpxe/job.h>
00028 #include <gpxe/monojob.h>
00029 #include <gpxe/timer.h>
00030 
00031 /** @file
00032  *
00033  * Single foreground job
00034  *
00035  */
00036 
00037 static int monojob_rc;
00038 
00039 static void monojob_done ( struct job_interface *job __unused, int rc ) {
00040         monojob_rc = rc;
00041 }
00042 
00043 /** Single foreground job operations */
00044 static struct job_interface_operations monojob_operations = {
00045         .done           = monojob_done,
00046         .kill           = ignore_job_kill,
00047         .progress       = ignore_job_progress,
00048 };
00049 
00050 /** Single foreground job */
00051 struct job_interface monojob = {
00052         .intf = {
00053                 .dest = &null_job.intf,
00054                 .refcnt = NULL,
00055         },
00056         .op = &monojob_operations,
00057 };
00058 
00059 /**
00060  * Wait for single foreground job to complete
00061  *
00062  * @v string            Job description to display
00063  * @ret rc              Job final status code
00064  */
00065 int monojob_wait ( const char *string ) {
00066         int key;
00067         int rc;
00068         unsigned long last_progress_dot;
00069         unsigned long elapsed;
00070 
00071         printf ( "%s.", string );
00072         monojob_rc = -EINPROGRESS;
00073         last_progress_dot = currticks();
00074         while ( monojob_rc == -EINPROGRESS ) {
00075                 step();
00076                 if ( iskey() ) {
00077                         key = getchar();
00078                         switch ( key ) {
00079                         case CTRL_C:
00080                                 job_kill ( &monojob );
00081                                 rc = -ECANCELED;
00082                                 goto done;
00083                         default:
00084                                 break;
00085                         }
00086                 }
00087                 elapsed = ( currticks() - last_progress_dot );
00088                 if ( elapsed >= TICKS_PER_SEC ) {
00089                         printf ( "." );
00090                         last_progress_dot = currticks();
00091                 }
00092         }
00093         rc = monojob_rc;
00094 
00095 done:
00096         job_done ( &monojob, rc );
00097         if ( rc ) {
00098                 printf ( " %s\n", strerror ( rc ) );
00099         } else {
00100                 printf ( " ok\n" );
00101         }
00102         return rc;
00103 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1