diff --git a/src/lexer.c b/src/lexer.c index caa1684..ae3c3ce 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -29,15 +29,16 @@ char *process_line(char *line) 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]; // 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); - bzero(compbuf, MAX_STRING_LEN); - tok_srch = strtok(line, "=\" |"); + tok_srch = strtok(line, "=\" ;"); /* reuse function */ /*else if(strncmp("temp",tok_srch,4) == 0) @@ -56,8 +57,59 @@ char *process_line(char *line) // Lets add if and loop statements, make this // Somewhat usable as a language in other instances + + // Loop + else if(strncmp("loop:", tok_srch, 4) == 0) + { + int loop_count, ii; + char *proc_return; + + if(loopbuf != NULL || countbuf != NULL) + { + bzero(loopbuf, MAX_STRING_LEN); + bzero(countbuf, MAX_STRING_LEN); + } + + *loopbuf = '\0'; + // We have an if statement + tok_srch = strtok(NULL, ";"); + + if(tok_srch == NULL) + { + syn_warn("ss:warn:loop statement requires arguments"); + return NULL; + } + + strcat(countbuf, parse_vars(tok_srch)); + loop_count = atoi(countbuf); + + tok_srch = strtok(NULL, ";"); + if(tok_srch == NULL) + { + syn_warn("ss:warn:loop syntax error, missing last argument?"); + return NULL; + } + + strcat(loopbuf, parse_vars(tok_srch)); + + for(ii = 0; ii < loop_count; ii++) + { + proc_return = process_line(parse_vars(loopbuf)); + printf("%s\n", proc_return); + } + + return NULL; + } + + + // IF statement else if(strncmp("if:", tok_srch, 3) == 0) { + if(compbuf != NULL) + { + bzero(compbuf, MAX_STRING_LEN); + } + // We have an if statement tok_srch = strtok(NULL, "\""); if(tok_srch == NULL) @@ -76,13 +128,13 @@ char *process_line(char *line) // Make sure the end quote exists if(strtok(NULL, "\"") == NULL) { - syn_warn("ss:warn:if syntax error, missing quote?"); - return NULL; + syn_warn("ss:warn:if syntax error, missing quote?"); + return NULL; } if(strlen(tok_srch) < MAX_STRING_LEN) { - strcpy(compbuf, tok_srch); + strcpy(compbuf, parse_vars(tok_srch)); } else { @@ -111,7 +163,7 @@ char *process_line(char *line) return NULL; } - if(atoi(compbuf) == atoi(tok_srch)) + if(atoi(compbuf) == atoi(parse_vars(tok_srch))) { return "true"; } @@ -285,7 +337,7 @@ char *process_line(char *line) else if(strncmp("decompress",tok_srch,10) == 0) { char *filename; - struct tar_t *archive = NULL; + struct tar_t *archive = (void *)0; int fd; tok_srch = strtok(NULL, "\""); @@ -344,7 +396,13 @@ char *process_line(char *line) return NULL; } - tar_free(archive); + usleep(50); + + if(archive != (void *)0) + { + tar_free(archive); + archive = (void *)0; + } close(fd); // don't bother checking for fd < 0 @@ -359,7 +417,7 @@ char *process_line(char *line) { char filename[MAX_FILENAME_LEN+1]; // Files to be added into the archive char comp_size[128]; - struct tar_t *archive = NULL; + struct tar_t *archive = (void *)0; int fd; tok_srch = strtok(NULL, "\""); @@ -436,7 +494,11 @@ char *process_line(char *line) return NULL; } - tar_free(archive); + if(archive != (void *)0) + { + tar_free(archive); + archive = (void *)0; + } close(fd); // don't bother checking for fd < 0 FILE *in; diff --git a/src/tar.c b/src/tar.c index c251ed6..e7b53e8 100644 --- a/src/tar.c +++ b/src/tar.c @@ -46,7 +46,7 @@ int tar_read(const int fd, struct tar_t ** archive, const char verbosity){ if (update && (read_size(fd, (*tar) -> block, 512) != 512)){ V_PRINT(stderr, "Error: Bad read. Stopping"); tar_free(*tar); - *tar = NULL; + *tar = (void *)0; break; } @@ -56,20 +56,25 @@ int tar_read(const int fd, struct tar_t ** archive, const char verbosity){ if (read_size(fd, (*tar) -> block, 512) != 512){ V_PRINT(stderr, "Error: Bad read. Stopping"); tar_free(*tar); - *tar = NULL; + *tar = (void *)0; break; } // check if next block is all zeros as well if (iszeroed((*tar) -> block, 512)){ - tar_free(*tar); - *tar = NULL; - // skip to end of record if (lseek(fd, RECORDSIZE - (offset % RECORDSIZE), SEEK_CUR) == (off_t) (-1)){ RC_ERROR("Unable to seek file: %s", strerror(rc)); } + /* + if(*tar != (void *)0) + { + tar_free(*tar); + *tar = (void *)0; + } + */ + break; } @@ -135,6 +140,7 @@ int tar_write(const int fd, struct tar_t **archive, const size_t filecount, cons // write entries first if (write_entries(fd, tar, archive, filecount, files, &offset, verbosity) < 0){ tar_free(*archive); + *tar = (void *)0; } // write ending data @@ -158,9 +164,9 @@ void tar_free (struct tar_t *archive) if(archive != (void *) 0) { free(archive); - archive = NULL; + archive = (void *) 0; + archive = next; } - archive = next; } } @@ -847,6 +853,7 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c // stat file if (format_tar_data(*tar, files[i], verbosity) < 0){ tar_free(*tar); + *tar = (void *)0; } (*tar) -> begin = *offset; @@ -870,12 +877,14 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c // write metadata to (*tar) file if (write_size(fd, (*tar) -> block, 512) != 512){ tar_free(*tar); + *tar = (void *)0; } // go through directory DIR * d = opendir(parent); if (!d){ tar_free(*tar); + *tar = (void *)0; } struct dirent * dir; @@ -889,6 +898,7 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c // recursively write each subdirectory if (write_entries(fd, &((*tar) -> next), head, 1, (const char **) &path, offset, verbosity) < 0){ tar_free(*tar); + *tar = (void *)0; } // go to end of new data @@ -932,6 +942,7 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c // write metadata to (*tar) file if (write_size(fd, (*tar) -> block, 512) != 512){ tar_free(*tar); + *tar = (void *)0; } if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS)){ @@ -940,6 +951,7 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c int f = open((*tar) -> name, O_RDONLY); if (f < 0){ tar_free(*tar); + *tar = (void *)0; } int r = 0; @@ -961,6 +973,7 @@ int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, c for(unsigned int j = 0; j < pad; j++){ if (write_size(fd, "\0", 1) != 1){ tar_free(*tar); + *tar = (void *)0; } } *offset += pad;