char *deal_with_termcap(t_shell *sh) { t_prompt *prompt; int ret; char buff[4]; char *str; prompt = init_prompt(); stock_prompt(prompt, 0); prompt_print(prompt, 1); str = NULL; while ((ret = read(0, buff, BUFF_SIZE) != -1)) { prompt_shell(sh, prompt, buff); if (buff[0] == 4 && !prompt->cmd[0]) exit_eof(sh, prompt); if (buff[0] == 10) { str = input_return(sh, prompt); break ; } ft_bzero(buff, 4); } free_prompt(&prompt); return (str); }
/* * Reset the prompt. This is useful for when back grounded processes die. */ void rsh_reset_input(){ /* Now we have to restore the terminal. */ prompt_print(); CUR_SAVE(); rsh_buf_display(&buf); }
void go_up_line(t_prompt *prompt, char *buff) { if (UP_CMD && ft_strlen(prompt->cmd) > prompt->win_size) { prompt->i = prompt->i - prompt->win_size; prompt_print(prompt, 1); } }
void go_down_line(t_prompt *prompt, char *buff) { if (DOWN_CMD && ft_strlen(prompt->cmd) > prompt->win_size) { if ((size_t)prompt->i <= ft_strlen(prompt->cmd) - (prompt->win_size - 3)) prompt->i = prompt->i + prompt->win_size; else prompt->i = ft_strlen(prompt->cmd); prompt_print(prompt, 1); } }
/* * Display the history asynchronously. */ void rsh_history_display_async(int sig){ /* First tings first, print a NL. */ printf("\n"); /* Now display the history. */ rsh_history_print(); /* Now we have to restore the terminal. */ prompt_print(); CUR_SAVE(); rsh_buf_display(&buf); }
void start_auto_complete(t_prompt *prompt, char *buff) { char *res; if (buff[0] == 9 && prompt->cmd[0]) { if ((size_t)prompt->i < ft_strlen(prompt->cmd)) { prompt->i = ft_strlen(prompt->cmd); prompt_print(prompt, 1); } else { if (check_if_arg_is_dir(prompt) == 1) return ; res = auto_complete(prompt->cmd); ft_strcpy(prompt->cmd, res); if (res) free(res); prompt->i = ft_strlen(prompt->cmd); prompt_print(prompt, 1); } } }
static void arg_is_dir(t_prompt *prompt, char **arg, int i) { char *tmp; tmp = ft_strdup(arg[i - 1]); free(arg[i - 1]); arg[i - 1] = ft_strjoin(tmp, "/"); free(tmp); tmp = join_tab(arg); ft_strclr(prompt->cmd); ft_strcpy(prompt->cmd, tmp); prompt->i = ft_strlen(prompt->cmd); prompt_print(prompt, 1); free(tmp); }
/*print current output command*/ static void out_cmd_print(void) { cmd_strlen = strlen(history_buf[history_cur]); putchar('\r'); prompt_print(); while (cmd_cursor < cmd_strlen) term_echo(); /*clear characters after cursor */ printf("\033[J"); }
/*termial initial*/ static void term_init(char *out_cmd) { term_cursor = 0; cmd_cursor = 0; cmd_strp = out_cmd; cmd_strlen = strlen(out_cmd); // Initialize signal signal(SIGINT, term_sig_handler); signal(SIGUSR1, term_sig_handler); signal(SIGKILL, term_sig_handler); signal(SIGABRT, term_sig_handler); signal(SIGTERM, term_sig_handler); signal(SIGHUP, term_sig_handler); //signal(SIGQUIT, SIG_IGN); prompt_print(); }
/*print help info*/ static void handle_help(void) { int pmatch_id = GCMD_DESC_NO_MATCH, pmatch_sub_id = GCMD_DESC_NO_MATCH, pmatch_act_id = GCMD_DESC_NO_MATCH; int cmd_nr = 0, pmatch_nr = 0, pmatch_sub_nr = 0; char *tmp_str[3], *cmd_strp_cp = strdup(cmd_strp); /* split command string into temp array */ tmp_str[cmd_nr] = (void *) strtok(cmd_strp_cp, " "); while (tmp_str[cmd_nr]) { if (++cmd_nr == 3) break; tmp_str[cmd_nr] = (void *) strtok(NULL, " "); } /*echo input ? */ printf("?\n"); int is_print = 0; /* print matched command */ switch (cmd_nr) { case 3: pmatch_nr = search_cmd_type(tmp_str[0], &pmatch_id, is_print); if(FULL_MATCHED(pmatch_nr, pmatch_id)) pmatch_sub_nr = search_cmd_sub(pmatch_id, &pmatch_sub_id, tmp_str[1], is_print); if(FULL_MATCHED(pmatch_sub_nr, pmatch_sub_id)) { is_print = 1; search_cmd_action(pmatch_id, &pmatch_act_id, tmp_str[1], tmp_str[2], is_print); } break; case 2: pmatch_nr = search_cmd_type(tmp_str[0], &pmatch_id, is_print); if(FULL_MATCHED(pmatch_nr, pmatch_id)) { is_print = 1; search_cmd_sub(pmatch_id, &pmatch_sub_id, tmp_str[1], is_print); } break; case 1: is_print = 1; pmatch_nr = search_cmd_type(tmp_str[0], &pmatch_id, is_print); if(NONE_MATCHED(pmatch_nr, pmatch_id)) { print_cmd_all(); } else if(FULL_MATCHED(pmatch_nr, pmatch_id)) { print_sub_all(pmatch_id); } break; case 0: print_cmd_all(); break; default: break; } printf("\n"); /* re-print prompt */ prompt_print(); /* re-print from cursor */ while (cmd_cursor < cmd_strlen) term_echo(); if(cmd_strp_cp) free(cmd_strp_cp); }
/* re_refresh(): * draws the new virtual screen image from the current input * line, then goes line-by-line changing the real image to the new * virtual image. The routine to re-draw a line can be replaced * easily in hopes of a smarter one being placed there. */ libedit_private void re_refresh(EditLine *el) { int i, rhdiff; wchar_t *cp, *st; coord_t cur; #ifdef notyet size_t termsz; #endif ELRE_DEBUG(1, (__F, "el->el_line.buffer = :%ls:\r\n", el->el_line.buffer)); literal_clear(el); /* reset the Drawing cursor */ el->el_refresh.r_cursor.h = 0; el->el_refresh.r_cursor.v = 0; terminal_move_to_char(el, 0); /* temporarily draw rprompt to calculate its size */ prompt_print(el, EL_RPROMPT); /* reset the Drawing cursor */ el->el_refresh.r_cursor.h = 0; el->el_refresh.r_cursor.v = 0; if (el->el_line.cursor >= el->el_line.lastchar) { if (el->el_map.current == el->el_map.alt && el->el_line.lastchar != el->el_line.buffer) el->el_line.cursor = el->el_line.lastchar - 1; else el->el_line.cursor = el->el_line.lastchar; } cur.h = -1; /* set flag in case I'm not set */ cur.v = 0; prompt_print(el, EL_PROMPT); /* draw the current input buffer */ #if notyet termsz = el->el_terminal.t_size.h * el->el_terminal.t_size.v; if (el->el_line.lastchar - el->el_line.buffer > termsz) { /* * If line is longer than terminal, process only part * of line which would influence display. */ size_t rem = (el->el_line.lastchar-el->el_line.buffer)%termsz; st = el->el_line.lastchar - rem - (termsz - (((rem / el->el_terminal.t_size.v) - 1) * el->el_terminal.t_size.v)); } else #endif st = el->el_line.buffer; for (cp = st; cp < el->el_line.lastchar; cp++) { if (cp == el->el_line.cursor) { int w = wcwidth(*cp); /* save for later */ cur.h = el->el_refresh.r_cursor.h; cur.v = el->el_refresh.r_cursor.v; /* handle being at a linebroken doublewidth char */ if (w > 1 && el->el_refresh.r_cursor.h + w > el->el_terminal.t_size.h) { cur.h = 0; cur.v++; } } re_addc(el, *cp); } if (cur.h == -1) { /* if I haven't been set yet, I'm at the end */ cur.h = el->el_refresh.r_cursor.h; cur.v = el->el_refresh.r_cursor.v; } rhdiff = el->el_terminal.t_size.h - el->el_refresh.r_cursor.h - el->el_rprompt.p_pos.h; if (el->el_rprompt.p_pos.h && !el->el_rprompt.p_pos.v && !el->el_refresh.r_cursor.v && rhdiff > 1) { /* * have a right-hand side prompt that will fit * on the end of the first line with at least * one character gap to the input buffer. */ while (--rhdiff > 0) /* pad out with spaces */ re_putc(el, ' ', 1); prompt_print(el, EL_RPROMPT); } else { el->el_rprompt.p_pos.h = 0; /* flag "not using rprompt" */ el->el_rprompt.p_pos.v = 0; } re_putc(el, '\0', 0); /* make line ended with NUL, no cursor shift */ el->el_refresh.r_newcv = el->el_refresh.r_cursor.v; ELRE_DEBUG(1, (__F, "term.h=%d vcur.h=%d vcur.v=%d vdisplay[0]=\r\n:%80.80s:\r\n", el->el_terminal.t_size.h, el->el_refresh.r_cursor.h, el->el_refresh.r_cursor.v, ct_encode_string(el->el_vdisplay[0], &el->el_scratch))); ELRE_DEBUG(1, (__F, "updating %d lines.\r\n", el->el_refresh.r_newcv)); for (i = 0; i <= el->el_refresh.r_newcv; i++) { /* NOTE THAT re_update_line MAY CHANGE el_display[i] */ re_update_line(el, el->el_display[i], el->el_vdisplay[i], i); /* * Copy the new line to be the current one, and pad out with * spaces to the full width of the terminal so that if we try * moving the cursor by writing the character that is at the * end of the screen line, it won't be a NUL or some old * leftover stuff. */ re__copy_and_pad(el->el_display[i], el->el_vdisplay[i], (size_t) el->el_terminal.t_size.h); } ELRE_DEBUG(1, (__F, "\r\nel->el_refresh.r_cursor.v=%d,el->el_refresh.r_oldcv=%d i=%d\r\n", el->el_refresh.r_cursor.v, el->el_refresh.r_oldcv, i)); if (el->el_refresh.r_oldcv > el->el_refresh.r_newcv) for (; i <= el->el_refresh.r_oldcv; i++) { terminal_move_to_line(el, i); terminal_move_to_char(el, 0); /* This wcslen should be safe even with MB_FILL_CHARs */ terminal_clear_EOL(el, (int) wcslen(el->el_display[i])); #ifdef DEBUG_REFRESH terminal_overwrite(el, L"C\b", 2); #endif /* DEBUG_REFRESH */ el->el_display[i][0] = '\0'; } el->el_refresh.r_oldcv = el->el_refresh.r_newcv; /* set for next time */ ELRE_DEBUG(1, (__F, "\r\ncursor.h = %d, cursor.v = %d, cur.h = %d, cur.v = %d\r\n", el->el_refresh.r_cursor.h, el->el_refresh.r_cursor.v, cur.h, cur.v)); terminal_move_to_line(el, cur.v); /* go to where the cursor is */ terminal_move_to_char(el, cur.h); }
int main (int argc, char *argv[]) { char my_ip[STRING_IP_SIZE]; int my_port = MY_PORT; char host_ip[STRING_IP_SIZE]; int host_port; char *resource = NULL, *response; socket_t *socket_tcp; unsigned short command_op, op, len; struct prompt_t prompt; int client_error = 0; /* Obtener los argumentos */ if ( retrieve_arguments(argc, argv, &host_port, host_ip, &resource) ) { printf("(cliente) el uso debe ser --> cliente -l SLR -p PSLR -r REC\n"); return -1; } /* Obtiene la ip del host local */ if( get_my_ip(my_ip) ) { printf("(cliente) no se puede obtener ip local.\n"); return -1; } /* Localizacion del recurso en el SLR */ if ( (locate_resource_from_slr( my_port, my_ip, &host_port, host_ip, resource )) ) { printf("(cliente) no se ha localizado el recurso en el slr.\n"); return -1; } // strncpy(host_ip, "192.168.1.34", 15); // host_port = 7000; printf("(cliente) recurso localizado en servidor SPR [con IP: %s y puerto remoto: %d].\n", host_ip, host_port); socket_tcp = socket_create(my_port, my_ip, host_port, host_ip, SOCK_STREAM); if ( socket_connect(socket_tcp ) ) { printf("(cliente) connect error.\n"); return -1; } /* Establece el timeout de respuestas */ socket_setrcvtimeout(socket_tcp, TIMEOUT); printf ("(cliente) conexion establecida con servidor SPR [con IP: %s y puerto remoto: %d].\n", host_ip, host_port); printf ("(cliente) enviando solicitud de conexion.\n"); if ( conection_request( socket_tcp ) ) { printf("(cliente) solicitud de conexion rechazada.\n"); return -1; } prompt_init(); do { prompt_read_command(&prompt); command_op = cmdtous(prompt.command); if ( command_op == OP_UNKNOW ) { printf ("(cliente) orden desconocida.\n"); continue; } if( !(client_error = operation_request(socket_tcp, command_op, prompt.parameters)) ) { op = OP_RESPONSE( socket_readushort(socket_tcp) ); if ( socket_tcp->expire_timeout ) break; len = socket_readushort(socket_tcp); if ( socket_tcp->expire_timeout ) break; response = socket_readnchar(socket_tcp, len); if ( socket_tcp->expire_timeout ) break; if (op != command_op && op != ERROR) { printf ("(cliente) recibida respuesta desconocida.\n"); client_error = 1; } else prompt_print(NULL, response); free(response); } } while( command_op != OP_SALIR && !client_error ); prompt_destroy(); /* Cierra el socket */ socket_destroy(socket_tcp); printf ("(cliente) conexion cerrada con servidor SPR.\n"); return(0); }