diff --git a/.gitignore b/.gitignore index 5803c66..948a6f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ bin/* build/* -lib/* example/bin/* example/build/* TODO.md @@ -21,3 +20,4 @@ depcomp build/ .deps/ config.log +tags diff --git a/Makefile.am b/Makefile.am deleted file mode 100644 index 81856b8..0000000 --- a/Makefile.am +++ /dev/null @@ -1,20 +0,0 @@ -AUTOMAKE_OPTIONS = foreign - -SUBDIRS=lib - -bin_PROGRAMS=test -test_SOURCES=test.c -test_LDADD=lib/liboptkit.la -test_CFLAGS=-I lib - - -clean-local: - @rm config.status config.log - @rm Makefile - @rm -r autom4te.cache/ - @rm aclocal.m4 - @rm compile install-sh missing Makefile.in - @rm stamp-h1 - @rm *~ config.guess config.sub depcomp - - diff --git a/config.h b/config.h deleted file mode 100644 index 6060fcf..0000000 --- a/config.h +++ /dev/null @@ -1,110 +0,0 @@ -/* config.h. Generated from config.h.in by configure. */ -/* config.h.in. Generated from configure.ac by autoheader. */ - -/* Define to 1 if you have the 'asprintf' function. */ -#define HAVE_ASPRINTF 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_DLFCN_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_INTTYPES_H 1 - -/* Define to 1 if your system has a GNU libc compatible 'malloc' function, and - to 0 otherwise. */ -#define HAVE_MALLOC 1 - -/* Define to 1 if you have the 'opem_memstream' function. */ -/* #undef HAVE_OPEM_MEMSTREAM */ - -/* Define to 1 if you have the header file. */ -#define HAVE_STDINT_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STDIO_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STDLIB_H 1 - -/* Define to 1 if you have the 'strchr' function. */ -#define HAVE_STRCHR 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STRINGS_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_STRING_H 1 - -/* Define to 1 if you have the 'strrchr' function. */ -#define HAVE_STRRCHR 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_SYS_STAT_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_SYS_TYPES_H 1 - -/* Define to 1 if you have the header file. */ -#define HAVE_UNISTD_H 1 - -/* Define to the sub-directory where libtool stores uninstalled libraries. */ -#define LT_OBJDIR ".libs/" - -/* Name of package */ -#define PACKAGE "optkit" - -/* Define to the address where bug reports for this package should be sent. */ -#define PACKAGE_BUGREPORT "jUmarB@protonmail.com" - -/* Define to the full name of this package. */ -#define PACKAGE_NAME "optkit" - -/* Define to the full name and version of this package. */ -#define PACKAGE_STRING "optkit 0.1" - -/* Define to the one symbol short name of this package. */ -#define PACKAGE_TARNAME "optkit" - -/* Define to the home page for this package. */ -#define PACKAGE_URL "" - -/* Define to the version of this package. */ -#define PACKAGE_VERSION "0.1" - -/* Define to 1 if all of the C89 standard headers exist (not just the ones - required in a freestanding environment). This macro is provided for - backward compatibility; new code need not use it. */ -#define STDC_HEADERS 1 - -/* Version number of package */ -#define VERSION "0.1" - -/* Define to '__inline__' or '__inline' if that's what the C compiler - calls it, or to nothing if 'inline' is not supported under any name. */ -#ifndef __cplusplus -/* #undef inline */ -#endif - -/* Define to rpl_malloc if the replacement function should be used. */ -/* #undef malloc */ - -/* Define to the equivalent of the C99 'restrict' keyword, or to - nothing if this is not supported. Do not define if restrict is - supported only directly. */ -#define restrict __restrict__ -/* Work around a bug in older versions of Sun C++, which did not - #define __restrict__ or support _Restrict or __restrict__ - even though the corresponding Sun C compiler ended up with - "#define restrict _Restrict" or "#define restrict __restrict__" - in the previous line. This workaround can be removed once - we assume Oracle Developer Studio 12.5 (2016) or later. */ -#if defined __SUNPRO_CC && !defined __RESTRICT && !defined __restrict__ -# define _Restrict -# define __restrict__ -#endif - -/* Define as 'unsigned int' if doesn't define. */ -/* #undef size_t */ - -/* Define as 'int' if doesn't define. */ -/* #undef ssize_t */ diff --git a/config.h.in b/config.h.in deleted file mode 100644 index 46f72ee..0000000 --- a/config.h.in +++ /dev/null @@ -1,109 +0,0 @@ -/* config.h.in. Generated from configure.ac by autoheader. */ - -/* Define to 1 if you have the 'asprintf' function. */ -#undef HAVE_ASPRINTF - -/* Define to 1 if you have the header file. */ -#undef HAVE_DLFCN_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_INTTYPES_H - -/* Define to 1 if your system has a GNU libc compatible 'malloc' function, and - to 0 otherwise. */ -#undef HAVE_MALLOC - -/* Define to 1 if you have the 'opem_memstream' function. */ -#undef HAVE_OPEM_MEMSTREAM - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDINT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDIO_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDLIB_H - -/* Define to 1 if you have the 'strchr' function. */ -#undef HAVE_STRCHR - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRINGS_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRING_H - -/* Define to 1 if you have the 'strrchr' function. */ -#undef HAVE_STRRCHR - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_STAT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_TYPES_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_UNISTD_H - -/* Define to the sub-directory where libtool stores uninstalled libraries. */ -#undef LT_OBJDIR - -/* Name of package */ -#undef PACKAGE - -/* Define to the address where bug reports for this package should be sent. */ -#undef PACKAGE_BUGREPORT - -/* Define to the full name of this package. */ -#undef PACKAGE_NAME - -/* Define to the full name and version of this package. */ -#undef PACKAGE_STRING - -/* Define to the one symbol short name of this package. */ -#undef PACKAGE_TARNAME - -/* Define to the home page for this package. */ -#undef PACKAGE_URL - -/* Define to the version of this package. */ -#undef PACKAGE_VERSION - -/* Define to 1 if all of the C89 standard headers exist (not just the ones - required in a freestanding environment). This macro is provided for - backward compatibility; new code need not use it. */ -#undef STDC_HEADERS - -/* Version number of package */ -#undef VERSION - -/* Define to '__inline__' or '__inline' if that's what the C compiler - calls it, or to nothing if 'inline' is not supported under any name. */ -#ifndef __cplusplus -#undef inline -#endif - -/* Define to rpl_malloc if the replacement function should be used. */ -#undef malloc - -/* Define to the equivalent of the C99 'restrict' keyword, or to - nothing if this is not supported. Do not define if restrict is - supported only directly. */ -#undef restrict -/* Work around a bug in older versions of Sun C++, which did not - #define __restrict__ or support _Restrict or __restrict__ - even though the corresponding Sun C compiler ended up with - "#define restrict _Restrict" or "#define restrict __restrict__" - in the previous line. This workaround can be removed once - we assume Oracle Developer Studio 12.5 (2016) or later. */ -#if defined __SUNPRO_CC && !defined __RESTRICT && !defined __restrict__ -# define _Restrict -# define __restrict__ -#endif - -/* Define as 'unsigned int' if doesn't define. */ -#undef size_t - -/* Define as 'int' if doesn't define. */ -#undef ssize_t diff --git a/configure.ac b/configure.ac deleted file mode 100644 index 189890f..0000000 --- a/configure.ac +++ /dev/null @@ -1,41 +0,0 @@ -# -*- Autoconf -*- -# Process this file with autoconf to produce a configure script. - -AC_PREREQ([2.72]) -AC_INIT([optkit],[0.1],[jUmarB@protonmail.com]) -AM_INIT_AUTOMAKE -LT_INIT([dlopen]) -AC_CONFIG_SRCDIR([lib/optkit.c]) -AC_CONFIG_HEADERS([config.h]) - -# Checks for programs. -AC_PROG_CC -AC_PROG_RANLIB -AC_PROG_AR -AC_PROG_CXX -AC_PROG_CPP - -# Allow to use POSIX standard facilities -AC_SEARCH_LIBS([strerror],[cposix]) - -# Checks for libraries. - -# Checks for header files. -AC_CHECK_HEADERS([unistd.h]) - -# Checks for typedefs, structures, and compiler characteristics. -AC_C_INLINE -AC_C_RESTRICT -AC_TYPE_SIZE_T -AC_TYPE_SSIZE_T - -# Checks for library functions. -AC_FUNC_MALLOC -AC_CHECK_FUNCS([strchr - strrchr - asprintf - opem_memstream]) - -AC_CONFIG_FILES([Makefile - lib/Makefile]) -AC_OUTPUT diff --git a/lib/bits/types/optkit_cs_help.h b/lib/bits/types/optkit_cs_help.h new file mode 100644 index 0000000..06c22af --- /dev/null +++ b/lib/bits/types/optkit_cs_help.h @@ -0,0 +1,249 @@ +//!SPDX-License-Identifier: GPL-3.0 +/* + * - Optkit helper cookiestream + * A Custum memory stream io + * for helper or usage . + */ + +#if !defined(_SYS_TYPE_OPTKIT_CS_HLP) +#define _SYS_TYPE_OPTKIT_CS_HLP + +#if !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif +#define _FILE_OFFSET_BITS 64 + +#include +#include +#include + +#if defined(UNISTD_H) +# define HELPER_STREAM_BUFFER_SIZE sysconf(_SC_PAGESIZE) +#else +# if !defined(HELPER_STREAM_BUFFER_SIZE) +# define HELPER_STREAM_BUFFER_SIZE 0x1000 +# endif +#endif + +#define _IOM_WRITE 0 +#define _IOM_READ 1 + +//!NOTE : define helper usage as vector table +enum{ + USAGE_SECTION= 0, +#define _USAGE_SECTION {0x000,0xff} + SYNOPSYS_SECTION, +#define _SYNOPSYS_SECTION {0x100,0x3ff} + HELPER_SECTION, +#define _HELPER_SECTION {0x400,0xbff} + FOOTER_SECTION, +#define _FOOTER_SECTION {0xc00,0x1000} +}; + +#define NSECTIONS 4 + +typedef char iocookie_stream_buf_t; +typedef iocookie_stream_buf_t iocbs_t [HELPER_STREAM_BUFFER_SIZE] ; + + +typedef struct __section_t section_t ; +typedef struct __membuff_cookies_t membuff_cookies_t ; +typedef struct __optkit_memsb_t optkit_memsb_t ; + +struct __section_t { + size_t _begin, + _end; + union { + off_t _coffset ; //used to remember where were you last time + } ; +}; + +/* + * Internal memory buffer cookies structure + * used to manipulation the memory buffer + * */ +struct __membuff_cookies_t { + char * _sbuff ; + void * _ebuff ; + + size_t _endpos , + _page_allocation; + + off_t _offset, //used for rewind + _checkpoint; //used for persistant checkpoint + +}; + +typedef struct __section_t sections_t[NSECTIONS]; + +struct __optkit_memsb_t { + struct __membuff_cookies_t *_msbio_cookies ; + sections_t _scope_interval; + unsigned char _partition_index ; +}; + +struct __page_io_location_t{ + void * _io_location_address ; + struct __section_t _chunck_part; +}; + +typedef struct __partition_user_buffer_block_t pubb_t ; +struct __partition_user_buffer_block_t { + char * _buffer_block ; + size_t _block_size ; +}; + + +extern FILE * optkit_stream ; +extern struct __optkit_memsb_t ctx_cookies ; + + +static inline struct __membuff_cookies_t* +__setup_membuff_cookies(void) +{ + struct __membuff_cookies_t * mcookies = malloc(sizeof(*mcookies)) ; + if(!mcookies) + goto _free_mcookies ; + + + mcookies->_sbuff = malloc(HELPER_STREAM_BUFFER_SIZE) ; + if(!mcookies->_sbuff) + goto _free_mcookies ; + + + mcookies->_page_allocation = HELPER_STREAM_BUFFER_SIZE; + + mcookies->_ebuff = (mcookies->_sbuff+HELPER_STREAM_BUFFER_SIZE) ; + mcookies->_endpos = mcookies->_offset = 0 ; + + return mcookies ; + + +_free_mcookies: + free(mcookies) , mcookies=00 ; + + return 00 ; +} + +static inline off_t +__io_offaddr(struct __section_t secpartition , int io_mode) +{ + off_t offset = 0 ; + if(_IOM_WRITE == io_mode) + offset = secpartition._begin + secpartition._coffset; + + if(io_mode & _IOM_READ) + offset=secpartition._begin; + + return offset ; +} + + +static inline struct __page_io_location_t * +__get_partition_location_address(struct __optkit_memsb_t * new_ctxc, int io_mode) +{ + unsigned start = 0 ; + off_t offset_address = 0 ; + struct __page_io_location_t * ioloc = malloc(sizeof(struct __page_io_location_t *)) ; + if(!ioloc) + return 00 ; + + struct __section_t secpart = new_ctxc->_scope_interval[new_ctxc->_partition_index]; + + offset_address = __io_offaddr(secpart, io_mode); + + ioloc->_io_location_address = new_ctxc->_msbio_cookies->_sbuff+offset_address ; + + memcpy(&ioloc->_chunck_part , &secpart , sizeof(struct __section_t)) ; + + //!CHECK OVERFLOW ... + if(ioloc->_io_location_address > new_ctxc->_msbio_cookies->_ebuff) + { + free(ioloc) , ioloc = 00 ; + return (void* ) -10 ; // overflow code + } + + return ioloc ; + +} + +static inline size_t +write_at(struct __page_io_location_t * ioloc , const char * ubuff , size_t wbytes) +{ + ioloc->_chunck_part._coffset+=wbytes; + + if(ioloc->_chunck_part._coffset >= ioloc->_chunck_part._end) + return EOF ; + + + memcpy(ioloc->_io_location_address,ubuff , wbytes) ; + return (wbytes << 0x10 | ioloc->_chunck_part._coffset); +} + +static inline size_t +read_at(struct __page_io_location_t * ioloc , char * ubuff , size_t rbytes) +{ + size_t locpart_threshold = ioloc->_chunck_part._end - ioloc->_chunck_part._coffset ; + size_t rofft = ioloc->_chunck_part._coffset ; + if (rbytes > locpart_threshold) + rbytes = locpart_threshold ; + + + memcpy(ubuff , ioloc->_io_location_address+rofft , rbytes); + ioloc->_chunck_part._coffset++; + + return rbytes ; +} + + + +static inline void * __expand_buffer(struct __optkit_memsb_t * new_ctxc) +{ + char * buffer_expension = 00 ; + struct __membuff_cookies_t * mbios = new_ctxc->_msbio_cookies; + + mbios->_page_allocation<<=1 ; + buffer_expension = realloc(mbios->_sbuff , mbios->_page_allocation); + if(!buffer_expension) + return 00 ; + + mbios->_sbuff = buffer_expension ; + mbios->_ebuff = (mbios->_sbuff+ mbios->_page_allocation) ; + + return new_ctxc ; + +} + +struct __optkit_memsb_t * +init_memstream_buffer_cookies(void) ; + +/* Predefined Hooks for cookies stream io manipulation */ +ssize_t +iomem_write(void * ctx_cookies , const char * buff , size_t wbytesize) ; + +ssize_t +iomem_read(void * ctx_cookies , char * buff , size_t rbytesize) ; + +int +iomem_seek(void * ctx_cookies , off_t *offset , int whence) ; + +int +iomem_close(void * ctx_cookies) ; + + +static struct __optkit_memsb_t * +__update_context_cookies(struct __optkit_memsb_t * ctx_cookies, int partindex , int iomode_flags) ; + +static struct __optkit_memsb_t * +wupdate(struct __optkit_memsb_t * ctx_cookies, int partindex); +static struct __optkit_memsb_t * +rupdate(struct __optkit_memsb_t * ctx_cookies, int partindex); + +size_t +optkit_wat(int partion , const char * fmt , ...); + +size_t +optkit_rat(int partition); + + +#endif //! _SYS_TYPE_OPTKIT_CS_HLP diff --git a/lib/bits/types/optkit_t.h b/lib/bits/types/optkit_t.h new file mode 100644 index 0000000..5f6970e --- /dev/null +++ b/lib/bits/types/optkit_t.h @@ -0,0 +1,50 @@ +/*SPDX-License-Identifier: GPL-3.0 */ + +#if !defined(_SYS_TYPES_OPTKIT_T_H) +#define _SYS_TYPES_OPTKIT_T_H 1 + +#include +#include +#include + +#if defined(__has_feature) +# if !__has_feature(nullability) +# define _Nonnull +# define _Nullable +# endif +#else +# define _Nonnull +# define _Nullable +#endif + +typedef struct __base_optkit_t { + struct option _lopt; + const char * _description; +} base_optkit_t ; + +typedef struct __optkit_t { + struct __base_optkit_t * _optkit_base ; + char * _sysnosys ; + char * _footer ; + union { + struct __optkit_meta_t *_optkit_mcollect ; + }; + +} optkit_t ; + +typedef struct option gopt_t ; + +typedef struct __optkit_meta_t { + gopt_t * _ds_goptl; + char *_shortopts ; + unsigned int _max_entries ; +}optkit_meta_t; + + +typedef struct __optkit_recbuf_t { + FILE * _bmrec ; + char *_bptr ; + size_t _bytes; +} optkit_recbuf_t; + +#endif diff --git a/lib/meson.build b/lib/meson.build new file mode 100644 index 0000000..4c1fa41 --- /dev/null +++ b/lib/meson.build @@ -0,0 +1,8 @@ + +optkit_includes=['bits/types'] +optkit_sources=[ +'optkit_base.c', +'optkit_cs_help.c' + ] + +optkit_so = both_libraries('optkit' , optkit_sources , include_directories: optkit_includes) diff --git a/lib/optkit.h b/lib/optkit.h new file mode 100644 index 0000000..38e1f6b --- /dev/null +++ b/lib/optkit.h @@ -0,0 +1,6 @@ +//SPDX-License-Identifier: GPL-3.0 + +#if !defined(__optkit_h) +#define __optkit_h +#include "optkit_base.h" +#endif diff --git a/lib/optkit.o b/lib/optkit.o new file mode 100644 index 0000000..6c43937 Binary files /dev/null and b/lib/optkit.o differ diff --git a/lib/optkit_base.c b/lib/optkit_base.c new file mode 100644 index 0000000..c064463 --- /dev/null +++ b/lib/optkit_base.c @@ -0,0 +1,59 @@ +/*SPDX-License-Identifier: GPL-3.0 */ + +/* TODO: + * - [] Build the helper + * + */ + +#define _GNU_SOURCE +#include "optkit_base.h" +#include +#include +#include + +char * optkit_pbn =(char *) 00 ; +optkit_meta_t mopt = {0} ; +optkit_recbuf_t helper_record= {0} ; + + +char * optkit_get_basename(char *const * argv) +{ + int jmp_idx = 0 ; + jmp_idx ^=HAS_RPATH_SYMB(*argv) ; + + return optkit_copy_bn(*argv , jmp_idx) ; +} + + +void optkit_parse(base_optkit_t *restrict options, char * const *av) +{ + + if (!init_memstream_buffer_cookies()) + { + fprintf(stderr , "fail to initialize memory stream buffer \012") ; + return ; + } + optkit_pbn = optkit_get_basename(av) ; + mopt._max_entries = __optkit_get_entries(options) ; + mopt._ds_goptl= optkit_extract_option(options , &mopt); + + if(!mopt._ds_goptl){ + optkit_err(optkit_extract_option) ; + return ; + } + //optkit_show_usage() ; + + optkit_dump() ; + return ; +} + +void optkit_show_usage(void) +{ + setvbuf(stdout , 00 , _IONBF , 0); + printf("%s", helper_record._bptr); + +} + + + + diff --git a/lib/optkit_base.h b/lib/optkit_base.h new file mode 100644 index 0000000..d64e21c --- /dev/null +++ b/lib/optkit_base.h @@ -0,0 +1,185 @@ +//SPDX-License-Identifier: GPL-3.0 + +#if !defined(__optkit__base_h) +#define __optkit_base_h + +#define _GNU_SOURCE + +#include "optkit_macro.h" +#include "optkit_t.h" +#include "optkit_cs_help.h" +#include +#include +#include +#include +#include +#include +#include + + +//! just one record is enougth ... +extern optkit_recbuf_t helper_record ; /*Record helper in memory buffer*/ +extern optkit_meta_t mopt ; +extern char * optkit_pbn ; + + +//! Or use strlen +#define chrlen(str) __get_amount_nsize(str , 0) +static inline size_t __get_amount_nsize(const char * basename , int index) +{ + size_t size = strlen(basename) ; + if(0 < index) + size-=index ; + + return size; +} + +static inline char * optkit_copy_bn(const char * basename , int index) +{ + ssize_t bytes_copy = 0 ; + bytes_copy = __get_amount_nsize(basename , index) ; + char *s = (char *)00 , + *bn_addr = (char*)00 ; + + optkit_pbn = calloc(sizeof(char) , bytes_copy); + if(!optkit_pbn) + return (void *)00 ; + + index += (index > 0) ; + s = strrchr((basename+index) , '/'); + if(s){ + index +=(s-(basename+index))+1; + bytes_copy-=index-1; + } + + //!TODO::NOTE: release optkit_pbn ; + return memcpy(optkit_pbn ,(basename+index), bytes_copy); +} + +/* retrieve the number of flag given and also the max size of + * need for short options + * */ +static inline size_t __optkit_get_entries(base_optkit_t * options) +{ + size_t entry = ~0 , + shopt_len = 0; + + while( (options+(++entry))->_lopt.name ) + shopt_len+=-~(options+(entry))->_lopt.has_arg; + + return (entry<<8|shopt_len); +} +//! build pattern for shortoption like : "xxx:x::" +static inline void __optkit_build_sopt(char * _Nonnull sopt , const gopt_t lopt) +{ + + static int idx = 0 ; + char *args =00 , *s = 00; + asprintf(&args , "%c?%is", *lopt.name, lopt.has_arg); + + s = strchr(args , '?') ; *s='%'; + sprintf((sopt+idx) , args, (0 < lopt.has_arg ? ":" :"")); + + s = strchr(sopt , 0x20); + if(s) + *s=':' ; + + idx = strlen(sopt) ; + free(args) ; + +} + +static inline int __optkit_init_bmrec(void) +{ + + + /* + if(helper_record._bmrec) return 0; + helper_record._bmrec = open_memstream(&helper_record._bptr , &helper_record._bytes); + + return (helper_record._bmrec) ? 0 : ~0 ; + */ + return 0 ; +} + +static inline size_t __optkit_breffing_usage(int argreq) +{ + char usage_brief[][0x64] = OPTKIT_USAGE_CLASSIFICATIONS ; + //optkit_write_at(USAGE_SECTION,"Usage :%s %s",optkit_pbn , *(argreq + usage_brief)) ; + return 0 ; +} +/* Record options in memory stream buffer */ +static inline size_t __optkit_record_helper(base_optkit_t * option) +{ + static size_t argument_requested=3, + help_caddr=0, + stat = 0; + char *s = 0 ; + if(argument_requested){ + argument_requested^= !(option->_lopt.has_arg^stat) ? option->_lopt.has_arg: stat; + if(stat != argument_requested) + stat = argument_requested ; + } + + asprintf(&s ,HLPFMT , option->_lopt.val ,option->_lopt.name, option->_description); + + //!NOTE : Write at HELPER section see definition of coord in "bits/type/optkit_help_cs.h" + optkit_wat(HELPER_SECTION , "%s" , s ); + + return argument_requested ; +} + +static inline gopt_t * optkit_extract_option(base_optkit_t* options , + optkit_meta_t *__restrict__ meta_option) +{ + unsigned int entries = 1 , + shopt_size =entries, + idx =~0 , argn =0 ; + + entries+= (meta_option->_max_entries >> 8) ; + shopt_size = meta_option->_max_entries & 0xff ; + + gopt_t * super_opt = (gopt_t*) 00 ; + super_opt = malloc(sizeof(gopt_t) * entries); + if(!super_opt) + return (void *) 00 ; + + meta_option->_shortopts = calloc(sizeof(char) , shopt_size) ; + if(!meta_option->_shortopts) + { + free(super_opt) ; + return (void *) 00 ; + } + + while(++idx < entries){ + memcpy((super_opt+idx) ,&(options+idx)->_lopt, sizeof(gopt_t)); + + /*!collect and build short options */ + base_optkit_t * single_opt = (options+idx) ; + if(!single_opt->_lopt.name) + continue ; + + __optkit_build_sopt(meta_option->_shortopts, single_opt->_lopt); + argn =__optkit_record_helper(single_opt) ; + } + + + __optkit_breffing_usage(argn) ; + assert(!strlen(meta_option->_shortopts) ^shopt_size); + + return super_opt ; +} + +static inline size_t optkit_dump(void) +{ + + size_t rb = optkit_rat(HELPER_SECTION) ; + + return rb ; +} + +extern char * optkit_get_basename(char *const * argument_vector) ; +extern void optkit_show_usage(void) ; +extern void optkit_parse(base_optkit_t * __restrict__ _Nonnull options , char *const *av) ; + +#endif diff --git a/lib/optkit_cs_help.c b/lib/optkit_cs_help.c new file mode 100644 index 0000000..cf24eaa --- /dev/null +++ b/lib/optkit_cs_help.c @@ -0,0 +1,181 @@ +//!SPDX-License-Identifier:GPL-3.0 + +#include "optkit_cs_help.h" +#include +#include + + + + +//!NOTE::Warning: THIS ! is a shared resources between ios stream operation +//! : on memery buffer. + +FILE * optkit_stream = (FILE *) 00 ; +struct __optkit_memsb_t ctx_cookies = {00 , { + _USAGE_SECTION, + _SYNOPSYS_SECTION, + _HELPER_SECTION, + _FOOTER_SECTION +}, + USAGE_SECTION}; + +cookie_io_functions_t hooks = { + .write = iomem_write, + .read = iomem_read, + .close = (void *) 00, + .seek = (void *) 00 + /*.read= iomem_read, + *.close=iomem_close, + *.seek =iomem_seek + */ + }; + + +//init_memstream_buffer_cookies(int user_lock_permission) +struct __optkit_memsb_t * +init_memstream_buffer_cookies(void) +{ + ctx_cookies._msbio_cookies= __setup_membuff_cookies() ; + if(!ctx_cookies._msbio_cookies) + return 00 ; + + optkit_stream = fopencookie(&ctx_cookies , "w+" , hooks) ; + + if(!optkit_stream) + { + free(ctx_cookies._msbio_cookies), ctx_cookies._msbio_cookies=00; + return 00 ; + } + + setvbuf(optkit_stream , 00 , _IONBF ,0 ) ; + + return &ctx_cookies ; +} + +ssize_t +iomem_write(void * ctx_cookies , const char * user_buffer , size_t wbytes) +{ + + struct __optkit_memsb_t *new_ctx_cookies =(struct __optkit_memsb_t *) ctx_cookies ; + struct __membuff_cookies_t * mbios = new_ctx_cookies->_msbio_cookies ; + struct __page_io_location_t * waddr_offt = 00 ; + + size_t global_offset = mbios->_offset + wbytes , + local_offset = 0 ; + unsigned int partition_idx = new_ctx_cookies->_partition_index ; + char *buffer_expension = 00 ; + + /* + * Expand the page buffer when the global offset reach + * the end of buffer. by doubling the size of _page_allocation + * */ + if((global_offset &~(mbios->_page_allocation-1))){ + if(!__expand_buffer(new_ctx_cookies)) + return -ENOMEM; + } + + waddr_offt = __get_partition_location_address(new_ctx_cookies , _IOM_WRITE); + + local_offset = write_at(waddr_offt , user_buffer , wbytes) ; + if(-1 == local_offset) + return -EOVERFLOW ; + + size_t bwriten = (local_offset >> 0x10) , + loffset = (local_offset & 0xffff) ; + + //update the local offset of the current partion index + //new_ctx_cookies->_scope_interval[partition_idx]._coffset+=local_offset ; + new_ctx_cookies->_scope_interval[partition_idx]._coffset= loffset ; + + + //!update the global offset + if(mbios->_offset < loffset ){ + mbios->_offset = mbios->_checkpoint= loffset ; + } + + free(waddr_offt) , waddr_offt =0 ; + return bwriten ; +} + +ssize_t iomem_read(void * ctx_cookies , char * ubuff , size_t rbytes) +{ + + struct __optkit_memsb_t * new_ctx_cookies = (struct __optkit_memsb_t*) ctx_cookies ; + struct __section_t *secpart = (new_ctx_cookies->_scope_interval+new_ctx_cookies->_partition_index); + struct __page_io_location_t * raddr_offt = 00; + size_t rb = 0 ; + + raddr_offt = __get_partition_location_address(new_ctx_cookies , _IOM_READ); + rb = read_at(raddr_offt , ubuff , rbytes); + + memcpy(secpart, &raddr_offt->_chunck_part , sizeof(struct __section_t)) ; + + free(raddr_offt) ,raddr_offt=00; + + //!NOTE : The cursor offset is not updated !WARNING! + + return rb ; +} + +static struct __optkit_memsb_t * +__update_context_cookies(struct __optkit_memsb_t * ctxc , int partition_index , int iom_flags) +{ + ctxc->_partition_index = partition_index; + + if(iom_flags & _IOM_READ){ + //in read mode ; + struct __section_t p = ctxc->_scope_interval[partition_index] ; + + //!NO saved: because the read mode read on block at once ; + p._coffset = 0 ; + memcpy((ctxc->_scope_interval+partition_index) , &p , sizeof(p)) ; + } + + return ctxc ; +} + +struct __optkit_memsb_t * wupdate(struct __optkit_memsb_t * ctx , int partition_idx) +{ + return __update_context_cookies(ctx , partition_idx , _IOM_WRITE) ; +} +struct __optkit_memsb_t * rupdate(struct __optkit_memsb_t * ctx , int partition_idx ) +{ + return __update_context_cookies(ctx , partition_idx , _IOM_READ) ; +} + + +size_t optkit_wat(int partition , const char * fmt , ...) +{ + size_t bwriten=0 ; + va_list ap ; + va_start(ap , fmt); + + //!Update the global cookies context stream ... + ctx_cookies = *wupdate(&ctx_cookies , partition) ; + + bwriten = vfprintf(optkit_stream , fmt , ap) ; + va_end(ap) ; + + return bwriten ; +} + +size_t optkit_rat(int partition) +{ + struct __section_t section = ctx_cookies._scope_interval[partition]; + size_t breaded = section._coffset; + char * buffer = 00 ; + //!Update the global cookies context stream ... + ctx_cookies = *rupdate(&ctx_cookies , partition); + + buffer = malloc(breaded) ; + if(!buffer) + return ~0 ; + + breaded ^=fread(buffer ,1,breaded,optkit_stream) ; + + printf("-->> %s\012",buffer ) ; + + free(buffer) ; + + return breaded; +} diff --git a/lib/optkit_macro.h b/lib/optkit_macro.h new file mode 100644 index 0000000..c558f8e --- /dev/null +++ b/lib/optkit_macro.h @@ -0,0 +1,45 @@ +//!SPDX-License-Identifier: GPL-3.0 + +#if !defined(_M_optkit) +#define _M_optkit + + +#define HAS_RPATH_SYMB(__pn) !(0x2e2f ^ ((*__pn)<<8|(*__pn+1))) +#define __sterr__ "%s" , strerror(*__errno_location()) + +#define optkit_err(f) \ + do{perror(#f), fprintf(stderr , __sterr__);}while(0) + +#define HLPFMT " -%c, --%s \011\011%s\012" + +#define optkit_write_at(offset_region , ... ) \ + sprintf(offset_region , __VA_ARGS__) + +#define OPTKIT_USAGE_CLASSIFICATIONS {\ + "[OPTION]... [ARGS]...",\ + "[OPTION]...",\ + "[ARGS]...",\ +} + + +#define optkitnull {{00 ,0 ,00,0}, 00} +#define __shopt(__loption) (__loption[0] & 0xff) + +/*NOTE: Suggestion + * #define optkit_begin(identifer , sysnopys , footer) + **/ +#define optkit_begin(identier) \ + base_optkit_t identier[] = { + +#define noarg(__loption , __description) \ + {{__loption ,0 ,00, __shopt(__loption)} , __description} + +#define rearg(__loption , __description) \ + {{__loption ,1 ,00 , __shopt(__loption) } , __description} + +#define oparg(__loption , __description) \ + {{__loption ,2 ,00 , __shopt(__loption)} , __description} + +#define optkit_end optkitnull } ; + +#endif//_M_optkit diff --git a/meson.build b/meson.build index 7f35624..3cc3956 100644 --- a/meson.build +++ b/meson.build @@ -23,7 +23,7 @@ subdir('lib') executable( 'test_optkit', 'test.c', - include_directories:['lib'], + include_directories:['lib' , 'lib/bits/types'], link_with:optkit_so ) diff --git a/test.c b/test.c index ce537c9..23bd39a 100644 --- a/test.c +++ b/test.c @@ -19,7 +19,6 @@ int main(int ac , char * const *av) printf(" %s \012" , basename) ; - puts("this is a test") ; return 0 ; }