GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/bc/bc.c Lines: 299 382 78.3 %
Date: 2017-11-13 Branches: 135 214 63.1 %

Line Branch Exec Source
1
#include <stdlib.h>
2
#include <string.h>
3
#define YYBYACC 1
4
#define YYMAJOR 1
5
#define YYMINOR 9
6
#define YYLEX yylex()
7
#define YYEMPTY -1
8
#define yyclearin (yychar=(YYEMPTY))
9
#define yyerrok (yyerrflag=0)
10
#define YYRECOVERING() (yyerrflag!=0)
11
#define YYPREFIX "yy"
12
#line 2 "/usr/src/usr.bin/bc/bc.y"
13
/*	$OpenBSD: bc.y,v 1.51 2017/07/02 23:19:07 deraadt Exp $	*/
14
15
/*
16
 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
17
 *
18
 * Permission to use, copy, modify, and distribute this software for any
19
 * purpose with or without fee is hereby granted, provided that the above
20
 * copyright notice and this permission notice appear in all copies.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
23
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
24
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
25
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
27
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29
 */
30
31
/*
32
 * This implementation of bc(1) uses concepts from the original 4.4
33
 * BSD bc(1). The code itself is a complete rewrite, based on the
34
 * Posix defined bc(1) grammar. Other differences include type safe
35
 * usage of pointers to build the tree of emitted code, typed yacc
36
 * rule values, dynamic allocation of all data structures and a
37
 * completely rewritten lexical analyzer using lex(1).
38
 *
39
 * Some effort has been made to make sure that the generated code is
40
 * the same as the code generated by the older version, to provide
41
 * easy regression testing.
42
 */
43
44
#include <sys/types.h>
45
#include <sys/wait.h>
46
47
#include <ctype.h>
48
#include <err.h>
49
#include <errno.h>
50
#include <histedit.h>
51
#include <limits.h>
52
#include <search.h>
53
#include <signal.h>
54
#include <stdarg.h>
55
#include <string.h>
56
#include <unistd.h>
57
58
#include "extern.h"
59
#include "pathnames.h"
60
61
#define END_NODE	((ssize_t) -1)
62
#define CONST_STRING	((ssize_t) -2)
63
#define ALLOC_STRING	((ssize_t) -3)
64
65
struct tree {
66
	ssize_t			index;
67
	union {
68
		char		*astr;
69
		const char	*cstr;
70
	} u;
71
};
72
73
int			yyparse(void);
74
int			yywrap(void);
75
76
int			fileindex;
77
int			sargc;
78
char			**sargv;
79
char			*filename;
80
char			*cmdexpr;
81
82
static void		grow(void);
83
static ssize_t		cs(const char *);
84
static ssize_t		as(const char *);
85
static ssize_t		node(ssize_t, ...);
86
static void		emit(ssize_t, int);
87
static void		emit_macro(int, ssize_t);
88
static void		free_tree(void);
89
static ssize_t		numnode(int);
90
static ssize_t		lookup(char *, size_t, char);
91
static ssize_t		letter_node(char *);
92
static ssize_t		array_node(char *);
93
static ssize_t		function_node(char *);
94
95
static void		add_par(ssize_t);
96
static void		add_local(ssize_t);
97
static void		warning(const char *);
98
static void		init(void);
99
static __dead void	usage(void);
100
static char		*escape(const char *);
101
102
static ssize_t		instr_sz = 0;
103
static struct tree	*instructions = NULL;
104
static ssize_t		current = 0;
105
static int		macro_char = '0';
106
static int		reset_macro_char = '0';
107
static int		nesting = 0;
108
static int		breakstack[16];
109
static int		breaksp = 0;
110
static ssize_t		prologue;
111
static ssize_t		epilogue;
112
static bool		st_has_continue;
113
static char		str_table[UCHAR_MAX][2];
114
static bool		do_fork = true;
115
static u_short		var_count;
116
static pid_t		dc;
117
118
extern char *__progname;
119
120
#define BREAKSTACK_SZ	(sizeof(breakstack)/sizeof(breakstack[0]))
121
122
/* These values are 4.4BSD bc compatible */
123
#define FUNC_CHAR	0x01
124
#define ARRAY_CHAR	0xa1
125
126
/* Skip '\0', [, \ and ] */
127
#define ENCODE(c)	((c) < '[' ? (c) : (c) + 3);
128
#define VAR_BASE	(256-4)
129
#define MAX_VARIABLES	(VAR_BASE * VAR_BASE)
130
131
#line 124 "/usr/src/usr.bin/bc/bc.y"
132
#ifndef YYSTYPE_DEFINED
133
#define YYSTYPE_DEFINED
134
typedef union {
135
	ssize_t		node;
136
	struct lvalue	lvalue;
137
	const char	*str;
138
	char		*astr;
139
} YYSTYPE;
140
#endif /* YYSTYPE_DEFINED */
141
#line 142 "bc.c"
142
#define COMMA 257
143
#define SEMICOLON 258
144
#define LPAR 259
145
#define RPAR 260
146
#define LBRACE 261
147
#define RBRACE 262
148
#define LBRACKET 263
149
#define RBRACKET 264
150
#define DOT 265
151
#define NEWLINE 266
152
#define LETTER 267
153
#define NUMBER 268
154
#define STRING 269
155
#define DEFINE 270
156
#define BREAK 271
157
#define QUIT 272
158
#define LENGTH 273
159
#define RETURN 274
160
#define FOR 275
161
#define IF 276
162
#define WHILE 277
163
#define SQRT 278
164
#define SCALE 279
165
#define IBASE 280
166
#define OBASE 281
167
#define AUTO 282
168
#define CONTINUE 283
169
#define ELSE 284
170
#define PRINT 285
171
#define BOOL_OR 286
172
#define BOOL_AND 287
173
#define BOOL_NOT 288
174
#define EQUALS 289
175
#define LESS_EQ 290
176
#define GREATER_EQ 291
177
#define UNEQUALS 292
178
3430
#define LESS 293
179
3430
#define GREATER 294
180
6860
#define ASSIGN_OP 295
181
3430
#define PLUS 296
182
3430
#define MINUS 297
183
#define MULTIPLY 298
184
3430
#define DIVIDE 299
185
#define REMAINDER 300
186
180
#define EXPONENT 301
187
90
#define UMINUS 302
188
90
#define INCR 303
189
#define DECR 304
190
90
#define YYERRCODE 256
191
const short yylhs[] =
192
	{                                        -1,
193
    0,    0,    7,    7,    7,    7,   16,   16,   16,   16,
194
   18,   18,   18,   18,   18,   18,   11,   11,   17,   17,
195
   17,   17,   17,   17,   17,   17,   17,   17,   17,   17,
196
   17,    3,   19,    5,    6,   21,   21,   20,   20,   23,
197
   23,   23,   23,   22,   22,   22,   24,   24,   24,   24,
198
    8,    8,    2,    2,    2,   10,   10,   14,   14,   14,
199
   14,   14,   14,   14,   15,   15,   15,    9,    9,    4,
200
    4,    4,    4,    4,    4,    4,    4,    4,    4,    4,
201
    4,    4,    4,    4,    4,    4,    4,    4,    4,    4,
202
225
    4,    4,    4,    4,    4,    4,    4,    4,    1,    1,
203
    1,    1,    1,   13,   13,   12,   12,
204
225
};
205
const short yylen[] =
206
	{                                         2,
207
    0,    2,    2,    1,    2,    2,    0,    1,    3,    2,
208
    0,    1,    2,    3,    2,    3,    0,    1,    1,    3,
209
    1,    1,    1,    1,    2,   11,    7,   11,    7,    3,
210
    2,    0,    0,    9,    3,    0,    1,    0,    1,    1,
211
    3,    3,    5,    0,    3,    3,    1,    3,    3,    5,
212
    0,    1,    1,    3,    5,    0,    1,    3,    3,    3,
213
    3,    3,    3,    1,    0,    1,    2,    0,    1,    1,
214
205
    1,    1,    3,    4,    2,    3,    3,    3,    3,    3,
215
    3,    2,    2,    2,    2,    3,    4,    4,    4,    2,
216
205
    5,    5,    3,    3,    3,    3,    3,    3,    1,    4,
217
    1,    1,    1,    1,    3,    1,    1,
218
};
219
const short yydefred[] =
220
915
	{                                      1,
221
    0,    0,    0,    0,   71,    0,   72,   21,    0,   22,
222
915
   24,    0,    0,    0,    0,    0,    0,    0,  102,  103,
223
   23,    0,    0,    0,    0,    0,    0,    0,    4,    0,
224
    2,    0,    8,    5,    6,    0,    0,   12,    0,    0,
225
10
    0,    0,    0,    0,    0,   25,   32,   32,   32,    0,
226
    0,  107,    0,  104,    0,    0,   75,    0,  101,   82,
227
10
   83,    0,   84,   85,   32,   32,    0,    0,    0,    0,
228
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
229
    0,    0,    3,    0,   73,    0,   30,    0,    0,    0,
230
    0,    0,   35,    0,   67,    0,   33,    0,    0,    0,
231
    0,    0,   33,   33,    0,    0,    0,    0,    0,    0,
232
25
    0,    0,    0,    0,    0,    0,    0,    0,    0,    9,
233
    0,   16,   14,    0,   74,  100,   87,    0,    0,    0,
234
25
    0,    0,   88,   89,  105,    0,    0,   41,   37,    0,
235
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
236
    0,    0,    0,    0,    0,    0,    0,    0,   57,    0,
237
    0,    0,    0,    0,    0,    0,   33,   18,    0,   43,
238
   55,    0,    0,   29,    0,    0,    0,   32,    0,    0,
239
1455
    0,    0,   33,    0,    0,   46,   45,   34,   33,    0,
240
   48,    0,   26,   28,    0,   50,
241
1455
};
242
const short yydgoto[] =
243
2050
	{                                       1,
244
2010
   36,   89,   96,   28,   29,   30,   31,   91,  129,  158,
245
  167,   54,   55,  132,   46,   32,  168,   39,  130,   80,
246
  140,  176,   81,  180,
247
40
};
248
const short yysindex[] =
249
	{                                      0,
250
2050
  -77, -188,  196,   88,    0, -257,    0,    0, -260,    0,
251
    0, -236,  223, -225, -109, -107, -103, -102,    0,    0,
252
920
    0,  115,  196,  196, -227, -227, -291,  -17,    0, -237,
253
460
    0, -202,    0,    0,    0, -166,  479,    0,  -89,  196,
254
  196,  -87,  196,  142,  -17,    0,    0,    0,    0,  196,
255
460
  196,    0,  -17,    0, -195,  815,    0, -170,    0,    0,
256
    0,  196,    0,    0,    0,    0,  196,  196,  196,  196,
257
20
  196,  196,  196,  196,  196,  196,  196,  196,  -85, -157,
258
  -81,   88,    0,  196,    0,   88,    0,   88,  -72,  -17,
259
  -73,  607,    0,  496,    0,  196,    0,  196,  514,  531,
260
  115,  291,    0,    0,  291,  291,  291,  291,  291,  291,
261
20
  126,  126,  -71,  -71,  -71,  -71,  -33,  -37,  -32,    0,
262
40
  291,    0,    0,  250,    0,    0,    0,  -17,  -21,  196,
263
20
  787,  -20,    0,    0,    0,  196,  196,    0,    0,   -7,
264
20
   -6, -216,  -17,  196,    6,  196,  196,  196,  196,  196,
265
  196,   88,  802,  815,   -4,    7,  169,   20,    0,   88,
266
  291,  291,  291,  291,  291,  291,    0,    0,   21,    0,
267
20
    0,  196,    5,    0,   41,   88,   51,    0,   49, -207,
268
  -45,   88,    0,   54,   65,    0,    0,    0,    0,   88,
269
20
    0,   74,    0,    0,   76,    0,};
