Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

monojob.c 3.8KB

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