home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / OSProject / p8 / sh.c < prev    next >
Text File  |  2007-09-19  |  7KB  |  270 lines

  1. code sh
  2.  
  3.   -----------------------------------
  4.   ----                           ----
  5.   ----    BLITZ Shell Program    ----
  6.   ----                           ----
  7.   -----------------------------------
  8.  
  9.   const stdin  = 0
  10.         stdout = 1
  11.         EOF = '\xFF'
  12.  
  13.   const COMMAND_MAX = 30
  14.  
  15.   var commandLine: array [COMMAND_MAX] of char
  16.  
  17. -----------------------------  main  ---------------------------------
  18.  
  19.   function main ()
  20.     --
  21.     -- This is a simplified version of the shell program found in Unix.
  22.     --
  23.  
  24.       var pid, i: int
  25.           nextPos, len, start: int
  26.           kind: char        -- will be either ' ', '<', '\n', or '>'
  27.           progName, inFile, outFile: array [COMMAND_MAX] of char
  28.           gotCommand, gotInFile, gotOutFile: bool
  29.  
  30.       commandLine = new array of char { COMMAND_MAX of ' ' }
  31.       progName = commandLine
  32.       inFile = commandLine
  33.       outFile = commandLine
  34.  
  35.       Print ("Welcome to the BLITZ Shell Program.\n")
  36.       Print ("  Type 'exit' to terminate.\n")
  37.       Print ("  Type 'cat < help' for more info.\n")
  38.  
  39.       while true
  40.  
  41.         -- Print prompt...
  42.         Print ("% ")
  43.  
  44.         -- Read in a command line...
  45.         gotCommand = false
  46.         gotInFile = false
  47.         gotOutFile = false
  48.         ReadLine ()
  49.  
  50.         -- Run through the command line and find "progName", "inFile", & "outFile"...
  51.         nextPos = 0
  52.         while true
  53.           FindNextWord (&start, &nextPos, &len, &kind)
  54.           if kind == '\n'
  55.             break
  56.           elseIf kind == ' ' && !gotCommand
  57.             GetWord (& progName, start, len)
  58.             gotCommand = true
  59.           elseIf kind == '<' && !gotInFile
  60.             FindNextWord (&start, &nextPos, &len, &kind)
  61.             if kind == ' '
  62.               GetWord (&inFile, start, len)
  63.               gotInFile = true
  64.             else
  65.               Print ("sh: Missing name for redirect.\n")
  66.               gotCommand = false
  67.               break
  68.             endIf
  69.           elseIf kind == '>' && !gotOutFile
  70.             FindNextWord (&start, &nextPos, &len, &kind)
  71.             if kind == ' '
  72.               GetWord (&outFile, start, len)
  73.               gotOutFile = true
  74.             else
  75.               Print ("sh: Missing name for redirect.\n")
  76.               gotCommand = false
  77.               break
  78.             endIf
  79.           else
  80.             Print ("sh: Command line problems\n")
  81.             gotCommand = false
  82.             break
  83.           endIf
  84.         endWhile
  85.  
  86.         -- Make sure we got a command...
  87.         if !gotCommand
  88.           continue
  89.         endIf
  90.         
  91.         -- Check for exit...
  92.         if StrEqual (&progName, "exit")
  93.           Print ("[Shell process terminated]\n")
  94.           Sys_Exit (0)
  95.         endIf
  96.  
  97.         -- Execute the program and wait for it to terminate...
  98.         pid = Sys_Fork ()
  99.         if pid == 0
  100.           if gotInFile
  101.             Sys_Close (stdin)
  102.             i = Sys_Open (&inFile)
  103.             if i != stdin
  104.               Print ("sh: No such input file.\n")
  105.               Sys_Exit (-1)
  106.             endIf
  107.           endIf
  108.           if gotOutFile
  109.             Sys_Close (stdout)
  110.             i = Sys_Open (&outFile)
  111.             if i != stdout
  112.               i = Sys_Open ("terminal")
  113.               Print ("sh: No such output file.\n")
  114.               Sys_Exit (-1)
  115.             endIf
  116.           endIf
  117.           i = Sys_Exec (&progName)
  118.           Print ("sh: Command not found.\n")
  119.           Sys_Exit (-1)
  120.         else
  121.           i = Sys_Join (pid)
  122.         endIf
  123.  
  124.       endWhile
  125.  
  126.     endFunction
  127.  
  128. -----------------------------  ReadLine  ---------------------------------
  129.  
  130.   function ReadLine ()
  131.     --
  132.     -- This function reads in a line of input and it moves the character into
  133.     -- the commandLine buffer, echoing characters as they are typed.  It will
  134.     -- make sure that a \n goes into the commandLine buffer, even if the
  135.     -- user tries to overflow the buffer.
  136.     --
  137.       var i: int
  138.           ch: char
  139.       while true
  140.         ch = GetChar ()
  141.         if ch == EOF
  142.           -- Ignore EOF
  143.         elseIf ch == '\n'
  144.           PutChar (ch)
  145.           commandLine [i] = ch
  146.           i = i + 1
  147.           return
  148.         elseIf ch == '\b' || ch == '\x7f'  -- cntrl-H or "DEL" char on MAC keyboard
  149.           if i > 0
  150.             Print ("\b \b")
  151.             i = i - 1
  152.           else
  153.             PutChar ('\a')  -- bell/alert
  154.           endIf
  155.         else
  156.           PutChar (ch)
  157.           commandLine [i] = ch
  158.           i = i + 1
  159.         endIf
  160.         if i >= COMMAND_MAX-1
  161.           commandLine [i] = '\n'
  162.           PutChar ('\n')
  163.           return
  164.          endIf
  165.       endWhile
  166.     endFunction
  167.  
  168. -----------------------------  Print  ---------------------------------
  169.  
  170.   function Print (str: String)
  171.     --
  172.     -- This function is passed a string.  It prints it to stdout.
  173.     --
  174.       var i: int
  175.       i = Sys_Write (stdout, &str[0], str arraySize)     
  176.     endFunction
  177.  
  178. -----------------------------  GetChar  ---------------------------------
  179.  
  180.   function GetChar () returns char
  181.     --
  182.     -- This function reads a single character from stdin and returns it.
  183.     --
  184.       var c: char
  185.           i: int
  186.       i = Sys_Read (stdin, &c, 1)
  187.       if i == 0
  188.         return EOF
  189.       elseIf i < 0
  190.         print ("Problems in GetChar\n")
  191.       endIf
  192.       return c    
  193.     endFunction
  194.  
  195. -----------------------------  PutChar  ---------------------------------
  196.  
  197.   function PutChar (c: char)
  198.     --
  199.     -- This function writes a single character to stdout.
  200.     --
  201.       var i: int
  202.       i = Sys_Write (stdout, &c, 1)
  203.       if i < 0
  204.         print ("Problems in PutChar\n")
  205.       endIf
  206.  
  207.     endFunction
  208.  
  209. -----------------------------  FindNextWord  ---------------------------------
  210.  
  211.   function FindNextWord (start, pos, len: ptr to int, kind: ptr to char)
  212.     --
  213.     -- This function looks in the command line buffer and finds the next word.
  214.     -- It updates its parameters.
  215.     --
  216.       var ch: char
  217.  
  218.       -- Find the next non-blank...
  219.       while commandLine[*pos] == ' '
  220.         *pos = *pos + 1
  221.       endWhile
  222.  
  223.       *start = *pos
  224.  
  225.       -- If it is < or >...
  226.       if commandLine[*pos] == '<' ||
  227.          commandLine[*pos] == '>'
  228.         *kind = commandLine[*pos]
  229.         *pos = *pos + 1
  230.         *len = 1
  231.         return
  232.       endIf
  233.  
  234.       -- If it is \n...
  235.       if commandLine[*pos] == '\n'
  236.         *kind = '\n'
  237.         *len = 0
  238.         return
  239.       endIf
  240.  
  241.       -- Else it must be a normal word...
  242.       *kind = ' '  -- Normal word
  243.       while true
  244.         ch = commandLine[*pos]
  245.         if ch == ' ' ||
  246.            ch == '\n' ||
  247.            ch == '<' ||
  248.            ch == '>'
  249.           break
  250.         endIf
  251.         *pos = *pos + 1
  252.       endWhile
  253.       *len = *pos - *start
  254.  
  255.     endFunction
  256.  
  257. -----------------------------  GetWord  ---------------------------------
  258.  
  259.   function GetWord (str: String, start, len: int)
  260.     --
  261.     -- This function moves "len" characters from "commandLine" (starting
  262.     -- at "start") into the string "str".  It assumes that "str" is large
  263.     -- enough.
  264.     --
  265.       MemoryCopy ((&str[0]) asInteger, (&commandLine[start]) asInteger, len)
  266.       *(str asPtrTo int) = len
  267.     endFunction
  268.  
  269. endCode
  270.