sttemp

Simple template manager
git clone git://git.konyahin.xyz/sttemp
Log | Files | Refs | README | LICENSE

main.c (3173B)


      1 /* See LICENSE file for copyright and license details. */
      2 
      3 #include "config.h"
      4 #include "files.h"
      5 #include "strings.h"
      6 #include "token.h"
      7 
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 
     12 const int pat_start_len = sizeof(pattern_start) / sizeof(pattern_start[0]) - 1;
     13 const int pat_end_len = sizeof(pattern_end) / sizeof(pattern_end[0]) - 1;
     14 
     15 enum search {WO_ENV, W_ENV};
     16 typedef enum search Search;
     17 
     18 void show_usage() {
     19     printf("sttemp - simple template manager\n");
     20     printf("Usage:\n\tsttemp template_name [file_name]\n");
     21 }
     22 
     23 FILE* open_template(const char* template_name) {
     24     const char* dir;
     25     if (template_dir[0] == '~') {
     26         char* homedir = getenv("HOME");
     27         dir = strconcat(homedir, template_dir + 1);
     28     } else {
     29         dir = template_dir;
     30     }
     31     char *template_path = strconcat(dir, template_name);
     32     FILE *template = fopen(template_path, "rb");
     33     free(template_path);
     34     return template;
     35 }
     36 
     37 char* get_placeholder_value(Search search_type, const char* name, size_t length) {
     38     char* value = find_in_tokens(name, length);
     39     if (value != NULL) {
     40         return value;
     41     }
     42     char* new_name = strndup(name, length);
     43     if (search_type == W_ENV) {
     44         value = getenv(new_name);
     45         if (value != NULL) {
     46             free(new_name);
     47             return value;
     48         }
     49     }
     50 
     51     printf("Enter value for {%.*s}: ", (int) length, name);
     52     value =  freadline(stdin);
     53     add_new_token(new_name, value);
     54     return value;
     55 }
     56 
     57 int main(int argc, char *argv[]) {
     58     if (argc < 2) {
     59         show_usage();
     60         return 1;
     61     }
     62 
     63     if (strcmp("-h", argv[1]) == 0) {
     64         show_usage();
     65         return 0;
     66     }
     67 
     68     size_t first_file_arg = 1;
     69     Search search_type = WO_ENV;
     70     if (strcmp("-e", argv[1]) == 0) {
     71         search_type = W_ENV;
     72         first_file_arg++;
     73     }
     74     
     75     char* template_name = argv[first_file_arg++];
     76     char* target_name;
     77     if (first_file_arg < argc) {
     78         target_name  = argv[first_file_arg];
     79     } else {
     80         target_name = template_name;
     81     }
     82 
     83     FILE* template = open_template(template_name);
     84     if (template == NULL) {
     85         fprintf(stderr, "Template doesn't exist: %s\n", template_name);
     86         return 1;
     87     }
     88 
     89     size_t buf_len = 0;
     90     char *buf = freadall(template, &buf_len);
     91     fclose(template);
     92 
     93     FILE* output = fopen(target_name, "w");
     94 
     95     char *start = buf;
     96     char *last = start;
     97     while ((start = strstr(start, pattern_start)) != NULL) {
     98         fwrite(last, sizeof(char), start - last, output);
     99         start = start + pat_start_len;
    100 
    101         char* end = strstr(start, pattern_end);
    102         if (end == NULL) {
    103             fprintf(stderr, "Unfinished pattern: %10s", start);
    104             fclose(output);
    105             free(buf);
    106             return 1;
    107         }
    108 
    109         size_t token_length = end - start;
    110         char *value = get_placeholder_value(search_type, start, token_length);
    111         fwrite(value, sizeof(char), strlen(value), output);
    112 
    113         start = end + pat_end_len;
    114         last = start;
    115     }
    116 
    117     fwrite(last, sizeof(char), buf_len - (last - buf), output);
    118     fclose(output);
    119     free(buf);
    120     free_tokens();
    121 
    122     return 0;
    123 }