home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / telecomm / uemlsrc / shell.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  13KB  |  331 lines

  1. /* shell.c contains the routines to allow exec calls.  If there were a
  2.  * small command interpreter somewhere, we could do a real spawn.
  3.  * If you compile this to run from a shell, undefine GEMLOAD.  Since
  4.  * shells eat memory, you can't give as much back to the OS.  If you
  5.  * will be using this from GEM, give back 256K and you'll be able to
  6.  * run just about any program from within the editor.
  7.  */
  8.  
  9. #include "shell.h"
  10.  
  11. #ifdef  GEMLOAD
  12. unsigned long _STKSIZ = -262144;        /* give back 256K to the OS */
  13. #else
  14. unsigned long _STKSIZ = -147456;        /* give back 144K to the OS */
  15. #endif
  16.  
  17. char cline[MAXINPUT * 2];       /* holds the command line and other things */
  18. char pnam[MAXINPUT+1];          /* program name */
  19. char source[MAXINPUT+1];        /* name of file for aliasing */
  20. char path[MAXPATH+1];           /* current path string  */
  21. int delete = TRUE;              /* on/off for above     */
  22. int dolink = 0;                 /* link a program       */
  23. int temdrv;
  24. int curdrv;
  25. ALITAB *aheadp = NULL;          /* alias header */
  26.  
  27. shell(f, n)
  28. register int f, n;
  29. {
  30.         extern char *alias(), *index(), *rindex();
  31.         register char *ptr;
  32.  
  33.         if ((f=mlreply("!: ",cline,MAXINPUT))!=TRUE)
  34.                 return(f);
  35.         if ((ptr=index(cline,' '))!=(char *)NULL)
  36.                 {
  37.                 *ptr = '\0';
  38.                 strcpy(pnam,cline);
  39.                 strcpy(&cline[1],++ptr);
  40.                 }
  41.         else
  42.                 {
  43.                 strcpy(pnam,cline);
  44.                 strcpy(&cline[1]," ");
  45.                 }
  46.         if (pnam[0] == '~')
  47.                 parsefn(pnam);
  48.         if (alias(source,pnam)!=(char *)NULL)
  49.                 strcpy(pnam,source);
  50.         /* check for file type (prg) and append if not there */
  51.         if ((ptr=rindex(pnam,'\\'))!=(char *)NULL)      /* don't be fooled */
  52.                 {                                       /* by \foo.dir\bar */
  53.                 if (!index(ptr,'.'))
  54.                         strcat(ptr,".prg");
  55.                 }
  56.         else if (rindex(pnam,'.')==(char *)NULL)
  57.                 strcat(pnam,".prg");
  58.         if (cline[1] == '~')
  59.                 parsefn(&cline[1]);
  60.         while((ptr=index(&cline[1], '~'))!=(char *)NULL)
  61.                 if (!parsefn(ptr))
  62.                         break;
  63.         cline[0] = (char)strlen(&cline[1]);
  64.         ttclose();      /* reset terminal characteristics */
  65.         ttputc(0x1b); ttputc('E');
  66.         Setexc(2,buserr);       /* use default exception handlers */
  67.         Setexc(3,adderr);
  68.         f = (int)Pexec(0,pnam,cline,0L);
  69.         Setexc(2,&errexit);     /* reset local handlers */
  70.         Setexc(3,&errexit);
  71.         mtwrite("[Type a <CR>]");
  72.         ttgetc();
  73.         ttopen();       /* turn on uemail screen */
  74.         if (f == FALSE && n == HUGE)    /* called from term */
  75.                 ;
  76.         else
  77.                 {
  78.                 sgarbf = TRUE;  /* force full redraw */
  79.                 upmode();       /* set colors */
  80.                 update();       /* fix screen */
  81.                 }
  82.         mlwrite("Return from %s = %d",pnam,f);
  83.         return(TRUE);
  84. }
  85.  
  86. /* read in the cc.ini file if it's on the default drive.  Called once at
  87.  * editor startup and later by command.  Bound to CTRL-C.
  88.  */
  89. commfil(f,n)
  90. register int f,n;
  91. {
  92.         FILE *fopen();
  93.         register FILE *fp;
  94.         char *fgets(),*index();
  95.         char *ptr;
  96.         static char line[MAXINPUT+1];
  97.  
  98.         /* for the call from main */
  99.         if (f == FALSE && n == HUGE)
  100.                 {
  101.                 if ((fp=fopen("cc.ini","r")) == NULL)
  102.                         {
  103.                         Dgetpath(path,0);
  104.                         mlwrite("Cannot open %s\\cc.ini",path);
  105.                         return(FALSE);
  106.                         }
  107.                 }
  108.         else
  109.                 {
  110.                 if ((f=mlreply("Command file: ",line,MAXINPUT))!=TRUE)
  111.                         return(f);
  112.                 if (line[0] == '~')
  113.                         parsefn(line);
  114.                 if ((fp=fopen(line, "r"))==NULL)
  115.                         {
  116.                         mlwrite("Cannot open %s",line);
  117.                         return(FALSE);
  118.                         }
  119.                 }
  120.         while (fgets(line,MAXINPUT,fp))
  121.                 {
  122.                 /* kill trailing whitespace */
  123.                 if ((ptr=index(line,'\n'))!=(char *)NULL)
  124.                         *ptr = '\0';
  125.                 if ((ptr=index(line,' '))!=(char *)NULL)
  126.                         *ptr = '\0';
  127.                 if ((ptr=index(line,'\t'))!=(char *)NULL)
  128.                         *ptr = '\0';
  129.                 /* terminate alias and get address of path */
  130.                 if ((ptr=index(line,'='))!=(char *)NULL)
  131.                         *ptr = '\0';
  132.                 else    /* improper line */
  133.                         continue;
  134.                 ++ptr;  /* now equals &path[0] */
  135.                 /* test two cases that aren't easy in a switch */
  136.                 if (strcmp(line,"F10")==NULL)
  137.                         {
  138.                         bindkey((SPEC|'D'),ptr);
  139.                         continue;
  140.                         }
  141.                 if (strcmp(line,"NENTER")==NULL)
  142.                         {
  143.                         bindkey((SPEC|'r'),ptr);
  144.                         continue;
  145.                         }
  146.                 f = strlen(line);
  147.                 n = strlen(ptr);
  148.                 /* bind function keys */
  149.                 if (line[0] == 'F' && f == 2)
  150.                         {
  151.                         switch(line[1])
  152.                                 {
  153.                                 case '1':
  154.                                         bindkey((SPEC|';'),ptr);
  155.                                         break;
  156.                                 case '2':
  157.                                         bindkey((SPEC|'<'),ptr);
  158.                                         break;
  159.                                 case '3':
  160.                                         bindkey((SPEC|'='),ptr);
  161.                                         break;
  162.                                 case '4':
  163.                                         bindkey((SPEC|'>'),ptr);
  164.                                         break;
  165.                                 case '5':
  166.                                         bindkey((SPEC|'?'),ptr);
  167.                                         break;
  168.                                 case '6':
  169.                                         bindkey((SPEC|'@'),ptr);
  170.                                         break;
  171.                                 case '7':
  172.                                         bindkey((SPEC|'A'),ptr);
  173.                                         break;
  174.                                 case '8':
  175.                                         bindkey((SPEC|'B'),ptr);
  176.                                         break;
  177.                                 case '9':
  178.                                         bindkey((SPEC|'C'),ptr);
  179.                                         break;
  180.                                 default:
  181.                                         break;
  182.                                 }
  183.                         }
  184.                 /* or maybe a number pad key */
  185.                 else if (line[0] == 'N' && f == 2)
  186.                         {
  187.                         switch(line[1])
  188.                                 {
  189.                                 case '(':
  190.                                         bindkey((SPEC|'c'),ptr);
  191.                                         break;
  192.                                 case ')':
  193.                                         bindkey((SPEC|'d'),ptr);
  194.                                         break;
  195.                                 case '/':
  196.                                         bindkey((SPEC|'e'),ptr);
  197.                                         break;
  198.                                 case '*':
  199.                                         bindkey((SPEC|'f'),ptr);
  200.                                         break;
  201.                                 case '7':
  202.                                         bindkey((SPEC|'g'),ptr);
  203.                                         break;
  204.                                 case '8':
  205.                                         bindkey((SPEC|'h'),ptr);
  206.                                         break;
  207.                                 case '9':
  208.                                         bindkey((SPEC|'i'),ptr);
  209.                                         break;
  210.                                 case '-':
  211.                                         bindkey((SPEC|'J'),ptr);
  212.                                         break;
  213.                                 case '4':
  214.                                         bindkey((SPEC|'j'),ptr);
  215.                                         break;
  216.                                 case '5':
  217.                                         bindkey((SPEC|'k'),ptr);
  218.                                         break;
  219.                                 case '6':
  220.                                         bindkey((SPEC|'l'),ptr);
  221.                                         break;
  222.                                 case '+':
  223.                                         bindkey((SPEC|'N'),ptr);
  224.                                         break;
  225.                                 case '1':
  226.                                         bindkey((SPEC|'m'),ptr);
  227.                                         break;
  228.                                 case '2':
  229.                                         bindkey((SPEC|'n'),ptr);
  230.                                         break;
  231.                                 case '3':
  232.                                         bindkey((SPEC|'o'),ptr);
  233.                                         break;
  234.                                 case '.':
  235.                                         bindkey((SPEC|'q'),ptr);
  236.                                         break;
  237.                                 default:
  238.                                         break;
  239.                                 }
  240.                         }
  241.                 /* search for a match */
  242.                 /* check possible alias definitions */
  243.                 else if (insalias(line,ptr)==FALSE)
  244.                                 {
  245.                                 fclose(fp);
  246.                                 return(FALSE);
  247.                                 }
  248.                 else if (feof(fp))
  249.                         break;
  250.                 }
  251.         fclose(fp);
  252.         return(TRUE);
  253. }
  254.  
  255. insalias(al,pa)
  256. register char *al;
  257. register char *pa;
  258. {
  259.         register int f, n;
  260.         register ALITAB *apt;
  261.         register int found;
  262.  
  263.         found = FALSE;
  264.  
  265.         if (aheadp == NULL)
  266.                 {
  267.                 if ((aheadp=(ALITAB *)malloc(sizeof(ALITAB)))==NULL)
  268.                         {
  269.                         mlwrite("Cannot allocate %d bytes",sizeof(ALITAB));
  270.                         return(FALSE);
  271.                         }
  272.                 if ((aheadp->alias=malloc(5))==NULL)
  273.                         {
  274.                         mlwrite("Alias alloc failure");
  275.                         return(FALSE);
  276.                         }
  277.                 if ((aheadp->value=malloc(11))==NULL)
  278.                         {
  279.                         mlwrite("Alias alloc failure");
  280.                         return(FALSE);
  281.                         }
  282.                 strcpy(aheadp->alias,"home");
  283.                 strcpy(aheadp->value,"c:\\uemail\\");
  284.                 aheadp->a_forw = NULL;
  285.                 }
  286.         n = strlen(pa);
  287.         f = strlen(al);
  288.         apt = aheadp;
  289.         while (apt)
  290.                 {
  291.                 if (strcmp(apt->alias,al)==NULL)
  292.                         {
  293.                         free(apt->value);
  294.                         if((apt->value=malloc(n+1))==NULL)
  295.                                 {
  296.                                 mlwrite("Alias alloc failure %s",pa);
  297.                                 return(FALSE);
  298.                                 }
  299.                         strcpy(apt->value,pa);
  300.                         found == TRUE;
  301.                         }
  302.                 if (apt->a_forw)
  303.                         apt=apt->a_forw;
  304.                 else
  305.                         break;
  306.                 }
  307.         if (found)
  308.                 return(TRUE);
  309.         /* not already bound */
  310.         if ((apt->a_forw=(ALITAB *)malloc(sizeof(ALITAB)))== NULL)
  311.                 {
  312.                 mlwrite("Cannot allocate %d bytes",sizeof(ALITAB));
  313.                 return(FALSE);
  314.                 }
  315.         apt=apt->a_forw;
  316.         apt->a_forw=NULL;
  317.         if ((apt->alias=malloc(f+1))==NULL)
  318.                 {
  319.                 mlwrite("Alias alloc failure %s",al);
  320.                 return(FALSE);
  321.                 }
  322.         if ((apt->value=malloc(n+1))==NULL)
  323.                 {
  324.                 mlwrite("Alias alloc failure %s",pa);
  325.                 return(FALSE);
  326.                 }
  327.         strcpy(apt->alias,al);
  328.         strcpy(apt->value,pa);
  329.         return(TRUE);
  330. }
  331.