File: modecon.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (442 lines) | stat: -rw-r--r-- 9,971 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include<fcntl.h>
#include<DOS.h>

#define CF 1	/*Carry flag */
#define BYTE unsigned char

			/* action to be performed */
#define MA_Query    1   /* MODE detects a problem from DISPLAY.SYS */
#define MA_Prepare  2
#define MA_Select   3
#define MA_MODE   100   /* MODE detected a problem */


#define MaxBufSize   58880

/*////////// V A R I A B L E S  ////////////////////////////////////*/


	unsigned int table[20];	 /* table to allocate current status of DISPLAY */
	unsigned char buf[MaxBufSize];  /* was 9728 with raw files */


/*////////// F U N C T I O N S  ////////////////////////////////////*/


void deviceError ( BYTE action, BYTE errorcode )
{
	if (action==MA_Query)
	{
		switch (errorcode)	{
			case 26:  puts ("MODE: Active codepage was never set");
				    break;
			case 27:  puts ("MODE: Device read codepage error");
				    break;
			default:  puts ("MODE: Unknown error reading codepage");
		}
	}
	else if (action==MA_Prepare)
	{
		switch (errorcode)	{
			case 27:  puts ("MODE: Codepage was not found in CPI file");
				    break;
			case 29:  puts ("MODE: Error obtaining codepage information from file");
				    break;
			case 31:  puts ("MODE: specified CPI file is damaged or has errors");
				    break;
			default:  /* and case 29 (Aitor: unknwon error reason?) */
				    puts ("MODE: Unknown codepage prepare error");
		}
	}
	else if (action==MA_Select)
	{
		switch (errorcode)	{
			case 26:  puts ("MODE: Specified codepage was not found in file");
				    break;
			case 27:  puts ("MODE: KEYB failed to change codepage");
				    break;
			case 31:  puts ("MODE: Device select codepage error");
				    break;
			default:  puts ("MODE: Unknown select prepare error");
		}
	}
	else if (action==MA_MODE)
	{
		switch (errorcode)	{
			case 100:  puts ("MODE: INTERNAL ERROR: size for prepared table too small");
				     break;
			case 101:  puts ("MODE: Non-compatible driver in use");
				     break;
			case 102:  puts ("MODE: File not found");
				     break;
			case 103:  puts ("MODE: Error specifying the codepage and buffer");
				     break;
			case 104:  puts ("MODE: Error: buffer selected not present");
				     break;
			default:   puts ("MODE: Unknown MODE error");
		}
	}
	else puts ("MODE: Unknown agent error");

	exit (errorcode);
}


unsigned int GetCodepage ( void )
{
	unsigned int cp;

	asm {  MOV AX, 0xAD02
		 INT 0x2F
		 MOV cp, BX
	}

	return cp;
}


void FillTableData ( void )
{
	unsigned int s,o;
	unsigned char i;

	for (i=0; i<20; i++) table[i]=0;

	s = FP_SEG (table);
	o = FP_OFF (table);
	asm {	MOV AX, 0xAD03
		MOV CX, 25
		MOV ES, s
		MOV DI, o
		INT 0x2F
		PUSHF
		POP AX
		MOV i,AL
		}
	if (i & CF)
		deviceError (MA_MODE, 100);
	if (! (table[2] | table[0]) )
		deviceError (MA_Query, 27);
}


unsigned int ShowStatus ( void )
{
	unsigned int cp=0;
	unsigned char i;

	puts   ("Status for device CON:");
	puts   ("======================");
	puts   ("");

	cp = GetCodepage();
	if (cp != 0xFFFF)
		printf ("Active codepage for device CON is %u\n", cp);
	else
		puts   ("Active codepage was never set");

	FillTableData ();
	printf ("\n\nNumber of subfonts: %u\n",table[1]);
	printf ("Hardware codepages: ");
	for (i=1;i<=table[2];i++)
	{
	   printf ("%u",table[2+i]);
	   if (i<table[2]) printf(", ");
	}
	printf ("\nPrepared codepages: ");
	for (i=1;i<=table[0];i++)
	{
	   printf ("%u",table[2+i+table[2]]);
	   if (i<table[0]) printf(", ");
	}
	puts   ("\n\nMODE status code page function completed");
	return (cp);
}



