~ultradvorka/hstr/hh

« back to all changes in this revision

Viewing changes to src/hstr_blacklist.c

  • Committer: Martin Dvorak
  • Date: 2015-04-28 06:56:59 UTC
  • Revision ID: git-v1:f82800ae5daab4850e0dfb87682b83b9a4bb6999
Minor favorites refactoring and blacklist skeleton.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 ============================================================================
 
3
 Name        : hstr_blacklist.c
 
4
 Author      : martin.dvorak@midforger.com
 
5
 Copyright   : Apache 2.0
 
6
 Description : Commands to be skipped from history.
 
7
 ============================================================================
 
8
*/
 
9
 
 
10
#include <stdio.h>
 
11
#include <string.h>
 
12
#include <unistd.h>
 
13
 
 
14
#include "include/hstr_blacklist.h"
 
15
 
 
16
static const char *implicitCommandBlacklist[] = {
 
17
        "ls", "pwd", "cd", "cd ..", "hh", "mc",
 
18
        "ls ", "pwd ", "cd ", "cd .. ", "hh ", "mc "
 
19
};
 
20
 
 
21
void blacklist_init(Blacklist *blacklist)
 
22
{
 
23
    blacklist->loaded=false;
 
24
    blacklist->implicit=false;
 
25
    blacklist->set=malloc(sizeof(HashSet));
 
26
    hashset_init(blacklist->set);
 
27
}
 
28
 
 
29
char* blacklist_get_filename()
 
30
{
 
31
    char *home = getenv(ENV_VAR_HOME);
 
32
    char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_BLACKLIST) + 1);
 
33
    strcpy(fileName, home);
 
34
    strcat(fileName, "/");
 
35
    strcat(fileName, FILE_HH_BLACKLIST);
 
36
    return fileName;
 
37
}
 
38
 
 
39
void blacklist_get(Blacklist *blacklist)
 
40
{
 
41
    if(!blacklist->loaded) {
 
42
        char* fileName = favorites_get_filename();
 
43
        char *file_contents=NULL;
 
44
        if(access(fileName, F_OK) != -1) {
 
45
            long input_file_size;
 
46
 
 
47
            FILE *input_file = fopen(fileName, "rb");
 
48
            fseek(input_file, 0, SEEK_END);
 
49
            input_file_size = ftell(input_file);
 
50
            rewind(input_file);
 
51
            file_contents = malloc((input_file_size + 1) * (sizeof(char)));
 
52
            if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) {
 
53
                exit(EXIT_FAILURE);
 
54
            }
 
55
            fclose(input_file);
 
56
            file_contents[input_file_size] = 0;
 
57
 
 
58
            if(file_contents && strlen(file_contents)) {
 
59
                char *p=strchr(file_contents,'\n');
 
60
                while (p!=NULL) {
 
61
                    p=strchr(p+1,'\n');
 
62
                }
 
63
                char *pb=file_contents, *pe, *s;
 
64
                pe=strchr(file_contents, '\n');
 
65
                while(pe!=NULL) {
 
66
                    *pe=0;
 
67
                    if(!hashset_contains(blacklist->set,pb)) {
 
68
                        s=hstr_strdup(pb);
 
69
                        hashset_add(blacklist->set,s);
 
70
                    }
 
71
                    pb=pe+1;
 
72
                    pe=strchr(pb, '\n');
 
73
                }
 
74
                free(file_contents);
 
75
            }
 
76
        } else {
 
77
            // blacklist not found - use default one (flushed on exit)
 
78
            blacklist->loaded=true;
 
79
            blacklist->implicit=true;
 
80
            int i, length=sizeof(implicitCommandBlacklist)/sizeof(implicitCommandBlacklist[0]);
 
81
            for(i=0; i<length; i++) {
 
82
                hashset_add(blacklist->set, implicitCommandBlacklist[i]);
 
83
            }
 
84
        }
 
85
        free(fileName);
 
86
    }
 
87
}
 
88
 
 
89
bool blacklist_in(Blacklist *blacklist, char *cmd)
 
90
{
 
91
    return hashset_contains(blacklist->set, cmd);
 
92
}
 
93
 
 
94
void blacklist_destroy(Blacklist *blacklist)
 
95
{
 
96
    if(blacklist) {
 
97
        if(blacklist->implicit) {
 
98
            // TODO save blacklist to file if implicit
 
99
        }
 
100
        hashset_destroy(blacklist->set, false);
 
101
        free(blacklist->set);
 
102
        free(blacklist);
 
103
    }
 
104
}