diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-10-31 01:52:18 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-10-31 01:52:18 +0100 |
commit | 7c3c92c533b65d4c29f2990915c9c424c3f6629d (patch) | |
tree | d9eb784d9c458c3d91fcf9a9e31e636f39d8a360 /libbb | |
parent | a92a74961d838209f3468d10426bc945ba26070c (diff) | |
download | busybox-w32-7c3c92c533b65d4c29f2990915c9c424c3f6629d.tar.gz busybox-w32-7c3c92c533b65d4c29f2990915c9c424c3f6629d.tar.bz2 busybox-w32-7c3c92c533b65d4c29f2990915c9c424c3f6629d.zip |
man: make width selection more thorough; explain how to override it
Fedora's "man CMD >file" still uses terminal width, not 80 (but disables formatting),
this change mimics that.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 3f9a84ad4..45650edba 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -237,16 +237,27 @@ ssize_t FAST_FUNC full_write2_str(const char *str) | |||
237 | 237 | ||
238 | static int wh_helper(int value, int def_val, const char *env_name, int *err) | 238 | static int wh_helper(int value, int def_val, const char *env_name, int *err) |
239 | { | 239 | { |
240 | if (value == 0) { | 240 | /* Envvars override even if "value" from ioctl is valid (>0). |
241 | char *s = getenv(env_name); | 241 | * Rationale: it's impossible to guess what user wants. |
242 | if (s) { | 242 | * For example: "man CMD | ...": should "man" format output |
243 | value = atoi(s); | 243 | * to stdout's width? stdin's width? /dev/tty's width? 80 chars? |
244 | /* If LINES/COLUMNS are set, pretend that there is | 244 | * We _cant_ know it. If "..." saves text for e.g. email, |
245 | * no error getting w/h, this prevents some ugly | 245 | * then it's probably 80 chars. |
246 | * cursor tricks by our callers */ | 246 | * If "..." is, say, "grep -v DISCARD | $PAGER", then user |
247 | *err = 0; | 247 | * would prefer his tty's width to be used! |
248 | } | 248 | * |
249 | * Since we don't know, at least allow user to do this: | ||
250 | * "COLUMNS=80 man CMD | ..." | ||
251 | */ | ||
252 | char *s = getenv(env_name); | ||
253 | if (s) { | ||
254 | value = atoi(s); | ||
255 | /* If LINES/COLUMNS are set, pretend that there is | ||
256 | * no error getting w/h, this prevents some ugly | ||
257 | * cursor tricks by our callers */ | ||
258 | *err = 0; | ||
249 | } | 259 | } |
260 | |||
250 | if (value <= 1 || value >= 30000) | 261 | if (value <= 1 || value >= 30000) |
251 | value = def_val; | 262 | value = def_val; |
252 | return value; | 263 | return value; |
@@ -258,6 +269,20 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh | |||
258 | { | 269 | { |
259 | struct winsize win; | 270 | struct winsize win; |
260 | int err; | 271 | int err; |
272 | int close_me = -1; | ||
273 | |||
274 | if (fd == -1) { | ||
275 | if (isatty(STDOUT_FILENO)) | ||
276 | fd = STDOUT_FILENO; | ||
277 | else | ||
278 | if (isatty(STDERR_FILENO)) | ||
279 | fd = STDERR_FILENO; | ||
280 | else | ||
281 | if (isatty(STDIN_FILENO)) | ||
282 | fd = STDIN_FILENO; | ||
283 | else | ||
284 | close_me = fd = open("/dev/tty", O_RDONLY); | ||
285 | } | ||
261 | 286 | ||
262 | win.ws_row = 0; | 287 | win.ws_row = 0; |
263 | win.ws_col = 0; | 288 | win.ws_col = 0; |
@@ -268,6 +293,10 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh | |||
268 | *height = wh_helper(win.ws_row, 24, "LINES", &err); | 293 | *height = wh_helper(win.ws_row, 24, "LINES", &err); |
269 | if (width) | 294 | if (width) |
270 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); | 295 | *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); |
296 | |||
297 | if (close_me >= 0) | ||
298 | close(close_me); | ||
299 | |||
271 | return err; | 300 | return err; |
272 | } | 301 | } |
273 | int FAST_FUNC get_terminal_width(int fd) | 302 | int FAST_FUNC get_terminal_width(int fd) |