270
const short yyrindex[] =
271
	{                                      0,
272
 -175,    0,    0,  -43,    0,  -48,    0,    0,    0,    0,
273
20
    0,    0,    2,    0,    0,    0,    0,    1,    0,    0,
274
80
    0,    0,    0,    0,    0,    0,  461,  221,    0,   81,
275
40
    0,    0,    0,    0,    0,  275,    0,    0,    0,   82,
276
20
    0,    0,    0,    0,  384,    0,    0,    0,    0,    0,
277
    0,    0,  286,    0,  445, -229,    0,  320,    0,    0,
278
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
279
20
    0,    0,    0,    0,    0,    0,    0,    0, -152,    0,
280
   84, -133,    0,    0,    0,  -34,    0,   72,   86,  -86,
281
90
    0,    0,    0,    0,    0,   90,    0,    0,    0,    0,
282
    0, -126,    0,    0,  194,  663,  694,  704,  735,  745,
283
180
  576,  590,  339,  383,  400,  444,    0,   89,    0,    0,
284
90
  652,    0,    0,    0,    0,    0,    0, -197,    0,    0,
285
90
 -117,    0,    0,    0,    0,    0,    0,    0,    0,    0,
286
  -74, -180,  -35,   96,    0,    0,    0,    0,    0,    0,
287
    0,  577,  786,  776,    0,    0,    0,    0,    0,  577,
288
 -241, -160, -156,  -53,  112,  119,    0,    0,   48,    0,
289
    0,   98,   77,    0,    0,  -43,    0,    0, -169,    0,
290
    0,  577,    0,    0,    0,    0,    0,    0,    0,  577,
291
    0, -104,    0,    0,    0,    0,};
