home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / NET14 / START14.C < prev    next >
C/C++ Source or Header  |  1991-10-25  |  14KB  |  384 lines

  1.  
  2. #define NET14MASTER
  3.  
  4. #ifdef __TURBOC__
  5. #include "turboc.h"
  6. #endif
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <malloc.h>
  12. #include <dos.h>
  13. #include <ctype.h>
  14. #include <process.h>
  15. #include <errno.h>
  16. #ifdef MSC
  17. #include <direct.h>
  18. #include <signal.h>
  19. #include <time.h>
  20. #endif
  21.  
  22.  
  23. #ifdef MEMORY_DEBUG
  24. #include "memdebug.h"
  25. #endif
  26. #include "netevent.h"
  27. #include "hostform.h"
  28. #include "whatami.h"
  29. #include "externs.h"
  30.  
  31. /* #define DEBUG */
  32.  
  33. /*
  34. *    Global variables
  35. */
  36. static unsigned char path_name[_MAX_DRIVE+_MAX_DIR],   /* character storage for the path name */
  37.     temp_str[100],temp_data[100];        /* temporary character storage */
  38. #ifdef CHECKNULL
  39. unsigned char nullbuf[1024];        /* buffer to hold the lowest 1024 bytes */
  40. #endif
  41. unsigned char buf[256];
  42. static char *config;
  43. int debug = 0;              /* enable with -D option */
  44.  
  45. #define SERIAL  0x14
  46. #define NUM_COMM_PORTS  4   /* the number of comm. ports supported, remember to change this variable in int14.asm also */
  47.  
  48. extern volatile unsigned char *data_begin[NUM_COMM_PORTS];   /* pointers to the beginning of the data buffer for the comm.ports */
  49. extern unsigned char *data_max[NUM_COMM_PORTS];     /* pointers to the maximum value for the data_end pointers */
  50. extern volatile unsigned char *data_end[NUM_COMM_PORTS];     /* pointers to the end of the data buffer for the comm. ports */
  51. extern unsigned char *data_start[NUM_COMM_PORTS];   /* pointers to the start of the data buffer for the comm. ports */
  52. #define PORT_DATA_SIZE      2048        /* this is sort of a guess, might need larger */
  53. unsigned char port_data[NUM_COMM_PORTS][PORT_DATA_SIZE]; /* data buffer for data read in from the comm. port */
  54. int comm_port_index[NPORTS];                /* lookup table from a network port number to a comm port number */
  55. unsigned char myipnum[4];           /* the ip number for this machine */
  56. uint speedup=2;
  57.  
  58. #ifdef OLD_WAY
  59. int    bypass_passwd=0;        /* whether to bypass the password check, not used */
  60. struct machinfo *mp;
  61.  
  62. extern struct config def;   /* Default settings obtained from host file */
  63. extern unsigned char initialized_flags;     /* flags indicating whether a port has been initialized */
  64. extern unsigned char connected_flags;       /* flags indicating whether a port is connected yet */
  65. extern unsigned char opening_flags;         /* flags indicating whether a port is negotiating an open connection */
  66. extern unsigned char port_buffer[NUM_COMM_PORTS][64];    /* four buffers to store the machine name to connect to */
  67. extern unsigned char buffer_offset[NUM_COMM_PORTS];      /* the offset into the buffer currently */
  68. extern int pnum[NUM_COMM_PORTS];                         /* the port number we are connected to */
  69.  
  70.  
  71.  
  72. unsigned char parsedat[32];                 /* character buffer to store network writes */
  73. int telstate[NUM_COMM_PORTS]={STNORM,STNORM,STNORM,STNORM};  /* the telnet state for each comm. port */
  74. int substate[NUM_COMM_PORTS]={0,0,0,0};                  /* the telnet state for each comm. port */
  75. int echo[NUM_COMM_PORTS]={1,1,1,1};
  76. int igoahead[NUM_COMM_PORTS]={0,0,0,0};
  77. int ugoahead[NUM_COMM_PORTS]={0,0,0,0};
  78. int timing[NUM_COMM_PORTS]={0,0,0,0};
  79. char c;
  80. #ifdef QAK
  81. int buffer_stat=0,max_buffer_stat=0;
  82. #endif
  83. #endif
  84.  
  85. /*
  86. *    main ( argc, argv )
  87. *
  88. *    Entry : 
  89. *
  90. *        parameter 1 : machine name
  91. *
  92. */
  93.  
  94. /* #define TSR
  95. #define PORT 1 */
  96.  
  97. int main(argc,argv)
  98. int argc;
  99. char *argv[];
  100. {
  101. #ifdef TSR
  102.     union REGS inregs;      /* register set going into the interrupt */
  103.     union REGS outregs;     /* register set going out of the interrupt */
  104.     unsigned int str_length;
  105. #endif
  106.     int i,
  107. #ifdef TSR
  108.         cnt,ev,what,dat,
  109. #endif
  110.         exec_num;           /* the argument number which is the name of the file to execute */
  111.     char file_name[_MAX_PATH],
  112.         *str,
  113.         spawn_str[_MAX_PATH];    /* file name oft he executable to spawn */
  114.  
  115. #ifdef QAK
  116. printf("stackavail=%u\n",stackavail());
  117. #endif
  118.  
  119. #ifdef __TURBOC__
  120.     fnsplit(argv[0],path_name,buf,temp_str,temp_data);
  121. #else
  122.     _splitpath(argv[0],path_name,buf,temp_str,temp_data);   /* split the full path name of net14.exe into it's components */
  123. #endif
  124.     strcat(path_name,buf);    /* append the real path name to the drive specifier */
  125.  
  126.  
  127.     config = (getenv("CONFIG.TEL"));    /* check for a config.tel in the environment */
  128.     if(config)                  /* set a different config.tel file */
  129.         Shostfile(config);
  130.  
  131. #ifdef QAK
  132.     puts("National Center for Supercomputing Applications");    /* put the banner on the screen */
  133.     puts("Interrupt 14h driver");
  134.     puts("January 1991\n");
  135. #endif
  136.  
  137. #ifndef TSR
  138.     if(argc<2) {
  139.         puts("Usage: net14 [-h hostfile] childname [child_param]\n");
  140.         exit(1);
  141.       } /* end if */
  142. #endif
  143. /*
  144. *  work on parms
  145. */
  146.     for(i=1; i<argc; i++) {            /* look at each parm */
  147.         if(*argv[i]=='-') {     /* command line parameter */
  148.             switch(*(argv[i]+1)) {
  149.                 case 'h':
  150.                     Shostfile(argv[++i]);    /* set new name for host file */
  151.                     break;
  152.  
  153.                 case 'D':   /* turn on debugging */
  154.                     debug=1;
  155.                     break;
  156.  
  157.                 case '#':   /* set the clock speedup */
  158.                     speedup=atoi(argv[i]+2);    /* get the speedup factor */
  159.                     if(speedup!=1 && speedup!=2 && speedup!=4 && speedup!=8 && speedup!=16 && speedup!=32)  /* only accept multiples of two between 1 and 32 for the speedup factor */
  160.                         speedup=1;
  161.                     break;
  162.  
  163.                 default:
  164.                     puts("Usage: net14 [-h hostfile] [-#<speedup>] filename [param]\n");
  165.                     exit(1);
  166.               }
  167.           }
  168.         else {  /* must be the name of the file to execute */
  169.             exec_num=i;     /* save the argument number of the name to execute */
  170.             break;          /* break out of the loop (any further arguments must pertain to the program to execute) */
  171.           } /* end else */
  172.       }
  173.  
  174.     if(!int14init())    /* install the network stuff */
  175.         exit(1);
  176.  
  177.     for(i=0; i<NUM_COMM_PORTS; i++) {    /* initialize the port buffers */
  178.         data_begin[i]=port_data[i];     /* start at the beginning of the buffer */
  179.         data_end[i]=port_data[i];       /* set the end of the buffer at the beginning currently */
  180.         data_start[i]=port_data[i];     /* set the start of each buffer */
  181.         data_max[i]=port_data[i]+PORT_DATA_SIZE;    /* set the maximum value the buffer can use */
  182. /* Special: grab at least one port to communicate TCP over */
  183.         makeport();
  184. #ifdef DEBUG
  185. printf("%d: data_begin=%p, data_max=%p, data_end=%p\n",i,data_begin[i],data_max[i],data_end[i]);
  186. #endif
  187.       } /* end for */
  188.  
  189.     for(i=0; i<NPORTS; i++)     /* initialize the comm port indices */
  190.         comm_port_index[i]=-1;
  191.  
  192. #ifdef DEBUG
  193. printf("data_begin[2]=[%x][%x]\n",(unsigned)(*((unsigned *)&data_begin[2])),(unsigned)(*(((unsigned *)&data_begin[2])+1)));
  194. #endif
  195.  
  196.     int14inst();        /* install the int 14h handler */
  197. #ifdef QAK
  198. printf("int14 =%p\n",_dos_getvect(0x14));
  199. #endif
  200.  
  201. /* #ifndef TSR */
  202.     timeinst();         /* install the timer interrupt handler */
  203. /* #endif */
  204. #ifdef QAK
  205. printf("int1C =%p\n",_dos_getvect(0x1C));
  206. #endif
  207.  
  208. #ifdef TSR
  209.     inregs.h.ah=0;          /* set to initialize the comm. port */
  210.     inregs.x.dx=PORT;          /* use port two */
  211.     int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  212.  
  213.     inregs.h.ah=1;          /* set to send a character */
  214.     inregs.h.al=2;          /* send initialization character */
  215.     inregs.x.dx=PORT;          /* use port two */
  216.     int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  217.  
  218.     str=argv[1];
  219.     str_length=strlen(str);
  220.    for(i=0; i<(int)str_length; i++,str++) {
  221.         inregs.h.ah=1;          /* set to send a character */
  222.         inregs.h.al=*str;       /* send name character */
  223.         inregs.x.dx=PORT;          /* use port two */
  224.         int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  225.       } /* end for */
  226.  
  227.     inregs.h.ah=1;          /* set to send a character */
  228.     inregs.h.al=3;          /* send initialization character */
  229.     inregs.x.dx=PORT;          /* use port two */
  230.     int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  231.  
  232. #ifdef QAK
  233.     if(!int14open(argv[1]),2)    /* open session to the specified machine */
  234.         exit(1);
  235. #endif
  236.  
  237.     c = 0;
  238.     do {
  239.         if(kbhit()) {               /* has key been pressed */
  240.             c=(char)getch();
  241. if(c==16)
  242.     break;
  243. if(c==14)
  244. #ifdef QAK
  245.     printf("max_buffer_stat=%d\n",max_buffer_stat);
  246. #else
  247.     printf("stackavail()=%u\n",stackavail());
  248. #endif
  249.             inregs.h.ah=1;          /* set to send a character */
  250.             inregs.h.al=c;          /* send character */
  251.             inregs.x.dx=PORT;          /* use port two */
  252.             int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  253.           }
  254. #ifdef QAK
  255. int14netsleep();            /* call this to maintain network */
  256. #endif
  257. #ifdef QAK
  258.         inregs.h.ah=3;          /* set to check status */
  259.         inregs.x.dx=PORT;          /* use port two */
  260.         int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  261.         if(outregs.h.ah&0x01) { /* check for data being ready */
  262.             inregs.h.ah=2;          /* set to receive a character */
  263.             inregs.x.dx=PORT;          /* use port two */
  264.             int86(SERIAL,&inregs,&outregs);     /* call to initialize the interrupt */
  265.             c=(int)outregs.h.al;         /* get the return value */
  266.             putchar(c);
  267.           } /* end if */
  268. #else
  269.         dat=int14check();
  270.         if(dat&0x0100) {        /* chek for data begin ready */
  271.             c=int14receive();
  272.             putchar(c);
  273.           } /* end if */
  274. #endif
  275.       } while(c!=16);            /* Ctrl-P, arbitrary escape */
  276. #ifdef QAK
  277. printf("max_buffer_stat=%d\n",max_buffer_stat);
  278. #endif
  279.     timedeinst();   /* remove the timer interrupt handler */
  280.     int14deinst();  /* remove the int 14h handler */
  281.     netclose(pnum[PORT]);
  282.     netshut();
  283. #else
  284. #ifdef QAK
  285.     go_tsr();
  286. #else
  287. #ifdef QAK
  288. printf("sizeof(struct port)=%d\n",sizeof(struct port));
  289. #endif
  290. #ifdef CHECKNULL
  291.     memcpy(nullbuf,MK_FP(0x0,0x0),1024);
  292. #endif
  293. #ifdef OLD_WAY
  294.     str=malloc(5000);      /* malloc a large chunk of memory */
  295.     free(str);      /* just to get it into the memory pool */
  296. #endif
  297.     strupr(argv[exec_num]);
  298.     _searchenv(argv[exec_num],"PATH",file_name);  /* get the pathname of the file to execute */
  299.     if(file_name[0]=='\0') {  /* couldn't find the executable in the path, is must be in the current directory */
  300.         strcpy(spawn_str,argv[exec_num]);
  301.         strcat(spawn_str,".COM");
  302.         _searchenv(spawn_str,"PATH",file_name);  /* get the pathname of the file to execute */
  303.         if(file_name[0]=='\0') {  /* couldn't find the executable in the path, is must be in the current directory */
  304.             strcpy(spawn_str,argv[exec_num]);
  305.             strcat(spawn_str,".EXE");
  306.             _searchenv(spawn_str,"PATH",file_name);  /* get the pathname of the file to execute */
  307.             if(file_name[0]=='\0')  /* couldn't find the executable in the path, is must be in the current directory */
  308.                 getcwd(file_name,_MAX_PATH);
  309.           } /* end if */
  310.       } /* end if */
  311. #ifdef CHECKNULL
  312. /* If we are running in NULL debugging mode, check that */
  313.     if(memcmp(nullbuf,MK_FP(0x0,0x0),1024)) {
  314.         unsigned u;
  315.         unsigned char *s;
  316.  
  317.         puts("NULL overwritten!");
  318.         s=MK_FP(0x0,0x0);
  319.         for(u=0; u<1024; u++,s++)
  320.             if(*s!=nullbuf[u])
  321.                 printf("s=%p, u=%u, val=%u\n",s,u,(unsigned int)*s);
  322.         getch();
  323.       } /* end if */
  324.     else {
  325.         puts("NULL looks fine right before spawning a program");
  326.         getch();
  327.       } /* end else */
  328. #endif
  329.  
  330.     i=spawnvp(P_WAIT,file_name,&argv[exec_num]);   /* call the program which needs the re-direction */
  331. puts("After spawn returns");
  332.     if(i==(-1)) {       /* check for error spawning the process */
  333.         switch(errno) { /* find out what the problem was */
  334.             case E2BIG:     /* argument list too long to child process */
  335.                 puts("Error: bad argument list to child process\n");
  336.                 break;
  337.  
  338.             case EINVAL:    /* the execution mode was invalid */
  339.                 puts("Error: invalid execution mode for child process\n");
  340.                 break;
  341.  
  342.             case ENOENT:    /* file not found to spawn */
  343.                 puts("Error: child process not found\n");
  344.                 break;
  345.  
  346.             case ENOEXEC:   /* not an executable file */
  347.                 puts("Error: child process is not an executable file\n");
  348.                 break;
  349.  
  350.             case ENOMEM:    /* not enough memory to spawn process */
  351.                 puts("Error: not enough memory for child process\n");
  352.                 break;
  353.           } /* end switch() */
  354.       } /* end if */
  355.     timedeinst();   /* remove the timer interrupt handler */
  356.     int14deinst();  /* remove the int 14h handler */
  357. puts("After removing the interrupt handlers");
  358.     netshut();
  359. puts("After shutting down the network");
  360. #endif
  361. #endif
  362.     return(0);
  363. }
  364.  
  365. #ifdef QAK
  366. /*
  367. *    errhandle ()
  368. *
  369. *    Write error messages to the console window
  370. *
  371. */
  372. void errhandle(void )
  373. {
  374.     char *errmsg;
  375.     int i,j;
  376.  
  377.     while(ERR1==Sgetevent(ERRCLASS,&i,&j)) {
  378.         errmsg=neterrstring(j);
  379.         puts(errmsg);
  380.       }
  381. }
  382. #endif
  383.  
  384.