arg_end.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SPDX-FileCopyrightText: 1998-2001,2003-2011,2013 Stewart Heitmann
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*******************************************************************************
  7. * arg_end: Implements the error handling utilities
  8. *
  9. * This file is part of the argtable3 library.
  10. *
  11. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  12. * <sheitmann@users.sourceforge.net>
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. ******************************************************************************/
  37. #include "argtable3.h"
  38. #ifndef ARG_AMALGAMATION
  39. #include "argtable3_private.h"
  40. #endif
  41. #include <stdlib.h>
  42. static void arg_end_resetfn(struct arg_end* parent) {
  43. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  44. parent->count = 0;
  45. }
  46. static void arg_end_errorfn(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname) {
  47. /* suppress unreferenced formal parameter warning */
  48. (void)parent;
  49. progname = progname ? progname : "";
  50. argval = argval ? argval : "";
  51. arg_dstr_catf(ds, "%s: ", progname);
  52. switch (error) {
  53. case ARG_ELIMIT:
  54. arg_dstr_cat(ds, "too many errors to display");
  55. break;
  56. case ARG_EMALLOC:
  57. arg_dstr_cat(ds, "insufficient memory");
  58. break;
  59. case ARG_ENOMATCH:
  60. arg_dstr_catf(ds, "unexpected argument \"%s\"", argval);
  61. break;
  62. case ARG_EMISSARG:
  63. arg_dstr_catf(ds, "option \"%s\" requires an argument", argval);
  64. break;
  65. case ARG_ELONGOPT:
  66. arg_dstr_catf(ds, "invalid option \"%s\"", argval);
  67. break;
  68. default:
  69. arg_dstr_catf(ds, "invalid option \"-%c\"", error);
  70. break;
  71. }
  72. arg_dstr_cat(ds, "\n");
  73. }
  74. struct arg_end* arg_end(int maxcount) {
  75. size_t nbytes;
  76. struct arg_end* result;
  77. nbytes = sizeof(struct arg_end) + (size_t)maxcount * sizeof(int) /* storage for int error[maxcount] array*/
  78. + (size_t)maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */
  79. + (size_t)maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */
  80. result = (struct arg_end*)xmalloc(nbytes);
  81. /* init the arg_hdr struct */
  82. result->hdr.flag = ARG_TERMINATOR;
  83. result->hdr.shortopts = NULL;
  84. result->hdr.longopts = NULL;
  85. result->hdr.datatype = NULL;
  86. result->hdr.glossary = NULL;
  87. result->hdr.mincount = 1;
  88. result->hdr.maxcount = maxcount;
  89. result->hdr.parent = result;
  90. result->hdr.resetfn = (arg_resetfn*)arg_end_resetfn;
  91. result->hdr.scanfn = NULL;
  92. result->hdr.checkfn = NULL;
  93. result->hdr.errorfn = (arg_errorfn*)arg_end_errorfn;
  94. /* store error[maxcount] array immediately after struct arg_end */
  95. result->error = (int*)(result + 1);
  96. /* store parent[maxcount] array immediately after error[] array */
  97. result->parent = (void**)(result->error + maxcount);
  98. /* store argval[maxcount] array immediately after parent[] array */
  99. result->argval = (const char**)(result->parent + maxcount);
  100. ARG_TRACE(("arg_end(%d) returns %p\n", maxcount, result));
  101. return result;
  102. }
  103. void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname) {
  104. int i;
  105. ARG_TRACE(("arg_errors()\n"));
  106. for (i = 0; i < end->count; i++) {
  107. struct arg_hdr* errorparent = (struct arg_hdr*)(end->parent[i]);
  108. if (errorparent->errorfn)
  109. errorparent->errorfn(end->parent[i], ds, end->error[i], end->argval[i], progname);
  110. }
  111. }
  112. void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname) {
  113. arg_dstr_t ds = arg_dstr_create();
  114. arg_print_errors_ds(ds, end, progname);
  115. fputs(arg_dstr_cstr(ds), fp);
  116. arg_dstr_destroy(ds);
  117. }