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 predefine math routnes\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. */
138 /* Add file names to a list of files to process. */
139 while (optind < argc)
141 temp = (file_node *) bc_malloc(sizeof(file_node));
142 temp->name = argv[optind];
153 /* The main program for bc. */
163 /* Initialize many variables. */
164 compile_only = FALSE;
166 warn_not_std = FALSE;
168 if (isatty(0) && isatty(1))
176 /* attempt to simplify interaction with applications such as emacs */
177 (void) setvbuf(stdout, NULL, _IOLBF, 0);
180 /* Environment arguments. */
181 env_value = getenv ("BC_ENV_ARGS");
182 if (env_value != NULL)
185 env_argv[0] = "BC_ENV_ARGS";
186 while (*env_value != 0)
188 if (*env_value != ' ')
190 env_argv[env_argc++] = env_value;
191 while (*env_value != ' ' && *env_value != 0)
202 parse_args (env_argc, env_argv);
205 /* Command line arguments. */
206 parse_args (argc, argv);
208 /* Other environment processing. */
209 if (getenv ("POSIXLY_CORRECT") != NULL)
212 env_value = getenv ("BC_LINE_LENGTH");
213 if (env_value != NULL)
215 line_size = atoi (env_value);
222 /* Initialize the machine. */
226 /* Set up interrupts to print a message. */
228 signal (SIGINT, use_quit);
230 /* Initialize the front end. */
235 if (!open_new_file ())
240 /* Enable libedit support. */
241 edit = el_init ("bc", stdin, stdout, stderr);
242 hist = history_init();
243 el_set (edit, EL_EDITOR, "emacs");
244 el_set (edit, EL_HIST, history, hist);
245 el_set (edit, EL_PROMPT, null_prompt);
246 el_source (edit, NULL);
247 history (hist, &histev, H_SETSIZE, INT_MAX);
251 #if defined(READLINE)
253 /* Readline support. Set both application name and input file. */
254 rl_readline_name = "bc";
263 /* End the compile only output with a newline. */
271 /* This is the function that opens all the files.
272 It returns TRUE if the file was opened, otherwise
281 /* Set the line number. */
284 /* Check to see if we are done. */
285 if (is_std_in) return (FALSE);
287 /* Open the other files. */
288 if (use_math && first_file)
290 /* Load the code from a precompiled version of the math libarary. */
291 extern char *libmath[];
294 /* These MUST be in the order of first mention of each function.
295 That is why "a" comes before "c" even though "a" is defined after
296 after "c". "a" is used in "s"! */
297 tmp = lookup ("e", FUNCT);
298 tmp = lookup ("l", FUNCT);
299 tmp = lookup ("s", FUNCT);
300 tmp = lookup ("a", FUNCT);
301 tmp = lookup ("c", FUNCT);
302 tmp = lookup ("j", FUNCT);
310 /* One of the argv values. */
311 if (file_names != NULL)
313 new_file = fopen (file_names->name, "r");
314 if (new_file != NULL)
316 new_yy_file (new_file);
318 file_name = temp->name;
319 file_names = temp->next;
323 fprintf (stderr, "File %s is unavailable.\n", file_names->name);
327 /* If we fall through to here, we should return stdin. */
334 /* Set yyin to the new file. */
340 if (!first_file) fclose (yyin);
346 /* Message to use quit. */
352 printf ("\n(interrupt) use quit to exit.\n");
353 signal (SIGINT, use_quit);