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.

lineconsole.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2012 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Line-based console
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. #include <ipxe/ansiesc.h>
  27. #include <ipxe/lineconsole.h>
  28. /** Line-based console ANSI escape sequence handlers */
  29. static struct ansiesc_handler line_ansiesc_handlers[] = {
  30. { 0, NULL }
  31. };
  32. /** Line-based console ANSI escape sequence context */
  33. static struct ansiesc_context line_ansiesc_ctx = {
  34. .handlers = line_ansiesc_handlers,
  35. };
  36. /**
  37. * Print a character to a line-based console
  38. *
  39. * @v character Character to be printed
  40. * @ret print Print line
  41. */
  42. size_t line_putchar ( struct line_console *line, int character ) {
  43. /* Strip ANSI escape sequences */
  44. character = ansiesc_process ( &line_ansiesc_ctx, character );
  45. if ( character < 0 )
  46. return 0;
  47. /* Ignore carriage return */
  48. if ( character == '\r' )
  49. return 0;
  50. /* Treat newline as a terminator */
  51. if ( character == '\n' )
  52. character = 0;
  53. /* Add character to buffer */
  54. line->buffer[line->index++] = character;
  55. /* Do nothing more unless we reach end-of-line (or end-of-buffer) */
  56. if ( ( character != 0 ) &&
  57. ( line->index < ( line->len - 1 /* NUL */ ) ) ) {
  58. return 0;
  59. }
  60. /* Reset to start of buffer */
  61. line->index = 0;
  62. return 1;
  63. }