[2677758] | 1 | /*
|
---|
| 2 | * Copyright (C) 2003 Josef Cejka
|
---|
| 3 | * Copyright (C) 2005 Jakub Jermar
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 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
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <console/console.h>
|
---|
| 31 | #include <console/chardev.h>
|
---|
| 32 | #include <synch/waitq.h>
|
---|
| 33 | #include <synch/spinlock.h>
|
---|
| 34 | #include <arch/types.h>
|
---|
| 35 | #include <typedefs.h>
|
---|
| 36 | #include <arch.h>
|
---|
| 37 |
|
---|
| 38 | /** Standard input character device. */
|
---|
| 39 | chardev_t *stdin = NULL;
|
---|
[973be64e] | 40 | chardev_t *stdout = NULL;
|
---|
[2677758] | 41 |
|
---|
[0c8e692] | 42 | /** Get character from character device. Do not echo character.
|
---|
[e8a9dc3] | 43 | *
|
---|
| 44 | * @param chardev Character device.
|
---|
| 45 | *
|
---|
| 46 | * @return Character read.
|
---|
| 47 | */
|
---|
[0c8e692] | 48 | __u8 _getc(chardev_t *chardev)
|
---|
[e8a9dc3] | 49 | {
|
---|
| 50 | __u8 ch;
|
---|
| 51 | ipl_t ipl;
|
---|
| 52 |
|
---|
| 53 | waitq_sleep(&chardev->wq);
|
---|
| 54 | ipl = interrupts_disable();
|
---|
| 55 | spinlock_lock(&chardev->lock);
|
---|
| 56 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
|
---|
| 57 | chardev->counter--;
|
---|
| 58 | spinlock_unlock(&chardev->lock);
|
---|
| 59 | interrupts_restore(ipl);
|
---|
| 60 |
|
---|
| 61 | chardev->op->resume(chardev);
|
---|
| 62 |
|
---|
| 63 | return ch;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[2677758] | 66 | /** Get string from character device.
|
---|
| 67 | *
|
---|
| 68 | * Read characters from character device until first occurrence
|
---|
| 69 | * of newline character.
|
---|
| 70 | *
|
---|
| 71 | * @param chardev Character device.
|
---|
[ff3b3197] | 72 | * @param buf Buffer where to store string terminated by '\0'.
|
---|
[2677758] | 73 | * @param len Size of the buffer.
|
---|
[ff3b3197] | 74 | *
|
---|
| 75 | * @return Number of characters read.
|
---|
[2677758] | 76 | */
|
---|
[ff3b3197] | 77 | count_t gets(chardev_t *chardev, char *buf, size_t buflen)
|
---|
[2677758] | 78 | {
|
---|
| 79 | index_t index = 0;
|
---|
| 80 | char ch;
|
---|
| 81 |
|
---|
| 82 | while (index < buflen) {
|
---|
[e8a9dc3] | 83 | ch = _getc(chardev);
|
---|
| 84 | if (ch == '\b') {
|
---|
| 85 | if (index > 0) {
|
---|
| 86 | index--;
|
---|
| 87 | /* Space backspace, space */
|
---|
| 88 | putchar('\b');
|
---|
| 89 | putchar(' ');
|
---|
| 90 | putchar('\b');
|
---|
| 91 | }
|
---|
| 92 | continue;
|
---|
| 93 | }
|
---|
| 94 | putchar(ch);
|
---|
| 95 |
|
---|
[2677758] | 96 | if (ch == '\n') { /* end of string => write 0, return */
|
---|
[ff3b3197] | 97 | buf[index] = '\0';
|
---|
| 98 | return (count_t) index;
|
---|
[2677758] | 99 | }
|
---|
[ff3b3197] | 100 | buf[index++] = ch;
|
---|
[2677758] | 101 | }
|
---|
[ff3b3197] | 102 | return (count_t) index;
|
---|
[2677758] | 103 | }
|
---|
| 104 |
|
---|
[e8a9dc3] | 105 | /** Get character from device & echo it to screen */
|
---|
[2677758] | 106 | __u8 getc(chardev_t *chardev)
|
---|
| 107 | {
|
---|
| 108 | __u8 ch;
|
---|
| 109 |
|
---|
[e8a9dc3] | 110 | ch = _getc(chardev);
|
---|
| 111 | putchar(ch);
|
---|
[2677758] | 112 | return ch;
|
---|
| 113 | }
|
---|
[973be64e] | 114 |
|
---|
| 115 | void putchar(char c)
|
---|
| 116 | {
|
---|
| 117 | stdout->op->write(stdout, c);
|
---|
| 118 | }
|
---|