You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

monojob.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ipxe/process.h>
  28. #include <ipxe/console.h>
  29. #include <ipxe/keys.h>
  30. #include <ipxe/job.h>
  31. #include <ipxe/monojob.h>
  32. #include <ipxe/timer.h>
  33. /** @file
  34. *
  35. * Single foreground job
  36. *
  37. */
  38. static int monojob_rc;
  39. static void monojob_close ( struct interface *intf, int rc ) {
  40. monojob_rc = rc;
  41. intf_restart ( intf, rc );
  42. }
  43. static struct interface_operation monojob_intf_op[] = {
  44. INTF_OP ( intf_close, struct interface *, monojob_close ),
  45. };
  46. static struct interface_descriptor monojob_intf_desc =
  47. INTF_DESC_PURE ( monojob_intf_op );
  48. struct interface monojob = INTF_INIT ( monojob_intf_desc );
  49. /**
  50. * Wait for single foreground job to complete
  51. *
  52. * @v string Job description to display, or NULL to be silent
  53. * @v timeout Timeout period, in ticks (0=indefinite)
  54. * @ret rc Job final status code
  55. */
  56. int monojob_wait ( const char *string, unsigned long timeout ) {
  57. struct job_progress progress;
  58. unsigned long last_keycheck;
  59. unsigned long last_progress;
  60. unsigned long last_display;
  61. unsigned long now;
  62. unsigned long elapsed;
  63. unsigned long completed = 0;
  64. unsigned long scaled_completed;
  65. unsigned long scaled_total;
  66. unsigned int percentage;
  67. int shown_percentage = 0;
  68. int ongoing_rc;
  69. int key;
  70. int rc;
  71. if ( string )
  72. printf ( "%s...", string );
  73. monojob_rc = -EINPROGRESS;
  74. last_keycheck = last_progress = last_display = currticks();
  75. while ( monojob_rc == -EINPROGRESS ) {
  76. /* Allow job to progress */
  77. step();
  78. now = currticks();
  79. /* Check for keypresses. This can be time-consuming,
  80. * so check only once per clock tick.
  81. */
  82. elapsed = ( now - last_keycheck );
  83. if ( elapsed ) {
  84. if ( iskey() ) {
  85. key = getchar();
  86. if ( key == CTRL_C ) {
  87. monojob_rc = -ECANCELED;
  88. break;
  89. }
  90. }
  91. last_keycheck = now;
  92. }
  93. /* Monitor progress */
  94. ongoing_rc = job_progress ( &monojob, &progress );
  95. /* Reset timeout if progress has been made */
  96. if ( completed != progress.completed )
  97. last_progress = now;
  98. completed = progress.completed;
  99. /* Check for timeout, if applicable */
  100. elapsed = ( now - last_progress );
  101. if ( timeout && ( elapsed >= timeout ) ) {
  102. monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
  103. break;
  104. }
  105. /* Display progress, if applicable */
  106. elapsed = ( now - last_display );
  107. if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
  108. if ( shown_percentage )
  109. printf ( "\b\b\b\b \b\b\b\b" );
  110. /* Normalise progress figures to avoid overflow */
  111. scaled_completed = ( progress.completed / 128 );
  112. scaled_total = ( progress.total / 128 );
  113. if ( scaled_total ) {
  114. percentage = ( ( 100 * scaled_completed ) /
  115. scaled_total );
  116. printf ( "%3d%%", percentage );
  117. shown_percentage = 1;
  118. } else {
  119. printf ( "." );
  120. shown_percentage = 0;
  121. }
  122. last_display = now;
  123. }
  124. }
  125. rc = monojob_rc;
  126. monojob_close ( &monojob, rc );
  127. if ( shown_percentage )
  128. printf ( "\b\b\b\b \b\b\b\b" );
  129. if ( string ) {
  130. if ( rc ) {
  131. printf ( " %s\n", strerror ( rc ) );
  132. } else {
  133. printf ( " ok\n" );
  134. }
  135. }
  136. return rc;
  137. }