home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elvis_1.4.tar.Z / elvis_1.4.tar / shell.c < prev    next >
C/C++ Source or Header  |  1990-12-06  |  4KB  |  232 lines

  1. /* shell.c */
  2.  
  3. /* Author:
  4.  *    Guntram Blohm
  5.  *    Buchenstrasse 19
  6.  *    7904 Erbach, West Germany
  7.  *    Tel. ++49-7305-6997
  8.  *    sorry - no regular network connection
  9.  */
  10.  
  11. /*
  12.  * This file contains a minimal version of a shell for TOS. It allows the
  13.  * setting of an environment, calling programs, and exiting.
  14.  * If you don't have another one, this might be sufficient, but you should 
  15.  * prefer *any* other shell.
  16.  * You may, however, want to set your SHELL environment variable to this
  17.  * shell: it implements the -c switch, which is required by Elvis, and
  18.  * not supported by most other atari shells.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <osbind.h>
  24. extern char *getenv(), *malloc();
  25. extern char **environ;
  26. long _stksize=16384;
  27.  
  28. #define    MAXENV    50
  29.  
  30. struct
  31. {
  32.     char *name;
  33.     char *value;
  34. } myenv[MAXENV];
  35.  
  36. int cmd_set(), cmd_exit();
  37.  
  38. struct buildins
  39. {
  40.     char *name;
  41.     int (*func)();
  42. } buildins[]=
  43. {    "exit", cmd_exit,
  44.     "set", cmd_set,
  45.     0,
  46. };
  47.  
  48. main(argc, argv)
  49.     int argc;
  50.     char **argv;
  51. {
  52.     char buf[128];
  53.     int i;
  54.  
  55.     for (i=0; environ[i] && strncmp(environ[i],"ARGV=",5); i++)
  56.         cmd_set(environ[i]);
  57.     script("profile.sh");
  58.  
  59.     if (argc>1 && !strcmp(argv[1], "-c"))
  60.     {
  61.         buf[0]='\0';
  62.         for (i=2; i<argc; i++)
  63.         {    if (i>2)
  64.                 strcat(buf, " ");
  65.             strcat(buf, argv[i]);
  66.         }
  67.         execute(buf);
  68.     }
  69.     else
  70.         while (fputs("$ ", stdout), gets(buf))
  71.             execute(buf);
  72. }
  73.  
  74. execute(buf)
  75.     char *buf;
  76. {
  77.     char *scan=buf;
  78.     char cmd[80];
  79.     char line[128];
  80.     char env[4096], *ep=env;
  81.     int i;
  82.  
  83.     while (*scan==' ')
  84.         scan++;
  85.     if (!*scan)
  86.         return;
  87.     while (*scan && *scan!=' ')
  88.         scan++;
  89.     if (*scan)
  90.         *scan++='\0';
  91.  
  92.     for (i=0; buildins[i].name; i++)
  93.         if (!strcmp(buf, buildins[i].name))
  94.             return (*buildins[i].func)(scan);
  95.  
  96.     if (!searchpath(buf, cmd))
  97.     {    printf("%s: not found\n", buf);
  98.         return -1;
  99.     }
  100.  
  101.     strcpy(line+1, scan);
  102.     line[0]=strlen(scan);
  103.     for (i=0; i<MAXENV && myenv[i].name; i++)
  104.     {    strcpy(ep, myenv[i].name);
  105.         strcat(ep, "=");
  106.         strcat(ep, myenv[i].value);
  107.         ep+=strlen(ep)+1;
  108.     }
  109.     
  110.     *ep='\0';
  111.  
  112.     return Pexec(0, cmd, line, env);
  113. }
  114.  
  115. searchpath(from, to)
  116.     char *from, *to;
  117. {
  118.     char *path="";
  119.     char *scan;
  120.     char *end;
  121.     char *q;
  122.     int i;
  123.  
  124.     for (i=0; i<MAXENV && myenv[i].name; i++)
  125.         if (!strcmp(myenv[i].name,"PATH"))
  126.             path=myenv[i].value;
  127.     for (scan=from; *scan; scan++)
  128.         if (*scan==':' || *scan=='\\')
  129.         {    path=0;
  130.             break;
  131.         }
  132.     if (!path)
  133.     {    strcpy(to, from);
  134.         end=to+strlen(to);
  135.         strcpy(end, ".prg"); if (try(to)) return 1;
  136.         strcpy(end, ".ttp"); if (try(to)) return 1;
  137.         strcpy(end, ".tos"); if (try(to)) return 1;
  138.         *to='\0'; return 0;
  139.     }
  140.     for (scan=path; *scan; )
  141.     {
  142.         for (q=to; *scan && *scan!=';' && *scan!=','; scan++)
  143.             *q++=*scan;
  144.         if (*scan==';' || *scan==',')
  145.             scan++;
  146.         *q++='\\';
  147.         *q='\0';
  148.         strcpy(q, from);
  149.         end=q+strlen(q);
  150.         strcpy(end, ".prg"); if (try(to)) return 1;
  151.         strcpy(end, ".ttp"); if (try(to)) return 1;
  152.         strcpy(end, ".tos"); if (try(to)) return 1;
  153.     }
  154.     *to='\0';
  155.     return 0;
  156. }
  157.  
  158. try(name)
  159.     char *name;
  160. {
  161.     if (Fattrib(name, 0, 0) < 0)
  162.         return 0;
  163.     return 1;
  164. }
  165.  
  166. cmd_exit()
  167. {
  168.     exit(0);
  169. }
  170.  
  171. cmd_set(line)
  172.     char *line;
  173. {
  174.     char *value;
  175.     int i;
  176.  
  177.     if (!*line)
  178.     {
  179.         for (i=0; i<MAXENV && myenv[i].name; i++)
  180.             printf("%s=%s\n", myenv[i].name, myenv[i].value);
  181.         return 0;
  182.     }
  183.  
  184.     for (value=line; *value && *value!='='; value++)
  185.         ;
  186.     if (!*value)
  187.     {    printf("Usage: set name=var\n");
  188.         return -1;
  189.     }
  190.     *value++='\0';
  191.     doset(line, value);
  192. }
  193.  
  194. doset(line, value)
  195.     char *line, *value;
  196. {
  197.     int i;
  198.  
  199.     for (i=0; i<MAXENV && myenv[i].name && strcmp(myenv[i].name, line); i++)
  200.         ;
  201.     if (i==MAXENV)
  202.     {    printf("No Space\n");
  203.         return -1;
  204.     }
  205.     if (!myenv[i].name)
  206.     {    myenv[i].name=malloc(strlen(line)+1);
  207.         strcpy(myenv[i].name, line);
  208.     }
  209.     if (myenv[i].value)
  210.         free(myenv[i].value);
  211.     myenv[i].value=malloc(strlen(value)+1);
  212.     strcpy(myenv[i].value, value);
  213.     return 0;
  214. }
  215.  
  216. script(name)
  217.     char *name;
  218. {
  219.     FILE *fp;
  220.     char buf[128], *p;
  221.  
  222.     if ((fp=fopen(name, "r"))==0)
  223.         return;
  224.     while (fgets(buf, sizeof buf, fp))
  225.     {
  226.         if ((p=strchr(buf, '\n'))!=0)
  227.             *p='\0';
  228.         execute(buf);
  229.     }
  230.     fclose(fp);
  231. }
  232.