summaryrefslogtreecommitdiff
path: root/mh/mhl.c
blob: c27e53bde407235eac220fb704df076ec9726562 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2003 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 2, 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */

/* MH mhl command */

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

const char *program_version = "mhl (" PACKAGE_STRING ")";
static char doc[] = N_("GNU MH mhl\v"
"Use -help to obtain the list of traditional MH options.");
static char args_doc[] = N_("[files]");

/* GNU options */
static struct argp_option options[] = {
  {"folder",     ARG_FOLDER,     N_("FOLDER"), 0,
   N_("Specify folder to operate upon")},
  { "bell",      ARG_BELL,       N_("BOOL"), OPTION_ARG_OPTIONAL,
    N_("Ring the bell at the end of each output page") },
  {"nobell",     ARG_NOBELL,     NULL, OPTION_HIDDEN, "" },
  { "clear",     ARG_CLEAR,      N_("BOOL"), OPTION_ARG_OPTIONAL,
    N_("Clear the screen after each page of output")},
  {"noclear",    ARG_NOCLEAR,    NULL, OPTION_HIDDEN, "" },
  {"form",       ARG_FORM,       N_("FILE"), 0,
   N_("Read format from given file")},
  {"width",      ARG_WIDTH,      N_("NUMBER"), 0,
   N_("Set output width")},
  {"length",     ARG_LENGTH,     N_("NUMBER"), 0,
   N_("Set output screen length")},
  {"moreproc",   ARG_MOREPROC,   N_("PROG"), 0,
   N_("Use given PROG instead of the default") },
  {"nomoreproc", ARG_NOMOREPROC, NULL, 0,
   N_("Disable use of moreproc program") },
  {"license", ARG_LICENSE, 0,      0,
   N_("Display software license"), -1},
  { NULL }
};
   
/* Traditional MH options */
struct mh_option mh_option[] = {
  { "bell",       1,  MH_OPT_BOOL },
  { "clear",      1,  MH_OPT_BOOL },
  { "form",       1,  MH_OPT_ARG, "formatfile"},
  { "width",      1,  MH_OPT_ARG, "number"},
  { "length",     1,  MH_OPT_ARG, "number"},
  { "moreproc",   1,  MH_OPT_ARG, "program"},
  { "nomoreproc", 3, },
  { NULL }
};

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 char *moreproc;
static int nomoreproc;

static list_t format;

static int
opt_handler (int key, char *arg, void *unused, struct argp_state *state)
{
  switch (key)
    {
    case ARG_FOLDER:
      current_folder = arg;
      break;

    case ARG_BELL:
      if (is_true (arg))
        mhl_fmt_flags |= MHL_BELL;
      break;
      
    case ARG_NOBELL:
      mhl_fmt_flags &= ~MHL_BELL;
      break;
      
    case ARG_CLEAR:
      if (is_true (arg))
        mhl_fmt_flags |= MHL_CLEARSCREEN;
      break;
      
    case ARG_NOCLEAR:
      mhl_fmt_flags &= ~MHL_CLEARSCREEN;
      break;
      
    case ARG_FORM:
      formfile = arg;
      break;
      
    case ARG_WIDTH:
      width = strtoul (arg, NULL, 0);
      if (!width)
	{
	  argp_error (state, _("Invalid width"));
	  exit (1);
	}
      break;
      
    case ARG_LENGTH:
      length = strtoul (arg, NULL, 0);
      if (!length)
	{
	  argp_error (state, _("Invalid length"));
	  exit (1);
	}
      break;

    case ARG_MOREPROC:
      moreproc = arg;
      break;
      
    case ARG_NOMOREPROC:
      nomoreproc = 1;
      break;
      
    case ARG_LICENSE:
      mh_license (argp_program_version);
      break;

    default:
      return 1;
    }
  return 0;
}

static stream_t
open_output ()
{
  int rc;
  stream_t output;
  
  if (interactive && !moreproc && !nomoreproc)
    moreproc = mh_global_profile_get ("moreproc", getenv ("PAGER"));
  else
    moreproc = NULL;

  if (moreproc)
    rc = prog_stream_create (&output, moreproc, MU_STREAM_WRITE);
  else
    rc = stdio_stream_create (&output, stdout, MU_STREAM_WRITE);

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

  if ((rc = stream_open (output)))
    {
      mh_error (_("cannot open output stream: %s"), mu_strerror (rc));
      exit (1);
    }
  return output;
}

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

  if (!name)
    rc = stdio_stream_create (&input, stdin, MU_STREAM_SEEKABLE);
  else
    rc = file_stream_create (&input, name, MU_STREAM_READ);
  if (rc)
    {
      mh_error (_("cannot create input stream: %s"), mu_strerror (rc));
      return;
    }

  if ((rc = stream_open (input)))
    {
      mh_error (_("cannot open input stream: %s"), mu_strerror (rc));
      stream_destroy (&input, stream_get_owner (input));
      return;
    }

  msg = mh_stream_to_message (input);
  if (!msg)
    {
      mh_error (_("input stream %s is not a message (%s)"),
		name, mu_strerror (rc));
      stream_close (input);
      stream_destroy (&input, stream_get_owner (input));
    }
  else
    {
      mhl_format_run (format, width, length, mhl_fmt_flags, msg, output);
      message_unref (msg);
    }
}

int
main (int argc, char **argv)
{
  int index;
  stream_t output;
  
  interactive = isatty (1) && isatty (0);
  
  mu_init_nls ();
  mu_argp_init (program_version, NULL);
  mh_argp_parse (argc, argv, 0, options, mh_option, args_doc, doc,
		 opt_handler, NULL, &index);

  format = mhl_format_compile (formfile);
  if (!format)
    exit (1);
  
  argc -= index;
  argv += index;

  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);

  stream_close (output);
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.