home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xrm_ParseCmd.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  8KB  |  239 lines

  1. /* $XConsortium: ParseCmd.c,v 1.25 94/04/17 20:20:22 gildea Exp $ */
  2.  
  3. /***********************************************************
  4.  
  5. Copyright (c) 1987, 1988  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. Except as contained in this notice, the name of the X Consortium shall not be
  25. used in advertising or otherwise to promote the sale, use or other dealings
  26. in this Software without prior written authorization from the X Consortium.
  27.  
  28.  
  29. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  30.  
  31.                         All Rights Reserved
  32.  
  33. Permission to use, copy, modify, and distribute this software and its 
  34. documentation for any purpose and without fee is hereby granted, 
  35. provided that the above copyright notice appear in all copies and that
  36. both that copyright notice and this permission notice appear in 
  37. supporting documentation, and that the name of Digital not be
  38. used in advertising or publicity pertaining to distribution of the
  39. software without specific, written prior permission.  
  40.  
  41. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  42. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  43. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  44. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  45. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  46. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  47. SOFTWARE.
  48.  
  49. ******************************************************************/
  50. /* $XFree86: xc/lib/X11/ParseCmd.c,v 1.1.1.1.12.2 1998/10/04 15:22:49 hohndel Exp $ */
  51.  
  52. /* XrmParseCommand()
  53.  
  54.    Parse command line and store argument values into resource database
  55.  
  56.    Allows any un-ambiguous abbreviation for an option name, but requires
  57.    that the table be ordered with any options that are prefixes of
  58.    other options appearing before the longer version in the table.
  59. */
  60.  
  61. #include "Xlib_private.h"
  62. #include <X11/Xresource.h>
  63. #include <stdio.h>
  64.  
  65.  
  66. static void _XReportParseError(arg, msg)
  67.     XrmOptionDescRec *arg;
  68.     char *msg;
  69. {
  70.     (void) fprintf(stderr, "Error parsing argument \"%s\" (%s); %s\n",
  71.            arg->option, arg->specifier, msg);
  72.     exit(1);
  73. }
  74.  
  75. #if NeedFunctionPrototypes
  76. void XrmParseCommand(
  77.     XrmDatabase        *pdb,        /* data base */
  78.     register XrmOptionDescList options, /* pointer to table of valid options */
  79.     int            num_options,    /* number of options             */
  80.     _Xconst char    *prefix,    /* name to prefix resources with     */
  81.     int            *argc,        /* address of argument count          */
  82.     char        **argv)        /* argument list (command line)         */
  83. #else
  84. void XrmParseCommand(pdb, options, num_options, prefix, argc, argv)
  85.     XrmDatabase        *pdb;        /* data base */
  86.     register XrmOptionDescList options; /* pointer to table of valid options */
  87.     int            num_options;    /* number of options             */
  88.     char        *prefix;    /* name to prefix resources with     */
  89.     int            *argc;        /* address of argument count          */
  90.     char        **argv;        /* argument list (command line)         */
  91. #endif
  92. {
  93.     DBUG_ENTER("XrmParseCommand")
  94.     int         foundOption;
  95.     char        **argsave;
  96.     register int    i, myargc;
  97.     XrmBinding        bindings[100];
  98.     XrmQuark        quarks[100];
  99.     XrmBinding        *start_bindings;
  100.     XrmQuark        *start_quarks;
  101.     char        *optP, *argP = NULL, optchar, argchar;
  102.     int            matches;
  103.     enum {DontCare, Check, NotSorted, Sorted} table_is_sorted;
  104.     char        **argend;
  105.  
  106. #define PutCommandResource(value_str)                \
  107.     {                                \
  108.     XrmStringToBindingQuarkList(                \
  109.     options[i].specifier, start_bindings, start_quarks);    \
  110.     XrmQPutStringResource(pdb, bindings, quarks, value_str);    \
  111.     } /* PutCommandResource */
  112.  
  113.     myargc = (*argc); 
  114.     argend = argv + myargc;
  115.     argsave = ++argv;
  116.  
  117.     /* Initialize bindings/quark list with prefix (typically app name). */
  118.     quarks[0] = XrmStringToName(prefix);
  119.     bindings[0] = XrmBindTightly;
  120.     start_quarks = quarks+1;
  121.     start_bindings = bindings+1;
  122.  
  123.     table_is_sorted = (myargc > 2) ? Check : DontCare;
  124.     for (--myargc; myargc > 0; --myargc, ++argv) {
  125.     foundOption = False;
  126.     matches = 0;
  127.     for (i=0; i < num_options; ++i) {
  128.         /* checking the sort order first insures we don't have to
  129.            re-do the check if the arg hits on the last entry in
  130.            the table.  Useful because usually '=' is the last entry
  131.            and users frequently specify geometry early in the command */
  132.         if (table_is_sorted == Check && i > 0 &&
  133.         strcmp(options[i].option, options[i-1].option) < 0) {
  134.         table_is_sorted = NotSorted;
  135.         }
  136.         for (argP = *argv, optP = options[i].option;
  137.          (optchar = *optP++) &&
  138.          (argchar = *argP++) &&
  139.          argchar == optchar;);
  140.         if (!optchar) {
  141.         if (!*argP ||
  142.             options[i].argKind == XrmoptionStickyArg ||
  143.             options[i].argKind == XrmoptionIsArg) {
  144.             /* give preference to exact matches, StickyArg and IsArg */
  145.             matches = 1;
  146.             foundOption = i;
  147.             break;
  148.         }
  149.         }
  150.         else if (!argchar) {
  151.         /* may be an abbreviation for this option */
  152.         matches++;
  153.         foundOption = i;
  154.         }
  155.         else if (table_is_sorted == Sorted && optchar > argchar) {
  156.         break;
  157.         }
  158.         if (table_is_sorted == Check && i > 0 &&
  159.         strcmp(options[i].option, options[i-1].option) < 0) {
  160.         table_is_sorted = NotSorted;
  161.         }
  162.     }
  163.     if (table_is_sorted == Check && i >= (num_options-1))
  164.         table_is_sorted = Sorted;
  165.     if (matches == 1) {
  166.         i = foundOption;
  167.         switch (options[i].argKind){
  168.         case XrmoptionNoArg:
  169.             --(*argc);
  170.             PutCommandResource(options[i].value);
  171.             break;
  172.                 
  173.         case XrmoptionIsArg:
  174.             --(*argc);
  175.             PutCommandResource(*argv);
  176.             break;
  177.  
  178.         case XrmoptionStickyArg:
  179.             --(*argc);
  180.             PutCommandResource(argP);
  181.             break;
  182.  
  183.         case XrmoptionSepArg:
  184.             if (myargc > 1) {
  185.             ++argv; --myargc; --(*argc); --(*argc);
  186.             PutCommandResource(*argv);
  187.             } else
  188.             (*argsave++) = (*argv);
  189.             break;
  190.         
  191.         case XrmoptionResArg:
  192.             if (myargc > 1) {
  193.             ++argv; --myargc; --(*argc); --(*argc);
  194.             XrmPutLineResource(pdb, *argv);
  195.             } else
  196.             (*argsave++) = (*argv);
  197.             break;
  198.         
  199.         case XrmoptionSkipArg:
  200.             if (myargc > 1) {
  201.             --myargc;
  202.             (*argsave++) = (*argv++);
  203.             }
  204.             (*argsave++) = (*argv); 
  205.             break;
  206.  
  207.         case XrmoptionSkipLine:
  208.             for (; myargc > 0; myargc--)
  209.             (*argsave++) = (*argv++);
  210.             break;
  211.  
  212.         case XrmoptionSkipNArgs:
  213.             {
  214.             register int j = 1 + (long) options[i].value;
  215.  
  216.             if (j > myargc) j = myargc;
  217.             for (; j > 0; j--) {
  218.                 (*argsave++) = (*argv++);
  219.                 myargc--;
  220.             }
  221.             argv--;        /* went one too far before */
  222.             myargc++;
  223.             }
  224.             break;
  225.  
  226.         default:
  227.             _XReportParseError (&options[i], "unknown kind");
  228.             break;
  229.         }
  230.     }
  231.     else
  232.         (*argsave++) = (*argv);  /*compress arglist*/ 
  233.     }
  234.  
  235.     if (argsave < argend)
  236.     (*argsave)=NULL; /* put NULL terminator on compressed argv */
  237.     DBUG_VOID_RETURN;
  238. }
  239.