home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d523 / bmake.lha / BMake / source.lzh / param.c < prev    next >
C/C++ Source or Header  |  1991-07-28  |  4KB  |  198 lines

  1. /*    param.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <ctype.h>
  7. #include <scdir.h>
  8.  
  9. #include <clib/exec_protos.h>
  10.  
  11. #include "make.h"
  12.  
  13. struct parameters Param = {
  14.     {
  15.     (struct Node *)&Param.filelist.lh_Tail,    /* lh_Head */
  16.     (struct Node *)NULL,                    /* lh_Tail */
  17.     (struct Node *)&Param.filelist.lh_Head,    /* lh_TailPred */
  18.     (UBYTE)NT_USER,
  19.     (UBYTE)0
  20.     },    /* filelist */
  21.     1024, /* MaxLine */
  22.     1,    /* verbosity */
  23.     0,    /* log */
  24.     0,    /* debug */
  25.     0,    /* touch mode */
  26.     0,    /* all mode */
  27.     0,    /* no builtins */
  28.     0,    /* pretend mode */
  29.     0,    /* print database */
  30.     NULL /* makefile */
  31. };
  32.  
  33. const char *usage_string =
  34.     "Usage: make -abdhnpt -v# +m# -fmakefile var=value wildcards...";
  35.  
  36. const char *help_lines[] = {
  37.     "-a\t"    "make all dependents regardless of modification time",
  38.     "-b\t"    "do not use builtin rules",
  39. #if DEBUG
  40.     "-d\t"    "debug mode (enable printf)",
  41. #endif
  42.     "+m#\t"    "sets the maximum line length to # (minimum = 1024)",
  43.     "-n\t"    "prints commands to be executed, but do not execute them",
  44.     "-p\t"    "prints the database, but do not run",
  45.     "-t\t"    "make targets up to date by touching (commands not executed)",
  46.     "-v#\t"    "set verbose level to #; 0=silent",
  47.     "-fmakefile\t"    "specify the makefile to run",
  48.     "var=value\t"    "assigns the value to a variable",
  49.     "wildcards\t"    "specify targets to make",
  50.     NULL
  51. };
  52.  
  53. void
  54. usage( void )
  55. {
  56.     printf( "%s %s\n", version_string+7, verdate_string );
  57.     puts( usage_string );
  58. }
  59.  
  60. int
  61. help( void )
  62. {
  63.     int i = 0;
  64.     char *helpline;
  65.  
  66.     usage();
  67.     puts( copyright_string );
  68.     puts( "" );
  69.     while( helpline = help_lines[ i++ ] ) {
  70.         puts( helpline );
  71.     }
  72.  
  73.     return( 0 );
  74. }
  75.  
  76. static int
  77. expand_wildcard( const char *wild )
  78. {
  79.     struct string_node *sptr = NULL;
  80.     char *fn;
  81.  
  82.     while( fn = scdir( wild )) {
  83.         if( sptr = new_snode( fn )) {
  84.             AddTail( &Param.filelist, &sptr->node );
  85.         }
  86.         else return( 1 );
  87.     }
  88.     return( 0 );
  89. }
  90.  
  91. int
  92. parse_parameters( int argc, char *argv[] )
  93. {
  94.     char *arg;
  95.  
  96.     for( int i = 1; i < argc; i++ ) {
  97.         if( !(arg = argv[ i ] )) break;
  98.         switch( *arg ) {
  99.         case '-':
  100.             switch( *++arg ) {
  101.             default:
  102.             case '?':
  103.                 usage();
  104.                 goto death;
  105.             case 'V':
  106.                 puts( version_string + 7);
  107.                 puts( copyright_string );
  108.                 goto death;
  109.             case 'a':
  110.                 Param.all_mode = 1;
  111.                 break;
  112.             case 'b':
  113.                 Param.no_builtins = 1;
  114.                 break;
  115.             case 'd':
  116. #if DEBUG
  117.                 Param.debug = 1;
  118. #else
  119.                 puts( "Debugging not available!" );
  120. #endif
  121.                 break;
  122.             case 'f':
  123.                 if( Param.makefile ) {
  124.                     puts( "only one -f argument is allowed" );
  125.                     goto death;
  126.                 }
  127.                 else {
  128.                     if( *++arg ) Param.makefile = strdup( arg );
  129.                     else if( ++i < argc ) {
  130.                         arg = argv[ i ];
  131.                         if( *arg ) Param.makefile = strdup( arg );
  132.                     }
  133.                 }
  134.                 break;
  135.             case 'h':
  136.                 help();
  137.                 goto death;
  138.             case 'n':
  139.                 Param.pretend_mode = 1;
  140.                 break;
  141.             case 'p':
  142.                 Param.print_database = 1;
  143.             case 't':
  144.                 Param.touch_mode = 1;
  145.                 break;
  146.             case 'v':
  147.                 Param.verbosity = atoi( ++arg );
  148.                 break;
  149.             }
  150.             break;
  151.         case '+':
  152.             switch( *++arg ) {
  153.             case 'l':
  154.                 Param.log = (Param.log) ? 0 : 1;
  155.                 break;
  156.             case 'm':
  157.                 {
  158.                     int maxline = atoi( ++arg );
  159.                     Param.MaxLine = max( Param.MaxLine, maxline);
  160.                 }
  161.             }
  162.             break;
  163.         case '?':
  164.             usage();
  165.             goto death;
  166.         default: /* a filename */
  167.             if( scdir( arg ) ) {
  168.                 scdir_abort();
  169.                 if( expand_wildcard( arg )) goto death;
  170.             }
  171.             else {
  172.                 struct string_node *sptr = new_snode( arg );
  173.                 if( sptr ) {
  174.                     AddTail( &Param.filelist, &sptr->node );
  175.                 }
  176.             }
  177.             break;
  178.         }
  179.     }
  180.     if( !Param.makefile ) Param.makefile = strdup( "Makefile" );
  181.  
  182.     return( 0 );
  183. death:
  184.     return( 1 );
  185. }
  186.  
  187. void
  188. delete_params( void )
  189. {
  190.     if( Param.makefile ) {
  191.         free( Param.makefile );
  192.         Param.makefile = NULL;
  193.     }
  194.     /* free Param */
  195.     delete_slist( &Param.filelist );
  196.     NewList( &Param.filelist );
  197. }
  198.