Beispiel #1
0
/*
** Like strtok, but allows empty "cells".  E.g. if there are consecutive
** tabs in a tab-delimited file, this will return an empty string for
** the empty cell.  strtok() won't do that.
*/
char *wstrtok(char *s,char *t)

    {
    static char buf[1024];
    static char buf2[1024];
    static char emptystr[16];
    static int  len;
    int j,i;

    emptystr[0]='\0';
    if (s!=NULL)
        {
        strcpy(buf,s);
        len=strlen(buf);
        i=0;
        }
    else
        i=strlen(buf)+1;
    if (i>=len)
        return(emptystr);
    j=i;
    if (i>0)
        buf[i-1]=t[0];
    for (;buf[i]!='\0' && buf[i]!=t[0];i++);
    buf[i]='\0';
    strcpy(buf2,&buf[j]);
    clean_line(buf2);
    return(buf2);
    }
Beispiel #2
0
Datei: del_c.c Projekt: kedric/42
void	put_stradd(t_term *term, char *buff)
{
	int		i;
	int		j;
	char	tmp[BUFF_SIZE];

	i = 0;
	j = 0;
	ft_bzero(tmp, BUFF_SIZE);
	ft_strcpy(tmp, term->hist->buff);
	term->hist->len++;
	clean_line(term);
	while (i < term->hist->len)
	{
		if (i == term->hist->pose_cursor)
			term->hist->buff[i++] = buff[0];
		term->hist->buff[i++] = tmp[j++];
	}
	i = term->hist->len;
	ft_putstr(term->hist->buff);
	while (i - 1 != term->hist->pose_cursor)
	{
		ft_putstr_fd(term->key[4], term->fd);
		i--;
	}
	term->hist->pose_cursor++;
	term->hist->modif = 1;
}
Beispiel #3
0
Datei: del_c.c Projekt: kedric/42
void	put_strdel(t_term *term)
{
	int	i;
	int	j;

	i = 0;
	j = 0;
	clean_line(term);
	if (i < term->hist->len)
	{
		while (i < term->hist->len)
		{
			if (i == term->hist->pose_cursor - 1)
			{
				rm_special_char(term, term->hist->buff[j]);
				j++;
			}
			term->hist->buff[i++] = term->hist->buff[j++];
		}
		term->hist->len--;
		term->hist->pose_cursor--;
		ft_putstr(term->hist->buff);
		i = term->hist->len + 1;
		while (--i != term->hist->pose_cursor)
			ft_putstr_fd(term->key[4], term->fd);
		term->hist->modif = 1;
	}
}
Beispiel #4
0
/*
** Parses most date/time formats.  The key assumption is that the time
** part of the string is HH:MM[:SS.SS][am|pm] (i.e. has colons and no spaces,
** though it can have a space before the AM or PM.)
*/
int structtm_from_datetime(struct tm *date,char *datetime)

    {
    char buf[64];
    char time[64];
    int  i,p,i0,status;
    struct tm dt;

    date->tm_year=0;
    date->tm_mon=0;
    date->tm_mday=1;
    date->tm_hour=0;
    date->tm_min=0;
    date->tm_sec=0;

    /* Make temp copy of string */
    strncpy(buf,datetime,63);
    buf[63]='\0';

    /* Clean up string a bit */
    for (i=0;buf[i]!='\0';i++)
        if (buf[i]=='\t' || buf[i]==',')
            buf[i]=' ';
    clean_line(buf);

    /* First check for " am" or " pm" and remove preceding space if found. */
    while (1)
        {
        p=in_string(buf," am");
        if (p<0)
            p=in_string(buf," pm");
        if (p>=0)
            memmove(&buf[p],&buf[p+1],strlen(buf)-p);
        else
            break;
        }

    /* Now find string with colons (time string) */
    for (i=0;buf[i]!='\0' && buf[i]!=':';i++);
    /* If no colons found, assume it's a date only */
    if (buf[i]!=':')
        return(structtm_from_date(date,datetime));
    /* Put time into "time" */
    for (;i>=0 && buf[i]!=' ';i--);
    for (i++,i0=i;buf[i]!=' ' && buf[i]!='\0';i++)
        time[i-i0]=buf[i];
    time[i-i0]='\0';
    /* Remove time string from buf */
    memmove(&buf[i0-1],&buf[i],strlen(buf)-i+1);
    /* Parse time string */
    structtm_from_time(date,time);
    /* Parse date string */
    dt=(*date);
    status=structtm_from_date(date,buf);
    date->tm_hour = dt.tm_hour;
    date->tm_min = dt.tm_min;
    date->tm_sec = dt.tm_sec;
    return(status);
    }
