[go: up one dir, main page]

File: page.c

package info (click to toggle)
cook 2.25-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,816 kB
  • ctags: 4,036
  • sloc: ansic: 50,692; sh: 13,734; makefile: 4,528; yacc: 3,168; perl: 224; awk: 219
file content (190 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 *	cook - file construction tool
 *	Copyright (C) 1997, 1998, 2003 Peter Miller;
 *	All rights reserved.
 *
 *	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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate pages
 */

#include <ac/stdlib.h>
#include <ac/sys/ioctl.h>
#include <ac/termios.h>

#include <arglex.h>
#include <error_intl.h>
#include <help.h>
#include <page.h>


#define MIN_PAGE_WIDTH 40
/* MAX_PAGE_WIDTH is defined in common/page.h */
#define DEFAULT_PAGE_WIDTH 80
#define MIN_PAGE_LENGTH 10
#define MAX_PAGE_LENGTH 30000
#define DEFAULT_PAGE_LENGTH 24

static	int	page_length;
static	int	page_width;


static void default_page_sizes _((void));

static void
default_page_sizes()
{
	if (!page_width)
	{
		char	*cp;

		cp = getenv("COLS");
		if (cp)
		{
			int	n;

			n = atoi(cp);
			if (n > 0)
			{
				if (n < MIN_PAGE_WIDTH)
					n = MIN_PAGE_WIDTH;
				if (n > MAX_PAGE_WIDTH)
					n = MAX_PAGE_WIDTH;
				page_width = n;
			}
		}
	}
	if (!page_length)
	{
		char	*cp;

		cp = getenv("LINES");
		if (cp)
		{
			int	n;

			n = atoi(cp);
			if (n > 0)
			{
				if (n < MIN_PAGE_LENGTH)
					n = MIN_PAGE_LENGTH;
				if (n > MAX_PAGE_LENGTH)
					n = MAX_PAGE_LENGTH;
				page_length = n;
			}
		}
	}

#ifdef TIOCGWINSZ
	if (!page_width || !page_length)
	{
		struct winsize	ws;

		if (ioctl(0, TIOCGWINSZ, &ws) == 0)
		{
			if (!page_width && ws.ws_col > 0)
			{
				page_width = ws.ws_col;
				if (page_width < MIN_PAGE_WIDTH)
					page_width = MIN_PAGE_WIDTH;
				if (page_width > MAX_PAGE_WIDTH)
					page_width = MAX_PAGE_WIDTH;
			}
			if (!page_length && ws.ws_row > 0)
			{
				page_length = ws.ws_row;
				if (page_length < MIN_PAGE_LENGTH)
					page_length = MIN_PAGE_LENGTH;
				if (page_length > MAX_PAGE_LENGTH)
					page_length = MAX_PAGE_LENGTH;
			}
		}
	}
#endif

	if (!page_width)
		page_width = DEFAULT_PAGE_WIDTH;
	if (!page_length)
		page_length = DEFAULT_PAGE_LENGTH;
}


void
page_width_set(n)
	int		n;
{
	sub_context_ty	*scp;

	if (page_width)
	{
		arg_duplicate(arglex_token_page_width, 0);
		/* NOTREACHED */
	}
	if (n < MIN_PAGE_WIDTH || n > MAX_PAGE_WIDTH)
	{
		scp = sub_context_new();
		sub_var_set(scp, "Number", "%d", n);
		fatal_intl(scp, i18n("page width $number out of range"));
		/* NOTREACHED */
	}
	page_width = n;
}


int
page_width_get()
{
	/*
	 * must not generate a fatal error in this function,
	 * as it is used by 'error.c' when reporting fatal errors.
	 *
	 * must not put tracing in this function,
	 * because 'trace.c' uses it to determine the width.
	 */
	if (!page_width)
		default_page_sizes();
	return page_width;
}


void
page_length_set(n)
	int		n;
{
	sub_context_ty	*scp;

	if (page_length)
	{
		arg_duplicate(arglex_token_page_length, 0);
		/* NOTREACHED */
	}
	if (n < MIN_PAGE_LENGTH || n > MAX_PAGE_LENGTH)
	{
		scp = sub_context_new();
		sub_var_set(scp, "Number", "%d", n);
		fatal_intl(scp, i18n("page length $number out of range"));
		/* NOTREACHED */
	}
	page_length = n;
}


int
page_length_get()
{
	if (!page_length)
		default_page_sizes();
	return page_length;
}