Advertisement
Guest User

gopt.c

a guest
Dec 31st, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.61 KB | None | 0 0
  1. /* gopt.c version 8.1: tom.viza@gmail.com PUBLIC DOMAIN 2003-8 */
  2. /*
  3. I, Tom Vajzovic, am the author of this software and its documentation and
  4. permanently abandon all copyright and other intellectual property rights in
  5. them, including the right to be identified as the author.
  6.  
  7. I am fairly certain that this software does what the documentation says it
  8. does, but I cannot guarantee that it does, or that it does what you think it
  9. should, and I cannot guarantee that it will not have undesirable side effects.
  10.  
  11. You are free to use, modify and distribute this software as you please, but
  12. you do so at your own risk.  If you remove or hide this warning then you are
  13. responsible for any problems encountered by people that you make the software
  14. available to.
  15.  
  16. Before modifying or distributing this software I ask that you would please
  17. read http://www.purposeful.co.uk/tfl/
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "gopt.h"
  24.  
  25. #ifdef USE_SYSEXITS
  26. #include <sysexits.h>
  27. #else
  28. #define EX_OSERR EXIT_FAILURE
  29. #define EX_USAGE EXIT_FAILURE
  30. #endif
  31.  
  32. struct opt_spec_s {
  33.   int key;
  34.   int flags;
  35.   const char *shorts;
  36.   const char* const *longs;
  37. };
  38. typedef struct opt_spec_s opt_spec_t;
  39.  
  40. struct opt_s {
  41.   int key;
  42.   const char *arg;
  43. };
  44. typedef struct opt_s opt_t;
  45.  
  46. void *gopt_sort( int *argc, const char **argv, const void *opt_specs ){
  47.   void *opts;
  48.   {{{
  49.     const char* const *arg_p= argv + 1;
  50.     size_t opt_count= 1;
  51.     for( ; *arg_p; ++arg_p )
  52.       if( '-' == (*arg_p)[0] && (*arg_p)[1] )
  53.         if( '-' == (*arg_p)[1] )
  54.           if( (*arg_p)[2] )
  55.             ++opt_count;
  56.           else
  57.             break;
  58.         else {
  59.           const opt_spec_t *opt_spec_p= opt_specs;
  60.           for( ; opt_spec_p-> key; ++opt_spec_p )
  61.             if( strchr( opt_spec_p-> shorts, (*arg_p)[1] )){
  62.               opt_count+= opt_spec_p-> flags & GOPT_ARG ? 1 : strlen( (*arg_p) + 1 );
  63.               break;
  64.             }
  65.         }
  66.     opts= malloc( opt_count * sizeof(opt_t) );
  67.   }}}
  68.   {
  69.     const char **arg_p= argv + 1;
  70.     const char **next_operand= arg_p;
  71.     opt_t *next_option= opts;
  72.  
  73.     if( ! opts ){
  74.       perror( argv[0] );
  75.       exit( EX_OSERR );
  76.     }  
  77.     for( ; *arg_p; ++arg_p )
  78.       if( '-' == (*arg_p)[0] && (*arg_p)[1] )
  79.         if( '-' == (*arg_p)[1] )
  80.           if( (*arg_p)[2] )
  81.           {{{
  82.             const opt_spec_t *opt_spec_p= opt_specs;
  83.             const char* const *longs= opt_spec_p-> longs;
  84.             next_option-> key= 0;
  85.             while( *longs ){
  86.               const char *option_cp= (*arg_p) + 2;
  87.               const char *name_cp= *longs;
  88.               while( *option_cp && *option_cp == *name_cp ){
  89.                 ++option_cp;
  90.                 ++name_cp;
  91.               }
  92.               if( '=' == *option_cp || !*option_cp ){
  93.                 if( *name_cp ){
  94.                   if( next_option-> key ){
  95.                     fprintf( stderr, "%s: --%.*s: abbreviated option is ambiguous\n", argv[0], (int)( option_cp -( (*arg_p) + 2 )), (*arg_p) + 2 );
  96.                     free( opts );
  97.                     exit( EX_USAGE );
  98.                   }
  99.                   next_option-> key= opt_spec_p-> key;
  100.                 }
  101.                 else {
  102.                   next_option-> key= opt_spec_p-> key;
  103.                   goto found_long;
  104.                 }
  105.               }
  106.               if( !*++longs ){
  107.                 ++opt_spec_p;
  108.                 if( opt_spec_p-> key )
  109.                   longs= opt_spec_p-> longs;
  110.               }
  111.             }
  112.             if( ! next_option-> key ){
  113.               fprintf( stderr, "%s: --%.*s: unknown option\n", argv[0], (int)strcspn( (*arg_p) + 2, "=" ), (*arg_p) + 2 );
  114.               free( opts );
  115.               exit( EX_USAGE );              
  116.             }
  117.             for( opt_spec_p= opt_specs; opt_spec_p-> key != next_option-> key; ++opt_spec_p );
  118.             found_long:
  119.            
  120.             if( !( opt_spec_p-> flags & GOPT_REPEAT )){
  121.               const opt_t *opt_p= opts;
  122.               for( ; opt_p != next_option; ++opt_p )
  123.                 if( opt_p-> key == opt_spec_p-> key ){
  124.                   fprintf( stderr, "%s: --%.*s: option may not be repeated (in any long or short form)\n", argv[0], (int)strcspn( (*arg_p) + 2, "=" ), (*arg_p) + 2 );
  125.                   free( opts );
  126.                   exit( EX_USAGE );
  127.                 }
  128.             }
  129.             if( opt_spec_p-> flags & GOPT_ARG ){
  130.               next_option-> arg= strchr( (*arg_p) + 2, '=' ) + 1;
  131.               if( (char*)0 + 1 == next_option-> arg ){
  132.                 ++arg_p;
  133.                 if( !*arg_p || '-' == (*arg_p)[0] && (*arg_p)[1] ){
  134.                   fprintf( stderr, "%s: --%s: option requires an option argument\n", argv[0], (*(arg_p-1)) + 2 );
  135.                   free( opts );
  136.                   exit( EX_USAGE );
  137.                 }
  138.                 next_option-> arg= *arg_p;
  139.               }
  140.             }
  141.             else {
  142.               if( strchr( (*arg_p) + 2, '=' )){
  143.                 fprintf( stderr, "%s: --%.*s: option may not take an option argument\n", argv[0], (int)strcspn( (*arg_p) + 2, "=" ), (*arg_p) + 2 );
  144.                 free( opts );
  145.                 exit( EX_USAGE );
  146.               }
  147.               next_option-> arg= NULL;
  148.             }
  149.             ++next_option;
  150.           }}}
  151.           else {
  152.             for( ++arg_p; *arg_p; ++arg_p )
  153.               *next_operand++= *arg_p;
  154.             break;
  155.           }
  156.         else
  157.         {{{
  158.           const char *short_opt= (*arg_p) + 1;
  159.           for( ;*short_opt; ++short_opt ){
  160.             const opt_spec_t *opt_spec_p= opt_specs;
  161.            
  162.             for( ; opt_spec_p-> key; ++opt_spec_p )
  163.               if( strchr( opt_spec_p-> shorts, *short_opt )){
  164.                 if( !( opt_spec_p-> flags & GOPT_REPEAT )){
  165.                   const opt_t *opt_p= opts;
  166.                   for( ; opt_p != next_option; ++opt_p )
  167.                     if( opt_p-> key == opt_spec_p-> key ){
  168.                       fprintf( stderr, "%s: -%c: option may not be repeated (in any long or short form)\n", argv[0], *short_opt );
  169.                       free( opts );
  170.                       exit( EX_USAGE );
  171.                     }
  172.                 }
  173.                 next_option-> key= opt_spec_p-> key;
  174.  
  175.                 if( opt_spec_p-> flags & GOPT_ARG ){
  176.                   if( short_opt[1] )
  177.                     next_option-> arg= short_opt + 1;
  178.                  
  179.                   else {
  180.                     ++arg_p;
  181.                     if( !*arg_p || '-' == (*arg_p)[0] && (*arg_p)[1] ){
  182.                       fprintf( stderr, "%s: -%c: option requires an option argument\n", argv[0], *short_opt );
  183.                       free( opts );
  184.                       exit( EX_USAGE );
  185.                     }
  186.                     next_option-> arg= *arg_p;
  187.                   }
  188.                   ++next_option;
  189.                   goto break_2;
  190.                 }
  191.                 next_option-> arg= NULL;
  192.                 ++next_option;
  193.                 goto continue_2;
  194.               }
  195.             fprintf( stderr, "%s: -%c: unknown option\n", argv[0], *short_opt );
  196.             free( opts );
  197.             exit( EX_USAGE );
  198.             continue_2: 0;
  199.           }
  200.           break_2: 0;
  201.         }}}
  202.       else
  203.         *next_operand++= *arg_p;
  204.  
  205.     next_option-> key= 0;
  206.     *next_operand= NULL;
  207.     *argc= next_operand - argv;
  208.   }
  209.   return opts;
  210. }
  211.  
  212. size_t gopt( const void *vptr_opts, int key ){
  213.   const opt_t *opts= vptr_opts;
  214.   size_t count= 0;
  215.   for( ; opts-> key; ++opts )
  216.     count+= opts-> key == key;
  217.  
  218.   return count;
  219. }
  220.  
  221. size_t gopt_arg( const void *vptr_opts, int key, const char **arg ){
  222.   const opt_t *opts= vptr_opts;
  223.   size_t count= 0;
  224.  
  225.   for( ; opts-> key; ++opts )
  226.     if( opts-> key == key ){
  227.       if( ! count )
  228.         *arg= opts-> arg;
  229.       ++count;
  230.     }
  231.   return count;
  232. }
  233.  
  234. const char *gopt_arg_i( const void *vptr_opts, int key, size_t i ){
  235.   const opt_t *opts= vptr_opts;
  236.  
  237.   for( ; opts-> key; ++opts )
  238.     if( opts-> key == key ){
  239.       if( ! i )
  240.         return opts-> arg;      
  241.       --i;
  242.     }
  243.   return NULL;
  244. }
  245.  
  246. size_t gopt_args( const void *vptr_opts, int key, const char **args, size_t args_len ){
  247.   const char **args_stop= args + args_len;
  248.   const char **args_ptr= args;
  249.   const opt_t *opts= vptr_opts;
  250.  
  251.   for( ; opts-> key; ++opts )
  252.     if( opts-> key == key ){
  253.       if( args_stop == args_ptr )
  254.         return args_len + gopt( opts, key );
  255.      
  256.       *args_ptr++= opts-> arg;
  257.     }
  258.   if( args_stop != args_ptr )
  259.     *args_ptr= NULL;
  260.   return args_ptr - args;
  261. }
  262.  
  263. void gopt_free( void *vptr_opts ){
  264.   free( vptr_opts );
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement