File: termstuff.cc

package info (click to toggle)
nget 0.27.1-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,168 kB
  • ctags: 2,941
  • sloc: cpp: 15,311; python: 4,075; ansic: 239; makefile: 206; sh: 184
file content (73 lines) | stat: -rw-r--r-- 2,038 bytes parent folder | download | duplicates (4)
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
/*
    termstuff.* - terminal control functions
    Copyright (C) 2001  Matthew Mueller <donut AT dakotacom.net>

    This program 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.

    This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "termstuff.h"

#include "log.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_WORKING_TERMSTUFF
#include <term.h>
#endif

typedef void voidfunc(void);

//Fake clr_bol function incase we can't use termcap.  Assumes that we will be
//writing another line of near the same length after clearing, thus we don't
//really need to clear the whole line, only the end of it.
void generic_clr_bol(void) {
	printf("\b\b\b\b    ");
}

voidfunc *clr_bol_func = generic_clr_bol;

#ifdef HAVE_WORKING_TERMSTUFF
void tputs_clr_bol(void) {
	if (tputs(clr_bol, 1, putchar)<0) {
		generic_clr_bol();
		PDEBUG(DEBUG_MIN, "tputs_clr_bol: error");
	}
}
#endif

void init_term_stuff(void) {
#ifdef HAVE_WORKING_TERMSTUFF
	char tbuf[1024];
	char *term = getenv("TERM");
	if (!term){
		PDEBUG(DEBUG_MIN, "init_term_stuff: TERM env not set");
		return;
	}
	if (tgetent(tbuf, term) != 1){
		PDEBUG(DEBUG_MIN, "init_term_stuff: tgetent failure");
//		err(2, "tgetent failure");
		return;
	}
	clr_bol_func = tputs_clr_bol;
	PDEBUG(DEBUG_MIN, "init_term_stuff: using tputs_clr_bol");
#else
	PDEBUG(DEBUG_MIN, "init_term_stuff: using generic_clr_bol");
#endif
}

void clear_line_and_return(void) {
	clr_bol_func();
	printf("\r");
}