hdt: Adding lib-ansi to manage ansi escape codes
authorErwan Velu <erwan.velu@free.fr>
Tue, 31 Mar 2009 19:28:05 +0000 (21:28 +0200)
committerErwan Velu <erwan.velu@free.fr>
Tue, 31 Mar 2009 19:28:05 +0000 (21:28 +0200)
Impact: code is easier to read

Basic implementation of ansi escape codes. This make code easier to
read.

com32/hdt/Makefile
com32/hdt/hdt-cli.c
com32/hdt/hdt-common.c
com32/hdt/lib-ansi.c [new file with mode: 0644]
com32/hdt/lib-ansi.h [new file with mode: 0644]

index 9a51024..20f89d6 100644 (file)
@@ -54,6 +54,7 @@ hdt.elf: hdt.o hdt-ata.o hdt-menu.o hdt-menu-pci.o hdt-menu-kernel.o \
        hdt-cli-cpu.o hdt-cli-pxe.o hdt-cli-kernel.o \
        hdt-cli-syslinux.o hdt-cli-vesa.o\
        hdt-menu-pxe.o hdt-menu-summary.o hdt-menu-vesa.o\
+       lib-ansi.o\
        $(LIBS)
        @$(LD) $(LDFLAGS) -o $@ $^
 
index 0331ede..e40cf07 100644 (file)
@@ -32,6 +32,7 @@
 #include <getkey.h>
 #include "hdt-cli.h"
 #include "hdt-common.h"
