Ejemplo n.º 1
0
/*
 * Spread the selection outwards according to the selection mode.
 */
static pos
sel_spread_half(pos p, bool forward)
{
  switch (term.mouse_state) {
    when MS_SEL_CHAR: {
     /*
      * In this mode, every character is a separate unit, except
      * for runs of spaces at the end of a non-wrapping line.
      */
      termline *line = fetch_line(p.y);
      if (!(line->attr & LATTR_WRAPPED)) {
        termchar *q = line->chars + term.cols;
        while (q > line->chars && q[-1].chr == ' ' && !q[-1].cc_next)
          q--;
        if (q == line->chars + term.cols)
          q--;
        if (p.x >= q - line->chars)
          p.x = forward ? term.cols - 1 : q - line->chars;
      }
      release_line(line);
    }
    when MS_SEL_WORD:
      p = sel_spread_word(p, forward); 
    when MS_SEL_LINE:
      if (forward) {
        termline *line = fetch_line(p.y);
        while (line->attr & LATTR_WRAPPED) {
          release_line(line);
          line = fetch_line(++p.y);
          p.x = 0;
        }
        int x = p.x;
        p.x = term.cols - 1;
        do {
          if (get_char(line, x) != ' ')
            p.x = x;
        } while (++x < line->cols);
        release_line(line);
      }
      else {
        p.x = 0;
        while (p.y > -sblines()) {
          termline *line = fetch_line(p.y - 1);
          bool wrapped = line->attr & LATTR_WRAPPED;
          release_line(line);
          if (!wrapped)
            break;
          p.y--;
        }
      }
    default:
     /* Shouldn't happen. */
      break;
  }
  return p;
}
Ejemplo n.º 2
0
Archivo: sh.c Proyecto: danhil/050ade
/* fetch_line: read one line from user and put it in input_buf. */
int fetch_line(char* prompt)
{
	int	c;
	int	count;

	input_char = input_buf;
	token = token_buf;

	printf("%s", prompt);
	fflush(stdout);

	count = 0;

	for (;;) {

		c = getchar();

		if (c == EOF)
			return EOF;

		if (count < MAXBUF)
			input_buf[count++] = c;

		if (c == '\n' && count < MAXBUF) {
			input_buf[count] = 0;
			return count;
		}

		if (c == '\n') {
			printf("too long input line\n");
			return fetch_line(prompt);
		}

	}
}
Ejemplo n.º 3
0
void parse_config(char *filename, struct conf *conf) {
    int conf_file = open(filename, O_RDONLY);

    char buffer[DFINGER_BUFFER_SIZE];
    memset(buffer, 0, DFINGER_BUFFER_SIZE);
    char line[DFINGER_LINE_SIZE];
    memset(line, 0, DFINGER_LINE_SIZE);
    size_t blen = DFINGER_BUFFER_SIZE;
    size_t boffset = 0;
    size_t llen = DFINGER_LINE_SIZE;

    int ret;
    int num_read;
    while ((num_read = read(conf_file, buffer, DFINGER_BUFFER_SIZE) > 0)) {
        blen += num_read;
        while ((ret = fetch_line(buffer, blen, &boffset, line, llen)) !=
                RTL_WANT_MORE) {
            switch (ret) {
            case RTL_LINE_FETCHED:
                set_option(line, conf);
                break;
            case RTL_BLANK_LINE:
                break;
            default:
                return;
            }
        }
        move_buffer(buffer, blen, &boffset);
    }

    close(conf_file);
}
Ejemplo n.º 4
0
scanner::scanner(std::istream & strm, char const * strm_name, pos_info const & skip_to_pos) :
        scanner(strm, strm_name) {
    for (unsigned line_no = 1; line_no < skip_to_pos.first; line_no++)
        fetch_line();
    m_line = m_sline;
    for (unsigned col_idx = 0; col_idx < skip_to_pos.second; col_idx++)
        next();
}
Ejemplo n.º 5
0
static pos
sel_spread_word(pos p, bool forward)
{
  pos ret_p = p;
  termline *line = fetch_line(p.y);
  
  for (;;) {
    wchar c = get_char(line, p.x);
    if (iswalnum(c) || strchr("_#~+-", c))
      ret_p = p;
    else if (strchr(".@/\\", c)) {
      if (!forward)
        ret_p = p;
    }
    else if (!(strchr("&,?$%", c) || c == (forward ? '=' : ':')))
      break;

    if (forward) {
      p.x++;
      if (p.x >= term.cols - ((line->attr & LATTR_WRAPPED2) != 0)) {
        if (!(line->attr & LATTR_WRAPPED))
          break;
        p.x = 0;
        release_line(line);
        line = fetch_line(++p.y);
      }
    }
    else {
      if (p.x <= 0) {
        if (p.y <= -sblines())
          break;
        release_line(line);
        line = fetch_line(--p.y);
        if (!(line->attr & LATTR_WRAPPED))
          break;
        p.x = term.cols - ((line->attr & LATTR_WRAPPED2) != 0);
      }
      p.x--;
    }
  }
    
  release_line(line);
  return ret_p;
}
Ejemplo n.º 6
0
Archivo: sh.c Proyecto: danhil/050ade
/* main: main program of simple shell. */
int main(int argc, char** argv)
{
	progname = argv[0];

	init_search_path();	

	while (fetch_line("% ") != EOF)
		parse_line();

	return 0;
}
Ejemplo n.º 7
0
scanner::scanner(std::istream & strm, char const * strm_name):
    m_tokens(nullptr), m_stream(strm) {
    m_stream_name = strm_name ? strm_name : "[unknown]";
    m_sline = 0;
    m_spos  = 0;
    m_upos  = 0;
    m_uskip = 0;
    m_in_notation = false;
    m_last_line = false;
    fetch_line();
    m_line = m_sline;
}
Ejemplo n.º 8
0
int find_urb()
{
	do
	{
		if (line[0] == '[' && strstr(line, " URB ") &&
		   (strstr(line, "going down") || strstr(line, "coming back")))
		{
			return 1;
		}
	}
	while( fetch_line() );
	
	return 0;
}
/* This code should be replaced with header buckets. */
static apr_status_t fetch_headers(serf_bucket_t *bkt, response_context_t *ctx)
{
    apr_status_t status;

    /* RFC 2616 says that CRLF is the only line ending, but we can easily
     * accept any kind of line ending.
     */
    status = fetch_line(ctx, SERF_NEWLINE_ANY);
    if (SERF_BUCKET_READ_ERROR(status)) {
        return status;
    }
    /* Something was read. Process it. */

    if (ctx->linebuf.state == SERF_LINEBUF_READY && ctx->linebuf.used) {
        const char *end_key;
        const char *c;

        end_key = c = memchr(ctx->linebuf.line, ':', ctx->linebuf.used);
        if (!c) {
            /* Bad headers? */
            return SERF_ERROR_BAD_HTTP_RESPONSE;
        }

        /* Skip over initial ':' */
        c++;

        /* And skip all whitespaces. */
        for(; c < ctx->linebuf.line + ctx->linebuf.used; c++)
        {
            if (!apr_isspace(*c))
            {
              break;
            }
        }

        /* Always copy the headers (from the linebuf into new mem). */
        /* ### we should be able to optimize some mem copies */
        serf_bucket_headers_setx(
            ctx->headers,
            ctx->linebuf.line, end_key - ctx->linebuf.line, 1,
            c, ctx->linebuf.line + ctx->linebuf.used - c, 1);
    }

    return status;
}
Ejemplo n.º 10
0
void scanner::next() {
    lean_assert(m_curr != EOF);
    m_spos++;
    while (m_spos >= static_cast<int>(m_curr_line.size())) {
        if (m_last_line) {
            m_curr = EOF;
            return;
        } else {
            return fetch_line();
        }
    }
    m_curr = m_curr_line[m_spos];
    if (m_uskip > 0) {
        if (!is_utf8_next(m_curr))
            throw_exception("invalid utf-8 sequence character");
        m_uskip--;
    } else {
        m_upos++;
        m_uskip = get_utf8_size(m_curr);
        m_uskip--;
    }
}
Ejemplo n.º 11
0
unsigned char* parse_data_dump(int len)
{
	int count = 0;
	char* ptr= NULL;
	
	unsigned char* data = (unsigned char *)malloc(len);	
	if (!data)
		return NULL;
		
	while(count < len)
	{
		if ((count % 16) == 0)
		{
			if (count)
			{
				if (!fetch_line())
				{
					free(data);
					return NULL;
				}
			}

			ptr = strchr(line, ':');
			if (!ptr)
			{
				free(data);
				return NULL;
			}

			ptr += 2;
		}

		data[count++] = strtoul(ptr, NULL, 16);
		ptr += 3;
	}
	
	return data;
}
Ejemplo n.º 12
0
Archivo: term.c Proyecto: ttdoda/mintty
void
term_update_search()
{
  int update_type = term.results.update_type;
  if (term.results.update_type == NO_UPDATE)
    return;
  term.results.update_type = NO_UPDATE;

  if (term.results.query_length == 0)
    return;

  circbuf cb;
  // Allocate room for the circular buffer of termlines.
  int lcurr = 0;
  if (update_type == PARTIAL_UPDATE) {
    // How much of the backscroll we need to update on a partial update?
    // Do a ceil: (x + y - 1) / y
    // On query_length - 1
    int pstart = -((term.results.query_length + term.cols - 2) / term.cols) + term.sblines;
    lcurr = lcurr > pstart ? lcurr:pstart;
    results_partial_clear(lcurr);
  } else {
    term_clear_results();
  }
  int llen = term.results.query_length / term.cols + 1;
  if (llen < 2)
    llen = 2;

  circbuf_init(&cb, llen);

  // Fill in our starting set of termlines.
  for (int i = lcurr; i < term.rows + term.sblines && cb.length < cb.capacity; ++i) {
    circbuf_push(&cb, fetch_line(i - term.sblines));
  }

  int cpos = term.cols * lcurr;
  /* the number of matched chars in the current run */
  int npos = 0;
  /* the number of matched cells in the current run (anpos >= npos) */
  int anpos = 0;
  int end = term.cols * (term.rows + term.sblines);

  // Loop over every character and search for query.
  while (cpos < end) {
    // Determine the current position.
    int x = (cpos % term.cols);
    int y = (cpos / term.cols);

    // If our current position isn't in the buffer, add it in.
    if (y - lcurr >= llen) {
      circbuf_push(&cb, fetch_line(lcurr + llen - term.sblines));
      ++lcurr;
    }
    termline * lll = circbuf_get(&cb, y - lcurr);
    termchar * chr = lll->chars + x;

    if (npos == 0 && cpos + term.results.query_length >= end)
      break;

    if (chr->chr != term.results.query[npos]) {
      // Skip the second cell of any wide characters
      if (chr->chr == UCSWIDE) {
        ++anpos;
        ++cpos;
        continue;
      }
      cpos -= npos - 1;
      npos = 0;
      anpos = 0;
      continue;
    }

    ++anpos;
    ++npos;

    if (term.results.query_length == npos) {
      int start = cpos - anpos + 1;
      result run = {
        .x = start % term.cols,
        .y = start / term.cols,
        .len = anpos
      };
#ifdef debug_search
      printf("%d, %d, %d\n", run.x, run.y, run.len);
#endif
      results_add(run);
      npos = 0;
      anpos = 0;
    }

    ++cpos;
  }

  circbuf_destroy(&cb);
}
Ejemplo n.º 13
0
  child_write(buf, 6);
}

