home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 11 / AUCD11B.iso / LANGUAGES / WraithSet / AwkStuff / MawkSrc / c / init < prev    next >
Encoding:
Text File  |  1999-11-28  |  8.7 KB  |  395 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 1.11  1995/08/20  17:35:21  mike
  16.  * include <stdlib.h> for MSC, needed for environ decl
  17.  *
  18.  * Revision 1.10  1995/06/09  22:51:50  mike
  19.  * silently exit(0) if no program
  20.  *
  21.  * Revision 1.9  1995/06/06  00:18:30  mike
  22.  * change mawk_exit(1) to mawk_exit(2)
  23.  *
  24.  * Revision 1.8  1994/12/14  14:40:34  mike
  25.  * -Wi option
  26.  *
  27.  * Revision 1.7  1994/12/11  22:43:20  mike
  28.  * don't assume **environ is writable
  29.  *
  30.  * Revision 1.6  1994/12/11  22:14:16  mike
  31.  * remove THINK_C #defines.  Not a political statement, just no indication
  32.  * that anyone ever used it.
  33.  *
  34.  * Revision 1.5  1994/10/08  19:15:45  mike
  35.  * remove SM_DOS
  36.  *
  37.  * Revision 1.4  1994/03/11  02:23:49  mike
  38.  * -We option
  39.  *
  40.  * Revision 1.3  1993/07/17  00:45:14  mike
  41.  * indent
  42.  *
  43.  * Revision 1.2  1993/07/04  12:52:00  mike
  44.  * start on autoconfig changes
  45.  *
  46.  * Revision 5.5  1993/01/07  02:50:33  mike
  47.  * relative vs absolute code
  48.  *
  49.  * Revision 5.4  1992/12/24  01:58:19  mike
  50.  * 1.1.2d changes for MsDOS
  51.  *
  52.  * Revision 5.3  1992/07/10  16:17:10  brennan
  53.  * MsDOS: remove NO_BINMODE macro
  54.  *
  55.  * Revision 5.2  1992/01/09  08:46:14  brennan
  56.  * small change for MSC
  57.  *
  58.  * Revision 5.1  91/12/05  07:56:07  brennan
  59.  * 1.1 pre-release
  60.  *
  61. */
  62.  
  63.  
  64. /* init.c */
  65. #include "mawk.h"
  66. #include "code.h"
  67. #include "memory.h"
  68. #include "symtype.h"
  69. #include "init.h"
  70. #include "bi_vars.h"
  71. #include "field.h"
  72.  
  73. #ifdef MSDOS
  74. #include <fcntl.h>
  75. #ifdef MSDOS_MSC
  76. #include <stdlib.h>
  77. #endif
  78. #endif
  79.  
  80. #ifdef RISCOS
  81. #define GCW_SEP '.'
  82. #else
  83. #define GCW_SEP '/'
  84. #endif
  85.  
  86. static void PROTO(process_cmdline, (int, char **)) ;
  87. static void PROTO(set_ARGV, (int, char **, int)) ;
  88. static void PROTO(bad_option, (char *)) ;
  89. static void PROTO(no_program, (void)) ;
  90.  
  91. extern void PROTO(print_version, (void)) ;
  92. extern int PROTO(is_cmdline_assign, (char *)) ;
  93.  
  94. #if  MSDOS
  95. void PROTO(stdout_init, (void)) ;
  96. #if  HAVE_REARGV
  97. void PROTO(reargv, (int *, char ***)) ;
  98. #endif
  99. #endif
  100.  
  101. char *progname ;
  102. short interactive_flag = 0 ;
  103.  
  104. #ifndef  SET_PROGNAME
  105. #define  SET_PROGNAME() \
  106.    {char *p = strrchr(argv[0],GCW_SEP) ;\
  107.     progname = p ? p+1 : argv[0] ; }
  108. #endif
  109.  
  110. void
  111. initialize(argc, argv)
  112. int argc ; char **argv ;
  113. {
  114.  
  115.    SET_PROGNAME() ;
  116.  
  117.    bi_vars_init() ;              /* load the builtin variables */
  118.    bi_funct_init() ;             /* load the builtin functions */
  119.    kw_init() ;                   /* load the keywords */
  120.    field_init() ;
  121.  
  122. #if   MSDOS
  123.    {
  124.       char *p = getenv("MAWKBINMODE") ;
  125.  
  126.       if (p)  set_binmode(atoi(p)) ;
  127.    }
  128. #endif
  129.  
  130.  
  131.    process_cmdline(argc, argv) ;
  132.  
  133.    code_init() ;
  134.    fpe_init() ;
  135.    set_stderr() ;
  136.  
  137. #if  MSDOS
  138.    stdout_init() ;
  139. #endif
  140. }
  141.  
  142. int dump_code_flag ;             /* if on dump internal code */
  143. short posix_space_flag ;
  144.  
  145. #ifdef   DEBUG
  146. int dump_RE ;                    /* if on dump compiled REs  */
  147. #endif
  148.  
  149.  
  150. static void
  151. bad_option(s)
  152.    char *s ;
  153. {
  154.    errmsg(0, "not an option: %s", s) ; mawk_exit(2) ; 
  155. }
  156.  
  157. static void
  158. no_program()
  159. {
  160.    mawk_exit(0) ;
  161. }
  162.  
  163. static void
  164. process_cmdline(argc, argv)
  165.    int argc ;
  166.    char **argv ;
  167. {
  168.    int i, nextarg ;
  169.    char *optarg ;
  170.    PFILE dummy ;                 /* starts linked list of filenames */
  171.    PFILE *tail = &dummy ;
  172.  
  173.    for (i = 1; i < argc && argv[i][0] == '-'; i = nextarg)
  174.    {
  175.       if (argv[i][1] == 0)      /* -  alone */
  176.       {
  177.          if (!pfile_name) no_program() ;
  178.          break ;                 /* the for loop */
  179.       }
  180.       /* safe to look at argv[i][2] */
  181.  
  182.       if (argv[i][2] == 0)
  183.       {
  184.          if (i == argc - 1 && argv[i][1] != '-')
  185.          {
  186.             if (strchr("WFvf", argv[i][1]))
  187.             {
  188.                errmsg(0, "option %s lacks argument", argv[i]) ;
  189.                mawk_exit(2) ;
  190.             }
  191.             bad_option(argv[i]) ;
  192.          }
  193.  
  194.          optarg = argv[i + 1] ;
  195.          nextarg = i + 2 ;
  196.       }
  197.       else  /* argument glued to option */
  198.       {
  199.          optarg = &argv[i][2] ;
  200.          nextarg = i + 1 ;
  201.       }
  202.  
  203.       switch (argv[i][1])
  204.       {
  205.          case 'W':
  206.  
  207.             if (optarg[0] >= 'a' && optarg[0] <= 'z')
  208.                optarg[0] += 'A' - 'a' ;
  209.             if (optarg[0] == 'V')  print_version() ;
  210.             else if (optarg[0] == 'D')
  211.             {
  212.                dump_code_flag = 1 ;
  213.             }
  214.             else if (optarg[0] == 'S')
  215.             {
  216.                char *p = strchr(optarg, '=') ;
  217.                int x = p ? atoi(p + 1) : 0 ;
  218.  
  219.                if (x > SPRINTF_SZ)
  220.                {
  221.                   sprintf_buff = (char *) zmalloc(x) ;
  222.                   sprintf_limit = sprintf_buff + x ;
  223.                }
  224.             }
  225. #if  MSDOS
  226.             else if (optarg[0] == 'B')
  227.             {
  228.                char *p = strchr(optarg, '=') ;
  229.                int x = p ? atoi(p + 1) : 0 ;
  230.  
  231.                set_binmode(x) ;
  232.             }
  233. #endif
  234.             else if (optarg[0] == 'P')
  235.             {
  236.                posix_space_flag = 1 ;
  237.             }
  238.             else if (optarg[0] == 'E')
  239.             {
  240.                if ( pfile_name )
  241.                {
  242.                   errmsg(0, "-W exec is incompatible with -f") ;
  243.                   mawk_exit(2) ;
  244.                }
  245.                else if ( nextarg == argc ) no_program() ;
  246.  
  247.                pfile_name = argv[nextarg] ;
  248.                i = nextarg + 1 ;
  249.                goto no_more_opts ;
  250.             }
  251.             else if (optarg[0] == 'I')
  252.             {
  253.                interactive_flag = 1 ;
  254.                setbuf(stdout,(char*)0) ;
  255.             }
  256.             else  errmsg(0, "vacuous option: -W %s", optarg) ;
  257.  
  258.  
  259.             break ;
  260.  
  261.          case 'v':
  262.             if (!is_cmdline_assign(optarg))
  263.             {
  264.                errmsg(0, "improper assignment: -v %s", optarg) ;
  265.                mawk_exit(2) ;
  266.             }
  267.             break ;
  268.  
  269.          case 'F':
  270.  
  271.             rm_escape(optarg) ;  /* recognize escape sequences */
  272.             cell_destroy(FS) ;
  273.             FS->type = C_STRING ;
  274.             FS->ptr = (PTR) new_STRING(optarg) ;
  275.             cast_for_split(cellcpy(&fs_shadow, FS)) ;
  276.             break ;
  277.  
  278.          case '-':
  279.             if (argv[i][2] != 0)  bad_option(argv[i]) ;
  280.             i++ ;
  281.             goto no_more_opts ;
  282.  
  283.          case 'f':
  284.             /* first file goes in pfile_name ; any more go
  285.                on a list */
  286.             if (!pfile_name)  pfile_name = optarg ;
  287.             else
  288.             {
  289.                tail = tail->link = ZMALLOC(PFILE) ;
  290.                tail->fname = optarg ;
  291.             }
  292.             break ;
  293.  
  294.          default:
  295.             bad_option(argv[i]) ;
  296.       }
  297.    }
  298.  
  299.  no_more_opts:
  300.  
  301.    tail->link = (PFILE *) 0 ;
  302.    pfile_list = dummy.link ;
  303.  
  304.    if (pfile_name)
  305.    {
  306.       set_ARGV(argc, argv, i) ;
  307.       scan_init((char *) 0) ;
  308.    }
  309.    else  /* program on command line */
  310.    {
  311.       if (i == argc)  no_program() ;
  312.       set_ARGV(argc, argv, i + 1) ;
  313.  
  314. #if  MSDOS && ! HAVE_REARGV     /* reversed quotes */
  315.       {
  316.          char *p ;
  317.  
  318.          for (p = argv[i]; *p; p++)
  319.             if (*p == '\'')  *p = '\"' ;
  320.       }
  321. #endif
  322.       scan_init(argv[i]) ;
  323. /* #endif  */
  324.    }
  325. }
  326.  
  327.  
  328. static void
  329. set_ARGV(argc, argv, i)
  330. int argc ; char **argv ;
  331.    int i ;                       /* argv[i] = ARGV[i] */
  332. {
  333.    SYMTAB *st_p ;
  334.    CELL argi ;
  335.    register CELL *cp ;
  336.  
  337.    st_p = insert("ARGV") ;
  338.    st_p->type = ST_ARRAY ;
  339.    Argv = st_p->stval.array = new_ARRAY() ;
  340.    argi.type = C_DOUBLE ;
  341.    argi.dval = 0.0 ;
  342.    cp = array_find(st_p->stval.array, &argi, CREATE) ;
  343.    cp->type = C_STRING ;
  344.    cp->ptr = (PTR) new_STRING(progname) ;
  345.  
  346.    /* ARGV[0] is set, do the rest
  347.      The type of ARGV[1] ... should be C_MBSTRN
  348.      because the user might enter numbers from the command line */
  349.  
  350.    for (argi.dval = 1.0; i < argc; i++, argi.dval += 1.0)
  351.    {
  352.       cp = array_find(st_p->stval.array, &argi, CREATE) ;
  353.       cp->type = C_MBSTRN ;
  354.       cp->ptr = (PTR) new_STRING(argv[i]) ;
  355.    }
  356.    ARGC->type = C_DOUBLE ;
  357.    ARGC->dval = argi.dval ;
  358. }
  359.  
  360.  
  361. /*----- ENVIRON ----------*/
  362.  
  363. void
  364. load_environ(ENV)
  365.    ARRAY ENV ;
  366. {
  367.    CELL c ;
  368. #ifndef  MSDOS_MSC              /* MSC declares it near */
  369.    extern char **environ ;
  370. #endif
  371.    register char **p = environ ; /* walks environ */
  372.    char *s ;                     /* looks for the '=' */
  373.    CELL *cp ;                    /* pts at ENV[&c] */
  374.  
  375.    c.type = C_STRING ;
  376.  
  377.    while (*p)
  378.    {
  379.       if (s = strchr(*p, '='))  /* shouldn't fail */
  380.       {
  381.          int len = s - *p ;
  382.          c.ptr = (PTR) new_STRING0(len) ;
  383.          memcpy(string(&c)->str, *p, len) ;
  384.          s++ ;
  385.  
  386.          cp = array_find(ENV, &c, CREATE) ;
  387.          cp->type = C_MBSTRN ;
  388.          cp->ptr = (PTR) new_STRING(s) ;
  389.  
  390.          free_STRING(string(&c)) ;
  391.       }
  392.       p++ ;
  393.    }
  394. }
  395.