ParseCmd.c revision 07fb9b8f
11ab64890Smrg 21ab64890Smrg/*********************************************************** 31ab64890Smrg 41ab64890SmrgCopyright 1987, 1988, 1998 The Open Group 51ab64890Smrg 61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its 71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that 81ab64890Smrgthe above copyright notice appear in all copies and that both that 91ab64890Smrgcopyright notice and this permission notice appear in supporting 101ab64890Smrgdocumentation. 111ab64890Smrg 121ab64890SmrgThe above copyright notice and this permission notice shall be included in 131ab64890Smrgall copies or substantial portions of the Software. 141ab64890Smrg 151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 211ab64890Smrg 221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be 231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings 241ab64890Smrgin this Software without prior written authorization from The Open Group. 251ab64890Smrg 261ab64890Smrg 271ab64890SmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. 281ab64890Smrg 291ab64890Smrg All Rights Reserved 301ab64890Smrg 3161b2299dSmrgPermission to use, copy, modify, and distribute this software and its 3261b2299dSmrgdocumentation for any purpose and without fee is hereby granted, 331ab64890Smrgprovided that the above copyright notice appear in all copies and that 3461b2299dSmrgboth that copyright notice and this permission notice appear in 351ab64890Smrgsupporting documentation, and that the name of Digital not be 361ab64890Smrgused in advertising or publicity pertaining to distribution of the 3761b2299dSmrgsoftware without specific, written prior permission. 381ab64890Smrg 391ab64890SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 401ab64890SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 411ab64890SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 421ab64890SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 431ab64890SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 441ab64890SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 451ab64890SmrgSOFTWARE. 461ab64890Smrg 471ab64890Smrg******************************************************************/ 481ab64890Smrg 491ab64890Smrg/* XrmParseCommand() 501ab64890Smrg 511ab64890Smrg Parse command line and store argument values into resource database 521ab64890Smrg 531ab64890Smrg Allows any un-ambiguous abbreviation for an option name, but requires 541ab64890Smrg that the table be ordered with any options that are prefixes of 551ab64890Smrg other options appearing before the longer version in the table. 561ab64890Smrg*/ 571ab64890Smrg 581ab64890Smrg#ifdef HAVE_CONFIG_H 591ab64890Smrg#include <config.h> 601ab64890Smrg#endif 611ab64890Smrg#include "Xlibint.h" 621ab64890Smrg#include <X11/Xresource.h> 631ab64890Smrg#include <stdio.h> 641ab64890Smrg 651ab64890Smrg 661ab64890Smrgstatic void _XReportParseError(XrmOptionDescRec *arg, const char *msg) 671ab64890Smrg{ 681ab64890Smrg (void) fprintf(stderr, "Error parsing argument \"%s\" (%s); %s\n", 691ab64890Smrg arg->option, arg->specifier, msg); 701ab64890Smrg exit(1); 711ab64890Smrg} 721ab64890Smrg 731ab64890Smrgvoid 741ab64890SmrgXrmParseCommand( 751ab64890Smrg XrmDatabase *pdb, /* data base */ 761ab64890Smrg register XrmOptionDescList options, /* pointer to table of valid options */ 771ab64890Smrg int num_options, /* number of options */ 781ab64890Smrg _Xconst char *prefix, /* name to prefix resources with */ 791ab64890Smrg int *argc, /* address of argument count */ 801ab64890Smrg char **argv) /* argument list (command line) */ 811ab64890Smrg{ 821ab64890Smrg int foundOption; 831ab64890Smrg char **argsave; 841ab64890Smrg register int i, myargc; 851ab64890Smrg XrmBinding bindings[100]; 861ab64890Smrg XrmQuark quarks[100]; 871ab64890Smrg XrmBinding *start_bindings; 881ab64890Smrg XrmQuark *start_quarks; 891ab64890Smrg char *optP, *argP = NULL, optchar, argchar = 0; 901ab64890Smrg int matches; 911ab64890Smrg enum {DontCare, Check, NotSorted, Sorted} table_is_sorted; 921ab64890Smrg char **argend; 931ab64890Smrg 941ab64890Smrg#define PutCommandResource(value_str) \ 9507fb9b8fSmrg do { \ 961ab64890Smrg XrmStringToBindingQuarkList( \ 971ab64890Smrg options[i].specifier, start_bindings, start_quarks); \ 981ab64890Smrg XrmQPutStringResource(pdb, bindings, quarks, value_str); \ 9907fb9b8fSmrg } while (0) /* PutCommandResource */ 1001ab64890Smrg 10161b2299dSmrg myargc = (*argc); 1021ab64890Smrg argend = argv + myargc; 1031ab64890Smrg argsave = ++argv; 1041ab64890Smrg 1051ab64890Smrg /* Initialize bindings/quark list with prefix (typically app name). */ 1061ab64890Smrg quarks[0] = XrmStringToName(prefix); 1071ab64890Smrg bindings[0] = XrmBindTightly; 1081ab64890Smrg start_quarks = quarks+1; 1091ab64890Smrg start_bindings = bindings+1; 1101ab64890Smrg 1111ab64890Smrg table_is_sorted = (myargc > 2) ? Check : DontCare; 1121ab64890Smrg for (--myargc; myargc > 0; --myargc, ++argv) { 1131ab64890Smrg foundOption = False; 1141ab64890Smrg matches = 0; 1151ab64890Smrg for (i=0; i < num_options; ++i) { 1161ab64890Smrg /* checking the sort order first insures we don't have to 1171ab64890Smrg re-do the check if the arg hits on the last entry in 1181ab64890Smrg the table. Useful because usually '=' is the last entry 1191ab64890Smrg and users frequently specify geometry early in the command */ 1201ab64890Smrg if (table_is_sorted == Check && i > 0 && 1211ab64890Smrg strcmp(options[i].option, options[i-1].option) < 0) { 1221ab64890Smrg table_is_sorted = NotSorted; 1231ab64890Smrg } 1241ab64890Smrg for (argP = *argv, optP = options[i].option; 1251ab64890Smrg (optchar = *optP++) && 1261ab64890Smrg (argchar = *argP++) && 1271ab64890Smrg argchar == optchar;); 1281ab64890Smrg if (!optchar) { 1291ab64890Smrg if (!*argP || 1301ab64890Smrg options[i].argKind == XrmoptionStickyArg || 1311ab64890Smrg options[i].argKind == XrmoptionIsArg) { 1321ab64890Smrg /* give preference to exact matches, StickyArg and IsArg */ 1331ab64890Smrg matches = 1; 1341ab64890Smrg foundOption = i; 1351ab64890Smrg break; 1361ab64890Smrg } 1371ab64890Smrg } 1381ab64890Smrg else if (!argchar) { 1391ab64890Smrg /* may be an abbreviation for this option */ 1401ab64890Smrg matches++; 1411ab64890Smrg foundOption = i; 1421ab64890Smrg } 1431ab64890Smrg else if (table_is_sorted == Sorted && optchar > argchar) { 1441ab64890Smrg break; 1451ab64890Smrg } 1461ab64890Smrg if (table_is_sorted == Check && i > 0 && 1471ab64890Smrg strcmp(options[i].option, options[i-1].option) < 0) { 1481ab64890Smrg table_is_sorted = NotSorted; 1491ab64890Smrg } 1501ab64890Smrg } 1511ab64890Smrg if (table_is_sorted == Check && i >= (num_options-1)) 1521ab64890Smrg table_is_sorted = Sorted; 1531ab64890Smrg if (matches == 1) { 1541ab64890Smrg i = foundOption; 1551ab64890Smrg switch (options[i].argKind){ 1561ab64890Smrg case XrmoptionNoArg: 1571ab64890Smrg --(*argc); 1581ab64890Smrg PutCommandResource(options[i].value); 1591ab64890Smrg break; 16061b2299dSmrg 1611ab64890Smrg case XrmoptionIsArg: 1621ab64890Smrg --(*argc); 1631ab64890Smrg PutCommandResource(*argv); 1641ab64890Smrg break; 1651ab64890Smrg 1661ab64890Smrg case XrmoptionStickyArg: 1671ab64890Smrg --(*argc); 1681ab64890Smrg PutCommandResource(argP); 1691ab64890Smrg break; 1701ab64890Smrg 1711ab64890Smrg case XrmoptionSepArg: 1721ab64890Smrg if (myargc > 1) { 1731ab64890Smrg ++argv; --myargc; --(*argc); --(*argc); 1741ab64890Smrg PutCommandResource(*argv); 1751ab64890Smrg } else 1761ab64890Smrg (*argsave++) = (*argv); 1771ab64890Smrg break; 17861b2299dSmrg 1791ab64890Smrg case XrmoptionResArg: 1801ab64890Smrg if (myargc > 1) { 1811ab64890Smrg ++argv; --myargc; --(*argc); --(*argc); 1821ab64890Smrg XrmPutLineResource(pdb, *argv); 1831ab64890Smrg } else 1841ab64890Smrg (*argsave++) = (*argv); 1851ab64890Smrg break; 18661b2299dSmrg 1871ab64890Smrg case XrmoptionSkipArg: 1881ab64890Smrg if (myargc > 1) { 1891ab64890Smrg --myargc; 1901ab64890Smrg (*argsave++) = (*argv++); 1911ab64890Smrg } 19261b2299dSmrg (*argsave++) = (*argv); 1931ab64890Smrg break; 1941ab64890Smrg 1951ab64890Smrg case XrmoptionSkipLine: 1961ab64890Smrg for (; myargc > 0; myargc--) 1971ab64890Smrg (*argsave++) = (*argv++); 1981ab64890Smrg break; 1991ab64890Smrg 2001ab64890Smrg case XrmoptionSkipNArgs: 2011ab64890Smrg { 2021ab64890Smrg register int j = 1 + (long) options[i].value; 2031ab64890Smrg 2041ab64890Smrg if (j > myargc) j = myargc; 2051ab64890Smrg for (; j > 0; j--) { 2061ab64890Smrg (*argsave++) = (*argv++); 2071ab64890Smrg myargc--; 2081ab64890Smrg } 2091ab64890Smrg argv--; /* went one too far before */ 2101ab64890Smrg myargc++; 2111ab64890Smrg } 2121ab64890Smrg break; 2131ab64890Smrg 2141ab64890Smrg default: 2151ab64890Smrg _XReportParseError (&options[i], "unknown kind"); 2161ab64890Smrg break; 2171ab64890Smrg } 2181ab64890Smrg } 2191ab64890Smrg else 22061b2299dSmrg (*argsave++) = (*argv); /*compress arglist*/ 2211ab64890Smrg } 2221ab64890Smrg 2231ab64890Smrg if (argsave < argend) 2241ab64890Smrg (*argsave)=NULL; /* put NULL terminator on compressed argv */ 2251ab64890Smrg} 226