static pos
box_pos(pos p)
{
  p.y = min(max(0, p.y), term.rows - 1);
  p.x = min(max(0, p.x), term.cols - 1);
  return p;
}

static pos
get_selpoint(const pos p)
{
  pos sp = { .y = p.y + term.disptop, .x = p.x };
  termline *line = fetch_line(sp.y);
  if ((line->attr & LATTR_MODE) != LATTR_NORM)
    sp.x /= 2;

 /*
  * Transform x through the bidi algorithm to find the _logical_
  * click point from the physical one.
  */
  if (term_bidi_line(line, p.y) != null)
    sp.x = term.post_bidi_cache[p.y].backward[sp.x];
  
  // Back to previous cell if current one is second half of a wide char
  if (line->chars[sp.x].chr == UCSWIDE)
    sp.x--;
  
  release_line(line);
Ejemplo n.º 14
0
void parse_urb_body(struct urb* urb)
{
	int setup_packet = 0;

	while (fetch_line())
	{
		if (line[0] == '[')
			break;
			
		if (strstr(line, "TransferBufferLength"))
		{
			urb->len = get_hex_value();
		}
		else if (strstr(line, "TransferFlags"))
		{
			urb->flags = get_hex_value();
		}
		else if (strstr(line, "Index"))
		{
			urb->index = get_hex_value();
		}
		else if (strstr(line, "DescriptorType"))
		{
			urb->type = get_hex_value();
		}
		else if (strstr(line, "RequestTypeReservedBits"))
		{
			urb->reserved = get_hex_value();
		}
		else if (strstr(line, "Request"))
		{
			urb->request = get_hex_value();
		}
		else if (strstr(line, "Value"))
		{
			urb->value = get_hex_value();
		}
		else if (strstr(line, "SetupPacket"))
		{
			setup_packet = 1;
		}
		else if (strstr(line, "00000000:"))
		{
			if (!setup_packet && !urb->data)
				urb->data = parse_data_dump(urb->len);
		}
		else if (strstr(line, "-- URB_FUNCTION_"))
		{
			if (strstr(line, "URB_FUNCTION_CONTROL_TRANSFER"))
				urb->func = FUNC_CONTROL_TXFER;
			else if (strstr(line, "URB_FUNCTION_CLASS_INTERFACE"))
				urb->func = FUNC_CLASS_INTERFACE;
			else if (strstr(line, "URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE"))
				urb->func = FUNC_GET_DESC;
			else if (strstr(line, "URB_FUNCTION_SELECT_CONFIGURATION"))
				urb->func = FUNC_SELECT_CONFIG;
			else if (strstr(line, "URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE"))
				urb->func = FUNC_GET_DESC_FROM_IFACE;
			else if (strstr(line, "URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER"))
				urb->func = FUNC_BULK_TXFER;
			else if (strstr(line, "URB_FUNCTION_RESET_PIPE"))
				urb->func = FUNC_RESET_PIPE;
			else if (strstr(line, "URB_FUNCTION_ABORT_PIPE"))
				urb->func = FUNC_ABORT_PIPE;
			else
			{
				urb->func = FUNC_UNKNOWN;
				printf("Unknown FUNC: %s\n", line);
				exit(0);
			}
		}
	}
}
/* Perform one iteration of the state machine.
 *
 * Will return when one the following conditions occurred:
 *  1) a state change
 *  2) an error
 *  3) the stream is not ready or at EOF
 *  4) APR_SUCCESS, meaning the machine can be run again immediately
 */
static apr_status_t run_machine(serf_bucket_t *bkt, response_context_t *ctx)
{
    apr_status_t status = APR_SUCCESS; /* initialize to avoid gcc warnings */

    switch (ctx->state) {
    case STATE_STATUS_LINE:
        /* RFC 2616 says that CRLF is the only line ending, but we can easily
         * accept any kind of line ending.
         */
        status = fetch_line(ctx, SERF_NEWLINE_ANY);
        if (SERF_BUCKET_READ_ERROR(status))
            return status;

        if (ctx->linebuf.state == SERF_LINEBUF_READY) {
            /* The Status-Line is in the line buffer. Process it. */
            status = parse_status_line(ctx, bkt->allocator);
            if (status)
                return status;

            /* Good times ahead: we're switching protocols! */
            if (ctx->sl.code == 101) {
                ctx->body =
                    serf_bucket_barrier_create(ctx->stream, bkt->allocator);
                ctx->state = STATE_DONE;
                break;
            }

            /* Okay... move on to reading the headers. */
            ctx->state = STATE_HEADERS;
        }
        else {
            /* The connection closed before we could get the next
             * response.  Treat the request as lost so that our upper
             * end knows the server never tried to give us a response.
             */
            if (APR_STATUS_IS_EOF(status)) {
                return SERF_ERROR_REQUEST_LOST;
            }
        }
        break;
    case STATE_HEADERS:
        status = fetch_headers(bkt, ctx);
        if (SERF_BUCKET_READ_ERROR(status))
            return status;

        /* If an empty line was read, then we hit the end of the headers.
         * Move on to the body.
         */
        if (ctx->linebuf.state == SERF_LINEBUF_READY && !ctx->linebuf.used) {
            const void *v;

            /* Advance the state. */
            ctx->state = STATE_BODY;

            ctx->body =
                serf_bucket_barrier_create(ctx->stream, bkt->allocator);

            /*
             * Instaweb/mod_pagespeed change: This section is
             * re-ordered from the original code from serf to Follow
             * HTTP spec by checking "Transfer-Encoding: chunked",
             * before "Content-Length".
             */

            /* Are we C-L, chunked, or conn close? */
            v = serf_bucket_headers_get(ctx->headers, "Transfer-Encoding");

            /* Need to handle multiple transfer-encoding. */
            if (v && strcasecmp("chunked", v) == 0) {
                ctx->chunked = 1;
                ctx->body = serf_bucket_dechunk_create(ctx->body,
                                                       bkt->allocator);
            }
            else {
                v = serf_bucket_headers_get(ctx->headers, "Content-Length");
                if (v) {
                    apr_uint64_t length;
                    length = apr_strtoi64(v, NULL, 10);
                    if (errno == ERANGE) {
                        return APR_FROM_OS_ERROR(ERANGE);
                    }
                    ctx->body = serf_bucket_limit_create(ctx->body, length,
                                                         bkt->allocator);
                }
                else if ((ctx->sl.code == 204 || ctx->sl.code == 304)) {
                    ctx->state = STATE_DONE;
                }
            }

            /*
             * Instaweb would prefer to receive gzipped output if that's what
             * was asked for.
             *
             * v = serf_bucket_headers_get(ctx->headers, "Content-Encoding");
             * if (v) {
             *   * Need to handle multiple content-encoding. *
             *  if (v && strcasecmp("gzip", v) == 0) {
             *      ctx->body =
             *          serf_bucket_deflate_create(ctx->body, bkt->allocator,
             *                                     SERF_DEFLATE_GZIP);
             *  }
             *  else if (v && strcasecmp("deflate", v) == 0) {
             *      ctx->body =
             *          serf_bucket_deflate_create(ctx->body, bkt->allocator,
             *                                     SERF_DEFLATE_DEFLATE);
             *  }
             * }
             */

            /* If we're a HEAD request, we don't receive a body. */
            if (ctx->head_req) {
                ctx->state = STATE_DONE;
            }
        }
        break;
    case STATE_BODY:
        /* Don't do anything. */
        break;
    case STATE_TRAILERS:
        status = fetch_headers(bkt, ctx);
        if (SERF_BUCKET_READ_ERROR(status))
            return status;

        /* If an empty line was read, then we're done. */
        if (ctx->linebuf.state == SERF_LINEBUF_READY && !ctx->linebuf.used) {
            ctx->state = STATE_DONE;
            return APR_EOF;
        }
        break;
    case STATE_DONE:
        return APR_EOF;
    default:
        /* Not reachable */
        return APR_EGENERAL;
    }

    return status;
}
Ejemplo n.º 16
0
void	read_obj(const char* strName)
{
  int i,j,k;
  FILE *fp;
  int nwords;
  char *comment_ptr;
  char *first_word;
  float x,y,z,w;

  /* read from standard input */
  fp = fopen(strName,"r");

  while (1) {

    comment_ptr = fetch_line (fp);

    if (comment_ptr == (char *) -1)  /* end-of-file */
      break;

    /* did we get a comment? */
    if (comment_ptr) {
      make_comment (comment_ptr);
      continue;
    }

    /* if we get here, the line was not a comment */

    nwords = fetch_words();

    /* skip empty lines */
    if (nwords == 0)
      continue;

    first_word = words[0];

    if (equal_strings (first_word, "v")) {
      if (nwords < 4) {
		  fprintf (stderr, "Too few coordinates: '%s'", str_orig);
		  exit (-1);
      }
#ifdef	NEW_LOAD

	  float3 pos;
	  pos.x	=	atof (words[1]);
	  pos.y	=	atof (words[2]);
	  pos.z	=	atof (words[3]);
	  vPosition.push_back(pos);
#else
      x = atof (words[1]);
      y = atof (words[2]);
      z = atof (words[3]);
      if (nwords == 5) {
        w = atof (words[3]);

	has_w = 1;
      }
      else
        w = 1.0;
      make_vertex (x, y, z, w);
#endif
    }
    else if (equal_strings (first_word, "vn")) {
#ifdef	NEW_LOAD
		float3 n;
		n.x	=	atof (words[1]);
		n.y	=	atof (words[2]);
		n.z	=	atof (words[3]);
		vNormal.push_back(n);	
		has_normals	=	true;
#endif
    }
    else if (equal_strings (first_word, "vt")) {
#ifdef	NEW_LOAD
		float2 t;
		t.x	=	atof (words[1]);
		t.y	=	atof (words[2]);
		vUV.push_back(t);	
		texture_coords	=	true;
#endif
    }
    else if (equal_strings (first_word, "f")) {
#ifdef NEW_LOAD
		LoadFace(&words[1],nwords-1);
#else
      make_face (&words[1], nwords-1);
#endif
    }
    else {
      fprintf (stderr, "Do not recognize: '%s'\n", str_orig);
    }

  }

  nverts	=	vVertex.size();
  nfaces	=	vIndex.size()/3;
  fclose(fp);
}
Ejemplo n.º 17
0
void read_obj ( void )

/******************************************************************************/
/*
  Purpose:

    READ_OBJ reads in a Wavefront OBJ file.

  Author:

    Greg Turk
*/
{
  char *comment_ptr;
  char *first_word;
  FILE *fp;
  int i;
  int j;
  int k;
  int nwords;
  float w;
  float x;
  float y;
  float z;
/*
  Read from standard input.
*/
  fp = stdin;

  while (1)
  {
    comment_ptr = fetch_line ( fp );
/*
  End of file?
*/
    if ( comment_ptr == ( char * ) -1 )
    {
      break;
    }
/*
  Did we actually get a comment?
*/
    if ( comment_ptr )
    {
      make_comment ( comment_ptr );
      continue;
    }
/*
  If we get here, the line was not a comment.
*/
    nwords = fetch_words ( );
/*
  Skip empty lines.
*/
    if ( nwords == 0 )
    {
      continue;
    }

    first_word = words[0];

    if (equal_strings (first_word, "v"))
    {
      if (nwords < 4)
      {
	    fprintf (stderr, "Too few coordinates: '%s'", str_orig);
	    exit (-1);
      }
      x = atof (words[1]);
      y = atof (words[2]);
      z = atof (words[3]);
      if (nwords == 5)
      {
        w = atof (words[3]);
	    has_w = 1;
      }
      else
      {
        w = 1.0;
      }
      make_vertex ( x, y, z, w );
    }
    else if (equal_strings (first_word, "vn"))
    {
    }
    else if (equal_strings (first_word, "vt"))
    {
    }
    else if (equal_strings (first_word, "f"))
    {
      make_face (&words[1], nwords-1);
    }
    else
    {
      fprintf (stderr, "Do not recognize: '%s'\n", str_orig);
    }
  }
  return;
}