292
const short yygindex[] =
293
95
	{                                      0,
294
   -1,    0,  -39,   -2,    0,    0,    0,    0,  203,    0,
295
 -146,  277,    0, -112,    0,    0,    4,  205,  -88,    0,
296
    0,    0,    0,    0,
297
95
};
298
#define YYTABLESIZE 1116
299
95
const short yytable[] =
300
	{                                      27,
301
   37,   40,   27,   62,   33,   41,   42,   38,   97,   98,
302
   45,   63,   64,  173,  136,  137,   58,  145,   58,   53,
303
   56,   57,   43,   60,   61,  103,  104,   90,   90,   79,
304
   90,  159,   90,   47,   90,  189,   90,   90,   92,   58,
305
65
   94,   37,   40,  194,   93,   93,  157,   99,  100,  185,
306
10
  186,   59,   19,   20,   90,   82,   90,   90,  187,  102,
307
5
   69,  101,   69,   83,  105,  106,  107,  108,  109,  110,
308
  111,  112,  113,  114,  115,  116,   99,   34,  174,   99,
309
60
   27,  121,    7,   35,   27,  120,   27,   47,   47,  122,
310
    7,  123,   41,  128,  190,  131,   47,   61,   53,   61,
311
  193,   63,  118,   63,   40,   99,   99,   40,   99,   99,
312
65
   99,   99,   99,   99,   99,   99,   99,   99,   99,   99,
313
65
   99,  143,   99,   99,   10,   96,   96,  131,   84,   98,
314
   98,   20,   10,  153,  154,   20,   63,   64,  183,   20,
315
   64,  131,   64,  161,  162,  163,  164,  165,  166,   48,
316
65
   27,   49,   49,   49,   92,   50,   51,   20,   27,   86,
317
   86,   49,   86,   86,   86,   86,   86,   86,   86,  128,
318
   53,   93,   87,   53,   27,  119,   88,  117,    2,   38,
319
165
   27,    3,   42,    4,  124,   42,  125,    5,   27,    6,
320
165
    7,    8,    9,   10,   11,   12,   13,   14,   15,   16,
321
   17,   18,   19,   20,   59,   21,   59,   22,   99,   99,
322
165
   23,   99,   86,   99,   11,   99,  188,   99,   11,   24,
323
   88,   54,   11,   15,   54,   25,   26,   15,  139,   78,
324
  138,   15,   94,   94,  141,   99,  144,   99,   99,  152,
325
205
   99,   99,   99,   99,   99,   99,   99,   99,   99,   99,
326
205
   99,   99,   99,  155,   99,   99,  156,  101,  101,   65,
327
205
  101,  169,  101,   65,  101,  160,  101,   65,   65,   66,
328
  170,   67,   68,   69,   70,   71,   72,  172,   73,   74,
329
   75,   76,   77,   78,  101,   65,  101,  101,  178,  101,
330
205
  101,  101,  101,  101,  101,  101,  101,  101,  101,  101,
331
  101,  101,  175,  101,  101,   44,   44,  179,   44,   44,
332
  182,  184,   44,   44,   44,   44,   44,  191,   44,   44,
333
   44,   44,   44,   44,   44,   44,   44,   44,   44,   13,
334
   44,  192,   44,   13,   27,   44,  195,   13,   27,  196,
335
135
   38,   51,   27,   39,   44,   52,    3,   68,    4,   36,
336
10
   44,   44,    5,   56,    6,    7,    8,   68,   10,   11,
337
   12,   13,   14,   15,   16,   17,   18,   19,   20,   60,
338
125
   21,   60,   22,    3,  177,   23,   62,  135,   62,    5,
339
135
  181,    6,    7,   52,   24,    0,    0,   12,    0,    0,
340
135
   25,   26,   17,   18,   19,   20,    0,   95,   95,    0,
341
    3,   95,   23,    0,   97,   97,    5,    0,    6,    7,
342
135
    0,   24,    0,    0,   12,    0,    0,   25,   26,   17,
343
   18,   19,   20,   75,   76,   77,   78,    3,    0,   23,
344
190
    0,    0,  171,    5,    0,    6,    7,    0,   24,    0,
345
    0,   12,    0,    0,   25,   26,   17,   18,   19,   20,
346
190
   93,   93,    0,   93,    3,   93,   23,   93,    0,   93,
347
    5,    0,    6,    7,    0,   24,    0,    0,   12,    0,
348
95
    0,   25,   26,   17,   18,   19,   20,   93,   19,   93,
349
   93,   44,   19,   23,    0,    0,   19,    5,    0,    6,
350
95
    7,    0,   24,    0,    0,   12,    0,    0,   25,   26,
351
   17,   18,   19,   20,   19,    0,    0,    0,    3,    0,
352
   23,    0,    0,    0,    5,    0,  142,    7,    0,   24,
353
    0,    0,   12,    0,    0,   25,   26,   17,   18,   19,
354
890
   20,   70,   70,    0,   70,    0,   70,   23,   70,    0,
355
890
   70,    0,  106,  106,    0,    0,   24,  106,    0,    0,
356
    0,  106,   25,   26,    0,    0,    0,    0,   70,    0,
357
890
   70,   70,    0,   70,   70,   70,   70,   70,   70,  106,
358
   70,   70,   70,   70,   70,   70,   99,   99,    0,   99,
359
    0,   99,    0,   99,    0,   99,   73,   74,   75,   76,
360
890
   77,   78,    0,    0,    0,   78,   78,    0,   78,    0,
361
   78,    0,   78,   99,   78,   99,   99,    0,   99,   99,
362
890
   99,   99,   99,   99,    0,   99,   99,   99,   99,   99,
363
   99,    0,   78,    0,   78,   78,    0,   78,   78,   78,
364
890
   78,   78,   78,    0,   78,   78,   78,   78,   78,   79,
365
   79,   66,   79,    0,   79,   66,   79,    0,   79,   66,
366
890
    0,    0,    0,    0,    0,    0,   80,   80,    0,   80,
367
    0,   80,    0,   80,    0,   80,   79,   66,   79,   79,
368
890
    0,   79,   79,   79,   79,   79,   79,    0,   79,   79,
369
   79,   79,   79,   80,    0,   80,   80,    0,   80,   80,
370
890
   80,   80,   80,   80,    0,   80,   80,   80,   80,   80,
371
   81,   81,   31,   81,    0,   81,   31,   81,    0,   81,
372
   31,    0,    0,    0,    0,    0,    0,    0,   70,    0,
373
    0,    0,   70,    0,    0,    0,   70,   81,   31,   81,
374
890
   81,    0,   81,   81,   81,   81,   81,   81,   85,   81,
375
   81,   81,   81,   81,   70,    0,   70,   70,    0,   70,
376
890
   70,   70,   70,   70,   70,  127,   70,   70,   70,   70,
377
   70,   70,    0,    0,   65,   66,    0,   67,   68,   69,
378
   70,   71,   72,  133,   73,   74,   75,   76,   77,   78,
379
    0,   65,   66,    0,   67,   68,   69,   70,   71,   72,
380
  134,   73,   74,   75,   76,   77,   78,    0,    0,   65,
381
   66,    0,   67,   68,   69,   70,   71,   72,    0,   73,
382
180
   74,   75,   76,   77,   78,    0,   65,   66,    0,   67,
383
90
   68,   69,   70,   71,   72,    0,   73,   74,   75,   76,
384
90
   77,   78,   76,   76,   17,   76,    0,   76,   17,   76,
385
90
    0,   76,   17,    0,    0,    0,   77,   77,    0,   77,
386
90
    0,   77,    0,   77,    0,   77,    0,    0,    0,   76,
387
90
   17,   76,   76,    0,   76,   76,   76,   76,   76,   76,
388
90
  126,   76,   76,   77,    0,   77,   77,    0,   77,   77,
389
   77,   77,   77,   77,    0,   77,   77,    0,    0,    0,
390
90
    0,    0,   65,   66,    0,   67,   68,   69,   70,   71,
391
   72,    0,   73,   74,   75,   76,   77,   78,   86,   86,
392
    0,   86,    0,   86,    0,   86,    0,   86,    0,   96,
393
   96,    0,   96,    0,   96,    0,   96,    0,   96,    0,
394
90
    0,    0,    0,    0,    0,   86,    0,   86,   86,    0,
395
90
   86,   86,   86,   86,   86,   86,   96,    0,   96,   96,
396
90
   98,   98,    0,   98,    0,   98,    0,   98,    0,   98,
397
90
   94,   94,    0,   94,    0,   94,    0,   94,    0,   94,
398
90
    0,    0,    0,    0,    0,    0,    0,   98,    0,   98,
399
90
   98,    0,    0,    0,    0,    0,    0,   94,    0,   94,
400
90
   94,   95,   95,    0,   95,    0,   95,    0,   95,    0,
401
   95,   97,   97,    0,   97,    0,   97,    0,   97,    0,
402
90
   97,    0,    0,    0,    0,    0,    0,    0,   95,    0,
403
   95,   95,    0,    0,    0,    0,    0,    0,   97,    0,
404
   97,   97,   91,   91,    0,   91,    0,   91,    0,   91,
405
    0,   91,   92,   92,    0,   92,    0,   92,    0,   92,
406
    0,   92,    0,    0,    0,    0,    0,    0,    0,   91,
407
    0,   91,   91,    0,    0,    0,    0,    0,    0,   92,
408
    0,   92,   65,   66,    0,  146,  147,  148,  149,  150,
409
  151,    0,   73,   74,   75,   76,   77,   78,   66,    0,
410
   67,   68,   69,   70,   71,   72,    0,   73,   74,   75,
411
   76,   77,   78,   67,   68,   69,   70,   71,   72,    0,
412
   73,   74,   75,   76,   77,   78,
413
};
414
const short yycheck[] =
415
	{                                       1,
416
50
    3,  259,    4,  295,    1,  263,  267,    4,   48,   49,
417
50
   13,  303,  304,  160,  103,  104,  258,  130,  260,   22,
418
   23,   24,  259,   25,   26,   65,   66,  257,  258,  267,
419
50
  260,  144,  262,  259,  264,  182,  266,   40,   41,  267,
420
   43,   44,  259,  190,  286,  287,  263,   50,   51,  257,
421
  258,  279,  280,  281,  284,  258,  286,  287,  266,   62,
422
  258,  257,  260,  266,   67,   68,   69,   70,   71,   72,
423
   73,   74,   75,   76,   77,   78,  257,  266,  167,  260,
424
   82,   84,  258,  272,   86,   82,   88,  257,  258,   86,
425
  266,   88,  263,   96,  183,   98,  266,  258,  101,  260,
426
30
  189,  258,  260,  260,  257,  286,  287,  260,  289,  290,
427
30
  291,  292,  293,  294,  295,  296,  297,  298,  299,  300,
428
  301,  124,  303,  304,  258,  286,  287,  130,  295,  286,
429
30
  287,  258,  266,  136,  137,  262,  303,  304,  178,  266,
430
  258,  144,  260,  146,  147,  148,  149,  150,  151,  259,
431
5
  152,  259,  257,  258,  157,  259,  259,  284,  160,  286,
432
5
  287,  266,  289,  290,  291,  292,  293,  294,  258,  172,
433
  257,  259,  262,  260,  176,  257,  266,  263,  256,  176,
434
5
  182,  259,  257,  261,  257,  260,  260,  265,  190,  267,
435
  268,  269,  270,  271,  272,  273,  274,  275,  276,  277,
436
  278,  279,  280,  281,  258,  283,  260,  285,  257,  258,
437
  288,  260,  258,  262,  258,  264,  262,  266,  262,  297,
438
  266,  257,  266,  258,  260,  303,  304,  262,  266,  301,
439
  264,  266,  286,  287,  267,  284,  258,  286,  287,  260,
440
  289,  290,  291,  292,  293,  294,  295,  296,  297,  298,
441
  299,  300,  301,  261,  303,  304,  263,  257,  258,  258,
442
  260,  266,  262,  262,  264,  260,  266,  266,  286,  287,
443
  264,  289,  290,  291,  292,  293,  294,  258,  296,  297,
444
  298,  299,  300,  301,  284,  284,  286,  287,  284,  289,
445
  290,  291,  292,  293,  294,  295,  296,  297,  298,  299,
446
  300,  301,  282,  303,  304,  258,  259,  267,  261,  262,
447
35
  260,  263,  265,  266,  267,  268,  269,  264,  271,  272,
448
35
  273,  274,  275,  276,  277,  278,  279,  280,  281,  258,
449
  283,  267,  285,  262,  258,  288,  263,  266,  262,  264,
450
35
  260,  260,  266,  260,  297,  260,  259,  258,  261,  261,
451
  303,  304,  265,  258,  267,  268,  269,  260,  271,  272,
452
  273,  274,  275,  276,  277,  278,  279,  280,  281,  258,
453
  283,  260,  285,  259,  172,  288,  258,  101,  260,  265,
454
  176,  267,  268,  269,  297,   -1,   -1,  273,   -1,   -1,
455
  303,  304,  278,  279,  280,  281,   -1,  286,  287,   -1,
456
  259,  260,  288,   -1,  286,  287,  265,   -1,  267,  268,
457
220
   -1,  297,   -1,   -1,  273,   -1,   -1,  303,  304,  278,
458
220
  279,  280,  281,  298,  299,  300,  301,  259,   -1,  288,
459
   -1,   -1,  264,  265,   -1,  267,  268,   -1,  297,   -1,
460
220
   -1,  273,   -1,   -1,  303,  304,  278,  279,  280,  281,
461
  257,  258,   -1,  260,  259,  262,  288,  264,   -1,  266,
462
  265,   -1,  267,  268,   -1,  297,   -1,   -1,  273,   -1,
463
   -1,  303,  304,  278,  279,  280,  281,  284,  258,  286,
464
  287,  259,  262,  288,   -1,   -1,  266,  265,   -1,  267,
465
  268,   -1,  297,   -1,   -1,  273,   -1,   -1,  303,  304,
466
  278,  279,  280,  281,  284,   -1,   -1,   -1,  259,   -1,
467
  288,   -1,   -1,   -1,  265,   -1,  267,  268,   -1,  297,
468
   -1,   -1,  273,   -1,   -1,  303,  304,  278,  279,  280,
469
  281,  257,  258,   -1,  260,   -1,  262,  288,  264,   -1,
470
  266,   -1,  257,  258,   -1,   -1,  297,  262,   -1,   -1,
471
70
   -1,  266,  303,  304,   -1,   -1,   -1,   -1,  284,   -1,
472
  286,  287,   -1,  289,  290,  291,  292,  293,  294,  284,
473
70
  296,  297,  298,  299,  300,  301,  257,  258,   -1,  260,
474
   -1,  262,   -1,  264,   -1,  266,  296,  297,  298,  299,
475
  300,  301,   -1,   -1,   -1,  257,  258,   -1,  260,   -1,
476
  262,   -1,  264,  284,  266,  286,  287,   -1,  289,  290,
477
  291,  292,  293,  294,   -1,  296,  297,  298,  299,  300,
478
  301,   -1,  284,   -1,  286,  287,   -1,  289,  290,  291,
479
  292,  293,  294,   -1,  296,  297,  298,  299,  300,  257,
480
55
  258,  258,  260,   -1,  262,  262,  264,   -1,  266,  266,
481
   -1,   -1,   -1,   -1,   -1,   -1,  257,  258,   -1,  260,
482
55
   -1,  262,   -1,  264,   -1,  266,  284,  284,  286,  287,
483
   -1,  289,  290,  291,  292,  293,  294,   -1,  296,  297,
484
5
  298,  299,  300,  284,   -1,  286,  287,   -1,  289,  290,
485
  291,  292,  293,  294,   -1,  296,  297,  298,  299,  300,
486
5
  257,  258,  258,  260,   -1,  262,  262,  264,   -1,  266,
487
  266,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  258,   -1,
488
5
   -1,   -1,  262,   -1,   -1,   -1,  266,  284,  284,  286,
489
  287,   -1,  289,  290,  291,  292,  293,  294,  260,  296,
490
  297,  298,  299,  300,  284,   -1,  286,  287,   -1,  289,
491
  290,  291,  292,  293,  294,  260,  296,  297,  298,  299,
492
  300,  301,   -1,   -1,  286,  287,   -1,  289,  290,  291,
493
5
  292,  293,  294,  260,  296,  297,  298,  299,  300,  301,
494
   -1,  286,  287,   -1,  289,  290,  291,  292,  293,  294,
495
5
  260,  296,  297,  298,  299,  300,  301,   -1,   -1,  286,
496
  287,   -1,  289,  290,  291,  292,  293,  294,   -1,  296,
497
  297,  298,  299,  300,  301,   -1,  286,  287,   -1,  289,
498
  290,  291,  292,  293,  294,   -1,  296,  297,  298,  299,
499
  300,  301,  257,  258,  258,  260,   -1,  262,  262,  264,
500
   -1,  266,  266,   -1,   -1,   -1,  257,  258,   -1,  260,
501
100
   -1,  262,   -1,  264,   -1,  266,   -1,   -1,   -1,  284,
502
  284,  286,  287,   -1,  289,  290,  291,  292,  293,  294,
503
100
  264,  296,  297,  284,   -1,  286,  287,   -1,  289,  290,
504
  291,  292,  293,  294,   -1,  296,  297,   -1,   -1,   -1,
505
5
   -1,   -1,  286,  287,   -1,  289,  290,  291,  292,  293,
506
  294,   -1,  296,  297,  298,  299,  300,  301,  257,  258,
507
5
   -1,  260,   -1,  262,   -1,  264,   -1,  266,   -1,  257,
508
  258,   -1,  260,   -1,  262,   -1,  264,   -1,  266,   -1,
509
75
   -1,   -1,   -1,   -1,   -1,  284,   -1,  286,  287,   -1,
510
  289,  290,  291,  292,  293,  294,  284,   -1,  286,  287,
511
75
  257,  258,   -1,  260,   -1,  262,   -1,  264,   -1,  266,
512
  257,  258,   -1,  260,   -1,  262,   -1,  264,   -1,  266,
513
15
   -1,   -1,   -1,   -1,   -1,   -1,   -1,  284,   -1,  286,
514
  287,   -1,   -1,   -1,   -1,   -1,   -1,  284,   -1,  286,
515
15
  287,  257,  258,   -1,  260,   -1,  262,   -1,  264,   -1,
516
  266,  257,  258,   -1,  260,   -1,  262,   -1,  264,   -1,
517
45
  266,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  284,   -1,
518
  286,  287,   -1,   -1,   -1,   -1,   -1,   -1,  284,   -1,
519
45
  286,  287,  257,  258,   -1,  260,   -1,  262,   -1,  264,
520
   -1,  266,  257,  258,   -1,  260,   -1,  262,   -1,  264,
521
5
   -1,  266,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  284,
522
   -1,  286,  287,   -1,   -1,   -1,   -1,   -1,   -1,  284,
523
5
   -1,  286,  286,  287,   -1,  289,  290,  291,  292,  293,
524
  294,   -1,  296,  297,  298,  299,  300,  301,  287,   -1,
525
320
  289,  290,  291,  292,  293,  294,   -1,  296,  297,  298,
526
  299,  300,  301,  289,  290,  291,  292,  293,  294,   -1,
527
320
  296,  297,  298,  299,  300,  301,
528
};
529
#define YYFINAL 1
530
#ifndef YYDEBUG
531
#define YYDEBUG 0
532
#endif
533
10
#define YYMAXTOKEN 304
534
5
#if YYDEBUG
535
const char * const yyname[] =
536
5
	{
537
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
538
170
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
539
85
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
540
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
541
85
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
542
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
543
10
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"COMMA","SEMICOLON","LPAR","RPAR",
544
5
"LBRACE","RBRACE","LBRACKET","RBRACKET","DOT","NEWLINE","LETTER","NUMBER",
545
"STRING","DEFINE","BREAK","QUIT","LENGTH","RETURN","FOR","IF","WHILE","SQRT",
546
5
"SCALE","IBASE","OBASE","AUTO","CONTINUE","ELSE","PRINT","BOOL_OR","BOOL_AND",
547
"BOOL_NOT","EQUALS","LESS_EQ","GREATER_EQ","UNEQUALS","LESS","GREATER",
548
"ASSIGN_OP","PLUS","MINUS","MULTIPLY","DIVIDE","REMAINDER","EXPONENT","UMINUS",
549
"INCR","DECR",
550
};
551
25
const char * const yyrule[] =
552
	{"$accept : program",
553
25
"program :",
554
"program : program input_item",
555
"input_item : semicolon_list NEWLINE",
556
"input_item : function",
557
"input_item : error NEWLINE",
558
2615
"input_item : error QUIT",
559
"semicolon_list :",
560
2615
"semicolon_list : statement",
561
10
"semicolon_list : semicolon_list SEMICOLON statement",
562
"semicolon_list : semicolon_list SEMICOLON",
563
10
"statement_list :",
564
"statement_list : statement",
565
2750
"statement_list : statement_list NEWLINE",
566
"statement_list : statement_list NEWLINE statement",
567
2750
"statement_list : statement_list SEMICOLON",
568
"statement_list : statement_list SEMICOLON statement",
569
230
"opt_statement :",
570
"opt_statement : statement",
571
230
"statement : expression",
572
"statement : named_expression ASSIGN_OP expression",
573
230
"statement : STRING",
574
115
"statement : BREAK",
575
"statement : CONTINUE",
576
115
"statement : QUIT",
577
"statement : RETURN return_expression",
578
115
"statement : FOR LPAR alloc_macro opt_expression SEMICOLON opt_relational_expression SEMICOLON opt_expression RPAR opt_statement pop_nesting",
579
"statement : IF LPAR alloc_macro pop_nesting relational_expression RPAR opt_statement",
580
50
"statement : IF LPAR alloc_macro pop_nesting relational_expression RPAR opt_statement ELSE alloc_macro pop_nesting opt_statement",
581
"statement : WHILE LPAR alloc_macro relational_expression RPAR opt_statement pop_nesting",
582
"statement : LBRACE statement_list RBRACE",
583
50
"statement : PRINT print_expression_list",
584
"alloc_macro :",
585
185
"pop_nesting :",
586
"function : function_header opt_parameter_list RPAR opt_newline LBRACE NEWLINE opt_auto_define_list statement_list RBRACE",
587
185
"function_header : DEFINE LETTER LPAR",
588
"opt_newline :",
589
90
"opt_newline : NEWLINE",
590
"opt_parameter_list :",
591
90
"opt_parameter_list : parameter_list",
592
"parameter_list : LETTER",
593
240
"parameter_list : LETTER LBRACKET RBRACKET",
594
"parameter_list : parameter_list COMMA LETTER",
595
240
"parameter_list : parameter_list COMMA LETTER LBRACKET RBRACKET",
596
"opt_auto_define_list :",
597
130
"opt_auto_define_list : AUTO define_list NEWLINE",
598
"opt_auto_define_list : AUTO define_list SEMICOLON",
599
130
"define_list : LETTER",
600
"define_list : LETTER LBRACKET RBRACKET",
601
10
"define_list : define_list COMMA LETTER",
602
"define_list : define_list COMMA LETTER LBRACKET RBRACKET",
603
10
"opt_argument_list :",
604
"opt_argument_list : argument_list",
605
5
"argument_list : expression",
606
"argument_list : argument_list COMMA expression",
607
5
"argument_list : argument_list COMMA LETTER LBRACKET RBRACKET",
608
"opt_relational_expression :",
609
"opt_relational_expression : relational_expression",
610
"relational_expression : expression EQUALS expression",
611
"relational_expression : expression UNEQUALS expression",
612
"relational_expression : expression LESS expression",
613
"relational_expression : expression LESS_EQ expression",
614
"relational_expression : expression GREATER expression",
615
"relational_expression : expression GREATER_EQ expression",
616
"relational_expression : expression",
617
"return_expression :",
618
"return_expression : expression",
619
70
"return_expression : LPAR RPAR",
620
35
"opt_expression :",
621
"opt_expression : expression",
622
35
"expression : named_expression",
623
"expression : DOT",
624
10
"expression : NUMBER",
625
5
"expression : LPAR expression RPAR",
626
"expression : LETTER LPAR opt_argument_list RPAR",
627
5
"expression : MINUS expression",
628
"expression : expression PLUS expression",
629
80
"expression : expression MINUS expression",
630
80
"expression : expression MULTIPLY expression",
631
"expression : expression DIVIDE expression",
632
"expression : expression REMAINDER expression",
633
"expression : expression EXPONENT expression",
634
"expression : INCR named_expression",
635
"expression : DECR named_expression",
636
80
"expression : named_expression INCR",
637
"expression : named_expression DECR",
638
40
"expression : named_expression ASSIGN_OP expression",
639
"expression : LENGTH LPAR expression RPAR",
640
40
"expression : SQRT LPAR expression RPAR",
641
"expression : SCALE LPAR expression RPAR",
642
20
"expression : BOOL_NOT expression",
643
"expression : expression BOOL_AND alloc_macro pop_nesting expression",
644
20
"expression : expression BOOL_OR alloc_macro pop_nesting expression",
645
"expression : expression EQUALS expression",
646
40
"expression : expression UNEQUALS expression",
647
"expression : expression LESS expression",
648
40
"expression : expression LESS_EQ expression",
649
"expression : expression GREATER expression",
650
15
"expression : expression GREATER_EQ expression",
651
"named_expression : LETTER",
652
15
"named_expression : LETTER LBRACKET expression RBRACKET",
653
"named_expression : SCALE",
654
65
"named_expression : IBASE",
655
65
"named_expression : OBASE",
656
65
"print_expression_list : print_expression",
657
"print_expression_list : print_expression_list COMMA print_expression",
658
65
"print_expression : expression",
659
"print_expression : STRING",
660
50
};
661
50
#endif
662
50
#ifdef YYSTACKSIZE
663
#undef YYMAXDEPTH
664
50
#define YYMAXDEPTH YYSTACKSIZE
665
#else
666
30
#ifdef YYMAXDEPTH
667
#define YYSTACKSIZE YYMAXDEPTH
668
30
#else
669
#define YYSTACKSIZE 10000
670
15
#define YYMAXDEPTH 10000
671
#endif
672
15
#endif
673
#define YYINITSTACKSIZE 200
674
25
/* LINTUSED */
675
int yydebug;
676
25
int yynerrs;
677
int yyerrflag;
678
15
int yychar;
679
short *yyssp;
680
15
YYSTYPE *yyvsp;
681
YYSTYPE yyval;
682
15
YYSTYPE yylval;
683
short *yyss;
684
15
short *yysslim;
685
YYSTYPE *yyvs;
686
15
unsigned int yystacksize;
687
int yyparse(void);
688
15
#line 743 "/usr/src/usr.bin/bc/bc.y"
689
690
691
static void
692
grow(void)
693
4605
{
694
	struct tree	*p;
695
4605
	size_t		newsize;
696
697
4605
	if (current == instr_sz) {
698
		newsize = instr_sz * 2 + 1;
699
4605
		p = reallocarray(instructions, newsize, sizeof(*p));
700
		if (p == NULL) {
701
10
			free(instructions);
702
5
			err(1, NULL);
703
10
		}
704
5
		instructions = p;
705
5
		instr_sz = newsize;
706
	}
707
5
}
708
709
175
static ssize_t
710
175
cs(const char *str)
711
{
712
175
	grow();
713
	instructions[current].index = CONST_STRING;
714
	instructions[current].u.cstr = str;
715
	return current++;
716
}
717
718
static ssize_t
719
as(const char *str)
720
{
721
	grow();
722
	instructions[current].index = ALLOC_STRING;
723
	instructions[current].u.astr = strdup(str);
724
	if (instructions[current].u.astr == NULL)
725
		err(1, NULL);
726
	return current++;
727
}
728
20
729
static ssize_t
730
20
node(ssize_t arg, ...)
731
{
732
	va_list		ap;
733
	ssize_t		ret;
734
30
735
	va_start(ap, arg);
736
30
737
	ret = current;
738
85
	grow();
739
85
	instructions[current++].index = arg;
740
85
741
	do {
742
85
		arg = va_arg(ap, ssize_t);
743
		grow();
744
		instructions[current++].index = arg;
745
	} while (arg != END_NODE);
746
747
	va_end(ap);
748
	return ret;
749
}
750
751
static void
752
emit(ssize_t i, int level)
753
{
754
	if (level > 1000)
755
		errx(1, "internal error: tree level > 1000");
756
	if (instructions[i].index >= 0) {
757
		while (instructions[i].index != END_NODE &&
758
		    instructions[i].index != i)  {
759
			emit(instructions[i].index, level + 1);
760
			i++;
761
		}
762
	} else if (instructions[i].index != END_NODE)
763
		fputs(instructions[i].u.cstr, stdout);
764
}
765
766
static void
767
emit_macro(int node, ssize_t code)
768
{
769
	putchar('[');
770
	emit(code, 0);
771
	printf("]s%s\n", instructions[node].u.cstr);
772
	nesting--;
773
}
774
775
static void
776
free_tree(void)
777
{
778
	ssize_t i;
779
780
	for (i = 0; i < current; i++)
781
		if (instructions[i].index == ALLOC_STRING)
782
			free(instructions[i].u.astr);
783
	current = 0;
784
}
785
786
static ssize_t
787
numnode(int num)
788
{
789
	const char *p;
790
791
	if (num < 10)
792
		p = str_table['0' + num];
793
	else if (num < 16)
794
		p = str_table['A' - 10 + num];
795
	else
796
		errx(1, "internal error: break num > 15");
797
	return node(cs(" "), cs(p), END_NODE);
798
}
799
800
801
static ssize_t
802
lookup(char * str, size_t len, char type)
803
{
804
	ENTRY	entry, *found;
805
	u_short	num;
806
	u_char	*p;
807
808
	/* The scanner allocated an extra byte already */
809
	if (str[len-1] != type) {
810
		str[len] = type;
811
		str[len+1] = '\0';
812
	}
813
	entry.key = str;
814
	found = hsearch(entry, FIND);
815
	if (found == NULL) {
816
		if (var_count == MAX_VARIABLES)
817
			errx(1, "too many variables");
818
		p = malloc(4);
819
		if (p == NULL)
820
			err(1, NULL);
821
		num = var_count++;
822
		p[0] = 255;
823
		p[1] = ENCODE(num / VAR_BASE + 1);
824
		p[2] = ENCODE(num % VAR_BASE + 1);
825
		p[3] = '\0';
826
827
		entry.data = (char *)p;
828
		entry.key = strdup(str);
829
		if (entry.key == NULL)
830
			err(1, NULL);
831
		found = hsearch(entry, ENTER);
832
		if (found == NULL)
833
			err(1, NULL);
834
	}
835
	return cs(found->data);
836
}
837
838
static ssize_t
839
letter_node(char *str)
840
{
841
	size_t len;
842
843
	len = strlen(str);
844
	if (len == 1 && str[0] != '_')
845
		return cs(str_table[(int)str[0]]);
846
	else
847
		return lookup(str, len, 'L');
848
}
849
850
static ssize_t
851
array_node(char *str)
852
{
853
	size_t len;
854
855
	len = strlen(str);
856
	if (len == 1 && str[0] != '_')
857
		return cs(str_table[(int)str[0] - 'a' + ARRAY_CHAR]);
858
	else
859
		return lookup(str, len, 'A');
860
}
861
862
static ssize_t
863
function_node(char *str)
864
{
865
	size_t len;
866
867
	len = strlen(str);
868
	if (len == 1 && str[0] != '_')
869
		return cs(str_table[(int)str[0] - 'a' + FUNC_CHAR]);
870
	else
871
		return lookup(str, len, 'F');
872
}
873
874
static void
875
add_par(ssize_t n)
876
{
877
	prologue = node(cs("S"), n, prologue, END_NODE);
878
	epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
879
}
880
881
static void
882
add_local(ssize_t n)
883
{
884
	prologue = node(cs("0S"), n, prologue, END_NODE);
885
	epilogue = node(epilogue, cs("L"), n, cs("s."), END_NODE);
886
}
887
888
void
889
yyerror(char *s)
890
{
891
	char	*str, *p;
892
	int	n;
893
894
	if (yyin != NULL && feof(yyin))
895
		n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF",
896
		    __progname, filename, lineno, s);
