File:  [OS/2 SDKs] / os2sdk / demos / examples / vioreg / register.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:11 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/*  Main entry point to REGISTER.  This routine does initial setup,   */
/*  such as opening the files, allocating the shared memory,	      */
/*  registering the replacement routine, calling the original routine */
/*  and starting the ACCESS routine in a different screen group.      */
/*								      */
/* Created by Microsoft Corp. 1988				      */


#define INCL_DOSSESMGR
#define INCL_DOSFILEMGR
#define INCL_DOSMEMMGR
#define INCL_DOSSEMAPHORES
#define INCL_SUB

#include <os2def.h>
#include <bse.h>
#include <stdio.h>
#include <fcntl.h>
#include "share.h"	   /* Common declarations between processes */

extern unsigned _aenvseg;  /* Used by GetPath in getting the full path */
extern unsigned _acmdln;   /* name of this program (the one used by    */
			   /* EXECPGM())			       */

int fileopen();
void GetPath();


main(argc, argv)
int argc;
NPCH argv[];

{ STARTDATA SDbuf;			 /* Stores process data for
					    DosStartSession */
  struct ShareRec far *SmemPtr; 	 /* Shared memory layout defined in
					    Share.h  */
  HFILE fpoint; 			 /* file pointer */
  USHORT byread;			 /* Number of bytes read by DosRead */
  unsigned rc;				 /* return code */
  SEL Selector; 			 /* Shared memory selector */
  USHORT SessionID, ProcessID;		 /* Values returned by DosStartSession
					    when child process is started.
					    ACCESS.EXE isn't a child process. */
  char string[10];			 /* Read by DosRead, written by
					    VioWrtTTy */
  char *spbuf, pathbuf[128], Inputs[10];
  char pathname[128], modname[128];

   /* If no file is specified, exit the program to start again */
   if (argc == 1)  {
      printf("Usage: Register n\n");
      printf("  Must specify n as 1,2, or 3\n");
      DosExit(EXIT_PROCESS,0);
      }

   /* Allocate shared memory for counter in ACCESS.EXE */
   if (rc = DosAllocShrSeg( SHRSEGSIZE, SHRSEGNAME, &Selector))  {
	     printf("Alloc of shared memory failed, error: %d\n", rc);
	     DosExit(EXIT_PROCESS,0);
	     }

   /* Get a far pointer from a 16-bit selector */
   SmemPtr = (struct ShareRec far *) MAKEP(Selector,0);

   /* Initialize shared memory semaphores */
   DosSemSet((HSEM)&(SmemPtr->updated));
   DosSemClear((HSEM)&(SmemPtr->mutexsem));
   SmemPtr->count = 0;
   SmemPtr->mutexsem = 0;
   SmemPtr->totalength = 0;

   /* Data that describes ACCESS.EXE for DosStartSession */
   spbuf = "SHARED MEMORY ACCESS";
   SDbuf.cb = sizeof(SDbuf);
   SDbuf.Related = 0;			  /* Unrelated process */
   SDbuf.FgBg = 0;			  /* Start ACCESS.EXE in foreground */
   SDbuf.TraceOpt = 0;			  /* Run synchronously */
   SDbuf.PgmTitle = spbuf;		  /* Title in session manager box */
   GetPath(pathbuf);
   sprintf(pathname, "%s%s", pathbuf,"ACCESS.EXE");
   SDbuf.PgmName= pathname;		  /* Exec path - see GetPath */
   SDbuf.PgmInputs= 0L; 		  /* No command line arguments */
   SDbuf.TermQ = 0L;			  /* No notification queue */

   /* Start ACCESS.EXE using above data */
   /* SessionID and ProcessID are not returned - unrelated process */
   DosStartSession(&SDbuf, &SessionID, &ProcessID);

   /* Register the replacement routine in the dynamic link module, within
      this screen group. VIOROUT is the name of the dll, and VIOROUT is the
      name of the replacement routine in the dll.  */
   rc = VioRegister("VIOROUT", "VIOROUT", 0x4000L, 0L);
   if (rc >= 1)
      printf(" VioRegister error: %d\n", rc);

   /* Open file for VioWrtTTy to read */
   fpoint = fileopen(pathbuf,*argv[1]);

   DosRead(fpoint, string, 10, &byread);
   while (byread != 0) {
      /* Calling VioWrtTTy sends control to the VIO router, which invokes
	 the replacement routine registered in VioRegister */
      VioWrtTTy(string,10,0);
      DosRead(fpoint, string, 10, &byread);
      }

   VioDeRegister();

}

int fileopen(pathvar,av)
NPCH pathvar;
int av;
{  int fp;
   char filename[128];

   switch (av) {
      case '1':
	 sprintf(filename, "%s%s", pathvar, "TEST1.DAT");
	 fp = open(filename, O_RDONLY);
	 if (fp == -1) {
	    printf(" Cannot open file 1.\n");
	    DosExit(EXIT_PROCESS,0); }
	 break;
      case '2':
	 sprintf(filename, "%s%s", pathvar, "TEST2.DAT");
	 fp = open(filename, O_RDONLY);
	 if (fp == -1)	{
	    printf(" Cannot open file 2.\n");
	    DosExit(EXIT_PROCESS,0);  }
	 break;
      case '3':
	 sprintf(filename, "%s%s", pathvar, "TEST3.DAT");
	 fp = open(filename, O_RDONLY);
	 if (fp == -1)	{
	    printf(" Cannot open file 3.\n");
	    DosExit(EXIT_PROCESS,0);  }
	 break;
      default:
	 sprintf(filename, "%s%s", pathvar, "TEST1.DAT");
	 fp = open(filename, O_RDONLY);
	 if (fp == -1) {
	    printf(" Cannot open file.\n");
	    DosExit(EXIT_PROCESS,0); }
	 break;
      }
   return(fp);
}

void GetPath(pbuf)
NPCH pbuf;

{    int i,c;
     PCH progname;


     SELECTOROF(progname) = _aenvseg;  /* Standard C runtime routines	   */
     OFFSETOF(progname) = _acmdln-1;   /* The pathname comes just prior    */
     c = 0;			       /* to argv[0]-but is in a different */
     while(*--progname != '\\')        /* segment, thus the copy to pbuf   */
	 ;

     /* Cut the program name off the path */
     while (*--progname)
	 c++;

     for (i=0; i<=c; i++)
	pbuf[i]= *++progname;

     pbuf[++c] = '\0';
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.