home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
SOFTWARE
/
SOURCES
/
MAWK11AS.ZIP
/
FIELD.C
(
.txt
)
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-18
|
14KB
|
608 lines
/********************************************
field.c
copyright 1991, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/* $Log: field.c,v $
* Revision 5.1 91/12/05 07:55:57 brennan
* 1.1 pre-release
*
*/
/* field.c */
#include "mawk.h"
#include "field.h"
#include "init.h"
#include "memory.h"
#include "scan.h"
#include "bi_vars.h"
#include "repl.h"
#include "regexp.h"
CELL field[FBANK_SZ+NUM_PFIELDS] ;
CELL *fbank[NUM_FBANK] = {field} ;
static int max_field = MAX_SPLIT ; /* maximum field actually created*/
static void PROTO( build_field0, (void) ) ;
static void PROTO( set_rs_shadow, (void) ) ;
static void PROTO( load_pfield, (char*, CELL*)) ;
static void PROTO( load_field_ov, (void)) ;
/* a description of how to split based on RS.
If RS is changed, so is rs_shadow */
SEPARATOR rs_shadow = {SEP_CHAR, '\n'} ;
/* a splitting CELL version of FS */
CELL fs_shadow = {C_SPACE} ;
int nf ;
/* nf holds the true value of NF. If nf < 0 , then
NF has not been computed, i.e., $0 has not been split
*/
static void set_rs_shadow()
{ CELL c ;
STRING *sval ;
char *s ;
unsigned len ;
if ( posix_space_flag && mawk_state == EXECUTION )
scan_code['\n'] = SC_UNEXPECTED ;
if ( rs_shadow.type == SEP_STR ) free_STRING((STRING*) rs_shadow.ptr) ;
cast_for_split( cellcpy(&c, RS) ) ;
switch( c.type )
{
case C_RE :
if ( s = is_string_split(c.ptr, &len) )
if ( len == 1 )
{ rs_shadow.type = SEP_CHAR ;
rs_shadow.c = s[0] ;
}
else
{ rs_shadow.type = SEP_STR ;
rs_shadow.ptr = (PTR) new_STRING(s) ;
}
else
{ rs_shadow.type = SEP_RE ;
rs_shadow.ptr = c.ptr ;
}
break ;
case C_SPACE :
rs_shadow.type = SEP_CHAR ;
rs_shadow.c = ' ' ;
break ;
case C_SNULL : /* RS becomes one or more blank lines */
if ( mawk_state == EXECUTION ) scan_code['\n'] = SC_SPACE ;
rs_shadow.type = SEP_MLR ;
sval = new_STRING( "\n\n+" ) ;
rs_shadow.ptr = re_compile(sval) ;
free_STRING(sval) ;
break ;
default : bozo("bad cell in set_rs_shadow") ;
}
}
static void load_pfield(name, cp)
char *name ;
CELL *cp ;
{ SYMTAB *stp ;
stp = insert(name) ; stp->type = ST_FIELD ;
stp->stval.cp = cp ;
}
/* initialize $0 and the pseudo fields */
void field_init()
{
field[0].type = C_STRING ;
field[0].ptr = (PTR) & null_str ;
null_str.ref_cnt++ ;
load_pfield("NF",NF) ;
NF->type = C_DOUBLE ;
NF->dval = 0.0 ;
load_pfield("RS", RS) ;
RS->type = C_STRING ;
RS->ptr = (PTR) new_STRING( "\n" ) ;
/* rs_shadow already set */
load_pfield("FS", FS) ;
FS->type = C_STRING ;
FS->ptr = (PTR) new_STRING( " " ) ;
/* fs_shadow is already set */
load_pfield("OFMT", OFMT) ;
OFMT->type = C_STRING ;
OFMT->ptr = (PTR) new_STRING( "%.6g" ) ;
load_pfield("CONVFMT", CONVFMT) ;
CONVFMT->type = C_STRING ;
CONVFMT->ptr = OFMT->ptr ;
string(OFMT)->ref_cnt++ ;
}
void set_field0( s, len)
char *s ;
unsigned len ;
{
cell_destroy( & field[0] ) ;
nf = -1 ;
if ( len )
{
field[0].type = C_MBSTRN ;
field[0].ptr = (PTR) new_STRING( (char *) 0, len) ;
(void) memcpy( string(&field[0])->str, s, SIZE_T(len) ) ;
}
else
{
field[0].type = C_STRING ;
field[0].ptr = (PTR) &null_str ;
null_str.ref_cnt++ ;
}
}
/* split field[0] into $1, $2 ... and set NF */
void split_field0()
{ register CELL *cp ;
register int cnt ;
CELL c ; /* copy field[0] here if not string */
if ( field[0].type < C_STRING )
{ cast1_to_s(cellcpy(&c, field+0)) ;
cp = &c ;
}
else cp = &field[0] ;
if ( string(cp)->len == 0 ) nf = 0 ;
else
{
switch( fs_shadow.type )
{
case C_SNULL : /* FS == "" */
nf = 1 ;
cell_destroy(NF) ;
NF->type = C_DOUBLE ;
NF->dval = 1.0 ;
field[1].type = C_MBSTRN ;
field[1].ptr = cp->ptr ;
if ( cp == field ) string(cp)->ref_cnt++ ;
/* else we gain one ref_cnt and lose one for a wash */
return ;
case C_SPACE :
nf = space_split(string(cp)->str, string(cp)->len) ;
break ;
default :
nf = re_split(string(cp)->str, fs_shadow.ptr) ;
break ;
}
}
cell_destroy(NF) ;
NF->type = C_DOUBLE ;
NF->dval = (double) nf ;
if ( nf > MAX_SPLIT )
{
cnt = MAX_SPLIT ; load_field_ov() ;
}
else cnt = nf ;
while ( cnt > 0 )
{
cell_destroy(field+cnt) ;
field[cnt].ptr = (PTR) split_buff[cnt-1] ;
field[cnt--].type = C_MBSTRN ;
}
if ( cp == &c ) free_STRING( string(cp) ) ;
}
/*
assign CELL *cp to field or pseudo field
and take care of all side effects
*/
void field_assign( fp, cp)
register CELL *fp ;
CELL *cp ;
{
CELL c ;
int i , j ;
/* the most common case first */
if ( fp == field )
{ cell_destroy(field) ;
(void) cellcpy(fp, cp) ;
nf = -1 ;
return ;
}
/* its not important to do any of this fast */
if ( nf < 0 ) split_field0() ;
#if LM_DOS
if ( !SAMESEG(fp,field) ) goto lm_dos_label ;
#endif
switch( i = (fp - field) )
{
case NF_field :
cell_destroy(NF) ;
(void) cellcpy(NF, cellcpy(&c,cp) ) ;
if ( c.type != C_DOUBLE ) cast1_to_d(&c) ;
if ( (j = (int) c.dval) < 0 )
rt_error("negative value assigned to NF") ;
if ( j > nf )
for ( i = nf+1 ; i <= j ; i++ )
{
cp = field_ptr(i) ;
cell_destroy(cp) ;
cp->type = C_STRING ;
cp->ptr = (PTR) &null_str ;
null_str.ref_cnt++ ;
}
nf = j ;
build_field0() ;
break ;
case RS_field :
cell_destroy(RS) ;
(void) cellcpy(RS, cp) ;
set_rs_shadow() ;
break ;
case FS_field :
cell_destroy(FS) ;
cast_for_split( cellcpy(&fs_shadow, cellcpy(FS, cp)) ) ;
break ;
case OFMT_field :
case CONVFMT_field:
/* If the user does something stupid with OFMT or CONVFMT,
we could crash.
We'll make an attempt to protect ourselves here. This is
why OFMT and CONVFMT are pseudo fields.
The ptrs of OFMT and CONVFMT always have a valid STRING,
even if assigned a DOUBLE or NOINIT
*/
free_STRING( string(fp) ) ;
(void) cellcpy(fp, cp) ;
if ( fp->type < C_STRING ) /* !! */
fp->ptr = (PTR) new_STRING( "%.6g" ) ;
else
if ( fp == CONVFMT )
{
/* It's a string, but if it's really goofy and CONVFMT,
it could still damage us. Test it .
*/
string_buff[256] = 0 ;
(void) sprintf(string_buff,
string(fp)->str, 3.1459) ;
if ( string_buff[256] )
rt_error("CONVFMT assigned unusable value") ;
}
break ;
#if LM_DOS
lm_dos_label :
#endif
default: /* $1 or $2 or ... */
cell_destroy(fp) ;
(void) cellcpy(fp, cp) ;
if ( i < 0 || i > MAX_SPLIT ) i = field_addr_to_index(fp) ;
if ( i > nf )
{ for ( j = nf+1 ; j < i ; j++ )
{
cp = field_ptr(j) ;
cell_destroy(cp) ;
cp->type = C_STRING ;
cp->ptr = (PTR) &null_str ;
null_str.ref_cnt++ ;
}
nf = i ;
cell_destroy(NF) ;
NF->type = C_DOUBLE ;
NF->dval = (double) i ;
}
build_field0() ;
}
}
/* construct field[0] from the ot