7360b892ca7c6051a200798da7179f21590aaffd
[pspp] / src / ui / terminal / terminal.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ui/terminal/terminal.h>
20
21 #include <stdbool.h>
22 #include <stdlib.h>
23
24 #include <libpspp/compiler.h>
25
26 #include "error.h"
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31 static int view_width = -1;
32 static int view_length = -1;
33
34 /* Initializes the terminal interface by determining the size of
35    the user's terminal.  Stores a pointer to the size variables
36    in *VIEW_WIDTH_P and *VIEW_LENGTH_P. */
37 void
38 terminal_init (int **view_width_p, int **view_length_p)
39 {
40   *view_width_p = &view_width;
41   *view_length_p = &view_length;
42   terminal_check_size ();
43 }
44
45 /* Code that interfaces to ncurses.  This must be at the very end
46    of this file because curses.h redefines "bool" on some systems
47    (e.g. OpenBSD), causing declaration mismatches with functions
48    that have parameters or return values of type "bool". */
49 #if LIBNCURSES_USABLE
50 #include <curses.h>
51 #include <term.h>
52 #endif
53
54 /* Determines the size of the terminal, if possible, or at least
55    takes an educated guess. */
56 void
57 terminal_check_size (void)
58 {
59 #if LIBNCURSES_USABLE
60   if (getenv ("TERM") != NULL)
61     {
62       char term_buffer [16384];
63
64       if (tgetent (term_buffer, getenv ("TERM")) > 0)
65         {
66           if (tgetnum ("li") > 0)
67             view_length = tgetnum ("li");
68           if (tgetnum ("co") > 1)
69             view_width = tgetnum ("co") - 1;
70         }
71       else
72         error (0, 0, _("could not access definition for terminal `%s'"),
73                getenv ("TERM"));
74     }
75 #endif
76
77   if (view_width <= 0)
78     {
79       if (getenv ("COLUMNS") != NULL)
80         view_width = atoi (getenv ("COLUMNS"));
81       if (view_width <= 0)
82         view_width = 79;
83     }
84
85   if (view_length <= 0)
86     {
87       if (getenv ("LINES") != NULL)
88         view_length = atoi (getenv ("LINES"));
89       if (view_length <= 0)
90         view_length = 24;
91     }
92 }