Beispiel #1
0
void
tail_reading_all(int N, FILE *stream)
{
  Line **line, *l;
  int i, last, first;

  if (!N)
    return;

  line = malloc(N * sizeof(Line *));

  if (!line)
  {
    fprintf(stderr, "Error in tail_reading_all\n");
    exit(EXIT_FAILURE);
  }

  for (i = 0; i < N; ++i)
    line[i] = NULL;

  last = 0;

  while ((l = io_readline(stream)))
  {
    if (line[last])
      line_free(line[last]); /* we could just reuse old buffers */

    line[last] = l;
    last = (last + 1) % N;
  }

  last = (N + last - 1) % N;

  first = (last + 1) % N;

  if (!line[first])
    first = 0;

  for (i = first; i != last; i = (i + 1) % N)
  {
    fwrite(line[i]->buf, 1, line[i]->size, stdout);
    fprintf(stdout, "\n");
  }

  if (N && line[last])
  {
    fwrite(line[last]->buf, 1, line[last]->size, stdout);
    fprintf(stdout, "\n");
  }

  for (i = 0; i < N; i++)
  {
    if (line[i])
      line_free(line[i]);
    else
      break;
  }

  free(line);
}
Beispiel #2
0
/* This links the given line into the scrollback-buffer */
static void link_to_scrollback(struct tsm_screen *con, struct line *line)
{
	struct line *tmp;

	if (con->sb_max == 0) {
		line_free(line);
		return;
	}

	/* Remove a line from the scrollback buffer if it reaches its maximum.
	 * We must take care to correctly keep the current position as the new
	 * line is linked in after we remove the top-most line here.
	 * sb_max == 0 is tested earlier so we can assume sb_max > 0 here. In
	 * other words, buf->sb_first is a valid line if sb_count >= sb_max. */
	if (con->sb_count >= con->sb_max) {
		tmp = con->sb_first;
		con->sb_first = tmp->next;
		if (tmp->next)
			tmp->next->prev = NULL;
		else
			con->sb_last = NULL;
		--con->sb_count;

		/* (position == tmp && !next) means we have sb_max=1 so set
		 * position to the new line. Otherwise, set to new first line.
		 * If position!=tmp and we have a fixed-position then nothing
		 * needs to be done because we can stay at the same line. If we
		 * have no fixed-position, we need to set the position to the
		 * next inserted line, which can be "line", too. */
		if (con->sb_pos) {
			if (con->sb_pos == tmp ||
			    !(con->flags & TSM_SCREEN_FIXED_POS)) {
				if (con->sb_pos->next)
					con->sb_pos = con->sb_pos->next;
				else
					con->sb_pos = line;
			}
		}
		line_free(tmp);
	}

	line->next = NULL;
	line->prev = con->sb_last;
	if (con->sb_last)
		con->sb_last->next = line;
	else
		con->sb_first = line;
	con->sb_last = line;
	++con->sb_count;
}
Beispiel #3
0
SHL_EXPORT
int tsm_screen_new(struct tsm_screen **out, tsm_log_t log, void *log_data)
{
	struct tsm_screen *con;
	int ret;
	unsigned int i;

	if (!out)
		return -EINVAL;

	con = malloc(sizeof(*con));
	if (!con)
		return -ENOMEM;

	memset(con, 0, sizeof(*con));
	con->ref = 1;
	con->llog = log;
	con->llog_data = log_data;
	con->age_cnt = 1;
	con->age = con->age_cnt;
	con->def_attr.fr = 255;
	con->def_attr.fg = 255;
	con->def_attr.fb = 255;

	ret = tsm_symbol_table_new(&con->sym_table);
	if (ret)
		goto err_free;

	ret = tsm_screen_resize(con, 80, 24);
	if (ret)
		goto err_free;

	llog_debug(con, "new screen");
	*out = con;

	return 0;

err_free:
	for (i = 0; i < con->line_num; ++i) {
		line_free(con->main_lines[i]);
		line_free(con->alt_lines[i]);
	}
	free(con->main_lines);
	free(con->alt_lines);
	free(con->tab_ruler);
	tsm_symbol_table_unref(con->sym_table);
	free(con);
	return ret;
}
Beispiel #4
0
/* set maximum scrollback buffer size */
void tsm_screen_set_max_sb(struct tsm_screen *con,
			       unsigned int max)
{
	struct line *line;

	if (!con)
		return;

	while (con->sb_count > max) {
		line = con->sb_first;
		con->sb_first = line->next;
		if (line->next)
			line->next->prev = NULL;
		else
			con->sb_last = NULL;
		con->sb_count--;

		/* We treat fixed/unfixed position the same here because we
		 * remove lines from the TOP of the scrollback buffer. */
		if (con->sb_pos == line)
			con->sb_pos = con->sb_first;

		line_free(line);
	}

	con->sb_max = max;
}
Beispiel #5
0
/* clear scrollback buffer */
SHL_EXPORT
void tsm_screen_clear_sb(struct tsm_screen *con)
{
	struct line *iter, *tmp;

	if (!con)
		return;

	screen_inc_age(con);
	/* TODO: more sophisticated ageing */
	con->age = con->age_cnt;

	for (iter = con->sb_first; iter; ) {
		tmp = iter;
		iter = iter->next;
		line_free(tmp);
	}

	con->sb_first = NULL;
	con->sb_last = NULL;
	con->sb_count = 0;
	con->sb_pos = NULL;

	if (con->sel_active) {
		if (con->sel_start.line) {
			con->sel_start.line = NULL;
			con->sel_start.y = SELECTION_TOP;
		}
		if (con->sel_end.line) {
			con->sel_end.line = NULL;
			con->sel_end.y = SELECTION_TOP;
		}
	}
}
Beispiel #6
0
int main( int argc, char ** argv )
{
	line_t * ptr;
	int sz;

	char * lines[3] = {
		"goober",
		"",
		"Looks like a real line.\n"
	};
	for(int i=0; i < 3; i++)
	{
		ptr = line_new();
		line_set(ptr , lines[i]);	
		sz = line_size( ptr );
		printf("ptr size: %d\n", sz );
		printf("ptr data: %s\n", line_get(ptr) );
		printf("ends in newline: %s\n", line_has_newline( ptr ) ? "Yes" : "No"  );	
		printf("null terminated: %s\n", line_null_terminated( ptr ) ? "Yes" : "No" );	
		line_free( ptr );
		printf("line after free is null? %s\n", ptr == NULL ? "Yes" : "No" );	
	}

	return 0;
}
Beispiel #7
0
/* clear scrollback buffer */
void tsm_screen_clear_sb(struct tsm_screen *con)
{
	struct line *iter, *tmp;

	if (!con)
		return;

	for (iter = con->sb_first; iter; ) {
		tmp = iter;
		iter = iter->next;
		line_free(tmp);
	}

	con->sb_first = NULL;
	con->sb_last = NULL;
	con->sb_count = 0;
	con->sb_pos = NULL;

	if (con->sel_active) {
		if (con->sel_start.line) {
			con->sel_start.line = NULL;
			con->sel_start.y = SELECTION_TOP;
		}
		if (con->sel_end.line) {
			con->sel_end.line = NULL;
			con->sel_end.y = SELECTION_TOP;
		}
	}
}
Beispiel #8
0
void fractal_polyline_free (fractal_line_struct *f) {
	if (f) {
		if (f->polyline)
			line_free(f->polyline);
		x_free(f);
	}
}
Beispiel #9
0
int tsm_screen_new(struct tsm_screen **out, tsm_log_t log)
{
	struct tsm_screen *con;
	int ret;
	unsigned int i;

	if (!out)
		return -EINVAL;

	con = malloc(sizeof(*con));
	if (!con)
		return -ENOMEM;

	memset(con, 0, sizeof(*con));
	con->ref = 1;
	con->llog = log;
	con->def_attr.fr = 255;
	con->def_attr.fg = 255;
	con->def_attr.fb = 255;

	ret = shl_timer_new(&con->timer);
	if (ret)
		goto err_free;

	ret = tsm_screen_resize(con, 80, 24);
	if (ret)
		goto err_timer;

	llog_debug(con, "new screen");
	*out = con;

	return 0;

err_timer:
	shl_timer_free(con->timer);
	for (i = 0; i < con->line_num; ++i) {
		line_free(con->main_lines[i]);
		line_free(con->alt_lines[i]);
	}
	free(con->main_lines);
	free(con->alt_lines);
	free(con->tab_ruler);
err_free:
	free(con);
	return ret;
}
Beispiel #10
0
void tsm_screen_unref(struct tsm_screen *con)
{
	unsigned int i;

	if (!con || !con->ref || --con->ref)
		return;

	llog_debug(con, "destroying screen");

	for (i = 0; i < con->line_num; ++i) {
		line_free(con->main_lines[i]);
		line_free(con->alt_lines[i]);
	}
	free(con->main_lines);
	free(con->alt_lines);
	free(con->tab_ruler);
	shl_timer_free(con->timer);
	free(con);
}
Beispiel #11
0
static void free_buffer(void)
{
	struct line *line, *tmp;

	line = buffer;

	while(line) {
		tmp = line->next;
		line_free(line);
		line = tmp;
	}
}
Beispiel #12
0
//~~~~~~~~~~~~~~~~~MAIN~~~~~~~~~~~~~~~~~~~~~~~
int main(int argc, char* argv[]){
  int debug = 0, flag = 1, arg, numRead = 0;
  char command, *stream;
  Line line = line_init( stdin);
  gen_parse_args( argc, argv, &debug);
  Htable htable = ht_init( ht_mod_hash);

  while(flag){
    arg = -1;
    fprintf( stdout, "\ncommand: ");
    line_read_line( line);
    stream = get_line( line);
    sscanf( stream, " %c%n", &command, &numRead);
    switch( command){
      case 'q': flag = 0; break;
      case 'i': sscanf( stream+numRead, "%d", &arg);
                if( arg < 0) fprintf( stderr, "MUST BE >= 0\n");
                else ht_add( htable, arg, arg, debug);
                break;
      case 'd': sscanf( stream+numRead, "%d", &arg);
                if( arg < 0) fprintf( stderr, "MUST BE >= 0\n");
                else{
                  int pos = ht_mod_hash( htable, arg);
                  node_delete( &((htable->table)[pos]), arg, arg, debug); 
                }
                break;
      case 'c': sscanf( stream+numRead, "%d", &arg);
                if( arg < 0) fprintf( stderr, "MUST BE >= 0\n");
                else{
                  if( ht_exists( htable, arg, debug) )
                    fprintf( stdout, "The value, %d, exists!\n", arg);
                  else
                    fprintf( stdout, "The value, %d, does NOT exist\n", arg);
                }
                break;
      case 'e': ht_erase( htable); break;
      case 'r': sscanf( stream+numRead, "%d", &arg);
                if( arg >= 1) ht_resize( htable, arg, debug);
                else fprintf( stderr, "Error, size must be >= 1\n");
                break;
      case 'l': ht_list( htable); break;
      case '\n': break;
      default: fprintf( stdout, "Sorry, Invalid command\n"); break;
    }
    free(stream);
  }
  line_free( line);
  ht_free( htable);
  return 0;
}
Beispiel #13
0
int main( int argc, char** argv){
  int d = 0, quit = 1, commandNum, numRead;
  char *input, command;
  Line l = line_init(stdin);
  Ll ll = ll_init();

  gen_parse_args( argc, argv, &d);

  while( quit){
    printf("Command: ");
    line_read_line( l);
    input = get_line( l);
    sscanf(input, " %c%n", &command, &numRead);
    switch( command){
      case 'q': quit = 0; break;
      case 'i': if( gen_exists_num( input+numRead) ){
                sscanf(input+numRead, "%d", &commandNum);
                ll_insert( ll, commandNum , d);
              }
              else printf("Sorry, you didn't enter a number!\n");
              break;
      case 'd': if( gen_exists_num( input+numRead) ){
                sscanf( input+numRead, "%d", &commandNum);
                ll_delete( ll, commandNum, d);
              }
              else printf("Sorry, you didn't enter a number!\n");
              break;
      case 'c': if( gen_exists_num( input+numRead) ){
                  sscanf( input+numRead, "%d", &commandNum);
                  if( ll_contains( ll, commandNum, d) )
                    printf("LIST DOES CONTAIN %d\n", commandNum);
                  else printf("LIST DOES NOT CONATAIN %d\n", commandNum);
                }
                else printf("Sorry, you didn't enter a number!\n");
                break;
      case 'e': ll_empty( ll, d); break;
      case 'l': ll_print( ll, d); break;
      case 'r': ll_print_rev( ll, d); break;
      case '?': gen_print_help(); break;
      case 'h': gen_print_help(); break;
      case '\n': break;
      default: printf("Invalid Command\n");
    }
    free( input);    
  }

  ll_free( ll, d);
  line_free( l);
  return 0;
}
Beispiel #14
0
void
line_destroy(Line *line)
{
	Eina_List          *l;
	for (l = drawing->layers; l; l = l->next)
	{                                           
		Layer              *layer;
		
		layer = (Layer *) l->data;
		layer->objects = eina_list_remove(layer->objects, line);
	}

	line_free(line);
}
Beispiel #15
0
static void merge_line(struct line *line)
{
	struct line *tmp;

	line_realloc(strlen(line->data) + strlen(line->next->data) + 1, line);

	tmp = line->next;

	line->next = line->next->next;
	if (line->next)
		line->next->prev = line;
	strcat(line->data, tmp->data);

	line_free(tmp);

	refresh(1);
}
Beispiel #16
0
/* clear scrollback buffer */
void tsm_screen_clear_sb(struct tsm_screen *con)
{
	struct line *iter, *tmp;

	if (!con)
		return;

	for (iter = con->sb_first; iter; ) {
		tmp = iter;
		iter = iter->next;
		line_free(tmp);
	}

	con->sb_first = NULL;
	con->sb_last = NULL;
	con->sb_count = 0;
	con->sb_pos = NULL;
}
Beispiel #17
0
/* set maximum scrollback buffer size */
SHL_EXPORT
void tsm_screen_set_max_sb(struct tsm_screen *con,
			       unsigned int max)
{
	struct line *line;

	if (!con)
		return;

	screen_inc_age(con);
	/* TODO: more sophisticated ageing */
	con->age = con->age_cnt;

	while (con->sb_count > max) {
		line = con->sb_first;
		con->sb_first = line->next;
		if (line->next)
			line->next->prev = NULL;
		else
			con->sb_last = NULL;
		con->sb_count--;

		/* We treat fixed/unfixed position the same here because we
		 * remove lines from the TOP of the scrollback buffer. */
		if (con->sb_pos == line)
			con->sb_pos = con->sb_first;

		if (con->sel_active) {
			if (con->sel_start.line == line) {
				con->sel_start.line = NULL;
				con->sel_start.y = SELECTION_TOP;
			}
			if (con->sel_end.line == line) {
				con->sel_end.line = NULL;
				con->sel_end.y = SELECTION_TOP;
			}
		}
		line_free(line);
	}

	con->sb_max = max;
}
Beispiel #18
0
/* not very efficient but simple */
Line *
io_readline (FILE *f)
{
  Line *l;
  int c;

  l = line_new();

  while (!feof(f))
  {
    c = fgetc(f);

    if (c == EOF || c == '\n')
      break;

    if (l->size >= l->max_size)
      line_grow(l);

    l->buf[l->size++] = c;
  }

  if (!l->size && feof(f))
  {
    line_free(l);
    return NULL;
  }

  if (l->size)
    line_compact(l);
  else
  {
    free(l->buf);
    l->buf = NULL;
  }

  return l;
}
Beispiel #19
0
/* Clears ckt and removes current circuit from database */
void
com_remcirc(wordlist *wl)
{
    struct variable *v, *next;
    struct line *dd;     /*in: the spice deck */
    struct circ *p, *prev = NULL;
#ifdef SHARED_MODULE
    TRANan *job;
#endif

    NG_IGNORE(wl);

    if (ft_curckt == NULL) {
        fprintf(cp_err, "Error: there is no circuit loaded.\n");
        return;
    }

#ifdef SHARED_MODULE
    /* This may happen only with shared ngspice during transient analysis,
       if simulation is stopped with 'bg_halt'
       and then circuit shall be removed prematurely. */
    job = (TRANan *) ft_curckt->ci_ckt->CKTcurJob;
    if (job && (job->JOBtype == 4) && (job->TRANplot))
        SPfrontEnd->OUTendPlot (job->TRANplot);
#endif

    /* delete numparam data structure dicoS */
    nupa_del_dicoS();

    dbfree(ft_curckt->ci_dbs);
    ft_curckt->ci_dbs = NULL;
    dbs = NULL;

    /* The next lines stem from com_rset */
    INPkillMods();

    if_cktfree(ft_curckt->ci_ckt, ft_curckt->ci_symtab);
    for (v = ft_curckt->ci_vars; v; v = next) {
        next = v->va_next;
        tfree(v->va_name);
        tfree(v);
    }
    ft_curckt->ci_vars = NULL;
    /* delete the deck, parameter list, and options list in ft_curckt */
    dd = ft_curckt->ci_deck;
    line_free(dd, TRUE);
    dd = ft_curckt->ci_param;
    line_free(dd, TRUE);
    dd = ft_curckt->ci_options;
    line_free(dd, TRUE);

    wl_free(ft_curckt->ci_commands);

    tfree(ft_curckt->FTEstats);

    ft_sim->deleteTask (ft_curckt->ci_ckt, ft_curckt->ci_defTask);

    if (ft_curckt->ci_name)
        tfree(ft_curckt->ci_name);
    if (ft_curckt->ci_filename)
        tfree(ft_curckt->ci_filename);

    /* delete the actual circuit entry from ft_circuits */
    for (p = ft_circuits; p; p = p->ci_next) {
        if (ft_curckt == p) {
            if (prev == NULL) {
                ft_circuits = p->ci_next;
                tfree(p);
                p = NULL;
                break;
            } else {
                prev->ci_next = p->ci_next;
                tfree(p);
                p = NULL;
                break;
            }
        }
        prev = p;
    }

    /* make first entry in ft_circuits the actual circuit (or NULL) */
    ft_curckt = ft_circuits;
    if (ft_curckt) {
        modtab = ft_curckt->ci_modtab;
        dbs = ft_curckt->ci_dbs;
    }
}
Beispiel #20
0
SHL_EXPORT
int tsm_screen_resize(struct tsm_screen *con, unsigned int x,
		      unsigned int y)
{
	struct line **cache;
	unsigned int i, j, width, diff, start;
	int ret;
	bool *tab_ruler;

	if (!con || !x || !y)
		return -EINVAL;

	if (con->size_x == x && con->size_y == y)
		return 0;

	/* First make sure the line buffer is big enough for our new screen.
	 * That is, allocate all new lines and make sure each line has enough
	 * cells to hold the new screen or the current screen. If we fail, we
	 * can safely return -ENOMEM and the buffer is still valid. We must
	 * allocate the new lines to at least the same size as the current
	 * lines. Otherwise, if this function fails in later turns, we will have
	 * invalid lines in the buffer. */
	if (y > con->line_num) {
		/* resize main buffer */
		cache = realloc(con->main_lines, sizeof(struct line*) * y);
		if (!cache)
			return -ENOMEM;

		if (con->lines == con->main_lines)
			con->lines = cache;
		con->main_lines = cache;

		/* resize alt buffer */
		cache = realloc(con->alt_lines, sizeof(struct line*) * y);
		if (!cache)
			return -ENOMEM;

		if (con->lines == con->alt_lines)
			con->lines = cache;
		con->alt_lines = cache;

		/* allocate new lines */
		if (x > con->size_x)
			width = x;
		else
			width = con->size_x;

		while (con->line_num < y) {
			ret = line_new(con, &con->main_lines[con->line_num],
				       width);
			if (ret)
				return ret;

			ret = line_new(con, &con->alt_lines[con->line_num],
				       width);
			if (ret) {
				line_free(con->main_lines[con->line_num]);
				return ret;
			}

			++con->line_num;
		}
	}

	/* Resize all lines in the buffer if we increase screen width. This
	 * will guarantee that all lines are big enough so we can resize the
	 * buffer without reallocating them later. */
	if (x > con->size_x) {
		tab_ruler = realloc(con->tab_ruler, sizeof(bool) * x);
		if (!tab_ruler)
			return -ENOMEM;
		con->tab_ruler = tab_ruler;

		for (i = 0; i < con->line_num; ++i) {
			ret = line_resize(con, con->main_lines[i], x);
			if (ret)
				return ret;

			ret = line_resize(con, con->alt_lines[i], x);
			if (ret)
				return ret;
		}
	}

	screen_inc_age(con);

	/* clear expansion/padding area */
	start = x;
	if (x > con->size_x)
		start = con->size_x;
	for (j = 0; j < con->line_num; ++j) {
		/* main-lines may go into SB, so clear all cells */
		i = 0;
		if (j < con->size_y)
			i = start;

		for ( ; i < con->main_lines[j]->size; ++i)
			screen_cell_init(con, &con->main_lines[j]->cells[i]);

		/* alt-lines never go into SB, only clear visible cells */
		i = 0;
		if (j < con->size_y)
			i = con->size_x;

		for ( ; i < x; ++i)
			screen_cell_init(con, &con->alt_lines[j]->cells[i]);
	}

	/* xterm destroys margins on resize, so do we */
	con->margin_top = 0;
	con->margin_bottom = con->size_y - 1;

	/* reset tabs */
	for (i = 0; i < x; ++i) {
		if (i % 8 == 0)
			con->tab_ruler[i] = true;
		else
			con->tab_ruler[i] = false;
	}

	/* We need to adjust x-size first as screen_scroll_up() and friends may
	 * have to reallocate lines. The y-size is adjusted after them to avoid
	 * missing lines when shrinking y-size.
	 * We need to carefully look for the functions that we call here as they
	 * have stronger invariants as when called normally. */

	con->size_x = x;
	if (con->cursor_x >= con->size_x)
		move_cursor(con, con->size_x - 1, con->cursor_y);

	/* scroll buffer if screen height shrinks */
	if (y < con->size_y) {
		diff = con->size_y - y;
		screen_scroll_up(con, diff);
		if (con->cursor_y > diff)
			move_cursor(con, con->cursor_x, con->cursor_y - diff);
		else
			move_cursor(con, con->cursor_x, 0);
	}

	con->size_y = y;
	con->margin_bottom = con->size_y - 1;
	if (con->cursor_y >= con->size_y)
		move_cursor(con, con->cursor_x, con->size_y - 1);

	return 0;
}