These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / monojob.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <ipxe/process.h>
30 #include <ipxe/console.h>
31 #include <ipxe/keys.h>
32 #include <ipxe/job.h>
33 #include <ipxe/monojob.h>
34 #include <ipxe/timer.h>
35
36 /** @file
37  *
38  * Single foreground job
39  *
40  */
41
42 static int monojob_rc;
43
44 static void monojob_close ( struct interface *intf, int rc ) {
45         monojob_rc = rc;
46         intf_restart ( intf, rc );
47 }
48
49 static struct interface_operation monojob_intf_op[] = {
50         INTF_OP ( intf_close, struct interface *, monojob_close ),
51 };
52
53 static struct interface_descriptor monojob_intf_desc =
54         INTF_DESC_PURE ( monojob_intf_op );
55
56 struct interface monojob = INTF_INIT ( monojob_intf_desc );
57
58 /**
59  * Wait for single foreground job to complete
60  *
61  * @v string            Job description to display, or NULL to be silent
62  * @v timeout           Timeout period, in ticks (0=indefinite)
63  * @ret rc              Job final status code
64  */
65 int monojob_wait ( const char *string, unsigned long timeout ) {
66         struct job_progress progress;
67         unsigned long last_keycheck;
68         unsigned long last_progress;
69         unsigned long last_display;
70         unsigned long now;
71         unsigned long elapsed;
72         unsigned long completed = 0;
73         unsigned long scaled_completed;
74         unsigned long scaled_total;
75         unsigned int percentage;
76         int shown_percentage = 0;
77         int ongoing_rc;
78         int key;
79         int rc;
80
81         if ( string )
82                 printf ( "%s...", string );
83         monojob_rc = -EINPROGRESS;
84         last_keycheck = last_progress = last_display = currticks();
85         while ( monojob_rc == -EINPROGRESS ) {
86
87                 /* Allow job to progress */
88                 step();
89                 now = currticks();
90
91                 /* Check for keypresses.  This can be time-consuming,
92                  * so check only once per clock tick.
93                  */
94                 elapsed = ( now - last_keycheck );
95                 if ( elapsed ) {
96                         if ( iskey() ) {
97                                 key = getchar();
98                                 if ( key == CTRL_C ) {
99                                         monojob_rc = -ECANCELED;
100                                         break;
101                                 }
102                         }
103                         last_keycheck = now;
104                 }
105
106                 /* Monitor progress */
107                 ongoing_rc = job_progress ( &monojob, &progress );
108
109                 /* Reset timeout if progress has been made */
110                 if ( completed != progress.completed )
111                         last_progress = now;
112                 completed = progress.completed;
113
114                 /* Check for timeout, if applicable */
115                 elapsed = ( now - last_progress );
116                 if ( timeout && ( elapsed >= timeout ) ) {
117                         monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
118                         break;
119                 }
120
121                 /* Display progress, if applicable */
122                 elapsed = ( now - last_display );
123                 if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
124                         if ( shown_percentage )
125                                 printf ( "\b\b\b\b    \b\b\b\b" );
126                         /* Normalise progress figures to avoid overflow */
127                         scaled_completed = ( progress.completed / 128 );
128                         scaled_total = ( progress.total / 128 );
129                         if ( scaled_total ) {
130                                 percentage = ( ( 100 * scaled_completed ) /
131                                                scaled_total );
132                                 printf ( "%3d%%", percentage );
133                                 shown_percentage = 1;
134                         } else {
135                                 printf ( "." );
136                                 shown_percentage = 0;
137                         }
138                         last_display = now;
139                 }
140         }
141         rc = monojob_rc;
142         monojob_close ( &monojob, rc );
143
144         if ( shown_percentage )
145                 printf ( "\b\b\b\b    \b\b\b\b" );
146
147         if ( string ) {
148                 if ( rc ) {
149                         printf ( " %s\n", strerror ( rc ) );
150                 } else {
151                         printf ( " ok\n" );
152                 }
153         }
154
155         return rc;
156 }