aboutsummaryrefslogtreecommitdiffstats
path: root/util/ttyio.c
blob: 74d31d1af6cfbfee814db0c211c7170cebbe38ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ttyio.c -  tty i/O functions
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * G10 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include "util.h"
#include "memory.h"
#include "ttyio.h"

static int last_prompt_len;

static FILE *
open_tty(void)
{
    FILE *tty = fopen("/dev/tty", "r");
    if( !tty )
	log_fatal("cannot open /dev/tty: %s\n", strerror(errno) );
    return tty;
}

static void
close_tty( FILE *tty )
{
    fclose(tty);
}



void
tty_printf( const char *fmt, ... )
{
    va_list arg_ptr;

    va_start( arg_ptr, fmt ) ;
    last_prompt_len += vfprintf(stderr,fmt,arg_ptr) ;
    va_end(arg_ptr);
    fflush(stderr);
}


/****************
 * Print a string, but filter all control characters out.
 */
void
tty_print_string( byte *p, size_t n )
{
    for( ; n; n--, p++ )
	if( iscntrl( *p ) ) {
	    putc('\\', stderr);
	    if( *p == '\n' )
		putc('n', stderr);
	    else if( !*p )
		putc('0', stderr);
	    else
		fprintf(stderr, "x%02x", *p );
	}
	else
	    putc(*p, stderr);
}



char *
tty_get( const char *prompt )
{
    char *buf;
    int c, n, i;
    FILE *fp;

    last_prompt_len = 0;
    tty_printf( prompt );
    buf = m_alloc(n=50);
    i = 0;
    fp = open_tty();
    while( (c=getc(fp)) != EOF && c != '\n' ) {
	last_prompt_len++;
	if( c == '\t' )
	    c = ' ';
	else if( iscntrl(c) )
	    continue;
	if( !(i < n-1) ) {
	    n += 50;
	    buf = m_realloc( buf, n );
	}
	buf[i++] = c;
    }
    close_tty(fp);
    buf[i] = 0;
    return buf;
}

char *
tty_get_hidden( const char *prompt )
{
    return tty_get( prompt ); /* fixme */
}


void
tty_kill_prompt()
{
    int i;
#if 0
    for(i=0; i < last_prompt_len; i ++ )
	fputc('\b', stderr);
    for(i=0; i < last_prompt_len; i ++ )
	fputc(' ', stderr);
    for(i=0; i < last_prompt_len; i ++ )
	fputc('\b', stderr);
#endif
    last_prompt_len = 0;
    fflush(stderr);
}