aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--badroff.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/badroff.c b/badroff.c
index c2c6515..3e2bd12 100644
--- a/badroff.c
+++ b/badroff.c
@@ -42,11 +42,11 @@ size_t fillbuf(size_t len){ // returns real len
size_t chrfill(char chr){
size_t len = buflen(inbuf);
char* str = peekstrbuf(inbuf, 0, len);
- size_t line;
- for (line = 0; line < len; line++)
- if (str[line] == chr) return line+1;
- for (int c = 0; c != chr && (c = addchr()) != EOF; line++);
- return line;
+ char* line = memchr(str,chr,len);
+ free(str);
+ if (line) return line-str+1;
+ for (int c = 0; c != chr && (c = addchr()) != EOF; len++);
+ return len;
}
#define min(a,b) a < b ? a : b
@@ -55,13 +55,11 @@ size_t chrnfill(char chr, size_t sz){//fills to first of chr or sz
size_t len = buflen(inbuf);
size_t lim = min(len,sz);
char* str = peekstrbuf(inbuf, 0, lim);
- size_t line;
- for (line = 0; line < lim; line++)
- if (str[line] == chr) return line+1;
+ char* line = memchr(str, chr, lim);
free(str);
- for (int c = 0; c != chr && (c = addchr()) != EOF
- && line<sz; line++);
- return line;
+ if (line) return line-str+1;
+ for (int c = 0; c != chr && (c = addchr()) != EOF&& len<sz; len++);
+ return len;
}
// typesetting config and commands to change.
@@ -90,6 +88,7 @@ char* (*call[])(char* txt) = {center, setwidth};
char* cmd(void){
char* dat = popstrbuf(inbuf, chrfill('\n'));
size_t low = 0; size_t high = sizeof(cmds)/sizeof(*cmds); //len
+ char* proc = NULL;
while (high >= low){
int mid = ((unsigned int)low + (unsigned int)high) >> 1;
char* mval = cmds[mid];
@@ -99,11 +98,13 @@ char* cmd(void){
high = mid - 1;
else if (cmp > 0)
low = mid + 1;
- else
- return call[mid](dat+strlen(mval));
+ else{
+ proc = call[mid](dat+strlen(mval));
+ break;
+ }
}
free(dat);
- return NULL;
+ return proc;
}
// normal typesetting
@@ -172,6 +173,7 @@ int main(int argc, char** argv){
char* out;
while ( (out = line())[0] != '\0'){
printf("%s",out);
+ free(out);
}
fclose(in);
return 0;