#include <stdio.h>

char *syntax= "Syntax: sskey [-bXXX] on   OR  sskey [-bXXX] off\n";
char *pcmcom= "Requires that \"pcmfun.com\" be loaded.\n";
char *helpem= "Turn on or off the \"Double-Shift-Key Sound\" feature.\n";
char *pcmnot= "error: pcmfun.com is not loaded.\n";
char *copyright= "sskey - v(1.0) - Copyright Media Vision, Inc. 1992\n";
char *programmer= "Bart Crane";

main(int argc, char **argv)
{
	unsigned int boardaddress= 0;

	if (argc < 2)
		{
		fprintf(stderr, syntax);
		fprintf(stderr, pcmcom);
		fprintf(stderr, helpem);
		return(1);
		}

	if (argv[1][0] == '-' && argv[1][1] == 'b')
		{
		char *b= &argv[1][2];

		while (isxdigit(*b))
			{
			boardaddress*= 16;
			if (*b >= '0' && *b <= '9') boardaddress+= *b- '0';
			else
			if (*b >= 'A' && *b <= 'F') boardaddress+= *b- 'A'+ 10;
			else
			if (*b >= 'a' && *b <= 'f') boardaddress+= *b- 'a'+ 10;
			b++;
			}

			{
			int i= 1;

			while (i < argc- 1)
				{
				argv[i]= argv[i+ 1];
				i++;
				}
			argv[i]= NULL;
			}

		boardaddress<<= 4;
		}

	if (!checkforsig(boardaddress))
		{
		fprintf(stderr, pcmnot);
		return(2);
		}

	if (!stricmp(argv[1], "on"))
		{
		_asm
			{
			mov ax, 0102h
			push si
			mov si, 8017h
			or si, boardaddress
			int 94h
			pop si
			}
		}
	else
	if (!stricmp(argv[1], "off"))
		{
		_asm
			{
			mov ax, 0002h
			push si
			mov si, 8017h
			or si, boardaddress
			int 94h
			pop si
			}
		}
	else
		{
		fprintf(stderr, syntax);
		return(1);
		}

	return(0);
}

checkforsig(int boardaddress)
{
	int status;
	int int94isthere= 0;

	_asm
		{
		mov al, 94h
		mov ah, 35h					; GETDOSVECTOR
		int 21h

		mov ax, es
		or ax, bx
		mov int94isthere, ax

		mov bx, 107h				; offset f_sig in segment holding int 94h

		mov ax, es:[bx][0]
		xor ax, 4350h				; 'CP'
		jnz nodriver

		mov ax, es:[bx][2]
		xor ax, 2D4dh				; '-M'
		jnz nodriver

		mov ax, es:[bx][4]
		xor ax, 4853h				; 'HS'
		jnz nodriver

		mov ax, es:[bx][6]
		xor ax, 5241h				; 'RA'
		jnz nodriver

		mov ax, es:[bx][8]
		xor al, 4Bh					; 'K'
		jnz nodriver

		cbw							; ax= 0

		cmp boardaddress, 0		; board specified?
		jz nodriver				

		cmp int94isthere, 0		; 
		jz nodriver
		
		push si
		mov si, 8002h
		or si, boardaddress
		int 94h
		pop si
		or ax, ax					; ax= 0 if not at address, !0 if is
		mov ax, 0					; ax= 0
		jnz nodriver				; ax= 0 if driver
		dec ax						; ax= !0 if nodriver

		nodriver:					

		mov status, ax				; ax= !0 if nodriver, 0 if driver
		}

	return(!status);				// status= 0 if nodriver, !0 if driver
}