+#include "lib-ansi.h"
 
 struct cli_mode_descr *list_modes[] = {
        &hdt_mode,
@@ -706,7 +707,7 @@ void start_cli_mode(struct s_hardware *hardware)
        more_printf("Entering CLI mode\n");
 
        /* Display the cursor */
-       fputs("\033[?25h", stdout);
+       display_cursor(true);
 
        reset_prompt();
 
@@ -723,7 +724,7 @@ void start_cli_mode(struct s_hardware *hardware)
                        /* clear until then end of line */
                case KEY_CTRL('k'):
                        /* Clear the end of the line */
-                       fputs("\033[0K", stdout);
+                       clear_end_of_line();
                        memset(&hdt_cli.input[hdt_cli.cursor_pos], 0,
                               strlen(hdt_cli.input) - hdt_cli.cursor_pos);
                        break;
@@ -735,14 +736,14 @@ void start_cli_mode(struct s_hardware *hardware)
 
                case KEY_LEFT:
                        if (hdt_cli.cursor_pos > 0) {
-                               fputs("\033[1D", stdout);
+                               move_cursor_left(1);
                                hdt_cli.cursor_pos--;
                        }
                        break;
 
                case KEY_RIGHT:
                        if (hdt_cli.cursor_pos < (int)strlen(hdt_cli.input)) {
-                               fputs("\033[1C", stdout);
+                               move_cursor_right(1);
                                hdt_cli.cursor_pos++;
                        }
                        break;
@@ -752,11 +753,8 @@ void start_cli_mode(struct s_hardware *hardware)
                        /* Calling with a 0 value will make the cursor move */
                        /* So, let's move the cursor only if needed */
                        if ((strlen(hdt_cli.input) - hdt_cli.cursor_pos) > 0) {
-                               memset(temp_command, 0, sizeof(temp_command));
-                               sprintf(temp_command, "\033[%dC",
-                                       strlen(hdt_cli.input) - hdt_cli.cursor_pos);
                                /* Return to the begining of line */
-                               fputs(temp_command, stdout);
+                               move_cursor_right(strlen(hdt_cli.input) - hdt_cli.cursor_pos);
                                hdt_cli.cursor_pos = strlen(hdt_cli.input);
                        }
                        break;
@@ -766,11 +764,8 @@ void start_cli_mode(struct s_hardware *hardware)
                        /* Calling with a 0 value will make the cursor move */
                        /* So, let's move the cursor only if needed */
                        if (hdt_cli.cursor_pos > 0) {
-                               memset(temp_command, 0, sizeof(temp_command));
-                               sprintf(temp_command, "\033[%dD",
-                                       hdt_cli.cursor_pos);
                                /* Return to the begining of line */
-                               fputs(temp_command, stdout);
+                               move_cursor_left(hdt_cli.cursor_pos);
                                hdt_cli.cursor_pos = 0;
                        }
                        break;
@@ -790,10 +785,10 @@ void start_cli_mode(struct s_hardware *hardware)
                        hdt_cli.history_pos=future_history_pos;
 
                        /* Clear the line */
-                       fputs("\033[2K", stdout);
+                       clear_line();
 
                        /* Move to the begining of line*/
-                       fputs("\033[0G", stdout);
+                       move_cursor_to_column(0);
 
                        reset_prompt();
                        printf("%s",hdt_cli.history[hdt_cli.history_pos]);
@@ -822,10 +817,10 @@ void start_cli_mode(struct s_hardware *hardware)
                        hdt_cli.history_pos=future_history_pos;
 
                        /* Clear the line */
-                       fputs("\033[2K", stdout);
+                       clear_line();
 
                        /* Move to the begining of line*/
-                       fputs("\033[0G", stdout);
+                       move_cursor_to_column(0);
 
                        reset_prompt();
                        printf("%s",hdt_cli.history[hdt_cli.history_pos]);
@@ -835,9 +830,9 @@ void start_cli_mode(struct s_hardware *hardware)
 
                case KEY_TAB:
                        if (autocomplete_backlog) {
-                               /* XXX Will go away */
-                               fputs("\033[2K", stdout);
-                               fputs("\033[0G", stdout);
+                               clear_line();
+                               /* Move to the begining of line*/
+                               move_cursor_to_column(0);
                                reset_prompt();
                                printf("%s",autocomplete_last_seen->autocomplete_token);
                                strncpy(hdt_cli.input,autocomplete_last_seen->autocomplete_token,sizeof(hdt_cli.input));
@@ -877,19 +872,18 @@ void start_cli_mode(struct s_hardware *hardware)
                        hdt_cli.input[strlen(hdt_cli.input) - 1] = '\0';
 
                        /* Get one char back */
-                       fputs("\033[1D", stdout);
+                       move_cursor_left(1);
+
                        /* Clear the end of the line */
-                       fputs("\033[0K", stdout);
+                       clear_end_of_line();
 
                        /* Print the resulting buffer */
                        printf("%s", hdt_cli.input + hdt_cli.cursor_pos - 1);
 
                        /* Realing to the place we were */
-                       memset(temp_command, 0, sizeof(temp_command));
-                       sprintf(temp_command, "\033[%dD",
-                               strlen(hdt_cli.input + hdt_cli.cursor_pos - 1));
-                       fputs(temp_command, stdout);
-                       fputs("\033[1C", stdout);
+                       move_cursor_left(strlen(hdt_cli.input + hdt_cli.cursor_pos - 1));
+                       move_cursor_right(1);
+
                        /* Don't decrement the position unless
                         * if we are at then end of the line*/
                        if (hdt_cli.cursor_pos > (int)strlen(hdt_cli.input))
@@ -925,14 +919,13 @@ void start_cli_mode(struct s_hardware *hardware)
                                         temp_command);
 
                                /* Clear the end of the line */
-                               fputs("\033[0K", stdout);
+                               clear_end_of_line();
 
                                /* Print the resulting buffer */
                                printf("%s", hdt_cli.input + hdt_cli.cursor_pos);
-                               sprintf(temp_command, "\033[%dD",
-                                       trailing_chars);
+
                                /* Return where we must put the new char */
-                               fputs(temp_command, stdout);
+                               move_cursor_left(trailing_chars);
 
                        } else {
                                putchar(current_key);
index 9abb61c..7293094 100644 (file)
@@ -32,8 +32,8 @@
 #include <getkey.h>
 #include "syslinux/config.h"
 #include "../lib/sys/vesa/vesa.h"
-
 #include "hdt-common.h"
+#include "lib-ansi.h"
 
 void detect_parameters(const int argc, const char *argv[],
                        struct s_hardware *hardware)
@@ -389,7 +389,12 @@ const char *find_argument(const char **argv, const char *argument)
 
 void clear_screen(void)
 {
-  fputs("\033e\033%@\033)0\033(B\1#0\033[?25l\033[2J", stdout);
+  move_cursor_to_next_line();
+  disable_utf8();
+  set_g1_special_char();
+  set_us_g0_charset();
+  display_cursor(false);
+  clear_entire_screen();
   display_line_nb = 0;
 }
 
diff --git a/com32/hdt/lib-ansi.c b/com32/hdt/lib-ansi.c
new file mode 100644 (file)
index 0000000..12e5ecc
--- /dev/null
@@ -0,0 +1,97 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2009 Erwan Velu - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * -----------------------------------------------------------------------
+ *  Ansi Sequences can be found here :
+ *  http://ascii-table.com/ansi-escape-sequences-vt-100.php
+ *  http://en.wikipedia.org/wiki/ANSI_escape_code
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+
+void display_cursor(bool status)
+{
+       if (status == true) {
+               fputs("\033[?25h", stdout);
+       } else {
+               fputs("\033[?25l", stdout);
+       }
+}
+
+void clear_end_of_line() {
+       fputs("\033[0K", stdout);
+}
+
+void move_cursor_left(int count) {
+       char buffer[10];
+       memset(buffer,0,sizeof(buffer));
+       sprintf(buffer,"\033[%dD",count);
+       fputs(buffer, stdout);
+}
+
+void move_cursor_right(int count) {
+       char buffer[10];
+       memset(buffer,0,sizeof(buffer));
+       sprintf(buffer,"\033[%dC",count);
+       fputs(buffer, stdout);
+}
+
+void clear_line() {
+       fputs("\033[2K", stdout);
+}
+
+void clear_beginning_of_line() {
+       fputs("\033[1K", stdout);
+}
+
+void move_cursor_to_column(int count) {
+       char buffer[10];
+        memset(buffer,0,sizeof(buffer));
+       sprintf(buffer,"\033[%dG",count);
+       fputs(buffer, stdout);
+}
+
+void move_cursor_to_next_line() {
+       fputs("\033e", stdout);
+}
+
+void disable_utf8() {
+       fputs("\033%@", stdout);
+}
+
+void set_g1_special_char(){
+       fputs("\033)0", stdout);
+}
+
+void set_us_g0_charset() {
+       fputs("\033(B\1#0", stdout);
+}
+
+void clear_entire_screen() {
+       fputs("\033[2J", stdout);
+}
diff --git a/com32/hdt/lib-ansi.h b/com32/hdt/lib-ansi.h
new file mode 100644 (file)
index 0000000..2a17766
--- /dev/null
@@ -0,0 +1,43 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *   Copyright 2009 Erwan Velu - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * -----------------------------------------------------------------------
+ */
+
+#ifndef DEFINE_LIB_ANSI_H
+#define DEFINE_LIB_ANSI_H
+void display_cursor(bool status);
+void clear_end_of_line();
+void move_cursor_left(int count);
+void move_cursor_right(int count);
+void clear_line();
+void clear_beginning_of_line();
+void move_cursor_to_column(int count);
+void move_cursor_to_next_line();
+void disable_utf8();
+void set_g1_special_char();
+void set_us_g0_charset();
+void clear_entire_screen();
+#endif