~ubuntu-branches/ubuntu/karmic/bf/karmic

« back to all changes in this revision

Viewing changes to tty_termios.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Beyer
  • Date: 2004-12-19 14:22:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041219142233-zvl1lahqie9b2164
Tags: 20041219
* bf.c: rewrite of ReadProgram() and Interprete() to gain speed,
        removed PutIntoProgram(), added FindMatchingBrackets()
        and removed -d (debug) option; minor changes
* errors.c: changes to get non-GNU-compatibility
* stack.[ch]: removed, because useless
* added examples/quine[123].b, examples/hello.b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* tty_termios.c
 
2
******************************************************************************
 
3
   This file is part of `bf', Yet another Brainfuck interpreter.
 
4
 
 
5
   Author: Stephan Beyer
 
6
 
 
7
   Description of this file:
 
8
       tty handling routines using termios.h
 
9
 
 
10
   Copyright (C) GPL, 2004 Stephan Beyer - s-beyer@gmx.net
 
11
  
 
12
   Permission is hereby granted, free of charge, to any person obtaining a
 
13
   copy of this software and associated documentation files (the "Software"),
 
14
   to deal in the Software without restriction, including without limitation
 
15
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
16
   and/or sell copies of the Software, and to permit persons to whom the
 
17
   Software is furnished to do so, subject to the following conditions:
 
18
 
 
19
   The above copyright notice and this permission notice shall be included in
 
20
   all copies or substantial portions of the Software.
 
21
 
 
22
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
23
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
24
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
25
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 
26
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
27
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
28
 
 
29
******************************************************************************
 
30
*/
 
31
 
 
32
#include <stdio.h> /* getchar */
 
33
#include <termios.h>
 
34
#include <unistd.h>
 
35
 
 
36
#include "tty.h"
 
37
#include "bf.h"
 
38
 
 
39
static struct termios *tty = NULL; /* save tty settings */
 
40
 
 
41
/* restore tty settings */
 
42
void ttyRestore(void)
 
43
{
 
44
        if (tty)
 
45
                tcsetattr(STDIN_FILENO, TCSANOW, tty);
 
46
}
 
47
 
 
48
/* set handlers and init tty according to input mode */
 
49
void ttyInit(void)
 
50
{
 
51
        static struct termios term;
 
52
        if (!tcgetattr(STDIN_FILENO, &term))
 
53
                tty = &term;
 
54
}
 
55
 
 
56
/* ... */
 
57
void ttyFoo(void)
 
58
{
 
59
        static struct termios term;
 
60
        term = *tty; /* copy saved tty */
 
61
        term.c_lflag &= ~ICANON; /* disable eof */
 
62
        term.c_lflag &= ~ICRNL; /* disable CR -> NL */
 
63
        if (opt.inputmode == 1 || opt.inputmode == 3)
 
64
                term.c_lflag &= ~ECHO; /* disable output of input */
 
65
        term.c_cc[VMIN] = 1; /* one character */
 
66
        tcsetattr(STDIN_FILENO, TCSANOW, &term);
 
67
}
 
68
 
 
69
char ttyGetInput(void)
 
70
{
 
71
        char c;
 
72
        if (tty) ttyFoo();
 
73
        c = getchar();
 
74
        ttyRestore();
 
75
        return c;
 
76
}