/vendor/freegetopt/test.c

http://github.com/feyeleanor/RubyGoLightly · C · 176 lines · 78 code · 29 blank · 69 comment · 19 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. * DEFINES
  42. */
  43. /**
  44. * flags for different command-line options
  45. *
  46. * these options don't do anything - there's just here
  47. * as examples
  48. */
  49. #define FLAG_INTERACT 0x0001 /* interactive mode */
  50. #define FLAG_FORCE 0x0002 /* force mode */
  51. #define FLAG_RECURSIVE 0x0004 /* recursive mode */
  52. /*****************************************************************************
  53. * GLOBALS
  54. */
  55. int flags = 0; /* store flags here */
  56. int verbose = 5; /* verbosity level */
  57. const char* in_fname = NULL; /* input filename */
  58. const char* out_fname = NULL; /* output filename */
  59. /*****************************************************************************
  60. * arg_to_int - Convert argument string to integer.
  61. *
  62. * min - Minimum allowed value, inclusive.
  63. * max - Maximum allowed value, inclusive.
  64. * defalt - The default value, in case of an error.
  65. * opt - Option string of this argument. (ex., "-h");
  66. */
  67. int arg_to_int(const char* arg, int min, int max, int defalt, const char* opt)
  68. {
  69. int i = defalt;
  70. int rv;
  71. /* no argument means we use the default value */
  72. if(!arg) goto done;
  73. /* make sure we got an integer argument */
  74. rv = sscanf(arg, "%d", &i);
  75. if(rv != 1) {
  76. fprintf(stderr, "%s: integer argument required.\n", opt);
  77. i = defalt;
  78. goto done;
  79. }
  80. /* make sure the integer argument is within the desired range */
  81. if(i < min || max < i) {
  82. fprintf(stderr, "%s: argument out of integer range.\n", opt);
  83. i = defalt;
  84. goto done;
  85. }
  86. done:
  87. return i;
  88. }
  89. /*****************************************************************************
  90. * help
  91. */
  92. void help()
  93. {
  94. printf(
  95. "getopt test program\n"
  96. "Usage: test [OPTION] [INPUT]\n"
  97. " INPUT set input filename (doesn't do anything)\n"
  98. " -h help menu (this screen)\n"
  99. " -i interactive mode (doesn't do anything)\n"
  100. " -f force mode (doesn't do anything)\n"
  101. " -r recursive mode (doesn't do anything)\n"
  102. " -v[level] set verbosity level (5 is default; doesn't do anything)\n"
  103. " -o filename set output filename (doesn't do anything)\n"
  104. );
  105. }
  106. /*****************************************************************************
  107. * MAIN
  108. */
  109. int main(int argc, char* argv[])
  110. {
  111. /* check arguments */
  112. while(1) {
  113. int c = getopt(argc, argv, "-ifrhv::o:");
  114. if(c == -1) break;
  115. switch(c) {
  116. case 'i': flags |= FLAG_INTERACT; break;
  117. case 'f': flags |= FLAG_FORCE; break;
  118. case 'r': flags |= FLAG_RECURSIVE; break;
  119. case 'h': help(); exit(0);
  120. case 'v': verbose = arg_to_int(optarg, 0, 10, 5, "v"); break;
  121. case 'o': out_fname = optarg; break;
  122. case 1: in_fname = optarg; break;
  123. #ifdef DEBUG
  124. default:
  125. printf("Option '%c' (%d) with '%s'\n", c, c, optarg);
  126. #endif
  127. }
  128. }
  129. #ifdef DEBUG
  130. printf("optind at %d; argv[optind] = '%s'\n", optind, argv[optind]);
  131. #endif
  132. /* print out what we got */
  133. if(flags & FLAG_INTERACT) printf("in interactive mode\n");
  134. else printf("not in interactive mode\n");
  135. if(flags & FLAG_FORCE) printf("in force mode\n");
  136. else printf("not in force mode\n");
  137. if(flags & FLAG_RECURSIVE) printf("in recursive mode\n");
  138. else printf("not in recursive mode\n");
  139. printf("verbosity level: %d\n", verbose);
  140. if(in_fname) printf("input filename: %s\n", in_fname);
  141. else printf("no input filename\n");
  142. if(out_fname) printf("output filename: %s\n", out_fname);
  143. else printf("no output filename\n");
  144. return 0;
  145. }
  146. /* vim:ts=3
  147. */