delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/05/26/10:42:22

Message-Id: <3.0.1.16.20030526104118.35f723ae@earthlink.net>
X-Sender: ethros AT earthlink DOT net
X-Mailer: Windows Eudora Light Version 3.0.1 (16)
Date: Mon, 26 May 2003 10:41:18 -0400
To: djgpp AT delorie DOT com
From: Ethan Rosenberg <ethros AT earthlink DOT net>
Subject: Programming Problems
Mime-Version: 1.0

I am trying to run a program in DOS which will write directly to the
keyboard buffer.  I think the program assumes Intel 16 bit segmented
architecture, and I can't get it to run on DJGPP.  I've attached the
program code.

Can you help??

Thank you.

Ethan Rosenberg

/* ******* TESTWS.C  ***********
 * A program to call a function which will write directly to the keyboard
 * buffer */

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <sys/farptr.h>
#include "sniptype.h"
#include "snipkbio.h"
#include "pchwio.h"
#include "mk_fp.h"
#include "extkword.h"
#include "MK_FP.h"
#define  Success_ 1
/*void FAR * getvect(unsigned intnum)
void FAR * getvect(unsigned intnum);
void setvect(unsigned intnum, void (INTERRUPT FAR *handler)())
void setvect(unsigned intnum, void (INTERRUPT FAR *handler)());*/
unsigned char Peekb(unsigned seg, unsigned ofs);
unsigned short Peekw(unsigned seg, unsigned ofs);
void Pokeb(unsigned seg, unsigned ofs, unsigned char ch);
void Pokew(unsigned seg, unsigned ofs, unsigned short num);
int ungetkey(unsigned short key);
int KB_stuff(char *str);

void main(void)
{
FILE *fptr1, *fptr2;
int i,j,k,l,m,num_read,z,len,inum,icycles,result;
float num,cycles;
char string1[3] = "0";
char string2[16];
char string3[20];
int  data[600];


    result = KB_stuff("dir\n");
    if(result == Success_)
        printf("Yipppeeee\n");
    else
        printf("Waaaaa\n");
}

/* +++Date last modified: 05-Jul-1997 */

/*
**  KB_STUFF.C - Functions to stuff characters and/or strings into a PC's
**               (BIOS) keyboard buffer.
**
**  Original Copyright 1988-1991 by Bob Stout as part of
**  the MicroFirm Function Library (MFL)
**
**  The user is granted a free limited license to use this source file
**  to create royalty-free programs, subject to the terms of the
**  license restrictions specified in the LICENSE.MFL file.
*/

#include <dos.h>
#include <stdio.h>
#include "sniptype.h"
#include "pchwio.h"
#include "snipkbio.h"
#include "MK_FP.h"

unsigned char Peekb(unsigned seg, unsigned ofs);
unsigned short Peekw(unsigned seg, unsigned ofs);
void Pokeb(unsigned seg, unsigned ofs, unsigned char ch);
void Pokew(unsigned seg, unsigned ofs, unsigned short num);

static unsigned head, tail, start, end;
static int idx = 0;
static unsigned short keystack[16][2];

/*
**  ungetkey()
**
**  Stuffs characters into the keyboard buffer.
**
**  Parameters: 1 - Extended character to stuff
**
**  Returns: Success_ or EOF
**
**  Note: This function assumes that the keyboard buffer is in
**        the normal (for IBM) location of 40:1E.
**
*/
unsigned char Peekb(unsigned seg, unsigned ofs);
unsigned short Peekw(unsigned seg, unsigned ofs);
void Pokeb(unsigned seg, unsigned ofs, unsigned char ch);
void Pokew(unsigned seg, unsigned ofs, unsigned short num);

int ungetkey(unsigned short key)
{
      int count;

      head  = Peekw(0x40, 0x1a);
      tail  = Peekw(0x40, 0x1c);
      start = Peekw(0x40, 0x80);
      end   = Peekw(0x40, 0x82);

      count = tail - head;
      if (0 > count)
            count += (16 * sizeof(unsigned));
      count >>= 1;

      if (15 > count)
      {
            disable();
            keystack[idx][0] = Peekw(0x40, tail);
            keystack[idx][1] = tail;
            Pokew(0x40, tail, key);
            tail += sizeof(unsigned);
            if (end <= tail)
                  tail = start;
            Pokew(0x40, 0x1c, tail);
            enable();
            return key;
      }
      return EOF;
}

/*
**  KB_stuff()
**
**  Stuffs strings into the keyboard buffer.
**
**  Parameters: 1 - String to stuff
**
**  Returns: Success_ if successful
**           Error_   in case of error, plus keyboard buffer is
**                    restored
**
**  Note: This function assumes that the keyboard buffer is in
**        the normal (for IBM) location of 40:1E.
*/
#include "MK_FP.h"

