commit 659b77d48f7e938d42d5a1c2f5756aef1c39d96e
parent 157074b407b5b57d17fc9623fe480e4134c18abb
Author: Anton Konyahin <me@konyahin.xyz>
Date: Sat, 8 Jan 2022 23:04:09 +0300
with -e read pattern value from environment variable
Diffstat:
M | Makefile | | | 9 | +++++++-- |
M | src/main.c | | | 68 | ++++++++++++++++++++++++++++---------------------------------------- |
A | src/token.c | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/token.h | | | 8 | ++++++++ |
4 files changed, 97 insertions(+), 42 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,6 @@
.POSIX:
.SUFFIXES:
+.PHONY: clean uninstall test
BIN = sttemp
CC = cc
@@ -13,11 +14,14 @@ strings.o: src/strings.c src/strings.h
files.o : src/files.c src/files.h
$(CC) $(CFLAGS) -c src/files.c
+token.o: src/token.c src/token.h
+ $(CC) $(CFLAGS) -c src/token.c
+
main.o : src/main.c
$(CC) $(CFLAGS) -c src/main.c
-$(BIN): main.o files.o strings.o
- $(CC) main.o files.o strings.o -o $(BIN)
+$(BIN): main.o files.o strings.o token.o
+ $(CC) main.o files.o strings.o token.o -o $(BIN)
clean:
rm $(BIN)
@@ -31,3 +35,4 @@ uninstall:
test: $(BIN)
./sttemp test && cat test && rm -f test
+
diff --git a/src/main.c b/src/main.c
@@ -2,6 +2,7 @@
#include "files.h"
#include "strings.h"
+#include "token.h"
#include <stdio.h>
#include <stdlib.h>
@@ -13,6 +14,9 @@ const char pattern_end[] = "|}";
const int pat_start_len = sizeof(pattern_start) / sizeof(pattern_start[0]) - 1;
const int pat_end_len = sizeof(pattern_end) / sizeof(pattern_end[0]) - 1;
+enum search {WO_ENV, W_ENV};
+typedef enum search Search;
+
void show_usage() {
printf("sttemp - simple template manager\n");
printf("Usage:\n\tsttemp template_name [file_name]\n");
@@ -25,46 +29,23 @@ FILE* open_template(const char* template_name) {
return template;
}
-struct token {
- char* name;
- char* value;
-};
-typedef struct token Token;
-
-void free_token(Token* token) {
- free(token->name);
- free(token->value);
- free(token);
-}
-
-Token** tokens = NULL;
-size_t tokens_len = 0;
-
-void free_tokens() {
- for (size_t i = 0; i < tokens_len; i++) {
- free_token(tokens[i]);
+char* get_placeholder_value(Search search_type, const char* name, size_t length) {
+ char* value = find_in_tokens(name, length);
+ if (value != NULL) {
+ return value;
}
- free(tokens);
- tokens = NULL;
-}
-
-char* get_placeholder_value(const char* name, size_t length) {
- // O(n) = n, but I don't worry about it right now
- for (size_t i = 0; i < tokens_len; i++) {
- if (strncmp(tokens[i]->name, name, length) == 0) {
- return tokens[i]->value;
+ char* new_name = strndup(name, length);
+ if (search_type == W_ENV) {
+ value = getenv(new_name);
+ if (value != NULL) {
+ free(new_name);
+ return value;
}
}
- printf("Enter value for {%.*s}: ", (int) length, name);
- char* value = freadline(stdin);
-
- Token* token = malloc(sizeof(Token));
- token->name = malloc(length);
- memcpy(token->name, name, length);
- token->value = value;
- tokens = realloc(tokens, sizeof(Token) * ++tokens_len);
- tokens[tokens_len - 1] = token;
+ printf("Enter value for {%.*s}: ", (int) length, name);
+ value = freadline(stdin);
+ add_new_token(new_name, value);
return value;
}
@@ -79,10 +60,17 @@ int main(int argc, char *argv[]) {
return 0;
}
- char* template_name = argv[1];
+ size_t first_file_arg = 1;
+ Search search_type = WO_ENV;
+ if (strcmp("-e", argv[1]) == 0) {
+ search_type = W_ENV;
+ first_file_arg++;
+ }
+
+ char* template_name = argv[first_file_arg++];
char* target_name;
- if (argc > 2) {
- target_name = argv[2];
+ if (first_file_arg < argc) {
+ target_name = argv[first_file_arg];
} else {
target_name = template_name;
}
@@ -114,7 +102,7 @@ int main(int argc, char *argv[]) {
}
size_t token_length = end - start;
- char *value = get_placeholder_value(start, token_length);
+ char *value = get_placeholder_value(search_type, start, token_length);
fwrite(value, sizeof(char), strlen(value), output);
start = end + pat_end_len;
diff --git a/src/token.c b/src/token.c
@@ -0,0 +1,54 @@
+/* See LICENSE file for copyright and license details. */
+
+#include "token.h"
+
+struct token {
+ char* name;
+ char* value;
+};
+typedef struct token Token;
+
+Token* new_token(char* name, char* value) {
+ Token* token = malloc(sizeof(Token));
+ token->name = name;
+ token->value = value;
+ return token;
+}
+
+void free_token(Token* token) {
+ free(token->name);
+ free(token->value);
+ free(token);
+}
+
+Token** tokens = NULL;
+size_t tokens_len = 0;
+
+void free_tokens() {
+ for (size_t i = 0; i < tokens_len; i++) {
+ free_token(tokens[i]);
+ }
+ free(tokens);
+ tokens = NULL;
+}
+
+void add_token(Token* token) {
+ tokens = realloc(tokens, sizeof(Token) * ++tokens_len);
+ tokens[tokens_len - 1] = token;
+}
+
+void add_new_token(char* name, char* value) {
+ Token* token = new_token(name, value);
+ add_token(token);
+}
+
+char* find_in_tokens(const char* name, size_t length) {
+ // O(n) = n, but I don't worry about it right now
+ for (size_t i = 0; i < tokens_len; i++) {
+ if (strncmp(tokens[i]->name, name, length) == 0) {
+ return tokens[i]->value;
+ }
+ }
+ return NULL;
+}
+
diff --git a/src/token.h b/src/token.h
@@ -0,0 +1,8 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdlib.h>
+#include <string.h>
+
+void free_tokens();
+void add_new_token(char* name, char* value);
+char* find_in_tokens(const char* name, size_t length);