unsigned int CodepageSelect ( unsigned int cp )
{
	unsigned int success;

	asm { MOV AX, 0xAD01
		MOV BX, cp
		INT 0x2F
		MOV success, AX
	}
	if (success)
		printf ("MODE select codepage %u function completed\n", cp);
	else  {
		printf ("MODE: unable to select codepage %u\n", cp);
		asm { MOV AX, 0xAD05
			XOR BX,BX
			INT 0x2F
			MOV success, BX
		}
		deviceError (MA_Select, success);
	}
	return (success);
}



unsigned int CheckFDDisplayVer ( void )
{
	unsigned char Rah;
	unsigned int  Rbx;

	asm { MOV AX, 0xAD00
		INT 0x2F
		MOV Rah, AH
		MOV Rbx, BX
	}

	return ( (Rah==0xFF) && (Rbx==0x000A) );
						/* version 0.10 only! */
}


unsigned int  ReadNumber (char *line, unsigned int *lineptr )
{
	char     cvtstr[8];
	unsigned int i=0;

	while ( (line[*lineptr]>='0') && (line[*lineptr]<='9'))
		cvtstr[i++] = line[(*lineptr)++];
	cvtstr[i] = 0;

	return (  (unsigned int) (atoi (cvtstr)) );
}



/* returns buffer to fill, starts on 0 */
unsigned int GetPosition (char *s, unsigned int *cp)
{
	unsigned int pos = 0;
	unsigned int i = 1;

	if (s[0] != '(')
		deviceError (MA_MODE, 103);

	while (s[i]==',')
	{	pos++;  i++; }

	*cp = ReadNumber (s, &i);

	while (s[i]==',') i++;



	if (s[i++] != ')')
		deviceError (MA_MODE, 103);

	if (s[i])
		deviceError (MA_MODE, 102);

	return pos;
}



unsigned int CodepagePrepare (char *bufferpos, char *filename)
{
	int cpf;
	unsigned int  cp, pos;
	unsigned int  s,o,f,n,m;

	/** Determine that it is FreeDOS DISPLAY **/
	if (!CheckFDDisplayVer())
		deviceError (MA_MODE, 101);

	/** Parse the number and position **/
	pos = GetPosition (bufferpos, &cp);

	FillTableData ();

	if (pos > table[0] )
		deviceError (MA_MODE, 104);

	/** Check that the file exists **/
	if ((cpf = open(filename, O_RDONLY | O_BINARY)) == -1)
		deviceError (MA_MODE, 102);

	/** Check that has correct size, warn otherwise **/
//	if ( filelength(cpf) != 58880)
//		puts ("Warning: CPI file has unstandard size, DISPLAY may fail");

	/** Read the buffer **/
	n = read(cpf, buf, MaxBufSize);
	m = filelength (cpf);
	close (cpf);
	if ( n != m )
		deviceError (MA_Prepare, 29);
	close (cpf);

	/** Send the buffer **/
	s = FP_SEG(buf);
	o = FP_OFF(buf);
	s = s + ((o & 0xFFF0) >> 4);	/* normalize a bit */
	o &= 0x000F;

	asm {   PUSH DS

		  MOV AX, 0xAD0E
		  MOV BX, cp
		  MOV DX, pos
		  MOV DS, s
		  MOV SI, o
		  INT 0x2F
		  PUSHF
		  POP AX

		  POP DS

		  MOV f, AX
	}

	/** Check the result **/
	if ( f & CF )
	{
		printf ("MODE: Error when preparing codepage %u\n", cp);
		asm { MOV AX, 0xAD05
			XOR BX,BX
			INT 0x2F
			MOV cpf, BX
		}
		deviceError (MA_Prepare, cpf);
	}

		printf ("MODE prepare codepage %u function completed\n", cp);

}



