xref: /freebsd/contrib/bc/src/args.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * *****************************************************************************
31  *
32  * Code for processing command-line arguments.
33  *
34  */
35 
36 #include <assert.h>
37 #include <ctype.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include <unistd.h>
43 
44 #include <vector.h>
45 #include <read.h>
46 #include <args.h>
47 #include <opt.h>
48 
49 static const BcOptLong bc_args_lopt[] = {
50 
51 	{ "expression", BC_OPT_REQUIRED, 'e' },
52 	{ "file", BC_OPT_REQUIRED, 'f' },
53 	{ "help", BC_OPT_NONE, 'h' },
54 	{ "interactive", BC_OPT_NONE, 'i' },
55 	{ "no-prompt", BC_OPT_NONE, 'P' },
56 #if BC_ENABLED
57 	{ "global-stacks", BC_OPT_BC_ONLY, 'g' },
58 	{ "mathlib", BC_OPT_BC_ONLY, 'l' },
59 	{ "quiet", BC_OPT_BC_ONLY, 'q' },
60 	{ "standard", BC_OPT_BC_ONLY, 's' },
61 	{ "warn", BC_OPT_BC_ONLY, 'w' },
62 #endif // BC_ENABLED
63 	{ "version", BC_OPT_NONE, 'v' },
64 	{ "version", BC_OPT_NONE, 'V' },
65 #if DC_ENABLED
66 	{ "extended-register", BC_OPT_DC_ONLY, 'x' },
67 #endif // DC_ENABLED
68 	{ NULL, 0, 0 },
69 
70 };
71 
72 static void bc_args_exprs(const char *str) {
73 	BC_SIG_ASSERT_LOCKED;
74 	if (vm.exprs.v == NULL) bc_vec_init(&vm.exprs, sizeof(uchar), NULL);
75 	bc_vec_concat(&vm.exprs, str);
76 	bc_vec_concat(&vm.exprs, "\n");
77 }
78 
79 static void bc_args_file(const char *file) {
80 
81 	char *buf;
82 
83 	BC_SIG_ASSERT_LOCKED;
84 
85 	vm.file = file;
86 
87 	bc_read_file(file, &buf);
88 	bc_args_exprs(buf);
89 	free(buf);
90 }
91 
92 void bc_args(int argc, char *argv[]) {
93 
94 	int c;
95 	size_t i;
96 	bool do_exit = false, version = false;
97 	BcOpt opts;
98 
99 	BC_SIG_ASSERT_LOCKED;
100 
101 	bc_opt_init(&opts, argv);
102 
103 	while ((c = bc_opt_parse(&opts, bc_args_lopt)) != -1) {
104 
105 		switch (c) {
106 
107 			case 'e':
108 			{
109 				if (vm.no_exit_exprs)
110 					bc_vm_verr(BC_ERR_FATAL_OPTION, "-e (--expression)");
111 				bc_args_exprs(opts.optarg);
112 				break;
113 			}
114 
115 			case 'f':
116 			{
117 				if (!strcmp(opts.optarg, "-")) vm.no_exit_exprs = true;
118 				else {
119 					if (vm.no_exit_exprs)
120 						bc_vm_verr(BC_ERR_FATAL_OPTION, "-f (--file)");
121 					bc_args_file(opts.optarg);
122 				}
123 				break;
124 			}
125 
126 			case 'h':
127 			{
128 				bc_vm_info(vm.help);
129 				do_exit = true;
130 				break;
131 			}
132 
133 			case 'i':
134 			{
135 				vm.flags |= BC_FLAG_I;
136 				break;
137 			}
138 
139 			case 'P':
140 			{
141 				vm.flags |= BC_FLAG_P;
142 				break;
143 			}
144 
145 #if BC_ENABLED
146 			case 'g':
147 			{
148 				assert(BC_IS_BC);
149 				vm.flags |= BC_FLAG_G;
150 				break;
151 			}
152 
153 			case 'l':
154 			{
155 				assert(BC_IS_BC);
156 				vm.flags |= BC_FLAG_L;
157 				break;
158 			}
159 
160 			case 'q':
161 			{
162 				assert(BC_IS_BC);
163 				// Do nothing.
164 				break;
165 			}
166 
167 			case 's':
168 			{
169 				assert(BC_IS_BC);
170 				vm.flags |= BC_FLAG_S;
171 				break;
172 			}
173 
174 			case 'w':
175 			{
176 				assert(BC_IS_BC);
177 				vm.flags |= BC_FLAG_W;
178 				break;
179 			}
180 #endif // BC_ENABLED
181 
182 			case 'V':
183 			case 'v':
184 			{
185 				do_exit = version = true;
186 				break;
187 			}
188 
189 #if DC_ENABLED
190 			case 'x':
191 			{
192 				assert(BC_IS_DC);
193 				vm.flags |= DC_FLAG_X;
194 				break;
195 			}
196 #endif // DC_ENABLED
197 
198 #ifndef NDEBUG
199 			// We shouldn't get here because bc_opt_error()/bc_vm_error() should
200 			// longjmp() out.
201 			case '?':
202 			case ':':
203 			default:
204 			{
205 				abort();
206 			}
207 #endif // NDEBUG
208 		}
209 	}
210 
211 	if (version) bc_vm_info(NULL);
212 	if (do_exit) exit((int) vm.status);
213 
214 	if (opts.optind < (size_t) argc && vm.files.v == NULL)
215 		bc_vec_init(&vm.files, sizeof(char*), NULL);
216 
217 	for (i = opts.optind; i < (size_t) argc; ++i)
218 		bc_vec_push(&vm.files, argv + i);
219 }
220