Exemple #1
0
void winch_handler(int sig) {
	struct winsize	ws;

	if(ioctl(0, TIOCGWINSZ, &ws) == -1) {
		fprintf(stderr, "ioctl() err\n");
	} else {
		// Work out our current index into the line...
		int pos = (row * width) + col;

		// Update the metrics...
		width = ws.ws_col;
		height = ws.ws_row;

		// Different terms do things differently, so our safest option
		// is to clear the screen!
		// TODO: is there a better way?
		putp(clear_screen);
		row = 0;
		col = 0;
		redraw_line(0);

		// Now reposition our cursor back to where it was...
		move_to(pos/width, pos%width);

		// Make sure we update the screen...
		fflush(stdout);
	}
}
Exemple #2
0
L9BOOL os_input (char *ibuff, int size)
{
  int c, pos = 0;
  int inputting = 1;
  unsigned long saved_pen;
  UWORD qualifier;

  *ibuff = '\0';
  cursor (*ibuff);
  saved_pen = RastPort->FgPen;
  SetAPen (RastPort, 2);

  while (inputting)
  {
    set_window (1, TextLength (RastPort, ibuff + pos, strlen (ibuff + pos)));
    c = get_key (&qualifier, *(ibuff + pos), 1);
    set_window (0, 0);

    switch (c)
    {
    case -1:
      cursor (*(ibuff + pos));
      SetAPen (RastPort, 1);
      MoreCount = 0;
      return 0;

    case -2:
      cursor (*(ibuff + pos));
      cursor_right (ibuff, &pos);
      strcpy(ibuff,"save");
      SetAPen (RastPort, 1);
      MoreCount = 0;
      return 1;

    case -3:
      cursor (*(ibuff + pos));
      cursor_right (ibuff, &pos);
      strcpy(ibuff,"#restore");
      SetAPen (RastPort, 1);
      MoreCount = 0;
      return 1;

    case 13:
      if (strlen (ibuff) > 0)
      {
	cursor (*(ibuff + pos));
	cursor_right (ibuff, &pos);
	SetAPen (RastPort, 1);
	store_hist (ibuff);
	MoreCount = 0;
	return 1;
      }
      break;

    case 264:
      redraw_line (ibuff, &pos, size);
      break;

    case 127:
      if (pos > 0)
      {
	int deleted;

	cursor (*(ibuff + pos));
	deleted = *(ibuff + pos - 1);
	memmove (ibuff + pos - 1, ibuff + pos, strlen (ibuff) - pos + 1);
	move_text (-char_len (deleted), Window->Width - Window->BorderRight);
	Move (RastPort, RastPort->cp_x - char_len (deleted), RastPort->cp_y);
	pos--;
	cursor (*(ibuff + pos));
      }
      break;

    case 260:
      if (pos < strlen (ibuff))
      {
	int deleted;

	cursor (*(ibuff + pos));
	deleted = *(ibuff + pos);
	memmove (ibuff + pos, ibuff + pos + 1, strlen (ibuff) - pos);
	Move (RastPort, RastPort->cp_x + char_len (deleted), RastPort->cp_y);
	move_text (-char_len (deleted), Window->Width - Window->BorderRight);
	Move (RastPort, RastPort->cp_x - char_len (deleted), RastPort->cp_y);
	cursor (*(ibuff + pos));
      }
      break;

    case 131:
      if (pos > 0)
      {
	if (qualifier & QUALIFIER_SHIFT)
	{
	  cursor (*(ibuff + pos));
	  cursor_left (ibuff, &pos);
	  cursor (*(ibuff + pos));
	}
	else
	{
	  cursor (*(ibuff + pos--));
	  Move (RastPort, RastPort->cp_x - char_len (*(ibuff + pos)), RastPort->cp_y);
	  cursor (*(ibuff + pos));
	}
      }
      break;

    case 132:
      if (pos < strlen (ibuff))
      {
	if (qualifier & QUALIFIER_SHIFT)
	{
	  cursor (*(ibuff + pos));
	  cursor_right (ibuff, &pos);
	  cursor (*(ibuff + pos));
	}
	else
	{
	  cursor (*(ibuff + pos));
	  Move (RastPort, RastPort->cp_x + char_len (*(ibuff + pos)), RastPort->cp_y);
	  pos++;
	  cursor (*(ibuff + pos));
	}
      }
      break;

    case 129:
      if (qualifier & QUALIFIER_SHIFT)
      {
	HistoryPosition = 0;
	while ((*(History + HistoryPosition) == 0) && (HistoryPosition < HISTORY_LINES))
	  HistoryPosition++;
	into_buffer (ibuff, HistoryPosition == HISTORY_LINES ?
	  (unsigned char *) "" : *(History + HistoryPosition), &pos, size);
      }
      else
      {
	if ((HistoryPosition > 0) && (*(History + HistoryPosition - 1) != 0))
	{
	  HistoryPosition--;
	  into_buffer (ibuff, *(History + HistoryPosition), &pos, size);
	}
      }
      break;

    case 130:
      if (qualifier & QUALIFIER_SHIFT)
      {
	HistoryPosition = HISTORY_LINES;
	into_buffer (ibuff, "", &pos, size);
      }
      else
      {
	if ((HistoryPosition < HISTORY_LINES - 1) && (*(History + HistoryPosition + 1) != 0))
	{
	  HistoryPosition++;
	  into_buffer (ibuff, HistoryPosition == HISTORY_LINES ?
	    (unsigned char *) "" : *(History + HistoryPosition), &pos, size);
	}
	else
	{
	  HistoryPosition = HISTORY_LINES;
	  into_buffer (ibuff, "", &pos, size);
	}
      }
      break;

    default:
      if (c >= 32 && c <= 127)
      {
	if ((strlen (ibuff) < size) && (fit_text (ibuff + pos, c)))
	{
	  memmove (ibuff + pos + 1, ibuff + pos, strlen (ibuff) - pos + 1);
	  *(ibuff + pos) = c;

	  pos++;
	  cursor (*(ibuff + pos));
	  move_text (char_len (c), Window->Width - Window->BorderRight);
	  os_printchar (c);
	  cursor (*(ibuff + pos));
	}
      }
      break;
    }
  }
}
Exemple #3
0
int vdehist_term_to_mgmt(struct vdehiststat *st)
{
	unsigned char buf[BUFSIZE];
	int n,i,rv=0;
	n=vdehist_termread(st->termfd,buf,BUFSIZE);
	//printf("termto mgmt N%d %x %x %x %x\n",n,buf[0],buf[1],buf[2],buf[3]);
	if (n==0)
		return 1;
	else if (n<0)
		return n;
	else {
		for (i=0;i<n && strlen(st->linebuf)<BUFSIZE;i++) {
			if (buf[i] == 0xff && buf[i+1] == 0xff)
				i++;
			if(buf[i]==0) buf[i]='\n'; /*telnet encode \n as a 0 when in raw mode*/
			if (buf[i] == 0xff && buf[i+1] != 0xff) {
				i+=telnet_options(st,buf+i);
			} else 

				if(buf[i] == 0x1b) {
					/* ESCAPE! */
					if (buf[i+1]=='[' && st->status == HIST_COMMAND) {
						st->edited=1;
						switch (buf[i+2]) {
							case 'A': //fprintf(stderr,"UP\n");
								erase_line(st,0);
								put_history(st);
								get_history(1,st);
								redraw_line(st,0);
								st->bufindex=strlen(st->linebuf);
								break;
							case 'B': //fprintf(stderr,"DOWN\n");
								erase_line(st,0);
								put_history(st);
								get_history(-1,st);
								redraw_line(st,0);
								break;
							case 'C': //fprintf(stderr,"RIGHT\n");
								if (st->linebuf[st->bufindex] != '\0') {
									vdehist_termwrite(st->termfd,"\033[C",3);
									(st->bufindex)++;
								}
								break;
							case 'D': //fprintf(stderr,"LEFT\n");
								if (st->bufindex > 0) {
									vdehist_termwrite(st->termfd,"\033[D",3);
									(st->bufindex)--;
								}
								break;
						}
						i+=3;
					}
					else
						i+=2;/* ignored */
				} else if(buf[i] < 0x20 && !(buf[i] == '\n' || buf[i] == '\r')) {
					/*ctrl*/
					if (buf[i] == 4) /*ctrl D is a shortcut for UNIX people! */ {
						rv=1;
						break;
					}
					switch (buf[i]) {
						case 3:  /*ctrl C cleans the current buffer */
							erase_line(st,0);
							st->bufindex=0;
							st->linebuf[(st->bufindex)]=0;
							break;
						case 12: /* ctrl L redraw */
							erase_line(st,1);
							redraw_line(st,1);
							break;
						case 1: /* ctrl A begin of line */
							erase_line(st,0);
							st->bufindex=0;
							redraw_line(st,0);
							break;
						case 5: /* ctrl E endofline */
							erase_line(st,0);
							st->bufindex=strlen(st->linebuf);
							redraw_line(st,0);
						case '\t': /* tab */
							if (st->lastchar== '\t') {
								erase_line(st,1);
								showexpand(st->linebuf,st->bufindex,st->termfd);
								redraw_line(st,1);
							} else {
								erase_line(st,0);
								st->bufindex=tabexpand(st->linebuf,st->bufindex,BUFSIZE);
								redraw_line(st,0);
							}
							break;
					}
				} else if(buf[i] == 0x7f) {
					if(st->bufindex > 0) {
						char *x;
						(st->bufindex)--;
						x=st->linebuf+st->bufindex;
						memmove(x,x+1,strlen(x));
						if (st->echo && !(st->status & HIST_PASSWDFLAG)) {
							if (st->edited)
								vdehist_termwrite(st->termfd,"\010\033[P",4);
							else
								vdehist_termwrite(st->termfd,"\010 \010",3);
						}
					}
				} else {
					if (st->echo && !(st->status & HIST_PASSWDFLAG)) {
						if (st->edited && buf[i] >= ' ')
							vdehist_termwrite(st->termfd,"\033[@",3);
						vdehist_termwrite(st->termfd,&(buf[i]),1);
					}
					if (buf[i] != '\r') {
						if (buf[i]=='\n') {
							if (st->status == HIST_COMMAND) {
								st->histindex=0;
								put_history(st);
								if (strlen(st->linebuf) > 0)
									shift_history(st);
							}
							st->bufindex=strlen(st->linebuf);
							if ((rv=hist_sendcmd(st)) != 0)
								break; 
							st->bufindex=st->edited=st->histindex=0;
							st->linebuf[(st->bufindex)]=0;
						} else {
							char *x;
							x=st->linebuf+st->bufindex;
							memmove(x+1,x,strlen(x)+1);
							st->linebuf[(st->bufindex)++]=buf[i];
						}
					}
				}
			st->lastchar=buf[i];
		}
	}
	return rv;
}
Exemple #4
0
void vdehist_mgmt_to_term(struct vdehiststat *st)
{
	char buf[BUFSIZE+1];
	int n=0,ib=0;
	/* erase the input line */
	erase_line(st,1);
	/* if the communication with the manager object holds, print the output*/
	//fprintf(stderr,"mgmt2term\n");
	if (st->mgmtfd) {
		n=vdehist_vderead(st->mgmtfd,buf,BUFSIZE);
		//fprintf(stderr,"mgmt2term n=%d\n",n);
		buf[n]=0;
		while (n>0) {
			for(ib=0;ib<n;ib++)
			{
				st->vlinebuf[(st->vbufindex)++]=buf[ib];
				if (buf[ib] == '\n') {
					st->vlinebuf[(st->vbufindex)-1]='\r';
					st->vlinebuf[(st->vbufindex)]='\n';
					st->vlinebuf[(st->vbufindex)+1]='\0';
					(st->vbufindex)++;
					if (st->vindata) {
						if (st->vlinebuf[0]=='.' && st->vlinebuf[1]=='\r')
							st->vindata=0;
						else
							vdehist_termwrite(st->termfd,st->vlinebuf,(st->vbufindex));
					} else {
						char *message=st->vlinebuf;
						//fprintf(stderr,"MSG1 \"%s\"\n",message);
						while (*message != '\0' &&
								!(isdigit(message[0]) &&
									isdigit(message[1]) &&
									isdigit(message[2]) &&
									isdigit(message[3])))
							message++;
						if (strncmp(message,"0000",4)==0)
							st->vindata=1;
						else if (isdigit(message[1]) &&
								isdigit(message[2]) &&
								isdigit(message[3])) {
							if(message[0]=='1') {
								message+=5;
								vdehist_termwrite(st->termfd,message,strlen(message));
							} else if (message[0]=='3') {
								message+=5;
								vdehist_termwrite(st->termfd,"** DBG MSG: ",12);
								vdehist_termwrite(st->termfd,(message),strlen(message));
							}
						}
					}
					(st->vbufindex)=0;
				}
			}
			n=vdehist_vderead(st->mgmtfd,buf,BUFSIZE);
		}
	}
	if (commandlist == NULL && st->mgmtfd >= 0) 
		vdehist_create_commandlist(st->mgmtfd);
	/* redraw the input line */
	redraw_line(st,1);
}
void key_map::draw_screen()
{
    bool finished = false;
    st_position config_text_pos;
    st_position cursor_pos;
    short _pick_pos = 0;

    config_text_pos.x = graphLib.get_config_menu_pos().x + 74;
    config_text_pos.y = graphLib.get_config_menu_pos().y + 40;
    cursor_pos = config_text_pos;

    graphLib.clear_area(config_text_pos.x-1, config_text_pos.y-1, 180,  180, 0, 0, 0);
    input.clean();
    input.waitTime(300);

    for (unsigned int i=0; i<_keys_list.size(); i++) {
        graphLib.draw_text(config_text_pos.x, config_text_pos.y + i*CURSOR_SPACING, _keys_list[i].c_str());
        redraw_line(i);
    }
    graphLib.draw_text(config_text_pos.x, config_text_pos.y + _keys_list.size()*CURSOR_SPACING, "RETURN");
    draw_lib.update_screen();

    //cout << "scenesLib::option_picker::START\n";
    graphLib.drawCursor(st_position(cursor_pos.x-CURSOR_SPACING, cursor_pos.y+(_pick_pos*CURSOR_SPACING)));

    while (finished == false) {
        input.readInput();
        if (input.p1_input[BTN_START]) {
            if (_pick_pos == (short)_keys_list.size()) {
                std::cout << "key_map::draw_screen - FINISHED #1" << std::endl;
                finished = true;
            } else {
                graphLib.draw_text(config_text_pos.x, config_text_pos.y + _keys_list.size()*CURSOR_SPACING+CURSOR_SPACING*2, "PRESS NEW KEY/BUTTON"); //input code (number)
                draw_lib.update_screen();
                //format_v_2_1_1::st_key_config new_key = input.get_pressed_key();
                graphLib.clear_area(config_text_pos.x, config_text_pos.y + _keys_list.size()*CURSOR_SPACING+CURSOR_SPACING*2-1, 180,  CURSOR_SPACING+1, 0, 0, 0);
                ///@TODO - key_config[_pick_pos].key_type = new_key.key_type;
                ///@TODO - key_config[_pick_pos].key_number = new_key.key_number;
                redraw_line(_pick_pos);
                draw_lib.update_screen();
            }
        }
        if (input.p1_input[BTN_DOWN]) {
                soundManager.play_sfx(SFX_CURSOR);
                graphLib.eraseCursor(st_position(cursor_pos.x-CURSOR_SPACING, cursor_pos.y+(_pick_pos*CURSOR_SPACING)));
                _pick_pos++;
                if (_pick_pos >= (short)_keys_list.size()+1) {
                    _pick_pos = 0;
                }
                graphLib.drawCursor(st_position(cursor_pos.x-CURSOR_SPACING, cursor_pos.y+(_pick_pos*CURSOR_SPACING)));
        }
        if (input.p1_input[BTN_UP]) {
                soundManager.play_sfx(SFX_CURSOR);
                graphLib.eraseCursor(st_position(cursor_pos.x-CURSOR_SPACING, cursor_pos.y+(_pick_pos*CURSOR_SPACING)));
                _pick_pos--;
                if (_pick_pos < 0) {
                    _pick_pos = _keys_list.size();
                }
                graphLib.drawCursor(st_position(cursor_pos.x-CURSOR_SPACING, cursor_pos.y+(_pick_pos*CURSOR_SPACING)));
        }
        if (input.p1_input[BTN_QUIT]) {
            std::cout << "key_map::draw_screen - FINISHED #2" << std::endl;
            finished = true;
        }
        input.clean();
        input.waitTime(10);
        draw_lib.update_screen();
    }
}
Exemple #6
0
char *
readline(const char *prompt)
{
    int cur_char;
    char *new_line;


    /* start with a string of MAXBUF chars */
    if (line_len != 0) {
	free(cur_line);
	line_len = 0;
    }
    cur_line = gp_alloc(MAXBUF, "readline");
    line_len = MAXBUF;

    /* set the termio so we can do our own input processing */
    set_termio();

    /* print the prompt */
    fputs(prompt, stderr);
    cur_line[0] = '\0';
    cur_pos = 0;
    max_pos = 0;
    cur_entry = NULL;

    /* get characters */
    for (;;) {

	cur_char = special_getc();

	/* Accumulate ascii (7bit) printable characters
	 * and all leading 8bit characters.
	 */
	if ((isprint(cur_char)
	      || (((cur_char & 0x80) != 0) && (cur_char != EOF)))
	    && (cur_char != 0x09) /* TAB is a printable character in some locales */
	    ) {
	    size_t i;

	    if (max_pos + 1 >= line_len) {
		extend_cur_line();
	    }
	    for (i = max_pos; i > cur_pos; i--) {
		cur_line[i] = cur_line[i - 1];
	    }
	    user_putc(cur_char);

	    cur_line[cur_pos] = cur_char;
	    cur_pos += 1;
	    max_pos += 1;
	    cur_line[max_pos] = '\0';

	    if (cur_pos < max_pos) {
		switch (encoding) {
		case S_ENC_UTF8:
		    if ((cur_char & 0xc0) == 0)
			fix_line(); /* Normal ascii character */
		    else if ((cur_char & 0xc0) == 0xc0)
			; /* start of a multibyte sequence. */
		    else if (((cur_char & 0xc0) == 0x80) &&
			 ((unsigned char)(cur_line[cur_pos-2]) >= 0xe0))
			; /* second byte of a >2 byte sequence */
		    else {
			/* Last char of multi-byte sequence */
			fix_line();
		    }
		    break;
		default:
		    fix_line();
		    break;
		}
	    }

	/* else interpret unix terminal driver characters */
#ifdef VERASE
	} else if (cur_char == term_chars[VERASE]) {	/* ^H */
	    delete_backward();
#endif /* VERASE */
#ifdef VEOF
	} else if (cur_char == term_chars[VEOF]) {	/* ^D? */
	    if (max_pos == 0) {
		reset_termio();
		return ((char *) NULL);
	    }
	    delete_forward();
#endif /* VEOF */
#ifdef VKILL
	} else if (cur_char == term_chars[VKILL]) {	/* ^U? */
	    clear_line(prompt);
#endif /* VKILL */
#ifdef VWERASE
	} else if (cur_char == term_chars[VWERASE]) {	/* ^W? */
	    delete_previous_word();
#endif /* VWERASE */
#ifdef VREPRINT
	} else if (cur_char == term_chars[VREPRINT]) {	/* ^R? */
	    putc(NEWLINE, stderr);	/* go to a fresh line */
	    redraw_line(prompt);
#endif /* VREPRINT */
#ifdef VSUSP
	} else if (cur_char == term_chars[VSUSP]) {
	    reset_termio();
	    kill(0, SIGTSTP);

	    /* process stops here */

	    set_termio();
	    /* print the prompt */
	    redraw_line(prompt);
#endif /* VSUSP */
	} else {
	    /* do normal editing commands */
	    /* some of these are also done above */
	    switch (cur_char) {
	    case EOF:
		reset_termio();
		return ((char *) NULL);
	    case 001:		/* ^A */
		while (cur_pos > 0)
		    backspace();
		break;
	    case 002:		/* ^B */
		if (cur_pos > 0)
		    backspace();
		break;
	    case 005:		/* ^E */
		while (cur_pos < max_pos) {
		    user_putc(cur_line[cur_pos]);
		    cur_pos += 1;
		}
		break;
	    case 006:		/* ^F */
		if (cur_pos < max_pos) {
		    step_forward();
		}
		break;
#if defined(HAVE_DIRENT_H) || defined(WIN32)
	    case 011:		/* ^I / TAB */
		tab_completion(TRUE); /* next tab completion */
		break;
	    case 034:		/* remapped by wtext.c or ansi_getc from Shift-Tab */
		tab_completion(FALSE); /* previous tab completion */
		break;
#endif
	    case 013:		/* ^K */
		clear_eoline(prompt);
		max_pos = cur_pos;
		break;
	    case 020:		/* ^P */
		if (history != NULL) {
		    if (cur_entry == NULL) {
			cur_entry = history;
			clear_line(prompt);
			copy_line(cur_entry->line);
		    } else if (cur_entry->prev != NULL) {
			cur_entry = cur_entry->prev;
			clear_line(prompt);
			copy_line(cur_entry->line);
		    }
		}
		break;
	    case 016:		/* ^N */
		if (cur_entry != NULL) {
		    cur_entry = cur_entry->next;
		    clear_line(prompt);
		    if (cur_entry != NULL)
			copy_line(cur_entry->line);
		    else
			cur_pos = max_pos = 0;
		}
		break;
	    case 014:		/* ^L */
	    case 022:		/* ^R */
		putc(NEWLINE, stderr);	/* go to a fresh line */
		redraw_line(prompt);
		break;
#ifndef DEL_ERASES_CURRENT_CHAR
	    case 0177:		/* DEL */
	    case 023:		/* Re-mapped from CSI~3 in ansi_getc() */
#endif
	    case 010:		/* ^H */
		delete_backward();
		break;
	    case 004:		/* ^D */
		if (max_pos == 0) {
		    reset_termio();
		    return ((char *) NULL);
		}
		/* intentionally omitting break */
#ifdef DEL_ERASES_CURRENT_CHAR
	    case 0177:		/* DEL */
	    case 023:		/* Re-mapped from CSI~3 in ansi_getc() */
#endif
		delete_forward();
		break;
	    case 025:		/* ^U */
		clear_line(prompt);
		break;
	    case 027:		/* ^W */
		delete_previous_word();
		break;
	    case '\n':		/* ^J */
	    case '\r':		/* ^M */
		cur_line[max_pos + 1] = '\0';
#ifdef OS2
		while (cur_pos < max_pos) {
		    user_putc(cur_line[cur_pos]);
		    cur_pos += 1;
		}
#endif
		putc(NEWLINE, stderr);

		/* Shrink the block down to fit the string ?
		 * if the alloc fails, we still own block at cur_line,
		 * but this shouldn't really fail.
		 */
		new_line = (char *) gp_realloc(cur_line, strlen(cur_line) + 1,
					       "line resize");
		if (new_line)
		    cur_line = new_line;
		/* else we just hang on to what we had - it's not a problem */

		line_len = 0;
		FPRINTF((stderr, "Resizing input line to %d chars\n", strlen(cur_line)));
		reset_termio();
		return (cur_line);
	    default:
		break;
	    }
	}
    }
}
char * editLine(char *prompt)
{
    /* The line to be edited is stored in cur_line.*/
    /* get characters */
    int cur_char;
    
    for(;;) {
	cur_char = special_getc();
	
	if(isprint(cur_char) || (((unsigned char)cur_char > 0x7f) &&
				 cur_char != EOF) || cur_char == '\t') {
	    int i,inc = 1;
	    if(cur_char == '\t') {
		inc = TABSTOPS;
		cur_char = ' ';
	    }
	    

	    if(max_pos+inc>=line_len) 
		extend_cur_line();

	    for(i=max_pos+inc-1; i-inc>=cur_pos; i--) {
		    cur_line[i] = cur_line[i-inc];
		}
	    max_pos += inc;
	    while(inc--) {
		user_putc(cur_char);
		cur_line[cur_pos++] = cur_char;
	    }
	    if (cur_pos < max_pos)
		fix_line();
	    cur_line[max_pos] = '\0';
#if MATCHPAREN
	    switch(cur_char) {
	      case ')':backupTo('(',')');break;
	      case ']':backupTo('[',']');break;
	    }
#endif
#if defined(VERASE) 
	} else if(cur_char == term_chars[VERASE] ){ /* DEL? */
	    if(cur_pos > 0) {
		int i;
		cur_pos -= 1;
		backspace();
		for(i=cur_pos; i<max_pos; i++)
		    cur_line[i] = cur_line[i+1];
		max_pos -= 1;
		fix_line();
	    }
	} else if(cur_char == term_chars[VEOF] ){ /* ^D? */
	    if(max_pos == 0) {
		copy_line("to exit EiC, enter  :exit\n");
		user_putc(BELL);

		reset_termio();		
		return((char*)NULL);
	    }
	    if((cur_pos < max_pos)&&(cur_char == 004)) { /* ^D */
		int i;
		for(i=cur_pos; i<max_pos; i++)
		    cur_line[i] = cur_line[i+1];
		max_pos -= 1;
		fix_line();
	    }

	} else if(cur_char == term_chars[VKILL] ){ /* ^U? */
	    clear_line(prompt);

	} else if(cur_char == term_chars[VWERASE] ){ /* ^W? */
	    while((cur_pos > 0) &&
		  (cur_line[cur_pos-1] == SPACE)) {
		cur_pos -= 1;
		backspace();
	    }
	    while((cur_pos > 0) &&
		  (cur_line[cur_pos-1] != SPACE)) {
		cur_pos -= 1;
		backspace();
	    }
	    clear_eoline();
	    max_pos = cur_pos;


	} else if(cur_char == term_chars[VREPRINT] ){ /* ^R? */
	    user_putc(NEWLINE); /* go to a fresh line */
	    redraw_line(prompt);


	} else if(cur_char == term_chars[VSUSP]) {
	    reset_termio();
	    kill(0, SIGTSTP);

	    /* process stops here */

	    set_termio();
	    /* print the prompt */
	    redraw_line(prompt);
#endif
	} else {
	    /* do normal editing commands */
	    /* some of these are also done above */
	    int i;
	    switch(cur_char) {
	      case EOF:
		reset_termio();
		return((char *)NULL);
	      case 001:		/* ^A */
		while(cur_pos > 0) {
		    cur_pos -= 1;
		    backspace();
		}
		break;
	      case 002:		/* ^B */
		if(cur_pos > 0) {
		    cur_pos -= 1;
		    backspace();
		}
		break;
	      case 005:		/* ^E */
		while(cur_pos < max_pos) {
		    user_putc(cur_line[cur_pos]);
		    cur_pos += 1;
		}
		break;
	      case 006:		/* ^F */
		if(cur_pos < max_pos) {
		    user_putc(cur_line[cur_pos]);
		    cur_pos += 1;
		}
		break;
	      case 013:		/* ^K */
		clear_eoline();
		max_pos = cur_pos;
		break;
		
	      case 020:		/* ^P */
		if(history != NULL) {
		    if(cur_entry == NULL) {
			cur_entry = history;
			clear_line(prompt);
			copy_line(cur_entry->line);
		    } else if(cur_entry->prev != NULL) {
			cur_entry = cur_entry->prev;
			clear_line(prompt);
			copy_line(cur_entry->line);
		    }else
			user_putc(BELL);
		}else
		    user_putc(BELL);
		break;

	    case 016:		/* ^N */
		if(cur_entry != NULL) {
		    cur_entry = cur_entry->next;
		    clear_line(prompt);
		    if(cur_entry != NULL) 
			copy_line(cur_entry->line);
		    else
			cur_pos = max_pos = 0;
		}else
		    user_putc(BELL);
		break;
	      case 014:		/* ^L */
	      case 022:		/* ^R */
		user_putc(NEWLINE); /* go to a fresh line */
		redraw_line(prompt);
		break;
	      case 0177:	/* DEL */
	      case 010:		/* ^H */
		if(cur_pos > 0) {
		    cur_pos -= 1;
		    backspace();
		    for(i=cur_pos; i<max_pos; i++)
			cur_line[i] = cur_line[i+1];
		    max_pos -= 1;
		    fix_line();
		}
		break;
	      case 004:		/* ^D */
		if(max_pos == 0) {
		    reset_termio();
		    return((char *)NULL);
		}
		if(cur_pos < max_pos) {
		    for(i=cur_pos; i<max_pos; i++)
			cur_line[i] = cur_line[i+1];
		    max_pos -= 1;
		    fix_line();
		}
		break;
	      case 025:		/* ^U */
		clear_line(prompt);
		break;
	      case 027:		/* ^W */
		while((cur_pos > 0) &&
		      (cur_line[cur_pos-1] == SPACE)) {
		    cur_pos -= 1;
		    backspace();
		}
		while((cur_pos > 0) &&
		      (cur_line[cur_pos-1] != SPACE)) {
		    cur_pos -= 1;
		    backspace();
		}
		clear_eoline();
		max_pos = cur_pos;
		break;
	    case '\n':	/* ^J */
	    case '\r':	/* ^M */
		user_putc(NEWLINE);
		cur_line[max_pos+1] = '\0';
		cur_line = (char *)ralloc(cur_line,
					  (unsigned
					   long)(strlen(cur_line)+2),
					  "line resize");
		line_len=0;
		
		reset_termio();
		return cur_line;
	      default:
		break;
	    }
	}
    }
}
Exemple #8
0
int readline(lua_State *L) {
	int		rc;
	char	*temp;
	char	*termtype = getenv("TERM");

	if(!termtype) {
		fprintf(stderr, "no TERM defined!\n");
		exit(1);
	}

	ioctl(0, TCGETS, &old_termio);
	new_termio = old_termio;
	new_termio.c_lflag &= ~ECHO;
	new_termio.c_lflag &= ~ICANON;

	ioctl(0, TCSETS, &new_termio);
	

	signal(SIGQUIT, int_handler);
	signal(SIGINT, int_handler);
	signal(SIGWINCH, winch_handler);
	printf(sample);
	printf("\n");

	// Setup terminfo stuff...
	rc = setupterm((char *)0, 1, (int *)0);
	fprintf(stderr, "rc=%d\n", rc);
	
//	rc = tgetent(NULL, termtype);
//	fprintf(stderr, "rc=%d\n", rc);
//
/*	int y;
	for(y=0; strnames[y]; y++) {
		fprintf(stderr, "%d: %s\n", y, strnames[y]);
	}
	fprintf(stderr, "lines=%s\n", cursor_down);
*/
	height = lines;
	width = columns;
	printf("w=%d h=%d\n", width, height);

	col = 0;
	row = 0;

	init_terminfo_data();

	int t;
	char *x = parm_down_cursor;
	printf("p=%p (%d)\n", x, strlen(x));
//	for(t=0; t < strlen(x); t++) {
//		printf("> %d  [%c] %d\n", t, x[t], x[t]);
//	}

	printf("arm=%d, eng=%d\n",  auto_right_margin, eat_newline_glitch);

//	printf(tparm(setb, 4));
	printf("HELLO\n");
//	printf(tparm(setb, 0));

	line = malloc(8192);
	strcpy(line, sample);
	line_len = strlen(line);

	int c;

	move_to((line_len/width), line_len%width);

//	show_line();
//	fflush(stdout);

	int redraw = 1;

	while(1) {
		if(redraw) {
			redraw_line();
			redraw = 0;
		}
		fflush(stdout);


		c = read_key();

		switch(c) {

		case KEY_LEFT:
			if(((row * width)+col) > 0) move_back();
			break;

		case KEY_RIGHT:
			if(((row * width)+col) < line_len) move_on();
			break;

		case KEY_DC:
			if(((row * width)+col) < line_len) {
				remove_char_at((row * width) + col);
				redraw = 1;
			}
			break;

		case 27:
			printf("X");
			break;

		case 127:
		case 8:
			// if we are the first char of the screen then we need to backup to the
			// end of the previous line
			//
			if(((row * width)+col) > 0) {
				move_back();
				remove_char_at((row * width) + col);
				redraw = 1;
			}
			break;

		default:
			insert_char_at((row * width) + col, c);
			move_on();
			redraw = 1;
		}
	}

	return 0;
}