add 'terminal program' functionality
[platform/kernel/u-boot.git] / common / cmd_terminal.c
1 /*
2  * (C) Copyright 2007 OpenMoko, Inc.
3  * Written by Harald Welte <laforge@openmoko.org>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Boot support
26  */
27 #include <common.h>
28 #include <command.h>
29 #include <devices.h>
30
31 #if defined(CONFIG_CMD_TERMINAL)
32
33 int do_terminal(cmd_tbl_t * cmd, int flag, int argc, char *argv[])
34 {
35         int i, l;
36         int last_tilde = 0;
37         device_t *dev = NULL;
38
39         if (argc < 1)
40                 return -1;
41
42         /* Scan for selected output/input device */
43         for (i = 1; i <= ListNumItems (devlist); i++) {
44                 device_t *tmp = ListGetPtrToItem (devlist, i);
45                 if (!strcmp(tmp->name, argv[1])) {
46                         dev = tmp;
47                         break;
48                 }
49         }
50         if (!dev)
51                 return -1;
52
53         serial_reinit_all();
54         printf("Entering terminal mode for port %s\n", dev->name);
55         puts("Use '~.' to leave the terminal and get back to u-boot\n");
56
57         while (1) {
58                 int c;
59
60                 /* read from console and display on serial port */
61                 if (stdio_devices[0]->tstc()) {
62                         c = stdio_devices[0]->getc();
63                         if (last_tilde == 1) {
64                                 if (c == '.') {
65                                         putc(c);
66                                         putc('\n');
67                                         break;
68                                 } else {
69                                         last_tilde = 0;
70                                         /* write the delayed tilde */
71                                         dev->putc('~');
72                                         /* fall-through to print current
73                                          * character */
74                                 }
75                         }
76                         if (c == '~') {
77                                 last_tilde = 1;
78                                 puts("[u-boot]");
79                                 putc(c);
80                         }
81                         dev->putc(c);
82                 }
83
84                 /* read from serial port and display on console */
85                 if (dev->tstc()) {
86                         c = dev->getc();
87                         putc(c);
88                 }
89         }
90         return 0;
91 }
92
93
94 /***************************************************/
95
96 U_BOOT_CMD(
97         terminal,       3,      1,      do_terminal,
98         "terminal - start terminal emulator\n",
99         ""
100 );
101
102 #endif /* CONFIG_CMD_TERMINAL */