From 93598fef1ea487d76af29eb0f0e6bd8e82dc2be8 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 28 Jan 2016 09:44:11 +0100 Subject: Removed duplicate code; improved Makefile --- Makefile | 17 +++++++++++++---- compile.c | 44 ++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 392730f..eb29c9a 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ +CFLAGS=-std=c99 -Wall -O0 -c +CLM=clm +CLMFLAGS=-I CleanReadLine -l -lreadline -l compile.o +RUNFLAGS=-nr -nt + all: iclean clean: @@ -5,14 +10,18 @@ clean: make -C CleanReadLine clean compile.o: compile.c - cc -O0 -c compile.c + $(CC) $(CFLAGS) compile.c iclean: compile.o iclean.icl readline - clm -I CleanReadLine -l -lreadline -l compile.o iclean -o iclean + $(CLM) $(CLMFLAGS) iclean -o iclean -readline: +readline: FORCE make -C CleanReadLine run: iclean - ./iclean -nr -nt + ./iclean $(RUNFLAGS) + +FORCE: + +.PHONY: FORCE clean run diff --git a/compile.c b/compile.c index d0a3ee0..0240698 100644 --- a/compile.c +++ b/compile.c @@ -31,6 +31,19 @@ #include #include "Clean.h" +char* cleanstoc(CleanString s) { + char* cs = calloc(sizeof(char), CleanStringLength(s) + 1); + if (cs == NULL) + return NULL; + + int i; + for (i = 0; i < CleanStringLength(s); i++) + cs[i] = CleanStringCharacters(s)[i]; + cs[i] = '\0'; + + return cs; +} + /** * Compile a Clean module in a path * @@ -44,39 +57,22 @@ */ int64_t compile(CleanString path, CleanString module) { char* pathchars, * modulechars; - int pathlength, modulelength; - // Copy from CleanString to normal char[] - pathlength = CleanStringLength(path); - modulelength = CleanStringLength(module); - - pathchars = calloc(sizeof(char), pathlength + 1); - modulechars = calloc(sizeof(char), pathlength + 1); + pathchars = cleanstoc(path); + modulechars = cleanstoc(module); + if (modulechars == NULL || pathchars == NULL) { printf("Couldn't allocate memory\n"); exit(-1); } - int i; - for (i=0;i" && clm -ms -o char cmd[64] = "cd \""; - strcat(cmd, pathchars); - strcat(cmd, "\" && clm -ms -nw "); - strcat(cmd, modulechars); - strcat(cmd, " -o "); - strcat(cmd, modulechars); + snprintf(cmd, 64, "cd \"%s\" && clm -ms -nw %s -o %s", pathchars, modulechars, modulechars); - free(pathchars); - free(modulechars); + free(pathchars); + free(modulechars); // Call clm return system(cmd); @@ -114,6 +110,6 @@ void run(CleanString executable) { // -nt strcat(execchars, " -nt"); system(execchars); - free(execchars); + free(execchars); } -- cgit v1.2.3