From 346b0368cf4a14587881e37ae3d8295417922eaf Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 29 Apr 1996 01:43:52 +0000 Subject: . --- src/asa.c | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 src/asa.c (limited to 'src/asa.c') diff --git a/src/asa.c b/src/asa.c new file mode 100644 index 000000000..64260f88f --- /dev/null +++ b/src/asa.c @@ -0,0 +1,277 @@ +/* + * asa.c - interpret ASA carriage control characters + * Copyright (C) 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#define _POSIX_SOURCE 1 + +/* System Headers */ + +#include +#include +#include +#include + +/* Macros */ + +#define LINESIZE 135 +#define NUMLINES 5 +#define MAX(a,b) ((a) > (b) ? (a) : (b)) + +/* Structures and unions */ + +typedef struct +{ + char *chr; + size_t len; +} str; + +/* File scope variables */ + +static char rcsid[] = "$Id: asa.c,v 1.1 1996/04/29 01:43:52 meyering Exp $"; +static str *line_buffer = (str *) 0; +static size_t line_num = 0; +static size_t linebuf_size; + +/* Function declarations */ + +static void *mymalloc(size_t); +static size_t readline(FILE *fp, char **a); +static void form_feed(void); +static void add_line(str); +static void flush(void); +static void copy_file(FILE *fp); + +/* Local functions */ + +static void* +mymalloc(size_t n) +{ + void *p; + if ((p=malloc(n))) + return p; + else + { + fprintf(stderr,"Virtual memory exhausted\n"); + exit(EXIT_FAILURE); + } +} + +static void* +myrealloc(void *p, size_t n) +{ + if ((p=realloc(p,n))) + return p; + else + { + fprintf(stderr,"Virtual memory exhausted\n"); + exit(EXIT_FAILURE); + } +} +static void +form_feed() +{ + putchar('\f'); +} + +static void +new_line() +{ + putchar('\n'); +} + +static void +add_line(str a) +{ + if (line_num >= linebuf_size) + { + linebuf_size += NUMLINES; + line_buffer = myrealloc(line_buffer, linebuf_size*sizeof(str *)); + } + line_buffer[line_num]=a; + 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 bufsize-2) + { + bufsize += LINESIZE; + buffer = myrealloc(buffer, bufsize); + } + for (i=0; i 0) + { + fname = *++argv; + if ((fp = fopen(fname,"r")) == NULL) + { + fprintf(stderr,"Cannot open %s: %s\n",fname,strerror(errno)); + exit(EXIT_FAILURE); + } + copy_file(fp); + fclose(fp); + } + } + flush(); + exit(EXIT_SUCCESS); +} -- cgit v1.2.3-54-g00ecf