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); ...@@ -61,7 +61,7 @@ int mu_list_get_comparator (mu_list_t _list, mu_list_comparator_t *_pcmp);
61 By default, it is not set. */ 61 By default, it is not set. */
62 typedef mu_deallocator_t mu_list_destroy_item_t; 62 typedef mu_deallocator_t mu_list_destroy_item_t;
63 63
64 /* An often used destroy function. It simply calls free(3) over the 64 /* An often used destroy function. It simply calls free(3) on the
65 _item. */ 65 _item. */
66 void mu_list_free_item (void *_item); 66 void mu_list_free_item (void *_item);
67 67
......
...@@ -20,11 +20,15 @@ ...@@ -20,11 +20,15 @@
20 #include <mh.h> 20 #include <mh.h>
21 21
22 static char prog_doc[] = N_("Check MH format string"); 22 static char prog_doc[] = N_("Check MH format string");
23 static char args_doc[] = N_("[FILE]");
23 24
24 char *format_str; 25 static char *format_str;
25 static mh_format_t format; 26 static mh_format_t format;
26 int dump_option; 27 static int dump_option;
27 int debug_option; 28 static int debug_option;
29 static char *input_file;
30 static size_t width;
31 static size_t msgno;
28 32
29 static struct mu_option options[] = { 33 static struct mu_option options[] = {
30 { "form", 0, N_("FILE"), MU_OPTION_DEFAULT, 34 { "form", 0, N_("FILE"), MU_OPTION_DEFAULT,
...@@ -40,31 +44,63 @@ static struct mu_option options[] = { ...@@ -40,31 +44,63 @@ static struct mu_option options[] = {
40 { "debug", 0, NULL, MU_OPTION_DEFAULT, 44 { "debug", 0, NULL, MU_OPTION_DEFAULT,
41 N_("enable parser debugging output"), 45 N_("enable parser debugging output"),
42 mu_c_bool, &debug_option }, 46 mu_c_bool, &debug_option },
47 { "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
48 N_("set output width"),
49 mu_c_size, &width },
50 { "msgno", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
51 N_("set message number"),
52 mu_c_size, &msgno },
43 53
44 MU_OPTION_END 54 MU_OPTION_END
45 }; 55 };
46 56
47 static int 57 static void
48 action_dump (void) 58 run (void)
49 { 59 {
50 if (!format_str) 60 mu_message_t msg = mh_file_to_message (NULL, input_file);
51 { 61 char *output;
52 mu_error (_("Format string not specified")); 62
53 return 1; 63 mh_format (&format, msg, msgno, width, &output);
54 } 64
55 mh_format_dump (&format); 65 mu_printf ("%s\n", output);
56 return 0;
57 } 66 }
58 67
59 int 68 int
60 main (int argc, char **argv) 69 main (int argc, char **argv)
61 { 70 {
62 mh_getopt (&argc, &argv, options, 0, NULL, prog_doc, NULL); 71 mh_getopt (&argc, &argv, options, 0, args_doc, prog_doc, NULL);
72 switch (argc)
73 {
74 case 0:
75 dump_option = 1;
76 break;
77
78 case 1:
79 input_file = argv[0];
80 break;
81
82 default:
83 mu_error (_("too many arguments"));
84 return 1;
85 }
86
63 mh_format_debug (debug_option); 87 mh_format_debug (debug_option);
64 if (format_str && mh_format_parse (format_str, &format)) 88 if (!format_str)
89 {
90 mu_error (_("Format string not specified"));
91 return 1;
92 }
93 if (mh_format_parse (format_str, &format))
65 { 94 {
66 mu_error (_("Bad format string")); 95 mu_error (_("Bad format string"));
67 exit (1); 96 exit (1);
68 } 97 }
69 return action_dump (); 98
99 if (dump_option)
100 mh_format_dump (&format);
101
102 if (input_file)
103 run ();
104
105 return 0;
70 } 106 }
......
...@@ -28,7 +28,7 @@ static char extra_doc[] = N_("Debug flags are:\n\ ...@@ -28,7 +28,7 @@ static char extra_doc[] = N_("Debug flags are:\n\
28 l - sieve action logs"); 28 l - sieve action logs");
29 29
30 static char *format_str = mh_list_format; 30 static char *format_str = mh_list_format;
31 static int width = 80; 31 static int width;
32 static mu_list_t input_file_list; 32 static mu_list_t input_file_list;
33 static char *audit_file; 33 static char *audit_file;
34 static FILE *audit_fp; 34 static FILE *audit_fp;
......
...@@ -242,6 +242,8 @@ void mh_install (char *name, int automode); ...@@ -242,6 +242,8 @@ void mh_install (char *name, int automode);
242 mu_property_t mh_read_property_file (char *name, int ro); 242 mu_property_t mh_read_property_file (char *name, int ro);
243 void mh_property_merge (mu_property_t dst, mu_property_t src); 243 void mh_property_merge (mu_property_t dst, mu_property_t src);
244 244
245 int mh_width (void);
246
245 #define mh_global_profile_get(name, defval) \ 247 #define mh_global_profile_get(name, defval) \
246 mu_mhprop_get_value (mu_mh_profile, name, defval) 248 mu_mhprop_get_value (mu_mh_profile, name, defval)
247 #define mh_global_profile_set(name, value) \ 249 #define mh_global_profile_set(name, value) \
......
...@@ -487,7 +487,9 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno, ...@@ -487,7 +487,9 @@ mh_format (mh_format_t *fmt, mu_message_t msg, size_t msgno,
487 487
488 mach.message = msg; 488 mach.message = msg;
489 mach.msgno = msgno; 489 mach.msgno = msgno;
490 490
491 if (width == 0)
492 width = mh_width ();
491 mach.width = width - 1; /* Count the newline */ 493 mach.width = width - 1; /* Count the newline */
492 mach.pc = 1; 494 mach.pc = 1;
493 mu_opool_create (&mach.pool, MU_OPOOL_ENOMEMABRT); 495 mu_opool_create (&mach.pool, MU_OPOOL_ENOMEMABRT);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
29 #include <errno.h> 29 #include <errno.h>
30 #include <fcntl.h> 30 #include <fcntl.h>
31 #include <fnmatch.h> 31 #include <fnmatch.h>
32 #include <sys/ioctl.h>
32 33
33 char mh_list_format[] = 34 char mh_list_format[] =
34 "%4(msg)" 35 "%4(msg)"
...@@ -1058,4 +1059,13 @@ mh_safe_make_file_name (const char *dir, const char *file) ...@@ -1058,4 +1059,13 @@ mh_safe_make_file_name (const char *dir, const char *file)
1058 return name; 1059 return name;
1059 } 1060 }
1060 1061
1062 int
1063 mh_width (void)
1064 {
1065 struct winsize ws;
1066 ws.ws_col = ws.ws_row = 0;
1067 if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_col == 0)
1068 return 80; /* FIXME: Should we exit()/abort() if col <= 0 ? */
1069 return ws.ws_col;
1070 }
1061 1071
......
...@@ -843,7 +843,7 @@ mhl_format_run (mu_list_t fmt, ...@@ -843,7 +843,7 @@ mhl_format_run (mu_list_t fmt,
843 env.bvar[B_NEWLINE] = 1; 843 env.bvar[B_NEWLINE] = 1;
844 mu_list_create (&env.printed_fields); 844 mu_list_create (&env.printed_fields);
845 mu_list_set_comparator (env.printed_fields, _comp_name); 845 mu_list_set_comparator (env.printed_fields, _comp_name);
846 env.ivar[I_WIDTH] = width; 846 env.ivar[I_WIDTH] = width ? width : mh_width ();
847 env.ivar[I_LENGTH] = length; 847 env.ivar[I_LENGTH] = length;
848 env.bvar[B_CLEARSCREEN] = flags & MHL_CLEARSCREEN; 848 env.bvar[B_CLEARSCREEN] = flags & MHL_CLEARSCREEN;
849 env.bvar[B_BELL] = flags & MHL_BELL; 849 env.bvar[B_BELL] = flags & MHL_BELL;
......
...@@ -27,7 +27,7 @@ static char args_doc[] = N_("[MESSAGE]"); ...@@ -27,7 +27,7 @@ static char args_doc[] = N_("[MESSAGE]");
27 27
28 static char *format_str = NULL; 28 static char *format_str = NULL;
29 static mh_format_t format; 29 static mh_format_t format;
30 static int width = 80; 30 static int width;
31 31
32 struct mh_whatnow_env wh_env = { 0 }; 32 struct mh_whatnow_env wh_env = { 0 };
33 static int initial_edit = 1; 33 static int initial_edit = 1;
......
...@@ -32,7 +32,7 @@ static char args_doc[] = N_("[MSGLIST]"); ...@@ -32,7 +32,7 @@ static char args_doc[] = N_("[MSGLIST]");
32 static int clear; 32 static int clear;
33 static char *format_str = mh_list_format; 33 static char *format_str = mh_list_format;
34 34
35 static int width = 80; 35 static int width;
36 static int reverse; 36 static int reverse;
37 static int header; 37 static int header;
38 38
......
...@@ -27,6 +27,7 @@ static char args_doc[] = N_("[MSGLIST]"); ...@@ -27,6 +27,7 @@ static char args_doc[] = N_("[MSGLIST]");
27 27
28 static int limit; 28 static int limit;
29 static int verbose; 29 static int verbose;
30 static int width;
30 static mu_mailbox_t mbox; 31 static mu_mailbox_t mbox;
31 static const char *mbox_path; 32 static const char *mbox_path;
32 33
...@@ -133,6 +134,9 @@ set_algo_quicksort (struct mu_parseopt *po, struct mu_option *opt, ...@@ -133,6 +134,9 @@ set_algo_quicksort (struct mu_parseopt *po, struct mu_option *opt,
133 } 134 }
134 135
135 static struct mu_option options[] = { 136 static struct mu_option options[] = {
137 { "width", 0, N_("NUMBER"), MU_OPTION_DEFAULT,
138 N_("set output width (for -list)"),
139 mu_c_int, &width },
136 MU_OPTION_GROUP (N_("Setting sort keys:")), 140 MU_OPTION_GROUP (N_("Setting sort keys:")),
137 { "datefield", 0, N_("STRING"), MU_OPTION_DEFAULT, 141 { "datefield", 0, N_("STRING"), MU_OPTION_DEFAULT,
138 N_("sort on the date field (default `Date:')"), 142 N_("sort on the date field (default `Date:')"),
...@@ -440,7 +444,7 @@ list_message (size_t num) ...@@ -440,7 +444,7 @@ list_message (size_t num)
440 mu_message_t msg = NULL; 444 mu_message_t msg = NULL;
441 char *buffer; 445 char *buffer;
442 mu_mailbox_get_message (mbox, num, &msg); 446 mu_mailbox_get_message (mbox, num, &msg);
443 mh_format (&format, msg, num, 76, &buffer); 447 mh_format (&format, msg, num, width, &buffer);
444 printf ("%s\n", buffer); 448 printf ("%s\n", buffer);
445 free (buffer); 449 free (buffer);
446 } 450 }
......