-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathawait.c
More file actions
1078 lines (958 loc) · 38.2 KB
/
Copy pathawait.c
File metadata and controls
1078 lines (958 loc) · 38.2 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <syslog.h>
#include <pthread.h>
#include <pwd.h>
#include <limits.h>
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
char *spinner[] = {"⣾","⣽","⣻","⢿","⡿","⣟","⣯","⣷"};
typedef struct {
int spinner;
char *command;
char *name;
char *out;
char *previousOut;
char *diffOut; // For storing difference-highlighted output
size_t outPos;
int status;
int change;
int warned127;
int pid;
pthread_t thread;
long start_time;
long last_duration_ms;
long prev_duration_ms;
} COMMAND;
COMMAND c[100];
COMMAND exec;
typedef struct {
int expectedStatus;
int interval;
int timeout;
int cmd_timeout;
long start_time;
int any;
int change;
int silent;
int forever;
int daemonize;
int fail;
int stdout;
int diff;
char *exec;
char* service;
char* args;
int nCommands;
int no_stderr;
int retry;
int json;
int lap;
} ARGS;
ARGS args = {.interval=200, .expectedStatus = 0, .silent=0, .change=0, .nCommands=0, .args="", .timeout=0, .cmd_timeout=0, .retry=0};
int const BUF_SIZE = 1024;
int const CHUNK_SIZE = BUF_SIZE * 100;
char* replace(const char* oldW, const char* newW, const char* s) {
char* result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
// Counting the number of times old word
// occur in the string
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], oldW) == &s[i]) {
cnt++;
// Jumping to index after the old word.
i += oldWlen - 1;
}
}
// Making new string of enough length
result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);
i = 0;
while (*s) {
// compare the substring with the result
if (strstr(s, oldW) == s) {
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
if (args.daemonize) closelog();
return 0;
}
void print_autocomplete_fish() {
printf("function __fish_await_no_subcommand\n"
" set -l cmd (commandline -opc)\n"
" for i in $cmd\n"
" switch $i\n"
" case --help --stdout --silent --fail --status --any --change --diff --exec --interval --timeout --cmd-timeout --forever --service --watch\n"
" return 1\n"
" end\n"
" end\n"
" return 0\n"
"end\n"
"complete -c await -n '__fish_await_no_subcommand' -l version -s v -d 'Print the version of await'\n"
"complete -c await -n '__fish_await_no_subcommand' -l help -d 'Print this help'\n"
"complete -c await -n '__fish_await_no_subcommand' -l stdout -s o -d 'Print stdout of commands'\n"
"complete -c await -n '__fish_await_no_subcommand' -l silent -s V -d 'Do not print spinners and commands'\n"
"complete -c await -n '__fish_await_no_subcommand' -l fail -s f -d 'Waiting commands to fail'\n"
"complete -c await -n '__fish_await_no_subcommand' -l status -s s -d 'Expected status [default: 0]' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l any -s a -d 'Terminate if any of command return expected status'\n"
"complete -c await -n '__fish_await_no_subcommand' -l change -s c -d 'Waiting for stdout to change and ignore status codes'\n"
"complete -c await -n '__fish_await_no_subcommand' -l diff -s d -d 'Highlight differences between previous and current output'\n"
"complete -c await -n '__fish_await_no_subcommand' -l exec -s e -d 'Run some shell command on success' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l interval -s i -d 'Seconds between one round of commands [default: 0.2]' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l timeout -s T -d 'Seconds to wait before giving up [default: 0]' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l cmd-timeout -s t -d 'Seconds per command before killing it' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l retry -s r -d 'Max number of attempts before giving up [default: 0 (unlimited)]' -r\n"
"complete -c await -n '__fish_await_no_subcommand' -l forever -s F -d 'Do not exit ever'\n"
"complete -c await -n '__fish_await_no_subcommand' -l service -s S -d 'Create systemd user service with same parameters and activate it'\n"
"complete -c await -n '__fish_await_no_subcommand' -l no-stderr -s E -d 'Surpress stderr of commands by adding 2>/dev/null to commands'\n"
"complete -c await -n '__fish_await_no_subcommand' -l watch -s w -d 'Equivalent to -fVodE (fail, silent, stdout, diff, no-stderr)'\n"
"\n"
"# For command completion\n"
"complete -c await -f -a '(__fish_complete_command)'\n");
}
void print_autocomplete_bash() {
printf("_await() {\n"
" local cur prev opts\n"
" COMPREPLY=()\n"
" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n"
" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n"
"\n"
" opts=\"--help --stdout --silent --fail --status --any --change --diff --exec --interval --timeout --cmd-timeout --forever --service --version --no-stderr --watch\"\n"
"\n"
" case \"${prev}\" in\n"
" --status|--exec|--interval|--timeout|--cmd-timeout)\n"
" COMPREPLY=($(compgen -f -- \"${cur}\"))\n"
" return 0\n"
" ;;\n"
" esac\n"
"\n"
" if [[ ${cur} == -* ]]; then\n"
" COMPREPLY=($(compgen -W \"${opts}\" -- \"${cur}\"))\n"
" return 0\n"
" fi\n"
"\n"
" COMPREPLY=($(compgen -c -- \"${cur}\"))\n"
" return 0\n"
"}\n"
"\n"
"complete -F _await await\n");
}
void print_autocomplete_zsh() {
printf("# Ensure compinit is loaded\n"
"autoload -Uz compinit\n"
"compinit\n"
"\n"
"# Define the completion function for 'await'\n"
"_await() {\n"
" _arguments -s -S \\\n"
" '--help[Print this help]' \\\n"
" '--version[Display version]' \\\n"
" '--stdout[Print stdout of commands]' \\\n"
" '--no-stderr[Surpress stderr of commands by adding 2>/dev/null to commands]' \\\n"
" '--silent[Do not print spinners and commands]' \\\n"
" '--fail[Wait for commands to fail]' \\\n"
" '--status[Expected status (default: 0)]::status' \\\n"
" '--any[Terminate if any command returns expected status]' \\\n"
" '--change[Wait for stdout to change and ignore status codes]' \\\n"
" '--diff[Highlight differences between previous and current output]' \\\n"
" '--exec[Run some shell command on success]::command:_command_names' \\\n"
" '--interval[Seconds between rounds of commands (default: 0.2)]::interval' \\\n"
" '--timeout[Seconds to wait before giving up (default: 0)]::timeout' \\\n"
" '--cmd-timeout[Seconds per command before killing it]::cmd-timeout' \\\n"
" '--forever[Do not exit ever]' \\\n"
" '--watch[Equivalent to -fVodE (fail, silent, stdout, diff, no-stderr)]' \\\n"
" '--service[Create systemd user service with same parameters and activate it]'\n"
"}\n"
"\n"
"# Register the completion function\n"
"compdef _await await\n");
}
void install_autocompletions() {
const char *home;
if ((home = getenv("HOME")) == NULL) {
home = getpwuid(getuid())->pw_dir;
}
// Get the path to the current binary
char binary_path[PATH_MAX];
ssize_t len = readlink("/proc/self/exe", binary_path, sizeof(binary_path) - 1);
if (len == -1) {
// Fallback to "await" if we can't get the binary path
strcpy(binary_path, "await");
} else {
binary_path[len] = '\0';
}
printf("Detecting installed shells and installing completions...\n\n");
// Check bash
if (access("/bin/bash", F_OK) == 0 || access("/usr/bin/bash", F_OK) == 0) {
printf("✓ bash found\n");
char bashrc[PATH_MAX];
snprintf(bashrc, sizeof(bashrc), "%s/.bashrc", home);
char cmd[PATH_MAX * 4];
// Check if completions already exist, only append if not present
snprintf(cmd, sizeof(cmd), "grep -q '_await' '%s' 2>/dev/null || '%s' --autocomplete-bash >> '%s' 2>/dev/null", bashrc, binary_path, bashrc);
int ret = system(cmd);
if (ret == 0) {
printf(" → completions installed to ~/.bashrc\n");
} else {
printf(" → failed to install completions\n");
}
} else {
printf("✗ bash not found\n");
}
// Check zsh
if (access("/bin/zsh", F_OK) == 0 || access("/usr/bin/zsh", F_OK) == 0) {
printf("✓ zsh found\n");
char zshrc[PATH_MAX];
snprintf(zshrc, sizeof(zshrc), "%s/.zshrc", home);
char cmd[PATH_MAX * 4];
// Check if completions already exist, only append if not present
snprintf(cmd, sizeof(cmd), "grep -q '_await' '%s' 2>/dev/null || '%s' --autocomplete-zsh >> '%s' 2>/dev/null", zshrc, binary_path, zshrc);
int ret = system(cmd);
if (ret == 0) {
printf(" → completions installed to ~/.zshrc\n");
} else {
printf(" → failed to install completions\n");
}
} else {
printf("✗ zsh not found\n");
}
// Check fish
if (access("/bin/fish", F_OK) == 0 || access("/usr/bin/fish", F_OK) == 0) {
printf("✓ fish found\n");
char fish_dir[PATH_MAX];
snprintf(fish_dir, sizeof(fish_dir), "%s/.config/fish/completions", home);
char fish_file[PATH_MAX];
snprintf(fish_file, sizeof(fish_file), "%s/await.fish", fish_dir);
char cmd[PATH_MAX * 4];
// Check if completions already exist, only append if not present
snprintf(cmd, sizeof(cmd), "mkdir -p '%s' 2>/dev/null && (grep -q '__fish_await' '%s' 2>/dev/null || '%s' --autocomplete-fish >> '%s' 2>/dev/null)", fish_dir, fish_file, binary_path, fish_file);
int ret = system(cmd);
if (ret == 0) {
printf(" → completions installed to ~/.config/fish/completions/await.fish\n");
} else {
printf(" → failed to install completions\n");
}
} else {
printf("✗ fish not found\n");
}
printf("\nAutocompletions installation complete!\n");
exit(0);
}
static void daemonize() {
pid_t pid;
pid = fork();
if (pid < 0) exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0) exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0) exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
// signal(SIGCHLD, SIG_IGN);
// signal(SIGHUP, SIG_IGN);
//
// /* Fork off for the second time*/
// pid = fork();
//
// if (pid < 0) exit(EXIT_FAILURE);
//
// /* Success: Let the parent terminate */
// if (pid > 0) exit(EXIT_SUCCESS);
/* Set new file permissions */
umask(0);
setpgid(0, 0);
/* Close all open file descriptors */
// int x;
// for (x = sysconf(_SC_OPEN_MAX); x>=0; x--) {
// close (x);
// }
// /* Open the log file */
openlog("await", LOG_PID, LOG_DAEMON);
}
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0)
{
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
long current_time_ms() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
char * replace_placeholders(char *string) {
for(int i = 0; i < args.nCommands; i = i + 1) {
if (!c[i].previousOut) continue;
char C[3];
sprintf(C, "\\%d", i+1);
string = replace(C, c[i].previousOut, string);
}
for(int i = 1; i <= args.nCommands; i++) {
if (!c[i].previousOut || !c[i].name || c[i].name == c[i].command) continue;
char named[256];
snprintf(named, sizeof(named), "\\%s", c[i].name);
string = replace(named, c[i].previousOut, string);
}
return string;
}
void print_json_result(int exit_code) {
long elapsed = args.timeout > 0 ? current_time_ms() - args.start_time : 0;
printf("{\"success\":%s,\"elapsed_ms\":%ld,\"commands\":[",
exit_code == 0 ? "true" : "false", elapsed);
for (int i = 1; i <= args.nCommands; i++) {
if (i > 1) printf(",");
char *out = c[i].previousOut ? c[i].previousOut : "";
// escape quotes in output
printf("{\"name\":\"%s\",\"command\":\"%s\",\"status\":%d,\"output\":\"",
c[i].name ? c[i].name : "", c[i].command, c[i].status);
for (char *p = out; *p; p++) {
if (*p == '"') printf("\\\"");
else if (*p == '\\') printf("\\\\");
else if (*p == '\n') printf("\\n");
else if (*p == '\r') printf("\\r");
else putchar(*p);
}
printf("\"}");
}
printf("]}\n");
}
char * colorize_comments(char *string) {
string = replace("#", "\033[33m#", string);
string = replace("\n", "\033[0m\n", string);
return string;
}
char * highlight_differences(const char *old_text, const char *new_text) {
if (!old_text || !new_text) return NULL;
if (strcmp(old_text, new_text) == 0) return NULL; // No differences
int old_len = strlen(old_text);
int new_len = strlen(new_text);
// Allocate enough space for highlighted text (worst case: every char highlighted)
char *highlighted = malloc(new_len * 10); // Generous space for ANSI codes
highlighted[0] = '\0';
int old_pos = 0, new_pos = 0;
int in_diff = 0;
// Simple character-by-character diff
while (new_pos < new_len) {
if (old_pos < old_len && old_text[old_pos] == new_text[new_pos]) {
// Characters match
if (in_diff) {
strcat(highlighted, "\033[0m"); // End highlighting
in_diff = 0;
}
// Add the matching character
int len = strlen(highlighted);
highlighted[len] = new_text[new_pos];
highlighted[len + 1] = '\0';
old_pos++;
new_pos++;
} else {
// Characters differ or we're at different positions
if (!in_diff) {
strcat(highlighted, "\033[32m"); // Start highlighting (green text)
in_diff = 1;
}
// Add the differing character from new text
int len = strlen(highlighted);
highlighted[len] = new_text[new_pos];
highlighted[len + 1] = '\0';
new_pos++;
// Skip characters in old text until we find a match or reach the end
if (old_pos < old_len) {
// Look ahead to see if we can find a match
int found_match = 0;
for (int lookahead = 1; lookahead <= 10 && (old_pos + lookahead) < old_len; lookahead++) {
if (new_pos < new_len && old_text[old_pos + lookahead] == new_text[new_pos]) {
old_pos += lookahead;
found_match = 1;
break;
}
}
if (!found_match) {
old_pos++;
}
}
}
}
// Close highlighting if still open
if (in_diff) {
strcat(highlighted, "\033[0m");
}
return highlighted;
}
void help() {
printf("%s", colorize_comments("await [options] commands\n\n"
"# runs list of commands and waits for their termination\n"
"\n\nEXAMPLES:\n"
"# wait until your deployment is ready\n"
" await 'curl 127.0.0.1:3000/healthz' \\\n\t'kubectl wait --for=condition=Ready pod it-takes-forever-8545bd6b54-fk5dz' \\\n\t\"docker inspect --format='{{json .State.Running}}' elasticsearch 2>/dev/null | grep true\" \n\n"
"# emulate watch https://linux.die.net/man/1/watch\n"
" await 'clear; du -h /tmp/file' 'dd if=/dev/random of=/tmp/file bs=1M count=1000 2>/dev/null' -of --silent\n\n"
"# action on specific file type changes\n"
" await 'stat **.c' --change --forever --exec 'gcc *.c -o await -lpthread'\n\n"
"# Kubernetes: wait for the pod, then forward the port\n"
" await 'kubectl get pod myapp | grep Running' --timeout 120 \\\n\t--exec 'kubectl port-forward pod/myapp 8080:80'\n\n"
"# wait for postgres AND redis, then run the migration\n"
" await 'pg_isready -h localhost' 'redis-cli ping' \\\n\t--exec 'python manage.py migrate'\n\n"
"# poll CI, auto-merge the moment it turns green\n"
" await 'gh run view --exit-status' --timeout 1800 --interval 30 \\\n\t--exec 'gh pr merge --auto --squash'\n\n"
"# connect to whichever replica answers first\n"
" await 'curl -sf primary.db/health' 'curl -sf replica.db/health' --any --exec connect_to_db\n\n"
"# waiting google (or your internet connection) to fail\n"
" await 'curl google.com' --fail\n\n"
"# waiting only google to fail (https://ec.haxx.se/usingcurl/usingcurl-returns)\n"
" await 'curl google.com' --status 7\n\n"
"# lazy version\n"
" await 'ls /tmp/redis.sock'; redis-cli -s /tmp/redis.sock\n\n"
"# daily checking if I am on french reviera. Just in case\n"
" await 'curl https://ipapi.co/json 2>/dev/null | jq .city | grep Nice' --interval 86400\n\n"
"# get pinged the moment your site goes down\n"
" await 'curl -sf https://myapp.com' --fail --forever --exec 'ntfy send \"site is down\"'\n\n"
"# ...as a systemd daemon that survives reboots\n"
" await 'curl -sf https://myapp.com' --fail --forever --exec 'ntfy send \"site is down\"' --service site-monitor\n\n"
"\nOPTIONS:\n"
" --help\t\t#print this help\n"
" --stdout -o\t\t#print stdout of commands\n"
" --no-stderr -E\t#suppress stderr output from commands\n"
" --watch -w\t\t#equivalent to -fVodE (fail, silent, stdout, diff, no-stderr)\n"
" --silent -V\t\t#do not print spinners and commands\n"
" --fail -f\t\t#waiting commands to fail\n"
" --status -s\t\t#expected status [default: 0]\n"
" --any -a\t\t#terminate if any of command return expected status\n"
" --change -c\t\t#waiting for stdout to change and ignore status codes\n"
" --diff -d\t\t#highlight differences between previous and current output (like watch -d)\n"
" --exec -e\t\t#run some shell command on success;\n"
" --interval -i\t\t#seconds between one round of commands [default: 0.2]\n"
" --timeout -T\t\t#seconds to wait before giving up [default: 0 (no timeout)]\n"
" --cmd-timeout -t\t#seconds per command before killing it (wraps with timeout(1))\n"
" --retry -r\t\t#max number of attempts before giving up [default: 0 (unlimited)]\n"
" --forever -F\t\t#do not exit ever\n"
" --name -n\t\t#label for the next command (shown in spinner, usable as \\name in --exec)\n"
" --json -j\t\t#output results as JSON on exit\n"
" --lap -l\t\t#show last run duration per command in spinner\n"
" --service -S\t\t#create systemd user service with same parameters and activate it\n"
" --version -v\t\t#print the version of await\n"
" --autocompletions\t#detect installed shells and auto-install completions for all of them\n"
" --autocomplete-fish\t#output fish shell autocomplete script\n"
" --autocomplete-bash\t#output bash shell autocomplete script\n"
" --autocomplete-zsh\t#output zsh shell autocomplete script\n"
"\n\nNOTES:\n"
"# \\1, \\2 ... \\n - will be subtituted with n-th command stdout\n"
"# you can use stdout substitution in --exec and in commands itself:\n"
" await 'echo -n 10' 'echo -n $RANDOM' 'expr \\1 + \\2' --exec 'echo \\3' --forever --silent\n"
// "# waiting for pup's author new blog post\n"
// " await 'mv /tmp/eric.new /tmp/eric.old &>/dev/null; http \"https://ericchiang.github.io/\" | pup \"a attr{href}\" > /tmp/eric.new; diff /tmp/eric.new /tmp/eric.old' --fail --exec 'ntfy send \"new article $1\"'\n\n"
));
exit(0);
}
volatile sig_atomic_t stop = 0;
// void handle_sigint(int sig) {
// stop = 1;
// }
int looks_like_bare_url(const char *s) {
if (strncmp(s, "http://", 7) != 0 && strncmp(s, "https://", 8) != 0)
return 0;
// If it contains shell metacharacters or whitespace, the user already
// wrapped it in a real command (e.g. "curl http://x | grep y").
return strpbrk(s, " \t|&;<>()$`\\\"'") == NULL;
}
void parse_args(int argc, char *argv[]) {
int getopt;
char *names[100] = {NULL};
int names_count = 0;
args.args = malloc(1000);
while (1) {
static struct option long_options[] = {
{"stdout", no_argument, 0, 'o'},
{"silent", no_argument, 0, 'V'},
{"any", no_argument, 0, 'a'},
{"fail", no_argument, 0, 'f'},
{"forever", no_argument, 0, 'F'},
{"change", no_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"diff", no_argument, 0, 'd'},
{"service", required_argument, 0, 'S'},
{"status", required_argument, 0, 's'},
{"exec", required_argument, 0, 'e'},
{"interval", required_argument, 0, 'i'},
{"timeout", required_argument, 0, 'T'},
{"cmd-timeout", required_argument, 0, 't'},
{"retry", required_argument, 0, 'r'},
{"no-stderr", no_argument, 0, 'E'},
{"watch", no_argument, 0, 'w'},
{"name", required_argument, 0, 'n'},
{"json", no_argument, 0, 'j'},
{"lap", no_argument, 0, 'l'},
{"autocompletions", no_argument, 0, 0},
{"autocomplete-fish", no_argument, 0, 0},
{"autocomplete-bash", no_argument, 0, 0},
{"autocomplete-zsh", no_argument, 0, 0},
{0, 0, 0, 0}
};
int option_index = 0;
getopt = getopt_long(argc, argv, "oVafFchdvS:s:e:i:T:t:r:Ewn:jl", long_options, &option_index);
if (getopt == -1)
break;
if (getopt != 'S') {
strcat(args.args, "--");
for (int i =0; i<18; i++) {
if (long_options[i].val == getopt)
strcat(args.args, long_options[i].name);
}
if (optarg) {
strcat(args.args, " \"");
strcat(args.args, optarg);
strcat(args.args, "\"");
}
strcat(args.args, " ");
}
switch (getopt) {
case 0:
if (strcmp(long_options[option_index].name, "autocompletions") == 0) {
install_autocompletions();
exit(0);
} else if (strcmp(long_options[option_index].name, "autocomplete-fish") == 0) {
print_autocomplete_fish();
exit(0);
} else if (strcmp(long_options[option_index].name, "autocomplete-bash") == 0) {
print_autocomplete_bash();
exit(0);
} else if (strcmp(long_options[option_index].name, "autocomplete-zsh") == 0) {
print_autocomplete_zsh();
exit(0);
}
break;
case 'V': args.silent = 1; break;
case 'o': args.stdout = 1; break;
case 'e': args.exec=optarg; break;
case 's': args.expectedStatus=atoi(optarg); break;
case 'f': args.fail = 1; break;
case 'a': args.any = 1; break;
case 'F': args.forever = 1; break;
case 'c': args.change = 1; break;
case 'S': args.service = optarg; break;
case 'i': args.interval = atoi(optarg) * 1000; break;
case 'T': args.timeout = atoi(optarg) * 1000; break;
case 't': args.cmd_timeout = atoi(optarg); break;
case 'r': args.retry = atoi(optarg); break;
case 'd': args.diff = 1; break;
case 'v': printf("2.7.0\n"); exit(0); break;
case 'h': case '?': help(); break;
case 1:
if (strcmp(long_options[option_index].name, "autocomplete-fish") == 0) {
print_autocomplete_fish();
exit(0);
}
break;
case 2:
if (strcmp(long_options[option_index].name, "autocomplete-bash") == 0) {
print_autocomplete_bash();
exit(0);
}
break;
case 3:
if (strcmp(long_options[option_index].name, "autocomplete-zsh") == 0) {
print_autocomplete_zsh();
exit(0);
}
break;
case 'E': args.no_stderr = 1; break;
case 'w':
args.fail = 1;
args.silent = 1;
args.stdout = 1;
args.diff = 1;
args.no_stderr = 1;
break;
case 'n': names[names_count++] = optarg; break;
case 'j': args.json = 1; break;
case 'l': args.lap = 1; break;
}
}
if (!args.exec && args.daemonize)
printf("NOTICE: --daemon is kinda meaningless without --exec 'command'");
c[0].command = "";
while (optind < argc) {
if (looks_like_bare_url(argv[optind])) {
fprintf(stderr,
"await: '%s' looks like a URL, not a command.\n"
" await runs shell commands, not URLs directly. Try:\n"
" await 'curl -sf %s'\n",
argv[optind], argv[optind]);
}
strcat(args.args, " \"");
strcat(args.args, argv[optind]);
strcat(args.args, "\"");
c[++args.nCommands].command = argv[optind];
c[args.nCommands].name = (args.nCommands <= names_count && names[args.nCommands-1]) ? names[args.nCommands-1] : argv[optind];
optind++;
}
if (args.nCommands == 0) help();
}
int service() {
FILE * fp;
const char *home;
if ((home = getenv("HOME")) == NULL)
home = getpwuid(getuid())->pw_dir;
char* service = replace("NAME", args.service, "NAME.service");
char* f = replace("SERVICE", service, replace("HOME", home, "HOME/.config/systemd/user/SERVICE"));
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
char binary[BUFSIZ];
readlink("/proc/self/exe", binary, BUFSIZ);
fp = fopen(f, "w");
fprintf(fp,
"[Unit]\n"\
"Description=await %s\n"\
"After=network-online.target\n"\
"Wants=network-online.target\n"\
"StartLimitIntervalSec=0\n"\
"[Service]\n"\
"WorkingDirectory=%s\n"\
"ExecStart=%s\n"\
"Restart=always\n"\
"[Install]\n"\
"WantedBy=default.target\n"
, args.args, cwd, replace("ARGS", args.args, replace("BINARY", binary, "BINARY ARGS")));
fclose(fp);
system(replace("SERVICE", service, "systemctl --user daemon-reload; systemctl cat --user SERVICE; systemctl enable --user SERVICE; systemctl restart --user SERVICE; journalctl --user --follow --unit SERVICE"));
return 0;
}
void *shell(void * arg) {
COMMAND *c = (COMMAND*)arg;
c->out = malloc(CHUNK_SIZE * sizeof(char));
strcpy(c->out, "");
c->previousOut = malloc(CHUNK_SIZE * sizeof(char));
c->diffOut = NULL;
char buf[BUF_SIZE];
while (1) {
c->outPos = 0;
strcpy(c->out, "");
int pipefd[2];
pipe(pipefd);
c->start_time = current_time_ms();
pid_t child_pid = fork();
if (child_pid == 0) {
// Child process
close(pipefd[0]); // Close read end
dup2(pipefd[1], STDOUT_FILENO); // Redirect stdout to pipe
if (args.no_stderr) {
// Redirect stderr to /dev/null
int devnull = open("/dev/null", O_WRONLY);
dup2(devnull, STDERR_FILENO);
close(devnull);
}
close(pipefd[1]);
if (args.cmd_timeout > 0)
alarm(args.cmd_timeout);
char *cmd = replace_placeholders(c->command);
execl("/bin/sh", "sh", "-c", cmd, NULL);
exit(1);
} else {
// Parent process
close(pipefd[1]); // Close write end
FILE *fp = fdopen(pipefd[0], "r");
c->pid = child_pid;
while (fgets(buf, BUF_SIZE, fp) !=NULL) {
c->outPos += BUF_SIZE;
if (c->outPos % CHUNK_SIZE > CHUNK_SIZE*0.8) {
c->out = realloc(c->out, c->outPos + c->outPos % CHUNK_SIZE + CHUNK_SIZE);
c->previousOut = realloc(c->previousOut, c->outPos + c->outPos % CHUNK_SIZE + CHUNK_SIZE);
}
sprintf(c->out, "%s%s", c->out, buf);
}
if (!c->spinner || c->spinner == 0) c->spinner = sizeof(spinner)/sizeof(spinner[0]);
c->spinner--;
fclose(fp);
int status;
waitpid(c->pid, &status, 0);
c->status = WIFSIGNALED(status) ? 128 + WTERMSIG(status) : WEXITSTATUS(status);
if (c->status == 127 && !c->warned127) {
c->warned127 = 1;
fprintf(stderr, "\nawait: '%s' exited with 127 (command not found).\n"
" Check for a typo, or that it's installed and on PATH.\n",
c->command);
}
c->prev_duration_ms = c->last_duration_ms;
c->last_duration_ms = current_time_ms() - c->start_time;
}
if (strcmp(c->previousOut, "first run") != 0) {
c->change = strcmp(c->previousOut,c->out) != 0;
// Compute differences if diff mode is enabled
if (args.diff && c->change) {
if (c->diffOut) free(c->diffOut);
c->diffOut = highlight_differences(c->previousOut, c->out);
}
}
strcpy(c->previousOut, c->out);
if (args.daemonize) syslog(LOG_NOTICE, "%d %s", c->status, c->command);
if (stop) {
break;
}
msleep(args.interval);
}
return NULL;
}
int main(int argc, char *argv[]) {
// Ensure the program is in the foreground and can catch SIGINT when run from a bash script
if (tcgetpgrp(STDIN_FILENO) == getpgrp()) {
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
}
pthread_t exec_thread;
// struct sigaction sa;
// sa.sa_handler = handle_sigint;
// sigemptyset(&sa.sa_mask);
// sa.sa_flags = 0;
// sigaction(SIGINT, &sa, NULL);
parse_args(argc, argv);
if (args.service) return service();
// Ensure the program does not ignore signals when running in a script
signal(SIGINT, SIG_DFL);
if (args.daemonize) daemonize();
FILE *fp;
for(int i = 0; i <= args.nCommands; i++) {
c[i].status = -1;
pthread_create(&c[i].thread, NULL, shell, &c[i]);
}
int not_done = 0;
int rounds = 0;
// TODO: make a clear screen option
// fprintf(stdout, "\033[2J\033[H");
// fprintf(stderr, "\033[2J\033[H");
// fflush(stdout);
// fflush(stderr);
static int first_output = 1;
static char *last_display = NULL;
static char *last_silent_output = NULL;
// Initialize start time for timeout
if (args.timeout > 0) {
args.start_time = current_time_ms();
}
while (1) {
not_done = 0;
if (!args.silent) {
// Clear previous output by going to beginning of last display
if (last_display) {
int lines = 0;
for (char *p = last_display; *p; p++) {
if (*p == '\n') lines++;
}
if (lines > 0) {
fprintf(stderr, "\033[%dA\033[J", lines);
} else {
fprintf(stderr, "\r\033[K");
}
free(last_display);
}
// Build the entire display string first
char *display = malloc(10000); // Large buffer for display
strcpy(display, "");
for(int i = 1; i <= args.nCommands; i++) {
int color = c[i].status == -1 ? 7 : c[i].status == args.expectedStatus ? 2 : 1;
// Add status line
char status_line[1000];
if (args.lap && c[i].last_duration_ms > 0) {
const char *time_color = "\033[2m";
if (c[i].prev_duration_ms > 0) {
if (c[i].last_duration_ms < c[i].prev_duration_ms) time_color = "\033[32m";
else if (c[i].last_duration_ms > c[i].prev_duration_ms) time_color = "\033[31m";
}
sprintf(status_line, "%s%.2fs\033[0m \033[0;3%dm%s\033[0m %s\n", time_color, c[i].last_duration_ms / 1000.0, color, spinner[c[i].spinner], c[i].name ? c[i].name : c[i].command);
}
else if (args.lap)
sprintf(status_line, " \033[0;3%dm%s\033[0m %s\n", color, spinner[c[i].spinner], c[i].name ? c[i].name : c[i].command);
else
sprintf(status_line, "\033[0;3%dm%s\033[0m %s\n", color, spinner[c[i].spinner], c[i].name ? c[i].name : c[i].command);
strcat(display, status_line);
// Add output if available, or previous output if command has run before
if (args.stdout) {
char *output_to_show = NULL;
// If diff mode is enabled and we have diff output, use that
if (args.diff && c[i].diffOut) {
output_to_show = malloc(strlen(c[i].diffOut) + 1);
strcpy(output_to_show, c[i].diffOut);
} else if (c[i].out && strlen(c[i].out) > 0) {
// Use current output
output_to_show = malloc(strlen(c[i].out) + 1);
strcpy(output_to_show, c[i].out);
} else if (c[i].previousOut && strlen(c[i].previousOut) > 0 && strcmp(c[i].previousOut, "first run") != 0) {
// Use previous output if current is empty but we have previous
output_to_show = malloc(strlen(c[i].previousOut) + 1);
strcpy(output_to_show, c[i].previousOut);
}
if (output_to_show) {
int len = strlen(output_to_show);
if (len > 0 && output_to_show[len - 1] == '\n') {
output_to_show[len - 1] = '\0';
}
strcat(display, output_to_show);
strcat(display, "\n");
free(output_to_show);
}
}
if (args.change) not_done = !c[i].change;
else not_done += c[i].status==-1 || (args.fail && c[i].status == 0) || (!args.fail && c[i].status != args.expectedStatus);
}
// Print the entire display at once
fprintf(stderr, "%s", display);
fflush(stderr);
// Save this display for next iteration
last_display = display;
} else {
// Silent mode - handle stdout only, similar to non-silent mode with clearing
if (args.stdout) {
// Clear previous silent output (but not on first run)
if (last_silent_output && !first_output) {
int lines = 0;
for (char *p = last_silent_output; *p; p++) {
if (*p == '\n') lines++;
}
if (lines > 0) {
printf("\033[%dA\033[J", lines);
} else {
printf("\r\033[K");
}
free(last_silent_output);
} else if (last_silent_output) {
free(last_silent_output);
}
// Build silent output display
char *silent_display = malloc(10000);
strcpy(silent_display, "");
int has_output = 0;
for(int i = 1; i <= args.nCommands; i++) {
char *output_to_show = NULL;
// If diff mode is enabled and we have diff output, use that
if (args.diff && c[i].diffOut) {
output_to_show = malloc(strlen(c[i].diffOut) + 1);
strcpy(output_to_show, c[i].diffOut);
} else if (c[i].out && strlen(c[i].out) > 0) {
// Use current output
output_to_show = malloc(strlen(c[i].out) + 1);
strcpy(output_to_show, c[i].out);
} else if (c[i].previousOut && strlen(c[i].previousOut) > 0 && strcmp(c[i].previousOut, "first run") != 0) {
// Use previous output if current is empty but we have previous
output_to_show = malloc(strlen(c[i].previousOut) + 1);
strcpy(output_to_show, c[i].previousOut);
}
if (output_to_show) {
int len = strlen(output_to_show);
if (len > 0 && output_to_show[len - 1] == '\n') {
output_to_show[len - 1] = '\0';
}
if (has_output) {
strcat(silent_display, "\n");
}
strcat(silent_display, output_to_show);
has_output = 1;
free(output_to_show);
}
if (args.change) not_done = !c[i].change;
else not_done += c[i].status==-1 || (args.fail && c[i].status == 0) || (!args.fail && c[i].status != args.expectedStatus);
}
// Print the silent display
if (has_output) {
if (first_output) {
printf("%s", silent_display);
first_output = 0;
} else {
printf("\r%s", silent_display);