340 lines
5.5 KiB
C
340 lines
5.5 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
|
|
#include<unistd.h>
|
|
#include<fcntl.h>
|
|
#include<sys/stat.h>
|
|
#include<sys/types.h>
|
|
|
|
#include "ped.h"
|
|
#include "types.h"
|
|
|
|
int main(int argc, char** args)
|
|
{
|
|
/* Make sure we have only two arguments */
|
|
if(argc == 2)
|
|
{
|
|
/* Get the filename */
|
|
char* filename = *(args+1);
|
|
|
|
/* Create a new session */
|
|
struct Session* session = newSession(filename);
|
|
|
|
if(session)
|
|
{
|
|
/* Open the editor */
|
|
newEditor(session);
|
|
|
|
/* Free the session */
|
|
free(session);
|
|
}
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
printf("Too many args, or missing a file.\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
void ttyPut(struct Session* session, char byte)
|
|
{
|
|
output(&byte, 1);
|
|
|
|
/* Check if the byte if not a line feed */
|
|
if(byte != 10)
|
|
{
|
|
session->teletype->cursorX++;
|
|
}
|
|
/* If the byte is a linefeed */
|
|
else if(byte == 10)
|
|
{
|
|
session->teletype->cursorX = 0;
|
|
session->teletype->cursorY++;
|
|
}
|
|
|
|
}
|
|
|
|
void goHome(struct Session* session)
|
|
{
|
|
/* Move up */
|
|
while(session->teletype->cursorY)
|
|
{
|
|
char seq[3] = {27, 91, 65};
|
|
output(seq, 3);
|
|
session->teletype->cursorY--;
|
|
}
|
|
|
|
/* Move home */
|
|
char cr = 13;
|
|
output(&cr, 1);
|
|
session->teletype->cursorX = 0;
|
|
}
|
|
|
|
void redraw2(struct Session* session)
|
|
{
|
|
/* Get dimensions */
|
|
unsigned int rows = session->teletype->rows;
|
|
unsigned int columns = session->teletype->columns;
|
|
|
|
/* General counter */
|
|
unsigned int i = 0;
|
|
|
|
|
|
goHome(session);
|
|
|
|
ttyPut(session, '[');
|
|
|
|
/* Draw name */
|
|
while(i < strlen(session->name))
|
|
{
|
|
ttyPut(session, *(session->name+i));
|
|
i++;
|
|
}
|
|
|
|
ttyPut(session, ']');
|
|
|
|
i = 0;
|
|
while(i < columns-strlen(session->name)-2)
|
|
{
|
|
char symbol = '-';
|
|
output(&symbol, 1);
|
|
i++;
|
|
}
|
|
|
|
ttyPut(session, 10);
|
|
|
|
}
|
|
|
|
void redraw(struct Session* session)
|
|
{
|
|
redraw2(session);
|
|
/* Move cursor back home */
|
|
char cr = 13;
|
|
output(&cr, 1);
|
|
|
|
/* Print all the data */
|
|
output(session->data, session->size);
|
|
|
|
/* Move cursor back home */
|
|
output(&cr, 1);
|
|
|
|
unsigned int i = 0;
|
|
while(i < session->position)
|
|
{
|
|
char seq[3] = {27, 91, 67};
|
|
output(seq, 3);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
void newEditor(struct Session* session)
|
|
{
|
|
/* Setup the tty */
|
|
startTTY();
|
|
|
|
/* Output the file as of now */
|
|
redraw(session);
|
|
|
|
/* Update the tty's dimensions */
|
|
updateDimensions(session->teletype);
|
|
|
|
/* Debugging */
|
|
//debugTTY(open("/dev/pts/18",O_RDWR), session->teletype);
|
|
|
|
//output("hello world", strlen("hello world"));
|
|
while(session->isActive)
|
|
{
|
|
char s;
|
|
s=getChar();
|
|
|
|
if (s == 17)
|
|
{
|
|
session->isActive = 0;
|
|
}
|
|
/* Control */
|
|
else if(s == 27)
|
|
{
|
|
/* Arrow keys */
|
|
s=getChar();
|
|
if(s == 91)
|
|
{
|
|
s=getChar();
|
|
char seq[3];
|
|
/* Up arrow */
|
|
if(s == 65)
|
|
{
|
|
session->teletype->cursorY--;
|
|
}
|
|
/* Down arrow */
|
|
else if (s == 66)
|
|
{
|
|
session->teletype->cursorY++;
|
|
}
|
|
/* Right arrow */
|
|
else if(s == 67)
|
|
{
|
|
seq[0] = 27;
|
|
seq[1] = 91;
|
|
seq[2] = 67;
|
|
|
|
session->position++;
|
|
session->teletype->cursorX++;
|
|
}
|
|
/* Left arrow */
|
|
else if (s == 68)
|
|
{
|
|
seq[0] = 27;
|
|
seq[1] = 91;
|
|
seq[2] = 68;
|
|
|
|
/* Only move cursor back if we not at home */
|
|
if(session->position)
|
|
{
|
|
session->position--;
|
|
session->teletype->cursorX--;
|
|
}
|
|
|
|
}
|
|
|
|
// output(seq, 3);
|
|
//continue;
|
|
|
|
}
|
|
}
|
|
/* Ctrl+D is command key */
|
|
else if(s == 4)
|
|
{
|
|
|
|
char* str = malloc(200);
|
|
*str = 0;
|
|
|
|
while(1)
|
|
{
|
|
s=getChar();
|
|
|
|
if(s==4)
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
strncat(str, &s, 1);
|
|
}
|
|
}
|
|
output(str,strlen(str));
|
|
|
|
runCommand(str, session);
|
|
|
|
|
|
free(str);
|
|
}
|
|
else
|
|
{
|
|
/* Set data at current position */
|
|
*(session->data+session->position) = s;
|
|
/* TODO: As we type position increases */
|
|
session->position++;
|
|
session->teletype->cursorX++;
|
|
//strncat(session->data, &s, 1);
|
|
session->size++;
|
|
}
|
|
|
|
//printf("%c\n",s);
|
|
char* l = malloc(20);
|
|
sprintf(l, "%u", s);
|
|
// output(l, strlen(l));
|
|
// output(&s, 1);
|
|
|
|
/* Update the tty's dimensions */
|
|
updateDimensions(session->teletype);
|
|
|
|
//debugTTY(open("/dev/pts/18",O_RDWR), session->teletype);
|
|
|
|
/* Redraw */
|
|
redraw(session);
|
|
}
|
|
|
|
char* bye = "\nBye mate!\n";
|
|
output(bye, strlen(bye));
|
|
|
|
/* Restore tty settings */
|
|
stopTTY();
|
|
|
|
}
|
|
|
|
struct TTY* newTTY()
|
|
{
|
|
struct TTY* tty = malloc(sizeof(struct TTY));
|
|
|
|
updateDimensions(tty);
|
|
|
|
return tty;
|
|
}
|
|
|
|
struct Session* newSession(char* filename)
|
|
{
|
|
/* Allocate the session */
|
|
struct Session* session = malloc(sizeof(struct Session));
|
|
|
|
/* Make sure it allocated */
|
|
if(session)
|
|
{
|
|
/* Open the file */
|
|
int fd = open(filename, O_CREAT|O_RDWR);
|
|
|
|
if(fd >= 0)
|
|
{
|
|
/* Set the file descriptor */
|
|
session->fd = fd;
|
|
|
|
/* Set name */
|
|
session->name = filename;
|
|
|
|
/* Get the stat struct */
|
|
struct stat statStruct;
|
|
|
|
/* Fill stat struct */
|
|
fstat(fd, &statStruct);
|
|
|
|
/* Set the initial size */
|
|
session->size = statStruct.st_size;
|
|
|
|
/* Allocate initial space for buffer */
|
|
/* TODO: Remove and be dynamic */
|
|
session->data = malloc(session->size+69);
|
|
|
|
/* Set the initial characters */
|
|
read(fd, session->data, session->size);
|
|
|
|
char* temp = malloc(session->size+1);
|
|
strncpy(temp, session->data, session->size);
|
|
//printf("%s", temp);
|
|
|
|
|
|
/* TODO: Use xy :: Set initial position to 0 */
|
|
session->position = session->size;
|
|
/* Set the tty */
|
|
session->teletype = newTTY();
|
|
session->teletype->cursorX = session->position;
|
|
|
|
/* Set the session to active */
|
|
session->isActive = 1;
|
|
|
|
|
|
|
|
/* On success, return the pointer to the session */
|
|
return session;
|
|
}
|
|
|
|
/* On successful malloc, but file failure */
|
|
free(session);
|
|
}
|
|
|
|
/* On error */
|
|
return 0;
|
|
}
|