1 #include "private.h"
2 #include <stdio.h>
3 #include <limits.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <termios.h>
11 
12 #include "tycommon.h"
13 
14 static void
print_usage(const char * argv0)15 print_usage(const char *argv0)
16 {
17    printf("Usage: %s"HELP_ARGUMENT_SHORT" FILE1 [FILE2 ...]\n"
18           "  Send file(s) to the terminal to save\n"
19           HELP_ARGUMENT_DOC"\n"
20           "\n",
21           argv0);
22 }
23 
24 static struct termios told, tnew;
25 
26 static int
echo_off(void)27 echo_off(void)
28 {
29    if (tcgetattr(0, &told) != 0) return -1;
30    tnew = told;
31    tnew.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
32    tnew.c_oflag &= ~(OPOST);
33    tnew.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN);
34    tnew.c_cflag &= ~(CSIZE | PARENB);
35    tnew.c_cflag |= CS8;
36    tnew.c_cc[VMIN] = 1;
37    tnew.c_cc[VTIME] = 0;
38    if (tcsetattr(0, TCSAFLUSH, &tnew) != 0) return -1;
39    return 0;
40 }
41 
42 static int
echo_on(void)43 echo_on(void)
44 {
45    return tcsetattr(0, TCSAFLUSH, &told);
46 }
47 
48 int
main(int argc,char ** argv)49 main(int argc, char **argv)
50 {
51    int i;
52 
53    ARGUMENT_ENTRY_CHECK(argc, argv, print_usage);
54 
55    if (argc <= 1)
56      {
57         print_usage(argv[0]);
58         return 0;
59      }
60 
61    echo_off();
62    for (i = 1; i < argc; i++)
63      {
64 #define BUFSZ 37268
65         char *path, tbuf[PATH_MAX * 3];
66         unsigned char rawbuf[(BUFSZ * 2) + 128], rawbuf2[(BUFSZ * 2) + 128];
67         char buf[4];
68         int file_fd, pksize, pksum, bin, bout;
69 
70         path = argv[i];
71         snprintf(tbuf, sizeof(tbuf), "%c}fr%s", 0x1b, path);
72         if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
73           goto err;
74         file_fd = open(path, O_RDONLY);
75         if (file_fd >= 0)
76           {
77              off_t off;
78 
79              off = lseek(file_fd, 0, SEEK_END);
80              lseek(file_fd, 0, SEEK_SET);
81              snprintf(tbuf, sizeof(tbuf), "%c}fs%llu", 0x1b, (unsigned long long)off);
82              if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
83                goto err;
84              for (;;)
85                {
86                   if (read(0, buf, 2) == 2)
87                     {
88                        if (buf[0] == 'k')
89                          {
90                             pksize = read(file_fd, rawbuf, BUFSZ);
91 
92                             if (pksize > 0)
93                               {
94                                  bout = 0;
95                                  for (bin = 0; bin < pksize; bin++)
96                                    {
97                                       rawbuf2[bout++] = (rawbuf[bin] >> 4 ) + '@';
98                                       rawbuf2[bout++] = (rawbuf[bin] & 0xf) + '@';
99                                    }
100                                  rawbuf2[bout] = 0;
101                                  pksum = 0;
102                                  for (bin = 0; bin < bout; bin++)
103                                    {
104                                       pksum += rawbuf2[bin];
105                                    }
106                                  snprintf(tbuf, sizeof(tbuf), "%c}fd%i ", 0x1b, pksum);
107                                  if (ty_write(1, tbuf, strlen(tbuf)) != (signed)(strlen(tbuf)))
108                                    goto err;
109                                  if (ty_write(1, rawbuf2, bout + 1) != bout + 1)
110                                    goto err;
111                               }
112                             else break;
113                          }
114                        else
115                          {
116                             echo_on();
117                             fprintf(stderr, "Send Fail\n");
118                             goto err;
119                          }
120                     }
121                   else goto err;
122                }
123              close(file_fd);
124           }
125         snprintf(tbuf, sizeof(tbuf), "%c}fx", 0x1b);
126         if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
127           goto err;
128         tbuf[0] = 0;
129         if (ty_write(1, tbuf, 1) != 1)
130           goto err;
131      }
132    echo_on();
133    return 0;
134 err:
135    echo_on();
136    return -1;
137 }
138