diff --git a/Makefile b/Makefile index c9115a0..5d1cb16 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,11 @@ all: install $(CC) src/utils.c -o utils.o -c $(CC) src/commands.c -o commands.o -c $(CC) src/editor.c -o editor.o -c - $(CC) $(FLAGS) -o $(EXE) src/ped.c tty.o utils.o commands.o editor.o + $(CC) src/session.c -o session.o -c + $(CC) src/plugins.c -o plugins.o -c + $(CC) $(FLAGS) -o $(EXE) src/ped.c tty.o utils.o commands.o editor.o session.o plugins.o gcc src/plugins/zifty.c -o zifty.o -shared -fPIC + gcc src/plugins/reload.c -o reload.o -shared -fPIC test: clean all ./bin/ped test diff --git a/src/commands.c b/src/commands.c index ff91849..b00d735 100644 --- a/src/commands.c +++ b/src/commands.c @@ -42,7 +42,7 @@ char** getCommand(char* str) } -void runCommand(char* str, struct Session* session) +void runCommand(char* str, struct Editor* editor) { /* TODO: Split the string by first space */ /* TODO: First item is command, after space is args */ @@ -63,16 +63,17 @@ void runCommand(char* str, struct Session* session) } else if(strcmp(command, "quit") == 0) { - session->isActive = 0; + editor->currentSession->isActive = 0; } else if(strcmp(command, "w") == 0) { - write(session->fd, session->data, session->size); + write(editor->currentSession->fd, editor->currentSession->data, editor->currentSession->size); } /* TODO: Replace with file mapping */ /* This tries to load the object file provided */ else { + //return; /* Generate the object file name */ char* objectFile = malloc(strlen(str)+3); strcpy(objectFile, command); @@ -82,11 +83,11 @@ void runCommand(char* str, struct Session* session) if(dynObjHandle) { - void (*funcPtr)(struct Session*) = dlsym(dynObjHandle, "dispatch"); + unsigned char (*funcPtr)(struct Editor*, char*, void*) = dlsym(dynObjHandle, "dispatch"); if(funcPtr) { - funcPtr(session); + funcPtr(editor, "onCommand", NULL); } else { diff --git a/src/ped.c b/src/ped.c index b3896c6..9dc5a60 100644 --- a/src/ped.c +++ b/src/ped.c @@ -24,6 +24,7 @@ int main(int argc, char** args) { /* Create a new editor instance */ newEditor2(&editor); + loadPlugins(&editor); /* Make sure we have only two arguments */ if(argc == 2) @@ -42,7 +43,7 @@ int main(int argc, char** args) if(session) { /* Open the editor */ - newEditor(session); + newEditor(); /* Free the session */ free(session); @@ -154,23 +155,18 @@ void statusDraw(struct Session* session) ttyPut(session, 13); } -void loadPlugins() -{ - -} - -void runDrawPlugins(struct Session* session) +void runDrawPlugins() { /* TODO: This is to load zifty, this should be a loop form a file */ void* dynObjHandle = dlopen("zifty.o", RTLD_NOW); - void (*funcPtr)(struct Session*) = dlsym(dynObjHandle, "dispatch"); - funcPtr(session); + unsigned char (*funcPtr)(struct Editor*, char*, void*) = dlsym(dynObjHandle, "dispatch"); + funcPtr(&editor, "onRedraw", NULL); } void redraw(struct Session* session) { /* Run all plugins that run on draw */ - runDrawPlugins(session); + runDrawPlugins(); /* Draw the status line */ statusDraw(session); @@ -269,8 +265,11 @@ void ring() output(&bell, 1); } -void newEditor(struct Session* session) +void newEditor() { + /* The current session */ + struct Session* session = editor.currentSession; + /* Setup the tty */ startTTY(); @@ -357,38 +356,43 @@ void newEditor(struct Session* session) /* Ctrl+D is command key */ else if(s == 4) { - - char* str = malloc(20); - *str = 0; + /* Allocate a byte */ + char* str = malloc(1); + + /* Receive input till next Ctrl+D */ unsigned int i = 0; while(1) { + /* Get next character */ s=getChar(); if(s==4) { + /* Null terminate the string */ + *(str+i) = 0; break; } else { - strncat(str, &s, 1); + /* Append new character */ + *(str+i) = s; } i++; - if(i==20) - { - str=realloc(str, i+20); - i=0; - } + str = realloc(str, i+1); } //output(str,strlen(str)); if(strlen(str) > 0) { - runCommand(str, session); + runCommand(str, &editor); + } + if(str==0) + { + /* TODO: Bug here, this if statement never runs and is useless */ + free(str); } - free(str); } else { @@ -439,8 +443,8 @@ void newEditor(struct Session* session) redraw(session); } - char* bye = "\nBye mate!\n"; - output(bye, strlen(bye)); + //char* bye = "\nBye mate!\n"; + //output(bye, strlen(bye)); /* Clean up commands */ @@ -456,72 +460,4 @@ struct TTY* newTTY() 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->fileX = session->size; - session->fileX = 0; - session->fileY = 0; - //session->fileY = linefeedCount(session->data, session->size); - /* Set the tty */ - session->teletype = newTTY(); - //session->teletype->cursorX = session->fileX; - - - /* Set the session to active */ - session->isActive = 1; - - /* Set the plugins to 0 */ - - - /* On success, return the pointer to the session */ - return session; - } - - /* On successful malloc, but file failure */ - free(session); - } - - /* On error */ - return 0; -} +} \ No newline at end of file diff --git a/src/ped.h b/src/ped.h index e98d4d3..7457941 100644 --- a/src/ped.h +++ b/src/ped.h @@ -1,3 +1,10 @@ -void output(char *string, unsigned int length); +void output(char*, unsigned int); struct Session *newSession(char*); -void newEditor(struct Session* session); +void newEditor(); + +void startTTY(); +void stopTTY(); + +char getChar(); + +void loadPlugins(struct Editor*); \ No newline at end of file diff --git a/src/plugins.c b/src/plugins.c index d54a468..7bdf3de 100644 --- a/src/plugins.c +++ b/src/plugins.c @@ -1,10 +1,30 @@ +#include "types.h" +#include +#include +#include + /** * Plugins * * Manages the plugin system. */ -void addPlugin(struct Session* session) +void loadPlugins(struct Editor* editor) +{ + /* Look through file ted.conf */ + int fd = open("ted.conf", O_RDWR); + + if(fd>=0) + { + + } + else + { + + } +} + +void addPlugin(struct Editor* editor, char* pluginName) { } \ No newline at end of file diff --git a/src/plugins/brink.c b/src/plugins/brink.c index 756368c..83dfedb 100644 --- a/src/plugins/brink.c +++ b/src/plugins/brink.c @@ -1,12 +1,12 @@ #include "../types.h" #include -void dispatch(struct Session* session) +void dispatch(struct Editor* editor) { //session->position = 5; int g = 1; g++; - printf("EXAAAAACTLY!\n"); - session->isActive=0; + printf("EXAAAAACTLasdfhfdjhjfasdhfkjasdhfaskhfdskjhsadfkjY!\n"); + editor->currentSession->isActive=0; } \ No newline at end of file diff --git a/src/plugins/collide.c b/src/plugins/collide.c new file mode 100644 index 0000000..6c7bfcf --- /dev/null +++ b/src/plugins/collide.c @@ -0,0 +1,45 @@ +#include "../types.h" +#include +#include +#include + +/** +* Collide +* +* Ring the terminal bell when we hit the edge +* of the screen. +*/ + +void dispatch(struct Session* session) +{ + /* Get dimensions */ + unsigned int columns = session->teletype->columns; + unsigned int rows = session->teletype->rows; + + /* General counter */ + unsigned int i = 0; + + /* STatus line */ + session->status = malloc(columns); + *session->status=0; + + /* Add name */ + strcat(session->status, "{🐻}️️["); + strcat(session->status, session->name); + strcat(session->status, "]"); + + strcat(session->status, "("); + char* temp = malloc(20); + sprintf(temp, "%u", session->size); + strcat(session->status, temp); + strcat(session->status, ")"); + + i = 0; + while(i < columns-strlen(session->name)-2-strlen(temp)-2-2-2) + { + strcat(session->status, "-"); + i++; + } + + //free(temp); +} \ No newline at end of file diff --git a/src/plugins/reload.c b/src/plugins/reload.c new file mode 100644 index 0000000..fbe68ea --- /dev/null +++ b/src/plugins/reload.c @@ -0,0 +1,47 @@ +#include "../types.h" +#include +#include +#include +#include +#include +#include + +/** +* Reload +* +* Closes the currently open fd, reopens it and +* reads from it into the data buffer. +*/ + +unsigned char dispatch(struct Editor* editor, char* callType, void* args) +{ + if(strcmp(callType, "onCommand") == 0) + { + /* Close the current file */ + close(editor->currentSession->fd); + + /* TODO: Error check */ + + /* Reopen */ + editor->currentSession->fd = open(editor->currentSession->name, O_RDWR); + + /* Get the stat struct */ + struct stat statStruct; + + /* Fill stat struct */ + fstat(editor->currentSession->fd, &statStruct); + + /* Set the file size */ + editor->currentSession->size = statStruct.st_size; + + /* Free the old file data */ + free(editor->currentSession->data); + + /* Allocate new data */ + editor->currentSession->data = malloc(editor->currentSession->size); + + /* Set the initial characters */ + read(editor->currentSession->fd, editor->currentSession->data, editor->currentSession->size); + } + +} \ No newline at end of file diff --git a/src/plugins/sessions.c b/src/plugins/sessions.c new file mode 100644 index 0000000..0af99f6 --- /dev/null +++ b/src/plugins/sessions.c @@ -0,0 +1,24 @@ +#include "../types.h" +#include +#include +#include + +/** +* Sessions +* +* Session management by Tristan and Stephen. +*/ +unsigned char dispatch(struct Editor* editor, char* callType, void* args) +{ + /* Check for what we are being called */ + if(strcmp(callType, "onCommand") == 0) + { + /* Get arguments */ + char** actualArgs = (char**)args; + + /* TODO: Continue here */ + return 0; + } + + return 1; +} \ No newline at end of file diff --git a/src/plugins/zifty.c b/src/plugins/zifty.c index 1d53e77..63ad2c9 100644 --- a/src/plugins/zifty.c +++ b/src/plugins/zifty.c @@ -10,35 +10,39 @@ * is default as Tristan knows best. */ -void dispatch(struct Session* session) +unsigned char dispatch(struct Editor* editor, char* callType, void* args) { - /* Get dimensions */ - unsigned int columns = session->teletype->columns; - - /* General counter */ - unsigned int i = 0; - - /* STatus line */ - session->status = malloc(columns); - *session->status=0; - - /* Add name */ - strcat(session->status, "{🐻}️️["); - strcat(session->status, session->name); - strcat(session->status, "]"); - - strcat(session->status, "("); - char* temp = malloc(20); - sprintf(temp, "%u", session->size); - strcat(session->status, temp); - strcat(session->status, ")"); - - i = 0; - while(i < columns-strlen(session->name)-2-strlen(temp)-2-2-2) + if(strcmp(callType, "onRedraw") == 0) { - strcat(session->status, "-"); - i++; - } + /* Get dimensions */ + unsigned int columns = editor->currentSession->teletype->columns; - //free(temp); + /* General counter */ + unsigned int i = 0; + + /* STatus line */ + editor->currentSession->status = malloc(columns); + *editor->currentSession->status=0; + + /* Add name */ + strcat(editor->currentSession->status, "{🐻}️️["); + strcat(editor->currentSession->status, editor->currentSession->name); + strcat(editor->currentSession->status, "]"); + + strcat(editor->currentSession->status, "("); + char* temp = malloc(20); + sprintf(temp, "%u", editor->currentSession->size); + strcat(editor->currentSession->status, temp); + strcat(editor->currentSession->status, ")"); + + i = 0; + while(i < columns-strlen(editor->currentSession->name)-2-strlen(temp)-2-2-2) + { + strcat(editor->currentSession->status, "-"); + i++; + } + + //free(temp); + } + } \ No newline at end of file diff --git a/src/session.c b/src/session.c new file mode 100644 index 0000000..981d038 --- /dev/null +++ b/src/session.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +#include "types.h" + +/** +* Allocates a new struct Session on the heap +* and associates a new file with it and all. +*/ +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->fileX = session->size; + session->fileX = 0; + session->fileY = 0; + //session->fileY = linefeedCount(session->data, session->size); + /* Set the tty */ + session->teletype = newTTY(); + //session->teletype->cursorX = session->fileX; + + + /* Set the session to active */ + session->isActive = 1; + + /* Set the plugins to 0 */ + + + /* On success, return the pointer to the session */ + return session; + } + + /* On successful malloc, but file failure */ + free(session); + } + + /* On error */ + return 0; +} diff --git a/src/types.h b/src/types.h index 0e8cd81..219da84 100644 --- a/src/types.h +++ b/src/types.h @@ -27,6 +27,35 @@ struct Plugin //void (*dispatch)(struct Session*) }; +/** +* Represents a line as part of a Map +*/ +struct Line +{ + char* data; + unsigned int length; +}; + +/** +* Represents (x,y) whereby (x,y) exists in file and does not +*/ +struct EditMap +{ + char** flags; + unsigned int rows; + unsigned int columns; +}; + +/** +* Represents the mapping of a file to a text editor screen +*/ +struct Map +{ + struct Edit* editMap; + struct Line* lines; + unsigned int lineCount; +}; + /** * Represents an open editor session. *