1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <plot.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <netinet/in.h>
7 #include <signal.h>
8 
9 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__bsdi__)
10 char	*progname = NULL;
11 #define setprogname(x) progname = x
12 #define getprogname() progname
13 #endif
14 
15 float		deltx;
16 float		delty;
17 
18 static void interrupt(void);
19 static void	fplt(FILE *fin);
20 static int	getsi(register FILE *fin);
21 static void	getstr(register char *s, register FILE *fin, int len);
22 
23 #ifdef __crtplot
24 static void	winresize(void);
25 #endif
26 
27 int
main(int argc,char * argv[])28 main(int argc, char *argv[])
29 {
30 	int		std = 1;
31 	FILE	       *fin;
32 	struct sigaction	act;
33 
34 	setprogname(argv[0]);
35 
36 	memset(&act, 0x00, sizeof(act));
37 	act.sa_handler = (void(*)(int)) interrupt;
38 	sigaction(SIGINT, &act, NULL);
39 
40 #ifdef __crtplot
41 	if (!isatty(fileno(stdout))) {
42 		fprintf(stderr, "%s: output must be a terminal\n", getprogname());
43 		exit(1);
44 	}
45 
46 	memset(&act, 0x00, sizeof(act));
47 	act.sa_handler = (void(*)(int)) winresize;
48 	sigaction(SIGWINCH, &act, NULL);
49 #endif
50 
51 	for (argc--, argv++; argc > 0; argc--, argv++) {
52 		if (argv[0][0] == '-') {
53 			switch (argv[0][1]) {
54 			case 'l':
55 				deltx = atoi(&argv[0][2]) - 1;
56 				break;
57 			case 'w':
58 				delty = atoi(&argv[0][2]) - 1;
59 				break;
60 			}
61 			continue;
62 		}
63 		std = 0;
64 		fin = fopen(argv[0], "r");
65 		if (fin == NULL) {
66 			fprintf(stderr, "%s: can't open %s\n", getprogname(), argv[0]);
67 			exit(1);
68 		}
69 		fplt(fin);
70 		fclose(fin);
71 	}
72 
73 	if (std) {
74 #ifdef __crtplot
75 		if (isatty(fileno(stdin))) {
76 			fprintf(stderr, "%s: input cannot be a terminal\n", getprogname());
77 			exit(1);
78 		}
79 #endif
80 		fplt(stdin);
81 	}
82 
83 	exit(0);
84 }
85 
86 static void
interrupt(void)87 interrupt(void)
88 {
89 	struct sigaction	act;
90 
91 	memset(&act, 0x00, sizeof(act));
92 	act.sa_handler = SIG_IGN;
93 	sigaction(SIGINT, &act, NULL);
94 
95 	pl_closevt();
96 	fprintf(stderr, "%s: terminal interrupted\n", getprogname());
97 	exit(1);
98 }
99 
100 static void
fplt(FILE * fin)101 fplt(FILE *fin)
102 {
103 	register int	c;
104 	char		s[256];
105 	int		xi, yi, x0, y0, x1, y1, r;
106 	int		dx, n, i;
107 	int	       *pat;
108 	unsigned int	pat_len = 256;
109 
110 	pat = malloc(pat_len * sizeof(int));
111 
112 	pl_openpl();
113 	while ((c = getc(fin)) != EOF) {
114 		switch (c) {
115 		case 'm':
116 			xi = getsi(fin);
117 			yi = getsi(fin);
118 			pl_move(xi, yi);
119 			break;
120 		case 'l':
121 			x0 = getsi(fin);
122 			y0 = getsi(fin);
123 			x1 = getsi(fin);
124 			y1 = getsi(fin);
125 			pl_line(x0, y0, x1, y1);
126 			break;
127 		case 't':
128 			getstr(s, fin, 256);
129 			pl_label(s);
130 			break;
131 		case 'e':
132 			pl_erase();
133 			break;
134 		case 'p':
135 			xi = getsi(fin);
136 			yi = getsi(fin);
137 			pl_point(xi, yi);
138 			break;
139 		case 'n':
140 			xi = getsi(fin);
141 			yi = getsi(fin);
142 			pl_cont(xi, yi);
143 			break;
144 		case 's':
145 			x0 = getsi(fin);
146 			y0 = getsi(fin);
147 			x1 = getsi(fin);
148 			y1 = getsi(fin);
149 			pl_space(x0, y0, x1, y1);
150 			break;
151 		case 'a':
152 			xi = getsi(fin);
153 			yi = getsi(fin);
154 			x0 = getsi(fin);
155 			y0 = getsi(fin);
156 			x1 = getsi(fin);
157 			y1 = getsi(fin);
158 			pl_arc(xi, yi, x0, y0, x1, y1);
159 			break;
160 		case 'c':
161 			xi = getsi(fin);
162 			yi = getsi(fin);
163 			r = getsi(fin);
164 			pl_circle(xi, yi, r);
165 			break;
166 		case 'f':
167 			getstr(s, fin, 256);
168 			pl_linemod(s);
169 			break;
170 		case 'd':
171 			xi = getsi(fin);
172 			yi = getsi(fin);
173 			dx = getsi(fin);
174 			n = getsi(fin);
175 			if (n > pat_len) {
176 				pat_len *= 2;
177 				pat = realloc(pat, pat_len * sizeof(int));
178 			}
179 			for (i = 0; i < n; i++)
180 				pat[i] = getsi(fin);
181 
182 			pl_dot(xi, yi, dx, n, pat);
183 			break;
184 		default:
185 			pl_closevt();
186 			fprintf(stderr, "%s: malformed input\n", getprogname());
187 			free(pat);
188 			exit(1);
189 		}
190 	}
191 
192 	pl_closepl();
193 	free(pat);
194 }
195 
196 /* get an integer. */
197 static int
getsi(register FILE * fin)198 getsi(register FILE * fin)
199 {
200 	short		b;
201 
202 	if (fread(&b, sizeof(b), 1, fin) < 1) {
203 		pl_closevt();
204 		fprintf(stderr, "%s: malformed input\n", getprogname());
205 		exit(1);
206 	}
207 	return ntohs(b);
208 }
209 
210 static void
getstr(register char * s,register FILE * fin,int len)211 getstr(register char *s, register FILE * fin, int len)
212 {
213 	if (fgets(s, len, fin) == NULL) {
214 		pl_closevt();
215 		fprintf(stderr, "%s: malformed input\n", getprogname());
216 		exit(1);
217 	}
218 	s[strlen(s) - 1] = '\0';
219 }
220 
221 #ifdef __crtplot
222 static void
winresize(void)223 winresize(void)
224 {
225 	struct sigaction	act;
226 
227 	memset(&act, 0x00, sizeof(act));
228 	act.sa_handler = SIG_IGN;
229 	sigaction(SIGWINCH, &act, NULL);
230 
231 	pl_closevt();
232 	fprintf(stderr, "%s: terminal resized\n", getprogname());
233 	exit(1);
234 }
235 #endif
236