[PATCH 0/5] More fixes to less

2015-07-24 Thread Ron Yorston
This patch series supersedes two of my recent patches to less that
haven't been committed.  It also adds a whole lot more.

The overall bloat introduced by the series is:

function old new   delta
update_num_lines   - 127+127
goto_lineno- 111+111
cap_cur_fline  - 101+101
at_end -  63 +63
buffer_to_line -  56 +56
reinitialize 197 245 +48
buffer_up 35  66 +31
less_main   26062615  +9
goto_match   125 127  +2
status_print 111 109  -2
buffer_down   81  56 -25
read_lines   819 770 -49
getch_nowait 319 264 -55
buffer_line   64   - -64
m_status_print   409 266-143
--
(add/remove: 5/1 grow/shrink: 4/5 up/down: 548/-338)  Total: 210 bytes

Ron
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH 1/5] less: move code to count lines into a separate function

2015-07-24 Thread Ron Yorston
function old new   delta
update_num_lines   - 159+159
m_status_print   409 266-143
--
(add/remove: 1/0 grow/shrink: 0/1 up/down: 159/-143)   Total: 16 bytes

Signed-off-by: Ron Yorston 
---
 miscutils/less.c | 52 
 1 file changed, 28 insertions(+), 24 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index dd932c5..4787f5a 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -609,33 +609,15 @@ static int safe_lineno(int fline)
return LINENO(flines[fline]) + 1;
 }
 
-/* Print a status line if -M was specified */
-static void m_status_print(void)
+/* count number of lines in file */
+static void update_num_lines(void)
 {
-   int first, last;
-   unsigned percent;
-
-   if (less_gets_pos >= 0) /* don't touch statusline while input is done! 
*/
-   return;
-
-   clear_line();
-   printf(HIGHLIGHT"%s", filename);
-   if (num_files > 1)
-   printf(" (file %i of %i)", current_file, num_files);
-
-   first = safe_lineno(cur_fline);
-   last = (option_mask32 & FLAG_S)
-   ? MIN(first + max_displayed_line, max_lineno)
-   : safe_lineno(cur_fline + max_displayed_line);
-   printf(" lines %i-%i", first, last);
+   int count, fd;
+   ssize_t len, i;
+   char buf[4096];
+   struct stat stbuf;
 
if (num_lines == READING_FILE) {
-   int count, fd;
-   ssize_t len, i;
-   char buf[4096];
-   struct stat stbuf;
-
-   /* count number of lines in file */
count = 0;
fd = open(filename, O_RDONLY);
if (fd < 0)
@@ -654,7 +636,29 @@ static void m_status_print(void)
close(fd);
  skip: ;
}
+}
+
+/* Print a status line if -M was specified */
+static void m_status_print(void)
+{
+   int first, last;
+   unsigned percent;
+
+   if (less_gets_pos >= 0) /* don't touch statusline while input is done! 
*/
+   return;
+
+   clear_line();
+   printf(HIGHLIGHT"%s", filename);
+   if (num_files > 1)
+   printf(" (file %i of %i)", current_file, num_files);
+
+   first = safe_lineno(cur_fline);
+   last = (option_mask32 & FLAG_S)
+   ? MIN(first + max_displayed_line, max_lineno)
+   : safe_lineno(cur_fline + max_displayed_line);
+   printf(" lines %i-%i", first, last);
 
+   update_num_lines();
if (num_lines >= 0)
printf("/%i", num_lines);
 
-- 
2.4.3

___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH 2/5] less: rearrange detection of non-regular files

2015-07-24 Thread Ron Yorston
Move the code to detect non-regular files to the point where the
file is being opened.  If num_lines == READING_FILE guarantees
that the file is regular.

Detect when a file becomes unreadable between it first being opened
and the call to update_num_lines.  Mark the file as being non-regular
so we don't try that again.

function old new   delta
reinitialize 197 245 +48
update_num_lines 159 127 -32
--
(add/remove: 0/0 grow/shrink: 1/1 up/down: 48/-32) Total: 16 bytes