897
	else if (yytext[0] == '\n')
898
		n = asprintf(&str,
899
		    "%s: %s:%d: %s: newline unexpected",
900
		    __progname, filename, lineno, s);
901
	else if (isspace((unsigned char)yytext[0]) ||
902
	    !isprint((unsigned char)yytext[0]))
903
		n = asprintf(&str,
904
		    "%s: %s:%d: %s: ascii char 0x%02x unexpected",
905
		    __progname, filename, lineno, s, yytext[0] & 0xff);
906
	else
907
		n = asprintf(&str, "%s: %s:%d: %s: %s unexpected",
908
		    __progname, filename, lineno, s, yytext);
909
	if (n == -1)
910
		err(1, NULL);
911
912
	fputs("c[", stdout);
913
	for (p = str; *p != '\0'; p++) {
914
		if (*p == '[' || *p == ']' || *p =='\\')
915
			putchar('\\');
916
		putchar(*p);
917
	}
918
	fputs("]ec\n", stdout);
919
	free(str);
920
}
921
922
void
923
fatal(const char *s)
924
{
925
	errx(1, "%s:%d: %s", filename, lineno, s);
926
}
927
928
static void
929
warning(const char *s)
930
{
931
	warnx("%s:%d: %s", filename, lineno, s);
932
}
933
934
static void
935
init(void)
936
{
937
	int i;
938
939
	for (i = 0; i < UCHAR_MAX; i++) {
940
		str_table[i][0] = i;
941
		str_table[i][1] = '\0';
942
	}
943
	if (hcreate(1 << 16) == 0)
944
		err(1, NULL);
945
}
946
947
948
static __dead void
949
usage(void)
950
{
951
	fprintf(stderr, "usage: %s [-cl] [-e expression] [file ...]\n",
952
	    __progname);
953
	exit(1);
954
}
955
956
static char *
957
escape(const char *str)
958
{
959
	char *ret, *p;
960
961
	ret = malloc(strlen(str) + 1);
962
	if (ret == NULL)
963
		err(1, NULL);
964
965
	p = ret;
966
	while (*str != '\0') {
967
		/*
968
		 * We get _escaped_ strings here. Single backslashes are
969
		 * already converted to double backslashes
970
		 */
971
		if (*str == '\\') {
972
			if (*++str == '\\') {
973
				switch (*++str) {
974
				case 'a':
975
					*p++ = '\a';
976
					break;
977
				case 'b':
978
					*p++ = '\b';
979
					break;
980
				case 'f':
981
					*p++ = '\f';
982
					break;
983
				case 'n':
984
					*p++ = '\n';
985
					break;
986
				case 'q':
987
					*p++ = '"';
988
					break;
989
				case 'r':
990
					*p++ = '\r';
991
					break;
992
				case 't':
993
					*p++ = '\t';
994
					break;
995
				case '\\':
996
					*p++ = '\\';
997
					break;
998
				}
999
				str++;
1000
			} else {
1001
				*p++ = '\\';
1002
				*p++ = *str++;
1003
			}
1004
		} else
1005
			*p++ = *str++;
1006
	}
1007
	*p = '\0';
1008
	return ret;
1009
}
1010
1011
/* ARGSUSED */
1012
static void
1013
sigchld(int signo)
1014
{
1015
	pid_t pid;
1016
	int status, save_errno = errno;
1017
1018
	for (;;) {
1019
		pid = waitpid(dc, &status, WCONTINUED | WNOHANG);
1020
		if (pid == -1) {
1021
			if (errno == EINTR)
1022
				continue;
1023
			_exit(0);
1024
		} else if (pid == 0)
1025
			break;
1026
		if (WIFEXITED(status) || WIFSIGNALED(status))
1027
			_exit(0);
1028
		else
1029
			break;
1030
	}
1031
	errno = save_errno;
1032
}
1033
1034
static const char *
1035
dummy_prompt(void)
1036
{
1037
1038
        return ("");
1039
}
1040
1041
int
1042
main(int argc, char *argv[])
1043
{
1044
	int	i, ch;
1045
	int	p[2];
1046
	char	*q;
1047
1048
	if (pledge("stdio rpath proc tty flock cpath wpath", NULL) == -1)
1049
		err(1, "pledge");
1050
1051
	init();
1052
	setvbuf(stdout, NULL, _IOLBF, 0);
1053
1054
	sargv = reallocarray(NULL, argc, sizeof(char *));
1055
	if (sargv == NULL)
1056
		err(1, NULL);
1057
1058
	if ((cmdexpr = strdup("")) == NULL)
1059
		err(1, NULL);
1060
	/* The d debug option is 4.4 BSD bc(1) compatible */
1061
	while ((ch = getopt(argc, argv, "cde:l")) != -1) {
1062
		switch (ch) {
1063
		case 'c':
1064
		case 'd':
1065
			do_fork = false;
1066
			break;
1067
		case 'e':
1068
			q = cmdexpr;
1069
			if (asprintf(&cmdexpr, "%s%s\n", cmdexpr, optarg) == -1)
1070
				err(1, NULL);
1071
			free(q);
1072
			break;
1073
		case 'l':
1074
			sargv[sargc++] = _PATH_LIBB;
1075
			break;
1076
		default:
1077
			usage();
1078
		}
1079
	}
1080
1081
	argc -= optind;
1082
	argv += optind;
1083
1084
	interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) &&
