Commit 6878eb80 6878eb8044703a339cb948d94acb0e06102506f7 by Sergey Poznyakoff

mh: determine output width automatically

* mh/fmtcheck.c: Take file name as optional argument.  Run format
on it, if supplied
* mh/mh.h (mh_width): New proto.
* mh/inc.c: Don't initialize width.
* mh/mh_format.c (mh_format): Use mh_width, if width is 0.
* mh/mh_list.c (mhl_format_run): Use mh_width, if width is 0.
* mh/mh_init.c (mh_width): New function.
* mh/repl.c: Likewise.
* mh/scan.c: Likewise.
* mh/sortm.c: Likewise.
New option -width
1 parent a0c5483c
......@@ -61,7 +61,7 @@ int mu_list_get_comparator (mu_list_t _list, mu_list_comparator_t *_pcmp);
By default, it is not set. */
typedef mu_deallocator_t mu_list_destroy_item_t;
/* An often used destroy function. It simply calls free(3) over the
/* An often used destroy function. It simply calls free(3) on the
_item. */
void mu_list_free_item (void *_item);
......
......@@ -20,11 +20,15 @@
#include <mh.h>
static char prog_doc[] = N_("Check MH format string");
static char args_doc[] = N_("[FILE]");
char *format_str;
static char *format_str;
static mh_format_t format;
int dump_option;
int debug_option;
static int dump_option;
static int debug_option;
static char *input_file;
static size_t width;
static size_t msgno;
static struct mu_option options[] = {
{ "form", 0, N_("FILE"), MU_OPTION_DEFAULT,
......@@ -40,31 +44,63 @@ static struct mu_option options[] = {
{ "debug", 0, NULL, MU_OPTION_DEFAULT,
N_("enable parser debugging output"),
mu_c_bool, &debug_option },
{ "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set output width"),
mu_c_size, &width },
{ "msgno", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set message number"),
mu_c_size, &msgno },
MU_OPTION_END
};
static int
action_dump (void)
static void
run (void)
{
if (!format_str)
{
mu_error (_("Format string not specified"));
return 1;
}
mh_format_dump (&format);
return 0;
mu_message_t msg = mh_file_to_message (NULL, input_file);
char *output;
mh_format (&format, msg, msgno, width, &output);
mu_printf ("%s\n", output);
}
int
main (int argc, char **argv)
{
mh_getopt (&argc, &argv, options, 0, NULL, prog_doc, NULL);
mh_getopt (&argc, &argv, options, 0, args_doc, prog_doc, NULL);
switch (argc)
{
case 0:
dump_option = 1;
break;
case 1:
input_file = argv[0];
break;
default:
mu_error (_("too many arguments"));
return 1;
}
mh_format_debug (debug_option);
if (format_str && mh_format_parse (format_str, &format))
if (!format_str)
{
mu_error (_("Format string not specified"));
return 1;
}
if (mh_format_parse (format_str, &format))
{
mu_error (_("Bad format string"));
exit (1);
}
return action_dump ();
if (dump_option)
mh_format_dump (&format);
if (input_file)
run ();
return 0;
}
......
......@@ -28,7 +28,7 @@ static char extra_doc[] = N_("Debug flags are:\n\
l - sieve action logs");
static char *format_str = mh_list_format;
static int width = 80;
static int width;
static mu_list_t input_file_list;
static char *audit_file;
static FILE *audit_fp;
......
......@@ -242,6 +242,8 @@ void mh_install (char *name, int automode);
mu_property_t mh_read_property_file (char *name, int ro);
void mh_property_merge (mu_property_t dst, mu_property_t src);
int mh_width (void);
#define mh_global_profile_get(name, defval) \
mu_mhprop_get_value (mu_mh_profile, name, defval)
#define mh_global_profile_set(name, value) \
......
......@@ -487,7 +487,9 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno,
mach.message = msg;
mach.msgno = msgno;
if (width == 0)
width = mh_width ();
mach.width = width - 1; /* Count the newline */
mach.pc = 1;
mu_opool_create (&mach.pool, MU_OPOOL_ENOMEMABRT);
......
......@@ -29,6 +29,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <sys/ioctl.h>
char mh_list_format[] =
"%4(msg)"
......@@ -1058,4 +1059,13 @@ mh_safe_make_file_name (const char *dir, const char *file)
return name;
}
int
mh_width (void)
{
struct winsize ws;
ws.ws_col = ws.ws_row = 0;
if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_col == 0)
return 80; /* FIXME: Should we exit()/abort() if col <= 0 ? */
return ws.ws_col;
}
......
......@@ -843,7 +843,7 @@ mhl_format_run (mu_list_t fmt,
env.bvar[B_NEWLINE] = 1;
mu_list_create (&env.printed_fields);
mu_list_set_comparator (env.printed_fields, _comp_name);
env.ivar[I_WIDTH] = width;
env.ivar[I_WIDTH] = width ? width : mh_width ();
env.ivar[I_LENGTH] = length;
env.bvar[B_CLEARSCREEN] = flags & MHL_CLEARSCREEN;
env.bvar[B_BELL] = flags & MHL_BELL;
......
......@@ -27,7 +27,7 @@ static char args_doc[] = N_("[MESSAGE]");
static char *format_str = NULL;
static mh_format_t format;
static int width = 80;
static int width;
struct mh_whatnow_env wh_env = { 0 };
static int initial_edit = 1;
......
......@@ -32,7 +32,7 @@ static char args_doc[] = N_("[MSGLIST]");
static int clear;
static char *format_str = mh_list_format;
static int width = 80;
static int width;
static int reverse;
static int header;
......
......@@ -27,6 +27,7 @@ static char args_doc[] = N_("[MSGLIST]");
static int limit;
static int verbose;
static int width;
static mu_mailbox_t mbox;
static const char *mbox_path;
......@@ -133,6 +134,9 @@ set_algo_quicksort (struct mu_parseopt *po, struct mu_option *opt,
}
static struct mu_option options[] = {
{ "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
N_("set output width (for -list)"),
mu_c_int, &width },
MU_OPTION_GROUP (N_("Setting sort keys:")),
{ "datefield", 0, N_("STRING"), MU_OPTION_DEFAULT,
N_("sort on the date field (default `Date:')"),
......@@ -440,7 +444,7 @@ list_message (size_t num)
mu_message_t msg = NULL;
char *buffer;
mu_mailbox_get_message (mbox, num, &msg);
mh_format (&format, msg, num, 76, &buffer);
mh_format (&format, msg, num, width, &buffer);
printf ("%s\n", buffer);
free (buffer);
}
......