Commit
Author: Kevin Schoon [me@kevinschoon.com]
Hash: 692b5b172a3317795c136ab336af13c520aa1be6
Timestamp: Thu, 06 Apr 2023 00:38:03 +0000 (1 year ago)

+108 -0 +/-3 browse
rewrite in c (WIP)
1diff --git a/.gitignore b/.gitignore
2index 4b0ab6f..44bc5a2 100644
3--- a/.gitignore
4+++ b/.gitignore
5 @@ -1,3 +1,5 @@
6 pkg
7 src
8+ bin
9+ .ccls-cache
10 *.tar.zst
11 diff --git a/Makefile b/Makefile
12new file mode 100644
13index 0000000..4ee1724
14--- /dev/null
15+++ b/Makefile
16 @@ -0,0 +1,5 @@
17+ default: bin
18+ gcc main.c -o bin/main
19+
20+ bin:
21+ mkdir -p bin
22 diff --git a/main.c b/main.c
23new file mode 100644
24index 0000000..3c552b0
25--- /dev/null
26+++ b/main.c
27 @@ -0,0 +1,101 @@
28+ #include <regex.h>
29+ #include <stdio.h>
30+ #include <stdlib.h>
31+ #include <string.h>
32+
33+ #define _MONITOR_REGEXP "unix:\\([-/.[:alnum:]]*\\)"
34+ #define _SPICE_REGEXP ".*addr=\\([-/.[:alnum:]]*\\)"
35+
36+ char *_get_arg_by_name(int argc, char *argv[], char *name) {
37+ char *res;
38+ int i;
39+ for (i = 0; i < argc; i++) {
40+ if (strcmp(argv[i], name) == 0 && (i < argc)) {
41+ res = malloc(strlen(argv[i + 1]));
42+ strcpy(res, argv[i + 1]);
43+ return res;
44+ }
45+ }
46+ return NULL;
47+ };
48+
49+ char *_get_arg_with_regexp(int argc, char *argv[], char *name,
50+ char *expression) {
51+ char *arg_raw;
52+ arg_raw = _get_arg_by_name(argc, argv, name);
53+ if (arg_raw == NULL) {
54+ return NULL;
55+ }
56+
57+ regex_t regex;
58+ regmatch_t groups[2];
59+
60+ if (regcomp(&regex, expression, 0) != 0) {
61+ fprintf(stderr, "regexp failed to compile\n");
62+ exit(1);
63+ }
64+
65+ char *result = malloc(strlen(arg_raw) + 1);
66+
67+ if (regexec(&regex, arg_raw, 2, groups, 0) == 0) {
68+ if (groups[1].rm_so == (size_t)-1) {
69+ fprintf(stderr, "cannot match against -montor flag:'%s'\n", arg_raw);
70+ exit(1);
71+ }
72+ strcpy(result, arg_raw);
73+ result[groups[1].rm_eo] = 0;
74+ result = result + groups[1].rm_so;
75+ } else {
76+ fprintf(stderr, "cannot match against -montor flag:'%s'\n", arg_raw);
77+ exit(1);
78+ }
79+
80+ regfree(&regex);
81+ return result;
82+ }
83+
84+ int main(int argc, char *argv[]) {
85+ char *name;
86+ name = _get_arg_by_name(argc, argv, "-name");
87+ int result;
88+ if (name == NULL) {
89+ printf("-name flag was not specified\n");
90+ return EXIT_FAILURE;
91+ }
92+ printf("NAME: %s\n", name);
93+ char *monitor_path;
94+ monitor_path = _get_arg_with_regexp(argc, argv, "-monitor", _MONITOR_REGEXP);
95+ if (monitor_path == NULL) {
96+ printf("-monitor flag was not specified");
97+ return EXIT_FAILURE;
98+ }
99+ printf("MONITOR: %s\n", monitor_path);
100+ char *spice_path;
101+ spice_path = _get_arg_with_regexp(argc, argv, "-spice", _SPICE_REGEXP);
102+ if (spice_path != NULL) {
103+ printf("SPICE: %s\n", spice_path);
104+ }
105+
106+ /* CATCH SIGNAL TODO */
107+ /* POPEN TODO */
108+
109+ /*
110+ FILE *fp;
111+ int status;
112+ char buf[128];
113+ fp = popen("ls", "r");
114+ if (fp == NULL) {
115+ puts("failed to open process");
116+ return EXIT_FAILURE;
117+ }
118+ while (fgets(buf, 128, fp) != NULL) {
119+ printf("%s", buf);
120+ }
121+ status = pclose(fp);
122+ if (status != 0) {
123+ printf("non-zero exit code");
124+ return status;
125+ }
126+ return EXIT_SUCCESS;
127+ */
128+ }