Subversion Repositories HelenOS

Rev

Rev 4156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4043 jermar 1
/*
2
 * Copyright (c) 2009 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
4156 trochtova 29
/** @addtogroup genarch
4043 jermar 30
 * @{
31
 */
32
/**
33
 * @file
4156 trochtova 34
 * @brief Serial line processing.
4043 jermar 35
 */
36
 
37
#include <genarch/srln/srln.h>
38
#include <console/chardev.h>
39
#include <console/console.h>
40
#include <proc/thread.h>
41
#include <arch.h>
4296 trochtova 42
#include <string.h>
4043 jermar 43
 
4156 trochtova 44
static indev_t srlnout;
4043 jermar 45
 
4156 trochtova 46
indev_operations_t srlnout_ops = {
47
    .poll = NULL
4043 jermar 48
};
49
 
50
static void ksrln(void *arg)
51
{
4156 trochtova 52
    indev_t *in = (indev_t *) arg;
53
    bool cr = false;
4296 trochtova 54
    uint32_t escape = 0;
4156 trochtova 55
 
56
    while (true) {
4296 trochtova 57
        wchar_t ch = _getc(in);
4043 jermar 58
 
4296 trochtova 59
        /* ANSI escape sequence processing */
60
        if (escape != 0) {
61
            escape <<= 8;
62
            escape |= ch & 0xff;
63
 
64
            if ((escape == 0x1b4f) || (escape == 0x1b5b) || (escape == 0x1b5b33))
65
                continue;
66
 
67
            switch (escape) {
68
            case 0x1b4f46:
69
            case 0x1b5b46:
70
                ch = U_END_ARROW;
71
                escape = 0;
72
                break;
73
            case 0x1b4f48:
74
            case 0x1b5b48:
75
                ch = U_HOME_ARROW;
76
                escape = 0;
77
                break;
78
            case 0x1b5b41:
79
                ch = U_UP_ARROW;
80
                escape = 0;
81
                break;
82
            case 0x1b5b42:
83
                ch = U_DOWN_ARROW;
84
                escape = 0;
85
                break;
86
            case 0x1b5b43:
87
                ch = U_RIGHT_ARROW;
88
                escape = 0;
89
                break;
90
            case 0x1b5b44:
91
                ch = U_LEFT_ARROW;
92
                escape = 0;
93
                break;
94
            case 0x1b5b337e:
95
                ch = U_DELETE;
96
                escape = 0;
97
                break;
98
            default:
99
                escape = 0;
100
            }
101
        }
102
 
103
        if (ch == 0x1b) {
104
            escape = ch & 0xff;
105
            continue;
106
        }
107
 
108
        /* Replace carriage return with line feed
109
           and suppress any following line feed */
4156 trochtova 110
        if ((ch == '\n') && (cr)) {
111
            cr = false;
4043 jermar 112
            continue;
4156 trochtova 113
        }
4043 jermar 114
 
4156 trochtova 115
        if (ch == '\r') {
116
            ch = '\n';
117
            cr = true;
118
        } else
119
            cr = false;
120
 
4296 trochtova 121
        /* Backspace */
4156 trochtova 122
        if (ch == 0x7f)
123
            ch = '\b';
124
 
125
        indev_push_character(stdin, ch);
4043 jermar 126
    }
127
}
128
 
4156 trochtova 129
void srln_init(indev_t *devin)
4043 jermar 130
{
4156 trochtova 131
    indev_initialize("srln", &srlnout, &srlnout_ops);
132
    thread_t *thread
133
        = thread_create(ksrln, devin, TASK, 0, "ksrln", false);
4043 jermar 134
 
4156 trochtova 135
    if (thread) {
136
        stdin = &srlnout;
137
        thread_ready(thread);
138
    }
4043 jermar 139
}
140
 
141
/** @}
4055 trochtova 142
 */