├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── argtable ├── CMakeLists.txt ├── LICENSE ├── README.md ├── argtable3.c ├── argtable3.h ├── examples │ ├── CMakeLists.txt │ ├── echo.c │ ├── ls.c │ ├── multisyntax.c │ ├── mv.c │ ├── myprog.c │ ├── myprog_C89.c │ ├── testargtable3.c │ └── uname.c └── tests │ ├── CMakeLists.txt │ ├── CuTest.c │ ├── CuTest.h │ ├── argtable3_private.h │ ├── testall.c │ ├── testargcmd.c │ ├── testargdate.c │ ├── testargdbl.c │ ├── testargdstr.c │ ├── testargfile.c │ ├── testarghashtable.c │ ├── testargint.c │ ├── testarglit.c │ ├── testargrex.c │ └── testargstr.c ├── lzma ├── Alloc.c ├── Alloc.h ├── CMakeLists.txt ├── LzFind.c ├── LzFind.h ├── LzHash.h ├── LzmaDec.c ├── LzmaDec.h ├── LzmaEnc.c ├── LzmaEnc.h ├── LzmaLib.c ├── LzmaLib.h └── Types.h ├── main.c └── main.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | cmake-build-debug 55 | cmake-build-release 56 | CMakeBuild 57 | .idea -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(broadcom_cfe_tool C) 3 | 4 | set(CMAKE_C_STANDARD 11) 5 | 6 | aux_source_directory(. DIR_SRCS) 7 | add_subdirectory(lzma) 8 | add_subdirectory(argtable) 9 | 10 | add_executable(broadcom_cfe_tool main.c) 11 | target_link_libraries(broadcom_cfe_tool lzma argtable) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # broadcom_cfe_tool 2 | A tool for compressing/decompressing data from/to Broadcom CFE file 3 | 4 | 5 | usage: 6 | ```powershell 7 | cp .\mtd0.bin .\mtd0_new.bin 8 | .\broadcom_cfe_tool.exe -d -i .\mtd0.bin -o phicomm_k2p_b1_cfe_nvram.txt 9 | .\broadcom_cfe_tool.exe -z -i .\phicomm_k2p_b1_cfe_nvram.txt -o .\mtd0_new.bin 10 | ``` 11 | 12 | --help: 13 | ``` 14 | -z, --compress compress NVRAM data to CFE file 15 | -d, --decompress decompress embedded NVRAM data from CFE file 16 | -b, --offset= offset within output to embed NVRAM (default 0x400) 17 | -c, --count= bytes of embed NVRAM to write (default 0x1000) 18 | -i, --input= input file 19 | -o, --output= output file 20 | ``` -------------------------------------------------------------------------------- /argtable/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(libargtable) 2 | aux_source_directory(. DIR_LIB_SRCS) 3 | add_library (argtable STATIC ${DIR_LIB_SRCS}) -------------------------------------------------------------------------------- /argtable/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of STEWART HEITMANN nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 20 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | 28 | NetBSD getopt library 29 | ===================== 30 | 31 | Copyright (c) 2000 The NetBSD Foundation, Inc. 32 | All rights reserved. 33 | 34 | This code is derived from software contributed to The NetBSD Foundation 35 | by Dieter Baron and Thomas Klausner. 36 | 37 | Redistribution and use in source and binary forms, with or without 38 | modification, are permitted provided that the following conditions 39 | are met: 40 | 1. Redistributions of source code must retain the above copyright 41 | notice, this list of conditions and the following disclaimer. 42 | 2. Redistributions in binary form must reproduce the above copyright 43 | notice, this list of conditions and the following disclaimer in the 44 | documentation and/or other materials provided with the distribution. 45 | 3. All advertising materials mentioning features or use of this software 46 | must display the following acknowledgement: 47 | This product includes software developed by the NetBSD 48 | Foundation, Inc. and its contributors. 49 | 4. Neither the name of The NetBSD Foundation nor the names of its 50 | contributors may be used to endorse or promote products derived 51 | from this software without specific prior written permission. 52 | 53 | THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 54 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 57 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 | POSSIBILITY OF SUCH DAMAGE. 64 | 65 | 66 | Tcl library 67 | =========== 68 | 69 | This software is copyrighted by the Regents of the University of 70 | California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState 71 | Corporation and other parties. The following terms apply to all files 72 | associated with the software unless explicitly disclaimed in 73 | individual files. 74 | 75 | The authors hereby grant permission to use, copy, modify, distribute, 76 | and license this software and its documentation for any purpose, provided 77 | that existing copyright notices are retained in all copies and that this 78 | notice is included verbatim in any distributions. No written agreement, 79 | license, or royalty fee is required for any of the authorized uses. 80 | Modifications to this software may be copyrighted by their authors 81 | and need not follow the licensing terms described here, provided that 82 | the new terms are clearly indicated on the first page of each file where 83 | they apply. 84 | 85 | IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY 86 | FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 87 | ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY 88 | DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE 89 | POSSIBILITY OF SUCH DAMAGE. 90 | 91 | THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, 92 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, 93 | FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE 94 | IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE 95 | NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR 96 | MODIFICATIONS. 97 | 98 | GOVERNMENT USE: If you are acquiring this software on behalf of the 99 | U.S. government, the Government shall have only "Restricted Rights" 100 | in the software and related documentation as defined in the Federal 101 | Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you 102 | are acquiring the software on behalf of the Department of Defense, the 103 | software shall be classified as "Commercial Computer Software" and the 104 | Government shall have only "Restricted Rights" as defined in Clause 105 | 252.227-7014 (b) (3) of DFARs. Notwithstanding the foregoing, the 106 | authors grant the U.S. Government and others acting in its behalf 107 | permission to use and distribute the software in accordance with the 108 | terms specified in this license. 109 | 110 | 111 | C Hash Table library 112 | ==================== 113 | 114 | Copyright (c) 2002, Christopher Clark 115 | All rights reserved. 116 | 117 | Redistribution and use in source and binary forms, with or without 118 | modification, are permitted provided that the following conditions 119 | are met: 120 | 121 | * Redistributions of source code must retain the above copyright 122 | notice, this list of conditions and the following disclaimer. 123 | 124 | * Redistributions in binary form must reproduce the above copyright 125 | notice, this list of conditions and the following disclaimer in the 126 | documentation and/or other materials provided with the distribution. 127 | 128 | * Neither the name of the original author; nor the names of any contributors 129 | may be used to endorse or promote products derived from this software 130 | without specific prior written permission. 131 | 132 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 133 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 134 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 135 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 136 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 137 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 138 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 139 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 140 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 141 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 142 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 143 | 144 | 145 | The Better String library 146 | ========================= 147 | 148 | Copyright (c) 2014, Paul Hsieh 149 | All rights reserved. 150 | 151 | Redistribution and use in source and binary forms, with or without 152 | modification, are permitted provided that the following conditions are met: 153 | 154 | * Redistributions of source code must retain the above copyright notice, this 155 | list of conditions and the following disclaimer. 156 | 157 | * Redistributions in binary form must reproduce the above copyright notice, 158 | this list of conditions and the following disclaimer in the documentation 159 | and/or other materials provided with the distribution. 160 | 161 | * Neither the name of bstrlib nor the names of its 162 | contributors may be used to endorse or promote products derived from 163 | this software without specific prior written permission. 164 | 165 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 166 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 168 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 169 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 170 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 171 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 172 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 173 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 174 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 175 | 176 | -------------------------------------------------------------------------------- /argtable/README.md: -------------------------------------------------------------------------------- 1 | 2 | Introduction of Argtable3 3 | ========================= 4 | 5 | **Argtable3** is an open source ANSI C library that parses GNU-style command-line 6 | options. It simplifies command-line parsing by defining a declarative-style API 7 | that you can use to specify what your command-line syntax looks like. Argtable3 8 | will automatically generate consistent error handling logic and textual 9 | descriptions of the command line syntax, which are essential but tedious to 10 | implement for a robust CLI program. 11 | 12 | 13 | Quick Start 14 | ----------- 15 | 16 | > We no longer provide the amalgamation source code (`argtable3.c` and `argtable3.h`) 17 | > in the source code repository. You can get the amalgamation distribution either 18 | > from the release page (`argtable-3.x.x-amalgamation.zip`), or generate the 19 | > distribution yourself by using the generator under the `tools` directory: 20 | > 21 | > 1. Navigate to the `tools` directory. 22 | > 2. Run `./build dist`, which will generate the distribution under the `/dist` 23 | > directory. 24 | 25 | 26 | Argtable3 is a single-file ANSI-C library. All you have to do is adding 27 | `argtable3.c` to your projects, and including `argtable3.h` in your source code. 28 | 29 | To build the library, examples, and unit tests, use CMake to generate out-of-source build: 30 | 31 | * If you use GCC (Linux, MinGW, Cygwin), run: 32 | 33 | ``` 34 | $ mkdir build 35 | $ cd build 36 | $ cmake .. 37 | $ make 38 | $ make test 39 | ``` 40 | 41 | To cleanup, type: 42 | 43 | ``` 44 | $ make clean 45 | ``` 46 | 47 | * If you use Microsoft Visual C++ compiler, run: 48 | 49 | ``` 50 | $ mkdir build 51 | $ cd build 52 | $ cmake -G "Visual Studio 15 2017 Win64" .. 53 | ``` 54 | 55 | Now you can use Visual Studio 2017 to open the generated solution. To cleanup, 56 | just remove the `build` directory. 57 | 58 | 59 | To build a tagged version, go to the project root directory, and use the 60 | `Makefile` in the project root folder to check out the specified version: 61 | 62 | ``` 63 | $ make taglist 64 | Available TAGs: 65 | v3.1.1.432a160 66 | $ make co TAG=v3.1.1.432a160 67 | $ cd .tags/v3.1.1.432a160 68 | $ mkdir build 69 | $ cd build 70 | $ cmake .. 71 | $ make 72 | $ make test 73 | ``` 74 | 75 | You will find the shared library (or Windows DLL), static library, and the 76 | amalgamation distribution under the build directory. 77 | 78 | 79 | Documentation and Examples 80 | -------------------------- 81 | 82 | To learn how to use the Argtable3 API, you can see the documentation on the web 83 | site, or examples in the `examples` folder. 84 | 85 | 86 | Unit Tests 87 | ---------- 88 | 89 | Argtable3 is a BSD-licensed open source library, so you can modify the library 90 | anyway you want. However, before committing your code to your own repository or 91 | the Argtable3 official repository, please make sure your changes can pass the 92 | unit tests included in the distribution. 93 | 94 | 95 | Authors 96 | ------- 97 | 98 | Argtable is Copyright (C) 1998-2001,2003-2011 Stewart Heitmann. 99 | Parts are Copyright (C) 1989-1994, 1996-1999, 2001, 2003 100 | Free Software Foundation, Inc. 101 | 102 | Argtable was written by Stewart Heitmann 103 | 104 | Argtable is now maintained by Tom G. Huang 105 | The project homepage of argtable 3.x is http://www.argtable.org 106 | The project homepage of argtable 2.x is http://argtable.sourceforge.net/ 107 | 108 | Here is a list of contributors who have helped to improve argtable: 109 | 110 | - **Nina Clemson**: Editing the original argtable-1.0 documentation. 111 | - **Livio Bertacco**: For bug fixes and the argtable-2.x Visual C++ Makefiles. 112 | - **Justin Dearing**: For bug fixes and Windows DLL support, plus code support for the Open Watcom compiler and help with the Mac OS X configuration. 113 | - **Asa Packer**: Contributing bug fixes and upgrades to the Visual C++ Makefiles. 114 | - **Danilo Cicerone**: For the Italian translation of "Introduction to Argtable-2x" on http://www.digitazero.org. 115 | - **Uli Fouquet**: For configuration patches and documentation related to cross-compiling argtable from Unix to Windows, as well as providing the arg_print_glossary_gnu function. 116 | - **Shachar Shemesh**: For Debian package integration and kick-starting the migration to automake/autoconf. 117 | - **Jasper Lievisse Adriaanse**: Maintaining the argtable package in OpenBSD ports. 118 | - **Ulrich Mohr**: For bug fixes relating to Texas Instrument DSP platforms. 119 | - **John Vickers**: For bug fixes relating to Solaris/Motorola platforms. 120 | - **Steve O'Neil**: For bug fixes relating to Solaris/Motorola platforms. 121 | - **Lori A. Pritchett-Sheats**: Fixing a makefile bug relating to "make dist". 122 | - **Paolo Bormida**: For instructions on building argtable with date and regex support on Windows. 123 | - **Michel Valin**: For bug fixes relating to the configure scripts on IBM AIX platforms and instructions on compiling the example code under AIX. 124 | - **Steve Christensen**: Providing prebuilt packages for SPARC/Solaris and x86/Solaris platforms on www.sunfreeware.com. 125 | - **Jess Portnoy**: Reworking the rpm package and integrating argtable into Fedora Linux. 126 | - **Michael Brown**: Incorporating support for pkg-config into the autoconf scripts. 127 | - **Alexander Lindert**: For extensions to the parser to support hex, octal and binary integer formats as well as KB/MB/GB suffixes. 128 | - **Rob Zaborowski**: Providing build configuration files for CMake. 129 | - **Moczik Gabor**: For bug fixes relating to the parsing of filepaths and filename extensions. 130 | 131 | Argtable 2.x uses `getopt` from GNU, which is LGPL licensed. In order to switch to BSD, we replaced GNU `getopt` with NetBSD `getopt`. We really appreciate this high quality library that lays the foundation of Argtable 3.x and here is its copyright notice: 132 | 133 | ``` 134 | Copyright (c) 2000 The NetBSD Foundation, Inc. 135 | All rights reserved. 136 | 137 | This code is derived from software contributed to The NetBSD Foundation 138 | by Dieter Baron and Thomas Klausner. 139 | 140 | Redistribution and use in source and binary forms, with or without 141 | modification, are permitted provided that the following conditions 142 | are met: 143 | 1. Redistributions of source code must retain the above copyright 144 | notice, this list of conditions and the following disclaimer. 145 | 2. Redistributions in binary form must reproduce the above copyright 146 | notice, this list of conditions and the following disclaimer in the 147 | documentation and/or other materials provided with the distribution. 148 | 3. All advertising materials mentioning features or use of this software 149 | must display the following acknowledgement: 150 | This product includes software developed by the NetBSD 151 | Foundation, Inc. and its contributors. 152 | 4. Neither the name of The NetBSD Foundation nor the names of its 153 | contributors may be used to endorse or promote products derived 154 | from this software without specific prior written permission. 155 | 156 | THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 157 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 158 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 159 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 160 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 161 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 162 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 163 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 164 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 165 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 166 | POSSIBILITY OF SUCH DAMAGE. 167 | ``` 168 | -------------------------------------------------------------------------------- /argtable/argtable3.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * argtable3: Declares the main interfaces of the library 3 | * 4 | * This file is part of the argtable3 library. 5 | * 6 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions are met: 12 | * * Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * * Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | ******************************************************************************/ 32 | 33 | #ifndef ARGTABLE3 34 | #define ARGTABLE3 35 | 36 | #include /* FILE */ 37 | #include /* struct tm */ 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | #define ARG_REX_ICASE 1 44 | #define ARG_DSTR_SIZE 200 45 | #define ARG_CMD_NAME_LEN 100 46 | #define ARG_CMD_DESCRIPTION_LEN 256 47 | 48 | #ifndef ARG_REPLACE_GETOPT 49 | #define ARG_REPLACE_GETOPT 1 /* use the embedded getopt as the system getopt(3) */ 50 | #endif /* ARG_REPLACE_GETOPT */ 51 | 52 | /* bit masks for arg_hdr.flag */ 53 | enum { ARG_TERMINATOR = 0x1, ARG_HASVALUE = 0x2, ARG_HASOPTVALUE = 0x4 }; 54 | 55 | #if defined(_WIN32) 56 | #if defined(argtable3_EXPORTS) 57 | #define ARG_EXTERN __declspec(dllexport) 58 | #elif defined(argtable3_IMPORTS) 59 | #define ARG_EXTERN __declspec(dllimport) 60 | #else 61 | #define ARG_EXTERN 62 | #endif 63 | #else 64 | #define ARG_EXTERN 65 | #endif 66 | 67 | typedef struct _internal_arg_dstr* arg_dstr_t; 68 | typedef void* arg_cmd_itr_t; 69 | 70 | typedef void(arg_resetfn)(void* parent); 71 | typedef int(arg_scanfn)(void* parent, const char* argval); 72 | typedef int(arg_checkfn)(void* parent); 73 | typedef void(arg_errorfn)(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname); 74 | typedef void(arg_dstr_freefn)(char* buf); 75 | typedef int(arg_cmdfn)(int argc, char* argv[], arg_dstr_t res); 76 | typedef int(arg_comparefn)(const void* k1, const void* k2); 77 | 78 | /* 79 | * The arg_hdr struct defines properties that are common to all arg_xxx structs. 80 | * The argtable library requires each arg_xxx struct to have an arg_hdr 81 | * struct as its first data member. 82 | * The argtable library functions then use this data to identify the 83 | * properties of the command line option, such as its option tags, 84 | * datatype string, and glossary strings, and so on. 85 | * Moreover, the arg_hdr struct contains pointers to custom functions that 86 | * are provided by each arg_xxx struct which perform the tasks of parsing 87 | * that particular arg_xxx arguments, performing post-parse checks, and 88 | * reporting errors. 89 | * These functions are private to the individual arg_xxx source code 90 | * and are the pointer to them are initiliased by that arg_xxx struct's 91 | * constructor function. The user could alter them after construction 92 | * if desired, but the original intention is for them to be set by the 93 | * constructor and left unaltered. 94 | */ 95 | struct arg_hdr { 96 | char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */ 97 | const char* shortopts; /* String defining the short options */ 98 | const char* longopts; /* String defiing the long options */ 99 | const char* datatype; /* Description of the argument data type */ 100 | const char* glossary; /* Description of the option as shown by arg_print_glossary function */ 101 | int mincount; /* Minimum number of occurences of this option accepted */ 102 | int maxcount; /* Maximum number of occurences if this option accepted */ 103 | void* parent; /* Pointer to parent arg_xxx struct */ 104 | arg_resetfn* resetfn; /* Pointer to parent arg_xxx reset function */ 105 | arg_scanfn* scanfn; /* Pointer to parent arg_xxx scan function */ 106 | arg_checkfn* checkfn; /* Pointer to parent arg_xxx check function */ 107 | arg_errorfn* errorfn; /* Pointer to parent arg_xxx error function */ 108 | void* priv; /* Pointer to private header data for use by arg_xxx functions */ 109 | }; 110 | 111 | struct arg_rem { 112 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 113 | }; 114 | 115 | struct arg_lit { 116 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 117 | int count; /* Number of matching command line args */ 118 | }; 119 | 120 | struct arg_int { 121 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 122 | int count; /* Number of matching command line args */ 123 | int* ival; /* Array of parsed argument values */ 124 | }; 125 | 126 | struct arg_dbl { 127 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 128 | int count; /* Number of matching command line args */ 129 | double* dval; /* Array of parsed argument values */ 130 | }; 131 | 132 | struct arg_str { 133 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 134 | int count; /* Number of matching command line args */ 135 | const char** sval; /* Array of parsed argument values */ 136 | }; 137 | 138 | struct arg_rex { 139 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 140 | int count; /* Number of matching command line args */ 141 | const char** sval; /* Array of parsed argument values */ 142 | }; 143 | 144 | struct arg_file { 145 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 146 | int count; /* Number of matching command line args*/ 147 | const char** filename; /* Array of parsed filenames (eg: /home/foo.bar) */ 148 | const char** basename; /* Array of parsed basenames (eg: foo.bar) */ 149 | const char** extension; /* Array of parsed extensions (eg: .bar) */ 150 | }; 151 | 152 | struct arg_date { 153 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 154 | const char* format; /* strptime format string used to parse the date */ 155 | int count; /* Number of matching command line args */ 156 | struct tm* tmval; /* Array of parsed time values */ 157 | }; 158 | 159 | enum { ARG_ELIMIT = 1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG }; 160 | struct arg_end { 161 | struct arg_hdr hdr; /* The mandatory argtable header struct */ 162 | int count; /* Number of errors encountered */ 163 | int* error; /* Array of error codes */ 164 | void** parent; /* Array of pointers to offending arg_xxx struct */ 165 | const char** argval; /* Array of pointers to offending argv[] string */ 166 | }; 167 | 168 | typedef struct arg_cmd_info { 169 | char name[ARG_CMD_NAME_LEN]; 170 | char description[ARG_CMD_DESCRIPTION_LEN]; 171 | arg_cmdfn* proc; 172 | } arg_cmd_info_t; 173 | 174 | /**** arg_xxx constructor functions *********************************/ 175 | 176 | ARG_EXTERN struct arg_rem* arg_rem(const char* datatype, const char* glossary); 177 | 178 | ARG_EXTERN struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary); 179 | ARG_EXTERN struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary); 180 | ARG_EXTERN struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary); 181 | 182 | ARG_EXTERN struct arg_key* arg_key0(const char* keyword, int flags, const char* glossary); 183 | ARG_EXTERN struct arg_key* arg_key1(const char* keyword, int flags, const char* glossary); 184 | ARG_EXTERN struct arg_key* arg_keyn(const char* keyword, int flags, int mincount, int maxcount, const char* glossary); 185 | 186 | ARG_EXTERN struct arg_int* arg_int0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 187 | ARG_EXTERN struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 188 | ARG_EXTERN struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); 189 | 190 | ARG_EXTERN struct arg_dbl* arg_dbl0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 191 | ARG_EXTERN struct arg_dbl* arg_dbl1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 192 | ARG_EXTERN struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); 193 | 194 | ARG_EXTERN struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 195 | ARG_EXTERN struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 196 | ARG_EXTERN struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); 197 | 198 | ARG_EXTERN struct arg_rex* arg_rex0(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary); 199 | ARG_EXTERN struct arg_rex* arg_rex1(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary); 200 | ARG_EXTERN struct arg_rex* arg_rexn(const char* shortopts, 201 | const char* longopts, 202 | const char* pattern, 203 | const char* datatype, 204 | int mincount, 205 | int maxcount, 206 | int flags, 207 | const char* glossary); 208 | 209 | ARG_EXTERN struct arg_file* arg_file0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 210 | ARG_EXTERN struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); 211 | ARG_EXTERN struct arg_file* arg_filen(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); 212 | 213 | ARG_EXTERN struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary); 214 | ARG_EXTERN struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary); 215 | ARG_EXTERN struct arg_date* arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary); 216 | 217 | ARG_EXTERN struct arg_end* arg_end(int maxerrors); 218 | 219 | #define ARG_DSTR_STATIC ((arg_dstr_freefn*)0) 220 | #define ARG_DSTR_VOLATILE ((arg_dstr_freefn*)1) 221 | #define ARG_DSTR_DYNAMIC ((arg_dstr_freefn*)3) 222 | 223 | /**** other functions *******************************************/ 224 | ARG_EXTERN int arg_nullcheck(void** argtable); 225 | ARG_EXTERN int arg_parse(int argc, char** argv, void** argtable); 226 | ARG_EXTERN void arg_print_option(FILE* fp, const char* shortopts, const char* longopts, const char* datatype, const char* suffix); 227 | ARG_EXTERN void arg_print_syntax(FILE* fp, void** argtable, const char* suffix); 228 | ARG_EXTERN void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix); 229 | ARG_EXTERN void arg_print_glossary(FILE* fp, void** argtable, const char* format); 230 | ARG_EXTERN void arg_print_glossary_gnu(FILE* fp, void** argtable); 231 | ARG_EXTERN void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname); 232 | ARG_EXTERN void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix); 233 | ARG_EXTERN void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix); 234 | ARG_EXTERN void arg_print_syntaxv_ds(arg_dstr_t ds, void** argtable, const char* suffix); 235 | ARG_EXTERN void arg_print_glossary_ds(arg_dstr_t ds, void** argtable, const char* format); 236 | ARG_EXTERN void arg_print_glossary_gnu_ds(arg_dstr_t ds, void** argtable); 237 | ARG_EXTERN void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname); 238 | ARG_EXTERN void arg_freetable(void** argtable, size_t n); 239 | 240 | ARG_EXTERN arg_dstr_t arg_dstr_create(); 241 | ARG_EXTERN void arg_dstr_destroy(arg_dstr_t ds); 242 | ARG_EXTERN void arg_dstr_reset(arg_dstr_t ds); 243 | ARG_EXTERN void arg_dstr_free(arg_dstr_t ds); 244 | ARG_EXTERN void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc); 245 | ARG_EXTERN void arg_dstr_cat(arg_dstr_t ds, char* str); 246 | ARG_EXTERN void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...); 247 | ARG_EXTERN char* arg_dstr_cstr(arg_dstr_t ds); 248 | 249 | ARG_EXTERN void arg_cmd_init(void); 250 | ARG_EXTERN void arg_cmd_uninit(void); 251 | ARG_EXTERN void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description); 252 | ARG_EXTERN void arg_cmd_unregister(const char* name); 253 | ARG_EXTERN int arg_cmd_dispatch(const char* name, int argc, char* argv[], arg_dstr_t res); 254 | ARG_EXTERN unsigned int arg_cmd_count(void); 255 | ARG_EXTERN arg_cmd_info_t* arg_cmd_info(const char* name); 256 | ARG_EXTERN arg_cmd_itr_t arg_cmd_itr_create(void); 257 | ARG_EXTERN void arg_cmd_itr_destroy(arg_cmd_itr_t itr); 258 | ARG_EXTERN int arg_cmd_itr_advance(arg_cmd_itr_t itr); 259 | ARG_EXTERN char* arg_cmd_itr_key(arg_cmd_itr_t itr); 260 | ARG_EXTERN arg_cmd_info_t* arg_cmd_itr_value(arg_cmd_itr_t itr); 261 | ARG_EXTERN int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k); 262 | ARG_EXTERN void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn); 263 | ARG_EXTERN void arg_make_get_help_msg(arg_dstr_t res); 264 | ARG_EXTERN void arg_make_help_msg(arg_dstr_t ds, char* cmd_name, void** argtable); 265 | ARG_EXTERN void arg_make_syntax_err_msg(arg_dstr_t ds, void** argtable, struct arg_end* end); 266 | ARG_EXTERN int arg_make_syntax_err_help_msg(arg_dstr_t ds, char* name, int help, int nerrors, void** argtable, struct arg_end* end, int* exitcode); 267 | ARG_EXTERN void arg_set_module_name(const char* name); 268 | ARG_EXTERN void arg_set_module_version(int major, int minor, int patch, const char* tag); 269 | 270 | /**** deprecated functions, for back-compatibility only ********/ 271 | ARG_EXTERN void arg_free(void** argtable); 272 | 273 | #ifdef __cplusplus 274 | } 275 | #endif 276 | #endif 277 | -------------------------------------------------------------------------------- /argtable/examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This file is part of the argtable3 library. 3 | # 4 | # Copyright (C) 2016-2019 Tom G. Huang 5 | # 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 are met: 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above copyright 13 | # notice, this list of conditions and the following disclaimer in the 14 | # documentation and/or other materials provided with the distribution. 15 | # * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | # may be used to endorse or promote products derived from this software 17 | # without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | # ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ################################################################################ 30 | 31 | file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples *.c) 32 | 33 | if(NOT ARGTABLE3_BUILD_STATIC_EXAMPLES) 34 | add_definitions(-Dargtable3_IMPORTS) 35 | endif() 36 | 37 | if(UNIX) 38 | set(ARGTABLE3_EXTRA_LIBS m) 39 | endif() 40 | 41 | foreach(examples_src ${EXAMPLES_SOURCES}) 42 | string(REPLACE ".c" "" examplename ${examples_src}) 43 | add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src}) 44 | target_include_directories(${examplename} PRIVATE ${PROJECT_SOURCE_DIR}/src) 45 | if(ARGTABLE3_BUILD_STATIC_EXAMPLES) 46 | target_link_libraries(${examplename} argtable3_static ${ARGTABLE3_EXTRA_LIBS}) 47 | else() 48 | target_link_libraries(${examplename} argtable3 ${ARGTABLE3_EXTRA_LIBS}) 49 | endif() 50 | endforeach() 51 | -------------------------------------------------------------------------------- /argtable/examples/echo.c: -------------------------------------------------------------------------------- 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 | * 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 | 35 | #include "argtable3.h" 36 | 37 | /* Here we only approximate the echo functionality */ 38 | void mymain(int n, int e, int E, const char** strings, int nstrings) 39 | { 40 | int j; 41 | 42 | printf("option -n = %s\n", ((n)?"YES":"NO")); 43 | printf("option -e = %s\n", ((e)?"YES":"NO")); 44 | printf("option -E = %s\n", ((E)?"YES":"NO")); 45 | for (j=0; js"); 57 | struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); 58 | struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit"); 59 | struct arg_str *strs = arg_strn(NULL,NULL,"STRING",0,argc+2,NULL); 60 | struct arg_end *end = arg_end(20); 61 | void* argtable[] = {n,e,E,help,vers,strs,end}; 62 | const char* progname = "echo"; 63 | int exitcode=0; 64 | int nerrors; 65 | 66 | /* verify the argtable[] entries were allocated sucessfully */ 67 | if (arg_nullcheck(argtable) != 0) 68 | { 69 | /* NULL entries were detected, some allocations must have failed */ 70 | printf("%s: insufficient memory\n",progname); 71 | exitcode=1; 72 | goto exit; 73 | } 74 | 75 | /* Parse the command line as defined by argtable[] */ 76 | nerrors = arg_parse(argc,argv,argtable); 77 | 78 | /* special case: '--help' takes precedence over error reporting */ 79 | if (help->count > 0) 80 | { 81 | printf("Usage: %s", progname); 82 | arg_print_syntax(stdout,argtable,"\n"); 83 | printf("Echo the STRINGs to standard output.\n\n"); 84 | arg_print_glossary(stdout,argtable," %-10s %s\n"); 85 | printf("\nWithout -E, the following sequences are recognized and interpolated:\n\n" 86 | " \\NNN the character whose ASCII code is NNN (octal)\n" 87 | " \\\\ backslash\n" 88 | " \\a alert (BEL)\n" 89 | " \\b backspace\n" 90 | " \\c suppress trailing newline\n" 91 | " \\f form feed\n" 92 | " \\n new line\n" 93 | " \\r carriage return\n" 94 | " \\t horizontal tab\n" 95 | " \\v vertical tab\n\n" 96 | "Report bugs to .\n"); 97 | exitcode=0; 98 | goto exit; 99 | } 100 | 101 | /* special case: '--version' takes precedence error reporting */ 102 | if (vers->count > 0) 103 | { 104 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 105 | printf("September 2003, Stewart Heitmann\n"); 106 | exitcode=0; 107 | goto exit; 108 | } 109 | 110 | /* If the parser returned any errors then display them and exit */ 111 | if (nerrors > 0) 112 | { 113 | /* Display the error details contained in the arg_end struct.*/ 114 | arg_print_errors(stdout,end,progname); 115 | printf("Try '%s --help' for more information.\n",progname); 116 | exitcode=1; 117 | goto exit; 118 | } 119 | 120 | /* Command line parsing is complete, do the main processing */ 121 | mymain(n->count, e->count, E->count, strs->sval, strs->count); 122 | 123 | exit: 124 | /* deallocate each non-null entry in argtable[] */ 125 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 126 | 127 | return exitcode; 128 | } 129 | -------------------------------------------------------------------------------- /argtable/examples/ls.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Example source code for using the argtable3 library to implement: 3 | * 4 | * ls [-aAbBcCdDfFgGhHiklLmnNopqQrRsStuUvxX1] [--author] 5 | * [--block-size=SIZE] [--color=[WHEN]] [--format=WORD] [--full-time] 6 | * [--si] [--dereference-command-line-symlink-to-dir] [--indicator-style=WORD] 7 | * [-I PATTERN] [--show-control-chars] [--quoting-style=WORD] [--sort=WORD] 8 | * [--time=WORD] [--time-style=STYLE] [-T COLS] [-w COLS] [--help] 9 | * [--version] [FILE]... 10 | * 11 | * This file is part of the argtable3 library. 12 | * 13 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 14 | * 15 | * All rights reserved. 16 | * 17 | * Redistribution and use in source and binary forms, with or without 18 | * modification, are permitted provided that the following conditions are met: 19 | * * Redistributions of source code must retain the above copyright 20 | * notice, this list of conditions and the following disclaimer. 21 | * * Redistributions in binary form must reproduce the above copyright 22 | * notice, this list of conditions and the following disclaimer in the 23 | * documentation and/or other materials provided with the distribution. 24 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 25 | * may be used to endorse or promote products derived from this software 26 | * without specific prior written permission. 27 | * 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 32 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 33 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 35 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | ******************************************************************************/ 39 | 40 | #include "argtable3.h" 41 | 42 | /* These variables hold the values parsed from the comand line by arg_parse() */ 43 | struct arg_lit *a, *A, *author, *b, *B, *c, *C, *d, *D, *f, *F, *fulltime; 44 | struct arg_lit *g, *G, *h, *H, *si, *deref, *i, *k, *l, *L, *m, *n, *N, *o, *p; 45 | struct arg_lit *q, *shcont, *Q, *r, *R, *s, *S, *t, *u, *U, *v, *x, *X, *one; 46 | struct arg_lit *help, *version; 47 | struct arg_int *blocksize, *T, *w; 48 | struct arg_str *color, *format, *indic, *I, *Qstyle, *sort, *Time, *timesty; 49 | struct arg_file *files; 50 | struct arg_end *end; 51 | 52 | /* Here we simply echo the command line option values as a demonstration. */ 53 | /* In a real program, this is where we would perform the main processing. */ 54 | int mymain(void) 55 | { 56 | int j; 57 | 58 | if (a->count > 0) 59 | printf("a=YES\n"); 60 | if (A->count > 0) 61 | printf("A=YES\n"); 62 | if (author->count > 0) 63 | printf("author=YES\n"); 64 | if (b->count > 0) 65 | printf("b=YES\n"); 66 | if (blocksize->count > 0) 67 | printf("blocksize=%d\n",blocksize->count); 68 | if (B->count > 0) 69 | printf("B=YES\n"); 70 | if (c->count > 0) 71 | printf("c=YES\n"); 72 | if (C->count > 0) 73 | printf("C=YES\n"); 74 | if (color->count > 0) 75 | printf("color=%s\n",color->sval[0]); 76 | if (d->count > 0) 77 | printf("d=YES\n"); 78 | if (D->count > 0) 79 | printf("D=YES\n"); 80 | if (f->count > 0) 81 | printf("f=YES\n"); 82 | if (F->count > 0) 83 | printf("F=YES\n"); 84 | if (format->count > 0) 85 | printf("format=%s\n",format->sval[0]); 86 | if (fulltime->count > 0) 87 | printf("fulltime=YES\n"); 88 | if (g->count > 0) 89 | printf("g=YES\n"); 90 | if (G->count > 0) 91 | printf("G=YES\n"); 92 | if (h->count > 0) 93 | printf("h=YES\n"); 94 | if (si->count > 0) 95 | printf("si=YES\n"); 96 | if (H->count > 0) 97 | printf("H=YES\n"); 98 | if (deref->count > 0) 99 | printf("deref=YES\n"); 100 | if (indic->count > 0) 101 | printf("indic=%s\n",indic->sval[0]); 102 | if (i->count > 0) 103 | printf("i=YES\n"); 104 | if (I->count > 0) 105 | printf("I=%s\n",I->sval[0]); 106 | if (k->count > 0) 107 | printf("k=YES\n"); 108 | if (l->count > 0) 109 | printf("l=YES\n"); 110 | if (L->count > 0) 111 | printf("L=YES\n"); 112 | if (m->count > 0) 113 | printf("m=YES\n"); 114 | if (n->count > 0) 115 | printf("n=YES\n"); 116 | if (N->count > 0) 117 | printf("N=YES\n"); 118 | if (o->count > 0) 119 | printf("o=YES\n"); 120 | if (p->count > 0) 121 | printf("p=YES\n"); 122 | if (q->count > 0) 123 | printf("q=YES\n"); 124 | if (shcont->count > 0) 125 | printf("shcont=YES\n"); 126 | if (Q->count > 0) 127 | printf("Q=YES\n"); 128 | if (Qstyle->count > 0) 129 | printf("Qstyle=%s\n",Qstyle->sval[0]); 130 | if (r->count > 0) 131 | printf("r=YES\n"); 132 | if (R->count > 0) 133 | printf("R=YES\n"); 134 | if (s->count > 0) 135 | printf("s=YES\n"); 136 | if (S->count > 0) 137 | printf("S=YES\n"); 138 | if (sort->count > 0) 139 | printf("sort=%s\n",sort->sval[0]); 140 | if (Time->count > 0) 141 | printf("time=%s\n",Time->sval[0]); 142 | if (timesty->count > 0) 143 | printf("timesty=%s\n",timesty->sval[0]); 144 | if (t->count > 0) 145 | printf("t=YES\n"); 146 | if (T->count > 0) 147 | printf("T=%d\n",T->ival[0]); 148 | if (u->count > 0) 149 | printf("u=YES\n"); 150 | if (U->count > 0) 151 | printf("U=YES\n"); 152 | if (v->count > 0) 153 | printf("v=YES\n"); 154 | if (w->count > 0) 155 | printf("w=%d\n",w->ival[0]); 156 | if (x->count > 0) 157 | printf("x=YES\n"); 158 | if (X->count > 0) 159 | printf("X=YES\n"); 160 | if (one->count > 0) 161 | printf("1=YES\n"); 162 | 163 | /* print the filenames */ 164 | for (j=0; jcount; j++) 165 | printf("filename[%d] = \"%s\"\n", j, files->filename[j]); 166 | 167 | return 0; 168 | } 169 | 170 | 171 | int main(int argc, char **argv) 172 | { 173 | /* The argtable[] entries define the command line options */ 174 | void *argtable[] = { 175 | a = arg_lit0("a", "all", "do not hide entries starting with ."), 176 | A = arg_lit0("A", "almost-all", "do not list implied . and .."), 177 | author = arg_lit0(NULL,"author", "print the author of each file"), 178 | b = arg_lit0("b", "escape", "print octal escapes for nongraphic characters"), 179 | blocksize = arg_int0(NULL,"block-size","SIZE", "use SIZE-byte blocks"), 180 | B = arg_lit0("B", "ignore-backups", "do not list implied entries ending with ~"), 181 | c = arg_lit0("c", NULL, "with -lt: sort by, and show, ctime (time of last"), 182 | arg_rem(NULL, " modification of file status information)"), 183 | arg_rem(NULL, " with -l: show ctime and sort by name"), 184 | arg_rem(NULL, " otherwise: sort by ctime"), 185 | C = arg_lit0("C", NULL, "list entries by columns"), 186 | color = arg_str0(NULL,"color","WHEN", "control whether color is used to distinguish file"), 187 | arg_rem(NULL, " types. WHEN may be `never', `always', or `auto'"), 188 | d = arg_lit0("d", "directory", "list directory entries instead of contents,"), 189 | arg_rem(NULL, " and do not dereference symbolic links"), 190 | D = arg_lit0("D", "dired", "generate output designed for Emacs' dired mode"), 191 | f = arg_lit0("f", NULL, "do not sort, enable -aU, disable -lst"), 192 | F = arg_lit0("F", "classify", "append indicator (one of */=@|) to entries"), 193 | format = arg_str0(NULL,"format","WORD", "across -x, commas -m, horizontal -x, long -l,"), 194 | arg_rem (NULL, " single-column -1, verbose -l, vertical -C"), 195 | fulltime = arg_lit0(NULL,"full-time", "like -l --time-style=full-iso"), 196 | g = arg_lit0("g", NULL, "like -l, but do not list owner"), 197 | G = arg_lit0("G", "no-group", "inhibit display of group information"), 198 | h = arg_lit0("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), 199 | si = arg_lit0(NULL,"si", "likewise, but use powers of 1000 not 1024"), 200 | H = arg_lit0("H", "dereference-command-line","follow symbolic links listed on the command line"), 201 | deref = arg_lit0(NULL,"dereference-command-line-symlink-to-dir","follow each command line symbolic link"), 202 | arg_rem(NULL, " that points to a directory"), 203 | indic = arg_str0(NULL,"indicator-style","WORD","append indicator with style WORD to entry names:"), 204 | arg_rem (NULL, " none (default), classify (-F), file-type (-p)"), 205 | i = arg_lit0("i", "inode", "print index number of each file"), 206 | I = arg_str0("I", "ignore","PATTERN", "do not list implied entries matching shell PATTERN"), 207 | k = arg_lit0("k", NULL, "like --block-size=1K"), 208 | l = arg_lit0("l", NULL, "use a long listing format"), 209 | L = arg_lit0("L", "dereference", "when showing file information for a symbolic"), 210 | arg_rem (NULL, " link, show information for the file the link"), 211 | arg_rem (NULL, " references rather than for the link itself"), 212 | m = arg_lit0("m", NULL, "fill width with a comma separated list of entries"), 213 | n = arg_lit0("n", "numeric-uid-gid", "like -l, but list numeric UIDs and GIDs"), 214 | N = arg_lit0("N", "literal", "print raw entry names (don't treat e.g. control"), 215 | arg_rem (NULL, " characters specially)"), 216 | o = arg_lit0("o", NULL, "like -l, but do not list group information"), 217 | p = arg_lit0("p", "file-type", "append indicator (one of /=@|) to entries"), 218 | q = arg_lit0("q", "hide-control-chars", "print ? instead of non graphic characters"), 219 | shcont = arg_lit0(NULL,"show-control-chars", "show non graphic characters as-is (default"), 220 | arg_rem (NULL, "unless program is `ls' and output is a terminal)"), 221 | Q = arg_lit0("Q", "quote-name", "enclose entry names in double quotes"), 222 | Qstyle = arg_str0(NULL,"quoting-style","WORD","use quoting style WORD for entry names:"), 223 | arg_rem (NULL, " literal, locale, shell, shell-always, c, escape"), 224 | r = arg_lit0("r", "reverse", "reverse order while sorting"), 225 | R = arg_lit0("R", "recursive", "list subdirectories recursively"), 226 | s = arg_lit0("s", "size", "print size of each file, in blocks"), 227 | S = arg_lit0("S", NULL, "sort by file size"), 228 | sort = arg_str0(NULL,"sort","WORD", "extension -X, none -U, size -S, time -t, version -v,"), 229 | arg_rem (NULL, "status -c, time -t, atime -u, access -u, use -u"), 230 | Time = arg_str0(NULL,"time","WORD", "show time as WORD instead of modification time:"), 231 | arg_rem (NULL, " atime, access, use, ctime or status; use"), 232 | arg_rem (NULL, " specified time as sort key if --sort=time"), 233 | timesty = arg_str0(NULL, "time-style","STYLE", "show times using style STYLE:"), 234 | arg_rem (NULL, " full-iso, long-iso, iso, locale, +FORMAT"), 235 | arg_rem (NULL, "FORMAT is interpreted like `date'; if FORMAT is"), 236 | arg_rem (NULL, "FORMAT1FORMAT2, FORMAT1 applies to"), 237 | arg_rem (NULL, "non-recent files and FORMAT2 to recent files;"), 238 | arg_rem (NULL, "if STYLE is prefixed with `posix-', STYLE"), 239 | arg_rem (NULL, "takes effect only outside the POSIX locale"), 240 | t = arg_lit0("t", NULL, "sort by modification time"), 241 | T = arg_int0("T", "tabsize", "COLS", "assume tab stops at each COLS instead of 8"), 242 | u = arg_lit0("u", NULL, "with -lt: sort by, and show, access time"), 243 | arg_rem (NULL, " with -l: show access time and sort by name"), 244 | arg_rem (NULL, " otherwise: sort by access time"), 245 | U = arg_lit0("U", NULL, "do not sort; list entries in directory order"), 246 | v = arg_lit0("v", NULL, "sort by version"), 247 | w = arg_int0("w", "width", "COLS", "assume screen width instead of current value"), 248 | x = arg_lit0("x", NULL, "list entries by lines instead of by columns"), 249 | X = arg_lit0("X", NULL, "sort alphabetically by entry extension"), 250 | one = arg_lit0("1", NULL, "list one file per line"), 251 | help = arg_lit0(NULL,"help", "display this help and exit"), 252 | version = arg_lit0(NULL,"version", "display version information and exit"), 253 | files = arg_filen(NULL, NULL, "FILE", 0, argc+2, NULL), 254 | end = arg_end(20), 255 | }; 256 | const char *progname = "ls"; 257 | int exitcode=0; 258 | int nerrors; 259 | 260 | /* verify the argtable[] entries were allocated sucessfully */ 261 | if (arg_nullcheck(argtable) != 0) 262 | { 263 | /* NULL entries were detected, some allocations must have failed */ 264 | printf("%s: insufficient memory\n",progname); 265 | exitcode=1; 266 | goto exit; 267 | } 268 | 269 | /* allow optional argument values for --color */ 270 | /* and set the default value to "always" */ 271 | color->hdr.flag |= ARG_HASOPTVALUE; 272 | color->sval[0] = "always"; 273 | 274 | /* Parse the command line as defined by argtable[] */ 275 | nerrors = arg_parse(argc,argv,argtable); 276 | 277 | /* special case: '--help' takes precedence over error reporting */ 278 | if (help->count > 0) 279 | { 280 | printf("Usage: %s", progname); 281 | arg_print_syntax(stdout,argtable,"\n"); 282 | printf("List information about the FILE(s) (the current directory by default).\n"); 283 | printf("Sort entries alphabetically if none of -cftuSUX nor --sort.\n\n"); 284 | arg_print_glossary(stdout,argtable," %-25s %s\n"); 285 | printf("\nSIZE may be (or may be an integer optionally followed by) one of following:\n" 286 | "kB 1000, K 1024, MB 1,000,000, M 1,048,576, and so on for G, T, P, E, Z, Y.\n\n" 287 | "By default, color is not used to distinguish types of files. That is\n" 288 | "equivalent to using --color=none. Using the --color option without the\n" 289 | "optional WHEN argument is equivalent to using --color=always. With\n" 290 | "--color=auto, color codes are output only if standard output is connected\n" 291 | "to a terminal (tty).\n\n" 292 | "Report bugs to .\n"); 293 | exitcode=0; 294 | goto exit; 295 | } 296 | 297 | /* special case: '--version' takes precedence error reporting */ 298 | if (version->count > 0) 299 | { 300 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 301 | printf("September 2003, Stewart Heitmann\n"); 302 | exitcode=0; 303 | goto exit; 304 | } 305 | 306 | /* If the parser returned any errors then display them and exit */ 307 | if (nerrors > 0) 308 | { 309 | /* Display the error details contained in the arg_end struct.*/ 310 | arg_print_errors(stdout,end,progname); 311 | printf("Try '%s --help' for more information.\n",progname); 312 | exitcode=1; 313 | goto exit; 314 | } 315 | 316 | /* Command line parsing is complete, do the main processing */ 317 | exitcode = mymain(); 318 | 319 | exit: 320 | /* deallocate each non-null entry in argtable[] */ 321 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 322 | 323 | return exitcode; 324 | } 325 | 326 | 327 | -------------------------------------------------------------------------------- /argtable/examples/multisyntax.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Example source code for using the argtable3 library to implement 3 | * a multi-syntax command line argument program 4 | * 5 | * usage 1: multisyntax [-nvR] insert []... [-o ] 6 | * usage 2: multisyntax [-nv] remove 7 | * usage 3: multisyntax [-v] search [-o ] 8 | * usage 4: multisyntax [--help] [--version] 9 | * 10 | * This file is part of the argtable3 library. 11 | * 12 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 13 | * 14 | * All rights reserved. 15 | * 16 | * Redistribution and use in source and binary forms, with or without 17 | * modification, are permitted provided that the following conditions are met: 18 | * * Redistributions of source code must retain the above copyright 19 | * notice, this list of conditions and the following disclaimer. 20 | * * Redistributions in binary form must reproduce the above copyright 21 | * notice, this list of conditions and the following disclaimer in the 22 | * documentation and/or other materials provided with the distribution. 23 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 24 | * may be used to endorse or promote products derived from this software 25 | * without specific prior written permission. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 31 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 32 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 33 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 34 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 | ******************************************************************************/ 38 | 39 | #include "argtable3.h" 40 | 41 | #define REG_EXTENDED 1 42 | #define REG_ICASE (REG_EXTENDED << 1) 43 | 44 | /* mymain1 implements the actions for syntax 1 */ 45 | int mymain1(int n, int v, int R, const char *outfile, 46 | const char **infiles, int ninfiles) 47 | { 48 | int i; 49 | printf("syntax 1 matched OK:\n"); 50 | printf("n=%d\n", n); 51 | printf("v=%d\n", v); 52 | printf("R=%d\n", R); 53 | printf("outfile=\"%s\"\n", outfile); 54 | for (i=0; i [file]... -o */ 122 | struct arg_rex *cmd1 = arg_rex1(NULL, NULL, "insert", NULL, REG_ICASE, NULL); 123 | struct arg_lit *noact1 = arg_lit0("n", NULL, "take no action"); 124 | struct arg_lit *verbose1 = arg_lit0("v", "verbose", "verbose messages"); 125 | struct arg_lit *recurse1 = arg_lit0("R", NULL, "recurse through subdirectories"); 126 | struct arg_file *infiles1 = arg_filen(NULL, NULL, NULL, 1,argc+2, "input file(s)"); 127 | struct arg_file *outfile1 = arg_file0("o", NULL, "", "output file (default is \"-\")"); 128 | struct arg_end *end1 = arg_end(20); 129 | void* argtable1[] = {cmd1,noact1,verbose1,recurse1,infiles1,outfile1,end1}; 130 | int nerrors1; 131 | 132 | /* SYNTAX 2: remove [-nv] */ 133 | struct arg_rex *cmd2 = arg_rex1(NULL, NULL, "remove", NULL, REG_ICASE, NULL); 134 | struct arg_lit *noact2 = arg_lit0("n", NULL, NULL); 135 | struct arg_lit *verbose2 = arg_lit0("v", "verbose", NULL); 136 | struct arg_file *infiles2 = arg_file1(NULL, NULL, NULL, NULL); 137 | struct arg_end *end2 = arg_end(20); 138 | void* argtable2[] = {cmd2,noact2,verbose2,infiles2,end2}; 139 | int nerrors2; 140 | 141 | /* SYNTAX 3: search [-v] [-o ] [--help] [--version] */ 142 | struct arg_rex *cmd3 = arg_rex1(NULL, NULL, "search", NULL, REG_ICASE, NULL); 143 | struct arg_lit *verbose3 = arg_lit0("v", "verbose", NULL); 144 | struct arg_str *pattern3 = arg_str1(NULL, NULL, "", "search string"); 145 | struct arg_file *outfile3 = arg_file0("o", NULL, "", NULL); 146 | struct arg_end *end3 = arg_end(20); 147 | void* argtable3[] = {cmd3,verbose3,pattern3,outfile3,end3}; 148 | int nerrors3; 149 | 150 | /* SYNTAX 4: [-help] [-version] */ 151 | struct arg_lit *help4 = arg_lit0(NULL,"help", "print this help and exit"); 152 | struct arg_lit *version4 = arg_lit0(NULL,"version", "print version information and exit"); 153 | struct arg_end *end4 = arg_end(20); 154 | void* argtable4[] = {help4,version4,end4}; 155 | int nerrors4; 156 | 157 | const char* progname = "multisyntax"; 158 | int exitcode=0; 159 | 160 | /* verify all argtable[] entries were allocated sucessfully */ 161 | if (arg_nullcheck(argtable1)!=0 || 162 | arg_nullcheck(argtable2)!=0 || 163 | arg_nullcheck(argtable3)!=0 || 164 | arg_nullcheck(argtable4)!=0 ) 165 | { 166 | /* NULL entries were detected, some allocations must have failed */ 167 | printf("%s: insufficient memory\n",progname); 168 | exitcode=1; 169 | goto exit; 170 | } 171 | 172 | /* set any command line default values prior to parsing */ 173 | outfile1->filename[0]="-"; 174 | outfile3->filename[0]="-"; 175 | 176 | /* Above we defined a separate argtable for each possible command line syntax */ 177 | /* and here we parse each one in turn to see if any of them are successful */ 178 | nerrors1 = arg_parse(argc,argv,argtable1); 179 | nerrors2 = arg_parse(argc,argv,argtable2); 180 | nerrors3 = arg_parse(argc,argv,argtable3); 181 | nerrors4 = arg_parse(argc,argv,argtable4); 182 | 183 | /* Execute the appropriate main routine for the matching command line syntax */ 184 | /* In this example program our alternate command line syntaxes are mutually */ 185 | /* exclusive, so we know in advance that only one of them can be successful. */ 186 | if (nerrors1==0) 187 | exitcode = mymain1(noact1->count, verbose1->count, recurse1->count, 188 | outfile1->filename[0], infiles1->filename, infiles1->count); 189 | else if (nerrors2==0) 190 | exitcode = mymain2(noact2->count, verbose2->count, infiles2->filename[0]); 191 | else if (nerrors3==0) 192 | exitcode = mymain3(verbose3->count, pattern3->sval[0], outfile3->filename[0]); 193 | else if (nerrors4==0) 194 | exitcode = mymain4(help4->count, version4->count, progname, 195 | argtable1, argtable2, argtable3, argtable4); 196 | else 197 | { 198 | /* We get here if the command line matched none of the possible syntaxes */ 199 | if (cmd1->count > 0) 200 | { 201 | /* here the cmd1 argument was correct, so presume syntax 1 was intended target */ 202 | arg_print_errors(stdout,end1,progname); 203 | printf("usage: %s ", progname); 204 | arg_print_syntax(stdout,argtable1,"\n"); 205 | } 206 | else if (cmd2->count > 0) 207 | { 208 | /* here the cmd2 argument was correct, so presume syntax 2 was intended target */ 209 | arg_print_errors(stdout,end2,progname); 210 | printf("usage: %s ", progname); 211 | arg_print_syntax(stdout,argtable2,"\n"); 212 | } 213 | else if (cmd3->count > 0) 214 | { 215 | /* here the cmd3 argument was correct, so presume syntax 3 was intended target */ 216 | arg_print_errors(stdout,end3,progname); 217 | printf("usage: %s ", progname); 218 | arg_print_syntax(stdout,argtable3,"\n"); 219 | } 220 | else 221 | { 222 | /* no correct cmd literals were given, so we cant presume which syntax was intended */ 223 | printf("%s: missing command.\n",progname); 224 | printf("usage 1: %s ", progname); arg_print_syntax(stdout,argtable1,"\n"); 225 | printf("usage 2: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); 226 | printf("usage 3: %s ", progname); arg_print_syntax(stdout,argtable3,"\n"); 227 | printf("usage 4: %s", progname); arg_print_syntax(stdout,argtable4,"\n"); 228 | } 229 | } 230 | 231 | exit: 232 | /* deallocate each non-null entry in each argtable */ 233 | arg_freetable(argtable1,sizeof(argtable1)/sizeof(argtable1[0])); 234 | arg_freetable(argtable2,sizeof(argtable2)/sizeof(argtable2[0])); 235 | arg_freetable(argtable3,sizeof(argtable3)/sizeof(argtable3[0])); 236 | arg_freetable(argtable4,sizeof(argtable4)/sizeof(argtable4[0])); 237 | 238 | return exitcode; 239 | } 240 | -------------------------------------------------------------------------------- /argtable/examples/mv.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Example source code for using the argtable3 library to implement: 3 | * 4 | * mv [-bfiuv] [--backup=[CONTROL]] [--reply={yes,no,query}] 5 | * [--strip-trailing-slashes] [-S SUFFIX] [--target-directory=DIRECTORY] 6 | * [--help] [--version] SOURCE [SOURCE]... DEST|DIRECTORY 7 | * 8 | * This file is part of the argtable3 library. 9 | * 10 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 11 | * 12 | * All rights reserved. 13 | * 14 | * Redistribution and use in source and binary forms, with or without 15 | * modification, are permitted provided that the following conditions are met: 16 | * * Redistributions of source code must retain the above copyright 17 | * notice, this list of conditions and the following disclaimer. 18 | * * Redistributions in binary form must reproduce the above copyright 19 | * notice, this list of conditions and the following disclaimer in the 20 | * documentation and/or other materials provided with the distribution. 21 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 29 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 32 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | ******************************************************************************/ 36 | 37 | #include "argtable3.h" 38 | 39 | int mymain(const char *backup_control, 40 | int backup, 41 | int force, 42 | int interactive, 43 | const char *reply, 44 | int strip_trailing_slashes, 45 | const char *suffix, 46 | const char *targetdir, 47 | int update, 48 | int verbose, 49 | const char **files, 50 | int nfiles) 51 | { 52 | int j; 53 | 54 | /* if verbose option was given then display all option settings */ 55 | if (verbose) 56 | { 57 | printf("backup = %s\n", ((backup)?"YES":"NO")); 58 | printf("backup CONTROL = %s\n", backup_control); 59 | printf("force = %s\n", ((force)?"YES":"NO")); 60 | printf("interactive mode = %s\n", ((interactive)?"YES":"NO")); 61 | printf("reply = %s\n", reply); 62 | printf("strip-trailing-slashes = %s\n", ((strip_trailing_slashes)?"YES":"NO")); 63 | printf("suffix = %s\n", suffix); 64 | printf("target-directory = %s\n", targetdir); 65 | printf("update = %s\n", ((update)?"YES":"NO")); 66 | printf("verbose = %s\n", ((verbose)?"YES":"NO")); 67 | } 68 | 69 | /* print the source filenames */ 70 | for (j=0; jsval[0] = "existing"; /* --backup={none,off,numbered,t,existing,nil,simple,never} */ 115 | suffix->sval[0] = "~"; /* --suffix=~ */ 116 | reply->sval[0] = "query"; /* --reply={yes,no,query} */ 117 | targetd->sval[0] = NULL; 118 | 119 | /* Parse the command line as defined by argtable[] */ 120 | nerrors = arg_parse(argc,argv,argtable); 121 | 122 | /* special case: '--help' takes precedence over error reporting */ 123 | if (help->count > 0) 124 | { 125 | printf("Usage: %s", progname); 126 | arg_print_syntax(stdout,argtable,"\n"); 127 | printf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\n"); 128 | arg_print_glossary(stdout,argtable," %-30s %s\n"); 129 | printf("\nThe backup suffix is \"~\", unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n" 130 | "The version control method may be selected via the --backup option or through\n" 131 | "the VERSION_CONTROL environment variable. Here are the values:\n\n" 132 | " none, off never make backups (even if --backup is given)\n" 133 | " numbered, t make numbered backups\n" 134 | " existing, nil numbered if numbered backups exist, simple otherwise\n" 135 | " simple, never always make simple backups\n\n" 136 | "Report bugs to .\n"); 137 | exitcode=0; 138 | goto exit; 139 | } 140 | 141 | /* special case: '--version' takes precedence error reporting */ 142 | if (version->count > 0) 143 | { 144 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 145 | printf("September 2003, Stewart Heitmann\n"); 146 | exitcode=0; 147 | goto exit; 148 | } 149 | 150 | /* If the parser returned any errors then display them and exit */ 151 | if (nerrors > 0) 152 | { 153 | /* Display the error details contained in the arg_end struct.*/ 154 | arg_print_errors(stdout,end,progname); 155 | printf("Try '%s --help' for more information.\n",progname); 156 | exitcode=1; 157 | goto exit; 158 | } 159 | 160 | /* Command line parsing is complete, do the main processing */ 161 | exitcode = mymain(backupc->sval[0], 162 | backup->count, 163 | force->count, 164 | interact->count, 165 | reply->sval[0], 166 | strpslsh->count, 167 | suffix->sval[0], 168 | targetd->sval[0], 169 | update->count, 170 | verbose->count, 171 | files->filename, 172 | files->count); 173 | 174 | exit: 175 | /* deallocate each non-null entry in argtable[] */ 176 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 177 | 178 | return exitcode; 179 | } 180 | -------------------------------------------------------------------------------- /argtable/examples/myprog.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Example source code for using the argtable3 library to implement: 3 | * 4 | * myprog [-lRv] [-k ] [-D MACRO]... [-o ] [--help] 5 | * [--version] []... 6 | * 7 | * This file is part of the argtable3 library. 8 | * 9 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 10 | * 11 | * All rights reserved. 12 | * 13 | * Redistribution and use in source and binary forms, with or without 14 | * modification, are permitted provided that the following conditions are met: 15 | * * Redistributions of source code must retain the above copyright 16 | * notice, this list of conditions and the following disclaimer. 17 | * * Redistributions in binary form must reproduce the above copyright 18 | * notice, this list of conditions and the following disclaimer in the 19 | * documentation and/or other materials provided with the distribution. 20 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 28 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | ******************************************************************************/ 35 | 36 | #include 37 | 38 | int mymain(int l, int R, int k, 39 | const char **defines, int ndefines, 40 | const char *outfile, 41 | int v, 42 | const char **infiles, int ninfiles) 43 | { 44 | int i; 45 | 46 | if (l>0) printf("list files (-l)\n"); 47 | if (R>0) printf("recurse through directories (-R)\n"); 48 | if (v>0) printf("verbose is enabled (-v)\n"); 49 | printf("scalar k=%d\n",k); 50 | printf("output is \"%s\"\n", outfile); 51 | 52 | for (i=0; i", "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[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end}; 75 | const char* progname = "myprog"; 76 | int nerrors; 77 | int exitcode=0; 78 | 79 | /* verify the argtable[] entries were allocated sucessfully */ 80 | if (arg_nullcheck(argtable) != 0) 81 | { 82 | /* NULL entries were detected, some allocations must have failed */ 83 | printf("%s: insufficient memory\n",progname); 84 | exitcode=1; 85 | goto exit; 86 | } 87 | 88 | /* set any command line default values prior to parsing */ 89 | repeat->ival[0]=3; 90 | outfile->filename[0]="-"; 91 | 92 | /* Parse the command line as defined by argtable[] */ 93 | nerrors = arg_parse(argc,argv,argtable); 94 | 95 | /* special case: '--help' takes precedence over error reporting */ 96 | if (help->count > 0) 97 | { 98 | printf("Usage: %s", progname); 99 | arg_print_syntax(stdout,argtable,"\n"); 100 | printf("This program demonstrates the use of the argtable2 library\n"); 101 | printf("for parsing command line arguments. Argtable accepts integers\n"); 102 | printf("in decimal (123), hexadecimal (0xff), octal (0o123) and binary\n"); 103 | printf("(0b101101) formats. Suffixes KB, MB and GB are also accepted.\n"); 104 | arg_print_glossary(stdout,argtable," %-25s %s\n"); 105 | exitcode=0; 106 | goto exit; 107 | } 108 | 109 | /* special case: '--version' takes precedence error reporting */ 110 | if (version->count > 0) 111 | { 112 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 113 | printf("September 2003, Stewart Heitmann\n"); 114 | exitcode=0; 115 | goto exit; 116 | } 117 | 118 | /* If the parser returned any errors then display them and exit */ 119 | if (nerrors > 0) 120 | { 121 | /* Display the error details contained in the arg_end struct.*/ 122 | arg_print_errors(stdout,end,progname); 123 | printf("Try '%s --help' for more information.\n",progname); 124 | exitcode=1; 125 | goto exit; 126 | } 127 | 128 | /* special case: uname with no command line options induces brief help */ 129 | if (argc==1) 130 | { 131 | printf("Try '%s --help' for more information.\n",progname); 132 | exitcode=0; 133 | goto exit; 134 | } 135 | 136 | /* normal case: take the command line options at face value */ 137 | exitcode = mymain(list->count, recurse->count, repeat->ival[0], 138 | defines->sval, defines->count, 139 | outfile->filename[0], verbose->count, 140 | infiles->filename, infiles->count); 141 | 142 | exit: 143 | /* deallocate each non-null entry in argtable[] */ 144 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 145 | 146 | return exitcode; 147 | } 148 | -------------------------------------------------------------------------------- /argtable/examples/myprog_C89.c: -------------------------------------------------------------------------------- 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 | * 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 | 44 | #include "argtable3.h" 45 | 46 | int mymain(int l, int R, int k, 47 | const char **defines, int ndefines, 48 | const char *outfile, 49 | int v, 50 | const char **infiles, int ninfiles) 51 | { 52 | int i; 53 | 54 | if (l>0) printf("list files (-l)\n"); 55 | if (R>0) printf("recurse through directories (-R)\n"); 56 | if (v>0) printf("verbose is enabled (-v)\n"); 57 | printf("scalar k=%d\n",k); 58 | printf("output is \"%s\"\n", outfile); 59 | 60 | for (i=0; i", "output file (default is \"-\")"); 77 | struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages"); 78 | struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); 79 | struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit"); 80 | struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)"); 81 | struct arg_end *end = arg_end(20); 82 | void* argtable[10]; 83 | const char* progname = "myprog_C89"; 84 | int nerrors; 85 | int exitcode=0; 86 | 87 | /* initialize the argtable array with ptrs to the arg_xxx structures constructed above */ 88 | argtable[0] = list; 89 | argtable[1] = recurse; 90 | argtable[2] = repeat; 91 | argtable[3] = defines; 92 | argtable[4] = outfile; 93 | argtable[5] = verbose; 94 | argtable[6] = help; 95 | argtable[7] = version; 96 | argtable[8] = infiles; 97 | argtable[9] = end; 98 | 99 | /* verify the argtable[] entries were allocated sucessfully */ 100 | if (arg_nullcheck(argtable) != 0) 101 | { 102 | /* NULL entries were detected, some allocations must have failed */ 103 | printf("%s: insufficient memory\n",progname); 104 | exitcode=1; 105 | goto exit; 106 | } 107 | 108 | /* set any command line default values prior to parsing */ 109 | repeat->ival[0]=3; 110 | outfile->filename[0]="-"; 111 | 112 | /* Parse the command line as defined by argtable[] */ 113 | nerrors = arg_parse(argc,argv,argtable); 114 | 115 | /* special case: '--help' takes precedence over error reporting */ 116 | if (help->count > 0) 117 | { 118 | printf("Usage: %s", progname); 119 | arg_print_syntax(stdout,argtable,"\n"); 120 | printf("This program demonstrates the use of the argtable2 library\n"); 121 | printf("for parsing command line arguments.\n"); 122 | arg_print_glossary(stdout,argtable," %-25s %s\n"); 123 | exitcode=0; 124 | goto exit; 125 | } 126 | 127 | /* special case: '--version' takes precedence error reporting */ 128 | if (version->count > 0) 129 | { 130 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 131 | printf("September 2003, Stewart Heitmann\n"); 132 | exitcode=0; 133 | goto exit; 134 | } 135 | 136 | /* If the parser returned any errors then display them and exit */ 137 | if (nerrors > 0) 138 | { 139 | /* Display the error details contained in the arg_end struct.*/ 140 | arg_print_errors(stdout,end,progname); 141 | printf("Try '%s --help' for more information.\n",progname); 142 | exitcode=1; 143 | goto exit; 144 | } 145 | 146 | /* special case: uname with no command line options induces brief help */ 147 | if (argc==1) 148 | { 149 | printf("Try '%s --help' for more information.\n",progname); 150 | exitcode=0; 151 | goto exit; 152 | } 153 | 154 | /* normal case: take the command line options at face value */ 155 | exitcode = mymain(list->count, recurse->count, repeat->ival[0], 156 | defines->sval, defines->count, 157 | outfile->filename[0], verbose->count, 158 | infiles->filename, infiles->count); 159 | 160 | exit: 161 | /* deallocate each non-null entry in argtable[] */ 162 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 163 | 164 | return exitcode; 165 | } 166 | -------------------------------------------------------------------------------- /argtable/examples/testargtable3.c: -------------------------------------------------------------------------------- 1 | #include "argtable3.h" 2 | 3 | /* global arg_xxx structs */ 4 | struct arg_lit *a, *b, *c, *verb, *help, *version; 5 | struct arg_int *scal; 6 | struct arg_file *o, *file; 7 | struct arg_end *end; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | /* the global arg_xxx structs are initialised within the argtable */ 12 | void *argtable[] = { 13 | help = arg_lit0(NULL, "help", "display this help and exit"), 14 | version = arg_lit0(NULL, "version", "display version info and exit"), 15 | a = arg_lit0("a", NULL,"the -a option"), 16 | b = arg_lit0("b", NULL, "the -b option"), 17 | c = arg_lit0("c", NULL, "the -c option"), 18 | scal = arg_int0(NULL, "scalar", "", "foo value"), 19 | verb = arg_lit0("v", "verbose", "verbose output"), 20 | o = arg_file0("o", NULL, "myfile", "output file"), 21 | file = arg_filen(NULL, NULL, "", 1, 100, "input files"), 22 | end = arg_end(20), 23 | }; 24 | 25 | int exitcode = 0; 26 | char progname[] = "testargtable2.exe"; 27 | 28 | int nerrors; 29 | nerrors = arg_parse(argc,argv,argtable); 30 | 31 | /* special case: '--help' takes precedence over error reporting */ 32 | if (help->count > 0) 33 | { 34 | printf("Usage: %s", progname); 35 | arg_print_syntax(stdout, argtable, "\n"); 36 | printf("List information about the FILE(s) " 37 | "(the current directory by default).\n\n"); 38 | arg_print_glossary(stdout, argtable, " %-25s %s\n"); 39 | exitcode = 0; 40 | goto exit; 41 | } 42 | 43 | /* If the parser returned any errors then display them and exit */ 44 | if (nerrors > 0) 45 | { 46 | /* Display the error details contained in the arg_end struct.*/ 47 | arg_print_errors(stdout, end, progname); 48 | printf("Try '%s --help' for more information.\n", progname); 49 | exitcode = 1; 50 | goto exit; 51 | } 52 | 53 | exit: 54 | /* deallocate each non-null entry in argtable[] */ 55 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 56 | return exitcode; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /argtable/examples/uname.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Example source code for using the argtable3 library to implement: 3 | * 4 | * uname [-asnrvmpio] [--help] [--version] 5 | * 6 | * This file is part of the argtable3 library. 7 | * 8 | * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann 9 | * 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 | 35 | #include "argtable3.h" 36 | 37 | /* Here we simulate the uname functionality */ 38 | int mymain(int kname, int nname, int krel, int kver, int mach, int proc, int hard, int opsys) 39 | { 40 | if (kname) printf("Linux "); 41 | if (nname) printf("localhost.localdomain "); 42 | if (krel) printf("2.4.19-16 "); 43 | if (kver) printf("#1 Fri Sep 20 18:15:05 CEST 2002 "); 44 | if (mach) printf("i686 "); 45 | if (proc) printf("Intel "); 46 | if (hard) printf("unknown "); 47 | if (opsys) printf("GNU/Linux "); 48 | printf("\n"); 49 | return 0; 50 | } 51 | 52 | 53 | int main(int argc, char **argv) 54 | { 55 | const char* progname = "uname"; 56 | struct arg_lit *all = arg_lit0("a", "all", "print all information, in the following order:"); 57 | struct arg_lit *kname = arg_lit0("s", "kernel-name", "print the kernel name"); 58 | struct arg_lit *nname = arg_lit0("n", "nodename", "print the node name"); 59 | struct arg_lit *krel = arg_lit0("r", "kernel-release", "print the kernel release"); 60 | struct arg_lit *kver = arg_lit0("v", "kernel-version", "print the kernel version"); 61 | struct arg_lit *mach = arg_lit0("m", "machine", "print the machine hardware name"); 62 | struct arg_lit *proc = arg_lit0("p", "processor", "print the processor type"); 63 | struct arg_lit *hard = arg_lit0("i", "hardware-platform","print the hardware platform"); 64 | struct arg_lit *opsys = arg_lit0("o", "operating-system", "print the operating system"); 65 | struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); 66 | struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit"); 67 | struct arg_end *end = arg_end(20); 68 | void* argtable[] = {all,kname,nname,krel,kver,mach,proc,hard,opsys,help,vers,end}; 69 | int nerrors; 70 | int exitcode=0; 71 | 72 | /* verify the argtable[] entries were allocated sucessfully */ 73 | if (arg_nullcheck(argtable) != 0) 74 | { 75 | /* NULL entries were detected, some allocations must have failed */ 76 | printf("%s: insufficient memory\n",progname); 77 | exitcode=1; 78 | goto exit; 79 | } 80 | 81 | /* Parse the command line as defined by argtable[] */ 82 | nerrors = arg_parse(argc,argv,argtable); 83 | 84 | /* special case: '--help' takes precedence over error reporting */ 85 | if (help->count > 0) 86 | { 87 | printf("Usage: %s", progname); 88 | arg_print_syntax(stdout,argtable,"\n"); 89 | printf("Print certain system information. With no options, same as -s.\n\n"); 90 | arg_print_glossary(stdout,argtable," %-25s %s\n"); 91 | printf("\nReport bugs to .\n"); 92 | exitcode=0; 93 | goto exit; 94 | } 95 | 96 | /* special case: '--version' takes precedence error reporting */ 97 | if (vers->count > 0) 98 | { 99 | printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); 100 | printf("September 2003, Stewart Heitmann\n"); 101 | exitcode=0; 102 | goto exit; 103 | } 104 | 105 | /* If the parser returned any errors then display them and exit */ 106 | if (nerrors > 0) 107 | { 108 | /* Display the error details contained in the arg_end struct.*/ 109 | arg_print_errors(stdout,end,progname); 110 | printf("Try '%s --help' for more information.\n",progname); 111 | exitcode=1; 112 | goto exit; 113 | } 114 | 115 | /* special case: uname with no command line options is equivalent to "uname -s" */ 116 | if (argc==1) 117 | { 118 | exitcode = mymain(0,1,0,0,0,0,0,0); 119 | goto exit; 120 | } 121 | 122 | /* special case: "uname -a" is equivalent to "uname -snrvmpi" */ 123 | if (all->count>0) 124 | { 125 | exitcode = mymain(1,1,1,1,1,1,1,1); 126 | goto exit; 127 | } 128 | 129 | /* normal case: take the command line options at face value */ 130 | exitcode = mymain(kname->count, nname->count, krel->count, kver->count, mach->count, proc->count, hard->count, opsys->count); 131 | 132 | exit: 133 | /* deallocate each non-null entry in argtable[] */ 134 | arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); 135 | 136 | return exitcode; 137 | } 138 | -------------------------------------------------------------------------------- /argtable/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This file is part of the argtable3 library. 3 | # 4 | # Copyright (C) 2016-2019 Tom G. Huang 5 | # 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 are met: 10 | # * Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # * Redistributions in binary form must reproduce the above copyright 13 | # notice, this list of conditions and the following disclaimer in the 14 | # documentation and/or other materials provided with the distribution. 15 | # * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | # may be used to endorse or promote products derived from this software 17 | # without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | # ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ################################################################################ 30 | 31 | set(TEST_PUBLIC_SRC_FILES 32 | testall.c 33 | testarglit.c 34 | testargstr.c 35 | testargint.c 36 | testargdate.c 37 | testargdbl.c 38 | testargfile.c 39 | testargrex.c 40 | testargdstr.c 41 | testargcmd.c 42 | CuTest.c 43 | ) 44 | 45 | set(TEST_SRC_FILES 46 | ${TEST_PUBLIC_SRC_FILES} 47 | testarghashtable.c 48 | ) 49 | 50 | if(UNIX) 51 | set(ARGTABLE3_EXTRA_LIBS m) 52 | endif() 53 | 54 | add_executable(test_shared ${TEST_PUBLIC_SRC_FILES}) 55 | target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) 56 | target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src) 57 | target_link_libraries(test_shared argtable3 ${ARGTABLE3_EXTRA_LIBS}) 58 | add_custom_command(TARGET test_shared POST_BUILD 59 | COMMAND ${CMAKE_COMMAND} -E copy_if_different 60 | "$" 61 | "$" 62 | ) 63 | 64 | add_executable(test_static ${TEST_SRC_FILES}) 65 | target_compile_definitions(test_static PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) 66 | target_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src) 67 | target_link_libraries(test_static argtable3_static ${ARGTABLE3_EXTRA_LIBS}) 68 | 69 | add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES}) 70 | target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src) 71 | target_link_libraries(test_src ${ARGTABLE3_EXTRA_LIBS}) 72 | 73 | add_custom_command(OUTPUT ${ARGTABLE3_AMALGAMATION_SRC_FILE} 74 | COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist 75 | WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools" 76 | ) 77 | 78 | add_executable(test_amalgamation ${TEST_SRC_FILES} ${ARGTABLE3_AMALGAMATION_SRC_FILE}) 79 | target_include_directories(test_amalgamation PRIVATE ${PROJECT_SOURCE_DIR}/src) 80 | target_link_libraries(test_amalgamation ${ARGTABLE3_EXTRA_LIBS}) 81 | add_custom_command(TARGET test_amalgamation PRE_BUILD 82 | COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist 83 | WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools" 84 | ) 85 | 86 | add_test(NAME test_shared COMMAND "$") 87 | add_test(NAME test_static COMMAND "$") 88 | add_test(NAME test_src COMMAND "$") 89 | add_test(NAME test_amalgamation COMMAND "$") 90 | -------------------------------------------------------------------------------- /argtable/tests/CuTest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "CuTest.h" 9 | 10 | /*-------------------------------------------------------------------------* 11 | * CuStr 12 | *-------------------------------------------------------------------------*/ 13 | 14 | char* CuStrAlloc(size_t size) { 15 | char* newStr = (char*)malloc(sizeof(char) * (size)); 16 | return newStr; 17 | } 18 | 19 | char* CuStrCopy(const char* old) { 20 | size_t len = strlen(old); 21 | char* newStr = CuStrAlloc(len + 1); 22 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 23 | strcpy_s(newStr, len + 1, old); 24 | #else 25 | strcpy(newStr, old); 26 | #endif 27 | 28 | return newStr; 29 | } 30 | 31 | /*-------------------------------------------------------------------------* 32 | * CuString 33 | *-------------------------------------------------------------------------*/ 34 | 35 | void CuStringInit(CuString* str) { 36 | str->length = 0; 37 | str->size = STRING_MAX; 38 | str->buffer = (char*)malloc(sizeof(char) * str->size); 39 | str->buffer[0] = '\0'; 40 | } 41 | 42 | CuString* CuStringNew(void) { 43 | CuString* str = (CuString*)malloc(sizeof(CuString)); 44 | str->length = 0; 45 | str->size = STRING_MAX; 46 | str->buffer = (char*)malloc(sizeof(char) * str->size); 47 | str->buffer[0] = '\0'; 48 | return str; 49 | } 50 | 51 | void CuStringDelete(CuString* str) { 52 | if (!str) 53 | return; 54 | free(str->buffer); 55 | free(str); 56 | } 57 | 58 | void CuStringResize(CuString* str, size_t newSize) { 59 | str->buffer = (char*)realloc(str->buffer, sizeof(char) * newSize); 60 | str->size = newSize; 61 | } 62 | 63 | void CuStringAppend(CuString* str, const char* text) { 64 | size_t length; 65 | 66 | if (text == NULL) { 67 | text = "NULL"; 68 | } 69 | 70 | length = strlen(text); 71 | if (str->length + length + 1 >= str->size) 72 | CuStringResize(str, str->length + length + 1 + STRING_INC); 73 | str->length += length; 74 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 75 | strcat_s(str->buffer, str->size, text); 76 | #else 77 | strcat(str->buffer, text); 78 | #endif 79 | } 80 | 81 | void CuStringAppendChar(CuString* str, char ch) { 82 | char text[2]; 83 | text[0] = ch; 84 | text[1] = '\0'; 85 | CuStringAppend(str, text); 86 | } 87 | 88 | void CuStringAppendFormat(CuString* str, const char* format, ...) { 89 | va_list argp; 90 | char buf[HUGE_STRING_LEN]; 91 | va_start(argp, format); 92 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 93 | vsprintf_s(buf, sizeof(buf), format, argp); 94 | #else 95 | vsprintf(buf, format, argp); 96 | #endif 97 | 98 | va_end(argp); 99 | CuStringAppend(str, buf); 100 | } 101 | 102 | void CuStringInsert(CuString* str, const char* text, int pos) { 103 | size_t length = strlen(text); 104 | if ((size_t)pos > str->length) 105 | pos = (int)str->length; 106 | if (str->length + length + 1 >= str->size) 107 | CuStringResize(str, str->length + length + 1 + STRING_INC); 108 | memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1); 109 | str->length += length; 110 | memcpy(str->buffer + pos, text, length); 111 | } 112 | 113 | /*-------------------------------------------------------------------------* 114 | * CuTest 115 | *-------------------------------------------------------------------------*/ 116 | 117 | void CuTestInit(CuTest* t, const char* name, TestFunction function) { 118 | t->name = CuStrCopy(name); 119 | t->failed = 0; 120 | t->ran = 0; 121 | t->message = NULL; 122 | t->function = function; 123 | t->jumpBuf = NULL; 124 | } 125 | 126 | CuTest* CuTestNew(const char* name, TestFunction function) { 127 | CuTest* tc = CU_ALLOC(CuTest); 128 | CuTestInit(tc, name, function); 129 | return tc; 130 | } 131 | 132 | void CuTestDelete(CuTest* t) { 133 | if (!t) 134 | return; 135 | free(t->name); 136 | free(t); 137 | } 138 | 139 | void CuTestRun(CuTest* tc) { 140 | jmp_buf buf; 141 | tc->jumpBuf = &buf; 142 | if (setjmp(buf) == 0) { 143 | tc->ran = 1; 144 | (tc->function)(tc); 145 | } 146 | tc->jumpBuf = 0; 147 | } 148 | 149 | static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string) { 150 | char buf[HUGE_STRING_LEN]; 151 | 152 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 153 | sprintf_s(buf, sizeof(buf), "%s:%d: ", file, line); 154 | #else 155 | sprintf(buf, "%s:%d: ", file, line); 156 | #endif 157 | CuStringInsert(string, buf, 0); 158 | 159 | tc->failed = 1; 160 | tc->message = string->buffer; 161 | if (tc->jumpBuf != 0) 162 | longjmp(*(tc->jumpBuf), 0); 163 | } 164 | 165 | void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message) { 166 | CuString string; 167 | 168 | CuStringInit(&string); 169 | if (message2 != NULL) { 170 | CuStringAppend(&string, message2); 171 | CuStringAppend(&string, ": "); 172 | } 173 | CuStringAppend(&string, message); 174 | CuFailInternal(tc, file, line, &string); 175 | } 176 | 177 | void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition) { 178 | if (condition) 179 | return; 180 | CuFail_Line(tc, file, line, NULL, message); 181 | } 182 | 183 | void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, const char* expected, const char* actual) { 184 | CuString string; 185 | if ((expected == NULL && actual == NULL) || (expected != NULL && actual != NULL && strcmp(expected, actual) == 0)) { 186 | return; 187 | } 188 | 189 | CuStringInit(&string); 190 | if (message != NULL) { 191 | CuStringAppend(&string, message); 192 | CuStringAppend(&string, ": "); 193 | } 194 | CuStringAppend(&string, "expected <"); 195 | CuStringAppend(&string, expected); 196 | CuStringAppend(&string, "> but was <"); 197 | CuStringAppend(&string, actual); 198 | CuStringAppend(&string, ">"); 199 | CuFailInternal(tc, file, line, &string); 200 | } 201 | 202 | void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual) { 203 | char buf[STRING_MAX]; 204 | if (expected == actual) 205 | return; 206 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 207 | sprintf_s(buf, sizeof(buf), "expected <%d> but was <%d>", expected, actual); 208 | #else 209 | sprintf(buf, "expected <%d> but was <%d>", expected, actual); 210 | #endif 211 | CuFail_Line(tc, file, line, message, buf); 212 | } 213 | 214 | void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta) { 215 | char buf[STRING_MAX]; 216 | if (fabs(expected - actual) <= delta) 217 | return; 218 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 219 | sprintf_s(buf, sizeof(buf), "expected <%f> but was <%f>", expected, actual); 220 | #else 221 | sprintf(buf, "expected <%f> but was <%f>", expected, actual); 222 | #endif 223 | CuFail_Line(tc, file, line, message, buf); 224 | } 225 | 226 | void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, void* expected, void* actual) { 227 | char buf[STRING_MAX]; 228 | if (expected == actual) 229 | return; 230 | #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) 231 | sprintf_s(buf, sizeof(buf), "expected pointer <0x%p> but was <0x%p>", expected, actual); 232 | #else 233 | sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); 234 | #endif 235 | CuFail_Line(tc, file, line, message, buf); 236 | } 237 | 238 | /*-------------------------------------------------------------------------* 239 | * CuSuite 240 | *-------------------------------------------------------------------------*/ 241 | 242 | void CuSuiteInit(CuSuite* testSuite) { 243 | testSuite->count = 0; 244 | testSuite->failCount = 0; 245 | memset(testSuite->list, 0, sizeof(testSuite->list)); 246 | } 247 | 248 | CuSuite* CuSuiteNew(void) { 249 | CuSuite* testSuite = CU_ALLOC(CuSuite); 250 | CuSuiteInit(testSuite); 251 | return testSuite; 252 | } 253 | 254 | void CuSuiteDelete(CuSuite* testSuite) { 255 | unsigned int n; 256 | for (n = 0; n < MAX_TEST_CASES; n++) { 257 | if (testSuite->list[n]) { 258 | CuTestDelete(testSuite->list[n]); 259 | } 260 | } 261 | free(testSuite); 262 | } 263 | 264 | void CuSuiteAdd(CuSuite* testSuite, CuTest* testCase) { 265 | assert(testSuite->count < MAX_TEST_CASES); 266 | testSuite->list[testSuite->count] = testCase; 267 | testSuite->count++; 268 | } 269 | 270 | void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) { 271 | int i; 272 | for (i = 0; i < testSuite2->count; ++i) { 273 | CuTest* testCase = testSuite2->list[i]; 274 | CuSuiteAdd(testSuite, testCase); 275 | } 276 | } 277 | 278 | void CuSuiteRun(CuSuite* testSuite) { 279 | int i; 280 | for (i = 0; i < testSuite->count; ++i) { 281 | CuTest* testCase = testSuite->list[i]; 282 | CuTestRun(testCase); 283 | if (testCase->failed) { 284 | testSuite->failCount += 1; 285 | } 286 | } 287 | } 288 | 289 | void CuSuiteSummary(CuSuite* testSuite, CuString* summary) { 290 | int i; 291 | for (i = 0; i < testSuite->count; ++i) { 292 | CuTest* testCase = testSuite->list[i]; 293 | CuStringAppend(summary, testCase->failed ? "F" : "."); 294 | } 295 | CuStringAppend(summary, "\n\n"); 296 | } 297 | 298 | void CuSuiteDetails(CuSuite* testSuite, CuString* details) { 299 | int i; 300 | int failCount = 0; 301 | 302 | if (testSuite->failCount == 0) { 303 | int passCount = testSuite->count - testSuite->failCount; 304 | const char* testWord = passCount == 1 ? "test" : "tests"; 305 | CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord); 306 | } else { 307 | if (testSuite->failCount == 1) 308 | CuStringAppend(details, "There was 1 failure:\n"); 309 | else 310 | CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount); 311 | 312 | for (i = 0; i < testSuite->count; ++i) { 313 | CuTest* testCase = testSuite->list[i]; 314 | if (testCase->failed) { 315 | failCount++; 316 | CuStringAppendFormat(details, "%d) %s: %s\n", failCount, testCase->name, testCase->message); 317 | } 318 | } 319 | CuStringAppend(details, "\n!!!FAILURES!!!\n"); 320 | 321 | CuStringAppendFormat(details, "Runs: %d ", testSuite->count); 322 | CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); 323 | CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); 324 | } 325 | } 326 | -------------------------------------------------------------------------------- /argtable/tests/CuTest.h: -------------------------------------------------------------------------------- 1 | #ifndef CU_TEST_H 2 | #define CU_TEST_H 3 | 4 | #include 5 | #include 6 | 7 | #define CUTEST_VERSION "CuTest 1.5" 8 | 9 | /* CuString */ 10 | 11 | char* CuStrAlloc(size_t size); 12 | char* CuStrCopy(const char* old); 13 | 14 | #define CU_ALLOC(TYPE) ((TYPE*)malloc(sizeof(TYPE))) 15 | 16 | #define HUGE_STRING_LEN 8192 17 | #define STRING_MAX 256 18 | #define STRING_INC 256 19 | 20 | typedef struct { 21 | size_t length; 22 | size_t size; 23 | char* buffer; 24 | } CuString; 25 | 26 | void CuStringInit(CuString* str); 27 | CuString* CuStringNew(void); 28 | void CuStringRead(CuString* str, const char* path); 29 | void CuStringAppend(CuString* str, const char* text); 30 | void CuStringAppendChar(CuString* str, char ch); 31 | void CuStringAppendFormat(CuString* str, const char* format, ...); 32 | void CuStringInsert(CuString* str, const char* text, int pos); 33 | void CuStringResize(CuString* str, size_t newSize); 34 | void CuStringDelete(CuString* str); 35 | 36 | /* CuTest */ 37 | 38 | typedef struct CuTest CuTest; 39 | 40 | typedef void (*TestFunction)(CuTest*); 41 | 42 | struct CuTest { 43 | char* name; 44 | TestFunction function; 45 | int failed; 46 | int ran; 47 | const char* message; 48 | jmp_buf* jumpBuf; 49 | }; 50 | 51 | void CuTestInit(CuTest* t, const char* name, TestFunction function); 52 | CuTest* CuTestNew(const char* name, TestFunction function); 53 | void CuTestRun(CuTest* tc); 54 | void CuTestDelete(CuTest* t); 55 | 56 | /* Internal versions of assert functions -- use the public versions */ 57 | void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); 58 | void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); 59 | void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, const char* expected, const char* actual); 60 | void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual); 61 | void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta); 62 | void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, void* expected, void* actual); 63 | 64 | /* public assert functions */ 65 | 66 | #define CuFail(tc, ms) CuFail_Line((tc), __FILE__, __LINE__, NULL, (ms)) 67 | #define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) 68 | #define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) 69 | 70 | #define CuAssertStrEquals(tc, ex, ac) CuAssertStrEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) 71 | #define CuAssertStrEquals_Msg(tc, ms, ex, ac) CuAssertStrEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) 72 | #define CuAssertIntEquals(tc, ex, ac) CuAssertIntEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) 73 | #define CuAssertIntEquals_Msg(tc, ms, ex, ac) CuAssertIntEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) 74 | #define CuAssertDblEquals(tc, ex, ac, dl) CuAssertDblEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac), (dl)) 75 | #define CuAssertDblEquals_Msg(tc, ms, ex, ac, dl) CuAssertDblEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac), (dl)) 76 | #define CuAssertPtrEquals(tc, ex, ac) CuAssertPtrEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) 77 | #define CuAssertPtrEquals_Msg(tc, ms, ex, ac) CuAssertPtrEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) 78 | 79 | #define CuAssertPtrNotNull(tc, p) CuAssert_Line((tc), __FILE__, __LINE__, "null pointer unexpected", (p != NULL)) 80 | #define CuAssertPtrNotNullMsg(tc, msg, p) CuAssert_Line((tc), __FILE__, __LINE__, (msg), (p != NULL)) 81 | 82 | /* CuSuite */ 83 | 84 | #define MAX_TEST_CASES 1024 85 | 86 | #define SUITE_ADD_TEST(SUITE, TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) 87 | 88 | typedef struct { 89 | int count; 90 | CuTest* list[MAX_TEST_CASES]; 91 | int failCount; 92 | 93 | } CuSuite; 94 | 95 | void CuSuiteInit(CuSuite* testSuite); 96 | CuSuite* CuSuiteNew(void); 97 | void CuSuiteDelete(CuSuite* testSuite); 98 | void CuSuiteAdd(CuSuite* testSuite, CuTest* testCase); 99 | void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); 100 | void CuSuiteRun(CuSuite* testSuite); 101 | void CuSuiteSummary(CuSuite* testSuite, CuString* summary); 102 | void CuSuiteDetails(CuSuite* testSuite, CuString* details); 103 | 104 | #endif /* CU_TEST_H */ 105 | -------------------------------------------------------------------------------- /argtable/tests/argtable3_private.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * argtable3_private: Declares private types, constants, and interfaces 3 | * 4 | * This file is part of the argtable3 library. 5 | * 6 | * Copyright (C) 2013-2019 Tom G. Huang 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions are met: 12 | * * Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * * Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 18 | * may be used to endorse or promote products derived from this software 19 | * without specific prior written permission. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | ******************************************************************************/ 32 | 33 | #ifndef ARG_UTILS_H 34 | #define ARG_UTILS_H 35 | 36 | #include 37 | 38 | #define ARG_ENABLE_TRACE 0 39 | #define ARG_ENABLE_LOG 1 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | enum { ARG_ERR_MINCOUNT = 1, ARG_ERR_MAXCOUNT, ARG_ERR_BADINT, ARG_ERR_OVERFLOW, ARG_ERR_BADDOUBLE, ARG_ERR_BADDATE, ARG_ERR_REGNOMATCH }; 46 | 47 | typedef void(arg_panicfn)(const char* fmt, ...); 48 | 49 | #if defined(_MSC_VER) 50 | #define ARG_TRACE(x) \ 51 | __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ 52 | if (ARG_ENABLE_TRACE) \ 53 | dbg_printf x; \ 54 | } \ 55 | while (0) \ 56 | __pragma(warning(pop)) 57 | 58 | #define ARG_LOG(x) \ 59 | __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ 60 | if (ARG_ENABLE_LOG) \ 61 | dbg_printf x; \ 62 | } \ 63 | while (0) \ 64 | __pragma(warning(pop)) 65 | #else 66 | #define ARG_TRACE(x) \ 67 | do { \ 68 | if (ARG_ENABLE_TRACE) \ 69 | dbg_printf x; \ 70 | } while (0) 71 | 72 | #define ARG_LOG(x) \ 73 | do { \ 74 | if (ARG_ENABLE_LOG) \ 75 | dbg_printf x; \ 76 | } while (0) 77 | #endif 78 | 79 | extern void dbg_printf(const char* fmt, ...); 80 | extern void arg_set_panic(arg_panicfn* proc); 81 | extern void* xmalloc(size_t size); 82 | extern void* xcalloc(size_t count, size_t size); 83 | extern void* xrealloc(void* ptr, size_t size); 84 | extern void xfree(void* ptr); 85 | 86 | struct arg_hashtable_entry { 87 | void *k, *v; 88 | unsigned int h; 89 | struct arg_hashtable_entry* next; 90 | }; 91 | 92 | typedef struct arg_hashtable { 93 | unsigned int tablelength; 94 | struct arg_hashtable_entry** table; 95 | unsigned int entrycount; 96 | unsigned int loadlimit; 97 | unsigned int primeindex; 98 | unsigned int (*hashfn)(void* k); 99 | int (*eqfn)(void* k1, void* k2); 100 | } arg_hashtable_t; 101 | 102 | /** 103 | * @brief Create a hash table. 104 | * 105 | * @param minsize minimum initial size of hash table 106 | * @param hashfn function for hashing keys 107 | * @param eqfn function for determining key equality 108 | * @return newly created hash table or NULL on failure 109 | */ 110 | arg_hashtable_t* arg_hashtable_create(unsigned int minsize, unsigned int (*hashfn)(void*), int (*eqfn)(void*, void*)); 111 | 112 | /** 113 | * @brief This function will cause the table to expand if the insertion would take 114 | * the ratio of entries to table size over the maximum load factor. 115 | * 116 | * This function does not check for repeated insertions with a duplicate key. 117 | * The value returned when using a duplicate key is undefined -- when 118 | * the hash table changes size, the order of retrieval of duplicate key 119 | * entries is reversed. 120 | * If in doubt, remove before insert. 121 | * 122 | * @param h the hash table to insert into 123 | * @param k the key - hash table claims ownership and will free on removal 124 | * @param v the value - does not claim ownership 125 | * @return non-zero for successful insertion 126 | */ 127 | void arg_hashtable_insert(arg_hashtable_t* h, void* k, void* v); 128 | 129 | #define ARG_DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ 130 | int fnname(arg_hashtable_t* h, keytype* k, valuetype* v) { return arg_hashtable_insert(h, k, v); } 131 | 132 | /** 133 | * @brief Search the specified key in the hash table. 134 | * 135 | * @param h the hash table to search 136 | * @param k the key to search for - does not claim ownership 137 | * @return the value associated with the key, or NULL if none found 138 | */ 139 | void* arg_hashtable_search(arg_hashtable_t* h, void* k); 140 | 141 | #define ARG_DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ 142 | valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_search(h, k)); } 143 | 144 | /** 145 | * @brief Remove the specified key from the hash table. 146 | * 147 | * @param h the hash table to remove the item from 148 | * @param k the key to search for - does not claim ownership 149 | */ 150 | void arg_hashtable_remove(arg_hashtable_t* h, void* k); 151 | 152 | #define ARG_DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ 153 | valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_remove(h, k)); } 154 | 155 | /** 156 | * @brief Return the number of keys in the hash table. 157 | * 158 | * @param h the hash table 159 | * @return the number of items stored in the hash table 160 | */ 161 | unsigned int arg_hashtable_count(arg_hashtable_t* h); 162 | 163 | /** 164 | * @brief Change the value associated with the key. 165 | * 166 | * function to change the value associated with a key, where there already 167 | * exists a value bound to the key in the hash table. 168 | * Source due to Holger Schemel. 169 | * 170 | * @name hashtable_change 171 | * @param h the hash table 172 | * @param key 173 | * @param value 174 | */ 175 | int arg_hashtable_change(arg_hashtable_t* h, void* k, void* v); 176 | 177 | /** 178 | * @brief Free the hash table and the memory allocated for each key-value pair. 179 | * 180 | * @param h the hash table 181 | * @param free_values whether to call 'free' on the remaining values 182 | */ 183 | void arg_hashtable_destroy(arg_hashtable_t* h, int free_values); 184 | 185 | typedef struct arg_hashtable_itr { 186 | arg_hashtable_t* h; 187 | struct arg_hashtable_entry* e; 188 | struct arg_hashtable_entry* parent; 189 | unsigned int index; 190 | } arg_hashtable_itr_t; 191 | 192 | arg_hashtable_itr_t* arg_hashtable_itr_create(arg_hashtable_t* h); 193 | 194 | void arg_hashtable_itr_destroy(arg_hashtable_itr_t* itr); 195 | 196 | /** 197 | * @brief Return the value of the (key,value) pair at the current position. 198 | */ 199 | extern void* arg_hashtable_itr_key(arg_hashtable_itr_t* i); 200 | 201 | /** 202 | * @brief Return the value of the (key,value) pair at the current position. 203 | */ 204 | extern void* arg_hashtable_itr_value(arg_hashtable_itr_t* i); 205 | 206 | /** 207 | * @brief Advance the iterator to the next element. Returns zero if advanced to end of table. 208 | */ 209 | int arg_hashtable_itr_advance(arg_hashtable_itr_t* itr); 210 | 211 | /** 212 | * @brief Remove current element and advance the iterator to the next element. 213 | */ 214 | int arg_hashtable_itr_remove(arg_hashtable_itr_t* itr); 215 | 216 | /** 217 | * @brief Search and overwrite the supplied iterator, to point to the entry matching the supplied key. 218 | * 219 | * @return Zero if not found. 220 | */ 221 | int arg_hashtable_itr_search(arg_hashtable_itr_t* itr, arg_hashtable_t* h, void* k); 222 | 223 | #define ARG_DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \ 224 | int fnname(arg_hashtable_itr_t* i, arg_hashtable_t* h, keytype* k) { return (arg_hashtable_iterator_search(i, h, k)); } 225 | 226 | #ifdef __cplusplus 227 | } 228 | #endif 229 | 230 | #endif 231 | -------------------------------------------------------------------------------- /argtable/tests/testall.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | 33 | #include "CuTest.h" 34 | 35 | CuSuite* get_arglit_testsuite(); 36 | CuSuite* get_argstr_testsuite(); 37 | CuSuite* get_argint_testsuite(); 38 | CuSuite* get_argdate_testsuite(); 39 | CuSuite* get_argdbl_testsuite(); 40 | CuSuite* get_argfile_testsuite(); 41 | CuSuite* get_argrex_testsuite(); 42 | CuSuite* get_argdstr_testsuite(); 43 | CuSuite* get_argcmd_testsuite(); 44 | 45 | #ifndef ARGTABLE3_TEST_PUBLIC_ONLY 46 | CuSuite* get_arghashtable_testsuite(); 47 | #endif 48 | 49 | void RunAllTests(void) { 50 | CuString* output = CuStringNew(); 51 | CuSuite* suite = CuSuiteNew(); 52 | 53 | CuSuiteAddSuite(suite, get_arglit_testsuite()); 54 | CuSuiteAddSuite(suite, get_argstr_testsuite()); 55 | CuSuiteAddSuite(suite, get_argint_testsuite()); 56 | CuSuiteAddSuite(suite, get_argdate_testsuite()); 57 | CuSuiteAddSuite(suite, get_argdbl_testsuite()); 58 | CuSuiteAddSuite(suite, get_argfile_testsuite()); 59 | CuSuiteAddSuite(suite, get_argrex_testsuite()); 60 | CuSuiteAddSuite(suite, get_argdstr_testsuite()); 61 | CuSuiteAddSuite(suite, get_argcmd_testsuite()); 62 | #ifndef ARGTABLE3_TEST_PUBLIC_ONLY 63 | CuSuiteAddSuite(suite, get_arghashtable_testsuite()); 64 | #endif 65 | 66 | CuSuiteRun(suite); 67 | CuSuiteSummary(suite, output); 68 | CuSuiteDetails(suite, output); 69 | printf("%s\n", output->buffer); 70 | } 71 | 72 | int main(void) { 73 | RunAllTests(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /argtable/tests/testargcmd.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | #include "CuTest.h" 37 | #include "argtable3.h" 38 | 39 | #if defined(_MSC_VER) 40 | #pragma warning(push) 41 | #pragma warning(disable : 4204) 42 | #endif 43 | 44 | int cmd1_proc(int argc, char* argv[], arg_dstr_t res) { 45 | if (argc == 0) { 46 | arg_dstr_catf(res, "cmd1 fail"); 47 | return 1; 48 | } 49 | 50 | arg_dstr_catf(res, "%d %s", argc, argv[0]); 51 | return 0; 52 | } 53 | 54 | void test_argcmd_basic_001(CuTest* tc) { 55 | arg_cmd_init(); 56 | CuAssertIntEquals(tc, 0, arg_cmd_count()); 57 | 58 | arg_cmd_register("cmd1", cmd1_proc, "description of cmd1"); 59 | CuAssertIntEquals(tc, 1, arg_cmd_count()); 60 | 61 | char* argv[] = { 62 | "cmd1", 63 | "-o", 64 | "file1", 65 | }; 66 | int argc = 3; 67 | CuAssertTrue(tc, strcmp(argv[0], "cmd1") == 0); 68 | CuAssertTrue(tc, strcmp(argv[1], "-o") == 0); 69 | CuAssertTrue(tc, strcmp(argv[2], "file1") == 0); 70 | 71 | arg_dstr_t res = arg_dstr_create(); 72 | int err = arg_cmd_dispatch("cmd1", argc, argv, res); 73 | CuAssertIntEquals(tc, 0, err); 74 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "3 cmd1") == 0); 75 | 76 | arg_dstr_reset(res); 77 | err = arg_cmd_dispatch("cmd1", 0, NULL, res); 78 | CuAssertIntEquals(tc, 1, err); 79 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "cmd1 fail") == 0); 80 | 81 | arg_cmd_uninit(); 82 | } 83 | 84 | CuSuite* get_argcmd_testsuite() { 85 | CuSuite* suite = CuSuiteNew(); 86 | SUITE_ADD_TEST(suite, test_argcmd_basic_001); 87 | return suite; 88 | } 89 | 90 | #if defined(_MSC_VER) 91 | #pragma warning(pop) 92 | #endif 93 | -------------------------------------------------------------------------------- /argtable/tests/testargdate.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | #include 33 | 34 | #include "CuTest.h" 35 | #include "argtable3.h" 36 | 37 | /* 38 | printf("tm_sec = %d\n", c->tmval->tm_sec); 39 | printf("tm_min = %d\n", c->tmval->tm_min); 40 | printf("tm_hour = %d\n", c->tmval->tm_hour); 41 | printf("tm_mday = %d\n", c->tmval->tm_mday); 42 | printf("tm_mon = %d\n", c->tmval->tm_mon); 43 | printf("tm_year = %d\n", c->tmval->tm_year); 44 | printf("tm_wday = %d\n", c->tmval->tm_wday); 45 | printf("tm_yday = %d\n", c->tmval->tm_yday); 46 | printf("tm_isdst = %d\n", c->tmval->tm_isdst); 47 | 48 | */ 49 | 50 | #if defined(_MSC_VER) 51 | #pragma warning(push) 52 | #pragma warning(disable : 4204) 53 | #endif 54 | 55 | void test_argdate_basic_001(CuTest* tc) { 56 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 57 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 58 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 59 | struct arg_end* end = arg_end(20); 60 | void* argtable[] = {a, b, c, end}; 61 | int nerrors; 62 | 63 | char* argv[] = {"program", "23:59", "--date", "12/31/04", NULL}; 64 | int argc = sizeof(argv) / sizeof(char*) - 1; 65 | 66 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 67 | 68 | nerrors = arg_parse(argc, argv, argtable); 69 | 70 | CuAssertTrue(tc, nerrors == 0); 71 | CuAssertTrue(tc, a->count == 1); 72 | CuAssertIntEquals(tc, a->tmval->tm_sec, 0); 73 | CuAssertIntEquals(tc, a->tmval->tm_min, 59); 74 | CuAssertIntEquals(tc, a->tmval->tm_hour, 23); 75 | CuAssertIntEquals(tc, a->tmval->tm_mday, 0); 76 | CuAssertIntEquals(tc, a->tmval->tm_mon, 0); 77 | CuAssertIntEquals(tc, a->tmval->tm_year, 0); 78 | CuAssertIntEquals(tc, a->tmval->tm_wday, 0); 79 | CuAssertIntEquals(tc, a->tmval->tm_yday, 0); 80 | CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); 81 | CuAssertTrue(tc, b->count == 0); 82 | CuAssertTrue(tc, c->count == 1); 83 | CuAssertIntEquals(tc, c->tmval->tm_sec, 0); 84 | CuAssertIntEquals(tc, c->tmval->tm_min, 0); 85 | CuAssertIntEquals(tc, c->tmval->tm_hour, 0); 86 | CuAssertIntEquals(tc, c->tmval->tm_mday, 31); 87 | CuAssertIntEquals(tc, c->tmval->tm_mon, 11); 88 | CuAssertIntEquals(tc, c->tmval->tm_year, 104); 89 | CuAssertIntEquals(tc, c->tmval->tm_wday, 0); 90 | CuAssertIntEquals(tc, c->tmval->tm_yday, 0); 91 | CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); 92 | 93 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 94 | } 95 | 96 | void test_argdate_basic_002(CuTest* tc) { 97 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 98 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 99 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 100 | struct arg_end* end = arg_end(20); 101 | void* argtable[] = {a, b, c, end}; 102 | int nerrors; 103 | 104 | char* argv[] = {"program", "--date", "12/31/04", "20:15", NULL}; 105 | int argc = sizeof(argv) / sizeof(char*) - 1; 106 | 107 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 108 | 109 | nerrors = arg_parse(argc, argv, argtable); 110 | 111 | CuAssertTrue(tc, nerrors == 0); 112 | CuAssertTrue(tc, a->count == 1); 113 | CuAssertIntEquals(tc, a->tmval->tm_sec, 0); 114 | CuAssertIntEquals(tc, a->tmval->tm_min, 15); 115 | CuAssertIntEquals(tc, a->tmval->tm_hour, 20); 116 | CuAssertIntEquals(tc, a->tmval->tm_mday, 0); 117 | CuAssertIntEquals(tc, a->tmval->tm_mon, 0); 118 | CuAssertIntEquals(tc, a->tmval->tm_year, 0); 119 | CuAssertIntEquals(tc, a->tmval->tm_wday, 0); 120 | CuAssertIntEquals(tc, a->tmval->tm_yday, 0); 121 | CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); 122 | CuAssertTrue(tc, b->count == 0); 123 | CuAssertTrue(tc, c->count == 1); 124 | CuAssertIntEquals(tc, c->tmval->tm_sec, 0); 125 | CuAssertIntEquals(tc, c->tmval->tm_min, 0); 126 | CuAssertIntEquals(tc, c->tmval->tm_hour, 0); 127 | CuAssertIntEquals(tc, c->tmval->tm_mday, 31); 128 | CuAssertIntEquals(tc, c->tmval->tm_mon, 11); 129 | CuAssertIntEquals(tc, c->tmval->tm_year, 104); 130 | CuAssertIntEquals(tc, c->tmval->tm_wday, 0); 131 | CuAssertIntEquals(tc, c->tmval->tm_yday, 0); 132 | CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); 133 | 134 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 135 | } 136 | 137 | void test_argdate_basic_003(CuTest* tc) { 138 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 139 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 140 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 141 | struct arg_end* end = arg_end(20); 142 | void* argtable[] = {a, b, c, end}; 143 | int nerrors; 144 | 145 | char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "06/07/84", NULL}; 146 | int argc = sizeof(argv) / sizeof(char*) - 1; 147 | 148 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 149 | 150 | nerrors = arg_parse(argc, argv, argtable); 151 | 152 | CuAssertTrue(tc, nerrors == 0); 153 | CuAssertTrue(tc, a->count == 1); 154 | CuAssertIntEquals(tc, a->tmval->tm_sec, 0); 155 | CuAssertIntEquals(tc, a->tmval->tm_min, 15); 156 | CuAssertIntEquals(tc, a->tmval->tm_hour, 20); 157 | CuAssertIntEquals(tc, a->tmval->tm_mday, 0); 158 | CuAssertIntEquals(tc, a->tmval->tm_mon, 0); 159 | CuAssertIntEquals(tc, a->tmval->tm_year, 0); 160 | CuAssertIntEquals(tc, a->tmval->tm_wday, 0); 161 | CuAssertIntEquals(tc, a->tmval->tm_yday, 0); 162 | CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); 163 | CuAssertTrue(tc, b->count == 0); 164 | CuAssertTrue(tc, c->count == 2); 165 | CuAssertIntEquals(tc, c->tmval->tm_sec, 0); 166 | CuAssertIntEquals(tc, c->tmval->tm_min, 0); 167 | CuAssertIntEquals(tc, c->tmval->tm_hour, 0); 168 | CuAssertIntEquals(tc, c->tmval->tm_mday, 31); 169 | CuAssertIntEquals(tc, c->tmval->tm_mon, 11); 170 | CuAssertIntEquals(tc, c->tmval->tm_year, 104); 171 | CuAssertIntEquals(tc, c->tmval->tm_wday, 0); 172 | CuAssertIntEquals(tc, c->tmval->tm_yday, 0); 173 | CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); 174 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0); 175 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0); 176 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0); 177 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7); 178 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5); 179 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84); 180 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0); 181 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0); 182 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0); 183 | 184 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 185 | } 186 | 187 | void test_argdate_basic_004(CuTest* tc) { 188 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 189 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 190 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 191 | struct arg_end* end = arg_end(20); 192 | void* argtable[] = {a, b, c, end}; 193 | int nerrors; 194 | 195 | char* argv[] = {"program", "--date", "12/31/04", "20:15", "-b", "1982-11-28", "--date", "06/07/84", NULL}; 196 | int argc = sizeof(argv) / sizeof(char*) - 1; 197 | 198 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 199 | 200 | nerrors = arg_parse(argc, argv, argtable); 201 | 202 | CuAssertTrue(tc, nerrors == 0); 203 | CuAssertTrue(tc, a->count == 1); 204 | CuAssertIntEquals(tc, a->tmval->tm_sec, 0); 205 | CuAssertIntEquals(tc, a->tmval->tm_min, 15); 206 | CuAssertIntEquals(tc, a->tmval->tm_hour, 20); 207 | CuAssertIntEquals(tc, a->tmval->tm_mday, 0); 208 | CuAssertIntEquals(tc, a->tmval->tm_mon, 0); 209 | CuAssertIntEquals(tc, a->tmval->tm_year, 0); 210 | CuAssertIntEquals(tc, a->tmval->tm_wday, 0); 211 | CuAssertIntEquals(tc, a->tmval->tm_yday, 0); 212 | CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); 213 | CuAssertTrue(tc, b->count == 1); 214 | CuAssertIntEquals(tc, b->tmval->tm_sec, 0); 215 | CuAssertIntEquals(tc, b->tmval->tm_min, 0); 216 | CuAssertIntEquals(tc, b->tmval->tm_hour, 0); 217 | CuAssertIntEquals(tc, b->tmval->tm_mday, 28); 218 | CuAssertIntEquals(tc, b->tmval->tm_mon, 10); 219 | CuAssertIntEquals(tc, b->tmval->tm_year, 82); 220 | CuAssertIntEquals(tc, b->tmval->tm_wday, 0); 221 | CuAssertIntEquals(tc, b->tmval->tm_yday, 0); 222 | CuAssertIntEquals(tc, b->tmval->tm_isdst, 0); 223 | CuAssertTrue(tc, c->count == 2); 224 | CuAssertIntEquals(tc, c->tmval->tm_sec, 0); 225 | CuAssertIntEquals(tc, c->tmval->tm_min, 0); 226 | CuAssertIntEquals(tc, c->tmval->tm_hour, 0); 227 | CuAssertIntEquals(tc, c->tmval->tm_mday, 31); 228 | CuAssertIntEquals(tc, c->tmval->tm_mon, 11); 229 | CuAssertIntEquals(tc, c->tmval->tm_year, 104); 230 | CuAssertIntEquals(tc, c->tmval->tm_wday, 0); 231 | CuAssertIntEquals(tc, c->tmval->tm_yday, 0); 232 | CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); 233 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0); 234 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0); 235 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0); 236 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7); 237 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5); 238 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84); 239 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0); 240 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0); 241 | CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0); 242 | 243 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 244 | } 245 | 246 | void test_argdate_basic_005(CuTest* tc) { 247 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 248 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 249 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 250 | struct arg_end* end = arg_end(20); 251 | void* argtable[] = {a, b, c, end}; 252 | int nerrors; 253 | 254 | char* argv[] = {"program", NULL}; 255 | int argc = sizeof(argv) / sizeof(char*) - 1; 256 | 257 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 258 | 259 | nerrors = arg_parse(argc, argv, argtable); 260 | CuAssertTrue(tc, nerrors == 2); 261 | 262 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 263 | } 264 | 265 | void test_argdate_basic_006(CuTest* tc) { 266 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 267 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 268 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 269 | struct arg_end* end = arg_end(20); 270 | void* argtable[] = {a, b, c, end}; 271 | int nerrors; 272 | 273 | char* argv[] = {"program", "25:59", "--date", "12/31/04", NULL}; 274 | int argc = sizeof(argv) / sizeof(char*) - 1; 275 | 276 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 277 | 278 | nerrors = arg_parse(argc, argv, argtable); 279 | CuAssertTrue(tc, nerrors == 1); 280 | 281 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 282 | } 283 | 284 | void test_argdate_basic_007(CuTest* tc) { 285 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 286 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 287 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 288 | struct arg_end* end = arg_end(20); 289 | void* argtable[] = {a, b, c, end}; 290 | int nerrors; 291 | 292 | char* argv[] = {"program", "23:59", "--date", "12/32/04", NULL}; 293 | int argc = sizeof(argv) / sizeof(char*) - 1; 294 | 295 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 296 | 297 | nerrors = arg_parse(argc, argv, argtable); 298 | CuAssertTrue(tc, nerrors == 1); 299 | 300 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 301 | } 302 | 303 | void test_argdate_basic_008(CuTest* tc) { 304 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 305 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 306 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 307 | struct arg_end* end = arg_end(20); 308 | void* argtable[] = {a, b, c, end}; 309 | int nerrors; 310 | 311 | char* argv[] = {"program", "23:59", "--date", "12/31/04", "22:58", NULL}; 312 | int argc = sizeof(argv) / sizeof(char*) - 1; 313 | 314 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 315 | 316 | nerrors = arg_parse(argc, argv, argtable); 317 | CuAssertTrue(tc, nerrors == 1); 318 | 319 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 320 | } 321 | 322 | void test_argdate_basic_009(CuTest* tc) { 323 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 324 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 325 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 326 | struct arg_end* end = arg_end(20); 327 | void* argtable[] = {a, b, c, end}; 328 | int nerrors; 329 | 330 | char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "26/07/84", NULL}; 331 | int argc = sizeof(argv) / sizeof(char*) - 1; 332 | 333 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 334 | 335 | nerrors = arg_parse(argc, argv, argtable); 336 | CuAssertTrue(tc, nerrors == 1); 337 | 338 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 339 | } 340 | 341 | void test_argdate_basic_010(CuTest* tc) { 342 | struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); 343 | struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); 344 | struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); 345 | struct arg_end* end = arg_end(20); 346 | void* argtable[] = {a, b, c, end}; 347 | int nerrors; 348 | 349 | char* argv[] = {"program", "-b", "1982-11-28", "-b", "1976-11-11", "--date", "12/07/84", NULL}; 350 | int argc = sizeof(argv) / sizeof(char*) - 1; 351 | 352 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 353 | 354 | nerrors = arg_parse(argc, argv, argtable); 355 | CuAssertTrue(tc, nerrors == 1); 356 | 357 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 358 | } 359 | 360 | CuSuite* get_argdate_testsuite() { 361 | CuSuite* suite = CuSuiteNew(); 362 | SUITE_ADD_TEST(suite, test_argdate_basic_001); 363 | SUITE_ADD_TEST(suite, test_argdate_basic_002); 364 | SUITE_ADD_TEST(suite, test_argdate_basic_003); 365 | SUITE_ADD_TEST(suite, test_argdate_basic_004); 366 | SUITE_ADD_TEST(suite, test_argdate_basic_005); 367 | SUITE_ADD_TEST(suite, test_argdate_basic_006); 368 | SUITE_ADD_TEST(suite, test_argdate_basic_007); 369 | SUITE_ADD_TEST(suite, test_argdate_basic_008); 370 | SUITE_ADD_TEST(suite, test_argdate_basic_009); 371 | SUITE_ADD_TEST(suite, test_argdate_basic_010); 372 | return suite; 373 | } 374 | 375 | #if defined(_MSC_VER) 376 | #pragma warning(pop) 377 | #endif 378 | -------------------------------------------------------------------------------- /argtable/tests/testargdstr.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | #include "CuTest.h" 37 | #include "argtable3.h" 38 | 39 | #if defined(_MSC_VER) 40 | #pragma warning(push) 41 | #pragma warning(disable : 4204) 42 | #endif 43 | 44 | void test_argdstr_basic_001(CuTest* tc) { 45 | arg_dstr_t ds = arg_dstr_create(); 46 | 47 | arg_dstr_set(ds, "hello ", ARG_DSTR_VOLATILE); 48 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello ") == 0); 49 | 50 | arg_dstr_cat(ds, "world"); 51 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); 52 | 53 | arg_dstr_destroy(ds); 54 | } 55 | 56 | void test_argdstr_basic_002(CuTest* tc) { 57 | arg_dstr_t ds = arg_dstr_create(); 58 | 59 | arg_dstr_set(ds, "hello world", ARG_DSTR_VOLATILE); 60 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); 61 | 62 | arg_dstr_reset(ds); 63 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "") == 0); 64 | 65 | arg_dstr_set(ds, "good", ARG_DSTR_VOLATILE); 66 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "good") == 0); 67 | 68 | arg_dstr_destroy(ds); 69 | } 70 | 71 | void test_argdstr_basic_003(CuTest* tc) { 72 | arg_dstr_t ds = arg_dstr_create(); 73 | arg_dstr_cat(ds, "hello world"); 74 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); 75 | 76 | arg_dstr_destroy(ds); 77 | } 78 | 79 | void test_argdstr_basic_004(CuTest* tc) { 80 | arg_dstr_t ds = arg_dstr_create(); 81 | arg_dstr_catf(ds, "%s %d", "hello world", 1); 82 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world 1") == 0); 83 | 84 | arg_dstr_destroy(ds); 85 | } 86 | 87 | void test_argdstr_basic_005(CuTest* tc) { 88 | arg_dstr_t ds = arg_dstr_create(); 89 | arg_dstr_catf(ds, "%d.", 1); 90 | arg_dstr_catf(ds, "%d.", 2); 91 | arg_dstr_catf(ds, "%d.", 3); 92 | arg_dstr_cat(ds, "456"); 93 | CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "1.2.3.456") == 0); 94 | 95 | arg_dstr_destroy(ds); 96 | } 97 | 98 | void test_argdstr_basic_006(CuTest* tc) { 99 | int i; 100 | 101 | arg_dstr_t ds = arg_dstr_create(); 102 | for (i = 0; i < 100000; i++) { 103 | arg_dstr_catf(ds, "%s", "1234567890"); 104 | } 105 | CuAssertTrue(tc, strlen(arg_dstr_cstr(ds)) == 1000000); 106 | 107 | arg_dstr_destroy(ds); 108 | } 109 | 110 | CuSuite* get_argdstr_testsuite() { 111 | CuSuite* suite = CuSuiteNew(); 112 | SUITE_ADD_TEST(suite, test_argdstr_basic_001); 113 | SUITE_ADD_TEST(suite, test_argdstr_basic_002); 114 | SUITE_ADD_TEST(suite, test_argdstr_basic_003); 115 | SUITE_ADD_TEST(suite, test_argdstr_basic_004); 116 | SUITE_ADD_TEST(suite, test_argdstr_basic_005); 117 | SUITE_ADD_TEST(suite, test_argdstr_basic_006); 118 | return suite; 119 | } 120 | 121 | #if defined(_MSC_VER) 122 | #pragma warning(pop) 123 | #endif 124 | -------------------------------------------------------------------------------- /argtable/tests/testarghashtable.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | #include "CuTest.h" 37 | #include "argtable3_private.h" 38 | 39 | #if defined(_MSC_VER) 40 | #pragma warning(push) 41 | #pragma warning(disable : 4204) 42 | #pragma warning(disable : 4996) 43 | #endif 44 | 45 | static unsigned int hash_key(void* key) { 46 | char* str = (char*)key; 47 | int c; 48 | unsigned int hash = 5381; 49 | 50 | while ((c = *str++) != 0) 51 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 52 | 53 | return hash; 54 | } 55 | 56 | static int equal_keys(void* key1, void* key2) { 57 | char* k1 = (char*)key1; 58 | char* k2 = (char*)key2; 59 | return (0 == strcmp(k1, k2)); 60 | } 61 | 62 | void test_arghashtable_basic_001(CuTest* tc) { 63 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 64 | CuAssertTrue(tc, h != 0); 65 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 66 | 67 | arg_hashtable_destroy(h, 1); 68 | } 69 | 70 | void test_arghashtable_basic_002(CuTest* tc) { 71 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 72 | CuAssertTrue(tc, h != 0); 73 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 74 | 75 | char* key_1 = "k1"; 76 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 77 | memset(k_1, 0, strlen(key_1) + 1); 78 | strncpy(k_1, key_1, strlen(key_1)); 79 | 80 | char* value_1 = "v1"; 81 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 82 | memset(v_1, 0, strlen(value_1) + 1); 83 | strncpy(v_1, value_1, strlen(value_1)); 84 | 85 | arg_hashtable_insert(h, k_1, v_1); 86 | CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); 87 | 88 | arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); 89 | CuAssertTrue(tc, itr != 0); 90 | CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(itr)); 91 | CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(itr), key_1) == 0); 92 | CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(itr)); 93 | CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(itr), value_1) == 0); 94 | 95 | arg_hashtable_itr_destroy(itr); 96 | arg_hashtable_destroy(h, 1); 97 | } 98 | 99 | void test_arghashtable_basic_003(CuTest* tc) { 100 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 101 | CuAssertTrue(tc, h != 0); 102 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 103 | 104 | char* key_1 = "k1"; 105 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 106 | memset(k_1, 0, strlen(key_1) + 1); 107 | strncpy(k_1, key_1, strlen(key_1)); 108 | 109 | char* value_1 = "v1"; 110 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 111 | memset(v_1, 0, strlen(value_1) + 1); 112 | strncpy(v_1, value_1, strlen(value_1)); 113 | 114 | arg_hashtable_insert(h, k_1, v_1); 115 | CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); 116 | 117 | char* key_2 = "k2"; 118 | char* k_2 = (char*)malloc(strlen(key_2) + 1); 119 | memset(k_2, 0, strlen(key_2) + 1); 120 | strncpy(k_2, key_2, strlen(key_2)); 121 | 122 | char* value_2 = "v2"; 123 | char* v_2 = (char*)malloc(strlen(value_2) + 1); 124 | memset(v_2, 0, strlen(value_2) + 1); 125 | strncpy(v_2, value_2, strlen(value_2)); 126 | 127 | arg_hashtable_insert(h, k_2, v_2); 128 | CuAssertIntEquals(tc, 2, arg_hashtable_count(h)); 129 | 130 | arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); 131 | CuAssertTrue(tc, itr != 0); 132 | 133 | int ret = arg_hashtable_itr_advance(itr); 134 | CuAssertTrue(tc, ret != 0); 135 | 136 | ret = arg_hashtable_itr_advance(itr); 137 | CuAssertTrue(tc, ret == 0); 138 | 139 | arg_hashtable_itr_destroy(itr); 140 | arg_hashtable_destroy(h, 1); 141 | } 142 | 143 | void test_arghashtable_basic_004(CuTest* tc) { 144 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 145 | CuAssertTrue(tc, h != 0); 146 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 147 | 148 | char* key_1 = "k1"; 149 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 150 | memset(k_1, 0, strlen(key_1) + 1); 151 | strncpy(k_1, key_1, strlen(key_1)); 152 | 153 | char* value_1 = "v1"; 154 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 155 | memset(v_1, 0, strlen(value_1) + 1); 156 | strncpy(v_1, value_1, strlen(value_1)); 157 | 158 | arg_hashtable_insert(h, k_1, v_1); 159 | CuAssertTrue(tc, h != 0); 160 | CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); 161 | 162 | arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); 163 | int ret = arg_hashtable_itr_remove(itr); 164 | CuAssertTrue(tc, ret == 0); 165 | CuAssertIntEquals(tc, 0, arg_hashtable_count(h)); 166 | 167 | arg_hashtable_itr_destroy(itr); 168 | arg_hashtable_destroy(h, 1); 169 | } 170 | 171 | void test_arghashtable_basic_005(CuTest* tc) { 172 | arg_hashtable_t* h = arg_hashtable_create(3, hash_key, equal_keys); 173 | CuAssertTrue(tc, h != 0); 174 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 175 | 176 | char* key_1 = "k1"; 177 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 178 | memset(k_1, 0, strlen(key_1) + 1); 179 | strncpy(k_1, key_1, strlen(key_1)); 180 | 181 | char* value_1 = "v1"; 182 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 183 | memset(v_1, 0, strlen(value_1) + 1); 184 | strncpy(v_1, value_1, strlen(value_1)); 185 | 186 | arg_hashtable_insert(h, k_1, v_1); 187 | CuAssertTrue(tc, h != 0); 188 | CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); 189 | 190 | arg_hashtable_remove(h, k_1); 191 | CuAssertIntEquals(tc, 0, arg_hashtable_count(h)); 192 | 193 | arg_hashtable_destroy(h, 1); 194 | } 195 | 196 | void test_arghashtable_basic_006(CuTest* tc) { 197 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 198 | CuAssertTrue(tc, h != 0); 199 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 200 | 201 | char* key_1 = "k1"; 202 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 203 | memset(k_1, 0, strlen(key_1) + 1); 204 | strncpy(k_1, key_1, strlen(key_1)); 205 | 206 | char* value_1 = "v1"; 207 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 208 | memset(v_1, 0, strlen(value_1) + 1); 209 | strncpy(v_1, value_1, strlen(value_1)); 210 | 211 | arg_hashtable_insert(h, k_1, v_1); 212 | CuAssertTrue(tc, arg_hashtable_count(h) == 1); 213 | 214 | char* vv = (char*)arg_hashtable_search(h, k_1); 215 | CuAssertTrue(tc, strcmp(vv, v_1) == 0); 216 | 217 | arg_hashtable_destroy(h, 1); 218 | } 219 | 220 | void test_arghashtable_basic_007(CuTest* tc) { 221 | arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); 222 | CuAssertTrue(tc, h != 0); 223 | CuAssertIntEquals(tc, arg_hashtable_count(h), 0); 224 | 225 | char* key_1 = "k1"; 226 | char* k_1 = (char*)malloc(strlen(key_1) + 1); 227 | memset(k_1, 0, strlen(key_1) + 1); 228 | strncpy(k_1, key_1, strlen(key_1)); 229 | 230 | char* value_1 = "v1"; 231 | char* v_1 = (char*)malloc(strlen(value_1) + 1); 232 | memset(v_1, 0, strlen(value_1) + 1); 233 | strncpy(v_1, value_1, strlen(value_1)); 234 | 235 | arg_hashtable_insert(h, k_1, v_1); 236 | CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); 237 | 238 | char* key_2 = "k2"; 239 | char* k_2 = (char*)malloc(strlen(key_2) + 1); 240 | memset(k_2, 0, strlen(key_2) + 1); 241 | strncpy(k_2, key_2, strlen(key_2)); 242 | 243 | char* value_2 = "v2"; 244 | char* v_2 = (char*)malloc(strlen(value_2) + 1); 245 | memset(v_2, 0, strlen(value_2) + 1); 246 | strncpy(v_2, value_2, strlen(value_2)); 247 | 248 | arg_hashtable_insert(h, k_2, v_2); 249 | CuAssertIntEquals(tc, 2, arg_hashtable_count(h)); 250 | 251 | arg_hashtable_itr_t itr; 252 | int ret = arg_hashtable_itr_search(&itr, h, k_1); 253 | CuAssertTrue(tc, ret != 0); 254 | CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(&itr)); 255 | CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(&itr)); 256 | CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(&itr), k_1) == 0); 257 | CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(&itr), v_1) == 0); 258 | 259 | arg_hashtable_destroy(h, 1); 260 | } 261 | 262 | CuSuite* get_arghashtable_testsuite() { 263 | CuSuite* suite = CuSuiteNew(); 264 | SUITE_ADD_TEST(suite, test_arghashtable_basic_001); 265 | SUITE_ADD_TEST(suite, test_arghashtable_basic_002); 266 | SUITE_ADD_TEST(suite, test_arghashtable_basic_003); 267 | SUITE_ADD_TEST(suite, test_arghashtable_basic_004); 268 | SUITE_ADD_TEST(suite, test_arghashtable_basic_005); 269 | SUITE_ADD_TEST(suite, test_arghashtable_basic_006); 270 | SUITE_ADD_TEST(suite, test_arghashtable_basic_007); 271 | return suite; 272 | } 273 | 274 | #if defined(_MSC_VER) 275 | #pragma warning(pop) 276 | #endif 277 | -------------------------------------------------------------------------------- /argtable/tests/testargrex.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * This file is part of the argtable3 library. 3 | * 4 | * Copyright (C) 2013-2019 Tom G. Huang 5 | * 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 are met: 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of STEWART HEITMANN nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | ******************************************************************************/ 30 | 31 | #include 32 | 33 | #include "CuTest.h" 34 | #include "argtable3.h" 35 | 36 | #if defined(_MSC_VER) 37 | #pragma warning(push) 38 | #pragma warning(disable : 4204) 39 | #endif 40 | 41 | void test_argrex_basic_001(CuTest* tc) { 42 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 43 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 44 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 45 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 46 | struct arg_end* end = arg_end(20); 47 | void* argtable[] = {a, b, c, d, end}; 48 | int nerrors; 49 | 50 | char* argv[] = {"program", "--beta", "world", "goodbye", NULL}; 51 | int argc = sizeof(argv) / sizeof(char*) - 1; 52 | 53 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 54 | 55 | nerrors = arg_parse(argc, argv, argtable); 56 | CuAssertTrue(tc, nerrors == 0); 57 | 58 | CuAssertTrue(tc, a->count == 0); 59 | CuAssertTrue(tc, b->count == 1); 60 | CuAssertStrEquals(tc, b->sval[0], "world"); 61 | CuAssertTrue(tc, c->count == 1); 62 | CuAssertStrEquals(tc, c->sval[0], "goodbye"); 63 | CuAssertTrue(tc, d->count == 0); 64 | 65 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 66 | } 67 | 68 | void test_argrex_basic_002(CuTest* tc) { 69 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 70 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 71 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 72 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 73 | struct arg_end* end = arg_end(20); 74 | void* argtable[] = {a, b, c, d, end}; 75 | int nerrors; 76 | 77 | char* argv[] = {"program", "GoodBye", "--beta", "World", NULL}; 78 | int argc = sizeof(argv) / sizeof(char*) - 1; 79 | 80 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 81 | 82 | nerrors = arg_parse(argc, argv, argtable); 83 | CuAssertTrue(tc, nerrors == 0); 84 | 85 | CuAssertTrue(tc, a->count == 0); 86 | CuAssertTrue(tc, b->count == 1); 87 | CuAssertStrEquals(tc, b->sval[0], "World"); 88 | CuAssertTrue(tc, c->count == 1); 89 | CuAssertStrEquals(tc, c->sval[0], "GoodBye"); 90 | CuAssertTrue(tc, d->count == 0); 91 | 92 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 93 | } 94 | 95 | void test_argrex_basic_003(CuTest* tc) { 96 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 97 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 98 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 99 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 100 | struct arg_end* end = arg_end(20); 101 | void* argtable[] = {a, b, c, d, end}; 102 | int nerrors; 103 | 104 | char* argv[] = {"program", "--beta", "world", "GOODBYE", "GoodBye", "gOoDbyE", "Anything", NULL}; 105 | int argc = sizeof(argv) / sizeof(char*) - 1; 106 | 107 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 108 | 109 | nerrors = arg_parse(argc, argv, argtable); 110 | CuAssertTrue(tc, nerrors == 0); 111 | 112 | CuAssertTrue(tc, a->count == 0); 113 | CuAssertTrue(tc, b->count == 1); 114 | CuAssertStrEquals(tc, b->sval[0], "world"); 115 | CuAssertTrue(tc, c->count == 3); 116 | CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); 117 | CuAssertStrEquals(tc, c->sval[1], "GoodBye"); 118 | CuAssertStrEquals(tc, c->sval[2], "gOoDbyE"); 119 | CuAssertTrue(tc, d->count == 1); 120 | CuAssertStrEquals(tc, d->sval[0], "Anything"); 121 | 122 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 123 | } 124 | 125 | void test_argrex_basic_004(CuTest* tc) { 126 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 127 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 128 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 129 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 130 | struct arg_end* end = arg_end(20); 131 | void* argtable[] = {a, b, c, d, end}; 132 | int nerrors; 133 | 134 | char* argv[] = {"program", "--beta", "world", "GOODBYE", "AnyHow", NULL}; 135 | int argc = sizeof(argv) / sizeof(char*) - 1; 136 | 137 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 138 | 139 | nerrors = arg_parse(argc, argv, argtable); 140 | CuAssertTrue(tc, nerrors == 0); 141 | 142 | CuAssertTrue(tc, a->count == 0); 143 | CuAssertTrue(tc, b->count == 1); 144 | CuAssertStrEquals(tc, b->sval[0], "world"); 145 | CuAssertTrue(tc, c->count == 1); 146 | CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); 147 | CuAssertTrue(tc, d->count == 1); 148 | CuAssertStrEquals(tc, d->sval[0], "AnyHow"); 149 | 150 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 151 | } 152 | 153 | void test_argrex_basic_005(CuTest* tc) { 154 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 155 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 156 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 157 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 158 | struct arg_end* end = arg_end(20); 159 | void* argtable[] = {a, b, c, d, end}; 160 | int nerrors; 161 | 162 | char* argv[] = {"program", "-a", "hello", "--beta", "world", "GOODBYE", "AnyHow", NULL}; 163 | int argc = sizeof(argv) / sizeof(char*) - 1; 164 | 165 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 166 | 167 | nerrors = arg_parse(argc, argv, argtable); 168 | CuAssertTrue(tc, nerrors == 0); 169 | 170 | CuAssertTrue(tc, a->count == 1); 171 | CuAssertStrEquals(tc, a->sval[0], "hello"); 172 | CuAssertTrue(tc, b->count == 1); 173 | CuAssertStrEquals(tc, b->sval[0], "world"); 174 | CuAssertTrue(tc, c->count == 1); 175 | CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); 176 | CuAssertTrue(tc, d->count == 1); 177 | CuAssertStrEquals(tc, d->sval[0], "AnyHow"); 178 | 179 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 180 | } 181 | 182 | void test_argrex_basic_006(CuTest* tc) { 183 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 184 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 185 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 186 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 187 | struct arg_end* end = arg_end(20); 188 | void* argtable[] = {a, b, c, d, end}; 189 | int nerrors; 190 | 191 | char* argv[] = {"program", "--beta", "WORLD", "goodbye", NULL}; 192 | int argc = sizeof(argv) / sizeof(char*) - 1; 193 | 194 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 195 | 196 | nerrors = arg_parse(argc, argv, argtable); 197 | CuAssertTrue(tc, nerrors == 1); 198 | 199 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 200 | } 201 | 202 | void test_argrex_basic_007(CuTest* tc) { 203 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 204 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 205 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 206 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 207 | struct arg_end* end = arg_end(20); 208 | void* argtable[] = {a, b, c, d, end}; 209 | int nerrors; 210 | 211 | char* argv[] = {"program", "--beta", "World", "goodby", NULL}; 212 | int argc = sizeof(argv) / sizeof(char*) - 1; 213 | 214 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 215 | 216 | nerrors = arg_parse(argc, argv, argtable); 217 | CuAssertTrue(tc, nerrors == 1); 218 | 219 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 220 | } 221 | 222 | void test_argrex_basic_008(CuTest* tc) { 223 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 224 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 225 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 226 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 227 | struct arg_end* end = arg_end(20); 228 | void* argtable[] = {a, b, c, d, end}; 229 | int nerrors; 230 | 231 | char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "gOoDbyE", "Anything", "goodbye", NULL}; 232 | int argc = sizeof(argv) / sizeof(char*) - 1; 233 | 234 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 235 | 236 | nerrors = arg_parse(argc, argv, argtable); 237 | CuAssertTrue(tc, nerrors == 1); 238 | 239 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 240 | } 241 | 242 | void test_argrex_basic_009(CuTest* tc) { 243 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 244 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 245 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 246 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 247 | struct arg_end* end = arg_end(20); 248 | void* argtable[] = {a, b, c, d, end}; 249 | int nerrors; 250 | 251 | char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "gOoDbyE", "Anything", "Anytime", NULL}; 252 | int argc = sizeof(argv) / sizeof(char*) - 1; 253 | 254 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 255 | 256 | nerrors = arg_parse(argc, argv, argtable); 257 | CuAssertTrue(tc, nerrors == 1); 258 | 259 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 260 | } 261 | 262 | void test_argrex_basic_010(CuTest* tc) { 263 | struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); 264 | struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); 265 | struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); 266 | struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); 267 | struct arg_end* end = arg_end(20); 268 | void* argtable[] = {a, b, c, d, end}; 269 | int nerrors; 270 | 271 | char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "Anything", "-a", "Hello", NULL}; 272 | int argc = sizeof(argv) / sizeof(char*) - 1; 273 | 274 | CuAssertTrue(tc, arg_nullcheck(argtable) == 0); 275 | 276 | nerrors = arg_parse(argc, argv, argtable); 277 | CuAssertTrue(tc, nerrors == 1); 278 | 279 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 280 | } 281 | 282 | CuSuite* get_argrex_testsuite() { 283 | CuSuite* suite = CuSuiteNew(); 284 | SUITE_ADD_TEST(suite, test_argrex_basic_001); 285 | SUITE_ADD_TEST(suite, test_argrex_basic_002); 286 | SUITE_ADD_TEST(suite, test_argrex_basic_003); 287 | SUITE_ADD_TEST(suite, test_argrex_basic_004); 288 | SUITE_ADD_TEST(suite, test_argrex_basic_005); 289 | SUITE_ADD_TEST(suite, test_argrex_basic_006); 290 | SUITE_ADD_TEST(suite, test_argrex_basic_007); 291 | SUITE_ADD_TEST(suite, test_argrex_basic_008); 292 | SUITE_ADD_TEST(suite, test_argrex_basic_009); 293 | SUITE_ADD_TEST(suite, test_argrex_basic_010); 294 | return suite; 295 | } 296 | 297 | #if defined(_MSC_VER) 298 | #pragma warning(pop) 299 | #endif 300 | -------------------------------------------------------------------------------- /lzma/Alloc.c: -------------------------------------------------------------------------------- 1 | /* Alloc.c -- Memory allocation functions 2 | 2008-09-24 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifdef _WIN32 7 | #include 8 | #endif 9 | #include 10 | 11 | #include "Alloc.h" 12 | 13 | /* #define _SZ_ALLOC_DEBUG */ 14 | 15 | /* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ 16 | #ifdef _SZ_ALLOC_DEBUG 17 | #include 18 | int g_allocCount = 0; 19 | int g_allocCountMid = 0; 20 | int g_allocCountBig = 0; 21 | #endif 22 | 23 | void *MyAlloc(size_t size) 24 | { 25 | if (size == 0) 26 | return 0; 27 | #ifdef _SZ_ALLOC_DEBUG 28 | { 29 | void *p = malloc(size); 30 | fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p); 31 | return p; 32 | } 33 | #else 34 | return malloc(size); 35 | #endif 36 | } 37 | 38 | void MyFree(void *address) 39 | { 40 | #ifdef _SZ_ALLOC_DEBUG 41 | if (address != 0) 42 | fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address); 43 | #endif 44 | free(address); 45 | } 46 | 47 | #ifdef _WIN32 48 | 49 | void *MidAlloc(size_t size) 50 | { 51 | if (size == 0) 52 | return 0; 53 | #ifdef _SZ_ALLOC_DEBUG 54 | fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); 55 | #endif 56 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 57 | } 58 | 59 | void MidFree(void *address) 60 | { 61 | #ifdef _SZ_ALLOC_DEBUG 62 | if (address != 0) 63 | fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); 64 | #endif 65 | if (address == 0) 66 | return; 67 | VirtualFree(address, 0, MEM_RELEASE); 68 | } 69 | 70 | #ifndef MEM_LARGE_PAGES 71 | #undef _7ZIP_LARGE_PAGES 72 | #endif 73 | 74 | #ifdef _7ZIP_LARGE_PAGES 75 | SIZE_T g_LargePageSize = 0; 76 | typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); 77 | #endif 78 | 79 | void SetLargePageSize() 80 | { 81 | #ifdef _7ZIP_LARGE_PAGES 82 | SIZE_T size = 0; 83 | GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) 84 | GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); 85 | if (largePageMinimum == 0) 86 | return; 87 | size = largePageMinimum(); 88 | if (size == 0 || (size & (size - 1)) != 0) 89 | return; 90 | g_LargePageSize = size; 91 | #endif 92 | } 93 | 94 | 95 | void *BigAlloc(size_t size) 96 | { 97 | if (size == 0) 98 | return 0; 99 | #ifdef _SZ_ALLOC_DEBUG 100 | fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); 101 | #endif 102 | 103 | #ifdef _7ZIP_LARGE_PAGES 104 | if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) 105 | { 106 | void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), 107 | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); 108 | if (res != 0) 109 | return res; 110 | } 111 | #endif 112 | return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 113 | } 114 | 115 | void BigFree(void *address) 116 | { 117 | #ifdef _SZ_ALLOC_DEBUG 118 | if (address != 0) 119 | fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); 120 | #endif 121 | 122 | if (address == 0) 123 | return; 124 | VirtualFree(address, 0, MEM_RELEASE); 125 | } 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /lzma/Alloc.h: -------------------------------------------------------------------------------- 1 | /* Alloc.h -- Memory allocation functions 2 | 2008-03-13 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __COMMON_ALLOC_H 7 | #define __COMMON_ALLOC_H 8 | 9 | #include 10 | 11 | void *MyAlloc(size_t size); 12 | void MyFree(void *address); 13 | 14 | #ifdef _WIN32 15 | 16 | void SetLargePageSize(); 17 | 18 | void *MidAlloc(size_t size); 19 | void MidFree(void *address); 20 | void *BigAlloc(size_t size); 21 | void BigFree(void *address); 22 | 23 | #else 24 | 25 | #define MidAlloc(size) MyAlloc(size) 26 | #define MidFree(address) MyFree(address) 27 | #define BigAlloc(size) MyAlloc(size) 28 | #define BigFree(address) MyFree(address) 29 | 30 | #endif 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /lzma/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(liblzma) 2 | aux_source_directory(. DIR_LIB_SRCS) 3 | add_library (lzma STATIC ${DIR_LIB_SRCS}) -------------------------------------------------------------------------------- /lzma/LzFind.h: -------------------------------------------------------------------------------- 1 | /* LzFind.h -- Match finder for LZ algorithms 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZFIND_H 5 | #define __LZFIND_H 6 | 7 | #include "Types.h" 8 | 9 | typedef UInt32 CLzRef; 10 | 11 | typedef struct _CMatchFinder 12 | { 13 | Byte *buffer; 14 | UInt32 pos; 15 | UInt32 posLimit; 16 | UInt32 streamPos; 17 | UInt32 lenLimit; 18 | 19 | UInt32 cyclicBufferPos; 20 | UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ 21 | 22 | UInt32 matchMaxLen; 23 | CLzRef *hash; 24 | CLzRef *son; 25 | UInt32 hashMask; 26 | UInt32 cutValue; 27 | 28 | Byte *bufferBase; 29 | ISeqInStream *stream; 30 | int streamEndWasReached; 31 | 32 | UInt32 blockSize; 33 | UInt32 keepSizeBefore; 34 | UInt32 keepSizeAfter; 35 | 36 | UInt32 numHashBytes; 37 | int directInput; 38 | int btMode; 39 | /* int skipModeBits; */ 40 | int bigHash; 41 | UInt32 historySize; 42 | UInt32 fixedHashSize; 43 | UInt32 hashSizeSum; 44 | UInt32 numSons; 45 | SRes result; 46 | UInt32 crc[256]; 47 | } CMatchFinder; 48 | 49 | #define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer) 50 | #define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)]) 51 | 52 | #define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos) 53 | 54 | int MatchFinder_NeedMove(CMatchFinder *p); 55 | Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); 56 | void MatchFinder_MoveBlock(CMatchFinder *p); 57 | void MatchFinder_ReadIfRequired(CMatchFinder *p); 58 | 59 | void MatchFinder_Construct(CMatchFinder *p); 60 | 61 | /* Conditions: 62 | historySize <= 3 GB 63 | keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB 64 | */ 65 | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, 66 | UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, 67 | ISzAlloc *alloc); 68 | void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc); 69 | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems); 70 | void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); 71 | 72 | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, 73 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, 74 | UInt32 *distances, UInt32 maxLen); 75 | 76 | /* 77 | Conditions: 78 | Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. 79 | Mf_GetPointerToCurrentPos_Func's result must be used only before any other function 80 | */ 81 | 82 | typedef void (*Mf_Init_Func)(void *object); 83 | typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index); 84 | typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); 85 | typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); 86 | typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances); 87 | typedef void (*Mf_Skip_Func)(void *object, UInt32); 88 | 89 | typedef struct _IMatchFinder 90 | { 91 | Mf_Init_Func Init; 92 | Mf_GetIndexByte_Func GetIndexByte; 93 | Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; 94 | Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; 95 | Mf_GetMatches_Func GetMatches; 96 | Mf_Skip_Func Skip; 97 | } IMatchFinder; 98 | 99 | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable); 100 | 101 | void MatchFinder_Init(CMatchFinder *p); 102 | UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 103 | UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); 104 | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 105 | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /lzma/LzHash.h: -------------------------------------------------------------------------------- 1 | /* LzHash.h -- HASH functions for LZ algorithms 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZHASH_H 5 | #define __LZHASH_H 6 | 7 | #define kHash2Size (1 << 10) 8 | #define kHash3Size (1 << 16) 9 | #define kHash4Size (1 << 20) 10 | 11 | #define kFix3HashSize (kHash2Size) 12 | #define kFix4HashSize (kHash2Size + kHash3Size) 13 | #define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) 14 | 15 | #define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8); 16 | 17 | #define HASH3_CALC { \ 18 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 19 | hash2Value = temp & (kHash2Size - 1); \ 20 | hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; } 21 | 22 | #define HASH4_CALC { \ 23 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 24 | hash2Value = temp & (kHash2Size - 1); \ 25 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 26 | hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; } 27 | 28 | #define HASH5_CALC { \ 29 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 30 | hash2Value = temp & (kHash2Size - 1); \ 31 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 32 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \ 33 | hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \ 34 | hash4Value &= (kHash4Size - 1); } 35 | 36 | /* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */ 37 | #define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF; 38 | 39 | 40 | #define MT_HASH2_CALC \ 41 | hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1); 42 | 43 | #define MT_HASH3_CALC { \ 44 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 45 | hash2Value = temp & (kHash2Size - 1); \ 46 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } 47 | 48 | #define MT_HASH4_CALC { \ 49 | UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ 50 | hash2Value = temp & (kHash2Size - 1); \ 51 | hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ 52 | hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /lzma/LzmaDec.h: -------------------------------------------------------------------------------- 1 | /* LzmaDec.h -- LZMA Decoder 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMADEC_H 5 | #define __LZMADEC_H 6 | 7 | #include "Types.h" 8 | 9 | /* #define _LZMA_PROB32 */ 10 | /* _LZMA_PROB32 can increase the speed on some CPUs, 11 | but memory usage for CLzmaDec::probs will be doubled in that case */ 12 | 13 | #ifdef _LZMA_PROB32 14 | #define CLzmaProb UInt32 15 | #else 16 | #define CLzmaProb UInt16 17 | #endif 18 | 19 | 20 | /* ---------- LZMA Properties ---------- */ 21 | 22 | #define LZMA_PROPS_SIZE 5 23 | 24 | typedef struct _CLzmaProps 25 | { 26 | unsigned lc, lp, pb; 27 | UInt32 dicSize; 28 | } CLzmaProps; 29 | 30 | /* LzmaProps_Decode - decodes properties 31 | Returns: 32 | SZ_OK 33 | SZ_ERROR_UNSUPPORTED - Unsupported properties 34 | */ 35 | 36 | SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); 37 | 38 | 39 | /* ---------- LZMA Decoder state ---------- */ 40 | 41 | /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. 42 | Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ 43 | 44 | #define LZMA_REQUIRED_INPUT_MAX 20 45 | 46 | typedef struct 47 | { 48 | CLzmaProps prop; 49 | CLzmaProb *probs; 50 | Byte *dic; 51 | const Byte *buf; 52 | UInt32 range, code; 53 | SizeT dicPos; 54 | SizeT dicBufSize; 55 | UInt32 processedPos; 56 | UInt32 checkDicSize; 57 | unsigned state; 58 | UInt32 reps[4]; 59 | unsigned remainLen; 60 | int needFlush; 61 | int needInitState; 62 | UInt32 numProbs; 63 | unsigned tempBufSize; 64 | Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; 65 | } CLzmaDec; 66 | 67 | #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } 68 | 69 | void LzmaDec_Init(CLzmaDec *p); 70 | 71 | /* There are two types of LZMA streams: 72 | 0) Stream with end mark. That end mark adds about 6 bytes to compressed size. 73 | 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */ 74 | 75 | typedef enum 76 | { 77 | LZMA_FINISH_ANY, /* finish at any point */ 78 | LZMA_FINISH_END /* block must be finished at the end */ 79 | } ELzmaFinishMode; 80 | 81 | /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! 82 | 83 | You must use LZMA_FINISH_END, when you know that current output buffer 84 | covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. 85 | 86 | If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, 87 | and output value of destLen will be less than output buffer size limit. 88 | You can check status result also. 89 | 90 | You can use multiple checks to test data integrity after full decompression: 91 | 1) Check Result and "status" variable. 92 | 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. 93 | 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. 94 | You must use correct finish mode in that case. */ 95 | 96 | typedef enum 97 | { 98 | LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ 99 | LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ 100 | LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ 101 | LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ 102 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ 103 | } ELzmaStatus; 104 | 105 | /* ELzmaStatus is used only as output value for function call */ 106 | 107 | 108 | /* ---------- Interfaces ---------- */ 109 | 110 | /* There are 3 levels of interfaces: 111 | 1) Dictionary Interface 112 | 2) Buffer Interface 113 | 3) One Call Interface 114 | You can select any of these interfaces, but don't mix functions from different 115 | groups for same object. */ 116 | 117 | 118 | /* There are two variants to allocate state for Dictionary Interface: 119 | 1) LzmaDec_Allocate / LzmaDec_Free 120 | 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs 121 | You can use variant 2, if you set dictionary buffer manually. 122 | For Buffer Interface you must always use variant 1. 123 | 124 | LzmaDec_Allocate* can return: 125 | SZ_OK 126 | SZ_ERROR_MEM - Memory allocation error 127 | SZ_ERROR_UNSUPPORTED - Unsupported properties 128 | */ 129 | 130 | SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc); 131 | void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc); 132 | 133 | SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc); 134 | void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); 135 | 136 | /* ---------- Dictionary Interface ---------- */ 137 | 138 | /* You can use it, if you want to eliminate the overhead for data copying from 139 | dictionary to some other external buffer. 140 | You must work with CLzmaDec variables directly in this interface. 141 | 142 | STEPS: 143 | LzmaDec_Constr() 144 | LzmaDec_Allocate() 145 | for (each new stream) 146 | { 147 | LzmaDec_Init() 148 | while (it needs more decompression) 149 | { 150 | LzmaDec_DecodeToDic() 151 | use data from CLzmaDec::dic and update CLzmaDec::dicPos 152 | } 153 | } 154 | LzmaDec_Free() 155 | */ 156 | 157 | /* LzmaDec_DecodeToDic 158 | 159 | The decoding to internal dictionary buffer (CLzmaDec::dic). 160 | You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! 161 | 162 | finishMode: 163 | It has meaning only if the decoding reaches output limit (dicLimit). 164 | LZMA_FINISH_ANY - Decode just dicLimit bytes. 165 | LZMA_FINISH_END - Stream must be finished after dicLimit. 166 | 167 | Returns: 168 | SZ_OK 169 | status: 170 | LZMA_STATUS_FINISHED_WITH_MARK 171 | LZMA_STATUS_NOT_FINISHED 172 | LZMA_STATUS_NEEDS_MORE_INPUT 173 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 174 | SZ_ERROR_DATA - Data error 175 | */ 176 | 177 | SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, 178 | const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 179 | 180 | 181 | /* ---------- Buffer Interface ---------- */ 182 | 183 | /* It's zlib-like interface. 184 | See LzmaDec_DecodeToDic description for information about STEPS and return results, 185 | but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need 186 | to work with CLzmaDec variables manually. 187 | 188 | finishMode: 189 | It has meaning only if the decoding reaches output limit (*destLen). 190 | LZMA_FINISH_ANY - Decode just destLen bytes. 191 | LZMA_FINISH_END - Stream must be finished after (*destLen). 192 | */ 193 | 194 | SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, 195 | const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 196 | 197 | 198 | /* ---------- One Call Interface ---------- */ 199 | 200 | /* LzmaDecode 201 | 202 | finishMode: 203 | It has meaning only if the decoding reaches output limit (*destLen). 204 | LZMA_FINISH_ANY - Decode just destLen bytes. 205 | LZMA_FINISH_END - Stream must be finished after (*destLen). 206 | 207 | Returns: 208 | SZ_OK 209 | status: 210 | LZMA_STATUS_FINISHED_WITH_MARK 211 | LZMA_STATUS_NOT_FINISHED 212 | LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 213 | SZ_ERROR_DATA - Data error 214 | SZ_ERROR_MEM - Memory allocation error 215 | SZ_ERROR_UNSUPPORTED - Unsupported properties 216 | SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). 217 | */ 218 | 219 | SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 220 | const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, 221 | ELzmaStatus *status, ISzAlloc *alloc); 222 | 223 | #endif 224 | -------------------------------------------------------------------------------- /lzma/LzmaEnc.h: -------------------------------------------------------------------------------- 1 | /* LzmaEnc.h -- LZMA Encoder 2 | 2008-10-04 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __LZMAENC_H 5 | #define __LZMAENC_H 6 | 7 | #include "Types.h" 8 | 9 | #define LZMA_PROPS_SIZE 5 10 | 11 | typedef struct _CLzmaEncProps 12 | { 13 | int level; /* 0 <= level <= 9 */ 14 | UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version 15 | (1 << 12) <= dictSize <= (1 << 30) for 64-bit version 16 | default = (1 << 24) */ 17 | int lc; /* 0 <= lc <= 8, default = 3 */ 18 | int lp; /* 0 <= lp <= 4, default = 0 */ 19 | int pb; /* 0 <= pb <= 4, default = 2 */ 20 | int algo; /* 0 - fast, 1 - normal, default = 1 */ 21 | int fb; /* 5 <= fb <= 273, default = 32 */ 22 | int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ 23 | int numHashBytes; /* 2, 3 or 4, default = 4 */ 24 | UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ 25 | unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ 26 | int numThreads; /* 1 or 2, default = 2 */ 27 | } CLzmaEncProps; 28 | 29 | void LzmaEncProps_Init(CLzmaEncProps *p); 30 | void LzmaEncProps_Normalize(CLzmaEncProps *p); 31 | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2); 32 | 33 | 34 | /* ---------- CLzmaEncHandle Interface ---------- */ 35 | 36 | /* LzmaEnc_* functions can return the following exit codes: 37 | Returns: 38 | SZ_OK - OK 39 | SZ_ERROR_MEM - Memory allocation error 40 | SZ_ERROR_PARAM - Incorrect paramater in props 41 | SZ_ERROR_WRITE - Write callback error. 42 | SZ_ERROR_PROGRESS - some break from progress callback 43 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 44 | */ 45 | 46 | typedef void * CLzmaEncHandle; 47 | 48 | CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc); 49 | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig); 50 | SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props); 51 | SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); 52 | SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, 53 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 54 | SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 55 | int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 56 | 57 | /* ---------- One Call Interface ---------- */ 58 | 59 | /* LzmaEncode 60 | Return code: 61 | SZ_OK - OK 62 | SZ_ERROR_MEM - Memory allocation error 63 | SZ_ERROR_PARAM - Incorrect paramater 64 | SZ_ERROR_OUTPUT_EOF - output buffer overflow 65 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 66 | */ 67 | 68 | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 69 | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 70 | ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /lzma/LzmaLib.c: -------------------------------------------------------------------------------- 1 | /* LzmaLib.c -- LZMA library wrapper 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #include "LzmaEnc.h" 7 | #include "LzmaDec.h" 8 | #include "Alloc.h" 9 | #include "LzmaLib.h" 10 | 11 | static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } 12 | static void SzFree(void *p, void *address) { p = p; MyFree(address); } 13 | static ISzAlloc g_Alloc = { SzAlloc, SzFree }; 14 | 15 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 16 | unsigned char *outProps, size_t *outPropsSize, 17 | int level, /* 0 <= level <= 9, default = 5 */ 18 | unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */ 19 | int lc, /* 0 <= lc <= 8, default = 3 */ 20 | int lp, /* 0 <= lp <= 4, default = 0 */ 21 | int pb, /* 0 <= pb <= 4, default = 2 */ 22 | int fb, /* 5 <= fb <= 273, default = 32 */ 23 | int numThreads /* 1 or 2, default = 2 */ 24 | ) 25 | { 26 | CLzmaEncProps props; 27 | LzmaEncProps_Init(&props); 28 | props.level = level; 29 | props.dictSize = dictSize; 30 | props.lc = lc; 31 | props.lp = lp; 32 | props.pb = pb; 33 | props.fb = fb; 34 | props.numThreads = numThreads; 35 | 36 | return LzmaEncode(dest, destLen, src, srcLen, &props, outProps, outPropsSize, 0, 37 | NULL, &g_Alloc, &g_Alloc); 38 | } 39 | 40 | 41 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t *srcLen, 42 | const unsigned char *props, size_t propsSize) 43 | { 44 | ELzmaStatus status; 45 | return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc); 46 | } 47 | -------------------------------------------------------------------------------- /lzma/LzmaLib.h: -------------------------------------------------------------------------------- 1 | /* LzmaLib.h -- LZMA library interface 2 | 2008-08-05 3 | Igor Pavlov 4 | Public domain */ 5 | 6 | #ifndef __LZMALIB_H 7 | #define __LZMALIB_H 8 | 9 | #include "Types.h" 10 | 11 | #ifdef __cplusplus 12 | #define MY_EXTERN_C extern "C" 13 | #else 14 | #define MY_EXTERN_C extern 15 | #endif 16 | 17 | #define MY_STDAPI MY_EXTERN_C int MY_STD_CALL 18 | 19 | #define LZMA_PROPS_SIZE 5 20 | 21 | /* 22 | RAM requirements for LZMA: 23 | for compression: (dictSize * 11.5 + 6 MB) + state_size 24 | for decompression: dictSize + state_size 25 | state_size = (4 + (1.5 << (lc + lp))) KB 26 | by default (lc=3, lp=0), state_size = 16 KB. 27 | 28 | LZMA properties (5 bytes) format 29 | Offset Size Description 30 | 0 1 lc, lp and pb in encoded form. 31 | 1 4 dictSize (little endian). 32 | */ 33 | 34 | /* 35 | LzmaCompress 36 | ------------ 37 | 38 | outPropsSize - 39 | In: the pointer to the size of outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 40 | Out: the pointer to the size of written properties in outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. 41 | 42 | LZMA Encoder will use defult values for any parameter, if it is 43 | -1 for any from: level, loc, lp, pb, fb, numThreads 44 | 0 for dictSize 45 | 46 | level - compression level: 0 <= level <= 9; 47 | 48 | level dictSize algo fb 49 | 0: 16 KB 0 32 50 | 1: 64 KB 0 32 51 | 2: 256 KB 0 32 52 | 3: 1 MB 0 32 53 | 4: 4 MB 0 32 54 | 5: 16 MB 1 32 55 | 6: 32 MB 1 32 56 | 7+: 64 MB 1 64 57 | 58 | The default value for "level" is 5. 59 | 60 | algo = 0 means fast method 61 | algo = 1 means normal method 62 | 63 | dictSize - The dictionary size in bytes. The maximum value is 64 | 128 MB = (1 << 27) bytes for 32-bit version 65 | 1 GB = (1 << 30) bytes for 64-bit version 66 | The default value is 16 MB = (1 << 24) bytes. 67 | It's recommended to use the dictionary that is larger than 4 KB and 68 | that can be calculated as (1 << N) or (3 << N) sizes. 69 | 70 | lc - The number of literal context bits (high bits of previous literal). 71 | It can be in the range from 0 to 8. The default value is 3. 72 | Sometimes lc=4 gives the gain for big files. 73 | 74 | lp - The number of literal pos bits (low bits of current position for literals). 75 | It can be in the range from 0 to 4. The default value is 0. 76 | The lp switch is intended for periodical data when the period is equal to 2^lp. 77 | For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's 78 | better to set lc=0, if you change lp switch. 79 | 80 | pb - The number of pos bits (low bits of current position). 81 | It can be in the range from 0 to 4. The default value is 2. 82 | The pb switch is intended for periodical data when the period is equal 2^pb. 83 | 84 | fb - Word size (the number of fast bytes). 85 | It can be in the range from 5 to 273. The default value is 32. 86 | Usually, a big number gives a little bit better compression ratio and 87 | slower compression process. 88 | 89 | numThreads - The number of thereads. 1 or 2. The default value is 2. 90 | Fast mode (algo = 0) can use only 1 thread. 91 | 92 | Out: 93 | destLen - processed output size 94 | Returns: 95 | SZ_OK - OK 96 | SZ_ERROR_MEM - Memory allocation error 97 | SZ_ERROR_PARAM - Incorrect paramater 98 | SZ_ERROR_OUTPUT_EOF - output buffer overflow 99 | SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 100 | */ 101 | 102 | MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, 103 | unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ 104 | int level, /* 0 <= level <= 9, default = 5 */ 105 | unsigned dictSize, /* default = (1 << 24) */ 106 | int lc, /* 0 <= lc <= 8, default = 3 */ 107 | int lp, /* 0 <= lp <= 4, default = 0 */ 108 | int pb, /* 0 <= pb <= 4, default = 2 */ 109 | int fb, /* 5 <= fb <= 273, default = 32 */ 110 | int numThreads /* 1 or 2, default = 2 */ 111 | ); 112 | 113 | /* 114 | LzmaUncompress 115 | -------------- 116 | In: 117 | dest - output data 118 | destLen - output data size 119 | src - input data 120 | srcLen - input data size 121 | Out: 122 | destLen - processed output size 123 | srcLen - processed input size 124 | Returns: 125 | SZ_OK - OK 126 | SZ_ERROR_DATA - Data error 127 | SZ_ERROR_MEM - Memory allocation arror 128 | SZ_ERROR_UNSUPPORTED - Unsupported properties 129 | SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) 130 | */ 131 | 132 | MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, 133 | const unsigned char *props, size_t propsSize); 134 | 135 | #endif 136 | -------------------------------------------------------------------------------- /lzma/Types.h: -------------------------------------------------------------------------------- 1 | /* Types.h -- Basic types 2 | 2008-11-23 : Igor Pavlov : Public domain */ 3 | 4 | #ifndef __7Z_TYPES_H 5 | #define __7Z_TYPES_H 6 | 7 | #ifndef BCMLZMA 8 | #include 9 | #endif 10 | 11 | #ifdef _WIN32 12 | #include 13 | #endif 14 | 15 | #define SZ_OK 0 16 | 17 | #define SZ_ERROR_DATA 1 18 | #define SZ_ERROR_MEM 2 19 | #define SZ_ERROR_CRC 3 20 | #define SZ_ERROR_UNSUPPORTED 4 21 | #define SZ_ERROR_PARAM 5 22 | #define SZ_ERROR_INPUT_EOF 6 23 | #define SZ_ERROR_OUTPUT_EOF 7 24 | #define SZ_ERROR_READ 8 25 | #define SZ_ERROR_WRITE 9 26 | #define SZ_ERROR_PROGRESS 10 27 | #define SZ_ERROR_FAIL 11 28 | #define SZ_ERROR_THREAD 12 29 | 30 | #define SZ_ERROR_ARCHIVE 16 31 | #define SZ_ERROR_NO_ARCHIVE 17 32 | 33 | typedef int SRes; 34 | 35 | #ifdef _WIN32 36 | typedef DWORD WRes; 37 | #else 38 | typedef int WRes; 39 | #endif 40 | 41 | #ifndef RINOK 42 | #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } 43 | #endif 44 | 45 | #ifndef Byte 46 | typedef unsigned char Byte; 47 | #endif 48 | typedef short Int16; 49 | typedef unsigned short UInt16; 50 | 51 | #ifdef _LZMA_UINT32_IS_ULONG 52 | typedef long Int32; 53 | typedef unsigned long UInt32; 54 | #else 55 | typedef int Int32; 56 | typedef unsigned int UInt32; 57 | #endif 58 | 59 | #ifdef _SZ_NO_INT_64 60 | 61 | /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. 62 | NOTES: Some code will work incorrectly in that case! */ 63 | 64 | typedef long Int64; 65 | typedef unsigned long UInt64; 66 | 67 | #else 68 | 69 | #if defined(_MSC_VER) || defined(__BORLANDC__) 70 | typedef __int64 Int64; 71 | typedef unsigned __int64 UInt64; 72 | #else 73 | typedef long long int Int64; 74 | typedef unsigned long long int UInt64; 75 | #endif 76 | 77 | #endif 78 | 79 | #ifdef _LZMA_NO_SYSTEM_SIZE_T 80 | typedef UInt32 SizeT; 81 | #else 82 | typedef size_t SizeT; 83 | #endif 84 | 85 | typedef int Bool; 86 | #define True 1 87 | #define False 0 88 | 89 | 90 | #ifdef _MSC_VER 91 | 92 | #if _MSC_VER >= 1300 93 | #define MY_NO_INLINE __declspec(noinline) 94 | #else 95 | #define MY_NO_INLINE 96 | #endif 97 | 98 | #define MY_CDECL __cdecl 99 | #define MY_STD_CALL __stdcall 100 | #define MY_FAST_CALL MY_NO_INLINE __fastcall 101 | 102 | #else 103 | 104 | #define MY_CDECL 105 | #define MY_STD_CALL 106 | #define MY_FAST_CALL 107 | 108 | #endif 109 | 110 | 111 | /* The following interfaces use first parameter as pointer to structure */ 112 | 113 | typedef struct 114 | { 115 | SRes (*Read)(void *p, void *buf, size_t *size); 116 | /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 117 | (output(*size) < input(*size)) is allowed */ 118 | } ISeqInStream; 119 | 120 | /* it can return SZ_ERROR_INPUT_EOF */ 121 | SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size); 122 | SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType); 123 | SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf); 124 | 125 | typedef struct 126 | { 127 | size_t (*Write)(void *p, const void *buf, size_t size); 128 | /* Returns: result - the number of actually written bytes. 129 | (result < size) means error */ 130 | } ISeqOutStream; 131 | 132 | typedef enum 133 | { 134 | SZ_SEEK_SET = 0, 135 | SZ_SEEK_CUR = 1, 136 | SZ_SEEK_END = 2 137 | } ESzSeek; 138 | 139 | typedef struct 140 | { 141 | SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ 142 | SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); 143 | } ISeekInStream; 144 | 145 | typedef struct 146 | { 147 | SRes (*Look)(void *p, void **buf, size_t *size); 148 | /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 149 | (output(*size) > input(*size)) is not allowed 150 | (output(*size) < input(*size)) is allowed */ 151 | SRes (*Skip)(void *p, size_t offset); 152 | /* offset must be <= output(*size) of Look */ 153 | 154 | SRes (*Read)(void *p, void *buf, size_t *size); 155 | /* reads directly (without buffer). It's same as ISeqInStream::Read */ 156 | SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); 157 | } ILookInStream; 158 | 159 | SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size); 160 | SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset); 161 | 162 | /* reads via ILookInStream::Read */ 163 | SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType); 164 | SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size); 165 | 166 | #define LookToRead_BUF_SIZE (1 << 14) 167 | 168 | typedef struct 169 | { 170 | ILookInStream s; 171 | ISeekInStream *realStream; 172 | size_t pos; 173 | size_t size; 174 | Byte buf[LookToRead_BUF_SIZE]; 175 | } CLookToRead; 176 | 177 | void LookToRead_CreateVTable(CLookToRead *p, int lookahead); 178 | void LookToRead_Init(CLookToRead *p); 179 | 180 | typedef struct 181 | { 182 | ISeqInStream s; 183 | ILookInStream *realStream; 184 | } CSecToLook; 185 | 186 | void SecToLook_CreateVTable(CSecToLook *p); 187 | 188 | typedef struct 189 | { 190 | ISeqInStream s; 191 | ILookInStream *realStream; 192 | } CSecToRead; 193 | 194 | void SecToRead_CreateVTable(CSecToRead *p); 195 | 196 | typedef struct 197 | { 198 | SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize); 199 | /* Returns: result. (result != SZ_OK) means break. 200 | Value (UInt64)(Int64)-1 for size means unknown value. */ 201 | } ICompressProgress; 202 | 203 | typedef struct 204 | { 205 | void *(*Alloc)(void *p, size_t size); 206 | void (*Free)(void *p, void *address); /* address can be 0 */ 207 | } ISzAlloc; 208 | 209 | #define IAlloc_Alloc(p, size) (p)->Alloc((p), size) 210 | #define IAlloc_Free(p, a) (p)->Free((p), a) 211 | 212 | #endif 213 | -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by spoon on 2019/5/24. 3 | // 4 | 5 | #ifndef BROADCOM_CFE_TOOL_MAIN_H 6 | #define BROADCOM_CFE_TOOL_MAIN_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include "lzma/LzmaLib.h" 12 | #include "lzma/LzmaDec.h" 13 | #include "lzma/Alloc.h" 14 | #include "argtable/argtable3.h" 15 | 16 | #define VERSION_STR "v0.2.3" 17 | 18 | #define READ_BUFFER_SIZE 0x100u 19 | 20 | #define DEF_EMBED_NVRAM_OFFSET 0x400u 21 | #define DEF_EMBED_NVRAM_SIZE 0x1000u 22 | 23 | #define NVRAM_HEADER_SIZE 20 24 | #define NVRAM_MAGIC 0x48534C46u /* 'FLSH' */ 25 | #define NVRAM_VERSION 1u 26 | 27 | #define CRC8_INIT_VALUE 0xffu /* Initial CRC8 checksum value */ 28 | 29 | static const unsigned char crc8_table[256] = { 30 | 0x00, 0xF7, 0xB9, 0x4E, 0x25, 0xD2, 0x9C, 0x6B, 31 | 0x4A, 0xBD, 0xF3, 0x04, 0x6F, 0x98, 0xD6, 0x21, 32 | 0x94, 0x63, 0x2D, 0xDA, 0xB1, 0x46, 0x08, 0xFF, 33 | 0xDE, 0x29, 0x67, 0x90, 0xFB, 0x0C, 0x42, 0xB5, 34 | 0x7F, 0x88, 0xC6, 0x31, 0x5A, 0xAD, 0xE3, 0x14, 35 | 0x35, 0xC2, 0x8C, 0x7B, 0x10, 0xE7, 0xA9, 0x5E, 36 | 0xEB, 0x1C, 0x52, 0xA5, 0xCE, 0x39, 0x77, 0x80, 37 | 0xA1, 0x56, 0x18, 0xEF, 0x84, 0x73, 0x3D, 0xCA, 38 | 0xFE, 0x09, 0x47, 0xB0, 0xDB, 0x2C, 0x62, 0x95, 39 | 0xB4, 0x43, 0x0D, 0xFA, 0x91, 0x66, 0x28, 0xDF, 40 | 0x6A, 0x9D, 0xD3, 0x24, 0x4F, 0xB8, 0xF6, 0x01, 41 | 0x20, 0xD7, 0x99, 0x6E, 0x05, 0xF2, 0xBC, 0x4B, 42 | 0x81, 0x76, 0x38, 0xCF, 0xA4, 0x53, 0x1D, 0xEA, 43 | 0xCB, 0x3C, 0x72, 0x85, 0xEE, 0x19, 0x57, 0xA0, 44 | 0x15, 0xE2, 0xAC, 0x5B, 0x30, 0xC7, 0x89, 0x7E, 45 | 0x5F, 0xA8, 0xE6, 0x11, 0x7A, 0x8D, 0xC3, 0x34, 46 | 0xAB, 0x5C, 0x12, 0xE5, 0x8E, 0x79, 0x37, 0xC0, 47 | 0xE1, 0x16, 0x58, 0xAF, 0xC4, 0x33, 0x7D, 0x8A, 48 | 0x3F, 0xC8, 0x86, 0x71, 0x1A, 0xED, 0xA3, 0x54, 49 | 0x75, 0x82, 0xCC, 0x3B, 0x50, 0xA7, 0xE9, 0x1E, 50 | 0xD4, 0x23, 0x6D, 0x9A, 0xF1, 0x06, 0x48, 0xBF, 51 | 0x9E, 0x69, 0x27, 0xD0, 0xBB, 0x4C, 0x02, 0xF5, 52 | 0x40, 0xB7, 0xF9, 0x0E, 0x65, 0x92, 0xDC, 0x2B, 53 | 0x0A, 0xFD, 0xB3, 0x44, 0x2F, 0xD8, 0x96, 0x61, 54 | 0x55, 0xA2, 0xEC, 0x1B, 0x70, 0x87, 0xC9, 0x3E, 55 | 0x1F, 0xE8, 0xA6, 0x51, 0x3A, 0xCD, 0x83, 0x74, 56 | 0xC1, 0x36, 0x78, 0x8F, 0xE4, 0x13, 0x5D, 0xAA, 57 | 0x8B, 0x7C, 0x32, 0xC5, 0xAE, 0x59, 0x17, 0xE0, 58 | 0x2A, 0xDD, 0x93, 0x64, 0x0F, 0xF8, 0xB6, 0x41, 59 | 0x60, 0x97, 0xD9, 0x2E, 0x45, 0xB2, 0xFC, 0x0B, 60 | 0xBE, 0x49, 0x07, 0xF0, 0x9B, 0x6C, 0x22, 0xD5, 61 | 0xF4, 0x03, 0x4D, 0xBA, 0xD1, 0x26, 0x68, 0x9F 62 | }; 63 | 64 | typedef struct nvram_header { 65 | unsigned int magic; 66 | unsigned int len; 67 | unsigned int crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */ 68 | unsigned int config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */ 69 | unsigned int config_ncdl; /* ncdl values for memc */ 70 | } NVRAM_HEADER; 71 | 72 | #endif //BROADCOM_CFE_TOOL_MAIN_H 73 | --------------------------------------------------------------------------------