arg_lit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SPDX-FileCopyrightText: 1998-2001,2003-2011,2013 Stewart Heitmann
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*******************************************************************************
  7. * arg_lit: Implements the literature command-line option
  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_lit_resetfn(struct arg_lit* parent) {
  43. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  44. parent->count = 0;
  45. }
  46. static int arg_lit_scanfn(struct arg_lit* parent, const char* argval) {
  47. int errorcode = 0;
  48. if (parent->count < parent->hdr.maxcount)
  49. parent->count++;
  50. else
  51. errorcode = ARG_ERR_MAXCOUNT;
  52. ARG_TRACE(("%s:scanfn(%p,%s) returns %d\n", __FILE__, parent, argval, errorcode));
  53. return errorcode;
  54. }
  55. static int arg_lit_checkfn(struct arg_lit* parent) {
  56. int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0;
  57. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  58. return errorcode;
  59. }
  60. static void arg_lit_errorfn(struct arg_lit* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) {
  61. const char* shortopts = parent->hdr.shortopts;
  62. const char* longopts = parent->hdr.longopts;
  63. const char* datatype = parent->hdr.datatype;
  64. switch (errorcode) {
  65. case ARG_ERR_MINCOUNT:
  66. arg_dstr_catf(ds, "%s: missing option ", progname);
  67. arg_print_option_ds(ds, shortopts, longopts, datatype, "\n");
  68. arg_dstr_cat(ds, "\n");
  69. break;
  70. case ARG_ERR_MAXCOUNT:
  71. arg_dstr_catf(ds, "%s: extraneous option ", progname);
  72. arg_print_option_ds(ds, shortopts, longopts, datatype, "\n");
  73. break;
  74. }
  75. ARG_TRACE(("%s:errorfn(%p, %p, %d, %s, %s)\n", __FILE__, parent, ds, errorcode, argval, progname));
  76. }
  77. struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary) {
  78. return arg_litn(shortopts, longopts, 0, 1, glossary);
  79. }
  80. struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary) {
  81. return arg_litn(shortopts, longopts, 1, 1, glossary);
  82. }
  83. struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary) {
  84. struct arg_lit* result;
  85. /* foolproof things by ensuring maxcount is not less than mincount */
  86. maxcount = (maxcount < mincount) ? mincount : maxcount;
  87. result = (struct arg_lit*)xmalloc(sizeof(struct arg_lit));
  88. /* init the arg_hdr struct */
  89. result->hdr.flag = 0;
  90. result->hdr.shortopts = shortopts;
  91. result->hdr.longopts = longopts;
  92. result->hdr.datatype = NULL;
  93. result->hdr.glossary = glossary;
  94. result->hdr.mincount = mincount;
  95. result->hdr.maxcount = maxcount;
  96. result->hdr.parent = result;
  97. result->hdr.resetfn = (arg_resetfn*)arg_lit_resetfn;
  98. result->hdr.scanfn = (arg_scanfn*)arg_lit_scanfn;
  99. result->hdr.checkfn = (arg_checkfn*)arg_lit_checkfn;
  100. result->hdr.errorfn = (arg_errorfn*)arg_lit_errorfn;
  101. /* init local variables */
  102. result->count = 0;
  103. ARG_TRACE(("arg_litn() returns %p\n", result));
  104. return result;
  105. }