Esempio n. 1
0
int CBuffer::writestring(char *str)
{
	int len = writechars(str);
	return len + writebyte('\0');
}
Esempio n. 2
0
int
loaddefs(int fromfile, char *fname, int mode)
{
  nialptr     ts;
  int         repeatloop,
              keepreading,
              nolines,
              inremark,
              linecnt;
  FILE       *f1 = NULL;     /* initialized to avoid complaint */
  int         errorsfound;

  if (fromfile) {
    f1 = openfile(fname, 'r', 't');
    if (f1 == OPENFAILED)
      return (false);
    pushsysfile(f1);
  }
  /* a loaddefs always affects the global environment. We reset current_env to
     relect this.  The code to restore the environment is below. This must be
     saved on the stack, otherwise it can get thrown away since it may only
     be owned by a transient definition value. The following example failed
     before I protected this on the stack: retry is { host 'vi bug.ndf';
     loaddefs"bug l } where this definition was in the file bug.ndf. */

  apush(current_env);
  current_env = Null;
  ts = topstack;             /* to monitor stack growth on each action */
  errorsfound = 0;           /* reset parse error counter */
  repeatloop = true;
  linecnt = 0;

  /* loop to pick up groups of lines */
  while (repeatloop) {      
    /* continue as long as their are line groups */

    /* test on each circuit if an interrupt signal has been posted */
#ifdef USER_BREAK_FLAG
    if (fromfile)
      checksignal(NC_CS_NORMAL);
#endif

    inremark = false;
    nolines = 0;
    keepreading = true;

    /* loop to pick up lines until a whitespace line occurs */
    while (keepreading) {
      if (fromfile) {
        /* reading a line from the file */
        readfileline(f1, (mode ? 2 : 0)); /* mode==2 only in a loaddefs */

        /* readfileline places result on the stack */
        if (top == Eoffault) {
          apop();            /* to remove the end of file marker */
          repeatloop = false;
          break;             /* to end read loop */
        }
      }

      else {
        /* select a line from array defsndf loadded from defstbl.h */
        char       *line;

        line = defsndf[linecnt++];
        if (linecnt == NOLINES) {
          repeatloop = false;
          keepreading = false;  /* to end read loop */
        }
        mkstring(line);      /* convert the line to a Nial string and push it */
      }

      if (nolines == 0) {    /* check first line of group for a remark */
        char        firstchar;
        int         i = 0;

        /* loop to skip blanks */
        while (i < tally(top) && fetch_char(top, i) <= BLANK)
          i++;

        /* note whether first char is "#" */
        firstchar = fetch_char(top, i);
        if (tally(top))
          inremark = firstchar == HASHSYMBOL;
        else
          inremark = false;
      }

      /* if the line is all while space then we are at the end of a group */
      if (top == Null || allwhitespace(pfirstchar(top))) {
        keepreading = false;
        freeup(apop());      /* to get rid of the empty line */
      }
      else                   /* count the line on the stack */
        nolines++;
    }

    /* we have a group of lines to process */
    if (nolines > 0) {
      mklist(nolines);       /* create a list of lines  and link them*/
      ilink(); 
      if (inremark) {
        freeup(apop()); /* remarks are ignored */
      }                      
      else 
      {                 
        /* carry out the actions of the main loop */
        iscan();
        parse(true);

        /* check whether parse produced an error */
        if (kind(top) == faulttype) {
          if (top != Nullexpr) {
            errorsfound++;
            if (mode == 0) { /* show error message */
              apush(top);
              ipicture();
              show(apop());
            }
          }
        }

        /* evaluate the parse tree, if it is a fault, it is the value returned */
        ieval();

#ifdef DEBUG
        memchk();
#endif

        if (mode) {  /* show the result */
          if (top != Nullexpr) {
            ipicture();
            show(apop());
          }
          else
            apop();          /* the Nullexpr */
        }
        else
          freeup(apop());    /* free because it might not be Nullexpr */
      }

      if (mode) {            /* now display empty line */
        writechars(STDOUT, "", (nialint) 0, true);
        if (keeplog && f1 == STDIN)
          writelog("", 0, true);
      }
    }
    /* check that the stack hasn't grown */
    if (ts != topstack) {
      while (ts != topstack)
        freeup(apop());
      exit_cover(NC_STACK_GROWN_I);
    }
  } 

  /* done reading groups of lines */
  if (fromfile) {
    closefile(f1);
    popsysfile();
  }

  /* restore the current_env */
  current_env = apop();
  if (errorsfound > 0)
    nprintf(OF_NORMAL_LOG, "errors found: %d\n", errorsfound);
  return (true);
}
Esempio n. 3
0
void
iprofile()
{
  char        sbuf[1000];
  nialptr     z;
  FILE       *prf = STDOUT;
  int         c1,
              c2;
  int         i;
  double      real_total_time = 0;
  double      profile_duration_time;
  int         NMWIDTH = 30;

  /* grab the argument */
  z = apop();

  if (newprofile) {
    buildfault("no profile available");
    freeup(z);
    return;
  }
  /* handle input argument */
  if (kind(z) == phrasetype) {
    apush(z);
    istring();
    z = apop();
  }
  if (tally(z) != 0) {
    if (!istext(z)) {
      buildfault("profile file name arg is not text");
      freeup(z);
      return;
    }
    else {
      prf = openfile(pfirstchar(z), 'w', 't');
      if (prf == OPENFAILED) {
        buildfault("unable to open specified file to write profile to");
        freeup(z);
        return;
        /* exit_cover(NC_PROFILE_FILE_W); removed Nov 22/95 because this
         * cleared profile information */
      }
    }
  }
  freeup(z);

  /* If profiling is still on, then turn it off */
  if (profile == true) {
    apush(createbool(0));
    isetprofile();
    apop();
  }

#ifndef OLD_BUILD_SYMBOL_TABLE
  if (symtab) 
    free_symtab();
  symtab = NULL;
  build_symbol_table();
#endif

  profile_duration_time = (calltree->total_time);
  for (i = 0;i <calltree->num_children;i++) {
    real_total_time += calltree->children[i]->total_time;
  }
  /* traverse the call tree placing nodes in the symbol table entries */
  if (!traversed) {
    traverse_tree(calltree);
    traversed = true;
  }
  /* generate the output and place it in the output file or stdout */

  /* if a filename has been specified then write that out */
  if (tally(z)) {
    sprintf(sbuf,"Profile output file: \"%s\"\n\n",pfirstchar(z));
    writechars(prf, sbuf, strlen(sbuf), false);
  }

  sprintf(sbuf, "\nTotal execution time of profile session: \t%f\n", profile_duration_time);
  writechars(prf, sbuf, strlen(sbuf), false);
  sprintf(sbuf, "Total execution time in top level calls:  \t%f\n\n",real_total_time);
  writechars(prf, sbuf, strlen(sbuf), false);

  /* header line for all data */
  sprintf(sbuf, "op name[.tr arg]                 calls[rec]    ");
  writechars(prf, sbuf, strlen(sbuf), false);
  sprintf(sbuf, "time time/call  %% time\n");
  writechars(prf, sbuf, strlen(sbuf), false);

  for (c1 = 0; c1 < symtabsize; c1++) {
    if ((symtab[c1]->num_locs > 0) || (symtab[c1]->num_rcalls > 0)) {
      double      totaloptime = 0;
      int         totalopcalls = 0;
      int         totalropcalls;
      char       *tmp;

      for (c2 = 0; c2 < symtab[c1]->num_locs; c2++) {
        if (symtab[c1]->id != symtab[c1]->locations[c2]->parent->opid)
          /* omit adding calls and time for direct recursions */
        {
          totaloptime += symtab[c1]->locations[c2]->total_time;
          totalopcalls += symtab[c1]->locations[c2]->total_calls;
        }
      }
      totalropcalls = symtab[c1]->num_rcalls;
      sprintf(sbuf, "%s%5d", (tmp = padright(NMWIDTH, (char *) symtab[c1]->name)),
              totalopcalls);
      writechars(prf, sbuf, strlen(sbuf), false);
      free(tmp);
      if (totalropcalls != 0) {
        sprintf(sbuf, "[%5d]", totalropcalls);
        writechars(prf, sbuf, strlen(sbuf), false);
      }
      else {
        sprintf(sbuf, "       ");
        writechars(prf, sbuf, strlen(sbuf), false);
      }
      /* details for each definition */
      sprintf(sbuf, "%8.2f %8.4f %8.1f%s\n",
              totaloptime,
              (totaloptime / totalopcalls),
              100 * (totaloptime / real_total_time),
              ((symtab[c1]->toplevel_call == true)?"<":""));
      writechars(prf, sbuf, strlen(sbuf), false);

      {
        struct node **chlist;
        int         c,
                    used;
        char        tname[40];

        chlist = merge_children(symtab[c1], &used);
        for (c = 0; c < used; c++) {
          char       *tmp;

          if (chlist[c]->opid == symtab[c1]->id)  /* recursions only counted */
            break;
          strcpy(tname, num_to_name(chlist[c]->opid));
          /* details for each definition it calls */
          if (chlist[c]->total_time > 0.0)
            sprintf(sbuf, " %s%5d       %8.2f %8.4f %8.2f\n",
                    (tmp = padright(NMWIDTH - 1, tname)),
                    chlist[c]->total_calls,
                    chlist[c]->total_time,
                    chlist[c]->total_time / chlist[c]->total_calls,
                    100 * (chlist[c]->total_time / totaloptime));
          else
            sprintf(sbuf, " %s%5d       %8.2f %8.4f %8.2f\n",
                    (tmp = padright(NMWIDTH - 1, tname)),
                    chlist[c]->total_calls,
                    chlist[c]->total_time,
                    0.0, 0.0);
          writechars(prf, sbuf, strlen(sbuf), false);
          free(tmp);
        }
        free_merge_list(chlist, used);
      }
      sprintf(sbuf, "\n");
      writechars(prf, sbuf, strlen(sbuf), false);
    }
  }
  if (prf != STDOUT)
    closefile(prf);
  apush(Nullexpr);
  return;
}