1
0

echo.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*******************************************************************************
  2. * Example source code for using the argtable3 library to implement:
  3. *
  4. * echo [-neE] [--help] [--version] [STRING]...
  5. *
  6. * This file is part of the argtable3 library.
  7. *
  8. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  9. * <sheitmann@users.sourceforge.net>
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. ******************************************************************************/
  34. #include "argtable3.h"
  35. /* Here we only approximate the echo functionality */
  36. void mymain(int n, int e, int E, const char** strings, int nstrings)
  37. {
  38. int j;
  39. printf("option -n = %s\n", ((n)?"YES":"NO"));
  40. printf("option -e = %s\n", ((e)?"YES":"NO"));
  41. printf("option -E = %s\n", ((E)?"YES":"NO"));
  42. for (j=0; j<nstrings; j++)
  43. printf("%s ", strings[j]);
  44. printf("\n");
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. /* Define the allowable command line options, collecting them in argtable[] */
  49. struct arg_lit *n = arg_lit0("n", NULL, "do not output the trailing newline");
  50. struct arg_lit *e = arg_lit0("e", NULL, "enable interpretation of the backslash-escaped characters listed below");
  51. struct arg_lit *E = arg_lit0("E", NULL, "disable interpretation of those sequences in <string>s");
  52. struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
  53. struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit");
  54. struct arg_str *strs = arg_strn(NULL,NULL,"STRING",0,argc+2,NULL);
  55. struct arg_end *end = arg_end(20);
  56. void* argtable[] = {n,e,E,help,vers,strs,end};
  57. const char* progname = "echo";
  58. int exitcode=0;
  59. int nerrors;
  60. /* verify the argtable[] entries were allocated sucessfully */
  61. if (arg_nullcheck(argtable) != 0)
  62. {
  63. /* NULL entries were detected, some allocations must have failed */
  64. printf("%s: insufficient memory\n",progname);
  65. exitcode=1;
  66. goto exit;
  67. }
  68. /* Parse the command line as defined by argtable[] */
  69. nerrors = arg_parse(argc,argv,argtable);
  70. /* special case: '--help' takes precedence over error reporting */
  71. if (help->count > 0)
  72. {
  73. printf("Usage: %s", progname);
  74. arg_print_syntax(stdout,argtable,"\n");
  75. printf("Echo the STRINGs to standard output.\n\n");
  76. arg_print_glossary(stdout,argtable," %-10s %s\n");
  77. printf("\nWithout -E, the following sequences are recognized and interpolated:\n\n"
  78. " \\NNN the character whose ASCII code is NNN (octal)\n"
  79. " \\\\ backslash\n"
  80. " \\a alert (BEL)\n"
  81. " \\b backspace\n"
  82. " \\c suppress trailing newline\n"
  83. " \\f form feed\n"
  84. " \\n new line\n"
  85. " \\r carriage return\n"
  86. " \\t horizontal tab\n"
  87. " \\v vertical tab\n\n"
  88. "Report bugs to <foo@bar>.\n");
  89. exitcode=0;
  90. goto exit;
  91. }
  92. /* special case: '--version' takes precedence error reporting */
  93. if (vers->count > 0)
  94. {
  95. printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
  96. printf("September 2003, Stewart Heitmann\n");
  97. exitcode=0;
  98. goto exit;
  99. }
  100. /* If the parser returned any errors then display them and exit */
  101. if (nerrors > 0)
  102. {
  103. /* Display the error details contained in the arg_end struct.*/
  104. arg_print_errors(stdout,end,progname);
  105. printf("Try '%s --help' for more information.\n",progname);
  106. exitcode=1;
  107. goto exit;
  108. }
  109. /* Command line parsing is complete, do the main processing */
  110. mymain(n->count, e->count, E->count, strs->sval, strs->count);
  111. exit:
  112. /* deallocate each non-null entry in argtable[] */
  113. arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  114. return exitcode;
  115. }