int KB_stuff(char *str)
{
      int ercode = Success_;

      idx = 0;
      while (*str)
      {
            if (EOF == ungetkey((unsigned)(*str++)))
            {
                  disable();
                  while (0 <= --idx)
                  {
                        tail = keystack[idx][1];
                        Pokew(0x40, tail, keystack[idx][0]);
                  }
                  Pokew(0x40, 0x1c, tail);
                  enable();
                  ercode = Error_;
                  break;
            }
            else  ++idx;
      }
      idx = 0;
      return ercode;
}
/* +++Date last modified: 05-Jul-1997 */

/*
**  PCHWIO.C - SNIPPETS portable hardware I/O access under DOS
**
**  public domain by Bob Stout
*/

#include "pchwio.h"
#include "mk_fp.h"

#if defined(__ZTC__) && !defined(__SC__)

void FAR * getvect(unsigned intnum)
{
      unsigned seg, off;

      int_getvector(intnum, &off, &seg);
      return MK_FP(seg, off);
}

void setvect(unsigned intnum, void (INTERRUPT FAR *handler)())
{
      unsigned seg = FP_SEG(handler), off = FP_OFF(handler);

      int_setvector(intnum, off, seg);
}

#endif /* ZTC getvect(), setvect() */

/* The only way this program will run is if I comment out the two lines below.
 * However, I get a segmentation error.  Without the lines commented out,  I 
* I get on undefined reference to Peekb, Pokeb, Peekw and Pokew */

/*#if defined(_MSC_VER) || defined(__WATCOMC__) || \
      defined(__ZTC__) || defined(__SC__)         */
      
#if !defined(MK_FP)
 #define MK_FP(seg,off) ((void far *)(((long)(seg) << 16)|(unsigned)(off)))
#endif

unsigned char Peekb(unsigned seg, unsigned ofs)
{
        unsigned char FAR *ptr;

        ptr = MK_FP(seg, ofs);
        return *ptr;
}

unsigned short Peekw(unsigned seg, unsigned ofs)
{
        unsigned FAR *ptr;

        ptr = MK_FP(seg, ofs);
        return *ptr;
}

void Pokeb(unsigned seg, unsigned ofs, unsigned char ch)
{
        unsigned char FAR *ptr;

        ptr = MK_FP(seg, ofs);
        *ptr = ch;
}

void Pokew(unsigned seg, unsigned ofs, unsigned short num)
{
        unsigned short FAR *ptr;

        ptr = MK_FP(seg, ofs);
        *ptr = num;
}

/*#endif /* MSC/ZTC/WC Peek(), poke() */
/* +++Date last modified: 05-Jul-1997 */

/*
**  PCHWIO.H - SNIPPETS header file for portable hardware I/O access under DOS
**
**  public domain by Bob Stout
*/

#ifndef PCHWIO__H
#define PCHWIO__H

#include <dos.h>
#include "extkword.h"


#if defined(__TURBOC__) || defined(__POWERC)
 #ifndef inp
  #define inp           inportb
 #endif
 #ifndef outp
  #define outp          outportb
 #endif
 #ifndef inpw
  #define inpw          inport
 #endif
 #ifndef outpw
  #define outpw         outport
 #endif
#elif defined(__ZTC__)
 #include <int.h>
 #define enable         int_on
 #define disable        int_off
 #if !defined(__SC__)
  void FAR * getvect(unsigned intnum);
  void setvect(unsigned intnum, void (INTERRUPT FAR *handler)());
 #else
  #define getvect       _dos_getvect
  #define setvect       _dos_setvect
 #endif
#else /* assume MSC/QC/WC */
 #include <conio.h>
 #if defined(__WATCOMC__)
  #include <i86.h>
 #endif
 #define enable         _enable
 #define disable        _disable
 #define getvect        _dos_getvect
 #define setvect        _dos_setvect
#endif


#if defined(_MSC_VER) || defined(__WATCOMC__) || \
      defined(__ZTC__) || defined(__SC__)

unsigned char  Peekb(unsigned seg, unsigned ofs);           /* PCHWIO.C */
unsigned short Peekw(unsigned seg, unsigned ofs);           /* PCHWIO.C */
void Pokeb(unsigned seg, unsigned ofs, unsigned char ch);   /* PCHWIO.C */
void Pokew(unsigned seg, unsigned ofs, unsigned short num); /* PCHWIO.C */
 
#elif defined(__TURBOC__)
 #define Peekw peek
 #define Pokew poke
 #define Peekb peekb
 #define Pokeb pokeb
