Debian Codegraph @

src/cliargs.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2016 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // CLI argument stuff
6 #include "cliargs.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "config.h"
10
11
12 // constants
13 const char cliargs_error[] = "Error in CLI arguments: ";
14
15
16 // variables
17 static int arguments_left; // number of CLI arguments left
18 static const char **next_argument; // next argument pointer
19
20
21 // return pointer to next command line argument (NULL if no more)
22 const char *cliargs_get_next(void)
23 {
24 if (arguments_left == 0)
25 return NULL;
26
27 --arguments_left;
28 return *next_argument++;
29 }
30
31
32 // parse command line switches
33 void cliargs_handle_options(char (*fn_short)(const char *), const char *(*fn_long)(const char *))
34 {
35 const char *problem_string,
36 *argument;
37 char problem_char;
38
39 for (;;) {
40 // if there are no more arguments, return immediately
41 if (arguments_left == 0)
42 return;
43
44 // if next argument is not an option, return immediately
45 if ((**next_argument) != '-')
46 return;
47
48 // officially fetch argument. We already know the
49 // first character is a '-', so advance pointer.
50 argument = cliargs_get_next() + 1;
51 // Check for "--"
52 if (*argument == '-') {
53 // long argument
54 if (argument[1] == '\0')
55 return; // when encountering "--", return
56
57 problem_string = fn_long(argument + 1);
58 if (problem_string) {
59 fprintf(stderr, "%sUnknown option (--%s).\n", cliargs_error, problem_string);
60 exit(EXIT_FAILURE);
61 }
62 } else {
63 problem_char = fn_short(argument);
64 if (problem_char) {
65 fprintf(stderr, "%sUnknown switch (-%c).\n", cliargs_error, problem_char);
66 exit(EXIT_FAILURE);
67 }
68 }
69 }
70 }
71
72
73 // return next arg. If there is none, complain and exit
74 const char *cliargs_safe_get_next(const char name[])
75 {
76 const char *string;
77
78 string = cliargs_get_next();
79 if (string)
80 return string;
81
82 fprintf(stderr, "%sMissing %s.\n", cliargs_error, name);
83 exit(EXIT_FAILURE);
84 }
85
86
87 // init command line handling stuff
88 const char *cliargs_init(int argc, const char *argv[])
89 {
90 arguments_left = argc;
91 next_argument = argv;
92 return cliargs_get_next();
93 }
94
95
96 // return unhandled (non-option) arguments. Complains if none.
97 void cliargs_get_rest(int *argc, const char ***argv, const char error[])
98 {
99 *argc = arguments_left;
100 *argv = next_argument;
101 if (error && (arguments_left == 0)) {
102 fprintf(stderr, "%s%s.\n", cliargs_error, error);
103 exit(EXIT_FAILURE);
104 }
105 }