Signed-off-by: Ron Yorston 
---
 miscutils/less.c | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index 4787f5a..93d09ee 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -167,7 +167,8 @@ enum { pattern_valid = 0 };
 
 enum {
READING_FILE = -1,
-   READING_STDIN = -2
+   READING_STDIN = -2,
+   READING_NONREG = -3
 };
 
 struct globals {
@@ -615,15 +616,16 @@ static void update_num_lines(void)
int count, fd;
ssize_t len, i;
char buf[4096];
-   struct stat stbuf;
 
+   /* only do this for regular files */
if (num_lines == READING_FILE) {
count = 0;
fd = open(filename, O_RDONLY);
-   if (fd < 0)
-   goto skip;
-   if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode))
-   goto do_close;
+   if (fd < 0) {
+   /* somebody stole my file! */
+   num_lines = READING_NONREG;
+   return;
+   }
while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
for (i = 0; i < len; ++i) {
if (buf[i] == '\n' && ++count == MAXLINES)
@@ -632,9 +634,7 @@ static void update_num_lines(void)
}
  done:
num_lines = count;
- do_close:
close(fd);
- skip: ;
}
 }
 
@@ -943,6 +943,13 @@ static void buffer_line(int linenum)
 static void open_file_and_read_lines(void)
 {
if (filename) {
+#if ENABLE_FEATURE_LESS_FLAGS
+   struct stat stbuf;
+
+   xstat(filename, &stbuf);
+   if (!S_ISREG(stbuf.st_mode))
+   num_lines = READING_NONREG;
+#endif
xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
} else {
/* "less" with no arguments in argv[] */
-- 
2.4.3

___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH 3/5] less: add a function to detect when display is at end of file

2015-07-24 Thread Ron Yorston
Add a function to package the test that detects whether enough has
been read from the file to allow a screenful to be displayed.

Also use this to determine when to display '(END)' in the status
line.  The previous code was incomplete and didn't handle truncated
lines (-S flag) properly.

function old new   delta
at_end -  63 +63
status_print 111 109  -2
read_lines   819 764 -55
getch_nowait 319 264 -55
--
(add/remove: 1/0 grow/shrink: 0/3 up/down: 63/-112)   Total: -49 bytes

Signed-off-by: Ron Yorston 
---
 miscutils/less.c | 25 -
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index 93d09ee..3e37794 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -406,6 +406,14 @@ static void fill_match_lines(unsigned pos);
 #define fill_match_lines(pos) ((void)0)
 #endif
 
+static int at_end(void)
+{
+   return (option_mask32 & FLAG_S) ?
+   !(cur_fline <= max_fline &&
+   max_lineno > LINENO(flines[cur_fline]) + 
max_displayed_line) :
+   !(max_fline > cur_fline + max_displayed_line);
+}
+
 /* Devilishly complex routine.
  *
  * Has to deal with EOF and EPIPE on input,
@@ -552,11 +560,7 @@ static void read_lines(void)
eof_error = 0; /* Pretend we saw EOF */
break;
}
-   if (!(option_mask32 & FLAG_S)
- ? (max_fline > cur_fline + max_displayed_line)
- : (max_fline >= cur_fline
-&& max_lineno > LINENO(flines[cur_fline]) + 
max_displayed_line)
-   ) {
+   if (!at_end()) {
 #if !ENABLE_FEATURE_LESS_REGEXP
break;
 #else
@@ -662,7 +666,7 @@ static void m_status_print(void)
if (num_lines >= 0)
printf("/%i", num_lines);
 
-   if (cur_fline >= (int)(max_fline - max_displayed_line)) {
+   if (at_end()) {
printf(" (END)");
if (num_files > 1 && current_file != num_files)
printf(" - next: %s", files[current_file]);
@@ -692,7 +696,7 @@ static void status_print(void)
 #endif
 
clear_line();
-   if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) {
+   if (cur_fline && !at_end()) {
bb_putchar(':');
return;
}
@@ -1009,12 +1013,7 @@ static int64_t getch_nowait(void)
 */
rd = 1;
/* Are we interested in stdin? */
-//TODO: reuse code for determining this
-   if (!(option_mask32 & FLAG_S)
-  ? !(max_fline > cur_fline + max_displayed_line)
-  : !(max_fline >= cur_fline
-  && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
-   ) {
+   if (at_end()) {
if (eof_error > 0) /* did NOT reach eof yet */
rd = 0; /* yes, we are interested in stdin */
}
-- 
2.4.3

___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH 4/5] less: fix line number confusion

2015-07-24 Thread Ron Yorston
Much of the code refers to lines using indices into the flines
array (which splits lines into portions that fit on the terminal).
In some cases this is wrong and actual line numbers should be
used:

- when lines are being truncated rather than wrapped (-S flag)
- when line numbers have been entered by the user

Also fix a bug in numeric input and improve the display at EOF.

function old new   delta
goto_lineno- 111+111
cap_cur_fline  - 101+101
buffer_to_line -  56 +56
buffer_up 35  66 +31
less_main   26062615  +9
goto_match   125 127  +2
buffer_down   81  56 -25
buffer_line   64   - -64
--
(add/remove: 3/1 grow/shrink: 3/1 up/down: 310/-89)   Total: 221 bytes

Signed-off-by: Ron Yorston 
---
 miscutils/less.c | 121 ++-
 1 file changed, 83 insertions(+), 38 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index 3e37794..f34b817 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -711,23 +711,6 @@ static void status_print(void)
print_hilite(p);
 }
 
-static void cap_cur_fline(int nlines)
-{
-   int diff;
-   if (cur_fline < 0)
-   cur_fline = 0;
-   if (cur_fline + max_displayed_line > max_fline + TILDES) {
-   cur_fline -= nlines;
-   if (cur_fline < 0)
-   cur_fline = 0;
-   diff = max_fline - (cur_fline + max_displayed_line) + TILDES;
-   /* As the number of lines requested was too large, we just move
-* to the end of the file */
-   if (diff > 0)
-   cur_fline += diff;
-   }
-}
-
 static const char controls[] ALIGN1 =
/* NUL: never encountered; TAB: not converted */
/**/"\x01\x02\x03\x04\x05\x06\x07\x08"  "\x0a\x0b\x0c\x0d\x0e\x0f"
@@ -913,37 +896,98 @@ static void buffer_fill_and_print(void)
buffer_print();
 }
 
+/* move cur_fline to a given line number, reading lines if necessary */
+static void goto_lineno(int target)
+{
+   if (target <= 0 ) {
+   cur_fline = 0;
+   }
+   else if (target > LINENO(flines[cur_fline])) {
+ retry:
+   while (LINENO(flines[cur_fline]) != target && cur_fline < 
max_fline)
+   ++cur_fline;
+   /* target not reached but more input is available */
+   if (LINENO(flines[cur_fline]) != target && eof_error > 0) {
+   read_lines();
+   goto retry;
+   }
+   }
+   else {
+   /* search backwards through already-read lines */
+   while (LINENO(flines[cur_fline]) != target && cur_fline > 0)
+   --cur_fline;
+   }
+}
+
+static void cap_cur_fline(void)
+{
+   if ((option_mask32 & FLAG_S)) {
+   if (cur_fline > max_fline)
+   cur_fline = max_fline;
+   if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno 
+ TILDES) {
+   goto_lineno(max_lineno - max_displayed_line + TILDES);
+   read_lines();
+   }
+   }
+   else {
+   if (cur_fline + max_displayed_line > max_fline + TILDES)
+   cur_fline = max_fline - max_displayed_line + TILDES;
+   if (cur_fline < 0)
+   cur_fline = 0;
+   }
+}
+
 /* Move the buffer up and down in the file in order to scroll */
 static void buffer_down(int nlines)
 {
-   cur_fline += nlines;
+   if ((option_mask32 & FLAG_S))
+   goto_lineno(LINENO(flines[cur_fline]) + nlines);
+   else
+   cur_fline += nlines;
read_lines();
-   cap_cur_fline(nlines);
+   cap_cur_fline();
buffer_fill_and_print();
 }
 
 static void buffer_up(int nlines)
 {
-   cur_fline -= nlines;
-   if (cur_fline < 0) cur_fline = 0;
+   if ((option_mask32 & FLAG_S)) {
+   goto_lineno(LINENO(flines[cur_fline]) - nlines);
+   }
+   else {
+   cur_fline -= nlines;
+   if (cur_fline < 0)
+   cur_fline = 0;
+   }
read_lines();
buffer_fill_and_print();
 }
 
-static void buffer_line(int linenum)
+/* display a given line where the argument can be either an index into
+ * the flines array or a line number */
+static void buffer_to_line(int linenum, int is_lineno)
 {
-   if (linenum < 0)
-   linenum = 0;
-   cur_f

[PATCH 5/5] less: allow use of last column of terminal

2015-07-24 Thread Ron Yorston
When read_lines tests whether a character will fit on the current
line it checks the *next* character but in case of overflow doesn't
display the *current* one.  This results in the last column of the
terminal never being used.

The test in re_wrap (used when the terminal width changes or line
numbers are enabled/disabled) is different:  it does allow the use
of the final column.

function old new   delta
read_lines   764 770  +6
--
(add/remove: 0/0 grow/shrink: 1/0 up/down: 6/0) Total: 6 bytes

Signed-off-by: Ron Yorston 
---
 miscutils/less.c | 20 ++--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/miscutils/less.c b/miscutils/less.c
index f34b817..2ed4139 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -512,16 +512,6 @@ static void read_lines(void)
*--p = '\0';
continue;
}
-   {
-   size_t new_last_line_pos = last_line_pos + 1;
-   if (c == '\t') {
-   new_last_line_pos += 7;
-   new_last_line_pos &= (~7);
-   }
-   if ((int)new_last_line_pos >= w)
-   break;
-   last_line_pos = new_last_line_pos;
-   }
/* ok, we will eat this char */
readpos++;
if (c == '\n') {
@@ -533,6 +523,16 @@ static void read_lines(void)
if (c == '\0') c = '\n';
*p++ = c;
*p = '\0';
+   {
+   size_t new_last_line_pos = last_line_pos + 1;
+   if (c == '\t') {
+   new_last_line_pos += 7;
+   new_last_line_pos &= (~7);
+   }
+   if ((int)new_last_line_pos >= w)
+   break;
+   last_line_pos = new_last_line_pos;
+   }
} /* end of "read chars until we have a line" loop */
 #if 0
 //BUG: also triggers on this:
-- 
2.4.3

___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: Setting timezone offset properly with busybox

2015-07-24 Thread Guillermo Rodriguez Garcia
Try JST-9. The offset is "the value added to localtime to arrive at UTC",
which is the opposite than the "intuitive" meaning.

El viernes, 24 de julio de 2015, Juha Lumme  escribió:

> Hi,
>
> I have created root fs with buildroot 2014.1, and it runs busybox version
> 1.22.1.
>
> I can't seem to understand how the timezone is meant to be set.
> NTP has set the system time appropriately to UTC:
> # date
> Fri Jul 24 02:03:16 UTC 2015
>
> In my case I would like to set Japan standard time, so I set the TZ
> variable (or write to /etc/TZ file) to "JST9" (My current interpretation of
>  http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
> ) and check time time
> # export TZ=JST9
> # date
> Thu Jul 23 17:03:24 JST 2015
>
> Now the time zone seems to have been set appropriately, but the time is 6
> hours in the future. The appropriate time should be the original "02:03:16"
> + 9 hours -> "11:03:16". Where do I get this additional offset ?
>
>
> Br,
> Juha
>
>

-- 
Guillermo Rodriguez Garcia
guille.rodrig...@gmail.com
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Re: Setting timezone offset properly with busybox

2015-07-24 Thread Guillermo Rodriguez Garcia
By the way the time you were seeing was not "6 hours in the future", but 9
hours in the past (check the date)

El viernes, 24 de julio de 2015, Guillermo Rodriguez Garcia <
guille.rodrig...@gmail.com> escribió:

> Try JST-9. The offset is "the value added to localtime to arrive at UTC",
> which is the opposite than the "intuitive" meaning.
>
> El viernes, 24 de julio de 2015, Juha Lumme  > escribió:
>
>> Hi,
>>
>> I have created root fs with buildroot 2014.1, and it runs busybox version
>> 1.22.1.
>>
>> I can't seem to understand how the timezone is meant to be set.
>> NTP has set the system time appropriately to UTC:
>> # date
>> Fri Jul 24 02:03:16 UTC 2015
>>
>> In my case I would like to set Japan standard time, so I set the TZ
>> variable (or write to /etc/TZ file) to "JST9" (My current interpretation of
>>  http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
>> ) and check time time
>> # export TZ=JST9
>> # date
>> Thu Jul 23 17:03:24 JST 2015
>>
>> Now the time zone seems to have been set appropriately, but the time is 6
>> hours in the future. The appropriate time should be the original "02:03:16"
>> + 9 hours -> "11:03:16". Where do I get this additional offset ?
>>
>>
>> Br,
>> Juha
>>
>>
>
> --
> Guillermo Rodriguez Garcia
> guille.rodrig...@gmail.com
> 
>


-- 
Guillermo Rodriguez Garcia
guille.rodrig...@gmail.com
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Regression? Cannot remount a loop device from ro to rw

2015-07-24 Thread Carl S. Gutekunst
I recently upgraded BusyBox from 1.18.1 to 1.23.2, and now I find I 
cannot change the read-write mode on a loop mount. That is, if the 
original mount is something like:


mount -o loop,ro /private/myopt.ext2 /opt

and I then remount this this:

mount -o remount,rw /opt

With 1.18.1 the filesystem is remounted as read-write as expected, but 
in 1.23.2 the command has no effect and the filesystem remains read-only.


At first glance, it looks like the problem was introduced in BusyBox 
1.21.0 when the set_loop() function in libbb/loop.c was modified to 
accept the ro argument. But I haven't yet narrowed down exactly what's 
going wrong.


Has anyone else seen this problem? I'm happy to work out a fix, but 
wanted to check with the list first.



___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: Setting timezone offset properly with busybox

2015-07-24 Thread Juha Lumme
Well, this is a good example of pipe vision :) Indeed the date was wrong.
Thanks, JST-9 works properly!

On Fri, Jul 24, 2015 at 11:12 PM, Guillermo Rodriguez Garcia <
guille.rodrig...@gmail.com> wrote:

> By the way the time you were seeing was not "6 hours in the future", but 9
> hours in the past (check the date)
>
> El viernes, 24 de julio de 2015, Guillermo Rodriguez Garcia <
> guille.rodrig...@gmail.com> escribió:
>
>> Try JST-9. The offset is "the value added to localtime to arrive at UTC",
>> which is the opposite than the "intuitive" meaning.
>>
>> El viernes, 24 de julio de 2015, Juha Lumme 
>> escribió:
>>
>>> Hi,
>>>
>>> I have created root fs with buildroot 2014.1, and it runs busybox
>>> version 1.22.1.
>>>
>>> I can't seem to understand how the timezone is meant to be set.
>>> NTP has set the system time appropriately to UTC:
>>> # date
>>> Fri Jul 24 02:03:16 UTC 2015
>>>
>>> In my case I would like to set Japan standard time, so I set the TZ
>>> variable (or write to /etc/TZ file) to "JST9" (My current interpretation of
>>>  http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
>>> ) and check time time
>>> # export TZ=JST9
>>> # date
>>> Thu Jul 23 17:03:24 JST 2015
>>>
>>> Now the time zone seems to have been set appropriately, but the time is
>>> 6 hours in the future. The appropriate time should be the original
>>> "02:03:16" + 9 hours -> "11:03:16". Where do I get this additional offset ?
>>>
>>>
>>> Br,
>>> Juha
>>>
>>>
>>
>> --
>> Guillermo Rodriguez Garcia
>> guille.rodrig...@gmail.com
>>
>
>
> --
> Guillermo Rodriguez Garcia
> guille.rodrig...@gmail.com
>
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox