From a409fac4e8baef69b7a5ac7320969c04037d53ca Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Thu, 4 May 2023 11:21:22 -0400 Subject: [PATCH] init --- src/Makefile | 19 ++++++++ src/emk.h | 28 +++++++++++ src/examples/config.emk | 6 +++ src/examples/emkfile | 18 +++++++ src/include.c | 65 +++++++++++++++++++++++++ src/lex-tests/lex2.l | 18 +++++++ src/lex-tests/lexer.l | 5 ++ src/lex-tests/lexer.l.old | 11 +++++ src/parse.c | 100 ++++++++++++++++++++++++++++++++++++++ src/parse.h | 27 ++++++++++ 10 files changed, 297 insertions(+) create mode 100644 src/Makefile create mode 100644 src/emk.h create mode 100644 src/examples/config.emk create mode 100644 src/examples/emkfile create mode 100644 src/include.c create mode 100644 src/lex-tests/lex2.l create mode 100644 src/lex-tests/lexer.l create mode 100644 src/lex-tests/lexer.l.old create mode 100644 src/parse.c create mode 100644 src/parse.h diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..58203de --- /dev/null +++ b/src/Makefile @@ -0,0 +1,19 @@ +CC = /bin/cc +LEX = /bin/lex +RM = /bin/rm + +all: + @echo "Run 'make parser' to build the parser, or 'make lexer' to build the lexer." + +parser: + $(CC) parse.c -o parser + +lexer: + $(LEX) lexer.l + $(CC) lex.yy.c -lfl -o lexer + +clean-parser: + $(RM) parser + +clean-lexer: + $(RM) lexer lex.yy.c diff --git a/src/emk.h b/src/emk.h new file mode 100644 index 0000000..53520ba --- /dev/null +++ b/src/emk.h @@ -0,0 +1,28 @@ +/* + emk.h - define functions + + This file is part of emk. + + emk is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + emk is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with emk. If not, see . +*/ + +#ifndef EMK_H_ +#define EMK_H_ + +int checkForRoot(); +char checkForEmkFile(); +char readEmkFile(); +char includeFromConfig(); + +#endif diff --git a/src/examples/config.emk b/src/examples/config.emk new file mode 100644 index 0000000..d501d20 --- /dev/null +++ b/src/examples/config.emk @@ -0,0 +1,6 @@ +// config.emk + +declare CC = "/bin/cc" +declare AR = "/bin/ar" + +declare PREFIX = "/usr" diff --git a/src/examples/emkfile b/src/examples/emkfile new file mode 100644 index 0000000..842241b --- /dev/null +++ b/src/examples/emkfile @@ -0,0 +1,18 @@ +include ./config.emk; + +declare NCVER = "1.0"; +declare MKOBJ = "$CC $CFLAGS $LDFLAGS -c"; + +function build { + echo "Building package"; + $COMPILE src/main.c -o src/main.o; + $COMPILE src/printevents.c -o src/printevents.o; + $COMPILE src/mkevent.c -o src/mkevent.o; + $CC $CFLAGS $LDFLAGS -o src/nc src/main.o src/printevents.o src/mkevent.o; +} + +function install { + install src/nc $PREFIX/bin; + install man/nc.1 $PREFIX/share/man/man1; + gzip $PREFIX/share/man/man1/nc.1; +} diff --git a/src/include.c b/src/include.c new file mode 100644 index 0000000..d65220a --- /dev/null +++ b/src/include.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include + +#include "parse.h" + +/* char process-old() { + char cwd[PATH_MAX]; + FILE *emkconfig, *emkfile; + char filename[100], contents; + + emkconfig = fopen(EMKCONFIG, "r"); + if (emkconfig == NULL) { + printf(COL_BLUE "INF" COL_RESET " | no config.emk found\n"); + } else if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf(COL_BLUE "INF" COL_RESET " | including config.emk at %s\n", cwd); + } + + emkfile = fopen(EMKFILE, "a+"); + + contents = fgetc(emkconfig); + while (contents != EOF) { + fputc(contents, emkfile); + contents = fgetc(emkconfig); + } + + fclose(emkconfig); + fclose(emkfile); +} */ + +char processFile() { + char cwd[PATH_MAX]; + FILE *emkconfig, *emkfile, *emkfile_new; + char filename[100], contents; + + emkconfig = fopen(EMKCONFIG, "r"); + emkfile = fopen(EMKFILE, "r"); + + emkfile_new = fopen(EMKFILE_NEW, "a+"); + + /* write contents of emkconfig to new emkfile */ + contents = fgetc(emkconfig); + while (contents != EOF) { + fputc(contents, emkfile_new); + contents = fgetc(emkconfig); + } + + /* write contents of emkfile to new emkfile */ + contents = fgetc(emkfile); + while (contents != EOF) { + fputc(contents, emkfile_new); + contents = fgetc(emkfile); + } + + fclose(emkconfig); + fclose(emkfile); +} + +int main() { + processFile(); +} + diff --git a/src/lex-tests/lex2.l b/src/lex-tests/lex2.l new file mode 100644 index 0000000..25e6fdb --- /dev/null +++ b/src/lex-tests/lex2.l @@ -0,0 +1,18 @@ +%% +"include" { printf("INCLUDE"); } +"declare" { printf("DECLARE"); } +"function" { printf("FUNCTION"); } +%% + +#define EMKFILE "emkfile" + +int yywrap(){} +int main() { + FILE *fp; + char filename[50]; + fp = fopen(EMKFILE, "r"); + yyin = fp; + + yylex(); + return 0; +} diff --git a/src/lex-tests/lexer.l b/src/lex-tests/lexer.l new file mode 100644 index 0000000..466e7cd --- /dev/null +++ b/src/lex-tests/lexer.l @@ -0,0 +1,5 @@ +%% +"include" { printf("INCLUDE"); } +"declare" { printf("DECLARE"); } +"function" { printf("FUNCTION"); } +%% diff --git a/src/lex-tests/lexer.l.old b/src/lex-tests/lexer.l.old new file mode 100644 index 0000000..98d5040 --- /dev/null +++ b/src/lex-tests/lexer.l.old @@ -0,0 +1,11 @@ +%% + +"function" { printf("FUNCTION"); } +"declare" { printf("DECLARE"); } +"include" { printf("INCLUDE"); } + +"""" { printf("STRING %s STRING_END", yytext); } + +. { printf("%s", yytext); } + +%% diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..225fcf0 --- /dev/null +++ b/src/parse.c @@ -0,0 +1,100 @@ +/* + parse.c - parse an emkfile + + This file is part of emk. + + emk is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + emk is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with emk. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include "parse.h" + +struct emkfileData { + char cc; + char prefix; +}; + +int checkForRoot() { + printf(COL_BLUE "INF" COL_RESET " | checking for root access...\n"); + if (geteuid() != 0) { + printf(COL_BLUE "INF" COL_RESET " | running emk without root\n"); + } else { + printf(COL_BLUE "INF" COL_RESET " | running emk as root\n"); + } +} + +char checkForEmkFile() { + char cwd[PATH_MAX]; + FILE *emkfile; + + emkfile = fopen(EMKFILE, "r"); + if (emkfile == NULL) { + printf(COL_RED "ERR" COL_RESET " | emkfile does not exist\n"); + exit(1); + } else if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf(COL_BLUE "INF" COL_RESET " | using emkfile at %s/emkfile\n", cwd); + } + fclose(emkfile); +} + +char readEmkFile() { + FILE *emkfile; + + char filename[100], contents; + + emkfile = fopen(EMKFILE, "r"); + printf(COL_BLUE "INF" COL_RESET " | opening emkfile\n"); + contents = fgetc(emkfile); + while (contents != EOF) { + printf ("%c", contents); + contents = fgetc(emkfile); + } + fclose(emkfile); +} + +char includeFromConfig() { + char cwd[PATH_MAX]; + FILE *emkconfig, *emkfile; + char filename[100], contents; + + emkconfig = fopen(EMKCONFIG, "r"); + if (emkconfig == NULL) { + printf(COL_BLUE "INF" COL_RESET " | no config.emk found\n"); + } else if (getcwd(cwd, sizeof(cwd)) != NULL) { + printf(COL_BLUE "INF" COL_RESET " | including config.emk at %s\n", cwd); + } + + emkfile = fopen(EMKFILE, "w"); + + contents = fgetc(emkconfig); + while (contents != EOF) { + fputc(contents, emkfile); + contents = fgetc(emkconfig); + } + + fclose(emkconfig); + fclose(emkfile); +} + +int main() { + checkForEmkFile(); + readEmkFile(); + checkForRoot(); +} diff --git a/src/parse.h b/src/parse.h new file mode 100644 index 0000000..2c37a68 --- /dev/null +++ b/src/parse.h @@ -0,0 +1,27 @@ +/* + parse.h - define macros for parse.c + + This file is part of emk. + + emk is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + emk is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with emk. If not, see . +*/ + +#ifndef PARSE_H_ +#define PARSE_H_ + +#define EMKFILE "emkfile" +#define EMKFILE_NEW "emkfile.new" +#define EMKCONFIG "config.emk" + +#endif