Browse Source

Adds argument parsing to asmsh + comments

Yann Weber 1 year ago
parent
commit
f6d6333e2a
3 changed files with 85 additions and 4 deletions
  1. 60
    2
      src/asmsh.c
  2. 2
    0
      src/shell.c
  3. 23
    2
      src/shell.h

+ 60
- 2
src/asmsh.c View File

@@ -15,6 +15,7 @@
15 15
 */
16 16
 #include "config.h"
17 17
 #include <stdio.h>
18
+#include <getopt.h>
18 19
 #include <readline/readline.h>
19 20
 #include <readline/history.h>
20 21
 
@@ -23,16 +24,73 @@
23 24
 #include "completion.h"
24 25
 #include "history.h"
25 26
 
27
+static struct option long_opts[] = {
28
+	{"debug", no_argument, 0, 'd'},
29
+	{"help", no_argument, 0, 'h'},
30
+	{0,0,0,0}
31
+};
32
+
33
+static const char *help_opts[] = {
34
+	"Enable debug output",
35
+	"Display this help",
36
+	NULL
37
+};
38
+
39
+void help(const char *argv0)
40
+{
41
+	printf("Usage : %s [OPTIONS] [FILENAME]\n", argv0);
42
+	for(size_t i=0; long_opts[i].name; i++)
43
+	{
44
+		const struct option *opt = &long_opts[i];
45
+		const char *help = help_opts[i];
46
+		printf("\t");
47
+		if(opt->val)
48
+		{
49
+			printf("-%c, ", opt->val);
50
+		}
51
+		printf("--%s%s\n",
52
+				opt->name,
53
+				opt->has_arg==0?"":(opt->has_arg==1?"arg":"[arg]"));
54
+		printf("\t\t%s\n", help);
55
+	}
56
+
57
+}
58
+
26 59
 int main(int argc, char *argv[], char *envp[])
27 60
 {
61
+	asmsh_loglevel_t loglevel = ASMSH_INFO;
62
+	int opt;
63
+
64
+	while((opt = getopt_long(argc, argv, "dh", long_opts, NULL)) != -1)
65
+	{
66
+		switch(opt)
67
+		{
68
+			case 'd':
69
+				loglevel = ASMSH_DEBUG;
70
+				break;
71
+			case 'h':
72
+				help(argv[0]);
73
+				return 0;
74
+			case '?':
75
+				help(argv[0]);
76
+				return 0;
77
+		}
78
+	}
79
+	if(optind < argc)
80
+	{
81
+		dprintf(2, "File execution not supported yet\n");
82
+		exit(1);
83
+	}
84
+
85
+
28 86
 	asmsh_logger_t *logger;
29 87
 	asmsh_t sh;
30
-	
88
+
31 89
 	int ret = 0;
32 90
 	char prompt[64];
33 91
 	bzero(prompt, sizeof(prompt));
34 92
 
35
-	logger = asmsh_logger_new(ASMSH_INFO);
93
+	logger = asmsh_logger_new(loglevel);
36 94
 	asmsh_logger_setup(logger);
37 95
 
38 96
 	if(asmsh_init(&sh, NULL))

+ 2
- 0
src/shell.c View File

@@ -111,6 +111,8 @@ int asmsh_exec(asmsh_t *sh, const char *_cmd)
111 111
 		cmd = m_cmd;
112 112
 	}
113 113
 
114
+	asmsh_log_debug("%s", cmd);
115
+
114 116
 	int ret;
115 117
 	//lstrip whitespace
116 118
 	for(; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);

+ 23
- 2
src/shell.h View File

@@ -29,28 +29,49 @@ typedef struct asmsh_s asmsh_t;
29 29
 
30 30
 struct asmsh_s
31 31
 {
32
+	///! Compilation context
32 33
 	asmsh_asmc_ctx_t *cctx;
34
+	///! Child process environment
33 35
 	asmsh_env_t *env;
34 36
 
37
+	/** Stores the child process exec path
38
+	 * @warning Can be NULL if a temporary file is used
39
+	 * @deprecated
40
+	 */
35 41
 	char *child_path;
42
+	/** May store the last executed instruction ? not sure if used or not */
36 43
 	char *last_instr;
44
+	/** Last executed bytecode ? */
37 45
 	asmsh_bytecode_t last_bcode;
38 46
 
39 47
 };
40 48
 
49
+/** Initialize a shell environment
50
+ * @param asmsh_t* Pointer on the shell environment
51
+ * @param const char* The child process executable path (NULL is recommended
52
+ * in order to use the embed executable)
53
+ * @return 0 if no error else -1 and set errno
54
+ */
41 55
 int asmsh_init(asmsh_t *sh, const char *child_path);
56
+
57
+/** Free all ressources holded by a shell environment
58
+ * @param asmsh_t* Pointer on the shell environment
59
+ */
42 60
 void asmsh_cleanup(asmsh_t *sh);
43 61
 
44
-/** @return <0 on error 0 on ok 1 on exit */
62
+/** 
63
+ * @return <0 on error 0 on ok 1 on exit */
45 64
 int asmsh_exec(asmsh_t *sh, const char *cmd);
46 65
 
47
-/** Parse all symbols found in given command
66
+/** Replace all labels found in given command by their relative offset
48 67
  * @param asmsh_t* The shell
49 68
  * @param char The prefix character before a symbol name
50 69
  * @param const char* The string we want to parse
51 70
  * @param char* A pointer on a buffer large enough to store the parsed string
52 71
  * @param size_t The buffer size
53 72
  * @return 0 on error else return the needed buffer size
73
+ * @todo move in asm_env.*
74
+ * @todo Enhance !!! Add support for relative or absolute address replacement
54 75
  */
55 76
 size_t asmsh_parse_labels(asmsh_t *sh, char preffix, const char *cmd,
56 77
 		char *buf, size_t buf_sz);

Loading…
Cancel
Save