static void backspace() { int i; if( len == 0 || offset == 0 ) { return; } memmove(line+offset-1, line+offset, STR_LINE_SIZE-(offset+1)); len--; offset--; //fprintf(stdout,"\r%s", line); term_cursor_left(); for(i = offset; i < len; i++) { term_putc(line[i]); } term_putc(' '); for(i = 0; i < len-offset; i++) { term_cursor_left(); } term_cursor_left(); }
// ---------------------------------------------------------------------------- void term_putc_cr(char c) { if (c == '\n') term_putc('\r'); term_putc(c); }
void readline_print_status() { char str_prompt[STR_SIZE]; #if 0 term_puts("myshell"); term_putc(get_prompt()); term_putc(' '); #endif sprintf(str_prompt, "%s@%s:%s%c ", get_username(), get_nodename(), get_current_short_dir(), get_prompt()); term_puts(str_prompt); }
static void append(char c) { int i; //fprintf(stderr, "c = %d\n", c); if( len >= STR_LINE_SIZE-1 ) { return; } memmove(line+offset+1, line+offset, STR_LINE_SIZE-(offset+1)); line[offset++] = c; len++; for(i = offset-1; i < len; i++) { term_putc(line[i]); } for(i = 0; i < len-offset; i++) { term_cursor_left(); } }
void cpu_state_dump(struct cpu_state * cpu) { char buf[32]; uint8_t * p; int i; term_puts("eax=0x"); itoa(buf, cpu->eax, 16, 8); term_puts(buf); term_puts(" ebx=0x"); itoa(buf, cpu->ebx, 16, 8); term_puts(buf); term_puts(" ecx=0x"); itoa(buf, cpu->ecx, 16, 8); term_puts(buf); term_puts(" edx=0x"); itoa(buf, cpu->edx, 16, 8); term_puts(buf); term_puts("\nesi=0x"); itoa(buf, cpu->esi, 16, 8); term_puts(buf); term_puts(" edi=0x"); itoa(buf, cpu->edi, 16, 8); term_puts(buf); term_puts("\nesp=0x"); itoa(buf, cpu->esp, 16, 8); term_puts(buf); term_puts(" ebp=0x"); itoa(buf, cpu->ebp, 16, 8); term_puts(buf); term_puts(" eip=0x"); itoa(buf, cpu->eip, 16, 8); term_puts(buf); term_puts("\neflags=0x"); itoa(buf, cpu->eflags, 16, 8); term_puts(buf); term_puts(" cs=0x"); itoa(buf, cpu->cs, 16, 4); term_puts(buf); term_puts(" ss=0x"); itoa(buf, cpu->ss, 16, 4); term_puts(buf); term_puts("\n\ncode @ eip: "); p = (uint8_t *)cpu->eip; for (i=0; i<32; i++) { itoa(buf, *p++, 16, 2); term_puts(buf); term_putc(' '); } }
//------------------------------------------------------------------------------ void term_put_c(tTermState * ts, char c) { if(c == DEL_KEY) term_del(ts); else if(c == '\r') ts->term_cur_x = 0; else term_putc(ts, c); }
static void rtas_display_character(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { char c = rtas_ld(vcpu->kvm, args, 0); term_putc(&c, 1, 0); rtas_st(vcpu->kvm, rets, 0, 0); }
static void rtas_put_term_char(struct kvm_cpu *vcpu, uint32_t token, uint32_t nargs, target_ulong args, uint32_t nret, target_ulong rets) { char c = rtas_ld(vcpu->kvm, args, 0); term_putc(CONSOLE_HV, &c, 1, 0); rtas_st(vcpu->kvm, rets, 0, 0); }
static void console_clear (void) { int x; term_goto(0, TERM_HEIGHT - 1); for (x = 0; x < TERM_WIDTH; x++) { term_putc(' '); } }
//------------------------------------------------------------------------------ void term_put_str(tTermState * ts, char * str) { while(*str != 0x00) { if(*str == DEL_KEY) term_del(ts); else if(*str == '\r') ts->term_cur_x = 0; else term_putc(ts, *str); str++; } }
inline void term_puts(const char *str) { int color = term_surf[current_term].fg_color; while (*str) { if (*str == '\33') { if (*++str == 'r') { term_surf[current_term].fg_color = color; str++; } else { uint8_t a = *str & 0xf0; uint8_t r = *str & 0x0f; ++str; uint8_t g = *str & 0xf0; uint8_t b = *str & 0x0f; ++str; term_surf[current_term].fg_color = a << 24 | r << 20 | g << 8 | b << 4; } } else { term_putc(*str++); } } term_surf[current_term].fg_color = color; }
int readline(char *str_line) { int c; memset(line, 0, STR_LINE_SIZE); offset = 0; len = 0; do{ c = term_getc(); /* printf("c = %d\n", c); if( is_send_signal_int() ) { fprintf(stderr, "sig"); term_putc('\n'); str_line[0] = '\0'; return 0; } */ switch( c ) { case -1 : fprintf(stderr, "\n-1\n"); break; case '\n' : term_putc('\n'); break; case 1 : move_begin(); break; case 5 : move_end(); break; case 127 : backspace(); history_backup_current_line(line); break; case 27 : switch( (c = term_getc()) ) { case 91 : switch( (c = term_getc()) ) { case 65 : move_up(); break; case 66 : move_down(); break; case 68 : move_left(); break; case 67 : move_right(); break; default: //printf("c = %d\n", c); break; } break; } break; default : //printf("c = %d\n", c); if( c >= ' ' ) { append(c); history_backup_current_line(line); } break; } }while( c != '\n' ); history_add(line); history_backup_current_line(""); memcpy(str_line, line, len+1); return len; }
// ---------------------------------------------------------------------------- void usbcan_decode_command(char *str, uint8_t length) { uint8_t temp; if (length == 0) return; switch (str[0]) { case 'S': // set bitrate // Lawicel has the folowing settings: // S0 10 kbps // S1 20 kbps // S2 50 kbps // S3 100 kbps // S4 125 kbps // S5 250 kbps // S6 500 kbps // S7 800 kbps // S8 1 mbps; is index 7 on USB2CAN // Note that the USB2CAN adapter does not support 800 kbps. // With index 7 USB2CAN runs on 1 mbps. // Report error if the range is wrong, communication channel // already open, Parameter wrong or the user tries to set // 800 kbps, which is not supported. if ( ((temp = str[1] - '0') > 8) || channel_open || length != 2 || temp == 7 ) { goto error; } else { // Take care of different index usage for 1 mbps. if ( temp == 8 ) { temp--; } // Set new bitrate, remember all MOB are cleared! can_init(temp); bitrate_set = true; } break; case 'r': // send frames case 't': case 'R': case 'T': if ( !channel_open ) { goto error; } else { if ( !usbcan_decode_message(str, length) ) { goto error; } } break; case 'M': // Set acceptance code for SJA1000 case 'm': // Set acceptance mask for SJA1000 case 's': // Set BTR0/BTR1 for SJA1000 goto error; // TODO break; case 'O': // Open channel, i.e. connect CAN to output. if ( channel_open || !bitrate_set ) { goto error; } else { can_set_mode(NORMAL_MODE); // In case the baudrate changed, re-enable some filters. can_filter_t filter = { .mask = 0, .id = 0, .flags.extended = 0, .flags.rtr = 0 }; for (uint8_t i=11; i<15; i++) { can_set_filter(i, &filter); } // Mark the channel as open. channel_open = true; } break; case 'L': // Open channel, i.e. connect CAN to output in listen only mode. if ( channel_open || !bitrate_set ) { goto error; } else { can_set_mode(LISTEN_ONLY_MODE); channel_open = true; } break; case 'C': // Close channel, i.e. disconnect CAN from output. if ( !channel_open ) { goto error; } else { channel_open = false; } break; case 'F': // read Status Flags term_put_hex(0); // TODO break; case 'N': // read serial number term_putc( 'N' ); term_put_hex( 0 ); term_put_hex( 0 ); break; case 'V': // read hardware and firmeware version term_putc( 'V' ); term_put_hex( (HARDWARE_VERSION_MAJOR << 4) | HARDWARE_VERSION_MINOR ); term_put_hex( (SOFTWARE_VERSION_MAJOR << 4) | SOFTWARE_VERSION_MINOR ); break; case 'Z': // Switch on or off timestamps. // On Lawicel this value is stored in EEPROM. if ( channel_open || length != 2) { goto error; } else { use_timestamps = (str[1] - '0' == 0) ? false : true; CANTIM = 0; } break; } term_putc('\r'); // command could be executed return; error: term_putc(7); // Error in command } // ---------------------------------------------------------------------------- // processes commands in usbcan-protocol mode void usbcan_handle_protocol(void) { static char buffer[40]; static uint8_t pos; static can_error_register_t last_error = { 0, 0 }; can_error_register_t error; // check for new messages if (can_check_message()) { can_t message; // Only communicate data if channel is open. // Due to left -> right evaluation messages are extracted from the // MOB to avoid overflow, even if no communication is desired. if ( can_get_message(&message) && channel_open ) { uint8_t length = message.length; if (message.flags.rtr) { // print identifier if (message.flags.extended) { printf_P(PSTR("R%08lx"), message.id); } else { uint16_t id = message.id; printf_P(PSTR("r%03x"), id); } term_putc(length + '0'); } else { // print identifier if (message.flags.extended) { printf_P(PSTR("T%08lx"), message.id); } else { uint16_t id = message.id; printf_P(PSTR("t%03x"), id); } term_putc(length + '0'); // print data for (uint8_t i = 0; i < length; i++) term_put_hex(message.data[i]); } if (use_timestamps) printf_P(PSTR("%04x"), message.timestamp); term_putc('\r'); } } // get commands while (term_data_available()) { char chr = term_getc(); if (chr != '\r') { buffer[pos] = chr; pos++; if (pos >= sizeof(buffer)) { // format-error: command to long! pos = 0; } } else { buffer[pos] = '\0'; usbcan_decode_command(buffer, pos); pos = 0; } } // get error-register error = can_read_error_register(); if (last_error.tx != error.tx || last_error.rx != error.rx) { printf_P(PSTR("E%02x%02x\r"), error.rx, error.tx); last_error = error; } }
void term_puts(Terminal* t, const char* str) { char c; while((c = *(str++))) term_putc(t, c); }