PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/freegetopt/test.c

http://github.com/feyeleanor/RubyGoLightly
C | 176 lines | 78 code | 29 blank | 69 comment | 17 complexity | e8aa57385bb253658c0636c47bd35071 MD5 | raw file
  1/*****************************************************************************
  2* getopt test program.
  3* $Header: /cvsroot/freegetopt/freegetopt/test.c,v 1.3 2003/10/26 03:10:20 vindaci Exp $
  4*
  5* Copyright (c)2002-2003 Mark K. Kim
  6* All rights reserved.
  7* 
  8* Redistribution and use in source and binary forms, with or without
  9* modification, are permitted provided that the following conditions
 10* are met:
 11*
 12*   * Redistributions of source code must retain the above copyright
 13*     notice, this list of conditions and the following disclaimer.
 14*
 15*   * Redistributions in binary form must reproduce the above copyright
 16*     notice, this list of conditions and the following disclaimer in
 17*     the documentation and/or other materials provided with the
 18*     distribution.
 19*
 20*   * Neither the original author of this software nor the names of its
 21*     contributors may be used to endorse or promote products derived
 22*     from this software without specific prior written permission.
 23*
 24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 25* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 26* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 27* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 28* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 29* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 30* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 31* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 32* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 33* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 34* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 35* DAMAGE.
 36*/
 37#include <stdio.h>
 38#include <stdlib.h>
 39#include "getopt.h"
 40
 41
 42/*****************************************************************************
 43* DEFINES
 44*/
 45
 46/**
 47* flags for different command-line options
 48*
 49* these options don't do anything - there's just here
 50* as examples
 51*/
 52#define FLAG_INTERACT   0x0001         /* interactive mode */
 53#define FLAG_FORCE      0x0002         /* force mode */
 54#define FLAG_RECURSIVE  0x0004         /* recursive mode */
 55
 56
 57/*****************************************************************************
 58* GLOBALS
 59*/
 60
 61int flags = 0;                         /* store flags here */
 62
 63int verbose = 5;                       /* verbosity level */
 64const char* in_fname = NULL;           /* input filename */
 65const char* out_fname = NULL;          /* output filename */
 66
 67
 68/*****************************************************************************
 69* arg_to_int - Convert argument string to integer.
 70*
 71* min - Minimum allowed value, inclusive.
 72* max - Maximum allowed value, inclusive.
 73* defalt - The default value, in case of an error.
 74* opt - Option string of this argument.  (ex., "-h");
 75*/
 76
 77int arg_to_int(const char* arg, int min, int max, int defalt, const char* opt)
 78{
 79	int i = defalt;
 80	int rv;
 81
 82	/* no argument means we use the default value */
 83	if(!arg) goto done;
 84
 85	/* make sure we got an integer argument */
 86	rv = sscanf(arg, "%d", &i);
 87	if(rv != 1) {
 88		fprintf(stderr, "%s: integer argument required.\n", opt);
 89		i = defalt;
 90		goto done;
 91	}
 92
 93	/* make sure the integer argument is within the desired range */
 94	if(i < min || max < i) {
 95		fprintf(stderr, "%s: argument out of integer range.\n", opt);
 96		i = defalt;
 97		goto done;
 98	}
 99
100done:
101	return i;
102}
103
104
105/*****************************************************************************
106* help
107*/
108
109void help()
110{
111	printf(
112"getopt test program\n"
113"Usage: test [OPTION] [INPUT]\n"
114"   INPUT           set input filename (doesn't do anything)\n"
115"   -h              help menu (this screen)\n"
116"   -i              interactive mode (doesn't do anything)\n"
117"   -f              force mode (doesn't do anything)\n"
118"   -r              recursive mode (doesn't do anything)\n"
119"   -v[level]       set verbosity level (5 is default; doesn't do anything)\n"
120"   -o filename     set output filename (doesn't do anything)\n"
121	);
122}
123
124
125/*****************************************************************************
126* MAIN
127*/
128
129int main(int argc, char* argv[])
130{
131	/* check arguments */
132	while(1) {
133		int c = getopt(argc, argv, "-ifrhv::o:");
134		if(c == -1) break;
135
136		switch(c) {
137			case 'i': flags |= FLAG_INTERACT; break;
138			case 'f': flags |= FLAG_FORCE; break;
139			case 'r': flags |= FLAG_RECURSIVE; break;
140
141			case 'h': help(); exit(0);
142
143			case 'v': verbose = arg_to_int(optarg, 0, 10, 5, "v"); break;
144			case 'o': out_fname = optarg; break;
145			case 1: in_fname = optarg; break;
146
147			#ifdef DEBUG
148			default:
149				printf("Option '%c' (%d) with '%s'\n", c, c, optarg);
150			#endif
151		}
152	}
153
154	#ifdef DEBUG
155		printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
156	#endif
157
158	/* print out what we got */
159	if(flags & FLAG_INTERACT) printf("in interactive mode\n");
160	else printf("not in interactive mode\n");
161	if(flags & FLAG_FORCE) printf("in force mode\n");
162	else printf("not in force mode\n");
163	if(flags & FLAG_RECURSIVE) printf("in recursive mode\n");
164	else printf("not in recursive mode\n");
165	printf("verbosity level: %d\n", verbose);
166	if(in_fname) printf("input filename: %s\n", in_fname);
167	else printf("no input filename\n");
168	if(out_fname) printf("output filename: %s\n", out_fname);
169	else printf("no output filename\n");
170
171	return 0;
172}
173
174
175/* vim:ts=3
176*/