Exemple #1
0
static PyObject *
python_pfout( PyObject *self, PyObject *args ) {
	char	*usage = "Usage: _stock._pfout( pfname, file )\n";
	char	*pfname;
	Pf	*pf;
	FILE	*fp;
	PyObject *fileobj;
	char	errmsg[STRSZ];
	long	rc;

	if( ! PyArg_ParseTuple( args, "sO!", &pfname, &PyFile_Type, &fileobj ) ) {

		USAGE;

		return NULL;
	}

	if( ( pf = getPf( pfname ) ) == (Pf *) NULL ) {
		
		sprintf( errmsg, "Failure opening parameter file '%s'\n", pfname );

		raise_elog( ELOG_COMPLAIN, errmsg );

		return NULL;
	}

	fp = PyFile_AsFile( fileobj );

	rc = pfout( fp, pf );

	return Py_BuildValue( "i", rc );
}
Exemple #2
0
static char *prevp(CSOUND *csound, SRTBLK *bp, char *p,
                   int lincnt, int pcnt, CORFIL *sco)
{
    char *q;
    int n;

    q = p;
    p++;                                    /* 1st char     */
    if (UNLIKELY(*p++ != 'p'))              /* 2nd char     */
      goto error;
    n = 999;
    if (isdigit(*p))
      n = *p++ - '0';
    if (isdigit(*p))                /* n is np subscript no */
      n = 10*n + (*p++ - '0');
    if (UNLIKELY(*p != SP && *p != LF))
      goto error;
    if (LIKELY((bp = prvins(bp)) != NULL   /* for prvins, same p1, */
               && n <= bp->pcnt)) {
      q = bp->text;
      while (n--)
        while (*q++ != SP)          /*   go find the pfield */
          ;
      pfout(csound,bp,q,lincnt,pcnt, sco); /*   and put it out */
    }
    else {
    error:
      csound->Message(csound,
          Str("swrite: output, sect%d line%d p%d makes illegal reference to "),
          csound->sectcnt,lincnt,pcnt);
      while (q < p)
        csound->Message(csound,"%c", *q++);
      while (*p != SP && *p != LF)
        csound->Message(csound,"%c", *p++);
      csound->Message(csound,Str("   Zero substituted\n"));
      corfile_putc(csound, '0', sco);
    }
    return(p);
}
/* This is the opposite of the previous */
void *pfstream_write_data(void *arg)
{
    Pfstream_control *pfsc=(Pfstream_control *)arg;
    Pf *pf,*pftest;
    int fini;
    int itest;

    while(1)
    {
        pmtfifo_pop(pfsc->mtf,(void **)(&pf));
        /* this is a safe test if FINISHED is not set.
        Should not require user to have to do this */
        itest = pfget(pf,"FINISHED",(void **)&pftest);
        if(itest==PFINVALID)
        {
            fini=0;
            pfput_boolean(pf,"FINISHED",0);
        }
        else
            fini=pfget_boolean(pf,"FINISHED");
        if(fini)
        {
            fprintf(pfsc->fp,"%s\n",END_OF_DATA_SENTINEL);
            fflush(pfsc->fp);
            sleep(20);
            fclose(pfsc->fp);
            break;
        }
        pfout(pfsc->fp,pf);
        fprintf(pfsc->fp,"%s\n", ENDPF_SENTINEL);
        fflush(pfsc->fp);
        /* We have release the memory for pf here as the caller
        is pushing this away a forgetting about it */
        pffree(pf);
    }
    return(NULL);
}
Exemple #4
0
void swritestr(CSOUND *csound, CORFIL *sco, int first)
{
    SRTBLK *bp;
    char   *p, c, isntAfunc;
    int    lincnt, pcnt=0;

    if (UNLIKELY((bp = csound->frstbp) == NULL))
      return;

    lincnt = 0;
    if ((c = bp->text[0]) != 'w'
        && c != 's' && c != 'e') {      /*   if no warp stmnt but real data,  */
      /* create warp-format indicator */
      if (first) corfile_puts(csound, "w 0 60\n", sco);
      lincnt++;
    }
 nxtlin:
    lincnt++;                           /* now for each line:           */
    p = bp->text;
    c = *p++;
    isntAfunc = 1;
    switch ((int) c) {
    case 'z':
      printf("skip z\n");
      //corfile_putc('\n', sco);
      break;
    case 'f':
      isntAfunc = 0;
      /* fall through */
    case 'q':
    case 'i':
    case 'd':
    case 'a':
      corfile_putc(csound, c, sco);
      corfile_putc(csound, *p++, sco);
      while ((c = *p++) != SP && c != LF)
        corfile_putc(csound, c, sco);                /* put p1       */
      corfile_putc(csound, c, sco);
      if (c == LF)
        break;
      fltout(csound, bp->p2val, sco);                        /* put p2val,   */
      corfile_putc(csound, SP, sco);
      if (first) fltout(csound, bp->newp2, sco);             /*   newp2,     */
      while ((c = *p++) != SP && c != LF)
        ;
      corfile_putc(csound, c, sco);                /*   and delim  */
      if (c == LF)
        break;
      if (isntAfunc) {
        fltout(csound, bp->p3val, sco);                      /* put p3val,   */
        corfile_putc(csound, SP, sco);
        if (first) fltout(csound, bp->newp3, sco);           /*   newp3,     */
        while ((c = *p++) != SP && c != LF)
          ;
      }
      else { /*make sure p3s (table length) are ints */
        char temp[256];
        snprintf(temp,256,"%d ",(int32)bp->p3val);   /* put p3val  */
        fpnum(csound,temp, lincnt, pcnt, sco);
        corfile_putc(csound, SP, sco);
        if (first) {
          snprintf(temp,256,"%d ",(int32)bp->newp3);   /* put newp3  */
          fpnum(csound,temp, lincnt, pcnt, sco);
        }
        while ((c = *p++) != SP && c != LF)
          ;
      }
      pcnt = 3;
      while (c != LF) {
        pcnt++;
        corfile_putc(csound, SP, sco);
        p = pfout(csound,bp,p,lincnt,pcnt, sco);     /* now put each pfield  */
        c = *p++;
      }
      corfile_putc(csound, '\n', sco);
      break;
    case 's':
    case 'e':
      if (bp->pcnt > 0) {
        char buffer[80];
        CS_SPRINTF(buffer, "f 0 %f %f\n", bp->p2val, bp->newp2);
        corfile_puts(csound, buffer, sco);
      }
      corfile_putc(csound, c, sco);
      corfile_putc(csound, LF, sco);
      break;
    case 'w':
    case 't':
      corfile_putc(csound, c, sco);
      while ((c = *p++) != LF)        /* put entire line      */
        corfile_putc(csound, c, sco);
      corfile_putc(csound, LF, sco);
      break;
    case 'x':
    case 'y':
    case -1:
      break;
    default:
      csound->Message(csound,
                      Str("swrite: unexpected opcode %c, section %d line %d\n"),
                      c, csound->sectcnt, lincnt);
      break;
    }
    if ((bp = bp->nxtblk) != NULL)
      goto nxtlin;
}
int
delegate_to_perl( Dbptr db, Pf *delegate_pf )
{
	char	scriptfile[FILENAME_MAX];
	char	fifofile[FILENAME_MAX];
	char	pffile[FILENAME_MAX];
	char	*commands;
	char	*mypath;
	char	*cwd;
	FILE	*fp;
	Tbl	*keys;
	char	*key;
	Pf	*pfpart;
	pid_t	pid;
	int	status;
	int	i;

	if( ( mypath = datafile( "PATH", "perl" ) ) == 0 ) {

		complain( 0, "Couldn't find perl on path.\n" );

		return -1;

	} else if( access( mypath, R_OK|X_OK ) ) {

		complain( 0, "Couldn't find perl on path.\n" );
		
		return -1;
	}

	if( ( commands = pfget_string( delegate_pf, "commands" ) ) == NULL ) {

		complain( 0, "No commands &Literal in parameter file\n" );

		return -1;
	}

	cwd = getcwd( NULL, FILENAME_MAX );

	sprintf( scriptfile, "/tmp/dbgme_script_%d_%d.pl", getuid(), getpid() );
	sprintf( fifofile, "/tmp/dbgme_fifo_%d_%d.fifoview", getuid(), getpid() );
	sprintf( pffile, "/tmp/dbgme_pf_%d_%d.pf", getuid(), getpid() );

	mkfifo( fifofile, 0775 );

	fp = fopen( pffile, "w" ); 
	keys = pfkeys( delegate_pf );
	for( i = 0; i < maxtbl( keys ); i++ ) {

		key = gettbl( keys, i );

		if( ! strcmp( key, "commands" ) ) {

			continue;
		}

		pfresolve( delegate_pf, key, 0, &pfpart );

		fprintf( fp, "%s ", key );
		pfout( fp, pfpart ); 
	}
	fclose( fp ); 
	
	fp = fopen( scriptfile, "w" );
	fprintf( fp, "@db = dbopen_table( \"%s\", \"r\" );\n", fifofile );
	fprintf( fp, "$pf = \"%s\";\n", pffile );
	fprintf( fp, "%s\n", commands );
	fprintf( fp, "exit\n" );
	fclose( fp );

	if( ( pid = fork() ) == 0 ) {

		if( Verbose ) {

			fprintf( stderr, "Running Perl delegate\n" );

		} else {

			fflush( stderr );
			fclose( stdout );
		}

		execlp( "perl", "perl", "-e", scriptfile, NULL );

	} else {

		fp = fopen( fifofile, "w" );
		dbwrite_view( db, fp );
		fclose( fp );

		wait( &status );
	}

	if( Verbose ) {

		fprintf( stderr, "Perl exit status is %d\n", status );
	}

	free( cwd );

	unlink( scriptfile );
	unlink( fifofile );
	unlink( pffile );

	if( status != 0 ) {

		return -1;

	} else {

		return 0;
	}
}
void main(int argc, char **argv)
{
	Dbptr db, dbv, dbge, dbgg;
	/*ensemble_mode is primary grouping, grouping_on set if secondary on*/
	int ensemble_mode,grouping_on;
	Tbl *ensemble_keys;
	Tbl *group_keys;
	int i;
	char *pfi=NULL;
	Pf *pf,*pfo_head,*pfo_total;
	Tbl *process_list;
	int dryrun=0;
	char *sift_exp=NULL;
	int sift;
/*Attributes listed in require will abort program if missing
passthrough parameters will generate an error message but not
cause the program to abort.  Both contain pointers to Attribute_map*/
	Tbl *require,*passthrough;
	Attribute_map *amap;
	FILE *fp;
	Tbl *error_list;
	char *tag;
	int sleep_time;  

	DB2PFS_verbose=0;

/*crack the command line*/
	if(argc<3) usage();
	elog_init(argc,argv);
	if(dbopen(argv[1],"r",&db)==dbINVALID)
	{
		elog_complain(0,"dbopen failed on database %s\n",argv[1]);
		usage();
	}

	for(i=3;i<argc;++i)
	{
		if(!strcmp(argv[i],"-pf"))
		{
			++i;
			if(i>argc) usage();
			pfi=argv[i];
		}
		else if(!strcmp(argv[i],"-V"))
			usage();
		else if(!strcmp(argv[i],"-v"))
			DB2PFS_verbose=1;
		else if(!strcmp(argv[i],"-n"))
			dryrun=1;
		else if(!strcmp(argv[i],"-sift"))
		{
			++i;
			if(i>argc) usage();
			sift=1;
			sift_exp=argv[i];
		}
		else
			usage();
	}
	if(!dryrun)
	{
		fp=fopen(argv[2],"r+");
		if(fp==NULL) usage();
	}
	
	if(pfi==NULL) pfi=strdup("db2pfstream");
	if(pfread(pfi,&pf))
		elog_die(0,"Error reading parameter file %s.pf\n",pfi);
	sleep_time=pfget_int(pf,"sleep_time");
	/* The output view gets a virtual table name that tags the
	overall collection of stuff.  This comes from the parameter file
	to make this program general, BUT it must be coordinated with
	the reader code. */
	tag = pfget_string(pf,"virtual_table_name");
	if(tag==NULL)elog_die(0,"Required parameter (virtual_table_name) missing from parameter file\n");
	ensemble_mode = pfget_boolean(pf,"ensemble_mode");
	if(ensemble_mode)
	{
		ensemble_keys=pfget_tbl(pf,"ensemble_keys");
		if(ensemble_keys==NULL)
			elog_die(0,
			 "ensemble_mode is on, but no grouping keys defined\nCheck parameter file\n");
		group_keys=pfget_tbl(pf,"group_keys");
		if(group_keys==NULL)
			grouping_on = 0;
		else
		{
			if(maxtbl(group_keys)>0)
				grouping_on = 1;
			else
				grouping_on = 0;
		}
	}
	if(DB2PFS_verbose && ensemble_mode)
	{
		char sm[128];
		char *key;
		strcpy(sm,"Defining ensemble with keys: ");
		for(i=0;i<maxtbl(ensemble_keys);++i)
		{
			key=gettbl(ensemble_keys,i);
			strcat(sm,key);
		}
		elog_log(0,"%s\n",sm);
		if(grouping_on)
		{
			strcpy(sm,"Grouping ensemble with keys:  ");
			for(i=0;i<maxtbl(group_keys);++i)
			{
				key = gettbl(group_keys,i);
				strcat(sm,key);
			}
			elog_log(0,"%s\n",sm);
		}
	}

	
	/*This function calls dbprocess using a tbl from pf*/
	dbv=dbform_working_view(db,pf,"dbprocess_list");
	if(dbv.record==dbINVALID)
		elog_die(0,"dbprocess failed:  check database and parameter file parameter dbprocess_list\n");
	if(sift_exp!=NULL)dbv=dbsubset(dbv,sift_exp,0);
	/*Now we form the groupings if needed */
	if(ensemble_mode)
	{
		dbge = dbgroup(dbv,ensemble_keys,"ensemble",1);
		if(dbge.record == dbINVALID)
			elog_die(0,"dbgroup with ensemble keys failed\n");
		if(grouping_on)
		{
			dbgg = dbgroup(dbv,group_keys,"groups",2);
			if(dbgg.record == dbINVALID)
				elog_die(0,"dbgroup on secondary grouping keys failed\n");
		}
	}

/*This builds the maps of which attributes will be saved */
	require = pfget_Attribute_map(pf,"require");
	if(require==NULL)
		elog_die(0,"Error in parameter file for parameter require\n");
	passthrough = pfget_Attribute_map(pf,"passthrough");
	if(passthrough==NULL)
		elog_die(0,"Error in parameter file for parameter passthrough\n");
	/* Unfortunately, we have two different loops for ensemble mode
	 and single object mode */
	if(ensemble_mode)
	{
		int number_ensembles;
		Dbptr db_bundle_1;
		Dbptr db_bundle_2;
		int is_ensemble, ie_ensemble;
		int isg, ieg;
		int number_groups;

		pfo_head=pfnew(0);
		pfput_tbl(pfo_head,"ensemble_keys",ensemble_keys);
		if(grouping_on) pfput_tbl(pfo_head,"group_keys",group_keys);
	

		dbquery(dbge,dbRECORD_COUNT,&number_ensembles);
		if(DB2PFS_verbose) elog_log(0,"database has %d ensembles\n",
					number_ensembles);
		if(grouping_on) 
		{
			dbgg.record=0;
			dbquery(dbgg,dbRECORD_COUNT,&number_groups);
			if(DB2PFS_verbose)
			  elog_log(0,"database has %d subgroups\n",
				number_groups);
		}
		for(dbge.record=0;dbge.record<number_ensembles;
					++dbge.record)
		{
			Pf_ensemble *pfe;
			int nmembers;
			char *grp_records;
			Tbl *grplist;

			dbgetv(dbge,0,"bundle",&db_bundle_1,0);
			dbget_range(db_bundle_1,&is_ensemble,&ie_ensemble);
			nmembers = ie_ensemble-is_ensemble;
			/* pfe does not need to hold the group records
			for this application so the ngroups variable is 
			passed as 0*/
			pfe=create_Pf_ensemble(nmembers,0);
			/*Now loop through and load the array of pf's
			with parameters defined by the attribute maps*/
			for(i=0,dbv.record=is_ensemble;i<nmembers;
				++i,++dbv.record)
			{
				pfe->pf[i]=pfnew(PFARR);
				error_list=db2pf(dbv,require,pfe->pf[i]);
				if(maxtbl(error_list)>0)
				{
				    if(dryrun)
				    {
					elog_log(0,"Ensemble %d at input database view record %d lacks required attributes\n",
						dbge.record,dbv.record);
					log_error_list(error_list);
				    }
				    else
				    {
					fprintf(fp,"%s\n",END_OF_DATA_SENTINEL);
					fflush(fp);
					elog_log(0,"FATAL ERROR: ensemble %d at input database view record %d lacks required attributes\nOUTPUT DATA STREAM TRUNCATED\n",
						i,dbv.record);
					log_error_list(error_list);
					exit(-1);
				    }
				}
				freetbl(error_list,free);
				error_list=db2pf(dbv,passthrough,pfe->pf[i]);
				if(DB2PFS_verbose)
				{
					elog_log(0,"Warning:  null passthrough attributes for ensemble %d at row %d\n",
						dbge.record,dbv.record);
					log_error_list(error_list);
				}
				freetbl(error_list,free);
				pfput_boolean(pfe->pf[i],"data_valid",1);
			}
			
			if(grouping_on)
			{
				grplist=newtbl(0);
				do {
				     dbgetv(dbgg,0,"bundle",&db_bundle_2,0);
				     dbget_range(db_bundle_2,&isg,&ieg);
				     grp_records=malloc(30);
				     sprintf(grp_records,"%d %d",
					isg-is_ensemble,ieg-is_ensemble-1);
				     pushtbl(grplist,grp_records);
				     ++dbgg.record;
				}
				while (ieg<ie_ensemble);
				pfput_tbl(pfo_head,"group_records",grplist);
			}

			pfo_total=build_ensemble(1,tag,pfo_head,pfe);
			free_Pf_ensemble(pfe);
			if(!dryrun) 
			{
				pfout(fp,pfo_total);
				fprintf(fp,"%s\n",ENDPF_SENTINEL);
				fflush(fp);
			}
			pffree(pfo_total);
			if(grouping_on)freetbl(grplist,free);
		}
	}
	else
	{
	/*This is the simpler, single block mode */
		int nrecords;
		Pf *pfo;

		dbquery(dbv,dbRECORD_COUNT,&nrecords);
		pfo=pfnew(PFARR);
		for(dbv.record=0;dbv.record<nrecords;++dbv.record)
		{
			error_list=db2pf(dbv,require,pfo);
			if(maxtbl(error_list)>0)
			{
			    if(dryrun)
			    {
				elog_log(0,"Input database view at row %d lacks required parameters\n",
					dbv.record);
				log_error_list(error_list);
			    }
			    else
			    {
				fprintf(fp,"%s\n",END_OF_DATA_SENTINEL);
				fflush(fp);
				elog_log(0,"FATAL: input database view at row %d lacks required parameters\n",
					dbv.record);
				log_error_list(error_list);
				exit(-1);
			    }
			}
			freetbl(error_list,free);
			error_list=db2pf(dbv,passthrough,pfo);
			pfput_boolean(pfo,"data_valid",1);
			if(DB2PFS_verbose)
			{
				elog_log(0,"Warning:  null passthrough attributes for row %d of input database view\n",
					dbv.record);
			}
			if(!dryrun)
			{
				pfout(fp,pfo);
				fprintf(fp,"%s\n",ENDPF_SENTINEL);
			}
		}
	}
	if(!dryrun)
	{
		fprintf(fp,"%s\n",END_OF_DATA_SENTINEL);
		fflush(fp);
		sleep(sleep_time);
		fclose(fp);
	}
	exit(0);
}
int
delegate_to_matlab( Dbptr db, Pf *delegate_pf )
{
	char	scriptfile[FILENAME_MAX];
	char	fifofile[FILENAME_MAX];
	char	pffile[FILENAME_MAX];
	char	cmd[STRSZ];
	char	*commands;
	char	*mypath;
	char	*cwd;
	FILE	*fp;
	Tbl	*keys;
	char	*key;
	Pf	*pfpart;
	pid_t	pid;
	int	status;
	int	i;

	if( ( mypath = datafile( "PATH", "matlab" ) ) == 0 ) {

		elog_complain( 0, "Couldn't find matlab on path.\n" );

		return -1;

	} else if( access( mypath, R_OK|X_OK ) ) {

		elog_complain( 0, "Couldn't find matlab on path.\n" );
		
		return -1;
	}

	if( ( commands = pfget_string( delegate_pf, "commands" ) ) == NULL ) {

		elog_complain( 0, "No commands &Literal in parameter file\n" );

		return -1;
	}

	cwd = getcwd( NULL, FILENAME_MAX );

	sprintf( scriptfile, "/tmp/dbgme_script_%d_%d.m", getuid(), getpid() );
	sprintf( fifofile, "/tmp/dbgme_fifo_%d_%d", getuid(), getpid() );
	sprintf( pffile, "/tmp/dbgme_pf_%d_%d.pf", getuid(), getpid() );

	mkfifo( fifofile, 0775 );

	fp = fopen( pffile, "w" ); 
	keys = pfkeys( delegate_pf );
	for( i = 0; i < maxtbl( keys ); i++ ) {

		key = gettbl( keys, i );

		if( ! strcmp( key, "commands" ) ) {

			continue;
		}

		pfresolve( delegate_pf, key, 0, &pfpart );

		fprintf( fp, "%s ", key );
		pfout( fp, pfpart ); 
	}
	fclose( fp ); 
	
	fp = fopen( scriptfile, "w" );
	fprintf( fp, "addpath('%s')\n", cwd );
	fprintf( fp, "db = dbread_view( '%s' );\n", fifofile );
	fprintf( fp, "pf = dbpf( '%s' );\n", pffile );
	fprintf( fp, "%s\n", commands );
	fprintf( fp, "exit\n" );
	fclose( fp );

	sprintf( cmd, 
		"try, "
			"run %s, "
		"catch, "
			"lasterr, "
			"elog_clear_register('print'), "
			"unix(['kill -9 ' num2str(getpid)]), "
		"end", 
		scriptfile );

	if( ( pid = fork() ) == 0 ) {

		if( Verbose ) {

			fprintf( stderr, "Running Matlab delegate\n" );

		} else {

			fflush( stderr );
			fclose( stdout );
		}

		execlp( "matlab", "matlab", "-nodisplay", "-nojvm",
			"-r", cmd, NULL );

	} else {

		fp = fopen( fifofile, "w" );
		dbwrite_view( db, fp );
		fclose( fp );

		wait( &status );
	}

	if( Verbose ) {

		fprintf( stderr, "Matlab exit status is %d\n", status );
	}

	free( cwd );

	unlink( scriptfile );
	unlink( fifofile );
	unlink( pffile );

	if( status != 0 ) {

		return -1;

	} else {

		return 0;
	}
}