diff --git a/README.txt b/docs/README.txt similarity index 100% rename from README.txt rename to docs/README.txt diff --git a/examples/functions.ss b/docs/examples/functions.ss similarity index 100% rename from examples/functions.ss rename to docs/examples/functions.ss diff --git a/examples/net-listen.ss b/docs/examples/net-listen.ss similarity index 100% rename from examples/net-listen.ss rename to docs/examples/net-listen.ss diff --git a/examples/net-toss.ss b/docs/examples/net-toss.ss similarity index 100% rename from examples/net-toss.ss rename to docs/examples/net-toss.ss diff --git a/examples/webserver.ss b/docs/examples/webserver.ss similarity index 100% rename from examples/webserver.ss rename to docs/examples/webserver.ss diff --git a/src/eprintf.c b/src/eprintf.c index 8db4e97..8a75a0f 100644 --- a/src/eprintf.c +++ b/src/eprintf.c @@ -177,7 +177,7 @@ V_FUNC x_warn(s,a,b,c,d,e,f,g,h,i,j) char *s; /* format string */ unsigned long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */ { - z_msg (FALSE, "Warning: ", s,a,b,c,d,e,f,g,h,i,j); + z_msg (FALSE, ":", s,a,b,c,d,e,f,g,h,i,j); } /* ------------------------------------------------------------ */ @@ -199,7 +199,7 @@ V_FUNC x_error(s,a,b,c,d,e,f,g,h,i,j) char *s; /* format string */ long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */ { - z_msg (TRUE, "Error: ", s,a,b,c,d,e,f,g,h,i,j); + z_msg (TRUE, ":", s,a,b,c,d,e,f,g,h,i,j); } /* ------------------------------------------------------------ */ @@ -224,7 +224,7 @@ V_FUNC x_panic (s,a,b,c,d,e,f,g,h,i,j) char *s; /* format string */ unsigned long a,b,c,d,e,f,g,h,i,j; /* dummy arguments */ { - z_msg (TRUE, "Internal error: ", + z_msg (TRUE, "INTERNAL ERROR! :", s,a,b,c,d,e,f,g,h,i,j); } diff --git a/src/inc/deps.h b/src/inc/deps.h index 466dd3f..343f120 100644 --- a/src/inc/deps.h +++ b/src/inc/deps.h @@ -43,7 +43,7 @@ #define MAX_VAR_NAME_LEN 512 #define MAX_VAR_NAME_BUFSIZE (MAX_VAR_NAME_LEN + 1) -#define MAX_STRING_LEN 65535 +#define MAX_STRING_LEN 8128 #define MAX_STRING_BUFSIZE (MAX_STRING_LEN + 1) #define MAX_VAR_DATA_LEN 65535 #define MAX_DATA_BUFSIZE (MAX_VAR_DATA_LEN + 1) diff --git a/src/inc/lexer.h b/src/inc/lexer.h index d2aeea3..55c4602 100644 --- a/src/inc/lexer.h +++ b/src/inc/lexer.h @@ -5,4 +5,9 @@ View README file supplied with this software for more details */ +#include "x3mem.h" + +UX3_EXT QLIST QM_RETURN [ONE]; // Dynamic-memory QLIST + + char *process_line(char *line); diff --git a/src/inc/tar.h b/src/inc/tar.h index 532f2ed..5a6c862 100644 --- a/src/inc/tar.h +++ b/src/inc/tar.h @@ -30,6 +30,7 @@ THE SOFTWARE UX3_EXT QLIST QM_TAR [ONE]; // Dynamic-memory QLIST + extern void tar_free_pool (void); #ifndef _DEFAULT_SOURCE diff --git a/src/lexer.c b/src/lexer.c index 603953e..6811afc 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -21,22 +21,21 @@ char *process_line(char *line) { - char *tok_srch; + char *tok_srch, *retbuf; //printf("%s", &line[strlen(line)-2]); static char concatbuf[MAX_CONCAT_BUF+1]; static char filebuf[MAX_READFILE_LEN+1]; - static char retbuf[MAX_STRING_BUFSIZE]; static char compbuf[MAX_STRING_BUFSIZE]; static char loopbuf[MAX_STRING_BUFSIZE]; static char countbuf[MAX_STRING_BUFSIZE]; + static char dirpathbuf[MAX_STRING_BUFSIZE]; // Make sure static buffers are empty before initialize // Write with zeros bzero(filebuf, MAX_READFILE_LEN); bzero(concatbuf, MAX_CONCAT_BUF); - bzero(retbuf, MAX_STRING_LEN); tok_srch = strtok(line, "=\" ;"); @@ -58,14 +57,14 @@ char *process_line(char *line) else if(strncmp("version", tok_srch, 7) == 0) { - sprintf(retbuf, "ss:version: %s, type 'help' for function list\n", VERSION); - return retbuf; + x_warn(":ss:version: %s, type 'help' for function list\n", VERSION); + return NULL; } else if(strncmp("help", tok_srch, 4) == 0) { - sprintf(retbuf, "ss:help:\n%s", FUNCTION_HELP); - return retbuf; + x_warn(":ss:help:\n%s", FUNCTION_HELP); + return NULL; } // Lets add if and loop statements, make this @@ -260,11 +259,11 @@ char *process_line(char *line) else if(strncmp("showpath", tok_srch, 8) == 0 || strncmp("pwd", tok_srch, 3) == 0) { // Get current directory, if it errors, return NULL - if (getcwd(retbuf, sizeof(retbuf)) == NULL) { + if (getcwd(retbuf, sizeof(dirpathbuf)) == NULL) { return NULL; } // All good, return path - return retbuf; + return dirpathbuf; } // Change directory @@ -317,10 +316,8 @@ char *process_line(char *line) if(dirname == NULL) dirname = parse_vars(tok_srch); - sprintf(retbuf, "%s\n", "ss:showdir:"); - struct dirent **files; - int nn; + int nn, dirstr_size; nn = scandir (dirname, &files, NULL, alphasort); @@ -330,18 +327,27 @@ char *process_line(char *line) return NULL; } + dirstr_size = 14; + + for (int jj = 0; jj < nn; jj++) + { + struct dirent *ent = files [jj]; + dirstr_size += (strlen(ent->d_name)+3); + } + + retbuf = qmalloc(QM_RETURN, dirstr_size); + + sprintf(retbuf, "%s\n", "ss:showdir:"); + for (int ii = 0; ii < nn; ii++) { struct dirent *ent = files [ii]; - if((strlen(retbuf) + strlen(ent->d_name) + 1) < MAX_STRING_LEN) - { - strcat(retbuf, ent->d_name); - strcat(retbuf, "\n"); - } - } + strcat(retbuf, ent->d_name); + strcat(retbuf, "\n"); + } - for (int ii = 0; ii < nn; ii++) { free (files [ii]); } - free (files); + for (int ii = 0; ii < nn; ii++) { free (files [ii]); } + free (files); return retbuf; } @@ -386,7 +392,7 @@ char *process_line(char *line) sprintf(origsize, "%ld", fsize(in)); uint32_t deco_return = ss_decompress(filename, filedecout); - + retbuf = qmalloc(QM_RETURN, (sizeof(deco_return) + strlen(filename) + strlen(origsize) + 40)); sprintf(retbuf, "ss: %s: decompressed: %s -> %u", filename, origsize, deco_return); fclose(in); @@ -534,9 +540,12 @@ char *process_line(char *line) { uint32_t comp_return = ss_compress(filename, file_comp, i); uint32_t deco_return = ss_decompress(file_comp, filedecout); - sprintf(retbuf, "ss: %s: compressed: %u -> %u", file_comp, deco_return, comp_return); if(atoi(comp_size) < (int)comp_return && comp_return != 0 && deco_return != 0 && i != 1) - break; + { + retbuf = qmalloc(QM_RETURN, sizeof(deco_return) + sizeof(comp_return) + strlen(file_comp) + 40); + sprintf(retbuf, "ss: %s: compressed: %u -> %u", file_comp, deco_return, comp_return); + break; + } sprintf(comp_size, "%u", comp_return); printf("pass %u decompressed/compressed: %u/%u\n", i, deco_return, comp_return); @@ -1084,9 +1093,10 @@ char *process_line(char *line) catfile = ss_concat(file_md5_val, file_line); if(strlen(catfile) < MAX_CONCAT_BUF) { - strcpy(concatbuf, catfile); + retbuf = qmalloc(QM_RETURN, strlen(catfile)+1); + strcpy(retbuf, catfile); free(catfile); - return concatbuf; + return retbuf; } else { @@ -1314,40 +1324,26 @@ char *process_line(char *line) read_size = ftell(read_file); rewind(read_file); - dynfile = (char *)malloc(read_size + 1); - - *dynfile = '\0'; + dynfile = qmalloc(QM_RETURN, read_size); /* Check if file was opened successfully */ if(read_file == NULL) { - free(dynfile); x_warn("ss:warn:write, failed open"); return NULL; } + char *nullbyte = "\0"; + strcpy(dynfile, nullbyte); + while(fgets(read_line, sizeof(read_line), read_file) != NULL) { strcat(dynfile, read_line); } - if(read_size <= MAX_READFILE_LEN) - { - // Copy string to end of file, rewrite file - sprintf(filebuf, "%s", dynfile); - free(dynfile); - } - else - { - free(dynfile); - fclose(read_file); - x_warn("ss:warn:write, file too large (2MB max)"); - return NULL; - } - fclose(read_file); - return strip_nl(filebuf); + return strip_nl(dynfile); } /* read function */ /* Cat function, writes to end of file specified */ @@ -1427,9 +1423,9 @@ char *process_line(char *line) read_size = ftell(read_file); rewind(read_file); - dynfile = (char *)malloc(read_size + 1); + dynfile = qmalloc(QM_RETURN, read_size); - *dynfile = '\0'; + strcpy(dynfile, "\0"); while(fgets(read_line, sizeof(read_line), read_file) != NULL) { @@ -1440,15 +1436,15 @@ char *process_line(char *line) if((read_size + strlen(parse_vars(tok_srch))) <= MAX_READFILE_LEN) { + char *filebuf = qmalloc(QM_RETURN, (read_size + strlen(parse_vars(tok_srch)))); read_file = fopen(filename, "w"); sprintf(filebuf, "%s%s", dynfile, parse_vars(tok_srch)); fprintf(read_file, "%s", filebuf); - free(dynfile); fclose(read_file); + return NULL; } else { - free(dynfile); fclose(read_file); x_warn("ss:warn:cat, file too large (2MB max)"); return NULL; diff --git a/src/main.c b/src/main.c index 6a67dba..409a3d2 100644 --- a/src/main.c +++ b/src/main.c @@ -53,7 +53,7 @@ int main(int argc, char **argv) if (is_interactive) { - printf ("%s", "ss:prompt: "); + printf ("%s", ":ss:prompt: "); fflush (stdout); } @@ -85,7 +85,10 @@ int main(int argc, char **argv) // set PIPE var back to NULL after use, keep the struct clean set_var(varc, "\0", "\0"); // Check to see if there's anything to even display - if(return_dat == NULL) break; + if(return_dat == NULL) + { + break; + } } } @@ -95,6 +98,9 @@ int main(int argc, char **argv) printf("%s\n", return_dat); fflush(stdout); } + + // Free used mallocs within QM_RETURN + qflush(QM_RETURN); } /* end of while */ } /* file null */