summaryrefslogtreecommitdiff
path: root/libmailutils/tests/wordwrap.c
blob: d94f48ee83fb29b5b2bd27e0a5106d7fc8f271ee (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2011-2012, 2014-2017 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/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif 
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <mailutils/stream.h>
#include <mailutils/stdstream.h>
#include <mailutils/diag.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>

static void
usage (FILE *fp)
{
  fprintf (fp,
	   "usage: %s [-r N] [-l [+-]N] FILE [[-l [+-]N] FILE...]\n",
	   mu_program_name);
  fprintf (fp, "\n");
  fprintf (fp, "  -l N       set left margin\n");
  fprintf (fp, "  -l +N      move left margin N chars to the right from the current position\n");
  fprintf (fp, "  -l -N      move left margin N chars to the left from the current position\n");
  fprintf (fp, "  -r N       set right margin\n");
  fprintf (fp, "  -h, -?     display this help\n");
}

int
main (int argc, char **argv)
{
  unsigned left = 0, right = 80;
  mu_stream_t str;
  int i;
  
  mu_set_program_name (argv[0]);
 
  for (i = 1; i < argc; i++)
    {
      char *arg = argv[i];
      if (strncmp (arg, "-l", 2) == 0)
	{
	  if (arg[2] == 0)
	    {
	      ++i;
	      left = strtoul (argv[i], NULL, 10);
	    }
	  else
	    {
	      left = strtoul (argv[i] + 2, NULL, 10);
	    }
	}
      else if (strncmp (arg, "-r", 2) == 0)
	{
	  if (arg[2] == 0)
	    {
	      ++i;
	      right = strtoul (argv[i], NULL, 10);
	    }
	  else
	    {
	      right = strtoul (argv[i] + 2, NULL, 10);
	    }
	}
      else if (strcmp (arg, "--") == 0)
	{
	  i++;
	  break;
	}
      else if (arg[0] == '-')
	{
	  if (arg[1] == 0)
	    break;
	  else if ((arg[1] == 'h' || arg[1] == '?') && arg[2] == 0)
	    {
	      usage (stdout);
	      return 0;
	    }
	  else
	    {
	      fprintf (stderr, "%s: unrecognized argument %s\n",
		       mu_program_name, arg);
	      usage (stderr);
	      return 1;
	    }
	}
      else
	break;
    }

  if (i == argc)
    {
      fprintf (stderr, "%s: no files\n", mu_program_name);
      usage (stderr);
      return 1;
    }
  
  mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
  MU_ASSERT (mu_wordwrap_stream_create (&str, mu_strout, left, right));

  for (; i < argc; i++)
    {
      char *arg = argv[i];
      if (strncmp (arg, "-l", 2) == 0)
	{
	  if (arg[2] == 0)
	    {
	      ++i;
	      arg = argv[i];
	    }
	  else
	    arg += 2;

	  if (arg[0] == '+' || arg[0] == '-')
	    {
	      int off = strtol (arg, NULL, 10);
	      MU_ASSERT (mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM,
					  MU_IOCTL_WORDWRAP_MOVE_MARGIN,
					  &off));
	    }
	  else
	    {
	      left = strtoul (arg, NULL, 10);
	      MU_ASSERT (mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM,
					  MU_IOCTL_WORDWRAP_SET_MARGIN,
					  &left));
	    }
	}
      else if (arg[0] == '-' && arg[1] == 0)
	{
	  MU_ASSERT (mu_stream_copy (str, mu_strin, 0, NULL));
	  mu_stream_close (mu_strin);
	}
      else
	{
	  mu_stream_t in;
	  MU_ASSERT (mu_file_stream_create (&in, arg, MU_STREAM_READ));
	  MU_ASSERT (mu_stream_copy (str, in, 0, NULL));
	  mu_stream_destroy (&in);
	}
    }
  mu_stream_destroy (&str);
  return 0;
}
    

  
	    

Return to:

Send suggestions and report system problems to the System administrator.