diff options
author | Jim Meyering <jim@meyering.net> | 1996-04-29 01:44:33 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1996-04-29 01:44:33 +0000 |
commit | ea11fd8868779815ca6482d7fe0ee9d50d351d8c (patch) | |
tree | 92b9b31f37e96742197712df49e8f1f9f721bce7 /src | |
parent | 346b0368cf4a14587881e37ae3d8295417922eaf (diff) | |
download | coreutils-ea11fd8868779815ca6482d7fe0ee9d50d351d8c.tar.xz |
FIXME
Diffstat (limited to 'src')
-rw-r--r-- | src/asa.c | 333 |
1 files changed, 160 insertions, 173 deletions
@@ -1,23 +1,28 @@ -/* - * 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 +/* +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 */ @@ -34,244 +39,226 @@ /* Structures and unions */ -typedef struct -{ +struct Str + { char *chr; size_t len; -} str; + }; +typedef struct Str 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 */ +char *xmalloc (); +char *xrealloc (); -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 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* -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() +form_feed () { - putchar('\f'); + putchar ('\f'); } static void -new_line() +new_line () { - putchar('\n'); + putchar ('\n'); } static void -add_line(str a) +add_line (str *line) { - if (line_num >= linebuf_size) + if (line_num >= linebuf_size) { - linebuf_size += NUMLINES; - line_buffer = myrealloc(line_buffer, linebuf_size*sizeof(str *)); + linebuf_size += NUMLINES; + line_buffer = (str *) xrealloc (line_buffer, linebuf_size * sizeof (str *)); } - line_buffer[line_num]=a; - line_num++; + line_buffer[line_num] = *line; + line_num++; } static void -flush() +flush () { - size_t i,j; - size_t max_len; + size_t i, j; + size_t max_len; - if (line_num == 0) - return; - if (line_num == 1) + if (line_num == 0) + return; + if (line_num == 1) { - printf("%s\n", line_buffer[0].chr +1); - line_num = 0; - return; + 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); + 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++) + for (i = 1; i <= max_len; i++) { - int printed = 0; + int printed = 0; - for (j=0; j< line_num; j++) + for (j = 0; j < line_num; j++) { - if (line_buffer[j].len > i) + if (line_buffer[j].len > i) { - int ch = line_buffer[j].chr[i]; - - if (ch != ' ') { - if (printed) - putchar('\b'); - putchar(ch); - printed = 1; + int ch = line_buffer[j].chr[i]; + + if (ch != ' ') + { + if (printed) + putchar ('\b'); + putchar (ch); + printed = 1; } } } - if (!printed) - putchar(' '); + if (!printed) + putchar (' '); } - for (j=0; j<line_num; j++) - free(line_buffer[j].chr); + for (j = 0; j < line_num; j++) + free (line_buffer[j].chr); - line_num = 0; - putchar('\n'); + line_num = 0; + putchar ('\n'); } static size_t -readline(FILE *fp, char **ret) +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 *) mymalloc(LINESIZE); - - while(1) + 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 = fgetc (fp); + if (ch == EOF) + break; + if (ch == '\t') { - ch = ' '; - inc = 8 - (len % 8); + ch = ' '; + inc = 8 - (len % 8); } - else - inc = 1; + else + inc = 1; - if (len + inc > bufsize-2) + if (len + inc > bufsize - 2) { - bufsize += LINESIZE; - buffer = myrealloc(buffer, bufsize); + bufsize += LINESIZE; + buffer = xrealloc (buffer, bufsize); } - for (i=0; i<inc; i++) - buffer[len+i] = ch; - len += inc; - if (ch == '\n') - break; + for (i = 0; i < inc; i++) + buffer[len + i] = ch; + len += inc; + if (ch == '\n') + break; } - buffer[len]='\0'; - ret_buff = mymalloc(len+1); - strcpy(ret_buff, buffer); - *ret = ret_buff; - return len; + buffer[len] = '\0'; + ret_buff = xmalloc (len + 1); + strcpy (ret_buff, buffer); + *ret = ret_buff; + return len; } static void -copy_file(FILE *fp) +copy_file (FILE *fp) { - str line; - static first_line = 1; + str line; + static first_line = 1; - while ((line.len=readline(fp, &(line.chr)))) + while ((line.len = readline (fp, &(line.chr)))) { - if (line.chr[line.len-1] == '\n') + if (line.chr[line.len - 1] == '\n') { - line.chr[line.len-1] = '\0'; - line.len--; + line.chr[line.len - 1] = '\0'; + line.len--; } - switch (line.chr[0]) + switch (line.chr[0]) { case '+': - add_line(line); - break; + add_line (&line); + break; case '0': - flush(); - new_line(); - add_line(line); - break; + flush (); + new_line (); + add_line (&line); + break; case '1': - flush(); - if (! first_line) - form_feed(); - add_line(line); - break; + flush (); + if (!first_line) + form_feed (); + add_line (&line); + break; case ' ': - flush(); - add_line(line); - break; + flush (); + add_line (&line); + break; default: - flush(); - add_line(line); - break; + flush (); + add_line (&line); + break; } - first_line = 0; + first_line = 0; } } /* Global functions */ int -main(int argc, char **argv) +main (int argc, char **argv) { - - line_buffer = (str *) mymalloc(NUMLINES*sizeof(str*)); - linebuf_size = NUMLINES; - - if (argc == 1) - copy_file(stdin); - else + 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; + FILE *fp; + char *fname; - while(--argc > 0) + while (--argc > 0) { - fname = *++argv; - if ((fp = fopen(fname,"r")) == NULL) + fname = *++argv; + if ((fp = fopen (fname, "r")) == NULL) + { + err = 1; + error (0, errno, "%s", fname); + } + else { - fprintf(stderr,"Cannot open %s: %s\n",fname,strerror(errno)); - exit(EXIT_FAILURE); + copy_file (fp); + fclose (fp); } - copy_file(fp); - fclose(fp); } } - flush(); - exit(EXIT_SUCCESS); + flush (); + exit (err ? EXIT_FAILURE : EXIT_SUCCESS); } |