Beispiel #5
0
// clean to end of screen
void clean_to_eos(termstate_t *term)
{
    int y;
    clean_to_eol(term);
    if (term->cur_y + 1 < term->phys_h)
        for (y = term->cur_y + 1; y < term->phys_h; y++)
            clean_line(term,  y);
}
Beispiel #6
0
// clean from start of screen
void clean_from_sos(termstate_t *term)
{
    int y;
    clean_from_sol(term);
    if (term->cur_y + 1 < term->phys_h)
        for (y = 0; y < term->cur_y; y++)
            clean_line(term,  y);
}
bool read_line(const std::vector<std::string> & lines,
               const unsigned int row, const unsigned int nrows,
               std::vector<int> & r, std::vector<int> & g, std::vector<int> & b) {
  // ARToolKit files are encoded in BGR
  // seen thanks to samples obtained on http://flash.tarotaro.org/blog/2009/07/12/mgo2/
  std::string bline = lines[row], gline = lines[row+nrows], rline = lines[row+2*nrows];
  clean_line(rline);
  clean_line(gline);
  clean_line(bline);
  StringUtils::StringSplit_<int>(rline, " ", &r);
  StringUtils::StringSplit_<int>(gline, " ", &g);
  StringUtils::StringSplit_<int>(bline, " ", &b);
  if (r.size() != nrows || g.size() != nrows || b.size() != nrows) {
    printf("Nb of values for row %i: r:%i, g:%i b:%i,  and not %i!\n",
           row, r.size(), g.size(), b.size(), nrows);
    return false;
  }
  return true;
} // end read_line()
Beispiel #8
0
bool read_param_file(char* fname)
/* returns FALSE if problems occurred */
{
  FILE *fp;
  char* line;

  if (param != 0)
    free_params();
  n_params = 0;
  lineno = 0;
  
  if ((fp = fopen(fname, "r")) == NULL) {
    fprintf(stderr, "error opening param file %s\n", fname);
    return 0;
  }
  
  while (line = read_line(fp)) {
    line = clean_line(line);
    if (*line) {
      char* p = line;
      while (!isspace(*p) && *p != '\0') p++;
      /* from line to p is the name of the param */
      if (*p == '\0')
	add_param(line, "");
      else {
	*p = '\0';
	p++;
	while (isspace(*p)) p++;
	/* from p on is the value; or '\0' if no value was specified */
	add_param(line, p);
      }
    }
  }
  fclose(fp);
  return 1;
}
Beispiel #9
0
static void
clean_info_bar(void)
{
    clean_line(MAX_Y - 0);
    clean_line(MAX_Y - 1);
}
Beispiel #10
0
/* Convert the Un*x-ish style directory listing stored in FILE to a
   linked list of fileinfo (system-independent) entries.  The contents
   of FILE are considered to be produced by the standard Unix `ls -la'
   output (whatever that might be).  BSD (no group) and SYSV (with
   group) listings are handled.

   The time stamps are stored in a separate variable, time_t
   compatible (I hope).  The timezones are ignored.  */