#endif /* peek(), poke() */

#endif /* PCHWIO__H */
/* +++Date last modified: 05-Jul-1997 */

/*
**    SNIPKBIO.H - Snippets header file for keyboard I/O function
*/

#ifndef SNIPKBIO__H
#define SNIPKBIO__H

#include <dos.h>
#include <conio.h>
#include "sniptype.h"
#include "pchwio.h"

Boolean_T getYN(char *prompt, int def_ch,             /* Getyn.C        */
                unsigned timeout);

int  ungetkey(unsigned short key);                    /* Kb_Stuff.C     */
int  KB_stuff(char *str);                             /* Kb_Stuff.C     */

int  IsLeftShift(void);                               /* Isshift.C      */
int  IsRightShift(void);                              /* Isshift.C      */
int  IsShift(void);                                   /* Isshift.C      */

int  IsLeftAlt(void);                                 /* Isshift.C      */
int  IsRightAlt(void);                                /* Isshift.C      */
int  IsAlt(void);                                     /* Isshift.C      */

int  IsLeftCtl(void);                                 /* Isshift.C      */
int  IsRightCtl(void);                                /* Isshift.C      */
int  IsCtl(void);                                     /* Isshift.C      */

int  IsSysRq(void);                                   /* Isshift.C      */
int  timed_getch(int n_seconds);                      /* Timegetc.C     */
int  isxkeybrd(void);                                 /* Isxkbrd.C      */
void setcaps(void);                                   /* Keylocks.C     */
void clrcaps(void);                                   /* Keylocks.C     */
void setnumlock(void);                                /* Keylocks.C     */
void clrnumlock(void);                                /* Keylocks.C     */

#define     RIGHT_SHIFT       0x0001
#define     LEFT_SHIFT        0x0002

#define     EITHER_ALT        0x0008
#define     LEFT_ALT          0x0200

#define     EITHER_CTL        0x0004
#define     LEFT_CTL          0x0100

#if defined(__OS2__)
 #define    SYSRQ             0x8000
#else /* assume DOS */
 #define    SYSRQ             0x0400
#endif

#ifdef __OS2__
 #define INCL_NOPM
 #define INCL_KBD
 #define INCL_DOSPROCESS    /* for DosSleep() */
 #include <os2.h>

KBDINFO setkbmode(void);    /* Change keyboard to binary mode */
void restkbmode(KBDINFO);   /* restore keyboard mode */
                            /* both defined in EXT_KEYS.C */

/* 30-Mar-96 - EBB:
** OS/2 doesn't have a place in memory where information about the last
** key is held, like DOS does.      All the information about a keystroke is
** wrapped up into a structure (KBDKEYINFO) returned from OS/2's
** get-a-key API call, KbdCharIn(), so if you want to query that information
** at some arbitrary later time, you have to save it somewhere.  I have
** chosen to use a global variable defined in ISSHIFT.C to do this.  The
** functions in ISSHIFT.C report the status of the last key stored in that
** global, which may not be the last key pressed in your program if you're
** also storing keystrokes elsewhere.
*/
extern KBDKEYINFO ki;         /* Holds key info - defined in ISSHIFT.C */

 #define peekkey()      (&ki.fsState)
#else /* !__OS2__ */
 #define key_seg  0x40
 #define key_off  0x17
 #define peekkey()      ((unsigned short FAR*) MK_FP(key_seg, key_off))
#endif

#endif /* SNIPKBIO__H */
/* +++Date last modified: 14-Aug-2001 */

/*
**  SNIPTYPE.H - Include file for SNIPPETS data types and commonly used macros
*/

#ifndef SNIPTYPE__H
#define SNIPTYPE__H

#include <stdlib.h>                             /* For free()           */
#include <string.h>                             /* For NULL & strlen()  */

typedef enum {Error_ = -1, Success_, False_ = 0, True_} Boolean_T;

#if defined(__unix__)
 typedef unsigned char  BYTE;
 typedef unsigned long  DWORD;
 typedef unsigned short WORD;
 #if !defined(FAR)
  #define FAR
 #endif
 #if !defined(NEAR)
  #define NEAR
 #endif
 #if !defined(HUGE)
  #define HUGE
 #endif
 #if !defined(PASCAL)
  #define PASCAL
 #endif
 #if !defined(CDECL)
  #define CDECL
 #endif
 #if !defined(INTERRUPT)
  #define INTERRUPT
 #endif
#elif !defined(WIN32) && !defined(_WIN32) && !defined(__NT__) \
      && !defined(_WINDOWS)
 #if !defined(OS2)
  typedef unsigned char  BYTE;
  typedef unsigned long  DWORD;
 #endif
 typedef unsigned short WORD;
