aboutsummaryrefslogtreecommitdiff
path: root/later.c
blob: 485b825bd2c22cd4b1c9f19339b6374dbc164b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "later.h"

LaterItem_t* new_item() {
    LaterItem_t* item = (LaterItem_t*)malloc(sizeof(LaterItem_t));
    item->directory = NULL;
    item->path = NULL;
    item->command = NULL;
    return item;
}

LaterFile_t* new_LaterFile() {
    LaterFile_t* file = (LaterFile_t*)malloc(sizeof(LaterFile_t));
    file->filename = NULL;
    file->file = NULL;
    file->capacity = 8;
    file->items = (LaterItem_t**)malloc(sizeof(LaterItem_t*) * file->capacity);
    file->size = 0;
    file->loaded = false;
    file->modified = false;
    return file;
}

bool build_LaterFile(LaterFile_t* file, char* laterfile_path) {
    // open the file for reading/appending (so it gets created if necessary)
    FILE* laterfile = fopen(laterfile_path, "a+");
    fseek(laterfile, 0, SEEK_END);
    int filesize = ftell(laterfile);
    rewind(laterfile);

    file->filename = laterfile_path;
    file->file = laterfile;

    // if the file is empty, we're done
    if (filesize == 0) {
        printf("empty file!\n");
        return true;
    }

    // start reading in entries
    char buf[2048];
    char* ret;
    LaterItem_t* curitem = NULL;
    while (true) {
        // get the next line
        ret = fgets(buf, sizeof(buf), laterfile);
        if (ret == NULL) {
            break;
        } else if (ret[strlen(ret)-1] == '\n') {
            ret[strlen(ret)-1] = '\0';
        }

        // are we starting a new item?
        if (curitem == NULL) {
            curitem = new_item();
        }

        // if we've reached the end of a definition
        if (strcmp(ret, "--") == 0) {
            file->items[file->size] = curitem;
            file->size++;
            curitem = NULL;
        } else if (strncmp(ret, "DIR ", 4) == 0) {
            // directory
            curitem->directory = strdup(ret+4);
        } else if (strncmp(ret, "PATH ", 5) == 0) {
            //path
            curitem->path = strdup(ret+5);
        } else if (strncmp(ret, "DO ", 3) == 0) {
            // command
            curitem->command = strdup(ret+3);
        } else {
            // fallthrough
            printf("unrecognized: %s\n", ret);
            break;
        }
    }

    if (curitem != NULL) {
        free(curitem);
        return false;
    }
        
    return true;
}

void add_item(LaterFile_t* file, LaterItem_t* item) {
    // TODO capacity things
    file->items[file->size] = item;
    file->size++;
    file->modified = true;
}

void free_LaterFile(LaterFile_t* file) {
    fclose(file->file);
    LaterItem_t* cur;
    for (int i = 0; i < file->capacity; i++) {
        cur = file->items[i];
        if (cur != NULL) {
            free(cur);
        }
    }
}

void free_item(LaterItem_t* item) {
    if (item->directory) free(item->directory);
    if (item->path) free(item->path);
    if (item->command) free(item->command);
    free(item);
}

void build_item(LaterItem_t* item, char** raw_command, int raw_command_length) {
    // get the current directory
    char directory[255];
    getcwd(directory, sizeof(directory)-1);
    item->directory = strdup(directory);

    // get the current path
    char* path = getenv("PATH");
    item->path = path;

    // copy the command
    char* command = (char*)malloc(2048);
    int command_idx = 0;
    char* cur;
    for (int raw_idx = 0; raw_idx < raw_command_length; raw_idx++) {
        cur = raw_command[raw_idx];
        command_idx += strlen(cur);
        strcat(command, cur);
        command[command_idx] = ' ';
        command_idx++;
    }
    item->command = command;
}

void debug_item(LaterItem_t* item) {
    /*
     * Format:
     * DIR <pwd>
     * PATH <path>
     * DO <command>
     * --
     */
    printf("DIR %s\nPATH %s\nDO %s\n--\n", item->directory, item->path, item->command);
}

void write_file(LaterFile_t* file) {
    // reopen the file for writing
    FILE* newfp = freopen(file->filename, "w", file->file);
    LaterItem_t* curitem;
    for (int i = 0; i < file->size; i++) {
        curitem = file->items[i];
        fprintf(newfp, "DIR %s\nPATH %s\nDO %s\n--\n", curitem->directory, curitem->path, curitem->command);
    }
}

int main(int argc, char** argv) {
    /* how it could work
     *  - read in tokens
     *  - wait for --
     *  - after --, read everything in and save to the LATERFILE
     *  - make sure to also save the PWD
     */
    char* laterfile_path = getenv("LATERFILE");
    if (!laterfile_path || strlen(laterfile_path) == 0) {
        printf("where's your LATERFILE?\n");
        return 1;
    }

    LaterFile_t* file = new_LaterFile();
    bool ret = build_LaterFile(file, laterfile_path);
    if (!ret) {
        printf("couldn't build file, exiting\n");
        return 1;
    }

    // debug the existing file
    LaterItem_t* cur;
    for (int i = 0; i < file->size; i++) {
        cur = file->items[i];
        debug_item(cur);
    }

    // loop through the arguments
    int argi = 1;
    while (argi < argc && strncmp(argv[argi], "--", 2) != 0)
        argi++;

    if (argi == argc) {
        printf("unrecognized arguments\n");
        free(file);
        return 1;
    }

    argi++;  // go past the --
    
    // build new item
    cur = new_item();
    build_item(cur, argv+argi, argc-argi);
    add_item(file, cur);

    // write the file if necessary
    if (file->modified)
        write_file(file);

    // cleanup
    free(file);
    return 0;
}