home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / comlin.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  7KB  |  320 lines

  1. /* -*-C-*-
  2.  
  3. Copyright (c) 1987-1999 Massachusetts Institute of Technology
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at
  8. your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* $Id: comlin.c,v 1.9 1999/01/02 06:11:34 cph Exp $
  21.  *
  22.  * This file contains the scheme command parser.
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #ifndef toupper
  28. #include <ctype.h>
  29. #endif
  30.  
  31. #include "comlin.h"
  32.  
  33. /* Some string utilities */
  34.  
  35. char *
  36. DEFUN (remove_initial_substring, (sub, str),
  37.        register char * sub
  38.        AND register char * str)
  39. {
  40.   for ( ; *sub != '\0'; sub++, str++)
  41.   {
  42.     if (*sub != *str)
  43.     {
  44.       return ((char *) NULL);
  45.     }
  46.   }
  47.   return (str);
  48. }
  49.  
  50. boolean
  51. DEFUN (STREQUAL, (s1, s2),
  52.        register char * s1
  53.        AND register char * s2)
  54. {
  55.   for ( ; *s1 != '\0'; s1++, s2++)
  56.   {
  57.     if (toupper(*s1) != toupper(*s2))
  58.     {
  59.       return (false);
  60.     }
  61.   }
  62.   return (*s2 == '\0');
  63. }
  64.  
  65. /* Usage information */
  66.  
  67. void
  68. DEFUN (print_usage_and_exit, (options, val),
  69.        struct keyword_struct * options
  70.        AND int val)
  71. {
  72.   register int i;
  73.  
  74.   fprintf(stderr, "usage: %s", program_name);
  75.  
  76.   if ((options[0].type_tag) == LAST_KYWRD)
  77.   {
  78.     fprintf(stderr, "\n");
  79.     exit(val);
  80.   }
  81.  
  82.   fprintf(stderr, " [args]\n");
  83.   fprintf(stderr, "    where args are as follows:\n");
  84.  
  85.   for (i = 0;
  86.        ((options[i].type_tag) != LAST_KYWRD);
  87.        i++)
  88.   {
  89.     switch (options[i].type_tag)
  90.     {
  91.       case BOOLEAN_KYWRD:
  92.     fprintf(stderr, "        %s={true,false}\n",
  93.         options[i].keyword);
  94.     break;
  95.  
  96.       case INT_KYWRD:
  97.       case DOUBLE_KYWRD:
  98.     fprintf(stderr, "        %s=%s\n",
  99.         options[i].keyword, options[i].format);
  100.     break;
  101.  
  102.       case STRING_KYWRD:
  103.     fprintf(stderr, "        %s=%%s\n",
  104.         options[i].keyword);
  105.     break;
  106.     }
  107.   }
  108.   exit(val);
  109. }
  110.  
  111. void
  112. DEFUN (supply, (options, j),
  113.        struct keyword_struct * options
  114.        AND int j)
  115. {
  116.   if (options[j].supplied_p != ((boolean *) NULL))
  117.   {
  118.     if (*(options[j].supplied_p))
  119.     {
  120.       fprintf(stderr,
  121.           "parse_keywords: Repeated keyword: %s\n",
  122.           options[j].keyword);
  123.       print_usage_and_exit(&options[0], 1);
  124.     }
  125.     else
  126.     {
  127.       *(options[j].supplied_p) = true;
  128.     }
  129.   }
  130.   return;
  131. }
  132.  
  133. char * program_name;
  134.  
  135. /* This parser assumes that no keyword is an initial
  136.    substring of another.
  137.  */
  138.  
  139. void
  140. DEFUN (parse_keywords,
  141.        (argc, argv, options, allow_others_p),
  142.        int argc
  143.        AND char **argv
  144.        AND struct keyword_struct * options
  145.        AND boolean allow_others_p)
  146. {
  147.   register int i, j, length;
  148.   char *argument;
  149.  
  150.   program_name = argv[0];
  151.   argv += 1;
  152.   argc -= 1;
  153.  
  154.   /* Initialize defaults */
  155.  
  156.   for (length = 0;
  157.        ((options[length].type_tag) != LAST_KYWRD);
  158.        length++)
  159.   {
  160.     if (options[length].supplied_p != ((boolean *) NULL))
  161.     {
  162.       *(options[length].supplied_p) = false;
  163.     }
  164.  
  165.     switch (options[length].type_tag)
  166.     {
  167.       case BOOLEAN_KYWRD:
  168.         if (options[length].format != BFRMT)
  169.     {
  170.       fprintf(stderr,
  171.           "parse_keywords: format (%s) for boolean keyword %s\n",
  172.           options[length].format,
  173.           options[length].keyword);
  174.       exit(1);
  175.     }
  176.     break;
  177.  
  178.       case INT_KYWRD:
  179.     break;
  180.  
  181.       case DOUBLE_KYWRD:
  182.     break;
  183.  
  184.       case STRING_KYWRD:
  185.         if (options[length].format != SFRMT)
  186.     {
  187.       fprintf(stderr,
  188.           "parse_keywords: format (%s) for string keyword %s\n",
  189.           options[length].format,
  190.           options[length].keyword);
  191.       exit(1);
  192.     }
  193.     break;
  194.  
  195.       default:
  196.         fprintf(stderr, "parse_keywords: bad type %d\n",
  197.         options[length].type_tag);
  198.     exit(1);
  199.     }
  200.   }
  201.  
  202.   for (i = 0; i < argc; i++)
  203.   {
  204.     for (j = 0; j < length; j++)
  205.     {
  206.       argument = remove_initial_substring(options[j].keyword,argv[i]);
  207.       if (argument != ((char *) NULL))
  208.       {
  209.     switch (options[j].type_tag)
  210.     {
  211.  
  212.       case BOOLEAN_KYWRD:
  213.       {
  214.         boolean value;
  215.  
  216.         if (*argument != '\0')
  217.         {
  218.           if (*argument != '=')
  219.           {
  220.         fprintf(stderr,
  221.             "parse_keywords: unrecognized parameter: %s\n",
  222.             argv[i]);
  223.         print_usage_and_exit(&options[0], 1);
  224.           }
  225.           else
  226.           {
  227.         argument = &argument[1];
  228.         if (STREQUAL(argument,"t") || STREQUAL(argument,"true"))
  229.         {
  230.           value = true;
  231.         }
  232.         else if (STREQUAL(argument,"f") ||
  233.              STREQUAL(argument,"false") ||
  234.              STREQUAL(argument,"nil"))
  235.         {
  236.           value = false;
  237.         }
  238.         else
  239.         {
  240.           fprintf(stderr,
  241.               "parse_keywords: Invalid boolean value: %s\n",
  242.               argv[i]);
  243.           print_usage_and_exit(&options[0], 1);
  244.         }
  245.           }
  246.         }
  247.         else
  248.         {
  249.           value = true;
  250.         }
  251.         supply(options, j);
  252.         *(BOOLEAN_LVALUE(options[j])) = value;
  253.         break;
  254.       }
  255.  
  256.       case INT_KYWRD:
  257.         if (*argument != '=')
  258.         {
  259.           {
  260.         fprintf(stderr,
  261.             "parse_keywords: %s: %s\n",
  262.             ((*argument == '\0')    ?
  263.              "missing integer value"    :
  264.              "unrecognized parameter"),
  265.             argv[i]);
  266.         print_usage_and_exit(&options[0], 1);
  267.           }
  268.         }
  269.         supply(options, j);
  270.         sscanf(&argument[1], options[j].format, INT_LVALUE(options[j]));
  271.         break;
  272.  
  273.       case DOUBLE_KYWRD:
  274.         if (*argument != '=')
  275.         {
  276.           {
  277.         fprintf(stderr,
  278.             "parse_keywords: %s: %s\n",
  279.             ((*argument == '\0')        ?
  280.              "missing floating point value"    :
  281.              "unrecognized parameter"),
  282.             argv[i]);
  283.         print_usage_and_exit(&options[0], 1);
  284.           }
  285.         }
  286.         supply(options, j);
  287.         sscanf(&argument[1], options[j].format, DOUBLE_LVALUE(options[j]));
  288.         break;
  289.  
  290.       case STRING_KYWRD:
  291.         if (*argument != '=')
  292.         {
  293.           {
  294.         fprintf(stderr,
  295.             "parse_keywords: %s: %s\n",
  296.             ((*argument == '\0')    ?
  297.              "missing string value"    :
  298.              "unrecognized parameter"),
  299.             argv[i]);
  300.         print_usage_and_exit(&options[0], 1);
  301.           }
  302.         }
  303.         supply(options, j);
  304.         *(STRING_LVALUE(options[j])) = &argument[1];
  305.         break;
  306.       }
  307.     break;
  308.       }
  309.     }
  310.     if ((j >= length) && (!allow_others_p))
  311.     {
  312.       fprintf(stderr,
  313.           "parse_keywords: unrecognized parameter: %s\n",
  314.           argv[i]);
  315.       print_usage_and_exit(&options[0], 1);
  316.     }
  317.   }
  318.   return;
  319. }
  320.