1085
	    isatty(STDERR_FILENO);
1086
	for (i = 0; i < argc; i++)
1087
		sargv[sargc++] = argv[i];
1088
1089
	if (do_fork) {
1090
		if (pipe(p) == -1)
1091
			err(1, "cannot create pipe");
1092
		dc = fork();
1093
		if (dc == -1)
1094
			err(1, "cannot fork");
1095
		else if (dc != 0) {
1096
			signal(SIGCHLD, sigchld);
1097
			close(STDOUT_FILENO);
1098
			dup(p[1]);
1099
			close(p[0]);
1100
			close(p[1]);
1101
		} else {
1102
			char *dc_argv[] = { "dc", "-x", NULL };
1103
			extern int dc_main(int, char **);
1104
1105
			if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
1106
				err(1, "pledge");
1107
1108
			close(STDIN_FILENO);
1109
			dup(p[0]);
1110
			close(p[0]);
1111
			close(p[1]);
1112
1113
			exit (dc_main(2, dc_argv));
1114
		}
1115
	}
1116
	if (interactive) {
1117
		gettty(&ttysaved);
1118
		el = el_init("bc", stdin, stderr, stderr);
1119
		hist = history_init();
1120
		history(hist, &he, H_SETSIZE, 100);
1121
		el_set(el, EL_HIST, history, hist);
1122
		el_set(el, EL_EDITOR, "emacs");
1123
		el_set(el, EL_SIGNAL, 0);
1124
		el_set(el, EL_PROMPT, dummy_prompt);
1125
		el_set(el, EL_ADDFN, "bc_eof", "", bc_eof);
1126
		el_set(el, EL_BIND, "^D", "bc_eof", NULL);
1127
		el_source(el, NULL);
1128
	}
1129
1130
	if (pledge("stdio rpath tty flock cpath wpath", NULL) == -1)
1131
		err(1, "pledge");
1132
1133
	yywrap();
1134
	return yyparse();
