summaryrefslogtreecommitdiffabout
path: root/lib/getans.c
blob: 91fae584a284cf21dcb08d0ce816ce0dfe3cb748 (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
/* This file is part of Eclat.
   Copyright (C) 2012-2014 Sergey Poznyakoff.
 
   Eclat 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 3, or (at your option)
   any later version.
 
   Eclat 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 Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <termios.h>
#include <signal.h>
#include <sysexits.h>
#include <errno.h>
#include <string.h>

void
eclat_trimnl(char *s)
{
	size_t len = strlen(s);
	while (len > 0 && s[len-1] == '\n')
		--len;
	s[len] = 0;
}

#ifndef TCSASOFT
# define TCSASOFT 0
#endif
char *
eclat_getans(char *prompt, char *dfl, int pass)
{
	char *buf = NULL;
	size_t size = 0;
	struct termios t, tsave;
	int rc;
	int fd = fileno(stdin);
	sigset_t oldset;
	
	fprintf(stdout, "%s", prompt);
	if (dfl)
		fprintf(stdout, " [%s]", dfl);
	fprintf(stdout, ": ");
	fflush(stdout);

	if (pass) {
		sigset_t set;
		static int signum[] = {
			SIGHUP, SIGINT, SIGQUIT, SIGABRT, SIGPIPE,
			SIGALRM, SIGTERM
		};
		int i;
		
		if (tcgetattr(fd, &t))
			die(EX_SOFTWARE, "cannot query console attributes: %s",
			    strerror(errno));
		tsave = t;
		t.c_lflag &= ~(ECHO | ISIG);
		if (tcsetattr(fd, TCSAFLUSH | TCSASOFT, &t))
			die(EX_SOFTWARE, "cannot turn echo off; %s",
			    strerror(errno));
		sigemptyset(&set);
		for (i = 0; i < sizeof(signum)/sizeof(signum[0]); i++)
			sigaddset(&set, signum[i]);
		sigprocmask(SIG_BLOCK, &set, &oldset);
	}
	rc = grecs_getline(&buf, &size, stdin);
	if (pass) {
		if (tcsetattr(fd, TCSAFLUSH | TCSASOFT, &tsave))
			die(EX_SOFTWARE, "failed to restore echo state: %s",
			    strerror(errno));
		sigprocmask(SIG_SETMASK, &oldset, NULL);
	}
	if (rc <= 0) {
		free(buf);
		return NULL;
	}
	eclat_trimnl(buf);
	if (buf[0] == 0 && dfl) {
		free(buf);
		buf = grecs_strdup(dfl);
	}
	return buf;
}

Return to:

Send suggestions and report system problems to the System administrator.