[RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-30 Thread Jani Nikula
On Sat, 29 Oct 2011 19:30:50 +0200, Daniel Schoepe  
wrote:
> On Fri, 28 Oct 2011 23:59:30 +0300, Jani Nikula  wrote:
> > @@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char 
> > *argv[])
> > fprintf (stderr, "Invalid value for --sort: %s\n", opt);
> > return 1;
> > }
> > +   } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
> > +   const char *p;
> > +   opt = argv[i] + sizeof ("--maxitems=") - 1;
> > +   maxitems = strtoul(opt, , 10);
> 
> p should be of type `char *', not `const char *', as it will be
> modified by strtoul. (Otherwise, gcc will produce a warning about this).

strtoul() won't touch the data pointed to by p (it only modifies p), so
in that sense it could be const, but you're right in that it really
should be 'char *', just for a more complicated reason. Thanks for
making me look it up: http://c-faq.com/ansi/constmismatch.html (not the
best of explanations, perhaps, but gives an idea why the 2nd parameter
of strtoul() can't be 'const char **').

BR,
Jani.


[RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Daniel Schoepe
On Sat, 29 Oct 2011 23:08:04 +0300, Jani Nikula  wrote:
> strtoul() won't touch the data pointed to by p (it only modifies p), so
> in that sense it could be const, but you're right in that it really
> should be 'char *', just for a more complicated reason. Thanks for
> making me look it up: http://c-faq.com/ansi/constmismatch.html (not the
> best of explanations, perhaps, but gives an idea why the 2nd parameter
> of strtoul() can't be 'const char **').

Oh, right, thanks for the clarification. As I often do in situations
such as this, I confused "const char *" and "char const *".
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: 



[RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Daniel Schoepe
On Fri, 28 Oct 2011 23:59:30 +0300, Jani Nikula  wrote:
> @@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char 
> *argv[])
>   fprintf (stderr, "Invalid value for --sort: %s\n", opt);
>   return 1;
>   }
> + } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
> + const char *p;
> + opt = argv[i] + sizeof ("--maxitems=") - 1;
> + maxitems = strtoul(opt, , 10);

p should be of type `char *', not `const char *', as it will be
modified by strtoul. (Otherwise, gcc will produce a warning about this).

Cheers,
Daniel
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: 



[RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Jani Nikula
Add command line parameter --maxitems=N to notmuch search to limit the
number of displayed messages to N.

These two are equal:

$ notmuch search --output=messages --sort=newest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=newest-first SEARCH | head

As are these:

$ notmuch search --output=messages --sort=oldest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=oldest-first SEARCH | tail

Note that N refers to the maximum amount of messages, even for
--output=threads.

Signed-off-by: Jani Nikula 
---
 notmuch-search.c |   10 ++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 6f04d9a..a3a6475 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -394,6 +394,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 const search_format_t *format = _text;
 int i, ret;
 output_t output = OUTPUT_SUMMARY;
+unsigned int maxitems = 0;

 argc--; argv++; /* skip subcommand argument */

@@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
fprintf (stderr, "Invalid value for --sort: %s\n", opt);
return 1;
}
+   } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
+   const char *p;
+   opt = argv[i] + sizeof ("--maxitems=") - 1;
+   maxitems = strtoul(opt, , 10);
+   if (*opt == '\0' || p == opt || *p != '\0') {
+   fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);
+   return 1;
+   }
} else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
opt = argv[i] + sizeof ("--format=") - 1;
if (strcmp (opt, "text") == 0) {
@@ -473,6 +482,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 }

 notmuch_query_set_sort (query, sort);
