1 /* display.c */
2 /*****************************************************************************
3 					�ԣ�������У���������أԣ�
4 
5 				�Υǥ����ץ쥤���ϡ��ͣ��塤�ţ���������
6 *****************************************************************************/
7 
8 #include "xtr.h"
9 
10 static int displayflag = OFF;
11 
12 /************************** Disp **************************************/
13 
14 #define PL(n)			((n) == 1 ? ' ' : 's')
15 #if  __human68k__
16 #define CONDRV_OFF(fp) fprintf((fp), "\033" "0")
17 #define CONDRV_ON(fp)  fprintf((fp), "\033" "1")
18 #else
19 #define CONDRV_OFF(fp)
20 #define CONDRV_ON(fp)
21 #endif
22 
23 #define FNAME2LENMAX  38			/* ���ĤΥե�����̾��Ĺ���ι�פκ����� */
24 
25 #define NARROWS  (sizeof(arrowv)/sizeof(arrowv[0]))
26 
27 /* ��������Υ��������ץ������� */
28 #if MSDOS
29  #define E_CLRLN	"\033[K"		/* �����ޤǥ��ꥢ */
30  #define E_MORE		"\033[;7m"		/* "-- More --" ��°�� */
31  #define E_GUIDE	"\033[35m"		/* �Ȥ���ɽ����°�� */
32  #define E_PLAIN	"\033[m"		/* °�����ꥢ */
33 #else
34 #if __human68k__
35  #define E_CLRLN	"\033[K"		/* �����ޤǥ��ꥢ */
36  #define E_MORE		"\033[;7m"		/* "-- More --" ��°�� */
37  #define E_GUIDE	"\033[31m"		/* �Ȥ���ɽ����°�� */
38  #define E_PLAIN	"\033[m"		/* °�����ꥢ */
39 #else
40  #define E_CLRLN	""
41  #define E_MORE		""
42  #define E_GUIDE	""
43  #define E_PLAIN	""
44 #endif
45 #endif
46 
47 void
Disp(int endflag)48 Disp(int endflag)
49 /* �¹Էв�ɽ�� */
50 {
51 	static uchar *arrowv[] = {
52 							"-xtr->",
53 							"xt-r>-",
54 							"xtr>--",
55 							"-->xtr",
56 							"->x-tr",
57 							">-xtr>"
58 							};
59 
60 	static int arrowc = 0;
61 	static uchar *previfilename = NULL;
62 	static uchar *prevofilename = NULL;
63 	static long fsize = 0;
64 	static uchar *ifile = NULL;
65 	static uchar *ofile = NULL;
66 
67 	long fbytes;
68 	int fpar;
69 	long ofbytes;
70 	struct stat st;
71 
72 	unsigned ilines = ilineno;
73 	unsigned olines = lineno;
74 
75 	if (conoutmode || coninmode || silentmode || tmpincludeflag)
76 		return;
77 
78 	if (ifilename != previfilename || ofilename != prevofilename) {
79 		/* ���ϥե����뤫���ϥե����뤬�����Ѥ�äƤ����� */
80 
81 		ifile = ifilename;
82 		ofile = ofilename;
83 
84 		if (strlen((char *)ifile) + strlen((char *)ofile) > FNAME2LENMAX) {
85 			/* �ե�����̾��Ĺ���ι�פ�FNAME2LENMAX���ۤ��ʤ��褦�� */
86 			ifile = FNameName(ifile);
87 			if (strlen((char *)ifile) + strlen((char *)ofile) > FNAME2LENMAX) {
88 				ofile = FNameName(ofile);
89 			}
90 		}
91 
92 		arrowc = 0;
93 
94 		/* ���ϥե�����Υ�������Ĵ�٤� */
95 
96 		if (fstat(fileno(ifp), &st) == -1)
97 			return;
98 
99 		fsize = st.st_size;
100 
101 #if 0
102 		if (displayflag) {
103 			fprintf(stderr, "\n");	/* ���Υե�����ηв�ɽ��������в��� */
104 		}
105 #endif
106 	}
107 
108 	assert(ifile != NULL);
109 	assert(ofile != NULL);
110 
111 	if (fsize == 0)
112 		return;
113 
114 	if (endflag) {
115 		/* �ե����뤬����äƤ����褿�ʤ� */
116 		fbytes = fsize;
117 		fpar = 100;
118 		arrowc = 0;
119 		--ilines;		/* ������Ȥ������Ƥ�ʬ���᤹ */
120 		--olines;
121 	} else if ((fbytes = ftell(ifp)) != -1L)
122 		fpar = (int)((fbytes * 100) / fsize);
123 	else
124 		return;						/* ftell �ǥ��顼 */
125 
126 	CONDRV_OFF(stderr);
127 	if (binarymode) {
128 		if (prnoutmode || (ofbytes = ftell(ofp)) == -1L) {
129 			fprintf(stderr, "\r%s %5ld Byte%c (%3d%%) %s %s" E_CLRLN "\r",
130 				ifile, fbytes, PL(fbytes),
131 				fpar, arrowv[arrowc], ofile);
132 		} else {
133 			fprintf(stderr,
134 				"\r%s %5ld Byte%c (%3d%%) %s %s %5ld Byte%c" E_CLRLN "\r",
135 				ifile, fbytes, PL(fbytes),
136 				fpar, arrowv[arrowc], ofile, ofbytes, PL(ofbytes));
137 		}
138 	} else {
139 		if (pagemode && !plaintextmode || abspageno > 1) {
140 			fprintf(stderr, "\r%s %4u Line%c (%3d%%) %s %s %2d Page%c" E_CLRLN "\r",
141 			   ifile, ilines, PL(ilines),
142 			   fpar, arrowv[arrowc], ofile, abspageno, PL(abspageno));
143 		} else {
144 			fprintf(stderr, "\r%s %4u Line%c (%3d%%) %s %s %4u Line%c" E_CLRLN "\r",
145 				ifile, ilines,PL(ilines),
146 				fpar, arrowv[arrowc], ofile, olines, PL(olines));
147 		}
148 	}
149 	if (endflag) {
150 		fprintf(stderr, "\n");
151 		displayflag = OFF;
152 	} else
153 		displayflag = ON;
154 	CONDRV_ON(stderr);
155 
156 	if (!arrowc--)
157 		arrowc = NARROWS - 1;
158 
159 	previfilename = ifilename;
160 	prevofilename = ofilename;
161 }
162 
163 
164 
165 /************************** More **************************************/
166 
167 void
More(void)168 More(void)
169 /* ���Խ��ϤΤ��Ȥ���� */
170 {
171 #if MSDOS || WINNT || __human68k__
172 	unsigned c;
173 	int reenterflag = 0;
174 
175 	if (delaymode && conoutmode && !coninmode) {
176 		/* ��ä���⡼�ɡʤ���������� */
177 		int k;
178 		for (k = 0; k / 16 < delaytime; k++) kbhit();
179 	}
180 	if (!(moremode && conoutmode && !coninmode &&
181 					morecnt > 0 && --morecnt == 0))
182 		return;
183 
184 	CONDRV_OFF(stdout);
185 	do {
186 		printf(E_MORE "-- More --" E_PLAIN);
187 		fflush(stdout);
188 
189 		while (!kbhit()) ;		/* ^C �����ߤ�����դ��뤿�� */
190 
191 		c = getch();
192 		if (iskanji(c)) {
193 			c = c * 0x100 + getch();
194 		}
195 		printf("\r          " E_CLRLN "\r");
196 		reenterflag = 0;
197 		switch (c) {
198 		case '\r':
199 			morecnt = 1;		/* �ãҤ������줿�鼡�ΰ��ɽ�� */
200 			break;
201 		case ' ':				/* ���ڡ����ʤ鼡�Σ����� */
202 		case ZENSP:
203 			morecnt = moresize;
204 			break;
205 		case '\033':			/* �ţӣå����ʤ齪��ˤ��� */
206 			Terminate(0);
207 			break;
208 		default:				/* ����¾�Υ����ʤ� HELP */
209 			printf("            " E_GUIDE
210 				   "<Return> Next line, <Space> Next screen, <ESC> Exit"
211 				   E_PLAIN "\r");
212 			reenterflag = 1;
213 		}
214 	} while (reenterflag);
215 	CONDRV_ON(stdout);
216 #endif
217 }
218 
219 
220 /********************* Error ******************************************/
221 
222 static void
CheckDisp(void)223 CheckDisp(void)
224 {
225 	if (displayflag) {
226 		Disp(0);
227 		CONDRV_OFF(stderr);
228 		fprintf(stderr, "\n");	/* �в�ɽ������ä����Բ��Ԥ��Ƥ��� */
229 		CONDRV_ON(stderr);
230 		displayflag = OFF;
231 	}
232 }
233 
234 #if UNIX
235 #define BELL_STR ""
236 #else
237 #define BELL_STR "\a"
238 #endif
239 
240 static void
VError(const uchar * fmt,va_list argp,int ferrorflag)241 VError(const uchar *fmt, va_list argp, int ferrorflag)
242 {
243 	errorflag++;
244 
245 	flushall();
246 
247 	if (intextflag || includeflag) {
248 		CheckDisp();
249 
250 		fprintf(stderr, "%s %4u %s*** ", ifilename, ilineno, BELL_STR);
251 		if (fmt && *fmt) {
252 			vfprintf(stderr, (const char *)fmt, argp);
253 			fprintf(stderr, " ***");
254 		}
255 		fprintf(stderr, ferrorflag ? " !!\n" : "\n");
256 
257 		if (ferrorflag)
258 			Terminate(3);				/* exit failure */
259 
260 		More();
261 
262 		longjmp(errorjb, ERR);
263 	} else {
264 		fprintf(stderr, "XTR: %s*** ", BELL_STR);
265 		vfprintf(stderr, (const char *)fmt, argp);
266 		fprintf(stderr, " ***\n");
267 		Terminate(2);					/* exit failure */
268 	}
269 }
270 
271 /* ���顼��å������Ф���æ�Ф��� */
272 void
Error(const uchar * fmt,...)273 Error(const uchar *fmt, ...)
274 {
275 	va_list argp;
276 	VA_START(argp, fmt);
277 	VError(fmt, argp, 0);
278 	va_end(argp);
279 }
280 
281 
282 void
FError(const uchar * fmt,...)283 FError(const uchar *fmt, ...)
284 /* ��̿Ū���顼 */
285 {
286 	va_list argp;
287 	VA_START(argp, fmt);
288 	VError(fmt, argp, 1);
289 	va_end(argp);
290 }
291 
292 
293 void
Message(const uchar * fmt,...)294 Message(const uchar *fmt, ...)
295 /* ñ�˥�å�������ɽ������ */
296 {
297 	va_list argp;
298 	VA_START(argp, fmt);
299 	CheckDisp();
300 	vfprintf(stderr, (const char *)fmt, argp);
301 	va_end(argp);
302 }
303 
304 /*
305  * Local variables:
306  * mode: c
307  * c-indent-level: 4
308  * c-continued-statement-offset: 4
309  * c-brace-offset: -4
310  * c-argdecl-indent: 4
311  * c-label-offset: -4
312  * tab-width: 4
313  * tab-stop-list: (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80)
314  * End:
315  */
316