Annotation of src/usr.bin/fpr/fpr.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: fpr.c,v 1.3 1995/09/01 01:34:16 jtc Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1989, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * This code is derived from software contributed to Berkeley by
! 8: * Robert Corbett.
! 9: *
! 10: * Redistribution and use in source and binary forms, with or without
! 11: * modification, are permitted provided that the following conditions
! 12: * are met:
! 13: * 1. Redistributions of source code must retain the above copyright
! 14: * notice, this list of conditions and the following disclaimer.
! 15: * 2. Redistributions in binary form must reproduce the above copyright
! 16: * notice, this list of conditions and the following disclaimer in the
! 17: * documentation and/or other materials provided with the distribution.
! 18: * 3. All advertising materials mentioning features or use of this software
! 19: * must display the following acknowledgement:
! 20: * This product includes software developed by the University of
! 21: * California, Berkeley and its contributors.
! 22: * 4. Neither the name of the University nor the names of its contributors
! 23: * may be used to endorse or promote products derived from this software
! 24: * without specific prior written permission.
! 25: *
! 26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 36: * SUCH DAMAGE.
! 37: */
! 38:
! 39: #ifndef lint
! 40: static char copyright[] =
! 41: "@(#) Copyright (c) 1989, 1993\n\
! 42: The Regents of the University of California. All rights reserved.\n";
! 43: #endif /* not lint */
! 44:
! 45: #ifndef lint
! 46: #if 0
! 47: static char sccsid[] = "@(#)fpr.c 8.1 (Berkeley) 6/6/93";
! 48: #endif
! 49: static char rcsid[] = "$NetBSD: fpr.c,v 1.3 1995/09/01 01:34:16 jtc Exp $";
! 50: #endif /* not lint */
! 51:
! 52: #include <stdio.h>
! 53:
! 54: #define BLANK ' '
! 55: #define TAB '\t'
! 56: #define NUL '\000'
! 57: #define FF '\f'
! 58: #define BS '\b'
! 59: #define CR '\r'
! 60: #define VTAB '\013'
! 61: #define EOL '\n'
! 62:
! 63: #define TRUE 1
! 64: #define FALSE 0
! 65:
! 66: #define MAXCOL 170
! 67: #define TABSIZE 8
! 68: #define INITWIDTH 8
! 69:
! 70: typedef
! 71: struct column
! 72: {
! 73: int count;
! 74: int width;
! 75: char *str;
! 76: }
! 77: COLUMN;
! 78:
! 79: char cc;
! 80: char saved;
! 81: int length;
! 82: char *text;
! 83: int highcol;
! 84: COLUMN *line;
! 85: int maxpos;
! 86: int maxcol;
! 87:
! 88: extern char *malloc();
! 89: extern char *calloc();
! 90: extern char *realloc();
! 91:
! 92:
! 93:
! 94: main()
! 95: {
! 96: register int ch;
! 97: register char ateof;
! 98: register int i;
! 99: register int errorcount;
! 100:
! 101:
! 102: init();
! 103: errorcount = 0;
! 104: ateof = FALSE;
! 105:
! 106: ch = getchar();
! 107: if (ch == EOF)
! 108: exit(0);
! 109:
! 110: if (ch == EOL)
! 111: {
! 112: cc = NUL;
! 113: ungetc((int) EOL, stdin);
! 114: }
! 115: else if (ch == BLANK)
! 116: cc = NUL;
! 117: else if (ch == '1')
! 118: cc = FF;
! 119: else if (ch == '0')
! 120: cc = EOL;
! 121: else if (ch == '+')
! 122: cc = CR;
! 123: else
! 124: {
! 125: errorcount = 1;
! 126: cc = NUL;
! 127: ungetc(ch, stdin);
! 128: }
! 129:
! 130: while ( ! ateof)
! 131: {
! 132: gettext();
! 133: ch = getchar();
! 134: if (ch == EOF)
! 135: {
! 136: flush();
! 137: ateof = TRUE;
! 138: }
! 139: else if (ch == EOL)
! 140: {
! 141: flush();
! 142: cc = NUL;
! 143: ungetc((int) EOL, stdin);
! 144: }
! 145: else if (ch == BLANK)
! 146: {
! 147: flush();
! 148: cc = NUL;
! 149: }
! 150: else if (ch == '1')
! 151: {
! 152: flush();
! 153: cc = FF;
! 154: }
! 155: else if (ch == '0')
! 156: {
! 157: flush();
! 158: cc = EOL;
! 159: }
! 160: else if (ch == '+')
! 161: {
! 162: for (i = 0; i < length; i++)
! 163: savech(i);
! 164: }
! 165: else
! 166: {
! 167: errorcount++;
! 168: flush();
! 169: cc = NUL;
! 170: ungetc(ch, stdin);
! 171: }
! 172: }
! 173:
! 174: if (errorcount == 1)
! 175: fprintf(stderr, "Illegal carriage control - 1 line.\n");
! 176: else if (errorcount > 1)
! 177: fprintf(stderr, "Illegal carriage control - %d lines.\n", errorcount);
! 178:
! 179: exit(0);
! 180: }
! 181:
! 182:
! 183:
! 184: init()
! 185: {
! 186: register COLUMN *cp;
! 187: register COLUMN *cend;
! 188: register char *sp;
! 189:
! 190:
! 191: length = 0;
! 192: maxpos = MAXCOL;
! 193: sp = malloc((unsigned) maxpos);
! 194: if (sp == NULL)
! 195: nospace();
! 196: text = sp;
! 197:
! 198: highcol = -1;
! 199: maxcol = MAXCOL;
! 200: line = (COLUMN *) calloc(maxcol, (unsigned) sizeof(COLUMN));
! 201: if (line == NULL)
! 202: nospace();
! 203: cp = line;
! 204: cend = line + (maxcol-1);
! 205: while (cp <= cend)
! 206: {
! 207: cp->width = INITWIDTH;
! 208: sp = calloc(INITWIDTH, (unsigned) sizeof(char));
! 209: if (sp == NULL)
! 210: nospace();
! 211: cp->str = sp;
! 212: cp++;
! 213: }
! 214: }
! 215:
! 216:
! 217:
! 218: gettext()
! 219: {
! 220: register int i;
! 221: register char ateol;
! 222: register int ch;
! 223: register int pos;
! 224:
! 225:
! 226: i = 0;
! 227: ateol = FALSE;
! 228:
! 229: while ( ! ateol)
! 230: {
! 231: ch = getchar();
! 232: if (ch == EOL || ch == EOF)
! 233: ateol = TRUE;
! 234: else if (ch == TAB)
! 235: {
! 236: pos = (1 + i/TABSIZE) * TABSIZE;
! 237: if (pos > maxpos)
! 238: {
! 239: maxpos = pos + 10;
! 240: text = realloc(text, (unsigned) maxpos);
! 241: if (text == NULL)
! 242: nospace();
! 243: }
! 244: while (i < pos)
! 245: {
! 246: text[i] = BLANK;
! 247: i++;
! 248: }
! 249: }
! 250: else if (ch == BS)
! 251: {
! 252: if (i > 0)
! 253: {
! 254: i--;
! 255: savech(i);
! 256: }
! 257: }
! 258: else if (ch == CR)
! 259: {
! 260: while (i > 0)
! 261: {
! 262: i--;
! 263: savech(i);
! 264: }
! 265: }
! 266: else if (ch == FF || ch == VTAB)
! 267: {
! 268: flush();
! 269: cc = ch;
! 270: i = 0;
! 271: }
! 272: else
! 273: {
! 274: if (i >= maxpos)
! 275: {
! 276: maxpos = i + 10;
! 277: text = realloc(text, (unsigned) maxpos);
! 278: if (text == NULL)
! 279: nospace();
! 280: }
! 281: text[i] = ch;
! 282: i++;
! 283: }
! 284: }
! 285:
! 286: length = i;
! 287: }
! 288:
! 289:
! 290:
! 291: savech(col)
! 292: int col;
! 293: {
! 294: register char ch;
! 295: register int oldmax;
! 296: register COLUMN *cp;
! 297: register COLUMN *cend;
! 298: register char *sp;
! 299: register int newcount;
! 300:
! 301:
! 302: ch = text[col];
! 303: if (ch == BLANK)
! 304: return;
! 305:
! 306: saved = TRUE;
! 307:
! 308: if (col >= highcol)
! 309: highcol = col;
! 310:
! 311: if (col >= maxcol)
! 312: {
! 313: oldmax = maxcol;
! 314: maxcol = col + 10;
! 315: line = (COLUMN *) realloc(line, (unsigned) maxcol*sizeof(COLUMN));
! 316: if (line == NULL)
! 317: nospace();
! 318: cp = line + oldmax;
! 319: cend = line + (maxcol - 1);
! 320: while (cp <= cend)
! 321: {
! 322: cp->width = INITWIDTH;
! 323: cp->count = 0;
! 324: sp = calloc(INITWIDTH, (unsigned) sizeof(char));
! 325: if (sp == NULL)
! 326: nospace();
! 327: cp->str = sp;
! 328: cp++;
! 329: }
! 330: }
! 331:
! 332: cp = line + col;
! 333: newcount = cp->count + 1;
! 334: if (newcount > cp->width)
! 335: {
! 336: cp->width = newcount;
! 337: sp = realloc(cp->str, (unsigned) newcount*sizeof(char));
! 338: if (sp == NULL)
! 339: nospace();
! 340: cp->str = sp;
! 341: }
! 342: cp->count = newcount;
! 343: cp->str[newcount-1] = ch;
! 344: }
! 345:
! 346:
! 347:
! 348: flush()
! 349: {
! 350: register int i;
! 351: register int anchor;
! 352: register int height;
! 353: register int j;
! 354:
! 355:
! 356: if (cc != NUL)
! 357: putchar(cc);
! 358:
! 359: if ( ! saved)
! 360: {
! 361: i = length;
! 362: while (i > 0 && text[i-1] == BLANK)
! 363: i--;
! 364: length = i;
! 365: for (i = 0; i < length; i++)
! 366: putchar(text[i]);
! 367: putchar(EOL);
! 368: return;
! 369: }
! 370:
! 371: for (i =0; i < length; i++)
! 372: savech(i);
! 373:
! 374: anchor = 0;
! 375: while (anchor <= highcol)
! 376: {
! 377: height = line[anchor].count;
! 378: if (height == 0)
! 379: {
! 380: putchar(BLANK);
! 381: anchor++;
! 382: }
! 383: else if (height == 1)
! 384: {
! 385: putchar( *(line[anchor].str) );
! 386: line[anchor].count = 0;
! 387: anchor++;
! 388: }
! 389: else
! 390: {
! 391: i = anchor;
! 392: while (i < highcol && line[i+1].count > 1)
! 393: i++;
! 394: for (j = anchor; j <= i; j++)
! 395: {
! 396: height = line[j].count - 1;
! 397: putchar(line[j].str[height]);
! 398: line[j].count = height;
! 399: }
! 400: for (j = anchor; j <= i; j++)
! 401: putchar(BS);
! 402: }
! 403: }
! 404:
! 405: putchar(EOL);
! 406: highcol = -1;
! 407: }
! 408:
! 409:
! 410:
! 411: nospace()
! 412: {
! 413: fputs("Storage limit exceeded.\n", stderr);
! 414: exit(1);
! 415: }