commit 978a3a66d0a0ee35c1d790e6ac68a3e4bab93c5f
parent 659b77d48f7e938d42d5a1c2f5756aef1c39d96e
Author: Anton Konyahin <me@konyahin.xyz>
Date: Sun, 9 Jan 2022 19:53:11 +0300
add man, add ~ resolve
Diffstat:
4 files changed, 73 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,10 +1,13 @@
.POSIX:
.SUFFIXES:
-.PHONY: clean uninstall test
+.PHONY: clean install uninstall test
-BIN = sttemp
-CC = cc
-CFLAGS = -Wall -Werror -O
+BIN = sttemp
+CFLAGS = -Wall -Werror -Os
+LDFLAGS = -s
+
+PREFIX = /usr/local
+MANPREFIX = ${PREFIX}/share/man
all: $(BIN)
@@ -17,22 +20,26 @@ files.o : src/files.c src/files.h
token.o: src/token.c src/token.h
$(CC) $(CFLAGS) -c src/token.c
-main.o : src/main.c
+main.o : src/main.c src/config.h
$(CC) $(CFLAGS) -c src/main.c
$(BIN): main.o files.o strings.o token.o
$(CC) main.o files.o strings.o token.o -o $(BIN)
clean:
- rm $(BIN)
- rm *.o
+ rm -f $(BIN)
+ rm -f *.o
install: $(BIN)
- cp sttemp /usr/local/bin/
+ mkdir -p $(DESTDIR)$(PREFIX)/bin
+ mkdir -p $(DESTDIR)$(MANPREFIX)/man1
+ install -m 775 $(BIN) $(DESTDIR)$(PREFIX)/bin/
+ install -m 644 $(BIN).1 $(DESTDIR)$(MANPREFIX)/man1/
uninstall:
- rm -f /usr/local/bin/sttemp
+ rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
+ rm -f $(DESTDIR)$(MANPREFIX)/man1/$(BIN).1
test: $(BIN)
- ./sttemp test && cat test && rm -f test
+ ./$(BIN) test && cat test && rm -f test
diff --git a/src/config.h b/src/config.h
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+
+const char template_dir[] = "~/projects/templates/";
+const char pattern_start[] = "{|";
+const char pattern_end[] = "|}";
diff --git a/src/main.c b/src/main.c
@@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
+#include "config.h"
#include "files.h"
#include "strings.h"
#include "token.h"
@@ -8,9 +9,6 @@
#include <stdlib.h>
#include <string.h>
-const char template_dir[] = "/Users/antonkonjahin/projects/templates/";
-const char pattern_start[] = "{|";
-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;
@@ -23,7 +21,14 @@ void show_usage() {
}
FILE* open_template(const char* template_name) {
- char *template_path = strconcat(template_dir, template_name);
+ const char* dir;
+ if (template_dir[0] == '~') {
+ char* homedir = getenv("HOME");
+ dir = strconcat(homedir, template_dir + 1);
+ } else {
+ dir = template_dir;
+ }
+ char *template_path = strconcat(dir, template_name);
FILE *template = fopen(template_path, "rb");
free(template_path);
return template;
diff --git a/sttemp.1 b/sttemp.1
@@ -0,0 +1,42 @@
+.TH STTEMP 1 2021-01-09 V1.0
+
+.SH NAME
+sttemp \- simple template manager
+
+.SH SYNOPSIS
+.B sttemp
+[\fB\-h\fR]
+
+.B sttemp
+[\fB\-e\fR]
+\fBtemplate\fR
+[\fBoutput\fR]
+
+.SH DESCRIPTION
+.B sttemp
+copy file from your template directory and fill all
+substitutions with user input, or environment variables.
+Template directory and substitution pattern described in
+\fBsrc/config.h\fR file, in suckless style. You can use
+\fB~\fR as \fBfirst\fR symbol of your path, for relative
+path from your home directory.
+
+.SH OPTIONS
+.TP
+.BI \-h
+displays help information
+.TP
+.BI \-e
+use environment variables for fill substitution in
+template, if \fBsttemp\fR can't find environment variable
+with needed name, it will use user input
+.TP
+.BI template
+name of file in your templates directory, which will use
+to create new file
+.TP
+.BI output
+name of target file, if omit - sttemp will use template name
+
+.SH AUTHOR
+Anton Konyahin <me@konyahin.xyz>