1135
}
1136
#line 1129 "bc.c"
1137
180
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
1138
90
static int yygrowstack(void)
1139
{
1140
    unsigned int newsize;
1141
    long sslen;
1142
    short *newss;
1143
90
    YYSTYPE *newvs;
1144
1145
    if ((newsize = yystacksize) == 0)
1146
        newsize = YYINITSTACKSIZE;
1147
    else if (newsize >= YYMAXDEPTH)
1148
        return -1;
1149

180
    else if ((newsize *= 2) > YYMAXDEPTH)
1150
        newsize = YYMAXDEPTH;
1151
180
    sslen = yyssp - yyss;
1152
90
#ifdef SIZE_MAX
1153
90
#define YY_SIZE_MAX SIZE_MAX
1154
#else
1155
90
#define YY_SIZE_MAX 0xffffffffU
1156
90
#endif
1157

180
    if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)
1158
        goto bail;
1159
180
    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :
1160
90
      (short *)malloc(newsize * sizeof *newss); /* overflow check above */
1161
90
    if (newss == NULL)
1162
        goto bail;
1163
90
    yyss = newss;
1164
90
    yyssp = newss + sslen;
1165
90
    if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)
1166
90
        goto bail;
1167
90
    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :
1168
      (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check above */
1169
    if (newvs == NULL)
1170
        goto bail;
1171
    yyvs = newvs;
1172
    yyvsp = newvs + sslen;
1173
    yystacksize = newsize;
1174
    yysslim = yyss + newsize - 1;
1175
    return 0;
1176
bail:
1177
90
    if (yyss)
1178
            free(yyss);
1179
    if (yyvs)
1180
            free(yyvs);
1181
    yyss = yyssp = NULL;
1182
    yyvs = yyvsp = NULL;
1183
    yystacksize = 0;
1184
    return -1;
1185
}
1186
1187
#define YYABORT goto yyabort
1188
#define YYREJECT goto yyabort
1189
#define YYACCEPT goto yyaccept
1190
#define YYERROR goto yyerrlab
1191
int
1192
yyparse(void)
1193
{
1194
    int yym, yyn, yystate;
1195
#if YYDEBUG
1196
    const char *yys;
1197
1198
180
    if ((yys = getenv("YYDEBUG")))
1199
90
    {
1200
90
        yyn = *yys;
1201
        if (yyn >= '0' && yyn <= '9')
1202

180
            yydebug = yyn - '0';
1203
90
    }
1204
90
#endif /* YYDEBUG */
1205
90
1206
    yynerrs = 0;
1207
    yyerrflag = 0;
1208
55020
    yychar = (-1);
1209
36080
1210
    if (yyss == NULL && yygrowstack()) goto yyoverflow;
1211
17875
    yyssp = yyss;
1212
    yyvsp = yyvs;
1213
    *yyssp = yystate = 0;
1214
1215
yyloop:
1216
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
1217
    if (yychar < 0)
1218
    {
1219
        if ((yychar = yylex()) < 0) yychar = 0;
1220
#if YYDEBUG
1221
        if (yydebug)
1222
17875
        {
1223

142885
            yys = 0;
1224
70725
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
1225
            if (!yys) yys = "illegal-symbol";
1226
            printf("%sdebug: state %d, reading %d (%s)\n",
1227
                    YYPREFIX, yystate, yychar, yys);
1228
        }
1229
#endif
1230
    }
1231

21485
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
1232
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
1233
    {
1234
#if YYDEBUG
1235
21485
        if (yydebug)
1236
21485
            printf("%sdebug: state %d, shifting to state %d\n",
1237
21485
                    YYPREFIX, yystate, yytable[yyn]);
1238
21485
#endif
1239
21485
        if (yyssp >= yysslim && yygrowstack())
1240
        {
1241

58380
            goto yyoverflow;
1242
29190
        }
1243
        *++yyssp = yystate = yytable[yyn];
1244
14595
        *++yyvsp = yylval;
1245
14595
        yychar = (-1);
1246
        if (yyerrflag > 0)  --yyerrflag;
1247
        goto yyloop;
1248
    }
1249
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
1250
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
1251
    {
1252
        yyn = yytable[yyn];
1253
        goto yyreduce;
1254
    }
1255
    if (yyerrflag) goto yyinrecovery;
1256
#if defined(__GNUC__)
1257
    goto yynewerror;
1258
#endif
1259
yynewerror:
1260
    yyerror("syntax error");
1261
#if defined(__GNUC__)
1262
    goto yyerrlab;
1263
#endif
1264
yyerrlab:
1265
    ++yynerrs;
1266
yyinrecovery:
1267
    if (yyerrflag < 3)
1268
    {
1269
        yyerrflag = 3;
1270
        for (;;)
1271
        {
1272
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
1273
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
1274
            {
1275
#if YYDEBUG
1276
                if (yydebug)
1277
                    printf("%sdebug: state %d, error recovery shifting\
1278
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
1279
#endif
1280
                if (yyssp >= yysslim && yygrowstack())
1281
                {
1282
                    goto yyoverflow;
1283
                }
1284
                *++yyssp = yystate = yytable[yyn];
1285
                *++yyvsp = yylval;
1286
                goto yyloop;
1287
            }
1288
            else
1289
            {
1290
#if YYDEBUG
1291
                if (yydebug)
1292
                    printf("%sdebug: error recovery discarding state %d\n",
1293
                            YYPREFIX, *yyssp);
1294
#endif
1295
                if (yyssp <= yyss) goto yyabort;
1296
                --yyssp;
1297
                --yyvsp;
1298
            }
1299
        }
1300
    }
1301
    else
1302
    {
1303
        if (yychar == 0) goto yyabort;
1304
#if YYDEBUG
1305
        if (yydebug)
1306
        {
1307
            yys = 0;
1308
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
1309
            if (!yys) yys = "illegal-symbol";
1310
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
1311
                    YYPREFIX, yystate, yychar, yys);
1312
        }
1313
#endif
1314
        yychar = (-1);
1315
33535
        goto yyloop;
1316
33535
    }
1317
30925
yyreduce:
1318
#if YYDEBUG
1319
2610
    if (yydebug)
1320






















58325
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
1321
                YYPREFIX, yystate, yyn, yyrule[yyn]);
1322
#endif
1323
    yym = yylen[yyn];
1324
    if (yym)
1325
        yyval = yyvsp[1-yym];
1326
    else
1327
        memset(&yyval, 0, sizeof yyval);
1328
    switch (yyn)
1329
    {
1330
case 3:
1331
#line 177 "/usr/src/usr.bin/bc/bc.y"
1332
{
1333
				emit(yyvsp[-1].node, 0);
1334
				macro_char = reset_macro_char;
1335
				putchar('\n');
1336
				free_tree();
1337
				st_has_continue = false;
1338
			}
1339
break;
1340
case 4:
1341
#line 185 "/usr/src/usr.bin/bc/bc.y"
1342
{
1343
				putchar('\n');
1344
				free_tree();
1345
				st_has_continue = false;
1346
			}
1347
break;
1348
case 5:
1349
#line 191 "/usr/src/usr.bin/bc/bc.y"
1350
{
1351
				yyerrok;
1352
			}
1353
break;
1354
case 6:
1355
#line 195 "/usr/src/usr.bin/bc/bc.y"
1356
{
1357
				yyerrok;
1358
			}
1359
break;
1360
case 7:
1361
#line 201 "/usr/src/usr.bin/bc/bc.y"
1362
{
1363
				yyval.node = cs("");
1364
			}
1365
break;
1366
case 9:
1367
#line 206 "/usr/src/usr.bin/bc/bc.y"
1368
{
1369
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, END_NODE);
1370
			}
1371
break;
1372
case 11:
1373
#line 213 "/usr/src/usr.bin/bc/bc.y"
1374
{
1375
				yyval.node = cs("");
1376
			}
1377
break;
1378
case 14:
1379
#line 219 "/usr/src/usr.bin/bc/bc.y"
1380
{
1381
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, END_NODE);
1382
			}
1383
break;
1384
case 16:
1385
#line 224 "/usr/src/usr.bin/bc/bc.y"
1386
{
1387
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, END_NODE);
1388
			}
1389
break;
1390
case 17:
1391
#line 231 "/usr/src/usr.bin/bc/bc.y"
1392
{
1393
				yyval.node = cs("");
1394
			}
1395
break;
1396
case 19:
1397
#line 238 "/usr/src/usr.bin/bc/bc.y"
1398
{
1399
				yyval.node = node(yyvsp[0].node, cs("ps."), END_NODE);
1400
			}
1401
break;
1402
case 20:
1403
#line 242 "/usr/src/usr.bin/bc/bc.y"
1404
{
1405
				if (yyvsp[-1].str[0] == '\0')
1406
					yyval.node = node(yyvsp[0].node, cs(yyvsp[-1].str), yyvsp[-2].lvalue.store,
1407
					    END_NODE);
1408
				else
1409
					yyval.node = node(yyvsp[-2].lvalue.load, yyvsp[0].node, cs(yyvsp[-1].str), yyvsp[-2].lvalue.store,
1410
					    END_NODE);
1411
			}
1412
break;
1413
case 21:
1414
#line 251 "/usr/src/usr.bin/bc/bc.y"
1415
{
1416
				yyval.node = node(cs("["), as(yyvsp[0].str),
1417
				    cs("]P"), END_NODE);
1418
			}
1419
break;
1420
case 22:
1421
#line 256 "/usr/src/usr.bin/bc/bc.y"
1422
{
1423
				if (breaksp == 0) {
1424
					warning("break not in for or while");
1425
					YYERROR;
1426
				} else {
1427
					yyval.node = node(
1428
					    numnode(nesting -
1429
						breakstack[breaksp-1]),
1430
					    cs("Q"), END_NODE);
1431
				}
1432
			}
1433
break;
1434
case 23:
1435
#line 268 "/usr/src/usr.bin/bc/bc.y"
1436
{
1437
				if (breaksp == 0) {
1438
					warning("continue not in for or while");
1439
					YYERROR;
1440
				} else {
1441
					st_has_continue = true;
1442
					yyval.node = node(numnode(nesting -
1443
					    breakstack[breaksp-1] - 1),
1444
					    cs("J"), END_NODE);
1445
				}
1446
			}
1447
break;
1448
case 24:
1449
#line 280 "/usr/src/usr.bin/bc/bc.y"
1450
{
1451
				sigset_t mask;
1452
1453
				putchar('q');
1454
				fflush(stdout);
1455
				if (dc) {
1456
					sigprocmask(SIG_BLOCK, NULL, &mask);
1457
					sigsuspend(&mask);
1458
				} else
1459
					exit(0);
1460
			}
1461
break;
1462
case 25:
1463
#line 292 "/usr/src/usr.bin/bc/bc.y"
1464
{
1465
				if (nesting == 0) {
1466
					warning("return must be in a function");
1467
					YYERROR;
1468
				}
1469
				yyval.node = yyvsp[0].node;
1470
			}