static struct fileinfo *
ftp_parse_unix_ls (const char *file, int ignore_perms)
{
  FILE *fp;
  static const char *months[] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  int next, len, i, error, ignore;
  int year, month, day;         /* for time analysis */
  int hour, min, sec, ptype;
  struct tm timestruct, *tnow;
  time_t timenow;
  size_t bufsize = 0;

  char *line = NULL, *tok, *ptok;      /* tokenizer */
  struct fileinfo *dir, *l, cur; /* list creation */

  fp = fopen (file, "rb");
  if (!fp)
    {
      logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
      return NULL;
    }
  dir = l = NULL;

  /* Line loop to end of file: */
  while ((len = getline (&line, &bufsize, fp)) > 0)
    {
      len = clean_line (line, len);
      /* Skip if total...  */
      if (!strncasecmp (line, "total", 5))
        continue;
      /* Get the first token (permissions).  */
      tok = strtok (line, " ");
      if (!tok)
        continue;

      cur.name = NULL;
      cur.linkto = NULL;

      /* Decide whether we deal with a file or a directory.  */
      switch (*tok)
        {
        case '-':
          cur.type = FT_PLAINFILE;
          DEBUGP (("PLAINFILE; "));
          break;
        case 'd':
          cur.type = FT_DIRECTORY;
          DEBUGP (("DIRECTORY; "));
          break;
        case 'l':
          cur.type = FT_SYMLINK;
          DEBUGP (("SYMLINK; "));
          break;
        default:
          cur.type = FT_UNKNOWN;
          DEBUGP (("UNKNOWN; "));
          break;
        }

      if (ignore_perms)
        {
          switch (cur.type)
            {
            case FT_PLAINFILE:
              cur.perms = 0644;
              break;
            case FT_DIRECTORY:
              cur.perms = 0755;
              break;
            default:
              /*cur.perms = 1023;*/     /* #### What is this?  --hniksic */
              cur.perms = 0644;
            }
          DEBUGP (("implicit perms %0o; ", cur.perms));
        }
       else
         {
           cur.perms = symperms (tok + 1);
           DEBUGP (("perms %0o; ", cur.perms));
         }

      error = ignore = 0;       /* Erroneous and ignoring entries are
                                   treated equally for now.  */
      year = hour = min = sec = 0; /* Silence the compiler.  */
      month = day = 0;
      ptype = TT_DAY;
      next = -1;
      /* While there are tokens on the line, parse them.  Next is the
         number of tokens left until the filename.

         Use the month-name token as the "anchor" (the place where the
         position wrt the file name is "known").  When a month name is
         encountered, `next' is set to 5.  Also, the preceding
         characters are parsed to get the file size.

         This tactic is quite dubious when it comes to
         internationalization issues (non-English month names), but it
         works for now.  */
      tok = line;
      while (ptok = tok,
             (tok = strtok (NULL, " ")) != NULL)
        {
          --next;
          if (next < 0)         /* a month name was not encountered */
            {
              for (i = 0; i < 12; i++)
                if (!strcasecmp (tok, months[i]))
                  break;
              /* If we got a month, it means the token before it is the
                 size, and the filename is three tokens away.  */
              if (i != 12)
                {
                  wgint size;

                  /* Parse the previous token with str_to_wgint.  */
                  if (ptok == line)
                    {
                      /* Something has gone wrong during parsing. */
                      error = 1;
                      break;
                    }
                  errno = 0;
                  size = str_to_wgint (ptok, NULL, 10);
                  if (size == WGINT_MAX && errno == ERANGE)
                    /* Out of range -- ignore the size.  #### Should
                       we refuse to start the download.  */
                    cur.size = 0;
                  else
                    cur.size = size;
                  DEBUGP (("size: %s; ", number_to_static_string(cur.size)));

                  month = i;
                  next = 5;
                  DEBUGP (("month: %s; ", months[month]));
                }
            }
          else if (next == 4)   /* days */
            {
              if (tok[1])       /* two-digit... */
                day = 10 * (*tok - '0') + tok[1] - '0';
              else              /* ...or one-digit */
                day = *tok - '0';
              DEBUGP (("day: %d; ", day));
            }
          else if (next == 3)
            {
              /* This ought to be either the time, or the year.  Let's
                 be flexible!

                 If we have a number x, it's a year.  If we have x:y,
                 it's hours and minutes.  If we have x:y:z, z are
                 seconds.  */
              year = 0;
              min = hour = sec = 0;
              /* We must deal with digits.  */
              if (c_isdigit (*tok))
                {
                  /* Suppose it's year.  */
                  for (; c_isdigit (*tok); tok++)
                    year = (*tok - '0') + 10 * year;
                  if (*tok == ':')
                    {
                      /* This means these were hours!  */
                      hour = year;
                      year = 0;
                      ptype = TT_HOUR_MIN;
                      ++tok;
                      /* Get the minutes...  */
                      for (; c_isdigit (*tok); tok++)
                        min = (*tok - '0') + 10 * min;
                      if (*tok == ':')
                        {
                          /* ...and the seconds.  */
                          ++tok;
                          for (; c_isdigit (*tok); tok++)
                            sec = (*tok - '0') + 10 * sec;
                        }
                    }
                }
              if (year)
                DEBUGP (("year: %d (no tm); ", year));
              else
                DEBUGP (("time: %02d:%02d:%02d (no yr); ", hour, min, sec));
            }
          else if (next == 2)    /* The file name */
            {
              int fnlen;
              char *p;

              /* Since the file name may contain a SPC, it is possible
                 for strtok to handle it wrong.  */
              fnlen = strlen (tok);
              if (fnlen < len - (tok - line))
                {
                  /* So we have a SPC in the file name.  Restore the
                     original.  */
                  tok[fnlen] = ' ';
                  /* If the file is a symbolic link, it should have a
                     ` -> ' somewhere.  */
                  if (cur.type == FT_SYMLINK)
                    {
                      p = strstr (tok, " -> ");
                      if (!p)
                        {
                          error = 1;
                          break;
                        }
                      cur.linkto = xstrdup (p + 4);
                      DEBUGP (("link to: %s\n", cur.linkto));
                      /* And separate it from the file name.  */
                      *p = '\0';
                    }
                }
              /* If we have the filename, add it to the list of files or
                 directories.  */
              /* "." and ".." are an exception!  */
              if (!strcmp (tok, ".") || !strcmp (tok, ".."))
                {
                  DEBUGP (("\nIgnoring `.' and `..'; "));
                  ignore = 1;
                  break;
                }
              /* Some FTP sites choose to have ls -F as their default
                 LIST output, which marks the symlinks with a trailing
                 `@', directory names with a trailing `/' and
                 executables with a trailing `*'.  This is no problem
                 unless encountering a symbolic link ending with `@',
                 or an executable ending with `*' on a server without
                 default -F output.  I believe these cases are very
                 rare.  */
              fnlen = strlen (tok); /* re-calculate `fnlen' */
              cur.name = xmalloc (fnlen + 1);
              memcpy (cur.name, tok, fnlen + 1);
              if (fnlen)
                {
                  if (cur.type == FT_DIRECTORY && cur.name[fnlen - 1] == '/')
                    {
                      cur.name[fnlen - 1] = '\0';
                      DEBUGP (("trailing `/' on dir.\n"));
                    }
                  else if (cur.type == FT_SYMLINK && cur.name[fnlen - 1] == '@')
                    {
                      cur.name[fnlen - 1] = '\0';
                      DEBUGP (("trailing `@' on link.\n"));
                    }
                  else if (cur.type == FT_PLAINFILE
                           && (cur.perms & 0111)
                           && cur.name[fnlen - 1] == '*')
                    {
                      cur.name[fnlen - 1] = '\0';
                      DEBUGP (("trailing `*' on exec.\n"));
                    }
                } /* if (fnlen) */
              else
                error = 1;
              break;
            }
          else
            abort ();
        } /* while */

      if (!cur.name || (cur.type == FT_SYMLINK && !cur.linkto))
        error = 1;

      DEBUGP (("%s\n", cur.name ? cur.name : ""));

      if (error || ignore)
        {
          DEBUGP (("Skipping.\n"));
          xfree_null (cur.name);
          xfree_null (cur.linkto);
          continue;
        }

      if (!dir)
        {
          l = dir = xnew (struct fileinfo);
          memcpy (l, &cur, sizeof (cur));
          l->prev = l->next = NULL;
        }
      else
        {
Beispiel #11
0
static void
init_ram_segments(void)
{
	int i, errflag;
        FILE *iomem; 
	char buf[BUFSIZE], *p1, *p2;
	physaddr_t start, end;

	if ((iomem = fopen("/proc/iomem", "r")) == NULL)
		goto fail_iomem;

	while (fgets(buf, BUFSIZE, iomem)) {
		if (strstr(buf, "System RAM")) {
			console(buf);
			nr_segments++;
		}
	}
	if (!nr_segments)
		goto fail_iomem;

	ram_segments = (struct ram_segments *)
		GETBUF(sizeof(struct ram_segments) * nr_segments);

	rewind(iomem);
	i = 0;
	while (fgets(buf, BUFSIZE, iomem)) {
		if (strstr(buf, "System RAM")) {
			if (!(p1 = strstr(buf, ":")))
				goto fail_iomem;
			*p1 = NULLCHAR;
			clean_line(buf);
			if (strstr(buf, " "))
				goto fail_iomem;
			p1 = buf;
			if (!(p2 = strstr(buf, "-")))
				goto fail_iomem;
			*p2 = NULLCHAR;
			p2++;
			errflag = 0;
			start = htoll(p1, RETURN_ON_ERROR|QUIET, &errflag);
			end = htoll(p2, RETURN_ON_ERROR|QUIET, &errflag);
			if (errflag)
				goto fail_iomem;
			ram_segments[i].start = PHYSPAGEBASE(start);
			if (PAGEOFFSET(start))
				ram_segments[i].start += PAGESIZE();
			ram_segments[i].end = PHYSPAGEBASE(end);
			if (PAGEOFFSET(end) == (PAGESIZE()-1))
				ram_segments[i].end += PAGESIZE();
			console("ram_segments[%d]: %016llx %016llx [%s-%s]\n", i,
				(ulonglong)ram_segments[i].start, 
				(ulonglong)ram_segments[i].end, p1, p2);
			i++;
		}
	}

	fclose(iomem);
	return;

fail_iomem:
	fclose(iomem);
	nr_segments = 0;
	if (ram_segments)
		FREEBUF(ram_segments);

	return; 
}
TEST(StanGmArgumentsConfiguration, TestMethod) {
  
  // Prepare model
  std::vector<std::string> model_path;
  model_path.push_back("src");
  model_path.push_back("test");
  model_path.push_back("gm");
  model_path.push_back("arguments");
  model_path.push_back("test_model");
  
  // Prepare arguments
  std::stringstream s;
  
  std::vector<stan::gm::argument*> valid_arguments;
  valid_arguments.push_back(new stan::gm::arg_method());
  
  stan::gm::argument_probe probe(valid_arguments);
  probe.probe_args(s);
  
  // Check argument consistency
  bool expected_success = false;
  
  std::string l1;
  std::stringstream expected_output;
  std::stringstream output;
  
  while (s.good()) {
    std::getline(s, l1);
    if (!s.good()) 
      continue;
    
    if (l1 == "good") 
      expected_success = true;
    else if (l1 == "bad") 
      expected_success = false;
    else if (l1 != "") 
      expected_output << l1 << std::endl;
    else {
      int n_output = 0;
      
      std::string l2;
      std::string argument("");
      
      while (expected_output.good()) {
        std::getline(expected_output, l2);
        if (!expected_output.good()) 
          continue;
        clean_line(l2);
        argument += " " + l2;
        ++n_output;
      }
      
      if (argument.length() == 0) 
        continue;
      
      remove_duplicates(argument);

      std::string command = convert_model_path(model_path) + argument;

      SCOPED_TRACE(command);

      run_command_output out = run_command(command);

      expected_output.clear();
      expected_output.seekg(std::ios_base::beg);
      
      if (expected_success == false) {
        unsigned int c1 = out.output.find("is not");
        out.output.erase(0, c1);

        unsigned int c2 = out.output.find(" \"");

        unsigned int c3 = out.output.find("Failed to parse");

        if (c3 != c2)
          out.output.replace(c2, c3 - c2, "\n");
        
        expected_output.str(std::string());

        expected_output << "is not a valid value for" << std::endl;
        expected_output << "Failed to parse arguments, terminating Stan" << std::endl;
        n_output = 2;
        EXPECT_EQ(int(stan::gm::error_codes::USAGE), out.err_code);
      } else {
        EXPECT_EQ(int(stan::gm::error_codes::OK), out.err_code);
      }
      
      output.clear();
      output.seekg(std::ios_base::beg);
      output.str(out.output);
      
      for (int i = 0; i < n_output; ++i) {
        std::string expected_line;
        std::getline(expected_output, expected_line);
        
        
        std::string actual_line;
        std::getline(output, actual_line);
        
        EXPECT_EQ(expected_line, actual_line);
      }
      
      expected_output.clear();
      expected_output.seekg(std::ios_base::beg);
      expected_output.str(std::string());
    }
  }
  
  for (size_t i = 0; i < valid_arguments.size(); ++i)
    delete valid_arguments.at(i);
}
Beispiel #13
0
// you may want to have a look into /usr/src/linux/drivers/char/console.c
void parse_character(termstate_t * term, l4_uint8_t c)
{
    int i;

    // these must be handled independetly of state
    switch (c)
    {
    case 0: // ignore
        return;
    case 7: // bell
        // todo: impl. me
        return;
    case 8: // backspace
        if (term->cur_x > 0)
            term->cur_x--;
        return;
    case 9: // tab
        cursor_tab(term);
        return;
    case 10: // newline (lf)
    case 11: // ?
    case 12: // ?
        cursor_nl(term);
        return;
    case 13: // do a cr here, maybe a smart one (+ lf)
        cursor_cr(term);
        return;
    case 27: // ESC
        term->esc_state = ESesc;
        return;
    }

    // now check for the state-dependant characters
    switch (term->esc_state)
    {
    case ESnormal:
        // normal character
        if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe))
        {
            if (term->insert_mode == VT100_INSMODE_REPLACE)
            {
                set_char(term, c);
                //vt100_redraw_xy(term, term->cur_x, term->cur_y);
                cursor_next(term);
            }
            else // VT100_INSMODE_INSERT
            {
                insert_char(term, c);
                //vt100_redraw(term);
            }
            return;
        }
//        switch (c)
//        {
//        }
        break;
    case ESesc: // normal ESC found
        term->esc_state = ESnormal;
        switch (c)
        {
        case '[':
            term->esc_state = ESsquare;
            return;
        case 'D':  // lf
            cursor_lf(term);
            //vt100_redraw(term);
            return;
        case 'E':  // cr + lf
            cursor_nl(term);
            //vt100_redraw(term);
            return;
        case 'H':  // set tab at current position
            set_tab( term, term->cur_x );
            return;
        case 'M':
            rev_scroll(term);
            //vt100_redraw(term);
            return;
        case '7':  // save cursor position and attributes
            term->cur_stored_x    = term->cur_x;
            term->cur_stored_y    = term->cur_y;
            term->cur_stored_attr = term->attrib_mode;
            return;
        case '8':  // restore saved cursor position and attributes
            term->cur_x         = term->cur_stored_x;
            term->cur_y         = term->cur_stored_y;
            term->attrib_mode   = term->cur_stored_attr;
            return;
        case '#':
            term->esc_state = EShash;
            return;
        case 'c':  // reset vt to default settings
            init_termstate(term, term->w, term->phys_h, term->virt_h);
            return;
        }
    case ESsquare:
        for(i = 0; i < NUM_PAR; i++)
            term->par[i] = 0;
        term->used_par = 0;
        term->esc_state = ESgetpars;
        term->ques = (c=='?');
        if (term->ques)
            return;
        // fall-through
    case ESgetpars:
        if (c == ';' && term->used_par < NUM_PAR - 1)
        {
            term->used_par++;
            return;
        }
        else if (c >= '0' && c <= '9')
        {
            term->par[term->used_par] *= 10;
            term->par[term->used_par] += c - '0';
            return;
        } else term->esc_state = ESgotpars;
        // fall-through
    case ESgotpars:
        term->esc_state = ESnormal;
        switch (c)
        {
        case 'h':
            if (term->ques)
            {
                // handle question commands ending with h
                for (i = 0; i <= term->used_par; i++)
                {
                    switch( term->par[i] )
                    {
                    case  5: // activate inverse screen
                        if (set_mode(term, 1))
                            //vt100_redraw(term);
                        break;
                    case  6: // origin mode = scroll region
                        term->origin_mode = VT100_ORIGIN_SCROLL;
                        break;
                    case  7: // autowrap mode on
                        term->autowrap   = 1;
                        break;
                    case  8: // autorepeat on
                        term->autorepeat = 1;
                        break;
                    case 25: // activate cursor
                        term->cursor_vis = 1; return;
                    default:
                        break;
                    }
                }
                return;
            }
            else // handle commands without question mark
            {
                for(i = 0; i <= term->used_par; i++)
                {
                    switch( term->par[i] )
                    {
                    case 4: // insert mode on
                        term->insert_mode = VT100_INSMODE_INSERT;
                        break;
                    case 12: // echo on
                        term->echo = 1;
                        break;
                    case 20: // line feed mode
                        term->newline = 1;
                        break;
                    default: break;
                    }
                }
                return;
            }
        case 'l':
            if (term->ques)
            {
                for(i = 0; i <= term->used_par; i++)
                {
                    // handle question commands ending with l
                    switch( term->par[i] )
                    {
                    case  3: // set 80 column mode, clear screen
                        // todo: impl. resize and use it here
                        break;
                    case  5: // deactivate inverse screen
                        if(set_mode(term, 0))
                            //vt100_redraw(term);
                        break;
                    case  6: // origin mode = whole screen
                        term->origin_mode = VT100_ORIGIN_GLOBAL;
                    case  7: // autowrap mode off
                        term->autowrap = 0;
                        break;
                    case  8: // autorepeat off
                        term->autorepeat = 0;
                        break;
                    case 25: // deactivate cursor
                        term->cursor_vis = 0; return;
                    default: break;
                    }
                    return;
                }
            }
            else // handle commands without question mark
            {
                for( i=0; i <= term->used_par; i++ )
                {
                    switch( term->par[i] )
                    {
                    case 4: // insert mode off
                        term->insert_mode = VT100_INSMODE_REPLACE;
                        break;
                    case 12: // echo off
                        term->echo = 0;
                        break;
                    case 20: // carriage return mode
                        term->newline = 0;
                        break;
                    default:
                        break;
                    }
                    return;
                }
            }
        }
        if (term->ques)
        {
            term->ques = 0;
            return;
        }
        switch (c)
        {
        case 'F':  // CR + up
            term->cur_x     = 0;
            // fall through
        case 'A':  // up
            if (! term->par[0])
                term->par[0]++;
            cursor_up(term);
            return;
        case 'E':  // CR + down
            term->cur_x     = 0;
            // fall through
        case 'B':  // down
            if (! term->par[0])
                term->par[0]++;
            cursor_down(term);
            return;
        case 'C':  // right
            // no parameter -> set parameter to default (=1)
            if (! term->par[0])
                term->par[0]++;
            cursor_right(term);
            return;
        case 'D':  // left
            if (! term->par[0])
                term->par[0]++;
            cursor_left(term);
            return;
        case 'G':  // cursor position horizontal absolute
            if ( (term->par[0] > 0) &&
                 (term->par[0] < term->w) )
                term->cur_x = term->par[0];
            return;
        case 'H':  // absolute position (x,y)
        case 'f':  // f is the same as H (correct me, if I'm wrong)
            // well it is the same in the Linux kernel, in theorie the
            // 'f' commands depend on the PUM (Position unit mode),
            // which can be characters (seems to be default) or inch
            if (term->par[0] > 0)  // convert to (0, 0) based coords
                term->par[0]--;
            if (term->par[1] > 0)
                term->par[1]--;
            cursor_move_abs(term, term->par[1], term->par[0]);
            return;
        case 'I':  // insert tabs ('\t', term->par[0] times)
            if (!term->par[0])
                term->par[0]++;
            for (i=0;i<term->par[0];i++)
                cursor_tab(term);
            return;
        case 'J':
            switch(term->par[0])
            {
            case 0:  // kill to end of screen
                clean_to_eos(term);
                //vt100_redraw(term);
                return;
            case 1:  // kill from start of screen
                clean_from_sos(term);
                //vt100_redraw(term);
                return;
            case 2:  // kill whole screen
                clean_screen(term, ' ');
                //vt100_redraw(term);
                return;
            }
        case 'K':  // kill to end of line
            switch(term->par[0])
            {
            case 0: // clean to end of line
                clean_to_eol(term);
                //vt100_redraw(term);
                return;
            case 1: // clean from start of line
                clean_from_sol(term);
                return;
            case 2: // erase whole line
                clean_line(term, term->cur_y);
                return;
            }
        case 'L':  // insert lines
            if (!term->par[0])
                term->par[0] = 1;
            for (i=0; i<term->par[0]; i++)
                insert_line(term);
            //vt100_redraw(term);
            return;
        case 'M':  // erase lines
            if (!term->par[0])
                term->par[0] = 1;
            for (i=0; i<term->par[0]; i++)
                erase_line(term);
            //vt100_redraw(term);
            return;
        case 'c':  // we were asked to identify the terminal type
            identify(term);
            return;
        case 'g':  // erase tab(s)
            switch(term->par[0])
            {
                case 0:  remove_tab( term, term->cur_x );
                         break;
                case 3:  clear_tabs( term );
                         break;
                default: break;
            }
            return;
        case 'm':
            csi_m(term);
            return;
        case 'r':  // set scrolling region
            if (term->par[0] == 0)
                term->par[0]++;
            if (term->par[1] == 0)
                term->par[1] = term->phys_h;
            /* Minimum allowed region is 2 lines */
            if (term->par[0] < term->par[1] &&
                term->par[1] <= term->phys_h)
            {
                term->scroll_top = term->par[0] - 1;
                term->scroll_bottom = term->par[1]; // this line is excluded
                // if in origin mode, make sure that the cursor is placed
                // inside the scrolling region immediately
                if (term->origin_mode == VT100_ORIGIN_SCROLL)
                {
                    term->cur_x = 0;
                    term->cur_y = term->scroll_top;
                }
            }
            return;
        }
    case EShash:
        term->esc_state = ESnormal;
        switch(c)
        {
        case '8':  // fill the screen with 'E'
            clean_screen(term, 'E');
            //vt100_redraw(term);
            break;
        }
        break;
    }
}
static int overwrite_fail(char *outname,double overwrite_minsize_mb)

    {
    double size_mb;
    char basepath[512];
    char buf[512];
    char newname[512];

    if (wfile_status(outname)==0)
        return(0);
    if (overwrite_minsize_mb < 0.)
        return(0);
    if (k2files_overwrite==1)
        return(0);
    size_mb = wfile_size(outname)/1024./1024.;
    if (size_mb < overwrite_minsize_mb)
        return(0);
    if (k2files_overwrite==-1)
        return(1);
    wfile_basepath(basepath,outname);
    strcpy(newname,outname);
    k2printf("\n\a");
    while (1)
        {
        while (1)
            {
#ifdef HAVE_K2GUI
            if (k2gui_active())
                {
                int reply;
                reply=k2gui_yes_no_all("File overwrite query","File %s (%.1f MB) already exists!  "
                                       "Overwrite it?",newname,size_mb);
                if (reply==2)
                    {
                    overwrite_set(-1);
                    return(1);
                    }
                if (reply==3)
                    overwrite_set(1);
                return(0);
                }
            else
                {
#endif
            k2printf("File " TTEXT_MAGENTA "%s" TTEXT_NORMAL " (%.1f MB) already exists!\n"
                      "   Overwrite it (y[es]/n[o]/a[ll]/q[uit])? " TTEXT_INPUT,
                    newname,size_mb);
            k2gets(buf,16,"y");
            k2printf(TTEXT_NORMAL);
            clean_line(buf);
            buf[0]=tolower(buf[0]);
#ifdef HAVE_K2GUI
                }
#endif
            if (buf[0]!='y' && buf[0]!='n' && buf[0]!='a' && buf[0]!='q')
                {
                k2printf("\a\n  ** Must respond with 'y', 'n', 'a', or 'q' **\n\n");
                continue;
                }
            break;
            }
        if (buf[0]=='q')
            return(-1);
        if (buf[0]=='a' || buf[0]=='y')
            {
            if (buf[0]=='a')
                overwrite_set(1);
            return(0);
            }

        k2printf("Enter a new output base name (.pdf will be appended, q=quit).\n"
                "New name: " TTEXT_INPUT);
        k2gets(buf,255,"__out__.pdf");
        k2printf(TTEXT_NORMAL);
        clean_line(buf);
        if (!stricmp(buf,"q"))
            return(-1);
        if (buf[0]=='/' || buf[0]=='\\' || buf[1]==':')
            strcpy(newname,buf);
        else
            wfile_fullname(newname,basepath,buf);
        if (!strcmp(wfile_ext(newname),""))
            strcat(newname,".pdf");
        if (wfile_status(newname)==0)
            break;
        }
    strcpy(outname,newname);
    return(0);
    }
Beispiel #15
0
TEST_F(StanGmArgumentsConfiguration, TestOutputWithMethod) {
  
  // Prepare arguments
  std::stringstream method_output;
  stan::gm::arg_method method;
  method.print(&method_output, 0, '\0');
  
  std::string l0;
  std::string method_argument("");
  int n_method_output = 0;
  
  while (method_output.good()) {
    std::getline(method_output, l0);
    if (!method_output.good()) continue;
    clean_line(l0);
    method_argument += " " + l0;
    ++n_method_output;
  }
  
  remove_duplicates(method_argument);
  
  method_output.clear();
  method_output.seekg(std::ios_base::beg);
  
  std::stringstream s;
  std::vector<stan::gm::argument*> valid_arguments;
  valid_arguments.push_back(new stan::gm::arg_output());
  
  stan::gm::argument_probe probe(valid_arguments);
  probe.probe_args(s);
  
  // Check argument consistency
  bool expected_success = false;
  
  std::string l1;
  std::stringstream expected_output;
  std::stringstream output;
  
  while (s.good()) {
    
    std::getline(s, l1);
    if (!s.good()) continue;
    
    if      (l1 == "good") expected_success = true;
    else if (l1 == "bad") expected_success = false;
    else if (l1 != "") expected_output << l1 << std::endl;
    else {
      
      int n_output = 0;
      
      std::string l2;
      std::string argument("");

      while (expected_output.good()) {
        std::getline(expected_output, l2);
        if (!expected_output.good()) continue;
        clean_line(l2);
        argument += " " + l2;
        ++n_output;
      }
      
      if (argument.length() == 0) continue;
      remove_duplicates(argument);
      argument = method_argument + argument;
      
      run_command_output out = run_command(command + argument);
      
      expected_output.clear();
      expected_output.seekg(std::ios_base::beg);
      expected_output.str( method_output.str() + expected_output.str() );
      
      if (expected_success == false) {
        
        unsigned int c1 = out.output.find("is not");
        out.output.erase(0, c1);
        unsigned int c2 = out.output.find(" \"");
        unsigned int c3 = out.output.find("Failed to parse");
        out.output.replace(c2, c3 - c2, "\n");
        
        expected_output.str(std::string());
        expected_output << "is not a valid value for" << std::endl;
        expected_output << "Failed to parse arguments, terminating Stan" << std::endl;
        n_output = 2;
        
      }
      
      output.str(out.output);
      std::string actual_line;
      
      for (int i = 0; i < n_output; ++i) {
        std::string expected_line;
        std::getline(expected_output, expected_line);
        
        std::getline(output, actual_line);
        
        EXPECT_EQ(expected_line, actual_line);
      }
      
      expected_output.clear();
      expected_output.seekg(std::ios_base::beg);
      expected_output.str(std::string());
      
    }
    
  }
  
  for (size_t i = 0; i < valid_arguments.size(); ++i)
    delete valid_arguments.at(i);
  
}
Beispiel #16
0
void cache_loaded(void)
{
    clean_line(MAX_Y - 2);
    mvwprintw(window, MAX_Y - 2, 0, "Paths loaded");
    wrefresh(window);
}
Beispiel #17
0
void run_term(void)
{
    const config_t *conf = config();

    int c;

    while ((c = wgetch(window)) != KEY_ESCAPE) {
        if (conf->section_main->emacs_bindings) {
            int key = read_emacs_keys(keyname(c));

            if (key != -1) {
                if (key == KEY_ESCAPE)
                    break;
                c = key;
            }
        }

        open_by_shortcut(c);

        if (c == KEY_DOWN) {
            menu_driver(menu_list, REQ_DOWN_ITEM);
            update_info_bar();
        } else if (c == KEY_UP) {
            menu_driver(menu_list, REQ_UP_ITEM);
            update_info_bar();
        } else if (c == KEY_RETURN) {
            set_app_to_run();
        } else if (c == KEY_NPAGE) {
            menu_driver(menu_list, REQ_SCR_DPAGE);
            update_info_bar();
        } else if (c == KEY_PPAGE) {
            menu_driver(menu_list, REQ_SCR_UPAGE);
            update_info_bar();
        } else if (c == KEY_BACKSPACE) {
            if (query_len >= 1) {
                query_len--;

                if (query_len == 0) {
                    reset_query();
                } else {
                    form_driver(form, REQ_DEL_PREV);
                    form_driver(form, REQ_VALIDATION);

                    snprintf(
                        query,
                        MAX_INPUT_LENGTH,
                        "%s",
                        field_buffer(field[0], 0)
                    );

                    char *new_query = smalloc(query_len + 1);
                    memcpy(new_query, query, query_len);
                    new_query[query_len] = '\0';

                    search(new_query);
                    prepare_for_new_results(True);

                    free(new_query);
                }
            }
        } else if (isprint(c)) {
            mvprintw(0, 0, "$");

            if (! is_cache_ready()) {
                continue;
            }

            if (query_len == 0 && c == ' ') {
                reset_query();

                continue;
            } else if (query_len == 0) {
                clean_line(0);
            }

            form_driver(form, c);
            form_driver(form, REQ_VALIDATION);

            snprintf(
                query,
                MAX_INPUT_LENGTH,
                "%s",
                field_buffer(field[0], 0)
            );

            query_len++;
            char new_query[query_len];

            memcpy(new_query, query, query_len);
            new_query[query_len] = '\0';

            if (search(new_query))
                prepare_for_new_results(True);
        }

        if (run_app == True) {
            break;
        }
    }
}