home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / TERMS / ZMP-SRC.LZH / ZMP.C < prev    next >
Text File  |  2000-06-30  |  10KB  |  441 lines

  1. /*************************************************************************/
  2. /*                                     */
  3. /*        ZMP - A ZMODEM Program for CP/M                 */
  4. /*                                     */
  5. /*    Developed from H. Maney's Heath-specific HMODEM             */
  6. /*        by Ron Murray and Lindsay Allen                 */
  7. /*                                     */
  8. /*************************************************************************/
  9. /*                                     */
  10. /* See separate file zmp-hist.doc for details of modification history     */
  11. /*                                     */
  12. /*************************************************************************/
  13. /*                                                                       */
  14. /*  This source code may be distributed freely without restriction       */
  15. /*  for the express purpose of promoting the use of the ZMODEM           */
  16. /*  file transfer protocol.  Programmers are requested to include        */
  17. /*  credit (in the source code) to the developers for any code           */
  18. /*  incorporated from this program.                                      */
  19. /*                                                                       */
  20. /*  This program was inspired by lmodem.c written by David D. Clark      */
  21. /*  in the November 83 issue of Byte.  Although it bears no resemblance  */
  22. /*  to David's program, I can credit him with sparking my interest in    */
  23. /*  modem programs written in C.                                         */
  24. /*                                 - Hal Maney                           */
  25. /*                                                                       */
  26. /*************************************************************************/
  27. /*                                                                       */
  28. /*  The following files comprise ZMP:                     */
  29. /*                                                                       */
  30. /*    zmp.h            the header file                                  */
  31. /*    zmp.c             the main program Pt.1                            */
  32. /*    zmp2.c            The main program Pt.2                 */
  33. /*    zmterm.c        Terminal overlay                 */
  34. /*    zmterm2.c            "                     */
  35. /*    zmconfig.c        Configuration overlay                 */
  36. /*    zminit.c        Initialisation overlay                 */
  37. /*    zmxfer.c +>>    File transfer overlay (with the next 3)         */
  38. /*    zmxfer2.c        Chuck Forsberg's sz.c modified for cp/m          */
  39. /*    zmxfer3.c                "                 */
  40. /*    zmxfer4.c         Chuck Forsberg's rz.c modified for cp/m          */
  41. /*    zmxfer5.c                "                 */
  42. /*    zzm.c        Chuck Forsberg's zm.c modified for cp/m          */
  43. /*    zzm2.c                "                 */
  44. /*    zmovl.mac        Sample user-dependent overlay source         */
  45. /*    zmodem.h          zmodem header file                               */
  46. /*    zmconfig.c        configuration overlay                            */
  47. /*    zconfig.h         configuration overlay header                     */
  48. /*                                                                       */
  49. /*************************************************************************/
  50.  
  51. #define  MAIN
  52.  
  53. #include "zmp.h"
  54.  
  55. #ifdef   AZTEC_C
  56. #include "libc.h"
  57. #else
  58. #include <stdio.h>
  59. #endif
  60.  
  61. #ifdef HI_TECH_C
  62. #include <signal.h>
  63. #endif
  64.  
  65. char *alloc();
  66.  
  67. main()
  68. {
  69.     static int termcmd;
  70.     short *p, *getvars(), i;
  71.     char *q;
  72.  
  73. #ifdef HI_TECH_C
  74.     signal(SIGINT,SIG_IGN);        /* Ignore control-c's */
  75. #endif
  76.  
  77. #ifdef AZTEC_C
  78.     settop(MAXOVLY);        /* Set top of memory */
  79. #endif
  80.  
  81.     Invokdrive = bdos(GETCUR,NULL) + 'A';
  82.     Invokuser = getuid();
  83.     p = getvars();
  84.     Overdrive = *p++;    /* get du: for overlays etc */
  85.     Overuser = *p++;
  86.     Userover = (char *) *p++;    /* user-defined overlay list */
  87.  
  88.     if (!Overdrive) {    /* use Invokdrive if zero */
  89.         Overdrive = Invokdrive;
  90.         Overuser = Invokuser;
  91.     }
  92.     strcpy(Pathname,Initovly);
  93.     addu(Pathname,Overdrive,Overuser);
  94.     ovloader(Pathname);        /* Do initialisation */
  95.  
  96.    /************** the main loop ************************/
  97.  
  98.    while (TRUE) {
  99.     printf("\nWait..");
  100.     strcpy(Pathname,Termovly);
  101.     addu(Pathname,Overdrive,Overuser);
  102.     termcmd = ovloader(Pathname);    /* Load the TERM overlay */
  103.     printf("\nLoading overlay...\n");
  104.     switch(termcmd) {
  105.  
  106.     case RECEIVE:
  107.     case SEND:
  108.         strcpy(Pathname,Xferovly);
  109.         addu(Pathname,Overdrive,Overuser);
  110.         ovloader(Pathname,termcmd);
  111.         putchar('\007');    /* tell user it's finished */
  112.         mswait(300);
  113.         putchar('\007');
  114.         break;
  115.  
  116. #ifdef HOSTON
  117.             case HOST:
  118.                keep(Lastlog);    /* This will need modifying */
  119.                QuitFlag = FALSE;
  120.                Inhost = TRUE;    /* if host mode is enabled */
  121.                while (!QuitFlag)
  122.                   dohost();
  123.                Inhost = FALSE;
  124.                flush();
  125.                cls();
  126.                startcapture();
  127.                break;
  128. #endif
  129.  
  130.     case CONFIG:
  131.         strcpy(Pathname,Configovly);
  132.         addu(Pathname,Overdrive,Overuser);
  133.         ovloader(Pathname);
  134.         break;
  135.  
  136.     case USER:
  137.         for (i = 0, q = Userover; *q; i++) {
  138.             if (i == Userid) {        /* if it's the one, */
  139.                 strcpy(Pathname, q);
  140.                 addu(Pathname, Overdrive, Overuser);
  141.                 ovloader(Pathname);    /* execute it */
  142.                 break;
  143.             }
  144.             else
  145.                 while (*q++);    /* find the end of this one */
  146.         }
  147.         if (!(*q))
  148.             printf("Overlay %d not defined.\n",Userid);
  149.         break;
  150.  
  151.     default:
  152.         printf("Fatal error in %s.OVR\n",Termovly);
  153.         exit(0);
  154.         break;
  155.             }                            /* end of switch*/
  156.    }    /* end of while */
  157. }  /* end of main */
  158.  
  159.  
  160. char *
  161. grabmem(sizep)         /* grab all available memory */
  162. unsigned *sizep;       /* place to store how much we got */
  163. {
  164.     static char *p,*q;
  165.     static unsigned size;
  166.  
  167. #ifdef HI_TECH_C
  168.     q = alloc(BUFSIZ + 10);    /* Make sure we have enough for disk i/o */
  169. #endif
  170.  
  171. #ifdef AZTEC_C
  172.     q = alloc(BUFSIZ);        /* Ditto */
  173. #endif
  174.  
  175.     size = BUFSTART + 10;        /* allow some overrun */
  176.     while ((p = alloc(size)) == (char *) MEMORY_FULL) {
  177.         size -= 1024;
  178.         if ((size - 10) < 2048) {
  179.             size = 0;
  180.             break;
  181.         }
  182.     }
  183.  
  184. #ifdef DEBUG
  185.     printf("\ngrabmem = %x %d\n",p,size);
  186. #endif
  187.  
  188.     *sizep = size - 10;        /* don't mention the overrun */
  189.     free(q);            /* Free disk i/o space */
  190.     return p;
  191. }
  192.  
  193. getpathname(string)
  194. char *string;
  195. {
  196.     static int c;
  197.     static char *buffer;
  198.  
  199.     buffer = Pathname;
  200.     sprintf(buffer,"\nPlease enter file name%s:  ",string);
  201.     printf(buffer);
  202.     if (!(c=getline(Pathname,PATHLEN)))
  203.         return 0;
  204.     return linetolist();
  205. }
  206.  
  207. linetolist()   /* expand and put Pathnames in Pathlist, return count */
  208. {
  209.     static char *p;
  210.     static int count;
  211.     static char **tempalloc;
  212.  
  213. #ifdef DEBUG
  214.     static int i;
  215. #endif
  216.  
  217.     tempalloc = Pathlist = (char **) alloc(510);
  218.     if (allocerror(tempalloc))
  219.         return 0;
  220.  
  221. #ifdef   DEBUG
  222.     printf("Pathlist = %x\n",Pathlist);
  223. #endif
  224.  
  225.     count = 0;
  226.     Pathlist[count++] = Pathname;
  227.     for (p=Pathname; *p; p++) {         /* break up into substrings */
  228.         if (*p == ' ') {
  229.             *p = '\0';
  230.             while (*++p == ' ');         /* dump extra spaces */
  231.             Pathlist[count++] = p;
  232.         }
  233.     }
  234.  
  235. #ifdef   DEBUG
  236.     printf("\nbefore command\n");
  237.     for (i=0; i < count; i++)
  238.         printf("%d %s\n",i,Pathlist[i]);
  239. #endif
  240.  
  241.     command(&count,&Pathlist);
  242.  
  243. #ifdef   DEBUG
  244.     printf("\nafter command\n");
  245.     for (i=0; i < count; i++)
  246.         printf("%d %s\n",i,Pathlist[i]);
  247. #endif
  248.  
  249.     free(tempalloc);
  250.     return count;
  251. }
  252.  
  253. freepath(n)
  254. int n;
  255. {
  256.     if (n) {
  257.         while (n)
  258.             free(Pathlist[--n]);
  259.         free(Pathlist);
  260.     }
  261. }
  262.  
  263. reset(drive,user)
  264. unsigned drive;
  265. int user;
  266. {
  267.     drive = toupper(drive);
  268.     if (isalpha(drive) && drive <= Maxdrive && user >= 0 && user <= 15) {
  269.         Currdrive = drive;
  270.         bdos(RESET,NULL);
  271.         bdos(SELDSK,(Currdrive-'A')&0xff);
  272.         Curruser = user;
  273.         setuid(user);
  274.     }
  275. }
  276.  
  277. addu(filename,drive,user)
  278. char *filename;
  279. int drive,user;
  280. {
  281.     if (!isin(filename,":") && user >= 0 && user <= 15) {
  282.         strcpy(Buf,filename);
  283.         filename[0] = (char)drive;
  284.         sprintf(filename+1,"%d",user);
  285.         sprintf((user < 10) ? filename+2 : filename+3,":%s",Buf);
  286.     }
  287. }
  288.  
  289. deldrive(filename)
  290. char *filename;
  291. {
  292.     char *i, *index();
  293.  
  294.     if ((i = index(filename,':')) != (char *) NULL)
  295.         strcpy(filename,i+1);
  296. }
  297.  
  298. dio()          /* direct console port inp when bdos is too slow */
  299. {
  300.     return bios(2 + 1);
  301. }
  302.  
  303. chrin()        /* Direct console input which repeats character */
  304. {
  305.     return bdos(CONIN);
  306. }
  307.  
  308. getch()
  309. {
  310.    return bdos(DIRCTIO,INPUT);
  311. }
  312.  
  313. flush()
  314. {
  315.     while(bdos(GCS,NULL))          /*clear type-ahead buffer*/
  316.         bdos(CONIN,NULL);
  317.     getch();           /*and anything else*/
  318. }
  319.  
  320. purgeline()
  321. {
  322.     while (minprdy())              /*while there are characters...*/
  323.         mcharinp();             /*gobble them*/
  324. }
  325.  
  326. openerror(chan,fname,test)
  327. int chan,test;
  328. char *fname;
  329. {
  330.     int result;
  331.  
  332.     if (result = (chan == test)) {
  333.         printf("\n\nERROR - Cannot open %s\n\n",fname);
  334.         wait(3);
  335.     }
  336.     return result;
  337. }
  338.  
  339. wrerror(fname)
  340. char *fname;
  341. {
  342.     printf("\n\nERROR - Cannot write to %s\n\n",fname);
  343.     wait(3);
  344. }
  345.  
  346. #ifdef HI_TECH_C
  347.  
  348. char *alloc(cnt)
  349. int cnt;
  350. {
  351.     char *malloc();
  352.  
  353.     return malloc(cnt);
  354. }
  355. #endif
  356.  
  357. allocerror(p)
  358. char *p;
  359. {
  360.     static int status;
  361.  
  362.     if (status = (p == (char *) MEMORY_FULL))
  363.         perror("Memory allocation failure");
  364.     return status;   
  365. }
  366.  
  367. perror(string)
  368. char *string;
  369. {
  370.     printf("\007\nERROR - %s\n\n",string);
  371.     wait(3);
  372. }
  373.  
  374.  
  375. kbwait(seconds)
  376. unsigned seconds;
  377. {
  378.     static unsigned t;
  379.     static int c;
  380.  
  381.     t = seconds * 10;
  382.     while(!(c=getch()) && (t--))
  383.     MSWAIT(100);
  384.     return ((c & 0xff) == ESC);
  385. }
  386.  
  387. readstr(p,t)
  388. char *p;
  389. int t;
  390. {
  391.     static int c;
  392.  
  393.     t *= 10;                /* convert to tenths */
  394.     flush();
  395.     while (((c=readline(t)) != CR) && (c != TIMEOUT)) {
  396.         if (c != LF)
  397.             *p++ = c;
  398.     }
  399.     *p = 0;
  400.     return c;
  401. }
  402.  
  403. isin(received,expected)
  404. char *received, *expected;
  405. {  
  406.     return (stindex(received,expected) != -1);
  407. }
  408.  
  409. report(row,string)
  410. int row;
  411. char *string;
  412. {
  413.     LOCATE(row,RPTPOS);
  414.     printf(string);
  415. }
  416.  
  417. mstrout(string,echo)
  418. char *string;
  419. int echo;             /* echo flag means send to screen also */
  420. {
  421.     static char c;
  422.  
  423.     while (c = *string++) {
  424.         if ((c == RET) || (c == '\n')) {      /* RET is a ! */
  425.             mcharout(CR);
  426.             mcharout(LF);
  427.             c = '\n';
  428.         }
  429.         else if (c == WAITASEC)
  430.             wait(1);
  431.         else
  432.             mcharout(c);
  433.         if (echo)
  434.             putchar(c);
  435.     }
  436.     MSWAIT(100);      /* wait 100 ms */
  437.     purgeline();
  438. }
  439.  
  440. /*            End of MAIN module File 1            */
  441.