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
monojob_close(struct interface * intf,int rc)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 * Clear previously displayed message
60 *
61 * @v len Length of previously displayed message
62 */
monojob_clear(size_t len)63 static void monojob_clear ( size_t len ) {
64 unsigned int i;
65
66 for ( i = 0 ; i < len ; i++ )
67 putchar ( '\b' );
68 for ( i = 0 ; i < len ; i++ )
69 putchar ( ' ' );
70 for ( i = 0 ; i < len ; i++ )
71 putchar ( '\b' );
72 }
73
74 /**
75 * Wait for single foreground job to complete
76 *
77 * @v string Job description to display, or NULL to be silent
78 * @v timeout Timeout period, in ticks (0=indefinite)
79 * @ret rc Job final status code
80 */
monojob_wait(const char * string,unsigned long timeout)81 int monojob_wait ( const char *string, unsigned long timeout ) {
82 struct job_progress progress;
83 unsigned long last_check;
84 unsigned long last_progress;
85 unsigned long last_display;
86 unsigned long now;
87 unsigned long elapsed;
88 unsigned long completed = 0;
89 unsigned long scaled_completed;
90 unsigned long scaled_total;
91 unsigned int percentage;
92 size_t clear_len = 0;
93 int ongoing_rc;
94 int key;
95 int rc;
96
97 if ( string )
98 printf ( "%s...", string );
99 monojob_rc = -EINPROGRESS;
100 last_check = last_progress = last_display = currticks();
101 while ( monojob_rc == -EINPROGRESS ) {
102
103 /* Allow job to progress */
104 step();
105 now = currticks();
106
107 /* Continue until a timer tick occurs (to minimise
108 * time wasted checking for progress and keypresses).
109 */
110 elapsed = ( now - last_check );
111 if ( ! elapsed )
112 continue;
113 last_check = now;
114
115 /* Check for keypresses */
116 if ( iskey() ) {
117 key = getchar();
118 if ( key == CTRL_C ) {
119 monojob_rc = -ECANCELED;
120 break;
121 }
122 }
123
124 /* Monitor progress */
125 ongoing_rc = job_progress ( &monojob, &progress );
126
127 /* Reset timeout if progress has been made */
128 if ( completed != progress.completed )
129 last_progress = now;
130 completed = progress.completed;
131
132 /* Check for timeout, if applicable */
133 elapsed = ( now - last_progress );
134 if ( timeout && ( elapsed >= timeout ) ) {
135 monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
136 break;
137 }
138
139 /* Display progress, if applicable */
140 elapsed = ( now - last_display );
141 if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
142 monojob_clear ( clear_len );
143 /* Normalise progress figures to avoid overflow */
144 scaled_completed = ( progress.completed / 128 );
145 scaled_total = ( progress.total / 128 );
146 if ( scaled_total ) {
147 percentage = ( ( 100 * scaled_completed ) /
148 scaled_total );
149 clear_len = printf ( "%3d%%", percentage );
150 } else {
151 printf ( "." );
152 clear_len = 0;
153 }
154 if ( progress.message[0] ) {
155 clear_len += printf ( " [%s]",
156 progress.message );
157 }
158 last_display = now;
159 }
160 }
161 rc = monojob_rc;
162 monojob_close ( &monojob, rc );
163
164 monojob_clear ( clear_len );
165 if ( string ) {
166 if ( rc ) {
167 printf ( " %s\n", strerror ( rc ) );
168 } else {
169 printf ( " ok\n" );
170 }
171 }
172
173 return rc;
174 }
175