File: mhl.c

package info (click to toggle)
mailutils 1%3A3.19-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 38,832 kB
  • sloc: ansic: 186,739; sh: 110,867; yacc: 7,463; cpp: 3,834; makefile: 3,161; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (177 lines) | stat: -rw-r--r-- 4,516 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2003-2025 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

/* MH mhl command */

#include <mh.h>
#include <sys/stat.h>
#include <unistd.h>

static char prog_doc[] = N_("Produce formatted listings of MH messages");
static char args_doc[] = N_("[FILE [FILE...]]");

static int bell_option;
static int clear_option;

static int interactive;  /* Using interactive output */
static int mhl_fmt_flags; /* MHL format flags. Controlled by -bell 
                             and -clear */
static int length = 40;  /* Length of output page */
static int width = 80;   /* Width of output page */
static char *formfile = MHLIBDIR "/mhl.format";
static const char *moreproc;
static int nomoreproc;

static mu_list_t format;

static struct mu_option options[] = {
  { "bell",      0,       NULL, MU_OPTION_DEFAULT,
    N_("ring the bell at the end of each output page"),
    mu_c_bool, &bell_option },
  { "clear",     0,       NULL, MU_OPTION_DEFAULT,
    N_("clear the screen after each page of output"),
    mu_c_bool, &clear_option },
  { "form",      0,       N_("FILE"), MU_OPTION_DEFAULT,
    N_("read format from given file"),
    mu_c_string, &formfile, mh_opt_find_file },
  { "width",     0,       N_("NUMBER"), MU_OPTION_DEFAULT,
    N_("set output width"),
    mu_c_int, &width },
  { "length",    0,       N_("NUMBER"), MU_OPTION_DEFAULT,
    N_("set output screen length"),
    mu_c_int, &length },
  { "moreproc",  0,       N_("PROG"), MU_OPTION_DEFAULT,
    N_("use given PROG instead of the default"),
    mu_c_string, &moreproc },
  { "nomoreproc", 0, NULL, MU_OPTION_DEFAULT,
    N_("disable use of moreproc program"),
    mu_c_bool, &nomoreproc },
  MU_OPTION_END
};
   
static mu_stream_t
open_output ()
{
  int rc;
  mu_stream_t output;

  if (interactive && !nomoreproc)
    {
      if (!moreproc || !moreproc[0])
	moreproc = mh_global_profile_get ("moreproc", getenv ("PAGER"));
    }
  else
    moreproc = NULL;

  if (moreproc)
    rc = mu_command_stream_create (&output, moreproc, MU_STREAM_WRITE);
  else
    {
      rc = 0;
      output = mu_strout;
      mu_stream_ref (output);
    }

  if (rc)
    {
      mu_error (_("cannot create output stream: %s"), mu_strerror (rc));
      exit (1);
    }

  return output;
}

static void
list_message (char *name, mu_stream_t output)
{
  int rc;
  mu_stream_t input;
  mu_message_t msg;

  if (!name)
    {
      rc = 0;
      input = mu_strin;
      mu_stream_ref (input);
    }
  else
    rc = mu_file_stream_create (&input, name, MU_STREAM_READ);
  if (rc)
    {
      mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
      return;
    }

  msg = mh_stream_to_message (input);
  if (!msg)
    {
      mu_error (_("input stream %s is not a message (%s)"),
		name, mu_strerror (rc));
      mu_stream_close (input);
      mu_stream_destroy (&input);
    }
  else
    {
      mhl_format_run (format, width, length, mhl_fmt_flags, msg, output);
      mu_message_unref (msg);
    }
}

int
main (int argc, char **argv)
{
  mu_stream_t output;
  
  interactive = isatty (1) && isatty (0);
  
  mh_getopt (&argc, &argv, options, MH_GETOPT_DEFAULT_FOLDER,
	     args_doc, prog_doc, NULL);  

  if (bell_option == -1)
    /* use default */;
  else if (bell_option)
    mhl_fmt_flags |= MHL_BELL;
  else
    mhl_fmt_flags &= ~MHL_BELL;

  if (clear_option == -1)
    /* use default */;
  else if (clear_option)
    mhl_fmt_flags |= MHL_CLEARSCREEN;
  else
    mhl_fmt_flags &= ~MHL_CLEARSCREEN;
  
  format = mhl_format_compile (formfile);
  if (!format)
    exit (1);
  
  if (argc == 0)
    nomoreproc = 1;

  if (!interactive)
    mhl_fmt_flags &= ~MHL_BELL;
  
  output = open_output ();
  
  if (argc == 0)
    list_message (NULL, output);
  else
    while (argc--)
      list_message (*argv++, output);

  mu_stream_close (output);
  return 0;
}