myprog_C89.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*******************************************************************************
  2. * This example source code is an alternate version of myprog.c
  3. * that adheres to ansi C89 standards rather than ansi C99.
  4. * The only difference being that C89 does not permit the argtable array
  5. * to be statically initialized with the contents of variables set at
  6. * runtime whereas C99 does.
  7. * Hence we cannot declare and initialize the argtable array in one declaration
  8. * as
  9. * void* argtable[] = {list, recurse, repeat, defines, outfile, verbose,
  10. * help, version, infiles, end};
  11. * Instead, we must declare
  12. * void* argtable[10];
  13. * and initialize the contents of the array separately.
  14. *
  15. * This file is part of the argtable3 library.
  16. *
  17. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  18. * <sheitmann@users.sourceforge.net>
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions are met:
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * * Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  39. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. ******************************************************************************/
  43. #include "argtable3.h"
  44. int mymain(int l, int R, int k,
  45. const char **defines, int ndefines,
  46. const char *outfile,
  47. int v,
  48. const char **infiles, int ninfiles)
  49. {
  50. int i;
  51. if (l>0) printf("list files (-l)\n");
  52. if (R>0) printf("recurse through directories (-R)\n");
  53. if (v>0) printf("verbose is enabled (-v)\n");
  54. printf("scalar k=%d\n",k);
  55. printf("output is \"%s\"\n", outfile);
  56. for (i=0; i<ndefines; i++)
  57. printf("user defined macro \"%s\"\n",defines[i]);
  58. for (i=0; i<ninfiles; i++)
  59. printf("infile[%d]=\"%s\"\n",i,infiles[i]);
  60. return 0;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. struct arg_lit *list = arg_lit0("lL",NULL, "list files");
  65. struct arg_lit *recurse = arg_lit0("R",NULL, "recurse through subdirectories");
  66. struct arg_int *repeat = arg_int0("k","scalar",NULL, "define scalar value k (default is 3)");
  67. struct arg_str *defines = arg_strn("D","define","MACRO",0,argc+2, "macro definitions");
  68. struct arg_file *outfile = arg_file0("o",NULL,"<output>", "output file (default is \"-\")");
  69. struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages");
  70. struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
  71. struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit");
  72. struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)");
  73. struct arg_end *end = arg_end(20);
  74. void* argtable[10];
  75. const char* progname = "myprog_C89";
  76. int nerrors;
  77. int exitcode=0;
  78. /* initialize the argtable array with ptrs to the arg_xxx structures constructed above */
  79. argtable[0] = list;
  80. argtable[1] = recurse;
  81. argtable[2] = repeat;
  82. argtable[3] = defines;
  83. argtable[4] = outfile;
  84. argtable[5] = verbose;
  85. argtable[6] = help;
  86. argtable[7] = version;
  87. argtable[8] = infiles;
  88. argtable[9] = end;
  89. /* verify the argtable[] entries were allocated sucessfully */
  90. if (arg_nullcheck(argtable) != 0)
  91. {
  92. /* NULL entries were detected, some allocations must have failed */
  93. printf("%s: insufficient memory\n",progname);
  94. exitcode=1;
  95. goto exit;
  96. }
  97. /* set any command line default values prior to parsing */
  98. repeat->ival[0]=3;
  99. outfile->filename[0]="-";
  100. /* Parse the command line as defined by argtable[] */
  101. nerrors = arg_parse(argc,argv,argtable);
  102. /* special case: '--help' takes precedence over error reporting */
  103. if (help->count > 0)
  104. {
  105. printf("Usage: %s", progname);
  106. arg_print_syntax(stdout,argtable,"\n");
  107. printf("This program demonstrates the use of the argtable2 library\n");
  108. printf("for parsing command line arguments.\n");
  109. arg_print_glossary(stdout,argtable," %-25s %s\n");
  110. exitcode=0;
  111. goto exit;
  112. }
  113. /* special case: '--version' takes precedence error reporting */
  114. if (version->count > 0)
  115. {
  116. printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
  117. printf("September 2003, Stewart Heitmann\n");
  118. exitcode=0;
  119. goto exit;
  120. }
  121. /* If the parser returned any errors then display them and exit */
  122. if (nerrors > 0)
  123. {
  124. /* Display the error details contained in the arg_end struct.*/
  125. arg_print_errors(stdout,end,progname);
  126. printf("Try '%s --help' for more information.\n",progname);
  127. exitcode=1;
  128. goto exit;
  129. }
  130. /* special case: uname with no command line options induces brief help */
  131. if (argc==1)
  132. {
  133. printf("Try '%s --help' for more information.\n",progname);
  134. exitcode=0;
  135. goto exit;
  136. }
  137. /* normal case: take the command line options at face value */
  138. exitcode = mymain(list->count, recurse->count, repeat->ival[0],
  139. defines->sval, defines->count,
  140. outfile->filename[0], verbose->count,
  141. infiles->filename, infiles->count);
  142. exit:
  143. /* deallocate each non-null entry in argtable[] */
  144. arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  145. return exitcode;
  146. }