コード例 #1
0
/*
*   Function: file_read()
*   Description: Reads bytes from a file and copies the bytes read into a buffer
*   inputs: fd -- file descriptor of the file to read
*           buf -- pointer to the buffer to copy data into
*           nbytes -- number of bytes to read
*   outputs: Returns the number of bytes read, or -1 on failure 
*   effects: copys data from a file into buf
*/
int32_t file_read (int32_t fd, void* buf, int32_t nbytes)
{
    uint32_t inode_number;

    /* Get current PCB Pointer */
    pcb_t *pcb = get_pcb_ptr();

    //get current file position
    uint32_t offset = pcb->fds[fd].file_position;
    //search for the file by name
    inode_number = pcb->fds[fd].inode;
    int temp = read_data(inode_number, offset, (uint8_t*)buf, nbytes);
    
    pcb->fds[fd].file_position += temp;
    
    return temp;
}
コード例 #2
0
ファイル: terminal.c プロジェクト: bjackson/ece391mp
/*
 * int32_t terminal_read(int32_t fd, void* buf, int32_t nbytes)
 * Decsription: Reads data into the terminal
 * Inputs: fd - file descriptor, buf - buffer, nbytes - max bytes to read
 * Outputs: -1 on failure, number of bytes read on success
 */
int32_t terminal_read(int32_t fd, void* buf, int32_t nbytes) {
    if(fd != STDIN_FD) {
        log(WARN, "Invalid file descriptor", "terminal_read");
        return -1;
    }

    pcb_t* pcb = get_pcb_ptr();
    if(pcb == NULL) {
        log(ERROR, "Can't call terminal functions before starting shell", "terminal_read");
        return -1;
    }
    uint32_t t_idx = pcb->terminal_index;

    memset(read_buffers[t_idx], 0x00, sizeof(read_buffers[t_idx]));

    // Wait for read_ready to be set
    uint32_t spin = 0;
    while (!read_ready_flags[t_idx]) {
        spin++;
    }

    int32_t bytes_to_read = (nbytes > KEYBOARD_BUFFER_SIZE) ?
        KEYBOARD_BUFFER_SIZE : nbytes;

    cli();
    int i;
    for(i = 0; i < bytes_to_read; i++) {
        uint8_t next = read_buffers[t_idx][i];
        ((uint8_t*) buf)[i] = next;

        // Stop returning bytes after encountering a newline
        if(next == '\n') {
            read_ready_flags[t_idx] = 0;
            sti();
            return i + 1;
        }
    }

    read_ready_flags[t_idx] = 0;
    sti();

    return bytes_to_read;
}
コード例 #3
0
ファイル: terminal.c プロジェクト: bjackson/ece391mp
/*
 * int32_t terminal_write_key(uint8_t key)
 * Decsription: Write data to the terminal buffer
 * Inputs: key - keyboard entry
 * Outputs: -1 on failure, 0 on success
 */
int32_t terminal_write_key(uint8_t key) {
    pcb_t* pcb = get_pcb_ptr();
    if(pcb == NULL) {
        log(ERROR, "Can't call terminal functions before starting shell", "terminal_write_key");
        return -1;
    }
    uint32_t t_idx = pcb->terminal_index;

    // Handle backspace
    if(key == '\b') {
        cli();
        if(keyboard_buffer_indices[t_idx] > 0) {
            putc('\b');
        }
        keyboard_buffer_indices[t_idx] = (keyboard_buffer_indices[t_idx] == 0) ? 0 :
            keyboard_buffer_indices[t_idx] - 1;
        keyboard_buffers[t_idx][keyboard_buffer_indices[t_idx]] = 0x00;
        sti();
        return 0;
    }

    // Handle enter
    if(key == '\n') {
        cli();
        keyboard_buffers[t_idx][keyboard_buffer_indices[t_idx]] = '\n';
        memcpy(read_buffers[t_idx], keyboard_buffers[t_idx], sizeof(keyboard_buffers[t_idx]));
        memset(keyboard_buffers[t_idx], 0x00, KEYBOARD_BUFFER_SIZE);
        keyboard_buffer_indices[t_idx] = 0;
        putc('\n');
        read_ready_flags[t_idx] = 1;
        sti();
        return 0;
    }

    // Save at least one character for the line feed
    if(keyboard_buffer_indices[t_idx] == KEYBOARD_BUFFER_SIZE - 1) {
        return -1;
    }

    keyboard_buffers[t_idx][keyboard_buffer_indices[t_idx]++] = key;
    putc(key);
    return 0;
}
コード例 #4
0
ファイル: terminal.c プロジェクト: bjackson/ece391mp
/*
 * int32_t terminal_clear()
 * Decsription: Clears the terminal
 * Inputs: none
 * Outputs: none
 */