1471
break;
1472
case 26:
1473
#line 302 "/usr/src/usr.bin/bc/bc.y"
1474
{
1475
				ssize_t n;
1476
1477
				if (st_has_continue)
1478
					n = node(yyvsp[-1].node, cs("M"), yyvsp[-3].node, cs("s."),
1479
					    yyvsp[-5].node, yyvsp[-8].node, END_NODE);
1480
				else
1481
					n = node(yyvsp[-1].node, yyvsp[-3].node, cs("s."), yyvsp[-5].node, yyvsp[-8].node,
1482
					    END_NODE);
1483
1484
				emit_macro(yyvsp[-8].node, n);
1485
				yyval.node = node(yyvsp[-7].node, cs("s."), yyvsp[-5].node, yyvsp[-8].node, cs(" "),
1486
				    END_NODE);
1487
			}
1488
break;
1489
case 27:
1490
#line 318 "/usr/src/usr.bin/bc/bc.y"
1491
{
1492
				emit_macro(yyvsp[-4].node, yyvsp[0].node);
1493
				yyval.node = node(yyvsp[-2].node, yyvsp[-4].node, cs(" "), END_NODE);
1494
			}
1495
break;
1496
case 28:
1497
#line 324 "/usr/src/usr.bin/bc/bc.y"
1498
{
1499
				emit_macro(yyvsp[-8].node, yyvsp[-4].node);
1500
				emit_macro(yyvsp[-2].node, yyvsp[0].node);
1501
				yyval.node = node(yyvsp[-6].node, yyvsp[-8].node, cs("e"), yyvsp[-2].node, cs(" "),
1502
				    END_NODE);
1503
			}
1504
break;
1505
case 29:
1506
#line 332 "/usr/src/usr.bin/bc/bc.y"
1507
{
1508
				ssize_t n;
1509
1510
				if (st_has_continue)
1511
					n = node(yyvsp[-1].node, cs("M"), yyvsp[-3].node, yyvsp[-4].node, END_NODE);
1512
				else
1513
					n = node(yyvsp[-1].node, yyvsp[-3].node, yyvsp[-4].node, END_NODE);
1514
				emit_macro(yyvsp[-4].node, n);
1515
				yyval.node = node(yyvsp[-3].node, yyvsp[-4].node, cs(" "), END_NODE);
1516
			}
1517
break;
1518
case 30:
1519
#line 343 "/usr/src/usr.bin/bc/bc.y"
1520
{
1521
				yyval.node = yyvsp[-1].node;
1522
			}
1523
break;
1524
case 31:
1525
#line 347 "/usr/src/usr.bin/bc/bc.y"
1526
{
1527
				yyval.node = yyvsp[0].node;
1528
			}
1529
break;
1530
case 32:
1531
#line 353 "/usr/src/usr.bin/bc/bc.y"
1532
{
1533
				yyval.node = cs(str_table[macro_char]);
1534
				macro_char++;
1535
				/* Do not use [, \ and ] */
1536
				if (macro_char == '[')
1537
					macro_char += 3;
1538
				/* skip letters */
1539
				else if (macro_char == 'a')
1540
					macro_char = '{';
1541
				else if (macro_char == ARRAY_CHAR)
1542
					macro_char += 26;
1543
				else if (macro_char == 255)
1544
					fatal("program too big");
1545
				if (breaksp == BREAKSTACK_SZ)
1546
					fatal("nesting too deep");
1547
				breakstack[breaksp++] = nesting++;
1548
			}
1549
break;
1550
case 33:
1551
#line 373 "/usr/src/usr.bin/bc/bc.y"
1552
{
1553
				breaksp--;
1554
			}
1555
break;
1556
case 34:
1557
#line 381 "/usr/src/usr.bin/bc/bc.y"
1558
{
1559
				int n = node(prologue, yyvsp[-1].node, epilogue,
1560
				    cs("0"), numnode(nesting),
1561
				    cs("Q"), END_NODE);
1562
				emit_macro(yyvsp[-8].node, n);
1563
				reset_macro_char = macro_char;
1564
				nesting = 0;
1565
				breaksp = 0;
1566
			}
1567
break;
1568
case 35:
1569
#line 393 "/usr/src/usr.bin/bc/bc.y"
1570
{
1571
				yyval.node = function_node(yyvsp[-1].astr);
1572
				free(yyvsp[-1].astr);
1573
				prologue = cs("");
1574
				epilogue = cs("");
1575
				nesting = 1;
1576
				breaksp = 0;
1577
				breakstack[breaksp] = 0;
1578
			}
1579
break;
1580
case 40:
1581
#line 415 "/usr/src/usr.bin/bc/bc.y"
1582
{
1583
				add_par(letter_node(yyvsp[0].astr));
1584
				free(yyvsp[0].astr);
1585
			}
1586
break;
1587
case 41:
1588
#line 420 "/usr/src/usr.bin/bc/bc.y"
1589
{
1590
				add_par(array_node(yyvsp[-2].astr));
1591
				free(yyvsp[-2].astr);
1592
			}
1593
break;
1594
case 42:
1595
#line 425 "/usr/src/usr.bin/bc/bc.y"
1596
{
1597
				add_par(letter_node(yyvsp[0].astr));
1598
				free(yyvsp[0].astr);
1599
			}
1600
break;
1601
case 43:
1602
#line 430 "/usr/src/usr.bin/bc/bc.y"
1603
{
1604
				add_par(array_node(yyvsp[-2].astr));
1605
				free(yyvsp[-2].astr);
1606
			}
1607
break;
1608
case 47:
1609
#line 446 "/usr/src/usr.bin/bc/bc.y"
1610
{
1611
				add_local(letter_node(yyvsp[0].astr));
1612
				free(yyvsp[0].astr);
1613
			}
1614
break;
1615
case 48:
1616
#line 451 "/usr/src/usr.bin/bc/bc.y"
1617
{
1618
				add_local(array_node(yyvsp[-2].astr));
1619
				free(yyvsp[-2].astr);
1620
			}
1621
break;
1622
case 49:
1623
#line 456 "/usr/src/usr.bin/bc/bc.y"
1624
{
1625
				add_local(letter_node(yyvsp[0].astr));
1626
				free(yyvsp[0].astr);
1627
			}
1628
break;
1629
case 50:
1630
#line 461 "/usr/src/usr.bin/bc/bc.y"
1631
{
1632
				add_local(array_node(yyvsp[-2].astr));
1633
				free(yyvsp[-2].astr);
1634
			}
1635
break;
1636
case 51:
1637
#line 470 "/usr/src/usr.bin/bc/bc.y"
1638
{
1639
				yyval.node = cs("");
1640
			}
1641
break;
1642
case 54:
1643
#line 479 "/usr/src/usr.bin/bc/bc.y"
1644
{
1645
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, END_NODE);
1646
			}
1647
break;
1648
case 55:
1649
#line 483 "/usr/src/usr.bin/bc/bc.y"
1650
{
1651
				yyval.node = node(yyvsp[-4].node, cs("l"), array_node(yyvsp[-2].astr),
1652
				    END_NODE);
1653
				free(yyvsp[-2].astr);
1654
			}
1655
break;
1656
case 56:
1657
#line 492 "/usr/src/usr.bin/bc/bc.y"
1658
{
1659
				yyval.node = cs(" 0 0=");
1660
			}
1661
break;
1662
case 58:
1663
#line 500 "/usr/src/usr.bin/bc/bc.y"
1664
{
1665
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("="), END_NODE);
1666
			}
1667
break;
1668
case 59:
1669
#line 504 "/usr/src/usr.bin/bc/bc.y"
1670
{
1671
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("!="), END_NODE);
1672
			}
1673
break;
1674
case 60:
1675
#line 508 "/usr/src/usr.bin/bc/bc.y"
1676
{
1677
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs(">"), END_NODE);
1678
			}
1679
break;
1680
case 61:
1681
#line 512 "/usr/src/usr.bin/bc/bc.y"
1682
{
1683
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("!<"), END_NODE);
1684
			}
1685
break;
1686
case 62:
1687
#line 516 "/usr/src/usr.bin/bc/bc.y"
1688
{
1689
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("<"), END_NODE);
1690
			}
1691
break;
1692
case 63:
1693
#line 520 "/usr/src/usr.bin/bc/bc.y"
1694
{
1695
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("!>"), END_NODE);
1696
			}
1697
break;
1698
case 64:
1699
#line 524 "/usr/src/usr.bin/bc/bc.y"
1700
{
1701
				yyval.node = node(yyvsp[0].node, cs(" 0!="), END_NODE);
1702
			}
1703
break;
1704
case 65:
1705
#line 532 "/usr/src/usr.bin/bc/bc.y"
1706
{
1707
				yyval.node = node(cs("0"), epilogue,
1708
				    numnode(nesting), cs("Q"), END_NODE);
1709
			}
1710
break;
1711
case 66:
1712
#line 537 "/usr/src/usr.bin/bc/bc.y"
1713
{
1714
				yyval.node = node(yyvsp[0].node, epilogue,
1715
				    numnode(nesting), cs("Q"), END_NODE);
1716
			}
1717
break;
1718
case 67:
1719
#line 542 "/usr/src/usr.bin/bc/bc.y"
1720
{
1721
				yyval.node = node(cs("0"), epilogue,
1722
				    numnode(nesting), cs("Q"), END_NODE);
1723
			}
1724
break;
1725
case 68:
1726
#line 550 "/usr/src/usr.bin/bc/bc.y"
1727
{
1728
				yyval.node = cs(" 0");
1729
			}
1730
break;
1731
case 70:
1732
#line 557 "/usr/src/usr.bin/bc/bc.y"
1733
{
1734
				yyval.node = node(yyvsp[0].lvalue.load, END_NODE);
1735
			}
1736
break;
1737
case 71:
1738
#line 560 "/usr/src/usr.bin/bc/bc.y"
1739
{
1740
				yyval.node = node(cs("l."), END_NODE);
1741
			}
1742
break;
1743
case 72:
1744
#line 564 "/usr/src/usr.bin/bc/bc.y"
1745
{
1746
				yyval.node = node(cs(" "), as(yyvsp[0].str), END_NODE);
1747
			}
1748
break;
1749
case 73:
1750
#line 568 "/usr/src/usr.bin/bc/bc.y"
1751
{
1752
				yyval.node = yyvsp[-1].node;
1753
			}
1754
break;
1755
case 74:
1756
#line 572 "/usr/src/usr.bin/bc/bc.y"
1757
{
1758
				yyval.node = node(yyvsp[-1].node, cs("l"),
1759
				    function_node(yyvsp[-3].astr), cs("x"),
1760
				    END_NODE);
1761
				free(yyvsp[-3].astr);
1762
			}
1763
break;
1764
case 75:
1765
#line 579 "/usr/src/usr.bin/bc/bc.y"
1766
{
1767
				yyval.node = node(cs(" 0"), yyvsp[0].node, cs("-"),
1768
				    END_NODE);
1769
			}
