]> git.sur5r.net Git - tio/blobdiff - src/tty.c
Update upstream source from tag 'upstream/2.7'
[tio] / src / tty.c
index 186fa77c9745100a6f0442bd9de5f4b612abcc23..027e7953cbc5a681cc8da7e80383ef307116cc58 100644 (file)
--- a/src/tty.c
+++ b/src/tty.c
@@ -82,6 +82,8 @@
 #define CMSPAR   010000000000
 #endif
 
+#define LINE_SIZE_MAX 1000
+
 #define KEY_0 0x30
 #define KEY_1 0x31
 #define KEY_2 0x32
 #define KEY_T 0x74
 #define KEY_U 0x55
 #define KEY_V 0x76
+#define KEY_X 0x78
+#define KEY_Y 0x79
 #define KEY_Z 0x7a
 
 enum line_mode_t
@@ -133,6 +137,8 @@ bool map_i_nl_cr = false;
 bool map_i_cr_nl = false;
 bool map_ign_cr = false;
 
+char key_hit = 0xff;
+
 static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old;
 static unsigned long rx_total = 0, tx_total = 0;
 static bool connected = false;
@@ -153,6 +159,7 @@ static char *tty_buffer_write_ptr = tty_buffer;
 static pthread_t thread;
 static int pipefd[2];
 static pthread_mutex_t mutex_input_ready = PTHREAD_MUTEX_INITIALIZER;
+static char line[LINE_SIZE_MAX];
 
 static void optional_local_echo(char c)
 {
@@ -299,6 +306,11 @@ void *tty_stdin_input_thread(void *arg)
         byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ);
         if (byte_count < 0)
         {
+            /* No error actually occurred */
+            if (errno == EINTR)
+            {
+                continue;
+            }
             tio_warning_printf("Could not read from stdin (%s)", strerror(errno));
         }
         else if (byte_count == 0)
@@ -316,6 +328,14 @@ void *tty_stdin_input_thread(void *arg)
             // Process quit and flush key command
             for (int i = 0; i<byte_count; i++)
             {
+                // first do key hit check for xmodem abort
+                if (!key_hit) {
+                    key_hit = input_buffer[i];
+                    byte_count--;
+                    memcpy(input_buffer+i, input_buffer+i+1, byte_count-i);
+                    continue;
+                }
+
                 input_char = input_buffer[i];
 
                 if (previous_char == option.prefix_code)
@@ -344,7 +364,7 @@ void *tty_stdin_input_thread(void *arg)
         }
 
         // Write all bytes read to pipe
-        while (byte_count)
+        while (byte_count > 0)
         {
             bytes_written = write(pipefd[1], input_buffer, byte_count);
             if (bytes_written < 0)
@@ -467,6 +487,33 @@ static void toggle_line(const char *line_name, int mask, enum line_mode_t line_m
     }
 }
 
+static int tio_readln(void)
+{
+    char *p = line;
+
+    /* Read line, accept BS and DEL as rubout characters */
+    for (p = line ; p < &line[LINE_SIZE_MAX-1]; )
+    {
+        if (read(pipefd[0], p, 1) > 0)
+        {
+            if (*p == 0x08 || *p == 0x7f)
+            {
+                if (p > line )
+                {
+                    write(STDOUT_FILENO, "\b \b", 3);
+                    p--;
+                }
+                continue;
+            }
+            write(STDOUT_FILENO, p, 1);
+            if (*p == '\r') break;
+            p++;
+        }
+    }
+    *p = 0;
+    return (p - line);
+}
+
 void handle_command_sequence(char input_char, char *output_char, bool *forward)
 {
     char unused_char;
@@ -557,7 +604,9 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
                 tio_printf(" ctrl-%c t       Toggle line timestamp mode", option.prefix_key);
                 tio_printf(" ctrl-%c U       Toggle conversion to uppercase on output", option.prefix_key);
                 tio_printf(" ctrl-%c v       Show version", option.prefix_key);
-                tio_printf(" ctrl-%c ctrl-%c  Send ctrl-%c character", option.prefix_key, option.prefix_key, option.prefix_key);
+                tio_printf(" ctrl-%c x       Send file via Xmodem-1K", option.prefix_key);
+                tio_printf(" ctrl-%c y       Send file via Ymodem", option.prefix_key);
+                tio_printf(" ctrl-%c ctrl-%c Send ctrl-%c character", option.prefix_key, option.prefix_key, option.prefix_key);
                 break;
 
             case KEY_SHIFT_L:
@@ -716,6 +765,17 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
                 tio_printf("tio v%s", VERSION);
                 break;
 
+            case KEY_X:
+            case KEY_Y:
+                tio_printf("Send file with %cMODEM", toupper(input_char));
+                tio_printf_raw("Enter file name: ");
+                if (tio_readln()) {
+                    tio_printf("Sending file '%s'  ", line);
+                    tio_printf("Press any key to abort transfer");
+                    tio_printf("%s", xymodem_send(fd, line, input_char) < 0 ? "Aborted" : "Done");
+                }
+                break;
+
             case KEY_Z:
                 tio_printf_array(random_array);
                 break;