void terminal_clear() {
    pcb_t* pcb = get_pcb_ptr();
    if(pcb == NULL) {
        log(ERROR, "Can't call terminal functions before starting shell", "terminal_clear");
        return;
    }
    uint32_t t_idx = pcb->terminal_index;

    // Will only clear the correct terminal due to video memory mapping
    clear();

    memset(keyboard_buffers[t_idx], 0x00, sizeof(keyboard_buffers[t_idx]));
    memset(read_buffers[t_idx], 0x00, sizeof(read_buffers[t_idx]));
    keyboard_buffer_indices[t_idx] = 0;
    read_ready_flags[t_idx] = 0;
    switch_screen_pos_x[t_idx] = 0;
    switch_screen_pos_y[t_idx] = 0;

    set_cursor(0, 0);
}
コード例 #5
0
ファイル: keyboard.c プロジェクト: GoingtoRock/osDevClass
/* change_terminal function
   Function handles switching terminals (ALT + FUNCTION-KEY)
   Input : new_terminal -- terminal to switch into
   Output : None
   Side Effect : Does nothing if the new terminal 
   is the same as the current terminal.
   Else it copies video memory into the correct video buffer
   and the correct video buffer into video memory
*/
void change_terminal(int32_t new_terminal)
{
	/* Check to make sure that the new_terminal is not the same
	   as the terminal displayed now */
	int32_t old_terminal = get_displayed_terminal();
	if(new_terminal == old_terminal){
		LOG("You are already in this terminal! Cannot Switch!\n");
		return;
	}

	/* Case where we try to launch shell with max_num_process already running */
	if(num_progs[TERMINAL_1] + num_progs[TERMINAL_2] + num_progs[TERMINAL_3] >= MAX_NUM_PROCESS && 
	   num_progs[new_terminal] == 0){
	   	printf("Reached maximum number of programs! Can't fire new shell in new terminal!391OS> ");
		return;
	}

	/* Grab the page dir for the old terminal */
	pcb_t* pcb_ptr = top_process[old_terminal]; 
	pde_t* current_pg_dir = pcb_ptr->pg_dir;

	/* Grab the page dir for new terminal */
	pcb_t* top_pcb = top_process[new_terminal];
	pde_t* new_term_pg_dir;
	if(top_pcb == NULL)
		new_term_pg_dir = NULL;
	else
		new_term_pg_dir = top_pcb -> pg_dir;

	/* Copy mem (video->buf) and remap page (USER_VIDEO -> BUF) for the old_terminal*/
	remap_user_video_and_memcpy(get_video_buf_for_terminal(old_terminal), VIDEO, current_pg_dir);
	
	/* Copy mem (buf-> video) and remap page (USER_VIDEO -> VIDEO) for the new_terminal */
	remap_user_video_and_memcpy(VIDEO, get_video_buf_for_terminal(new_terminal), new_term_pg_dir);

	/* Flush TLB */
	set_cr3_reg(get_pcb_ptr() -> pg_dir);

	/* Update the displayed terminal*/
	displayed_terminal = new_terminal;

	/* If the the new terminal is not executing any programs, Execute Shell */
	if(num_progs[new_terminal] == 0){
		key_press = 0;	// Will not return to keyboardhandler, update key_press	

		pcb_t* curr_ptr = get_pcb_ptr();
		uint8_t exec_cmd[15] = "shell";

		/* Save info for scheduling */
		curr_ptr -> esp0 = tss.esp0; 
		asm volatile("movl %%esp, %0;" 
	  		"movl %%ebp, %1;" : 
	  		"=b" (curr_ptr -> esp),
	  		"=c" (curr_ptr -> ebp));

		send_eoi(KEYBOARD_IRQ);		//Send EOI for ALT+FUNCTION-KEY

		/* Execute a Shell */
		if(-1 == sys_execute(exec_cmd)){
			LOG("Executing new shell from new terminal failed!\n");
		}
	}