home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 522 / okami12 / msh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-17  |  4.6 KB  |  204 lines

  1. /********************************************************************
  2.  
  3.          Mikro-Shell: Startprogramm fuer Okami-Shell
  4.          
  5.               Version 1.2
  6.  
  7.               WR 1.11.89
  8.               
  9.   Änderungen:
  10.   
  11.   18.02.90  Optional Übergabe der Konfigurationsdatei
  12.   01.03.90  Internes Kommando: echo
  13.   31.03.90  Übergabe mehrerer Konfigurationsdateien
  14.         internes Kommando: incl
  15.   10.05.90  Länge des Para-Strings in Pexec korrekt
  16.         Zeilenumbruch mit \ am Ende einer Zeile
  17.   28.12.90  Kommandozeile mit -c und Kommandos
  18.         internes Kommando: ver
  19.  
  20. *******************************************************************/
  21. static char _M_ID_[]="@(#)Microshell - msh.c";
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <osbind.h>
  26.  
  27. /* Die Include-Datei cmpdat.h wird von einem Okami-Script zum Compiler-
  28. ** Aufruf angelegt und enthält nur eine Zeile:
  29.  
  30.     #define _CMP_DAT "Datum, Uhrzeit"
  31.  
  32. ** Wer diese Datei nicht anlegen will, kann diese Zeile anstelle der
  33. ** jetzt folgenden include-Anweisung benutzen.
  34. */
  35.  
  36. #include <cmpdat.h>
  37.  
  38. /* Ein paar Makros                            */
  39.  
  40. #define StrEqu(a,b)    (!strcmp(a,b))        /* Test: Strings gleich */
  41. #define StrNEqu(a,b,n)    (!strncmp(a,b,n))    /* Test: Strings gleich */
  42. #define StrLast(S)    (*(S+strlen(S)-1))    /* String: letztes char.*/
  43.  
  44. /* Strings mit Programmname und Versionsnummer                */
  45.  
  46. char TPName[]=        "Microshell";
  47. char TPVersion[]=    "1.2";
  48.  
  49. /* Weitere globale Variablen und defines                */
  50.  
  51. char KONFFILE[]=    "msh.inf";    /* Default bei argc==1        */
  52. #define CMDLEN        (3*80)
  53. #define ARGV0        "msh"        /* eigentlich: argv[0]        */
  54.  
  55.  
  56. /************************************************************************/
  57.  
  58. main(argc,argv)
  59. int argc;
  60. char *argv[];
  61. {
  62.   register int i;
  63.   short ExFlag=0;            /* 0: Script, 1: Kommando    */
  64.   
  65.   if (argc==1)                /* keine Parameter        */
  66.     msh(KONFFILE);
  67.   else
  68.     for (i=1;i<argc;i++)
  69.     {
  70.       if (StrEqu(argv[i],"-c"))
  71.       {
  72.     ExFlag=1;
  73.     continue;
  74.       }
  75.       if (ExFlag)
  76.     DoCom(argv[i]);
  77.       else 
  78.     msh(argv[i]);
  79.     }
  80.       
  81. }
  82.  
  83. /************************************************************************
  84.  
  85.     msh: ein MSH-Script ausführen
  86.  
  87. ************************************************************************/
  88. msh(FName)
  89. char *FName;
  90. {
  91.   FILE *FPtr;                /* File-Ptr. Konf.datei     */
  92.   char    St[CMDLEN+1];            /* eingelesene Zeile        */
  93.   char    Para2[CMDLEN+1];        /* Hilfsstring            */
  94.   register char *Para;
  95.   char c;
  96.   
  97.   while (isspace(*FName)) FName++;
  98.  
  99.   if ((FPtr=fopen(FName,"r"))==NULL)
  100.   {
  101.     fputs(ARGV0,stdout);
  102.     fputs(": cannot open ",stdout);
  103.     puts(FName);
  104.     return;
  105.   }
  106.  
  107.   while (fgets(St,3*80,FPtr)!=NULL)
  108.   {
  109.     Para=St;
  110.  
  111.     while (isspace(*Para)) Para++;    /* führende Leerzeichen          */
  112.  
  113.     if (*Para=='\0' || *Para=='\n' || *Para=='#')
  114.       continue;
  115.  
  116.     for (;;)                /* endende Leerzeichen        */
  117.     {
  118.       c=StrLast(Para);
  119.       if (isspace(c))
  120.     StrLast(Para)='\0';
  121.       else
  122.     break;
  123.     }
  124.     
  125.     while(StrLast(Para)=='\\')        /* \ am Ende: Zeilenumbruch    */
  126.     {
  127.       StrLast(Para)='\0';
  128.       fgets(Para2,80,FPtr);
  129.       while (Para2[strlen(Para2)-1]=='\n')
  130.     Para2[strlen(Para2)-1]='\0';
  131.       strcat(Para,Para2);
  132.     }
  133.  
  134.     /* Kommando ausführen */
  135.  
  136.     DoCom(St);
  137.   }
  138.  
  139.   fclose(FPtr);
  140. }
  141.  
  142. /************************************************************************
  143.  
  144.     DoCom: ein MSH-Kommando ausführen
  145.  
  146. ************************************************************************/
  147. DoCom(Para)
  148. char *Para;
  149. {
  150.   char    *Com;
  151.   static short Flag=1;            /* Flag für Meldungen        */
  152.   char    Para2[CMDLEN+1];        /* Hilfsstring            */
  153.  
  154.   if (Para[0]=='-')            /* -: Meldung abschalten    */
  155.   {
  156.     Flag=0;
  157.     return;
  158.   }
  159.   if (Para[0]=='+')            /* +: Meldung einschalten    */
  160.   {
  161.     Flag=1;
  162.     return;
  163.   }
  164.  
  165.   for (Com=Para;*Para && !isspace(*Para);Para++);
  166.   if (*Para!='\0')
  167.     *Para++='\0';
  168.    
  169.   if (StrEqu(Com,"echo"))        /* internes Kommando: echo    */
  170.     puts(Para);
  171.       
  172.   else if (StrEqu(Com,"incl"))        /* internes Kommando: incl    */
  173.     msh(Para);
  174.  
  175.   else if (StrEqu(Com,"ver"))        /* internes Kommando: ver    */
  176.   {
  177.     fputs(TPName,stdout);        /* kein printf, dadurch wird    */
  178.     fputs(" Version ",stdout);        /* der Code zu lang        */
  179.     puts(TPVersion);
  180.     fputs("Compiled ",stdout);
  181.     puts(_CMP_DAT);
  182.   }
  183.     
  184.   else
  185.   {
  186.     /* Kommentarmodus: Kommando ausgeben */
  187.     if (Flag)
  188.     {
  189.       fputs(Com,stdout);
  190.       fputc(' ',stdout);
  191.       puts(Para);
  192.     }
  193.     
  194.     Para2[0]=(char)strlen(Para);
  195.     strcpy(Para2+1,Para);
  196.     if (Pexec(0,Com,Para2,NULL)<0)
  197.     if (Flag)
  198.     {
  199.       fputs(Com,stdout);
  200.       puts(": not found");
  201.     }
  202.   }
  203. }
  204.