summaryrefslogtreecommitdiff
path: root/src/asa.c
blob: 411122c38c8399748f8ac05fa91bdaea5693b2c1 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
TODO
  mark translatable strings
  add usage function
  call parse_long_options
  dcl, set program_name
  do fclose/error checking
  */

/* asa.c - interpret ASA carriage control characters
   Copyright (C) 94, 1996 Thomas Koenig

   This program 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 of the License, or
   (at your option) any later version.

   This program 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 this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* System Headers */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/* Macros */

#define LINESIZE 135
#define NUMLINES 5
#define MAX(a,b) ((a) > (b) ? (a) : (b))

/* Structures and unions */

struct Str
  {
    char *chr;
    size_t len;
  };
typedef struct Str str;

/* File scope variables */

static str *line_buffer = (str *) 0;
static size_t line_num = 0;
static size_t linebuf_size;

/* Function declarations */

char *xmalloc ();
char *xrealloc ();

static size_t readline (FILE *fp, char **a);
static void add_line (str *);
static void flush (void);
static void copy_file (FILE *fp);

/* Local functions */

static void
form_feed ()
{
  putchar ('\f');
}

static void
new_line ()
{
  putchar ('\n');
}

static void
add_line (str *line)
{
  if (line_num >= linebuf_size)
    {
      linebuf_size += NUMLINES;
      line_buffer = (str *) xrealloc (line_buffer, linebuf_size * sizeof (str *));
    }
  line_buffer[line_num] = *line;
  line_num++;
}

static void
flush ()
{
  size_t i, j;
  size_t max_len;

  if (line_num == 0)
    return;
  if (line_num == 1)
    {
      printf ("%s\n", line_buffer[0].chr + 1);
      line_num = 0;
      return;
    }
  max_len = 0;
  for (j = 0; j < line_num; j++)
    max_len = MAX (max_len, line_buffer[j].len);

  for (i = 1; i <= max_len; i++)
    {
      int printed = 0;

      for (j = 0; j < line_num; j++)
	{
	  if (line_buffer[j].len > i)
	    {
	      int ch = line_buffer[j].chr[i];

	      if (ch != ' ')
		{
		  if (printed)
		    putchar ('\b');
		  putchar (ch);
		  printed = 1;
		}
	    }
	}
      if (!printed)
	putchar (' ');
    }
  for (j = 0; j < line_num; j++)
    free (line_buffer[j].chr);

  line_num = 0;
  putchar ('\n');
}

static size_t
readline (FILE *fp, char **ret)
{
  static char *buffer = (char *) 0;
  char *ret_buff;
  static int bufsize = LINESIZE;
  int ch;
  size_t len = 0;
  int inc;
  int i;

  if (buffer == (char *) 0)
    buffer = (char *) xmalloc (LINESIZE);

  while (1)
    {
      ch = fgetc (fp);
      if (ch == EOF)
	break;
      if (ch == '\t')
	{
	  ch = ' ';
	  inc = 8 - (len % 8);
	}
      else
	inc = 1;

      if (len + inc > bufsize - 2)
	{
	  bufsize += LINESIZE;
	  buffer = xrealloc (buffer, bufsize);
	}
      for (i = 0; i < inc; i++)
	buffer[len + i] = ch;
      len += inc;
      if (ch == '\n')
	break;
    }
  buffer[len] = '\0';
  ret_buff = xmalloc (len + 1);
  strcpy (ret_buff, buffer);
  *ret = ret_buff;
  return len;
}

static void
copy_file (FILE *fp)
{
  str line;
  static first_line = 1;

  while ((line.len = readline (fp, &(line.chr))))
    {
      if (line.chr[line.len - 1] == '\n')
	{
	  line.chr[line.len - 1] = '\0';
	  line.len--;
	}

      switch (line.chr[0])
	{
	case '+':
	  add_line (&line);
	  break;

	case '0':
	  flush ();
	  new_line ();
	  add_line (&line);
	  break;

	case '1':
	  flush ();
	  if (!first_line)
	    form_feed ();
	  add_line (&line);
	  break;

	case ' ':
	  flush ();
	  add_line (&line);
	  break;

	default:
	  flush ();
	  add_line (&line);
	  break;
	}
      first_line = 0;
    }
}

/* Global functions */

int
main (int argc, char **argv)
{
  int err;
  line_buffer = (str *) xmalloc (NUMLINES * sizeof (str *));
  linebuf_size = NUMLINES;

  err = 0;
  if (argc == 1)
    copy_file (stdin);
  else
    {
      FILE *fp;
      char *fname;

      while (--argc > 0)
	{
	  fname = *++argv;
	  if ((fp = fopen (fname, "r")) == NULL)
	    {
	      err = 1;
	      error (0, errno, "%s", fname);
	    }
	  else
	    {
	      copy_file (fp);
	      fclose (fp);
	    }
	}
    }
  flush ();
  exit (err ? EXIT_FAILURE : EXIT_SUCCESS);
}