Skip to content

Commit

Permalink
fixes and safety features
Browse files Browse the repository at this point in the history
  • Loading branch information
timurci committed Jan 30, 2022
1 parent 2469a5b commit 02fe83c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
37 changes: 35 additions & 2 deletions baml.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ size_t valid_token(char *store)
return (0);
}

void pitem(char *istr)
{
size_t isize = strlen(istr);
size_t scan = 0;
size_t checkp = 0;
size_t trimmer = 0;
unsigned char memory = 0;

while (scan < isize)
{
if (istr[scan] > 32 )
trimmer = scan;
if (istr[scan] == '\'' || istr[scan] == '\"' || istr[scan] == '`')
{
memory = istr[scan];
istr[scan] = 0;
printf("%s\\%c", &istr[checkp], memory);
istr[scan] = memory;
checkp = ++scan;
continue ;
}
scan++;
}
istr[trimmer + 1] = 0;
if (memory == 0)
printf("%s", istr);
else if (scan != checkp)
printf("%s", &istr[checkp]);
}

void pheader(char *hstr, char **format, char *hopen)
{
size_t scan;
Expand All @@ -59,7 +89,8 @@ void pheader(char *hstr, char **format, char *hopen)
}
if (*hopen)
printf("%s\n", format[3]);
printf("%s%s", hstr, format[0]);
pitem(hstr);
printf("%s", format[0]);
*hopen = 1;
}

Expand All @@ -83,7 +114,9 @@ void pentry(char *estr, char **format, char *hopen)
printf("%s", format[1]);
else
*hopen = 2;
printf("%s%s%s", format[2], estr, format[2]);
printf("%s", format[2]);
pitem(estr);
printf("%s", format[2]);
}

size_t dstr(char *base, char *tail)
Expand Down
3 changes: 3 additions & 0 deletions baml.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
* "pentry" takes the line "hstr" comprising a valid token and prints entry
* output according to the "format"; "hopen" is passed 1 when there is no
* previous entry in the header, it is set to 2 when an entry is inserted.
* "pitem" takes the target string and prints it by adding '\' for any quote
* character and trimming the end of it.
* "dstr" takes a "base" string and concatenates "tail" by dynamically
* allocating memory. this function is used to store a complete line from
* the file.
*/

char **valid_arg(char *arg);
size_t valid_token(char *store);
void pitem(char *istr);
void pheader(char *hstr, char **format, char *hopen);
void pentry(char *estr, char **format, char *hopen);
size_t dstr(char *base, char *tail);
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ int main(int ac, char **av)
{
pheader(store, format, &hopen);
}
else if (stcursor = valid_token(store))
else if ((stcursor = valid_token(store)))
{
if(!hopen)
{
printf("Incomplete entry at line %d\n", lfile);
printf("Incomplete entry at line %ld\n", lfile);
return (1);
}
pentry(&store[stcursor], format, &hopen);
Expand Down

0 comments on commit 02fe83c

Please sign in to comment.