示例#1
0
static int scope(CSOUND *csound, int argc, char **argv)
{
#if 0
    PRS_PARM  qq;
    int len=100, p=0, n;
    char buff[1024];
    FILE *ff;
      /* Pre-process */
    memset(&qq, '\0', sizeof(PRS_PARM));
    csound->scorestr = corfile_create_w();
    csound_prslex_init(&qq.yyscanner);
    csound_prsset_extra(&qq, qq.yyscanner);
    csound->scorename = argv[1];
    ff = fopen(csound->scorename, "r");
    memset(buff, '\0', 1024);
    while ((n = fread(buff, 1, 1023, ff))) {
      corfile_puts(buff, csound->scorestr);
      memset(buff, '\0', 1024);
    }
    corfile_putc('\0', csound->scorestr);     /* For use in bison/flex */
    corfile_putc('\0', csound->scorestr);     /* For use in bison/flex */

    csound->expanded_sco = corfile_create_w();
    snprintf(buff, 1024, "#source %d\n",
            qq.lstack[0] = file_to_int(csound, csound->scorename));
    corfile_puts(buff, csound->expanded_sco);
    snprintf(buff, "#line %d\n", csound->orcLineOffset);
    corfile_puts(buff, csound->expanded_sco);
    qq.line = 1;
    csound_prslex(csound, qq.yyscanner);
    csound->DebugMsg(csound, "yielding >>%s<<\n",
                     corfile_body(csound->expanded_sco));
    csound_prslex_destroy(&qq.yyscanner);
#endif
    return 0;
}
示例#2
0
TREE *csoundParseOrc(CSOUND *csound, const char *str)
{
    int err;
    OPARMS *O = csound->oparms;
    csound->parserNamedInstrFlag = 2;
    {
      PRE_PARM    qq;
      /* Preprocess */
      memset(&qq, 0, sizeof(PRE_PARM));
      //csp_orc_sa_print_list(csound);
      csound_prelex_init(&qq.yyscanner);
      csound_preset_extra(&qq, qq.yyscanner);
      qq.line = csound->orcLineOffset;
      csound->expanded_orc = corfile_create_w();
      file_to_int(csound, "**unknown**");
      if (str == NULL) {
        char bb[80];

        if (csound->orchstr==NULL && !csound->oparms->daemon)
          csound->Die(csound,
                      Str("Failed to open input file %s\n"), csound->orchname);
        else if(csound->orchstr==NULL && csound->oparms->daemon)  return NULL;

        add_include_udo_dir(csound->orchstr);
        if (csound->orchname==NULL ||
            csound->orchname[0]=='\0') csound->orchname = csound->csdname;
        /* We know this is the start so stack is empty so far */
        snprintf(bb, 80, "#source %d\n",
                qq.lstack[0] = file_to_int(csound, csound->orchname));
        corfile_puts(bb, csound->expanded_orc);
        snprintf(bb, 80, "#line %d\n", csound->orcLineOffset);
        corfile_puts(bb, csound->expanded_orc);
      }
      else {
        if (csound->orchstr == NULL ||
            corfile_body(csound->orchstr) == NULL)
          csound->orchstr = corfile_create_w();
        else
          corfile_reset(csound->orchstr);
        corfile_puts(str, csound->orchstr);
        corfile_puts("\n#exit\n", csound->orchstr);
        corfile_putc('\0', csound->orchstr);
        corfile_putc('\0', csound->orchstr);
      }
      csound->DebugMsg(csound, "Calling preprocess on >>%s<<\n",
              corfile_body(csound->orchstr));
      //csound->DebugMsg(csound,"FILE: %s \n", csound->orchstr->body);
      //    csound_print_preextra(&qq);
      cs_init_math_constants_macros(csound, &qq);
      cs_init_omacros(csound, &qq, csound->omacros);
      //    csound_print_preextra(&qq);
      csound_prelex(csound, qq.yyscanner);
      if (UNLIKELY(qq.ifdefStack != NULL)) {
        csound->Message(csound, Str("Unmatched #ifdef\n"));
        csound->LongJmp(csound, 1);
      }
      csound_prelex_destroy(qq.yyscanner);
      csound->DebugMsg(csound, "yielding >>%s<<\n",
                       corfile_body(csound->expanded_orc));
      corfile_rm(&csound->orchstr);

    }
    {
      /* VL 15.3.2015 allocating memory here will cause
         unwanted growth.
         We just pass a pointer, which will be allocated
         by make leaf */
      TREE* astTree = NULL;// = (TREE *)csound->Calloc(csound, sizeof(TREE));
      TREE* newRoot;
      PARSE_PARM  pp;
      TYPE_TABLE* typeTable = NULL;

      /* Parse */
      memset(&pp, '\0', sizeof(PARSE_PARM));
      init_symbtab(csound);

      csound_orcdebug = O->odebug;
      csound_orclex_init(&pp.yyscanner);


      csound_orcset_extra(&pp, pp.yyscanner);
      csound_orc_scan_buffer(corfile_body(csound->expanded_orc),
                             corfile_tell(csound->expanded_orc), pp.yyscanner);

      //csound_orcset_lineno(csound->orcLineOffset, pp.yyscanner);
      //printf("%p \n", astTree);
      err = csound_orcparse(&pp, pp.yyscanner, csound, &astTree);
      //printf("%p \n", astTree);
      // print_tree(csound, "AST - AFTER csound_orcparse()\n", astTree);
      //csp_orc_sa_cleanup(csound);
      corfile_rm(&csound->expanded_orc);

      if (csound->synterrcnt) err = 3;
      if (LIKELY(err == 0)) {
        if(csound->oparms->odebug) csound->Message(csound,
                                                   Str("Parsing successful!\n"));
      }
      else {
        if (err == 1){
          csound->Message(csound, Str("Parsing failed due to invalid input!\n"));
        }
        else if (err == 2){
          csound->Message(csound,
                          Str("Parsing failed due to memory exhaustion!\n"));
        }
        else if (err == 3){
          csound->Message(csound, Str("Parsing failed due to %d syntax error%s!\n"),
                          csound->synterrcnt, csound->synterrcnt==1?"":"s");
        }
        goto ending;
      }
       if (UNLIKELY(PARSER_DEBUG)) {
        print_tree(csound, "AST - INITIAL\n", astTree);
       }
       //print_tree(csound, "AST - INITIAL\n", astTree);

      typeTable = csound->Malloc(csound, sizeof(TYPE_TABLE));
      typeTable->udos = NULL;

      typeTable->globalPool = csoundCreateVarPool(csound);
      typeTable->instr0LocalPool = csoundCreateVarPool(csound);

      typeTable->localPool = typeTable->instr0LocalPool;
      typeTable->labelList = NULL;

      /**** THIS NEXT LINE IS WRONG AS err IS int WHILE FN RETURNS TREE* ****/
      astTree = verify_tree(csound, astTree, typeTable);
//      csound->Free(csound, typeTable->instr0LocalPool);
//      csound->Free(csound, typeTable->globalPool);
//      csound->Free(csound, typeTable);
      //print_tree(csound, "AST - FOLDED\n", astTree);

      //FIXME - synterrcnt should not be global
      if (astTree == NULL || csound->synterrcnt){
          err = 3;
          if (astTree)
            csound->Message(csound,
                            Str("Parsing failed due to %d semantic error%s!\n"),
                            csound->synterrcnt, csound->synterrcnt==1?"":"s");
          else if (csound->synterrcnt)
            csound->Message(csound, Str("Parsing failed to syntax errors\n"));
          else
            csound->Message(csound, Str("Parsing failed due no input!\n"));
          goto ending;
      }
      err = 0;

      //csp_orc_analyze_tree(csound, astTree);

//      astTree = csound_orc_expand_expressions(csound, astTree);
//
      if (UNLIKELY(PARSER_DEBUG)) {
        print_tree(csound, "AST - AFTER VERIFICATION/EXPANSION\n", astTree);
        }

    ending:
      csound_orclex_destroy(pp.yyscanner);
      if (err) {
        csound->ErrorMsg(csound, Str("Stopping on parser failure"));
        csoundDeleteTree(csound, astTree);
        if (typeTable != NULL) {
          csoundFreeVarPool(csound, typeTable->globalPool);
          if(typeTable->instr0LocalPool != NULL) {
            csoundFreeVarPool(csound, typeTable->instr0LocalPool);
          }
          if(typeTable->localPool != typeTable->instr0LocalPool) {
            csoundFreeVarPool(csound, typeTable->localPool);
          }
          csound->Free(csound, typeTable);
        }
        return NULL;
      }

      astTree = csound_orc_optimize(csound, astTree);

      // small hack: use an extra node as head of tree list to hold the
      // typeTable, to be used during compilation
      newRoot = make_leaf(csound, 0, 0, 0, NULL);
      newRoot->markup = typeTable;
      newRoot->next = astTree;

      /* if(str!=NULL){ */
      /*        if (typeTable != NULL) { */
      /*     csoundFreeVarPool(csound, typeTable->globalPool); */
      /*     if(typeTable->instr0LocalPool != NULL) { */
      /*       csoundFreeVarPool(csound, typeTable->instr0LocalPool); */
      /*     } */
      /*     if(typeTable->localPool != typeTable->instr0LocalPool) { */
      /*       csoundFreeVarPool(csound, typeTable->localPool); */
      /*     } */
      /*     csound->Free(csound, typeTable); */
      /*   } */
      /* } */

      return newRoot;
    }
}
示例#3
0
文件: main.c 项目: BlakeJarvis/csound
PUBLIC int csoundCompileArgs(CSOUND *csound, int argc, char **argv)
{
    OPARMS  *O = csound->oparms;
    char    *s;
    FILE    *xfile = NULL;
    int     n;
    int     csdFound = 0;
    char    *fileDir;


    if ((n = setjmp(csound->exitjmp)) != 0) {
      return ((n - CSOUND_EXITJMP_SUCCESS) | CSOUND_EXITJMP_SUCCESS);
    }

    if(csound->engineStatus & CS_STATE_COMP){
      csound->Message(csound, Str("Csound is already started, call csoundReset()\n"
                                  "before starting again \n"));
       return CSOUND_ERROR;
    }

    if (--argc <= 0) {
      dieu(csound, Str("insufficient arguments"));
    }
    /* command line: allow orc/sco/csd name */
    csound->orcname_mode = 0;   /* 0: normal, 1: ignore, 2: fail */
    if (argdecode(csound, argc, argv) == 0)
      csound->LongJmp(csound, 1);
    /* do not allow orc/sco/csd name in .csound6rc */
    csound->orcname_mode = 2;
    checkOptions(csound);
    if (csound->delayederrormessages) {
      if (O->msglevel>8)
        csound->Warning(csound, csound->delayederrormessages);
      free(csound->delayederrormessages);
      csound->delayederrormessages = NULL;
    }
    /* check for CSD file */
    if (csound->orchname == NULL) {
      if(csound->info_message_request) {
        csound->info_message_request = 0;
        csound->LongJmp(csound, 1);
      }
      else if(csound->oparms->daemon == 0)
         dieu(csound, Str("no orchestra name"));

    }
    else if (csound->use_only_orchfile == 0
             && (csound->scorename == NULL || csound->scorename[0] == (char) 0)
             && csound->orchname[0] != '\0') {
      /* FIXME: allow orc/sco/csd name in CSD file: does this work ? */
      csound->orcname_mode = 0;
      csound->Message(csound, "UnifiedCSD:  %s\n", csound->orchname);

      /* Add directory of CSD file to search paths before orchname gets
       * replaced with temp orch name if default paths is enabled */
      if (!O->noDefaultPaths) {
        fileDir = csoundGetDirectoryForPath(csound, csound->orchname);
        csoundAppendEnv(csound, "SADIR", fileDir);
        csoundAppendEnv(csound, "SSDIR", fileDir);
        csoundAppendEnv(csound, "INCDIR", fileDir);
        csoundAppendEnv(csound, "MFDIR", fileDir);
        csound->Free(csound, fileDir);
      }

      if(csound->orchname != NULL) {
      csound->csdname = csound->orchname; /* save original CSD name */
      if (!read_unified_file(csound, &(csound->orchname),
                                       &(csound->scorename))) {
        csound->Die(csound, Str("Reading CSD failed ... stopping"));
      }
      csdFound = 1;
      }
    }

    /* IV - Feb 19 2005: run a second pass of argdecode so that */
    /* command line options override CSD options */
    /* this assumes that argdecode is safe to run multiple times */
    csound->orcname_mode = 1;           /* ignore orc/sco name */
    argdecode(csound, argc, argv);      /* should not fail this time */
    /* some error checking */
    if (csound->stdin_assign_flg &&
        (csound->stdin_assign_flg & (csound->stdin_assign_flg - 1)) != 0) {
      csound->Die(csound, Str("error: multiple uses of stdin"));
    }
    if (csound->stdout_assign_flg &&
        (csound->stdout_assign_flg & (csound->stdout_assign_flg - 1)) != 0) {
      csound->Die(csound, Str("error: multiple uses of stdout"));
    }
    /* done parsing csound6rc, CSD, and command line options */

    if (csound->scorename == NULL && csound->scorestr==NULL) {
      /* No scorename yet */
      csound->scorestr = corfile_create_r("f0 800000000000.0\n");
      corfile_flush(csound->scorestr);
      if (O->RTevents)
        csound->Message(csound, Str("realtime performance using dummy "
                                    "numeric scorefile\n"));
    }
    else if (!csdFound && !O->noDefaultPaths){
      /* Add directory of SCO file to search paths*/
      fileDir = csoundGetDirectoryForPath(csound, csound->scorename);
      csoundAppendEnv(csound, "SADIR", fileDir);
      csoundAppendEnv(csound, "SSDIR", fileDir);
      csoundAppendEnv(csound, "MFDIR", fileDir);
      csound->Free(csound, fileDir);
    }

    /* Add directory of ORC file to search paths*/
    if (!csdFound && !O->noDefaultPaths) {
      fileDir = csoundGetDirectoryForPath(csound, csound->orchname);
      csoundAppendEnv(csound, "SADIR", fileDir);
      csoundAppendEnv(csound, "SSDIR", fileDir);
      csoundAppendEnv(csound, "MFDIR", fileDir);
      csound->Free(csound, fileDir);
    }

    if (csound->orchstr==NULL && csound->orchname) {
      /*  does not deal with search paths */
      csound->Message(csound, Str("orchname:  %s\n"), csound->orchname);
      csound->orchstr = copy_to_corefile(csound, csound->orchname, NULL, 0);
      if (csound->orchstr==NULL)
        csound->Die(csound,
                    Str("Failed to open input file - %s\n"), csound->orchname);
      corfile_puts("\n#exit\n", csound->orchstr);
      corfile_putc('\0', csound->orchstr);
      corfile_putc('\0', csound->orchstr);
      //csound->orchname = NULL;
    }
    if (csound->xfilename != NULL)
      csound->Message(csound, "xfilename: %s\n", csound->xfilename);

    csoundLoadExternals(csound);    /* load plugin opcodes */
     /* VL: added this also to csoundReset() in csound.c   */
    if (csoundInitModules(csound) != 0)
      csound->LongJmp(csound, 1);
     if(csoundCompileOrc(csound, NULL) != 0){
       if(csound->oparms->daemon == 0)
         csoundDie(csound, Str("cannot compile orchestra"));
       else {
         /* VL -- 21-10-13 Csound does not need to die on
          failure to compile. It can carry on, because new
          instruments can be compiled again */
       csound->Warning(csound, Str("cannot compile orchestra."));
       csound->Warning(csound, Str("Csound will start with no instruments"));
       }
     }
     csound->modules_loaded = 1;

    s = csoundQueryGlobalVariable(csound, "_RTMIDI");
    if (csound->enableHostImplementedMIDIIO == 1) {
        if (s == NULL) {
          s = "hostbased";
        } else {
            strcpy(s, "hostbased");
        }
        csoundSetConfigurationVariable(csound,"rtmidi", s);
    }

    /* IV - Jan 28 2005 */
    print_benchmark_info(csound, Str("end of orchestra compile"));
    if (!csoundYield(csound))
      return -1;
    /* IV - Oct 31 2002: now we can read and sort the score */
    if (csound->scorename != NULL &&
        (n = strlen(csound->scorename)) > 4 &&  /* if score ?.srt or ?.xtr */
        (!strcmp(csound->scorename + (n - 4), ".srt") ||
         !strcmp(csound->scorename + (n - 4), ".xtr"))) {
      csound->Message(csound, Str("using previous %s\n"), csound->scorename);
      //playscore = sortedscore = csound->scorename;   /*  use that one */
      csound->scorestr = NULL;
      csound->scorestr = copy_to_corefile(csound, csound->scorename, NULL, 1);
    }
    else {
      //sortedscore = NULL;
      if (csound->scorestr==NULL) {
        csound->scorestr = copy_to_corefile(csound, csound->scorename, NULL, 1);
        if (csound->scorestr==NULL)
          csoundDie(csound, Str("cannot open scorefile %s"), csound->scorename);
      }
      csound->Message(csound, Str("sorting score ...\n"));
      scsortstr(csound, csound->scorestr);
      if (csound->keep_tmp) {
        FILE *ff = fopen("score.srt", "w");
        fputs(corfile_body(csound->scstr), ff);
        fclose(ff);
      }
    }
    if (csound->xfilename != NULL) {            /* optionally extract */
      if (!(xfile = fopen(csound->xfilename, "r")))
        csoundDie(csound, Str("cannot open extract file %s"),csound->xfilename);
      csoundNotifyFileOpened(csound, csound->xfilename,
                             CSFTYPE_EXTRACT_PARMS, 0, 0);
      csound->Message(csound, Str("  ... extracting ...\n"));
      scxtract(csound, csound->scstr, xfile);
      fclose(xfile);
      csound->tempStatus &= ~csPlayScoMask;
    }
    csound->Message(csound, Str("\t... done\n"));
    /* copy sorted score name */
    O->playscore = csound->scstr;
    /* IV - Jan 28 2005 */
    print_benchmark_info(csound, Str("end of score sort"));
    if (O->syntaxCheckOnly) {
      csound->Message(csound, Str("Syntax check completed.\n"));
      return CSOUND_EXITJMP_SUCCESS;
    }

    /* open MIDI output (moved here from argdecode) */
    if (O->Midioutname != NULL && O->Midioutname[0] == (char) '\0')
      O->Midioutname = NULL;
    if (O->FMidioutname != NULL && O->FMidioutname[0] == (char) '\0')
      O->FMidioutname = NULL;
    if (O->Midioutname != NULL || O->FMidioutname != NULL)
      openMIDIout(csound);

    return CSOUND_SUCCESS;
}