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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * @ret rc Job final status code
  50. */
  51. int monojob_wait ( const char *string ) {
  52. struct job_progress progress;
  53. int key;
  54. int rc;
  55. unsigned long last_keycheck;
  56. unsigned long last_progress;
  57. unsigned long now;
  58. unsigned long elapsed;
  59. unsigned long completed;
  60. unsigned long total;
  61. unsigned int percentage;
  62. int shown_percentage = 0;
  63. if ( string )
  64. printf ( "%s...", string );
  65. monojob_rc = -EINPROGRESS;
  66. last_keycheck = last_progress = currticks();
  67. while ( monojob_rc == -EINPROGRESS ) {
  68. /* Allow job to progress */
  69. step();
  70. now = currticks();
  71. /* Check for keypresses. This can be time-consuming,
  72. * so check only once per clock tick.
  73. */
  74. if ( now != last_keycheck ) {
  75. if ( iskey() ) {
  76. key = getchar();
  77. switch ( key ) {
  78. case CTRL_C:
  79. monojob_close ( &monojob, -ECANCELED );
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. last_keycheck = now;
  86. }
  87. /* Display progress, if applicable */
  88. elapsed = ( now - last_progress );
  89. if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
  90. if ( shown_percentage )
  91. printf ( "\b\b\b\b \b\b\b\b" );
  92. job_progress ( &monojob, &progress );
  93. /* Normalise progress figures to avoid overflow */
  94. completed = ( progress.completed / 128 );
  95. total = ( progress.total / 128 );
  96. if ( total ) {
  97. percentage = ( ( 100 * completed ) / total );
  98. printf ( "%3d%%", percentage );
  99. shown_percentage = 1;
  100. } else {
  101. printf ( "." );
  102. shown_percentage = 0;
  103. }
  104. last_progress = now;
  105. }
  106. }
  107. rc = monojob_rc;
  108. if ( shown_percentage )
  109. printf ( "\b\b\b\b \b\b\b\b" );
  110. if ( string ) {
  111. if ( rc ) {
  112. printf ( " %s\n", strerror ( rc ) );
  113. } else {
  114. printf ( " ok\n" );
  115. }
  116. }
  117. return rc;
  118. }