Subversion Repositories HelenOS-historic

Rev

Rev 607 | Rev 1050 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 607 Rev 631
1
/*
1
/*
2
 * Copyright (C) 2003 Josef Cejka
2
 * Copyright (C) 2003 Josef Cejka
3
 * Copyright (C) 2005 Jakub Jermar
3
 * Copyright (C) 2005 Jakub Jermar
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
#include <console/console.h>
30
#include <console/console.h>
31
#include <console/chardev.h>
31
#include <console/chardev.h>
32
#include <synch/waitq.h>
32
#include <synch/waitq.h>
33
#include <synch/spinlock.h>
33
#include <synch/spinlock.h>
34
#include <arch/types.h>
34
#include <arch/types.h>
35
#include <typedefs.h>
35
#include <typedefs.h>
36
#include <arch.h>
36
#include <arch.h>
37
#include <func.h>
37
#include <func.h>
38
#include <print.h>
38
#include <print.h>
-
 
39
#include <arch/atomic.h>
39
 
40
 
40
/** Standard input character device. */
41
/** Standard input character device. */
41
chardev_t *stdin = NULL;
42
chardev_t *stdin = NULL;
42
chardev_t *stdout = NULL;
43
chardev_t *stdout = NULL;
43
 
44
 
44
/** Get character from character device. Do not echo character.
45
/** Get character from character device. Do not echo character.
45
 *
46
 *
46
 * @param chardev Character device.
47
 * @param chardev Character device.
47
 *
48
 *
48
 * @return Character read.
49
 * @return Character read.
49
 */
50
 */
50
__u8 _getc(chardev_t *chardev)
51
__u8 _getc(chardev_t *chardev)
51
{
52
{
52
    __u8 ch;
53
    __u8 ch;
53
    ipl_t ipl;
54
    ipl_t ipl;
54
 
55
 
55
    if (haltstate) {
56
    if (atomic_get(&haltstate)) {
56
        /* If we are here, we are hopefully on the processor, that
57
        /* If we are here, we are hopefully on the processor, that
57
         * issued the 'halt' command, so proceed to read the character
58
         * issued the 'halt' command, so proceed to read the character
58
         * directly from input
59
         * directly from input
59
         */
60
         */
60
        if (chardev->op->read)
61
        if (chardev->op->read)
61
            return chardev->op->read(chardev);
62
            return chardev->op->read(chardev);
62
        /* no other way of interacting with user, halt */
63
        /* no other way of interacting with user, halt */
-
 
64
        if (CPU)
-
 
65
            printf("cpu%d: ", CPU->id);
-
 
66
        else
-
 
67
            printf("cpu: ");
63
        printf("cpu: halted - no kconsole\n");
68
        printf("halted - no kconsole\n");
64
        cpu_halt();
69
        cpu_halt();
65
    }
70
    }
66
 
71
 
67
    waitq_sleep(&chardev->wq);
72
    waitq_sleep(&chardev->wq);
68
    ipl = interrupts_disable();
73
    ipl = interrupts_disable();
69
    spinlock_lock(&chardev->lock);
74
    spinlock_lock(&chardev->lock);
70
    ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
75
    ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
71
    chardev->counter--;
76
    chardev->counter--;
72
    spinlock_unlock(&chardev->lock);
77
    spinlock_unlock(&chardev->lock);
73
    interrupts_restore(ipl);
78
    interrupts_restore(ipl);
74
 
79
 
75
    chardev->op->resume(chardev);
80
    chardev->op->resume(chardev);
76
 
81
 
77
    return ch;
82
    return ch;
78
}
83
}
79
 
84
 
80
/** Get string from character device.
85
/** Get string from character device.
81
 *
86
 *
82
 * Read characters from character device until first occurrence
87
 * Read characters from character device until first occurrence
83
 * of newline character.
88
 * of newline character.
84
 *
89
 *
85
 * @param chardev Character device.
90
 * @param chardev Character device.
86
 * @param buf Buffer where to store string terminated by '\0'.
91
 * @param buf Buffer where to store string terminated by '\0'.
87
 * @param len Size of the buffer.
92
 * @param len Size of the buffer.
88
 *
93
 *
89
 * @return Number of characters read.
94
 * @return Number of characters read.
90
 */
95
 */
91
count_t gets(chardev_t *chardev, char *buf, size_t buflen)
96
count_t gets(chardev_t *chardev, char *buf, size_t buflen)
92
{
97
{
93
    index_t index = 0;
98
    index_t index = 0;
94
    char ch;
99
    char ch;
95
 
100
 
96
    while (index < buflen) {
101
    while (index < buflen) {
97
        ch = _getc(chardev);
102
        ch = _getc(chardev);
98
        if (ch == '\b') {
103
        if (ch == '\b') {
99
            if (index > 0) {
104
            if (index > 0) {
100
                index--;
105
                index--;
101
                /* Space backspace, space */
106
                /* Space backspace, space */
102
                putchar('\b');
107
                putchar('\b');
103
                putchar(' ');
108
                putchar(' ');
104
                putchar('\b');
109
                putchar('\b');
105
            }
110
            }
106
            continue;
111
            continue;
107
        }
112
        }
108
        putchar(ch);
113
        putchar(ch);
109
 
114
 
110
        if (ch == '\n') { /* end of string => write 0, return */
115
        if (ch == '\n') { /* end of string => write 0, return */
111
            buf[index] = '\0';
116
            buf[index] = '\0';
112
            return (count_t) index;
117
            return (count_t) index;
113
        }
118
        }
114
        buf[index++] = ch;
119
        buf[index++] = ch;
115
    }
120
    }
116
    return (count_t) index;
121
    return (count_t) index;
117
}
122
}
118
 
123
 
119
/** Get character from device & echo it to screen */
124
/** Get character from device & echo it to screen */
120
__u8 getc(chardev_t *chardev)
125
__u8 getc(chardev_t *chardev)
121
{
126
{
122
    __u8 ch;
127
    __u8 ch;
123
 
128
 
124
    ch = _getc(chardev);
129
    ch = _getc(chardev);
125
    putchar(ch);
130
    putchar(ch);
126
    return ch;
131
    return ch;
127
}
132
}
128
 
133
 
129
void putchar(char c)
134
void putchar(char c)
130
{
135
{
131
    if (stdout->op->write)
136
    if (stdout->op->write)
132
        stdout->op->write(stdout, c);
137
        stdout->op->write(stdout, c);
133
}
138
}
134
 
139