#define NoAction    		0
#define ShowStatusCodepage 	1
#define SetCodepage 		2
#define PrepareCodepage 	3
#define FastHelp			4
#define RefreshCodepage		5
#define SyntaxError	     10
#define CPSyntaxError	     11


int main (int argc, char *argv[])
{

	unsigned char action = NoAction;
	unsigned int  codpn  = 0;

	puts ("FreeDOS MODECON 0.10\n");

	action = SyntaxError;

	switch (argc)
	{
		case 2:
		   if ( !strcmp (argv[1], "/?") )	/* check FastHelp /? */
		   {
			action = FastHelp;
			break;
		   }

		case 3: case 4: case 5:

		   /*** First param should always be CODEPAGE ***/
		   strupr (argv[1]);
		   if ( strcmp (argv[1], "CODEPAGE") && strcmp (argv[1], "CP"))
		   {
			action = SyntaxError;
			break;
		   }
		   if (argc==2)
		   {
			action = ShowStatusCodepage;
			break;
		   }
		   if (argc==3)
		   {
			if ( !strcmp(strupr(argv[2]),"REFRESH"))
			{
				codpn = GetCodepage();
				if (codpn == 0xFFFF)
				{
					puts ("MODE: Active codepage was never set");
					exit (0);
				}
			}
			action = RefreshCodepage;
			break;
		   }

		   /*** If 3 or 4 parameters, should be either PREPARE or SELECT ***/
		   strupr (argv[2]);
		   if ( !strcmp (argv[2], "SELECT") || !strcmp (argv[2], "SEL"))
		   {
			codpn = atoi (argv[3]);
			if  ( codpn )
				action = SetCodepage;
			break;
		   }
		   if ( argc==4 )
		   {
			action = SyntaxError;
			break;
		   }

		   if ( !strcmp (argv[2], "PREPARE") || !strcmp (argv[2], "PREP"))
			action = PrepareCodepage;
	}

	switch (action)
	{
			case ShowStatusCodepage:
			   ShowStatus ();
			   break;
			case RefreshCodepage:
			case SetCodepage:
			   CodepageSelect (codpn);
			   break;
			case PrepareCodepage:
			   CodepagePrepare (argv[3], argv[4]);
			   break;
			case FastHelp:
			   puts ("MODECON  CODEPAGE ");
			   puts ("MODECON  CODEPAGE  SELECT  n");
			   puts ("MODECON  CODEPAGE  PREPARE b filename");
			   puts ("MODECON  CODEPAGE  REFRESH");
			   puts ("MODECON   /?\n");
			   puts ("Interfaces codepage management with FreeDOS DISPLAY 0.06..0.08 driver");
			   puts ("CODEPAGE                     Shows currently active codepage");
			   puts ("CODEPAGE PREPARE b filename  Prepares codepage buffer, see below");
			   puts ("CODEPAGE REFRESH             Refreshes current codepage");
			   puts ("CODEPAGE SELECT n            Sets (previously prepared) codepage n");
			   puts (" 				         as active codepage");
			   puts ("REMARKS");
			   puts ("- CODEPAGE may be abbreviated by CP");
			   puts ("- PREPARE may be abbreviated by PREP");
			   puts ("- SELECT may be abbreviated by SP");
			   puts ("- Codepages should be prepared before they can be selected");
			   puts ("- To prepare a buffer, set the number in brackets, with commas to");
			   puts ("  indicate the buffer position. E.g.  (,,866) prepares cp866 in the");
			   puts ("  third buffer (only ONE buffer at a time)");
			   break;
			case SyntaxError:
			   puts ("MODECON: Syntax error");
			   puts ("Please, check  MODECON /?  for further information");
			   return (-1);
			case CPSyntaxError:
			   puts ("MODECON: Invalid codepage number.");
			   return (-1);
			default:
			   puts ("MODECON: Internal error");
			   puts ("Please, report commandline to aitor.sm@wanadoo.es");
			   return (-1);
	}
}