home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI 1.4.1 / GUSI / Examples / GUSITest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  4.4 KB  |  244 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. File        :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSITest.c        -    Common testing gear
  4. Author    :    Matthias Neeracher <neeri@iis.ethz.ch>
  5. Language    :    MPW C
  6.  
  7. $Log: GUSITest.c,v $
  8. Revision 1.1  1994/02/25  02:47:57  neeri
  9. Initial revision
  10.  
  11. Revision 0.2  1992/09/20  00:00:00  neeri
  12. Allow empty lines & comments
  13.  
  14. Revision 0.1  1992/09/08  00:00:00  neeri
  15. Factor out more common code
  16.  
  17. *********************************************************************/
  18.  
  19. #include <Memory.h>
  20. #include <QuickDraw.h>
  21. #include <GUSITest.h>
  22. #include <Types.h>
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <string.h>
  26. #include <sys/errno.h>
  27.  
  28. Boolean     HellHoundOnMyTrail = true;            /* Gotta keep on moving */
  29. char        infilename[200];
  30. char *    inputfrom;
  31. FILE *    input;
  32. int        inputline;
  33. CmdDef     commands[NROFCMDS];
  34.     
  35. void Help(char, char, const char *)
  36. {
  37.     char ch1,ch2;
  38.     
  39.     printf("Commands are:\n\n");
  40.     
  41.     for (ch1 = 'a'; ch1 <= 'z'; ++ch1)
  42.         for (ch2 = 0; ch2 <= 'z'; ch2 ? ++ch2 : (ch2 = 'a'))
  43.             if (HELPMSG(ch1,ch2))
  44.                 printf(
  45.                     "\t%c%c %-25s -- %s\n", 
  46.                     ch1, 
  47.                     ch2 ? ch2 : ' ', 
  48.                     USAGE(ch1,ch2), 
  49.                     HELPMSG(ch1,ch2));
  50.     
  51.     printf("\n");
  52. }
  53.  
  54. void Where()
  55. {
  56.     if (inputfrom)
  57.         printf("File '%s'; Line %d\n", inputfrom, inputline);
  58. }
  59.  
  60. void Prompt()
  61. {
  62.     extern int StandAlone;
  63.  
  64.     if (!inputfrom)
  65.         printf("[%d]%c", inputline, StandAlone ? ' ' : '\n');
  66. }
  67.  
  68. #define CASE(code)    case code: return #code
  69.  
  70. const char * Explain()
  71. {
  72.     switch (errno) {
  73.     CASE(EPERM);
  74.     CASE(ENOENT);
  75.     CASE(ESRCH);
  76.     CASE(EINTR);
  77.     CASE(EIO);
  78.     CASE(ENXIO);
  79.     CASE(E2BIG);
  80.     CASE(ENOEXEC);
  81.     CASE(EBADF);
  82.     CASE(ECHILD);
  83.     CASE(EDEADLK);
  84.     CASE(ENOMEM);
  85.     CASE(EACCES);
  86.     CASE(EFAULT);
  87.     CASE(ENOTBLK);
  88.     CASE(EBUSY);
  89.     CASE(EEXIST);
  90.     CASE(EXDEV);
  91.     CASE(ENODEV);
  92.     CASE(ENOTDIR);
  93.     CASE(EISDIR);
  94.     CASE(EINVAL);
  95.     CASE(ENFILE);
  96.     CASE(EMFILE);
  97.     CASE(ENOTTY);
  98.     CASE(ETXTBSY);
  99.     CASE(EFBIG);
  100.     CASE(ENOSPC);
  101.     CASE(ESPIPE);
  102.     CASE(EROFS);
  103.     CASE(EMLINK);
  104.     CASE(EPIPE);
  105.     CASE(EDOM);
  106.     CASE(ERANGE);
  107.     CASE(EWOULDBLOCK);
  108.     CASE(EINPROGRESS);
  109.     CASE(EALREADY);
  110.     CASE(ENOTSOCK);
  111.     CASE(EDESTADDRREQ);
  112.     CASE(EMSGSIZE);
  113.     CASE(EPROTOTYPE);
  114.     CASE(ENOPROTOOPT);
  115.     CASE(EPROTONOSUPPORT);
  116.     CASE(ESOCKTNOSUPPORT);
  117.     CASE(EOPNOTSUPP);
  118.     CASE(EPFNOSUPPORT);
  119.     CASE(EAFNOSUPPORT);
  120.     CASE(EADDRINUSE);
  121.     CASE(EADDRNOTAVAIL);
  122.     CASE(ENETDOWN);
  123.     CASE(ENETUNREACH);
  124.     CASE(ENETRESET);
  125.     CASE(ECONNABORTED);
  126.     CASE(ECONNRESET);
  127.     CASE(ENOBUFS);
  128.     CASE(EISCONN);
  129.     CASE(ENOTCONN);
  130.     CASE(ESHUTDOWN);
  131.     CASE(ETOOMANYREFS);
  132.     CASE(ETIMEDOUT);
  133.     CASE(ECONNREFUSED);
  134.     CASE(ELOOP);
  135.     CASE(ENAMETOOLONG);
  136.     CASE(EHOSTDOWN);
  137.     CASE(EHOSTUNREACH);
  138.     CASE(ENOTEMPTY);
  139.     CASE(EPROCLIM);
  140.     CASE(EUSERS);
  141.     CASE(EDQUOT);
  142.     CASE(ESTALE);
  143.     CASE(EREMOTE);
  144.     CASE(EBADRPC);
  145.     CASE(ERPCMISMATCH);
  146.     CASE(EPROGUNAVAIL);
  147.     CASE(EPROGMISMATCH);
  148.     CASE(EPROCUNAVAIL);
  149.     CASE(ENOLCK);
  150.     CASE(ENOSYS);
  151.     default:
  152.         return "Unknown";
  153.     }        
  154. }
  155.  
  156. void Usage(char ch1, char ch2)
  157. {
  158.     printf("# Usage is: %c%c %s\n", ch1, ch2 ? ch2 : ' ', USAGE(ch1,ch2));
  159.     Where();
  160. }
  161.  
  162. void Dispatch(const char * command)
  163. {
  164.     char        ch1    =    command[0];
  165.     char        ch2    =    command[1];
  166.     TestCmd    exec;
  167.     
  168.     /* We are guaranteed to have at least one valid character */
  169.     
  170.     switch (ch1) {
  171.     case '\n':
  172.     case '#':
  173.         return;
  174.     }
  175.  
  176.     if (!ch2)
  177.         ++command;
  178.     else {
  179.         if (isspace(ch2))    {
  180.             command    +=    1;
  181.             ch2        =    0;
  182.         } else
  183.             command    +=    2;
  184.         
  185.         /* Skip rest of first word */    
  186.         for (; *command && !isspace(*command); ++command);
  187.         
  188.         /* Skip whitespace */
  189.         while (isspace(*command))
  190.             ++command;
  191.     }
  192.     
  193.     if (isalpha(ch1) && (!ch2 || isalpha(ch2)) && (exec = DISPATCH(ch1,ch2)))
  194.         exec(ch1, ch2, command);
  195.     else {
  196.         if (ch2)
  197.             printf("# Unknown command: '%c%c'\n", ch1, ch2);
  198.         else
  199.             printf("# Unknown command: '%c'\n", ch1);
  200.             
  201.         printf("# Type 'h' for a list of known commands.\n"); 
  202.         
  203.         Where();
  204.     }
  205. }
  206.  
  207. void Quit(char, char, const char *)
  208. {
  209.     HellHoundOnMyTrail = false;
  210. }
  211.  
  212. void RunTest(int argc, char ** argv)
  213. {
  214.  
  215.     char         cmd[80];
  216.  
  217.     COMMAND('h',   0, Help,  "",                         "Print this list");
  218.     COMMAND('q',   0, Quit,  "",                         "End the sad existence of this program");
  219.  
  220.     InitGraf((Ptr) &qd.thePort);
  221.  
  222.     if (!--argc)
  223.         Help('h', 0, "");
  224.     
  225.     do {
  226.         if (argc && strcmp(inputfrom = *++argv, "-"))    {
  227.             printf("Executing %s…\n", inputfrom);
  228.             input          =    fopen(inputfrom, "r");
  229.         } else {
  230.             inputfrom    =    0;
  231.             input            =    stdin;
  232.         }
  233.  
  234.         inputline    =    1;
  235.         
  236.         while (HellHoundOnMyTrail && (Prompt(), fgets(cmd, 80, input)))    {
  237.             Dispatch(cmd);
  238.             ++inputline;
  239.         }
  240.     } while (HellHoundOnMyTrail && --argc > 0);
  241.         
  242.     printf("So long, it's been good to know you.\n");
  243. }
  244.