summaryrefslogtreecommitdiff
path: root/tests/t_wordwrap.c
blob: baf0f5bdd2d340b8cd1f4c3913c9110fa412f10a (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
/* This file is part of GDBM test suite.
   Copyright (C) 2021 Free Software Foundation, Inc.

   GDBM 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.

   GDBM 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 GDBM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "autoconf.h"
#include "gdbmapp.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

static void
h_left_margin (WORDWRAP_FILE wf, char *arg)
{
  wordwrap_set_left_margin (wf, atoi (arg));
}
  
static void
h_right_margin (WORDWRAP_FILE wf, char *arg)
{
  wordwrap_set_right_margin (wf, atoi (arg));
}

static void
h_flush (WORDWRAP_FILE wf, char *arg)
{
  wordwrap_flush (wf);
}

static void
h_file (WORDWRAP_FILE wf, char *arg)
{
  FILE *fp;
  char buf[256];
  
  fp = fopen (arg, "r");
  if (!fp)
    {
      perror (arg);
      exit (1);
    }

  while (fgets (buf, sizeof buf, fp))
    {
      wordwrap_write (wf, buf, strlen (buf));
    }
  fclose (fp);
}

static void
h_newline (WORDWRAP_FILE wf, char *arg)
{
  wordwrap_putc (wf, '\n');
}

static void
h_para (WORDWRAP_FILE wf, char *arg)
{
  wordwrap_para (wf);
}


struct wwt_option
{
  char *name;
  int arg;
  void (*handler) (WORDWRAP_FILE, char *);
};

struct wwt_option wwt_options[] = {
  { "left", 1, h_left_margin },
  { "right", 1, h_right_margin },
  { "flush", 0, h_flush },
  { "file", 1, h_file },
  { "newline", 0, h_newline },
  { "para", 0, h_para },
  { NULL }
};

enum
  {
    WWT_ARG,
    WWT_OPT,
    WWT_ERR
  };

int
wwt_getopt (WORDWRAP_FILE wf, char *arg)
{
  if (arg[0] == '-')
    {
      struct wwt_option *opt;
      size_t len;
      
      if (arg[1] == '-' && arg[2] == 0)
	return WWT_ARG;

      arg++;
      len = strcspn (arg, "=");

      for (opt = wwt_options; opt->name; opt++)
	{
	  if (strlen (opt->name) == len && memcmp (opt->name, arg, len) == 0)
	    {
	      if (opt->arg && arg[len])
		arg += len + 1;
	      else if (!opt->arg && !arg[len])
		arg = NULL;
	      else
		continue;
	      opt->handler (wf, arg);
	      return WWT_OPT;
	    }
	}
      return WWT_ERR;
    }
  return WWT_ARG;
}

int
main (int argc, char **argv)
{
  WORDWRAP_FILE wf;
  int escape = 0;

  setlocale (LC_ALL, "");
  if ((wf = wordwrap_fdopen (1)) == NULL)
    {
      perror ("wordwrap_open");
      exit (1);
    }

  while (--argc)
    {
      char *arg = *++argv;

      if (escape)
	{
	  wordwrap_write (wf, arg, strlen (arg));
	  if (argc > 1)
	    wordwrap_write (wf, " ", 1);
	  escape = 0;
	  continue;
	}
      
      switch (wwt_getopt (wf, arg))
	{
	case WWT_ARG:
	  if (strcmp (arg, "--") == 0)
	    escape = 1;
	  else
	    {
	      wordwrap_write (wf, arg, strlen (arg));
	      if (argc > 1)
		wordwrap_write (wf, " ", 1);
	    }
	  break;

	case WWT_OPT:
	  break;

	case WWT_ERR:
	  fprintf (stderr, "unrecognized option: %s\n", arg);
	}
    }

  wordwrap_close (wf);
}