settings: Make viewwidth, viewlength "int"s instead of pointers.
[pspp] / src / ui / terminal / terminal.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2010 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 "data/settings.h"
25 #include "libpspp/compiler.h"
26
27 #include "gl/error.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 /* Code that interfaces to ncurses.  This must be at the very end
33    of this file because curses.h redefines "bool" on some systems
34    (e.g. OpenBSD), causing declaration mismatches with functions
35    that have parameters or return values of type "bool". */
36 #if LIBNCURSES_USABLE
37 #include <curses.h>
38 #include <term.h>
39 #endif
40
41 /* Determines the size of the terminal, if possible, or at least
42    takes an educated guess. */
43 void
44 terminal_check_size (void)
45 {
46   int view_width = 0;
47   int view_length = 0;
48
49 #if LIBNCURSES_USABLE
50   if (getenv ("TERM") != NULL)
51     {
52       char term_buffer [16384];
53
54       if (tgetent (term_buffer, getenv ("TERM")) > 0)
55         {
56           if (tgetnum ("li") > 0)
57             view_length = tgetnum ("li");
58           if (tgetnum ("co") > 1)
59             view_width = tgetnum ("co") - 1;
60         }
61       else
62         error (0, 0, _("could not access definition for terminal `%s'"),
63                getenv ("TERM"));
64     }
65 #endif
66
67   if (view_width <= 0 && getenv ("COLUMNS") != NULL)
68     view_width = atoi (getenv ("COLUMNS"));
69   if (view_width > 0)
70     settings_set_viewwidth (view_width);
71
72   if (view_length <= 0 && getenv ("LINES") != NULL)
73     view_length = atoi (getenv ("LINES"));
74   if (view_length > 0)
75     settings_set_viewlength (view_length);
76 }