home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MAWK113.ZIP / mawk113 / init.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  7KB  |  359 lines

  1.  
  2. /********************************************
  3. init.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log: init.c,v $
  15.  * Revision 5.4  1992/12/24  01:58:19  mike
  16.  * 1.1.2d changes for MsDOS
  17.  *
  18.  * Revision 5.3  1992/07/10  16:17:10  brennan
  19.  * MsDOS: remove NO_BINMODE macro
  20.  *
  21.  * Revision 5.2  1992/01/09  08:46:14  brennan
  22.  * small change for MSC
  23.  *
  24.  * Revision 5.1  91/12/05  07:56:07  brennan
  25.  * 1.1 pre-release
  26.  * 
  27. */
  28.  
  29.  
  30. /* init.c */
  31. #include "mawk.h"
  32. #include "code.h"
  33. #include "memory.h"
  34. #include "symtype.h"
  35. #include "init.h"
  36. #include "bi_vars.h"
  37. #include "field.h"
  38.  
  39. #ifdef THINK_C
  40. #include <console.h>
  41. #endif
  42.  
  43. #if MSDOS
  44. #include <fcntl.h>
  45. #endif
  46.  
  47. static void PROTO( process_cmdline , (int, char **) ) ;
  48. static void PROTO( set_ARGV, (int, char **, int)) ;
  49. static void PROTO( bad_option, (char *)) ;
  50.  
  51. extern  void PROTO( print_version, (void) ) ;
  52. extern  int  PROTO( is_cmdline_assign, (char*)) ;
  53.  
  54. #if  MSDOS
  55. void PROTO(stdout_init,(void)) ;
  56. #if  HAVE_REARGV
  57. void PROTO(reargv, (int*,char***)) ;
  58. #endif
  59. #endif
  60.  
  61. char *progname ;
  62.  
  63. void initialize(argc, argv)
  64.   int argc ; char **argv ;
  65. {
  66.   SET_PROGNAME() ;
  67.  
  68.   bi_vars_init() ; /* load the builtin variables */
  69.   bi_funct_init() ; /* load the builtin functions */
  70.   kw_init() ; /* load the keywords */
  71.   field_init() ; 
  72.  
  73. #ifdef THINK_C
  74.   fputc('\n',stderr);   /* (causes Think C console window to appear) */
  75.   SetWTitle( FrontWindow(), "\pMacMAWK" );
  76.   argc = ccommand(&argv);
  77. #endif 
  78.  
  79. #if   MSDOS  
  80.   { char *p = getenv("MAWKBINMODE") ;
  81.  
  82.     if ( p )  set_binmode( atoi(p) ) ;
  83.   }
  84. #endif
  85.  
  86.  
  87.   process_cmdline(argc, argv)  ;   
  88.  
  89.   code_init() ;
  90.   fpe_init() ;
  91.   set_stderr() ;
  92.  
  93. #if  MSDOS
  94.   stdout_init() ;
  95. #endif
  96. }
  97.  
  98. void  compile_cleanup()
  99. /* program has parsed OK, free some memory
  100.    we don't need anymore */
  101. {
  102.    scan_cleanup() ;
  103.    code_cleanup() ;
  104. }
  105.  
  106. int  dump_code ;  /* if on dump internal code */
  107. int  posix_space_flag ;
  108.  
  109. #ifdef   DEBUG
  110. int  dump_RE   ;  /* if on dump compiled REs  */
  111. #endif
  112.  
  113.  
  114. static void bad_option(s)
  115.   char *s ;
  116. { errmsg(0, "not an option: %s", s) ; mawk_exit(1) ; }
  117.  
  118.  
  119. static void  process_cmdline(argc, argv)
  120.   int argc ;
  121.   char **argv ;
  122. {
  123.   int i , nextarg ;
  124.   char *optarg ;
  125.   PFILE dummy ;  /* starts linked list of filenames */
  126.   PFILE *tail = &dummy ;
  127.  
  128.   for( i = 1 ; i < argc && argv[i][0] == '-'  ; i = nextarg )
  129.   {
  130.     if ( argv[i][1] == 0 )  /* -  alone */
  131.     {
  132.       if ( ! pfile_name ) 
  133.       { errmsg(0, "no program") ; exit(1) ; }
  134.       break ; /* the for loop */
  135.     }
  136.     /* safe to look at argv[i][2] */
  137.  
  138.     if ( argv[i][2] == 0 )
  139.     {
  140.       if ( i == argc-1 && argv[i][1] != '-' )
  141.       { 
  142.         if ( strchr("WFvf", argv[i][1]) )
  143.         { errmsg(0, "option %s lacks argument", argv[i]) ;
  144.           mawk_exit(1) ;
  145.         }
  146.         bad_option(argv[i]) ;
  147.       }
  148.  
  149.       optarg = argv[i+1] ;
  150.       nextarg = i+2 ;
  151.     }
  152.     else /* argument glued to option */
  153.     {
  154.       optarg = &argv[i][2] ;
  155.       nextarg = i+1 ;
  156.     }
  157.  
  158.     switch( argv[i][1] )
  159.     {
  160.        case 'W' : 
  161.  
  162.         if ( optarg[0] >= 'a' && optarg[0] <= 'z')
  163.          optarg[0] += 'A' - 'a' ;
  164.             if ( optarg[0] == 'V' ) print_version() ;
  165.         else
  166.             if ( optarg[0] == 'D' )
  167.         { dump_code = 1 ;
  168. #if  SM_DOS
  169.           errmsg(0, "-W dump option unavailable in small model") ;
  170.           dump_code=0 ;
  171. #endif
  172.         }
  173.         else
  174.         if ( optarg[0] == 'S' )
  175.         {
  176.           char *p = strchr(optarg,'=') ;
  177.           int x = p ? atoi(p+1) : 0 ;
  178.  
  179.           if ( x > SPRINTF_SZ )
  180.           {
  181.         sprintf_buff = (char *) zmalloc(x) ;
  182.         sprintf_limit = sprintf_buff + x ;
  183.           }
  184.         }
  185. #if  MSDOS  
  186.         else
  187.         if ( optarg[0] == 'B' )
  188.         {
  189.           char *p = strchr(optarg,'=') ;
  190.           int x = p ? atoi(p+1) : 0 ;
  191.  
  192.           set_binmode(x) ;
  193.         }
  194. #endif
  195.         else
  196.         if ( optarg[0] == 'P' )
  197.         {
  198.           posix_space_flag = 1 ;
  199.         }
  200.         else
  201.           errmsg(0,"vacuous option: -W %s", optarg) ;
  202.           
  203.  
  204.             break ;
  205.  
  206.        case 'v' :  
  207.             if ( ! is_cmdline_assign(optarg) ) 
  208.             { errmsg(0 , "improper assignment: -v %s" , optarg) ;
  209.               exit(1) ;
  210.             }
  211.             break ;
  212.  
  213.        case 'F' :
  214.  
  215.             rm_escape(optarg) ; /* recognize escape sequences */
  216.             cell_destroy(FS) ;
  217.             FS->type = C_STRING ;
  218.             FS->ptr = (PTR) new_STRING(optarg) ;
  219.             cast_for_split( cellcpy(&fs_shadow, FS) ) ;
  220.             break ;
  221.  
  222.        case '-' :
  223.             if ( argv[i][2] != 0 )  bad_option(argv[i]) ;
  224.             i++ ;
  225.             goto  no_more_opts ;
  226.  
  227.        case 'f' :
  228.         /* first file goes in pfile_name ; any more go
  229.            on a list */
  230.             if ( ! pfile_name )  pfile_name = optarg ;
  231.             else 
  232.             { tail = tail->link = ZMALLOC(PFILE) ;
  233.               tail->fname = optarg ;
  234.             }
  235.             break ;
  236.  
  237.        default :  bad_option(argv[i]) ;
  238.  
  239.     }
  240.  
  241.     i = nextarg ;
  242.   }
  243.  
  244. no_more_opts:
  245.   
  246.   tail->link = (PFILE*) 0 ;
  247.   pfile_list = dummy.link ;
  248.  
  249.   if ( pfile_name )
  250.   {
  251.     set_ARGV(argc, argv, i) ;
  252.     scan_init( (char *) 0 ) ;
  253.   }
  254.   else /* program on command line */
  255.   {
  256. #if 0
  257. #if MSDOS && ! HAVE_REARGV  /* prompt for program */
  258.     set_ARGV(argc, argv, i) ;
  259.     emit_prompt() ;
  260.     pfile_name = "CON" ;
  261.     scan_init( (char *) 0 ) ;
  262. #else /* the real world */
  263. #endif
  264. #endif /* 0 */
  265.     if ( i == argc )
  266.     { errmsg(0, "no program") ; mawk_exit(1) ; }
  267.     set_ARGV(argc, argv, i+1) ;
  268.  
  269. #if  MSDOS && ! HAVE_REARGV  /* reversed quotes */
  270.     { char *p ;
  271.  
  272.       for( p = argv[i] ; *p ; p++ )
  273.      if ( *p == '\'' )  *p = '\"' ;
  274.     }
  275. #endif
  276.     scan_init( argv[i]) ;
  277. /* #endif  */
  278.   }
  279. }
  280.  
  281.  
  282. static void set_ARGV(argc, argv, i)
  283.   int argc ; char **argv ;
  284.   int i ; /* argv[i] = ARGV[1] */
  285. {
  286.   SYMTAB *st_p ;
  287.   CELL argi ;
  288.   register CELL *cp ;
  289.  
  290.   st_p = insert( "ARGV" ) ;
  291.   st_p->type = ST_ARRAY ;
  292.   Argv = st_p->stval.array = new_ARRAY() ;
  293.   argi.type = C_DOUBLE ;
  294.   argi.dval = 0.0 ;
  295.   cp = array_find( st_p->stval.array, &argi, CREATE) ;
  296.   cp->type = C_STRING ;
  297.   cp->ptr = (PTR) new_STRING( progname ) ;
  298.  
  299.   /* ARGV[0] is set, do the rest 
  300.      The type of ARGV[1] ... should be C_MBSTRN
  301.      because the user might enter numbers from the command line */
  302.  
  303.     for( argi.dval = 1.0 ; i < argc ; i++, argi.dval += 1.0 )
  304.     { 
  305.       cp = array_find( st_p->stval.array, &argi, CREATE) ;
  306.       cp->type = C_MBSTRN ;
  307.       cp->ptr = (PTR) new_STRING( argv[i] ) ;
  308.     }
  309.     ARGC->type = C_DOUBLE ;
  310.     ARGC->dval = argi.dval ;
  311. }
  312.  
  313. #if 0
  314. #if  MSDOS  &&  ! HAVE_REARGV
  315.  
  316. static void  emit_prompt()
  317. {  static char prompt[] = "mawk> " ;
  318.    int fd = open("CON", O_WRONLY, 0) ;
  319.  
  320.    (void) write(fd, prompt, strlen(prompt)) ;
  321.    (void) close(fd) ;
  322. }
  323. #endif
  324. #endif
  325.  
  326.  
  327. /*----- ENVIRON ----------*/
  328.  
  329. void load_environ( ENV )
  330.   ARRAY ENV ;
  331. {
  332.   CELL c ;
  333. #ifndef  MSDOS_MSC  /* MSC declares it near */
  334.   extern char **environ ;
  335. #endif
  336.   register char **p = environ ; /* walks environ */
  337.   char *s ; /* looks for the '=' */
  338.   CELL *cp ; /* pts at ENV[&c] */
  339.  
  340.   c.type = C_STRING ;
  341.  
  342.   while ( *p )
  343.   {
  344.     if ( s = strchr(*p, '=') ) /* shouldn't fail */
  345.     {
  346.       *s = 0 ;
  347.       c.ptr = (PTR) new_STRING(*p) ;
  348.       *s++ = '=' ;
  349.  
  350.       cp = array_find(ENV, &c, CREATE) ;
  351.       cp->type = C_MBSTRN ;
  352.       cp->ptr = (PTR) new_STRING(s) ;
  353.  
  354.       free_STRING(string(&c)) ;
  355.     }
  356.     p++ ;
  357.   }
  358. }
  359.