xref: /freebsd/contrib/bc/src/args.c (revision e17f5b1d)
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 <status.h>
45 #include <vector.h>
46 #include <read.h>
47 #include <vm.h>
48 #include <args.h>
49 #include <opt.h>
50 
51 static const BcOptLong bc_args_lopt[] = {
52 
53 	{ "expression", BC_OPT_REQUIRED, 'e' },
54 	{ "file", BC_OPT_REQUIRED, 'f' },
55 	{ "help", BC_OPT_NONE, 'h' },
56 	{ "interactive", BC_OPT_NONE, 'i' },
57 	{ "no-prompt", BC_OPT_NONE, 'P' },
58 #if BC_ENABLED
59 	{ "global-stacks", BC_OPT_BC_ONLY, 'g' },
60 	{ "mathlib", BC_OPT_BC_ONLY, 'l' },
61 	{ "quiet", BC_OPT_BC_ONLY, 'q' },
62 	{ "standard", BC_OPT_BC_ONLY, 's' },
63 	{ "warn", BC_OPT_BC_ONLY, 'w' },
64 #endif // BC_ENABLED
65 	{ "version", BC_OPT_NONE, 'v' },
66 	{ "version", BC_OPT_NONE, 'V' },
67 #if DC_ENABLED
68 	{ "extended-register", BC_OPT_DC_ONLY, 'x' },
69 #endif // DC_ENABLED
70 	{ NULL, 0, 0 },
71 
72 };
73 
74 static void bc_args_exprs(const char *str) {
75 	BC_SIG_ASSERT_LOCKED;
76 	if (vm.exprs.v == NULL) bc_vec_init(&vm.exprs, sizeof(uchar), NULL);
77 	bc_vec_concat(&vm.exprs, str);
78 	bc_vec_concat(&vm.exprs, "\n");
79 }
80 
81 static void bc_args_file(const char *file) {
82 
83 	char *buf;
84 
85 	BC_SIG_ASSERT_LOCKED;
86 
87 	vm.file = file;
88 
89 	bc_read_file(file, &buf);
90 	bc_args_exprs(buf);
91 	free(buf);
92 }
93 
94 void bc_args(int argc, char *argv[]) {
95 
96 	int c;
97 	size_t i;
98 	bool do_exit = false, version = false;
99 	BcOpt opts;
100 
101 	BC_SIG_ASSERT_LOCKED;
102 
103 	bc_opt_init(&opts, argv);
104 
105 	while ((c = bc_opt_parse(&opts, bc_args_lopt)) != -1) {
106 
107 		switch (c) {
108 
109 			case 'e':
110 			{
111 				bc_args_exprs(opts.optarg);
112 				break;
113 			}
114 
115 			case 'f':
116 			{
117 				bc_args_file(opts.optarg);
118 				break;
119 			}
120 
121 			case 'h':
122 			{
123 				bc_vm_info(vm.help);
124 				do_exit = true;
125 				break;
126 			}
127 
128 			case 'i':
129 			{
130 				vm.flags |= BC_FLAG_I;
131 				break;
132 			}
133 
134 			case 'P':
135 			{
136 				vm.flags |= BC_FLAG_P;
137 				break;
138 			}
139 
140 #if BC_ENABLED
141 			case 'g':
142 			{
143 				assert(BC_IS_BC);
144 				vm.flags |= BC_FLAG_G;
145 				break;
146 			}
147 
148 			case 'l':
149 			{
150 				assert(BC_IS_BC);
151 				vm.flags |= BC_FLAG_L;
152 				break;
153 			}
154 
155 			case 'q':
156 			{
157 				assert(BC_IS_BC);
158 				vm.flags |= BC_FLAG_Q;
159 				break;
160 			}
161 
162 			case 's':
163 			{
164 				assert(BC_IS_BC);
165 				vm.flags |= BC_FLAG_S;
166 				break;
167 			}
168 
169 			case 'w':
170 			{
171 				assert(BC_IS_BC);
172 				vm.flags |= BC_FLAG_W;
173 				break;
174 			}
175 #endif // BC_ENABLED
176 
177 			case 'V':
178 			case 'v':
179 			{
180 				do_exit = version = true;
181 				break;
182 			}
183 
184 #if DC_ENABLED
185 			case 'x':
186 			{
187 				assert(BC_IS_DC);
188 				vm.flags |= DC_FLAG_X;
189 				break;
190 			}
191 #endif // DC_ENABLED
192 
193 #ifndef NDEBUG
194 			// We shouldn't get here because bc_opt_error()/bc_vm_error() should
195 			// longjmp() out.
196 			case '?':
197 			case ':':
198 			default:
199 			{
200 				abort();
201 			}
202 #endif // NDEBUG
203 		}
204 	}
205 
206 	if (version) bc_vm_info(NULL);
207 	if (do_exit) exit((int) vm.status);
208 	if (vm.exprs.len > 1 || BC_IS_DC) vm.flags |= BC_FLAG_Q;
209 
210 	if (opts.optind < (size_t) argc)
211 		bc_vec_init(&vm.files, sizeof(char*), NULL);
212 
213 	for (i = opts.optind; i < (size_t) argc; ++i)
214 		bc_vec_push(&vm.files, argv + i);
215 }
216