1 /********************************************
2 bi_vars.c
3 copyright 2009,2010, Thomas E. Dickey
4 copyright 1991-1992,1993, 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  * $MawkId: bi_vars.c,v 1.10 2010/12/10 17:00:00 tom Exp $
15  * @Log: bi_vars.c,v @
16  * Revision 1.1.1.1  1993/07/03  18:58:09  mike
17  * move source to cvs
18  *
19  * Revision 5.2  1992/07/10  16:17:10  brennan
20  * MsDOS: remove NO_BINMODE macro
21  *
22  * Revision 5.1  1991/12/05  07:55:38  brennan
23  * 1.1 pre-release
24  *
25  */
26 
27 /* bi_vars.c */
28 
29 #include "mawk.h"
30 #include "symtype.h"
31 #include "bi_vars.h"
32 #include "field.h"
33 #include "init.h"
34 #include "memory.h"
35 
36 /* the builtin variables */
37 CELL bi_vars[NUM_BI_VAR];
38 
39 /* the order here must match the order in bi_vars.h */
40 
41 static const char *bi_var_names[NUM_BI_VAR] =
42 {
43     "NR",
44     "FNR",
45     "ARGC",
46     "FILENAME",
47     "OFS",
48     "ORS",
49     "RLENGTH",
50     "RSTART",
51     "SUBSEP"
52 #if USE_BINMODE
53     ,"BINMODE"
54 #endif
55 };
56 
57 /* insert the builtin vars in the hash table */
58 
59 void
bi_vars_init(void)60 bi_vars_init(void)
61 {
62     register int i;
63     register SYMTAB *s;
64 
65     for (i = 0; i < NUM_BI_VAR; i++) {
66 	s = insert(bi_var_names[i]);
67 	s->type = (char) ((i <= 1) ? ST_NR : ST_VAR);
68 	s->stval.cp = bi_vars + i;
69 	/* bi_vars[i].type = 0 which is C_NOINIT */
70     }
71 
72     s = insert("ENVIRON");
73     s->type = ST_ENV;
74 
75     /* set defaults */
76 
77     FILENAME->type = C_STRING;
78     FILENAME->ptr = (PTR) new_STRING("");
79 
80     OFS->type = C_STRING;
81     OFS->ptr = (PTR) new_STRING(" ");
82 
83     ORS->type = C_STRING;
84     ORS->ptr = (PTR) new_STRING("\n");
85 
86     SUBSEP->type = C_STRING;
87     SUBSEP->ptr = (PTR) new_STRING("\034");
88 
89     NR->type = FNR->type = C_DOUBLE;
90     /* dval is already 0.0 */
91 
92 #if USE_BINMODE
93     BINMODE->type = C_DOUBLE;
94 #endif
95 }
96 
97 #ifdef NO_LEAKS
98 void
bi_vars_leaks(void)99 bi_vars_leaks(void)
100 {
101     int n;
102 
103     for (n = 0; n < NUM_BI_VAR; ++n) {
104 	switch (bi_vars[n].type) {
105 	case C_STRING:
106 	case C_STRNUM:
107 	case C_MBSTRN:
108 	    free_STRING(string(&bi_vars[n]));
109 	    break;
110 	}
111     }
112 }
113 #endif
114