+notmuch_query_set_maxitems (query, maxitems);

 switch (output) {
 default:
-- 
1.7.5.4



Re: [RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Daniel Schoepe
On Fri, 28 Oct 2011 23:59:30 +0300, Jani Nikula j...@nikula.org wrote:
 @@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char 
 *argv[])
   fprintf (stderr, Invalid value for --sort: %s\n, opt);
   return 1;
   }
 + } else if (STRNCMP_LITERAL (argv[i], --maxitems=) == 0) {
 + const char *p;
 + opt = argv[i] + sizeof (--maxitems=) - 1;
 + maxitems = strtoul(opt, p, 10);

p should be of type `char *', not `const char *', as it will be
modified by strtoul. (Otherwise, gcc will produce a warning about this).

Cheers,
Daniel


pgpjX7J5cST8t.pgp
Description: PGP signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Jani Nikula
On Sat, 29 Oct 2011 19:30:50 +0200, Daniel Schoepe dan...@schoepe.org wrote:
 On Fri, 28 Oct 2011 23:59:30 +0300, Jani Nikula j...@nikula.org wrote:
  @@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char 
  *argv[])
  fprintf (stderr, Invalid value for --sort: %s\n, opt);
  return 1;
  }
  +   } else if (STRNCMP_LITERAL (argv[i], --maxitems=) == 0) {
  +   const char *p;
  +   opt = argv[i] + sizeof (--maxitems=) - 1;
  +   maxitems = strtoul(opt, p, 10);
 
 p should be of type `char *', not `const char *', as it will be
 modified by strtoul. (Otherwise, gcc will produce a warning about this).

strtoul() won't touch the data pointed to by p (it only modifies p), so
in that sense it could be const, but you're right in that it really
should be 'char *', just for a more complicated reason. Thanks for
making me look it up: http://c-faq.com/ansi/constmismatch.html (not the
best of explanations, perhaps, but gives an idea why the 2nd parameter
of strtoul() can't be 'const char **').

BR,
Jani.
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-29 Thread Daniel Schoepe
On Sat, 29 Oct 2011 23:08:04 +0300, Jani Nikula j...@nikula.org wrote:
 strtoul() won't touch the data pointed to by p (it only modifies p), so
 in that sense it could be const, but you're right in that it really
 should be 'char *', just for a more complicated reason. Thanks for
 making me look it up: http://c-faq.com/ansi/constmismatch.html (not the
 best of explanations, perhaps, but gives an idea why the 2nd parameter
 of strtoul() can't be 'const char **').

Oh, right, thanks for the clarification. As I often do in situations
such as this, I confused const char * and char const *.


pgpKRtOed6SyQ.pgp
Description: PGP signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[RFC PATCH 2/3] cli: add support for limiting the number of search results

2011-10-28 Thread Jani Nikula
Add command line parameter --maxitems=N to notmuch search to limit the
number of displayed messages to N.

These two are equal:

$ notmuch search --output=messages --sort=newest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=newest-first SEARCH | head

As are these:

$ notmuch search --output=messages --sort=oldest-first --maxitems=10 SEARCH
$ notmuch search --output=messages --sort=oldest-first SEARCH | tail

Note that N refers to the maximum amount of messages, even for
--output=threads.

Signed-off-by: Jani Nikula j...@nikula.org
---
 notmuch-search.c |   10 ++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 6f04d9a..a3a6475 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -394,6 +394,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 const search_format_t *format = format_text;
 int i, ret;
 output_t output = OUTPUT_SUMMARY;
+unsigned int maxitems = 0;
 
 argc--; argv++; /* skip subcommand argument */
 
@@ -412,6 +413,14 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
fprintf (stderr, Invalid value for --sort: %s\n, opt);
return 1;
}
+   } else if (STRNCMP_LITERAL (argv[i], --maxitems=) == 0) {
+   const char *p;
+   opt = argv[i] + sizeof (--maxitems=) - 1;
+   maxitems = strtoul(opt, p, 10);
+   if (*opt == '\0' || p == opt || *p != '\0') {
+   fprintf (stderr, Invalid value for --maxitems: %s\n, opt);
+   return 1;
+   }
} else if (STRNCMP_LITERAL (argv[i], --format=) == 0) {
opt = argv[i] + sizeof (--format=) - 1;
if (strcmp (opt, text) == 0) {
@@ -473,6 +482,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 }
 
 notmuch_query_set_sort (query, sort);
+notmuch_query_set_maxitems (query, maxitems);
 
 switch (output) {
 default:
-- 
1.7.5.4

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch