1 /* main.c: The main program for bc. */
3 /* This file is part of GNU bc.
4 Copyright (C) 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License , or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 The Free Software Foundation, Inc.
19 59 Temple Place, Suite 330
22 You may contact the author by:
23 e-mail: philnelson@acm.org
24 us-mail: Philip A. Nelson
25 Computer Science Department, 9062
26 Western Washington University
27 Bellingham, WA 98226-9062
31 *************************************************************************/
40 /* Variables for processing multiple files. */
41 static char first_file;
43 /* Points to the last node in the file name list for easy adding. */
44 static file_node *last = NULL;
46 /* long option support */
47 static struct option long_options[] =
49 {"compile", 0, &compile_only, TRUE},
51 {"interactive", 0, 0, 'i'},
52 {"mathlib", 0, &use_math, TRUE},
53 {"quiet", 0, &quiet, TRUE},
54 {"standard", 0, &std_only, TRUE},
55 {"version", 0, 0, 'v'},
56 {"warn", 0, &warn_not_std, TRUE},
63 usage (char *progname)
65 printf ("usage: %s [options] [file ...]\n%s%s%s%s%s%s%s", progname,
66 " -h --help print this usage and exit\n",
67 " -i --interactive force interactive mode\n",
68 " -l --mathlib use the predefined math routines\n",
69 " -q --quiet don't print initial banner\n",
70 " -s --standard non-standard bc constructs are errors\n",
71 " -w --warn warn about non-standard bc constructs\n",
72 " -v --version print version information and exit\n");
77 parse_args (argc, argv)
85 /* Force getopt to initialize. Depends on GNU getopt. */
88 /* Parse the command line */
91 optch = getopt_long (argc, argv, "chilqswv", long_options, &long_index);
93 if (optch == EOF) /* End of arguments. */
98 case 'c': /* compile only */
107 case 'i': /* force interactive */
111 case 'l': /* math lib */
115 case 'q': /* quiet mode */
119 case 's': /* Non standard features give errors. */
123 case 'v': /* Print the version. */
128 case 'w': /* Non standard features give warnings. */
142 /* Add file names to a list of files to process. */
143 while (optind < argc)
145 temp = (file_node *) bc_malloc(sizeof(file_node));
146 temp->name = argv[optind];
157 /* The main program for bc. */
167 /* Initialize many variables. */
168 compile_only = FALSE;
170 warn_not_std = FALSE;
172 if (isatty(0) && isatty(1))
180 /* attempt to simplify interaction with applications such as emacs */
181 (void) setvbuf(stdout, NULL, _IOLBF, 0);
184 /* Environment arguments. */
185 env_value = getenv ("BC_ENV_ARGS");
186 if (env_value != NULL)
189 env_argv[0] = "BC_ENV_ARGS";
190 while (*env_value != 0)
192 if (*env_value != ' ')
194 env_argv[env_argc++] = env_value;
195 while (*env_value != ' ' && *env_value != 0)
206 parse_args (env_argc, env_argv);
209 /* Command line arguments. */
210 parse_args (argc, argv);
212 /* Other environment processing. */
213 if (getenv ("POSIXLY_CORRECT") != NULL)
216 env_value = getenv ("BC_LINE_LENGTH");
217 if (env_value != NULL)
219 line_size = atoi (env_value);
226 /* Initialize the machine. */
230 /* Set up interrupts to print a message. */
232 signal (SIGINT, use_quit);
234 /* Initialize the front end. */
239 if (!open_new_file ())
244 /* Enable libedit support. */
245 edit = el_init ("bc", stdin, stdout, stderr);
246 hist = history_init();
247 el_set (edit, EL_EDITOR, "emacs");
248 el_set (edit, EL_HIST, history, hist);
249 el_set (edit, EL_PROMPT, null_prompt);
250 el_source (edit, NULL);
251 history (hist, &histev, H_SETSIZE, INT_MAX);
255 #if defined(READLINE)
257 /* Readline support. Set both application name and input file. */
258 rl_readline_name = "bc";
267 /* End the compile only output with a newline. */
275 /* This is the function that opens all the files.
276 It returns TRUE if the file was opened, otherwise
285 /* Set the line number. */
288 /* Check to see if we are done. */
289 if (is_std_in) return (FALSE);
291 /* Open the other files. */
292 if (use_math && first_file)
294 /* Load the code from a precompiled version of the math libarary. */
295 extern char *libmath[];
298 /* These MUST be in the order of first mention of each function.
299 That is why "a" comes before "c" even though "a" is defined after
300 after "c". "a" is used in "s"! */
301 tmp = lookup ("e", FUNCT);
302 tmp = lookup ("l", FUNCT);
303 tmp = lookup ("s", FUNCT);
304 tmp = lookup ("a", FUNCT);
305 tmp = lookup ("c", FUNCT);
306 tmp = lookup ("j", FUNCT);
314 /* One of the argv values. */
315 if (file_names != NULL)
317 new_file = fopen (file_names->name, "r");
318 if (new_file != NULL)
320 new_yy_file (new_file);
322 file_name = temp->name;
323 file_names = temp->next;
327 fprintf (stderr, "File %s is unavailable.\n", file_names->name);
331 /* If we fall through to here, we should return stdin. */
338 /* Set yyin to the new file. */
344 if (!first_file) fclose (yyin);
350 /* Message to use quit. */
356 printf ("\n(interrupt) use quit to exit.\n");
357 signal (SIGINT, use_quit);