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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
#include <stdarg.h>
#include "omc.h"
extern struct OMC_GLOBAL globals;
char *dirstack[1024];
const size_t dirstack_max = sizeof(dirstack) / sizeof(dirstack[0]);
size_t dirstack_len = 0;
int pushd(const char *path) {
if (dirstack_len + 1 > dirstack_max) {
return -1;
}
dirstack[dirstack_len] = realpath(".", NULL);
dirstack_len++;
return chdir(path);
}
int popd() {
int result = -1;
if (dirstack_len - 1 < 0) {
return result;
}
dirstack_len--;
result = chdir(dirstack[dirstack_len]);
free(dirstack[dirstack_len]);
dirstack[dirstack_len] = NULL;
return result;
}
int rmtree(char *_path) {
int status = 0;
char path[PATH_MAX] = {0};
strncpy(path, _path, sizeof(path));
DIR *dir;
struct dirent *d_entity;
dir = opendir(path);
if (!dir) {
return 1;
}
while ((d_entity = readdir(dir)) != NULL) {
char abspath[PATH_MAX] = {0};
strcat(abspath, path);
strcat(abspath, DIR_SEP);
strcat(abspath, d_entity->d_name);
if (!strcmp(d_entity->d_name, ".") || !strcmp(d_entity->d_name, "..") || !strcmp(abspath, path)) {
continue;
}
// Test for sufficient privilege
if (access(abspath, F_OK) < 0 && errno == EACCES) {
continue;
}
// Push directories on to the stack first
if (d_entity->d_type == DT_DIR) {
rmtree(abspath);
} else {
remove(abspath);
}
}
closedir(dir);
if (access(path, F_OK) == 0) {
remove(path);
}
return status;
}
/**
* Expand "~" to the user's home directory
*
* Example:
* ~~~{.c}
* char *home = expandpath("~"); // == /home/username
* char *config = expandpath("~/.config"); // == /home/username/.config
* char *nope = expandpath("/tmp/test"); // == /tmp/test
* char *nada = expandpath("/~/broken"); // == /~/broken
*
* free(home);
* free(config);
* free(nope);
* free(nada);
* ~~~
*
* @param _path (Must start with a `~`)
* @return success=expanded path or original path, failure=NULL
*/
char *expandpath(const char *_path) {
if (_path == NULL) {
return NULL;
}
const char *homes[] = {
"HOME",
"USERPROFILE",
};
char home[PATH_MAX];
char tmp[PATH_MAX];
char *ptmp = tmp;
char result[PATH_MAX];
char *sep = NULL;
memset(home, '\0', sizeof(home));
memset(ptmp, '\0', sizeof(tmp));
memset(result, '\0', sizeof(result));
strncpy(ptmp, _path, PATH_MAX - 1);
// Check whether there's a reason to continue processing the string
if (*ptmp != '~') {
return strdup(ptmp);
}
// Remove tilde from the string and shift its contents to the left
strchrdel(ptmp, "~");
// Figure out where the user's home directory resides
for (size_t i = 0; i < sizeof(homes) / sizeof(*homes); i++) {
char *tmphome;
if ((tmphome = getenv(homes[i])) != NULL) {
strncpy(home, tmphome, PATH_MAX - 1);
break;
}
}
// A broken runtime environment means we can't do anything else here
if (isempty(home)) {
return NULL;
}
// Scan the path for a directory separator
if ((sep = strpbrk(ptmp, "/\\")) != NULL) {
// Jump past it
ptmp = sep + 1;
}
// Construct the new path
strncat(result, home, PATH_MAX - 1);
if (sep) {
strncat(result, DIR_SEP, PATH_MAX - 1);
strncat(result, ptmp, PATH_MAX - 1);
}
return strdup(result);
}
/**
* Strip directory from file name
* Note: Caller is responsible for freeing memory
*
* @param _path
* @return success=file name, failure=NULL
*/
char *path_basename(char *path) {
char *result = NULL;
char *last = NULL;
if ((last = strrchr(path, '/')) == NULL) {
return result;
}
// Perform a lookahead ensuring the string is valid beyond the last separator
if (last++ != NULL) {
result = last;
}
return result;
}
char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn) {
FILE *fp = NULL;
char **result = NULL;
char *buffer = NULL;
size_t lines = 0;
int use_stdin = 0;
if (strcmp(filename, "-") == 0) {
use_stdin = 1;
}
if (use_stdin) {
fp = stdin;
} else {
fp = fopen(filename, "r");
}
if (fp == NULL) {
perror(filename);
fprintf(SYSERROR);
return NULL;
}
// Allocate buffer
if ((buffer = calloc(OMC_BUFSIZ, sizeof(char))) == NULL) {
perror("line buffer");
fprintf(SYSERROR);
if (!use_stdin) {
fclose(fp);
}
return NULL;
}
// count number the of lines in the file
while ((fgets(buffer, OMC_BUFSIZ - 1, fp)) != NULL) {
lines++;
}
if (!lines) {
free(buffer);
if (!use_stdin) {
fclose(fp);
}
return NULL;
}
rewind(fp);
// Handle invalid start offset
if (start > lines) {
start = 0;
}
// Adjust line count when start offset is non-zero
if (start != 0 && start < lines) {
lines -= start;
}
// Handle minimum and maximum limits
if (limit == 0 || limit > lines) {
limit = lines;
}
// Populate results array
result = calloc(limit + 1, sizeof(char *));
for (size_t i = start; i < limit; i++) {
if (i < start) {
continue;
}
if (fgets(buffer, OMC_BUFSIZ - 1, fp) == NULL) {
break;
}
if (readerFn != NULL) {
int status = readerFn(i - start, &buffer);
// A status greater than zero indicates we should ignore this line entirely and "continue"
// A status less than zero indicates we should "break"
// A zero status proceeds normally
if (status > 0) {
i--;
continue;
} else if (status < 0) {
break;
}
}
result[i] = strdup(buffer);
memset(buffer, '\0', OMC_BUFSIZ);
}
free(buffer);
if (!use_stdin) {
fclose(fp);
}
return result;
}
char *find_program(const char *name) {
static char result[PATH_MAX] = {0};
char *_env_path = getenv(PATH_ENV_VAR);
if (!_env_path) {
errno = EINVAL;
return NULL;
}
char *path = strdup(_env_path);
char *path_orig = path;
char *path_elem = NULL;
if (!path) {
errno = ENOMEM;
return NULL;
}
result[0] = '\0';
while ((path_elem = strsep(&path, PATH_SEP))) {
char abspath[PATH_MAX] = {0};
strcat(abspath, path_elem);
strcat(abspath, DIR_SEP);
strcat(abspath, name);
if (access(abspath, F_OK) < 0) {
continue;
}
strncpy(result, abspath, sizeof(result));
break;
}
path = path_orig;
free(path);
return strlen(result) ? result : NULL;
}
int touch(const char *filename) {
if (access(filename, F_OK) == 0) {
return 0;
}
FILE *fp = fopen(filename, "w");
if (!fp) {
perror(filename);
return 1;
}
fprintf(stderr, "");
fclose(fp);
return 0;
}
int git_clone(struct Process *proc, char *url, char *destdir, char *gitref) {
int result = -1;
char *chdir_to = NULL;
char *program = find_program("git");
if (!program) {
return result;
}
static char command[PATH_MAX];
sprintf(command, "%s clone --recursive %s", program, url);
if (destdir && access(destdir, F_OK) < 0) {
sprintf(command + strlen(command), " %s", destdir);
result = shell2(proc, command);
}
if (destdir) {
chdir_to = destdir;
} else {
chdir_to = path_basename(url);
}
pushd(chdir_to);
{
memset(command, 0, sizeof(command));
sprintf(command, "%s fetch --all", program);
result += shell2(proc, command);
if (gitref != NULL) {
memset(command, 0, sizeof(command));
sprintf(command, "%s checkout %s", program, gitref);
result += shell2(proc, command);
}
popd();
}
return result;
}
char *git_describe(const char *path) {
pushd(path);
static char version[NAME_MAX];
FILE *pp;
pp = popen("git describe --always --tags", "r");
memset(version, 0, sizeof(version));
fgets(version, sizeof(version) - 1, pp);
strip(version);
pclose(pp);
popd();
return version;
}
#define OMC_COLOR_RED "\e[1;91m"
#define OMC_COLOR_GREEN "\e[1;92m"
#define OMC_COLOR_YELLOW "\e[1;93m"
#define OMC_COLOR_BLUE "\e[1;94m"
#define OMC_COLOR_WHITE "\e[1;97m"
#define OMC_COLOR_RESET "\e[0;37m\e[0m"
void msg(unsigned type, char *fmt, ...) {
FILE *stream = NULL;
char header[255];
char status[255];
if (type & OMC_MSG_NOP) {
// quiet mode
return;
}
if (!globals.verbose && type & OMC_MSG_RESTRICT) {
// Verbose mode is not active
return;
}
memset(header, 0, sizeof(header));
memset(status, 0, sizeof(status));
va_list args;
va_start(args, fmt);
stream = stdout;
if (type & OMC_MSG_ERROR) {
// for error output
stream = stderr;
fprintf(stream, "%s", OMC_COLOR_RED);
strcpy(status, " ERROR: ");
} else if (type & OMC_MSG_WARN) {
stream = stderr;
fprintf(stream, "%s", OMC_COLOR_YELLOW);
strcpy(status, " WARNING: ");
} else {
fprintf(stream, "%s", OMC_COLOR_GREEN);
strcpy(status, " ");
}
if (type & OMC_MSG_L1) {
sprintf(header, "==>%s" OMC_COLOR_RESET OMC_COLOR_WHITE, status);
} else if (type & OMC_MSG_L2) {
sprintf(header, " ->%s" OMC_COLOR_RESET, status);
} else if (type & OMC_MSG_L3) {
sprintf(header, OMC_COLOR_BLUE " ->%s" OMC_COLOR_RESET, status);
}
fprintf(stream, "%s", header);
vfprintf(stream, fmt, args);
printf("%s", OMC_COLOR_RESET);
printf("%s", OMC_COLOR_RESET);
va_end(args);
}
void debug_shell() {
msg(OMC_MSG_L1 | OMC_MSG_WARN, "ENTERING OMC DEBUG SHELL\n" OMC_COLOR_RESET);
system("/bin/bash -c 'PS1=\"(OMC DEBUG) \\W $ \" bash --norc --noprofile'");
msg(OMC_MSG_L1 | OMC_MSG_WARN, "EXITING OMC DEBUG SHELL\n" OMC_COLOR_RESET);
exit(255);
}
|