aboutsummaryrefslogtreecommitdiff
path: root/tt.c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2020-10-23 02:08:13 +0200
committerJohn Ankarström <john@ankarstrom.se>2020-10-23 02:08:13 +0200
commit24da48a86bb542be247127258db5fcafab4ba011 (patch)
tree30e7b826df0d52ae9f5b3b0c779c460b076d2043 /tt.c
parent73bcdd66f3b4ce183bf8b9b0b5784830189c37f5 (diff)
downloadtt-24da48a86bb542be247127258db5fcafab4ba011.tar.gz
release 1.1
Diffstat (limited to 'tt.c')
-rw-r--r--tt.c86
1 files changed, 47 insertions, 39 deletions
diff --git a/tt.c b/tt.c
index 9548957..973a6ad 100644
--- a/tt.c
+++ b/tt.c
@@ -27,14 +27,14 @@ char *doc_prefix;
char *ref; /* current reference */
-char **ins; /* insertions */
+char ***ins; /* insertions */
char **refs; /* references */
int refs_c; /* count */
int refs_s; /* size */
void reference(char *line) {
int i, j;
- char *ln, **tmp;
+ char *ln, **tmp, ***tmp2;
ln = line;
@@ -79,9 +79,9 @@ space:
tmp = realloc(refs, refs_s * sizeof(char *));
if (tmp == NULL) err(1, "malloc");
refs = tmp;
- tmp = realloc(ins, refs_s * sizeof(char *));
- if (tmp == NULL) err(1, "malloc");
- ins = tmp;
+ tmp2 = realloc(ins, refs_s * sizeof(char *));
+ if (tmp2 == NULL) err(1, "malloc");
+ ins = tmp2;
for (i = refs_s - 10; i < refs_s; i++) /* TODO: is this right? */
ins[i] = NULL;
}
@@ -91,8 +91,8 @@ space:
}
bool code(char *line) {
- char *in, *_in; /* current insertion */
- int i, j;
+ char **tmp;
+ int i, j, len;
if (ref[0] == '\0') return false;
if (strlen(code_prefix) > 0)
@@ -104,30 +104,34 @@ bool code(char *line) {
if (strcmp(refs[i], ref) == 0) break;
if (ins[i] == NULL) {
- ins[i] = malloc(1 + (strlen(line) + 1) * sizeof(char));
- j = 0;
+ ins[i] = malloc(1 + 1 * sizeof(char *));
+ if (ins[i] == NULL) err(1, "malloc");
+ len = 0;
} else {
- j = strlen(ins[i]);
- _in = realloc(ins[i],
- 1 + j * sizeof(char) + (strlen(line) + 1) * sizeof(char));
- if (_in == NULL) err(1, "malloc");
- ins[i] = _in;
+ for (len = 0; ins[i][len] != NULL; len++) ;
+ tmp = realloc(ins[i], 1 + (len + 1) * sizeof(char *));
+ if (tmp == NULL) err(1, "malloc");
+ ins[i] = tmp;
}
- strncpy(ins[i] + j, line + strlen(code_prefix),
+ ins[i][len+1] = NULL;
+
+ ins[i][len] = malloc(1 + strlen(line) * sizeof(char));
+ if (ins[i][len] == NULL) err(1, "malloc");
+
+ strncpy(ins[i][len], line + strlen(code_prefix),
strlen(line) - strlen(code_prefix));
- ins[i][j + strlen(line) - strlen(code_prefix)] = '\n';
- ins[i][j + strlen(line) - strlen(code_prefix) + 1] = 0;
+ ins[i][len][strlen(line) - strlen(code_prefix)] = 0;
return true;
}
int main(int argc, char *argv[]) {
- bool finish, found, iscode, wascode;
+ bool finish, iscode, wascode;
char **a, b, c, *line, *out_prefix, *tangledfile, *tmp;
FILE *f, *fo;
- int i, j, k;
- int line_l, line_s, offset;
+ int i, j, k, m;
+ int indent, line_l, line_s, offset;
ref = malloc(1 + (REFMAX + 4) * sizeof(char)); /* incl. << and >> */
if (ref == NULL) err(1, "malloc");
@@ -135,7 +139,7 @@ int main(int argc, char *argv[]) {
refs_c = 0;
refs_s = 10;
refs = malloc(refs_s * sizeof(char *));
- ins = malloc(refs_s * sizeof(char *));
+ ins = malloc(refs_s * sizeof(char **));
if (refs == NULL || ins == NULL) err(1, "malloc");
for (i = 0; i < refs_s; i++)
ins[i] = NULL;
@@ -244,41 +248,45 @@ finish2:
line[line_l] = '\0';
line_l = 0; /* reset line length count */
- if (strncmp(line, "<<", 2) != 0
- || strncmp(line + strlen(line) - 2, ">>", 2) != 0) {
+ ref = line;
+ for (indent = 0; *ref == ' '; ref++) indent++;
+
+ if (strncmp(ref, "<<", 2) != 0
+ || strncmp(ref + strlen(ref) - 2, ">>", 2) != 0) {
fprintf(f, "%s\n", line);
continue;
}
- line += 2;
- line[strlen(line) - 2] = '\0';
+ ref += 2;
+ ref[strlen(ref) - 2] = '\0';
- for (i = 0; i < strlen(line); i++)
- if (isspace(line[i])) {
+ for (i = 0; i < strlen(ref); i++)
+ if (isspace(ref[i])) {
fprintf(f, "%s\n", line);
continue;
}
- if (strlen(line) > REFMAX)
+ if (strlen(ref) > REFMAX)
fprintf(stderr,
"Warning: Truncating identifier exceeding %d characters\n", REFMAX);
- ref = line;
-
- found = false;
for (i = 0; i < refs_c; i++)
- if (strcmp(refs[i], ref) == 0) { found = true; break; }
- if (!found) {
- fprintf(stderr, "Unreferenced destination: %s\n", ref);
- continue;
- }
+ if (strncmp(refs[i], ref, REFMAX) == 0) goto found;
+ fprintf(stderr, "Unreferenced destination: %s\n", ref);
+ continue;
+found:
if (ins[i] == NULL) {
fprintf(stderr, "Warning: Insertion for %s is empty\n", ref);
continue;
}
- if (strncmp(ins[i] + strlen(ins[i]) - 2, "\n\n", 2) == 0)
- ins[i][strlen(ins[i]) - 1] = '\0'; /* remove extra newline */
- fprintf(f, "%s", ins[i]);
+ for (j = 0; ins[i][j] != NULL; j++) {
+ if (ins[i][j + 1] == NULL) {
+ if (strlen(ins[i][j]) == 0)
+ break; /* remove extra newline */
+ }
+ for (m = indent; m > 0; m--) putc(' ', f);
+ fprintf(f, "%s\n", ins[i][j]);
+ }
}
if (c != '\n' && !finish) { finish = true; goto finish2; }