#else
 #define WIN32_LEAN_AND_MEAN
 #define NOGDI
 #define NOSERVICE
 #undef INC_OLE1
 #undef INC_OLE2
 #include <windows.h>
 #define HUGE
#endif

#define NUL '\0'
#define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
#define TOBOOL(x) (!(!(x)))
#define FREE(p) (free(p),(p)=NULL)

#endif /* SNIPTYPE__H */
/* +++Date last modified: 05-Jul-1997 */

/*
**  MK_FP.H
**
**  Standard header file making sure this pesky Intel macro is defined!
*/

#ifndef MK_FP__H
#define MK_FP__H

#include "extkword.h"

#if defined(__WATCOMC__)
 #include <i86.h>
#elif !defined(__PACIFIC__)
 #include <dos.h>
#endif

#if !defined(MK_FP)
    #define MK_FP(seg,off) \
      ((void FAR *)(((unsigned long)(seg) << 16)|(unsigned)(off)))
#endif

#endif /* MK_FP__H */
/* +++Date last modified: 05-Jul-1997 */

/*====================================================================

    _MSC_VER        Microsoft C 6.0 and later
    _QC             Microsoft Quick C 2.51 and later
    __TURBOC__      Borland Turbo C, Turbo C++ and BC++
    __BORLANDC__    Borland C++
    __ZTC__         Zortech C and C++
    __SC__          Symantec C++
    __WATCOMC__     WATCOM C
    __POWERC        Mix Power C
    __GNUC__        Gnu C

    Revised:

    25-Sep-95  Bob Stout      Original from PC-PORT.H
    30-Mar-96  Ed Blackman  OS/2 mods for OS/2 ver 2.0 and up
    30-May-96  Andrew Clarke  Added support for WATCOM C/C++ __NT__ macro.
    17-Jun-96  Bob Stout      Added __FLAT__ macros support
    20-Aug-96  Bob Stout      Eliminate Win32 conflicts
======================================================================*/


/* prevent multiple inclusions of this header file */

#ifndef EXTKWORD__H
#define EXTKWORD__H

#include <limits.h>                       /* For INT_MAX, LONG_MAX      */

/*
**  Watcom defines __FLAT__ for 32-bit environments and so will we
*/

#if !defined(__FLAT__) && !defined(__WATCOMC__) && !defined(_MSC_VER)
 #if defined(__GNUC__)
  #define __FLAT__ 1
 #elif defined (_WIN32) || defined(WIN32) || defined(__NT__)
  #define __FLAT__ 1
 #elif defined(__INTSIZE)
  #if (4 == __INTSIZE)
   #define __FLAT__ 1
  #endif
 #elif (defined(__ZTC__) && !defined(__SC__)) || defined(__TURBOC__)
  #if ((INT_MAX != SHRT_MAX) && (SHRT_MAX == 32767))
   #define __FLAT__ 1
  #endif
 #endif
#endif

/*
**  Correct extended keywords syntax
*/

#if defined(__OS2__)        /* EBB: not sure this works for OS/2 1.x */
 #include <os2def.h>
 #define INTERRUPT
 #define HUGE
#elif defined(_WIN32) || defined(WIN32) || defined(__NT__)
 #define WIN32_LEAN_AND_MEAN
 #define NOGDI
 #define NOSERVICE
 #undef INC_OLE1
 #undef INC_OLE2
 #include <windows.h>
 #define INTERRUPT
 #define HUGE
#else /* ! Win 32 or OS/2 */
 #if (defined(__POWERC) || (defined(__TURBOC__) && !defined(__BORLANDC__)) \
       || (defined(__ZTC__) && !defined(__SC__))) && !defined(__FLAT__)
  #define FAR far
  #define NEAR near
  #define PASCAL pascal
  #define CDECL cdecl
  #if (defined(__ZTC__) && !defined(__SC__)) || (defined(__SC__) && \
        (__SC__ < 0x700))
   #define HUGE far
   #define INTERRUPT
  #else
   #define HUGE huge
   #define INTERRUPT interrupt
  #endif
 #else
  #if (defined(__MSDOS__) || defined(MSDOS)) && !defined(__FLAT__)
   #define FAR _far
   #define NEAR _near
   #define HUGE _huge
   #define PASCAL _pascal
   #define CDECL _cdecl
   #define INTERRUPT _interrupt
  #else
   #define FAR
   #define NEAR
   #define HUGE
   #define PASCAL
   #define CDECL
  #endif
 #endif
#endif

#endif /* EXTKWORD__H */


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019