1770
break;
1771
case 76:
1772
#line 584 "/usr/src/usr.bin/bc/bc.y"
1773
{
1774
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("+"), END_NODE);
1775
			}
1776
break;
1777
case 77:
1778
#line 588 "/usr/src/usr.bin/bc/bc.y"
1779
{
1780
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("-"), END_NODE);
1781
			}
1782
break;
1783
case 78:
1784
#line 592 "/usr/src/usr.bin/bc/bc.y"
1785
{
1786
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("*"), END_NODE);
1787
			}
1788
break;
1789
case 79:
1790
#line 596 "/usr/src/usr.bin/bc/bc.y"
1791
{
1792
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("/"), END_NODE);
1793
			}
1794
break;
1795
case 80:
1796
#line 600 "/usr/src/usr.bin/bc/bc.y"
1797
{
1798
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("%"), END_NODE);
1799
			}
1800
break;
1801
case 81:
1802
#line 604 "/usr/src/usr.bin/bc/bc.y"
1803
{
1804
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("^"), END_NODE);
1805
			}
1806
break;
1807
case 82:
1808
#line 608 "/usr/src/usr.bin/bc/bc.y"
1809
{
1810
				yyval.node = node(yyvsp[0].lvalue.load, cs("1+d"), yyvsp[0].lvalue.store,
1811
				    END_NODE);
1812
			}
1813
break;
1814
case 83:
1815
#line 613 "/usr/src/usr.bin/bc/bc.y"
1816
{
1817
				yyval.node = node(yyvsp[0].lvalue.load, cs("1-d"),
1818
				    yyvsp[0].lvalue.store, END_NODE);
1819
			}
1820
break;
1821
case 84:
1822
#line 618 "/usr/src/usr.bin/bc/bc.y"
1823
{
1824
				yyval.node = node(yyvsp[-1].lvalue.load, cs("d1+"),
1825
				    yyvsp[-1].lvalue.store, END_NODE);
1826
			}
1827
break;
1828
case 85:
1829
#line 623 "/usr/src/usr.bin/bc/bc.y"
1830
{
1831
				yyval.node = node(yyvsp[-1].lvalue.load, cs("d1-"),
1832
				    yyvsp[-1].lvalue.store, END_NODE);
1833
			}
1834
break;
1835
case 86:
1836
#line 628 "/usr/src/usr.bin/bc/bc.y"
1837
{
1838
				if (yyvsp[-1].str[0] == '\0')
1839
					yyval.node = node(yyvsp[0].node, cs(yyvsp[-1].str), cs("d"), yyvsp[-2].lvalue.store,
1840
					    END_NODE);
1841
				else
1842
					yyval.node = node(yyvsp[-2].lvalue.load, yyvsp[0].node, cs(yyvsp[-1].str), cs("d"),
1843
					    yyvsp[-2].lvalue.store, END_NODE);
1844
			}
1845
break;
1846
case 87:
1847
#line 637 "/usr/src/usr.bin/bc/bc.y"
1848
{
1849
				yyval.node = node(yyvsp[-1].node, cs("Z"), END_NODE);
1850
			}
1851
break;
1852
case 88:
1853
#line 641 "/usr/src/usr.bin/bc/bc.y"
1854
{
1855
				yyval.node = node(yyvsp[-1].node, cs("v"), END_NODE);
1856
			}
1857
break;
1858
case 89:
1859
#line 645 "/usr/src/usr.bin/bc/bc.y"
1860
{
1861
				yyval.node = node(yyvsp[-1].node, cs("X"), END_NODE);
1862
			}
1863
break;
1864
case 90:
1865
#line 649 "/usr/src/usr.bin/bc/bc.y"
1866
{
1867
				yyval.node = node(yyvsp[0].node, cs("N"), END_NODE);
1868
			}
1869
break;
1870
case 91:
1871
#line 653 "/usr/src/usr.bin/bc/bc.y"
1872
{
1873
				ssize_t n = node(cs("R"), yyvsp[0].node, END_NODE);
1874
				emit_macro(yyvsp[-2].node, n);
1875
				yyval.node = node(yyvsp[-4].node, cs("d0!="), yyvsp[-2].node, END_NODE);
1876
			}
1877
break;
1878
case 92:
1879
#line 659 "/usr/src/usr.bin/bc/bc.y"
1880
{
1881
				ssize_t n = node(cs("R"), yyvsp[0].node, END_NODE);
1882
				emit_macro(yyvsp[-2].node, n);
1883
				yyval.node = node(yyvsp[-4].node, cs("d0="), yyvsp[-2].node, END_NODE);
1884
			}
1885
break;
1886
case 93:
1887
#line 665 "/usr/src/usr.bin/bc/bc.y"
1888
{
1889
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("G"), END_NODE);
1890
			}
1891
break;
1892
case 94:
1893
#line 669 "/usr/src/usr.bin/bc/bc.y"
1894
{
1895
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("GN"), END_NODE);
1896
			}
1897
break;
1898
case 95:
1899
#line 673 "/usr/src/usr.bin/bc/bc.y"
1900
{
1901
				yyval.node = node(yyvsp[0].node, yyvsp[-2].node, cs("("), END_NODE);
1902
			}
1903
break;
1904
case 96:
1905
#line 677 "/usr/src/usr.bin/bc/bc.y"
1906
{
1907
				yyval.node = node(yyvsp[0].node, yyvsp[-2].node, cs("{"), END_NODE);
1908
			}
1909
break;
1910
case 97:
1911
#line 681 "/usr/src/usr.bin/bc/bc.y"
1912
{
1913
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("("), END_NODE);
1914
			}
1915
break;
1916
case 98:
1917
#line 685 "/usr/src/usr.bin/bc/bc.y"
1918
{
1919
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, cs("{"), END_NODE);
1920
			}
1921
break;
1922
case 99:
1923
#line 692 "/usr/src/usr.bin/bc/bc.y"
1924
{
1925
				yyval.lvalue.load = node(cs("l"), letter_node(yyvsp[0].astr),
1926
				    END_NODE);
1927
				yyval.lvalue.store = node(cs("s"), letter_node(yyvsp[0].astr),
1928
				    END_NODE);
1929
				free(yyvsp[0].astr);
1930
			}
1931
break;
1932
case 100:
1933
#line 700 "/usr/src/usr.bin/bc/bc.y"
1934
{
1935
				yyval.lvalue.load = node(yyvsp[-1].node, cs(";"),
1936
				    array_node(yyvsp[-3].astr), END_NODE);
1937
				yyval.lvalue.store = node(yyvsp[-1].node, cs(":"),
1938
				    array_node(yyvsp[-3].astr), END_NODE);
1939
				free(yyvsp[-3].astr);
1940
			}
1941
break;
1942
case 101:
1943
#line 708 "/usr/src/usr.bin/bc/bc.y"
1944
{
1945
				yyval.lvalue.load = cs("K");
1946
				yyval.lvalue.store = cs("k");
1947
			}
1948
break;
1949
case 102:
1950
#line 713 "/usr/src/usr.bin/bc/bc.y"
1951
{
1952
				yyval.lvalue.load = cs("I");
1953
				yyval.lvalue.store = cs("i");
1954
			}
1955
break;
1956
case 103:
1957
#line 718 "/usr/src/usr.bin/bc/bc.y"
1958
{
1959
				yyval.lvalue.load = cs("O");
1960
				yyval.lvalue.store = cs("o");
1961
			}
1962
break;
1963
case 105:
1964
#line 727 "/usr/src/usr.bin/bc/bc.y"
1965
{
1966
				yyval.node = node(yyvsp[-2].node, yyvsp[0].node, END_NODE);
1967
			}
1968
break;
1969
case 106:
1970
#line 733 "/usr/src/usr.bin/bc/bc.y"
1971
{
1972
				yyval.node = node(yyvsp[0].node, cs("ds.n"), END_NODE);
1973
			}
1974
break;
1975
case 107:
1976
#line 737 "/usr/src/usr.bin/bc/bc.y"
1977
33445
{
1978
33445
				char *p = escape(yyvsp[0].str);
1979
33445
				yyval.node = node(cs("["), as(p), cs("]n"), END_NODE);
1980
33445
				free(p);
1981
33445
			}
1982
break;
1983
#line 1976 "bc.c"
1984
    }
1985
    yyssp -= yym;
1986
    yystate = *yyssp;
1987
    yyvsp -= yym;
1988
    yym = yylhs[yyn];
1989
3610
    if (yystate == 0 && yym == 0)
1990
3610
    {
1991
3610
#if YYDEBUG
1992
        if (yydebug)
1993
3610
            printf("%sdebug: after reduction, shifting from state 0 to\
1994
 state %d\n", YYPREFIX, YYFINAL);
1995
#endif
1996
        yystate = YYFINAL;
1997
        *++yyssp = YYFINAL;
1998
        *++yyvsp = yyval;
1999
        if (yychar < 0)
2000
        {
2001
            if ((yychar = yylex()) < 0) yychar = 0;
2002
#if YYDEBUG
2003
            if (yydebug)
2004
3610
            {
2005
3610
                yys = 0;
2006
3610
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
2007
                if (!yys) yys = "illegal-symbol";
2008

93065
                printf("%sdebug: state %d, reading %d (%s)\n",
2009
41630
                        YYPREFIX, YYFINAL, yychar, yys);
2010
16290
            }
2011
#endif
2012
13545
        }
2013
        if (yychar == 0) goto yyaccept;
2014
        goto yyloop;
2015
    }
2016
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
2017
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
2018

29835
        yystate = yytable[yyn];
2019
    else
2020
        yystate = yydgoto[yym];
2021
#if YYDEBUG
2022
29835
    if (yydebug)
2023
29835
        printf("%sdebug: after reduction, shifting from state %d \
2024
29835
to state %d\n", YYPREFIX, *yyssp, yystate);
2025
#endif
2026
    if (yyssp >= yysslim && yygrowstack())
2027
    {
2028
        goto yyoverflow;
2029
    }
2030
    *++yyssp = yystate;
2031
    *++yyvsp = yyval;
2032
    goto yyloop;
2033
yyoverflow:
2034
    yyerror("yacc stack overflow");
2035
yyabort:
2036
    if (yyss)
2037
            free(yyss);
2038
    if (yyvs)
2039
            free(yyvs);
2040
    yyss = yyssp = NULL;
2041
    yyvs = yyvsp = NULL;
2042
    yystacksize = 0;
2043
    return (1);
2044
yyaccept:
2045
    if (yyss)
2046
            free(yyss);
2047
    if (yyvs)
2048
            free(yyvs);
2049
    yyss = yyssp = NULL;
2050
    yyvs = yyvsp = NULL;
2051
    yystacksize = 0;
2052
    return (0);
2053
}