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 / TestProgram5.c < prev    next >
Text File  |  2007-09-19  |  30KB  |  791 lines

  1. code TestProgram5
  2.  
  3.  
  4.            -----------------------------------------------------
  5.            ------                                         ------
  6.            ------  Harry's Serial Driver Test Program     ------
  7.            ------                                         ------
  8.            -----------------------------------------------------
  9.  
  10.  
  11. -----------------------------  main  ---------------------------------
  12.  
  13. var bigBuffer: array [9000] of char = new array of char { 9000 of '?' }
  14.  
  15.   function main ()
  16.     --
  17.     -- For each of the following tests, you should uncomment the call,
  18.     -- compile and run this program, and hand in the output it produces.
  19.     --
  20.  
  21.       -- BasicSerialTest ()
  22.       -- KeyTest ()
  23.       -- EchoTest ()
  24.       -- LineEchoTest ()
  25.       -- EOFTest ()
  26.       -- OpenCloseTerminalTest ()
  27.       -- TerminalErrorTest ()
  28.       Menu ()
  29.  
  30.       Sys_Shutdown ()
  31.     endFunction
  32.  
  33. -----------------------------  BasicSerialTest  ---------------------------------
  34.  
  35.   function BasicSerialTest ()
  36.     --
  37.     -- This function performs a basic test of the Read and Write syscalls,
  38.     -- when they are directed to the serial "terminal" device.
  39.     --
  40.  
  41.       var fd, i: int
  42.           ch: char
  43.           UserBuffer: array [10] of char = new array of char { 10 of '?' }
  44.  
  45.       print ("\n==========  BasicSerialTest  ==========\n\n")
  46.       print ("This test should be run in raw mode.\n\n")
  47.  
  48.       fd = Sys_Open ("terminal")
  49.       if fd < 0
  50.         printIntVar ("\n**************************  ERROR: Problems with open, fd", fd)
  51.       endIf
  52.  
  53.       -- Try reading a single character...
  54.       print ("Hit the \"a\" key.  Do not hit ENTER or RETURN...\n")
  55.       ch = 'x'
  56.       i = Sys_Read (fd, &ch, 1)
  57.       -- Make sure the syscall returns 1...
  58.       if i == 1
  59.         print ("Returned value is correct.\n")
  60.       else
  61.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 1; returned value", i)
  62.       endIf
  63.       -- Make sure the buffer was modified correctly...
  64.       if ch == 'a'
  65.         print ("The buffer was updated correctly.\n")
  66.       elseIf ch == 'x'
  67.         print ("\n**************************  ERROR: The user-level buffer was not changed  **************************\n")
  68.       else
  69.         printCharVar ("\n**************************  ERROR: Something strange was stored in the user-level buffer, ch", ch)
  70.       endIf
  71.  
  72.       -- Try reading a several characters...
  73.       print ("\nPlease type \"abc\".  Do not hit ENTER or RETURN...\n")
  74.       StrCopy (&UserBuffer, "0123456789")
  75.       i = Sys_Read (fd, &UserBuffer[2], 3)
  76.       -- Make sure the syscall returns 3...
  77.       if i == 3
  78.         print ("Returned value is correct.\n")
  79.       else
  80.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 3; returned value", i)
  81.       endIf
  82.       -- Make sure the buffer was modified correctly...
  83.       if StrCmp (&UserBuffer, "01abc56789") == 0
  84.         print ("The buffer was updated correctly.\n")
  85.       else
  86.         print ("\n**************************  ERROR: The user-level buffer is not correct  **************************\n")
  87.         print ("              The buffer is \"")
  88.         print (&UserBuffer)
  89.         print ("\"\n")
  90.       endIf
  91.  
  92.       -- Try reading a several characters using 2 syscalls...
  93.       print ("\nPlease type \"hello\".  Do not hit ENTER or RETURN...\n")
  94.       StrCopy (&UserBuffer, "0123456789")
  95.       i = Sys_Read (fd, &UserBuffer[2], 3)
  96.       -- Make sure the first syscall returns 3...
  97.       if i == 3
  98.         print ("Returned value is correct.\n")
  99.       else
  100.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 3; returned value", i)
  101.       endIf
  102.       i = Sys_Read (fd, &UserBuffer[7], 2)
  103.       -- Make sure the second syscall returns 2...
  104.       if i == 2
  105.         print ("Returned value is correct.\n")
  106.       else
  107.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 2; returned value", i)
  108.       endIf
  109.       -- Make sure the buffer was modified correctly...
  110.       if StrCmp (&UserBuffer, "01hel56lo9") == 0
  111.         print ("The buffer was updated correctly.\n")
  112.       else
  113.         print ("\n**************************  ERROR: The user-level buffer is not correct  **************************\n")
  114.         print ("              The buffer is \"")
  115.         print (&UserBuffer)
  116.         print ("\"\n")
  117.       endIf
  118.  
  119.       -- Check that (newline) control-J works correctly...
  120.       print ("\nPlease type control-J.  Do not hit ENTER or RETURN...\n")
  121.       StrCopy (&UserBuffer, "0123456789")
  122.       i = Sys_Read (fd, &UserBuffer[2], 1)
  123.       -- Make sure the syscall returns 1...
  124.       if i == 1
  125.         print ("Returned value is correct.\n")
  126.       else
  127.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 1; returned value", i)
  128.       endIf
  129.       -- Make sure the buffer was modified correctly...
  130.       if StrCmp (&UserBuffer, "01\x0a3456789") == 0
  131.         print ("The buffer was updated correctly.\n")
  132.       else
  133.         print ("\n**************************  ERROR: The user-level buffer is not correct  **************************\n")
  134.         print ("              The buffer is \"")
  135.         print (&UserBuffer)
  136.         print ("\"\n")
  137.       endIf
  138.  
  139.       -- Check that (CR) control-M works correctly...
  140.       print ("\nPlease type control-M.  Do not hit ENTER or RETURN...\n")
  141.       StrCopy (&UserBuffer, "0123456789")
  142.       i = Sys_Read (fd, &UserBuffer[2], 1)
  143.       -- Make sure the syscall returns 1...
  144.       if i == 1
  145.         print ("Returned value is correct.\n")
  146.       else
  147.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 1; returned value", i)
  148.       endIf
  149.       -- Make sure the buffer was modified correctly...
  150.       if StrCmp (&UserBuffer, "01\x0a3456789") == 0
  151.         print ("The buffer was updated correctly.\n")
  152.       elseIf StrCmp (&UserBuffer, "01\x0d3456789") == 0
  153.         print ("The buffer was updated correctly; The kernel failed to translate CR into a NEWLINE.\n")
  154.       else
  155.         print ("\n**************************  ERROR: The user-level buffer is not correct  **************************\n")
  156.         print ("              The buffer is \"")
  157.         print (&UserBuffer)
  158.         print ("\"\n")
  159.       endIf
  160.  
  161.       -- Check that (BACKSPACE) control-H works correctly...
  162.       print ("\nPlease type control-H.  Do not hit ENTER or RETURN...\n")
  163.       StrCopy (&UserBuffer, "0123456789")
  164.       i = Sys_Read (fd, &UserBuffer[2], 1)
  165.       -- Make sure the syscall returns 1...
  166.       if i == 1
  167.         print ("Returned value is correct.\n")
  168.       else
  169.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 1; returned value", i)
  170.       endIf
  171.       -- Make sure the buffer was modified correctly...
  172.       if StrCmp (&UserBuffer, "01\x083456789") == 0
  173.         print ("The buffer was updated correctly.\n")
  174.       else
  175.         print ("\n**************************  ERROR: The user-level buffer is not correct  **************************\n")
  176.         print ("              The buffer is \"")
  177.         print (&UserBuffer)
  178.         print ("\"\n")
  179.       endIf
  180.  
  181.       -- Check that EOF (Control-D) works correctly...
  182.       print ("\nPlease type control-D.  Do not hit ENTER or RETURN...\n")
  183.       StrCopy (&UserBuffer, "0123456789")
  184.       i = Sys_Read (fd, &UserBuffer[2], 5)
  185.       -- Make sure the syscall returns 0...
  186.       if i == 0
  187.         print ("Returned value is correct.\n")
  188.       else
  189.         printIntVar ("\n**************************  ERROR: Returned value from Read was not 0; returned value", i)
  190.       endIf
  191.       -- Make sure the buffer was not modified...
  192.       if StrCmp (&UserBuffer, "0123456789") == 0
  193.         print ("The buffer was not modified, as expected.\n")
  194.       else
  195.         print ("\n**************************  ERROR: The user-level buffer was modified  **************************\n")
  196.         print ("              The buffer is \"")
  197.         print (&UserBuffer)
  198.         print ("\"\n")
  199.       endIf
  200.  
  201.       print ("\n==========  Test Complete  ==========\n\n")
  202.       Sys_Close (fd)
  203.  
  204.     endFunction
  205.  
  206. -----------------------------  KeyTest  ---------------------------------
  207.  
  208.   function KeyTest ()
  209.     --
  210.     -- This function reads a keystroke and then prints the value in hex.
  211.     -- It repeats in a loop until 'q' is entered.
  212.     --
  213.  
  214.       var fd, i: int
  215.           ch: char
  216.  
  217.       print ("\n==========  KeyTest  ==========\n\n")
  218.       print ("This test waits for a single character and then prints the value of that character.\n\n")
  219.       print ("  1. Start by typing \"abcABC123\"\n")
  220.       print ("  2. See what happens with characters like cntl-H (Backspace), cntl-J (NL) and cntl-M (CR).\n")
  221.       print ("  3. See what happens when you hit keys labelled SPACE, ENTER/RETURN, TAB, DEL/BACKSPACE, ARROW KEYS and ESC.\n")
  222.       print ("  4. See what happens when you hit cntl-D (EOF).\n")
  223.       print ("  5. Try this test in cooked mode and in raw mode.\n\n")
  224.       print ("This test will terminate when 'q' is typed.\n\n")
  225.       print ("(To change to cooked mode, type control-C, \"cooked\", and \"g\" to resume execution.\n")
  226.       print ("To change to raw mode, type control-C, ENTER, ENTER, \"raw\", and \"g\" to resume execution.)\n\n")
  227.  
  228.       fd = Sys_Open ("terminal")
  229.       if fd < 0
  230.         printIntVar ("\n**************************  ERROR: Problems with open, fd", fd)
  231.       endIf
  232.  
  233.       while true
  234.         -- Get one character...
  235.         i = Sys_Read (fd, &ch, 1)
  236.         if i == 0
  237.           print ("\n*****  WARNING: Returned value from Read is zero; This should only occur when control-D is typed\n")
  238.         elseIf i != 1
  239.           printIntVar ("\n**************************  ERROR: Returned value from Read is incorrect; returned value", i)
  240.         else
  241.           print ("ch = ")
  242.           printHex (ch)
  243.           print (" (decimal ")
  244.           printInt (ch)
  245.           print (")\n")
  246.           -- printHexVar ("ch", ch)
  247.           -- printCharVar ("ch", ch)
  248.         endIf
  249.         -- If the character was 'q' then return to caller...
  250.         if ch == 'q'
  251.           print ("\n==========  Test Complete  ==========\n\n")
  252.           Sys_Close (fd)
  253.           return
  254.         endIf
  255.       endWhile
  256.  
  257.     endFunction
  258.  
  259. -----------------------------  EchoTest  ---------------------------------
  260.  
  261.   function EchoTest ()
  262.     --
  263.     -- In a loop, this function reads characters and echos them one
  264.     -- at a time.
  265.     --
  266.  
  267.       var fd, i: int
  268.           ch: char
  269.  
  270.       print ("\n==========  EchoTest  ==========\n\n")
  271.  
  272.       print ("This test reads characters from the terminal.  It echoes each character, as it is received.\n\n")
  273.       print ("  1. Start by typing \"abc\\n\"\n")
  274.       print ("  2. See what happens with characters like cntl-H (Backspace), cntl-J (NL) and cntl-M (CR).\n")
  275.       print ("  3. See what happens when you hit keys labelled SPACE, ENTER/RETURN, TAB, DEL/BACKSPACE, and ESC.\n")
  276.       print ("  4. See what happens when you hit cntl-D (EOF).\n")
  277.       print ("  5. Try this test in cooked mode and in raw mode.\n")
  278.       print ("  6. While in raw mode, see what happens with sequences from page 342 in the textbook.\n")
  279.       print ("     For example, try typing these sequences:\n")
  280.       print ("         ESC  [  7  m\n")
  281.       print ("         ESC  [  5  A\n")
  282.       print ("         control-g\n")
  283.       print ("         up-arrow\n\n")
  284.       print ("This test will terminate when 'q' is typed.\n\n")
  285.       print ("(To change to cooked mode, type control-C, \"cooked\", and \"g\" to resume execution.\n")
  286.       print ("To change to raw mode, type control-C, ENTER, ENTER, \"raw\", and \"g\" to resume execution.)\n\n")
  287.  
  288.       fd = Sys_Open ("terminal")
  289.       if fd < 0
  290.         printIntVar ("\n**************************  ERROR: Problems with open, fd", fd)
  291.       endIf
  292.  
  293.       while true
  294.         -- Get one character...
  295.         i = Sys_Read (fd, &ch, 1)
  296.         -- If the returned value is not 1...
  297.         if i == 0
  298.           print ("\n*****  WARNING: Returned value from Read is zero; This should only occur when control-D is typed\n")
  299.         elseIf i != 1
  300.           printIntVar ("\n**************************  ERROR: Returned value from Read is incorrect, i", i)
  301.         else
  302.           -- Write that character...
  303.           i = Sys_Write (fd, &ch, 1)
  304.           if i != 1
  305.             printIntVar ("\n**************************  ERROR: returned value from Write incorrect, i", i)
  306.           endIf
  307.         endIf
  308.         -- If the charactaer was 'q' then return to caller...
  309.         if ch == 'q'
  310.           print ("\n==========  Test Complete  ==========\n\n")
  311.           Sys_Close (fd)
  312.           return
  313.         endIf
  314.       endWhile
  315.  
  316.     endFunction
  317.  
  318. -----------------------------  LineEchoTest  ---------------------------------
  319.  
  320.   const BUFF_MAX = 30
  321.  
  322.   function LineEchoTest ()
  323.     --
  324.     -- In a loop, this function reads whole lines and echos them one
  325.     -- after the next.
  326.     --
  327.  
  328.       var fd, i, j: int
  329.           smallBuff: array [BUFF_MAX] of char = new array of char { BUFF_MAX of '-' }
  330.  
  331.       print ("\n==========  LineEchoTest  ==========\n\n")
  332.       print ("This program reads an entire line of input (up to ")
  333.       printInt (BUFF_MAX)
  334.       print (" characters) into a buffer.\n")
  335.       print ("After this program gets the entire line, it prints it.  Since this program\n")
  336.       print ("does not echo characters as typed, you will not see the characters until after\n")
  337.       print ("the entire line is completed by typing ENTER, when running the emulator in raw\n")
  338.       print ("mode.  In cooked mode, the host (Unix) will echo characters and then, after the\n")
  339.       print ("line is complete, this program will (re) print the line.\n\n")
  340.       print ("  1. Start by typing \"abc\\n\"\n")
  341.       print ("  2. See what happens with characters like cntl-H (Backspace), cntl-J (NL) and cntl-M (CR).\n")
  342.       print ("  3. See what happens when you hit keys labelled SPACE, ENTER/RETURN, TAB, DEL/BACKSPACE, and ESC.\n")
  343.       print ("  4. See what happens when you hit cntl-D (EOF).\n")
  344.       print ("  5. See what happens when the size of the buffer is exceeded.\n")
  345.       print ("  6. In cooked mode, see how the host (Unix) processes editing keys, such as backspace.\n\n")
  346.       print ("This test will terminate when the first character entered is 'q'.\n\n")
  347.       print ("(To change to cooked mode, type control-C, \"cooked\", and \"g\" to resume execution.\n")
  348.       print ("To change to raw mode, type control-C, ENTER, ENTER, \"raw\", and \"g\" to resume execution.)\n\n")
  349.  
  350.       fd = Sys_Open ("terminal")
  351.       if fd < 0
  352.         printIntVar ("**************************  ERROR: Problems with open, fd", fd)
  353.       endIf
  354.  
  355.       while true
  356.         i = Sys_Read (fd, &(smallBuff[0]), BUFF_MAX)
  357.         printIntVar ("\nNumber of characters entered", i)
  358.         j = Sys_Write (fd,  &(smallBuff[0]), i)
  359.         if i != j
  360.           printIntVar ("\n**************************  ERROR: returned value from Write incorrect, j", j)
  361.         endIf
  362.         if smallBuff[0] == 'q'
  363.           print ("\n==========  Test Complete  ==========\n\n")
  364.           Sys_Close (fd)
  365.           return
  366.         endIf
  367.       endWhile
  368.  
  369.     endFunction
  370.  
  371. -----------------------------  EOFTest  ---------------------------------
  372.  
  373.   function EOFTest ()
  374.     --
  375.     -- This function reads a keystroke and then prints the value in hex.
  376.     -- It repeats in a loop until 'q' is entered.
  377.     --
  378.  
  379.       var fd, i: int
  380.           buff: array [6] of char = new array of char { '1','2','3','4','5','6' }
  381.  
  382.       print ("\n==========  EOFTest  ==========\n\n")
  383.       print ("This test should be run in 'raw' mode.\n\n")
  384.       print ("This function tests the handling of control-D.  Control-D is the\n")
  385.       print ("enf-of-file character.  When typed, it should cause an immediate\n")
  386.       print ("return from the Read syscall.  If no other characters have been\n")
  387.       print ("typed first, then the count returned from Read will be zero, which\n")
  388.       print ("many programs will interpret as 'end-of-file'.\n\n")
  389.  
  390.       fd = Sys_Open ("terminal")
  391.       if fd < 0
  392.         printIntVar ("\n**************************  ERROR: Problems with open, fd", fd)
  393.       endIf
  394.  
  395.       print ("Please hit control-D next.  The ENTER key should not be necessary...\n")
  396.       i = Sys_Read (fd, &(buff[0]), 6)
  397.       Check (i, 0)
  398.       if !StrEqual (&buff, "123456")
  399.         print ("**************************  ERROR: buffer was modified  **************************\n")
  400.       endIf
  401.  
  402.       print ("Please type \"abc\" followed by control-D.  The ENTER key should not be necessary...\n")
  403.       i = Sys_Read (fd, &(buff[0]), 6)
  404.       Check (i, 3)
  405.       if !StrEqual (&buff, "abc456")
  406.         print ("**************************  ERROR: buffer is incorrect  **************************\n")
  407.       endIf
  408.  
  409.       print ("\n==========  Test Complete  ==========\n\n")
  410.       Sys_Close (fd)
  411.  
  412.     endFunction
  413.  
  414. -----------------------------  OpenCloseTerminalTest  ---------------------------------
  415.  
  416.   function OpenCloseTerminalTest ()
  417.     --
  418.     -- Perform some tests of opening and closing the terminal.
  419.     --
  420.       var i, j, fd: int
  421.  
  422.       print ("\n==========  OpenCloseTerminalTest  ==========\n\n")
  423.  
  424.     -- Attempt to open and close "terminal" times.
  425.  
  426.       for j = 1 to 10
  427.         print ("Opening 'terminal' 10 times...\n")
  428.         for i = 0 to 9
  429.           fd = Sys_Open ("terminal")
  430.           if fd < 0
  431.             print ("**************************  ERROR: Open returned a negative number (")
  432.             printInt (fd)
  433.             print (") indicating the open failed  **************************\n")
  434.           elseIf fd != i  -- Should get a new fd each time!
  435.             print ("**************************  ERROR: The syscall returns an unexpected fd (")
  436.             printInt (fd)
  437.             print (")  **************************\n")
  438.           endIf
  439.         endFor
  440.  
  441.         -- Close all 10 fileDesctiptors.
  442.         print ("Closing all 10 fileDescriptors...\n")
  443.         for i = 0 to 9
  444.           Sys_Close (i)
  445.         endFor
  446.       endFor
  447.  
  448.       print ("Opening 'terminal' 10 times...\n")
  449.       for i = 0 to 9
  450.         fd = Sys_Open ("terminal")
  451.         if fd < 0
  452.           print ("**************************  ERROR: Open returned a negative number (")
  453.           printInt (fd)
  454.           print (") indicating the open failed  **************************\n")
  455.         elseIf fd != i  -- Should get a new fd each time!
  456.             print ("**************************  ERROR: The syscall returns an unexpected fd (")
  457.             printInt (fd)
  458.             print (")  **************************\n")
  459.         endIf
  460.       endFor
  461.  
  462.       print ("Attempting to open 'terminal' one more time, which should fail.\n")
  463.       fd = Sys_Open ("terminal")
  464.       if fd == -1
  465.         print ("The syscall correctly returns -1.\n")
  466.       else
  467.         print ("**************************  ERROR: Open returned ")
  468.         printInt (fd)
  469.         print ("instead of -1 as expected  **************************\n")
  470.       endIf
  471.  
  472.       -- Close all 10 fileDesctiptors.
  473.       print ("Closing all 10 fileDescriptors...\n")
  474.       for i = 0 to 9
  475.         Sys_Close (i)
  476.       endFor
  477.  
  478.       print ("\n==========  Test Complete  ==========\n\n")
  479.  
  480.     endFunction
  481.  
  482. -----------------------------  TerminalErrorTest  ---------------------------------
  483.  
  484.   function TerminalErrorTest ()
  485.     --
  486.     -- This tests a number of error and strange conditions that can arise with
  487.     -- reading from the terminal.
  488.     --
  489.       var i, fd, p, save: int
  490.           charPtr: ptr to char
  491.  
  492.       print ("\n==========  TerminalErrorTest  ==========\n\n")
  493.       print ("(This test should be run in cooked mode.)\n")
  494.  
  495.       print ("Opening 'terminal' file...\n")
  496.       fd = Sys_Open ("terminal")
  497.       if fd < 0
  498.         print ("**************************  ERROR: Opening terminal  **************************\n")
  499.         return
  500.       endIf
  501.  
  502.       print ("Reading with negative sizeInBytes...\n")
  503.       i = Sys_Read (fd, & bigBuffer[0], -1)
  504.       Check (i, -1)
  505.  
  506.       print ("Reading with negative sizeInBytes...\n")
  507.       i = Sys_Read (fd, & bigBuffer[0], -123123123)
  508.       Check (i, -1)
  509.  
  510.       print ("Reading with a pointer to a page which is read-only, which should be an error...\n")
  511.       print ("Please type \"abc\\n\" next.\n")
  512.       i = Sys_Read (fd, main asPtrTo char, 1)
  513.       -- The spec is unclear about whether 0 or -1 should be returned here...
  514.       if i != -1 && i != 0
  515.         print ("**************************  ERROR: Read returned ")
  516.         printInt (i)
  517.         print ("instead of 0 or -1 as expected  **************************\n")
  518.       endIf
  519.       NextInput (fd, 'a')
  520.  
  521.       print ("Reading with a pointer which isn't in our address space, which should be an error...\n")
  522.       print ("Please type \"xyz\\n\" next.\n")
  523.       i = Sys_Read (fd, 0x0fffffff asPtrTo char, 1)
  524.       -- The spec is unclear about whether 0 or -1 should be returned here...
  525.       if i != -1 && i != 0
  526.         print ("**************************  ERROR: Read returned ")
  527.         printInt (i)
  528.         print ("instead of 0 or -1 as expected  **************************\n")
  529.       endIf
  530.       NextInput (fd, 'x')
  531.  
  532.       -- Next get a pointer to the last word in our address space.
  533.       -- (This is done by first finding the page containing the stack.
  534.       p = ((&fd) asInteger & 0xffffe000) | 0x00001ffc
  535.  
  536.       -- Next, try a read of 5 bytes to the last word of our address space.
  537.       -- The first 4 bytes should be okay but then there should be a problem.
  538.       -- The OS should either
  539.       --        Read 4 bytes and return 4
  540.       --        Read 0 bytes and return -1
  541.       -- (The spec is not clear about which should happen.)
  542.       print ("Reading with a pointer which is near the end of our address space...\n")
  543.       print ("Please type \"123456\\n\" next.\n")
  544.       save = *(p asPtrTo int)
  545.       i = Sys_Read (fd, p asPtrTo char, 5)
  546.       if i == 4
  547.         if 0x31323334 != *(p asPtrTo int)
  548.           print ("**************************  ERROR: Return code is 4, but did not set the last 4 bytes correctly  **************************\n")
  549.         endIf
  550.         NextInput (fd, '5')
  551.       elseIf i == -1
  552.         if save != *(p asPtrTo int)
  553.           print ("**************************  ERROR: Return code is -1 but the last 4 bytes have been altered  **************************\n")
  554.         endIf
  555.         NextInput (fd, '1')
  556.       else
  557.         Check (i, -1)
  558.       endIf
  559.       *(p asPtrTo int) = save
  560.  
  561.       -- Set 'p' to point to somewhere in bigBuffer which happens to be on
  562.       -- a page boundary.
  563.       p = ((&bigBuffer[8999]) asInteger - 10) & 0xffffe000
  564.       charPtr = (p-3) asPtrTo char
  565.       -- printHexVar ("&bigBuffer[0]", (&bigBuffer[0]) asInteger)
  566.       -- printHexVar ("&bigBuffer[8999]", (&bigBuffer[8999]) asInteger)
  567.       -- printHexVar ("charPtr", charPtr asInteger)
  568.  
  569.       print ("Reading with a pointer that crosses a page boundary...\n")
  570.       print ("Please type \"abcdef\\n\" next.\n")
  571.       MemoryCopy ((charPtr-3) asInteger, (& "QWEabcdef\nRTY"[0]) asInteger, 13)
  572.       i = Sys_Read (fd, charPtr, 7)
  573.       Check (i, 7)
  574.       if ! MemoryEqual (charPtr-3, &("QWEabcdef\nRTY" [0]), 13)
  575.         print ("**************************  ERROR: The characters were not stored in memory correctly  **************************\n")
  576.       endIf
  577.  
  578.       print ("Writing with negative sizeInBytes...\n")
  579.       i = Sys_Write (fd, & bigBuffer[0], -1)
  580.       Check (i, -1)
  581.  
  582.       print ("Writing with negative sizeInBytes...\n")
  583.       i = Sys_Write (fd, & bigBuffer[0], -123123123)
  584.       Check (i, -1)
  585.  
  586.       print ("Writing with a pointer that crosses a page boundary...\n")
  587.       print ("\n\n==== This should print \"GREETINGS\" next ====\n")
  588.       print ("                        ")
  589.       MemoryCopy (charPtr asInteger, (& "GREETINGS"[0]) asInteger, 9)
  590.       i = Sys_Write (fd, charPtr, 9)
  591.       nl ()
  592.       Check (i, 9)
  593.  
  594.       print ("Writing with a pointer to a page which is read-only, which should be okay...\n")
  595.       print ("\n\n==== This should print \"KERNEL CODE\" next ====\n")
  596.       print ("                        ")
  597.       i = Sys_Write (fd, & ("KERNEL CODE"[0]), 11)
  598.       nl ()
  599.       if i != 11
  600.         print ("**************************  ERROR: Write returned ")
  601.         printInt (i)
  602.         print ("instead of 11 as expected  **************************\n")
  603.       endIf
  604.  
  605.       print ("\nWriting with a pointer which isn't in our address space, which should be an error...\n")
  606.       i = Sys_Write (fd, 0x0fffffff asPtrTo char, 1)
  607.       -- The spec is unclear about whether 0 or -1 should be returned here...
  608.       if i != -1 && i != 0
  609.         print ("**************************  ERROR: Write returned ")
  610.         printInt (i)
  611.         print ("instead of 0 or -1 as expected  **************************\n")
  612.       else
  613.         print ("Okay.\n")
  614.       endIf
  615.  
  616.       Sys_Close (fd)
  617.  
  618.       print ("\n==========  Test Complete  ==========\n\n")
  619.  
  620.     endFunction
  621.  
  622. -----------------------------  Check  ---------------------------------
  623.  
  624.   function Check (i, expectedVal: int)
  625.     --
  626.     -- This function is passed the return code from a syscall and
  627.     -- the expected value.  It prints an error message if it is incorrect.
  628.     --
  629.       if i == expectedVal
  630.         print ("Okay.\n")
  631.       else
  632.         print ("**************************  ERROR: Return value from syscall (")
  633.         printInt (i)
  634.         print (") is incorrect  **************************\n")
  635.       endIf
  636.       
  637.     endFunction
  638.  
  639. -----------------------------  NextInput  ---------------------------------
  640.  
  641.   function NextInput (fd: int, expectedChar: char)
  642.     --
  643.     -- This function makes sure the next character to be read from fileDescriptor
  644.     -- 'fd' is 'expectedChar', and prints an error if the next input character
  645.     -- is not what is expected.  It then reads the rest of the line.
  646.     --
  647.       var i: int
  648.           inputChar: char
  649.           buffer: array [100] of char = new array of char { 100 of '?' }
  650.  
  651.       -- Read the next character...
  652.       i = Sys_Read (fd, & inputChar, 1)
  653.       if i != 1
  654.         print ("**************************  ERROR: Return value from Read (")
  655.         printInt (i)
  656.         print (") is incorrect  **************************\n")
  657.       endIf
  658.  
  659.       -- Make sure it matches the 'expectedChar'...
  660.       if inputChar == expectedChar
  661.         print ("Okay.\n")
  662.       else
  663.         print ("**************************  ERROR: The next input character is expected to be \'")
  664.         printChar (expectedChar)
  665.         print ("\' but was \'")
  666.         printChar (inputChar)
  667.         print ("\' instead  **************************\n")
  668.       endIf
  669.  
  670.       -- If the last character read ws not \n, then readin the rest of the line...
  671.       if inputChar != '\n'
  672.         i = Sys_Read (fd, & buffer[0], buffer arraySize)
  673.       endIf
  674.       
  675.     endFunction
  676.  
  677. -----------------------------  Menu  ---------------------------------
  678.  
  679.   const stdin  = 0
  680.         stdout = 1
  681.  
  682.   function Menu ()
  683.     --
  684.     -- This function prints a menu and reads in a selection.  It then
  685.     -- executes the test in question.
  686.     --
  687.       var fd, pid, i: int
  688.           c: char
  689.       print ("Opening 'stdin' and 'stdout'...\n")
  690.       fd = Sys_Open ("terminal")
  691.       Check (fd, stdin)
  692.       fd = Sys_Open ("terminal")
  693.       Check (fd, stdout)
  694.       Print ("This menu works best in raw mode.\n\n")
  695.       while true
  696.         Print ("====================  MENU  ====================\n")
  697.         Print ("  1  -  BasicSerialTest\n")
  698.         Print ("  2  -  KeyTest\n")
  699.         Print ("  3  -  EchoTest\n")
  700.         Print ("  4  -  LineEchoTest\n")
  701.         Print ("  5  -  EOFTest\n")
  702.         Print ("  6  -  OpenCloseTerminalTest\n")
  703.         Print ("  7  -  TerminalErrorTest\n")
  704.         Print ("  8  -  Shell\n")
  705.         Print ("  9  -  Quit\n")
  706.         Print ("Please select by number: ")
  707.         c = GetChar ()
  708.         PutChar (c)
  709.         PutChar ('\n')
  710.         switch c
  711.           case '1': 
  712.                BasicSerialTest ()
  713.                break
  714.           case '2': 
  715.                KeyTest ()
  716.                break
  717.           case '3': 
  718.                EchoTest ()
  719.                break
  720.           case '4': 
  721.                LineEchoTest ()
  722.                break
  723.           case '5': 
  724.                EOFTest ()
  725.                break
  726.           case '6':
  727.                Sys_Close (stdin)
  728.                Sys_Close (stdout)
  729.                OpenCloseTerminalTest ()
  730.                fd = Sys_Open ("terminal")
  731.                Check (fd, stdin)
  732.                fd = Sys_Open ("terminal")
  733.                Check (fd, stdout)
  734.                break
  735.           case '7': 
  736.                TerminalErrorTest ()
  737.                break
  738.           case '8': 
  739.                pid = Sys_Fork ()
  740.                if pid == 0
  741.                  i = Sys_Exec ("sh")
  742.                  Print ("Unable to execute shell\n")
  743.                  Sys_Exit (-1)
  744.                else
  745.                  i = Sys_Join (pid)
  746.                endIf
  747.                break
  748.           case '9': 
  749.                Sys_Shutdown ()
  750.           default:
  751.                Print ("Invalid entry; enter a single digit.\n")
  752.                break
  753.         endSwitch
  754.       endWhile
  755.  
  756.     endFunction
  757.  
  758. -----------------------------  Print  ---------------------------------
  759.  
  760.   function Print (str: String)
  761.     --
  762.     -- This function is passed a string.  It prints it to stdout.
  763.     --
  764.       var i: int
  765.       i = Sys_Write (stdout, &str[0], str arraySize)     
  766.     endFunction
  767.  
  768. -----------------------------  GetChar  ---------------------------------
  769.  
  770.   function GetChar () returns char
  771.     --
  772.     -- This function reads a single character from stdin and returns it.
  773.     --
  774.       var c: char
  775.           i: int
  776.       i = Sys_Read (stdout, &c, 1)
  777.       return c    
  778.     endFunction
  779.  
  780. -----------------------------  PutChar  ---------------------------------
  781.  
  782.   function PutChar (c: char)
  783.     --
  784.     -- This function writes a single character to stdout.
  785.     --
  786.       var i: int
  787.       i = Sys_Write (stdout, &c, 1)
  788.     endFunction
  789.  
  790. endCode
  791.