summaryrefslogtreecommitdiff
path: root/dviware/beebe/src/derun.c
blob: 8e99fb3d63aeff4d0f59d77b49ee19c3fd91f646 (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
/* -*-C-*- derun.c */
/*-->derun*/
/**********************************************************************/
/******************************* derun ********************************/
/**********************************************************************/

/***********************************************************************
Expand run-length encoded Toshiba P1351 plot file to unexpanded form for
checking of run-length coding algorithm.

Usage:
	derun <infile >outfile
***********************************************************************/

#include <stdio.h>

#define ESC '\033'
#define FF '\f'
#define CTLZ '\032'
#define MAXBUF 14*180*4
#define NL '\n'
#define TRUE 1
#define FALSE 0
int buf[MAXBUF+5],kbuf;			/* leave extra space at end of buf[] */
#if    ANSI
void OutBuf(int);
#else
void OutBuf();
#endif

void
main(argc,argv)
int argc;
char* argv[];
{
    int c,n,isgraph;


    kbuf = 0;
    isgraph = FALSE;
    while (((c = getchar()) != EOF) && (kbuf < MAXBUF))
    {
	if (c == FF)			/* <CTL-L> */
	{
	    buf[kbuf++] = c;
	    OutBuf(FALSE);
	}
        else if (c == ESC)		/* <ESC> ... */
	{
	    c = getchar();
	    if (c == 'G')		/* <ESC> G */
	    {
	        buf[kbuf++] = ESC;
	        buf[kbuf++] = c;
		OutBuf(FALSE);
	    }
	    else if (c == CTLZ)		/* <ESC> <CTL-Z> I */
	    {
	        buf[kbuf++] = ESC;
	        buf[kbuf++] = c;
		buf[kbuf++] = getchar();
		OutBuf(FALSE);
	    }
	    else if (c == 'L')		/* <ESC> L n n */
	    {
	        buf[kbuf++] = ESC;
	        buf[kbuf++] = c;
		buf[kbuf++] = getchar();
		buf[kbuf++] = getchar();
		OutBuf(FALSE);
	    }
	    else if (c == 'H')
	    {
	        isgraph = TRUE;
	        n = (getchar() - '@')*256;
		n += (getchar() - '@')*16;
		n += (getchar() - '@');
		n *= 6;
		for ( ; (n > 0) && (kbuf < MAXBUF); --n)
		    buf[kbuf++] = '@';
	    }
	    else if (c == ';')
	    {
	        isgraph = TRUE;
	        n = (getchar() - '0')*1000;
		n += (getchar() - '0')*100;
		n += (getchar() - '0')*10;
		n += (getchar() - '0');
		n *= 4;
		for ( ; (n > 0) && (kbuf < MAXBUF); --n)
		    buf[kbuf++] = getchar();
	    }
	    else
	    {
	        buf[kbuf++] = ESC;
	        buf[kbuf++] = c;
	    }
	}
	else if (c == NL)
	{
	    if ((kbuf & 03) && isgraph)
	    {
	        (void)fprintf(stderr,"%Byte count %d not multiple of 4!\n",
		    kbuf);
	        (void)printf("%Byte count %d not multiple of 4!\n",kbuf);
	    }
	    if (isgraph)
	        (void)printf("\033;%04d",kbuf>>2);
	    OutBuf(TRUE);
	    isgraph = FALSE;
	}
	else
	    buf[kbuf++] = c;
    }
    if (kbuf >= MAXBUF)
        (void)fprintf(stderr,
	    "?Buffer overflow -- buf[%d] needs %d\n",MAXBUF,kbuf);
    if (kbuf > 0)
    {
        (void)fprintf(stderr,"%Last line not terminated by NL\n");
	OutBuf(FALSE);
    }
}

void
OutBuf(putnl)
int putnl;
{
    register int n;

    for (n = 0 ; n < kbuf; ++n)
        putchar((char)buf[n]);

    if (putnl)
        putchar(NL);
    kbuf = 0;
}