Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 18 | * |
| 19 | * The entry point for bc. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Gavin Howard | 3e7cf05 | 2018-01-27 00:50:45 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Gavin Howard | bc9fa77 | 2018-01-24 11:49:33 -0700 | [diff] [blame] | 27 | #include <locale.h> |
| 28 | |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 29 | #include <getopt.h> |
| 30 | |
Gavin Howard | 13ccc4a | 2018-02-15 14:09:41 -0700 | [diff] [blame] | 31 | #include <bc.h> |
Gavin Howard | dec399f | 2018-01-23 12:09:11 -0700 | [diff] [blame] | 32 | |
Gavin Howard | 07add2e | 2018-03-10 19:23:41 -0700 | [diff] [blame] | 33 | static const struct option bc_opts[] = { |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 34 | |
Gavin Howard | 914030d | 2018-02-08 20:05:19 -0700 | [diff] [blame] | 35 | { "code", no_argument, NULL, 'c' }, |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 36 | { "help", no_argument, NULL, 'h' }, |
| 37 | { "interactive", no_argument, NULL, 'i' }, |
| 38 | { "mathlib", no_argument, NULL, 'l' }, |
| 39 | { "quiet", no_argument, NULL, 'q' }, |
| 40 | { "standard", no_argument, NULL, 's' }, |
| 41 | { "version", no_argument, NULL, 'v' }, |
| 42 | { "warn", no_argument, NULL, 'w' }, |
| 43 | { 0, 0, 0, 0 }, |
| 44 | |
| 45 | }; |
| 46 | |
Gavin Howard | 07add2e | 2018-03-10 19:23:41 -0700 | [diff] [blame] | 47 | static const char *bc_short_opts = "chilqsvw"; |
Gavin Howard | 3e7cf05 | 2018-01-27 00:50:45 -0700 | [diff] [blame] | 48 | |
Gavin Howard | 07add2e | 2018-03-10 19:23:41 -0700 | [diff] [blame] | 49 | static const char *bc_help = |
Gavin Howard | f03f34c | 2018-02-20 13:46:46 -0700 | [diff] [blame] | 50 | "usage: bc [-hilqsvw] [long-options] [file...]\n" |
Gavin Howard | 3e7cf05 | 2018-01-27 00:50:45 -0700 | [diff] [blame] | 51 | "\n" |
| 52 | "bc is a command-line calculator with a Turing-complete language.\n" |
| 53 | "\n" |
Gavin Howard | 91072cf | 2018-02-08 21:07:51 -0700 | [diff] [blame] | 54 | " -c --code print bytecode instead of executing it\n" |
Gavin Howard | 3e7cf05 | 2018-01-27 00:50:45 -0700 | [diff] [blame] | 55 | " -h --help print this usage message and exit\n" |
| 56 | " -i --interactive force interactive mode\n" |
| 57 | " -l --mathlib use predefined math routines:\n\n" |
| 58 | " s(expr) = sine of expr in radians\n" |
| 59 | " c(expr) = cosine of expr in radians\n" |
| 60 | " a(expr) = arctangent of expr, returning radians\n" |
| 61 | " l(expr) = natural log of expr\n" |
| 62 | " e(expr) = raises e to the power of expr\n" |
| 63 | " j(n, x) = Bessel function of integer order n of x\n" |
| 64 | "\n" |
| 65 | " -q --quiet don't print version and copyright\n" |
| 66 | " -s --standard error if any non-POSIX extensions are used\n" |
| 67 | " -w --warn warn if any non-POSIX extensions are used\n" |
| 68 | " -v --version print version information and copyright and exit\n\n"; |
| 69 | |
Gavin Howard | 12ca470 | 2018-03-09 19:27:11 -0700 | [diff] [blame] | 70 | BcGlobals bcg; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 71 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 72 | int main(int argc, char *argv[]) { |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 73 | |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 74 | BcStatus status; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 75 | unsigned int flags; |
| 76 | unsigned int filec; |
Gavin Howard | b5f1cb1 | 2018-03-13 12:39:52 -0600 | [diff] [blame] | 77 | char** filev; |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 78 | bool do_exit; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 79 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame] | 80 | setlocale(LC_ALL, ""); |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 81 | |
Gavin Howard | 12ca470 | 2018-03-09 19:27:11 -0700 | [diff] [blame] | 82 | memset(&bcg, 0, sizeof(BcGlobals)); |
Gavin Howard | 729f764 | 2018-03-09 02:58:44 -0700 | [diff] [blame] | 83 | |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 84 | do_exit = false; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 85 | flags = 0; |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 86 | status = BC_STATUS_SUCCESS; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 87 | |
| 88 | // Getopt needs this. |
| 89 | int opt_idx = 0; |
| 90 | |
| 91 | int c = getopt_long(argc, argv, bc_short_opts, bc_opts, &opt_idx); |
| 92 | |
| 93 | while (c != -1) { |
| 94 | |
| 95 | switch (c) { |
| 96 | |
| 97 | case 0: |
| 98 | { |
| 99 | // This is the case when a long option is |
| 100 | // found, so we don't need to do anything. |
| 101 | break; |
| 102 | } |
| 103 | |
Gavin Howard | 914030d | 2018-02-08 20:05:19 -0700 | [diff] [blame] | 104 | case 'c': |
| 105 | { |
Gavin Howard | ee11033 | 2018-02-08 20:52:41 -0700 | [diff] [blame] | 106 | flags |= BC_FLAG_CODE; |
| 107 | break; |
Gavin Howard | 914030d | 2018-02-08 20:05:19 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 110 | case 'h': |
| 111 | { |
Gavin Howard | d8a2b50 | 2018-02-24 13:26:48 -0700 | [diff] [blame] | 112 | if (printf("%s", bc_help) < 0) return BC_STATUS_IO_ERR; |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 113 | do_exit = true; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
| 117 | case 'i': |
| 118 | { |
| 119 | flags |= BC_FLAG_INTERACTIVE; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | case 'l': |
| 124 | { |
| 125 | flags |= BC_FLAG_MATHLIB; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | case 'q': |
| 130 | { |
| 131 | flags |= BC_FLAG_QUIET; |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | case 's': |
| 136 | { |
| 137 | flags |= BC_FLAG_STANDARD; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | case 'v': |
| 142 | { |
Gavin Howard | a280bde | 2018-03-13 13:49:12 -0600 | [diff] [blame] | 143 | if (printf("%s", bc_header) < 0) return BC_STATUS_IO_ERR; |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 144 | do_exit = true; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | |
| 148 | case 'w': |
| 149 | { |
| 150 | flags |= BC_FLAG_WARN; |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | // Getopt printed an error message, but we should exit. |
| 155 | case '?': |
| 156 | default: |
| 157 | { |
| 158 | return BC_STATUS_INVALID_OPTION; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | c = getopt_long(argc, argv, bc_short_opts, bc_opts, &opt_idx); |
| 164 | } |
| 165 | |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 166 | if (do_exit) return status; |
Gavin Howard | 3e7cf05 | 2018-01-27 00:50:45 -0700 | [diff] [blame] | 167 | |
Gavin Howard | c5e491b | 2018-02-20 13:51:20 -0700 | [diff] [blame] | 168 | if (argv[optind] && strcmp(argv[optind], "--") == 0) ++optind; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 169 | |
| 170 | filec = argc - optind; |
Gavin Howard | b5f1cb1 | 2018-03-13 12:39:52 -0600 | [diff] [blame] | 171 | filev = argv + optind; |
Gavin Howard | dce6dc9 | 2018-01-26 12:08:43 -0700 | [diff] [blame] | 172 | |
Gavin Howard | f1ab44f | 2018-03-13 16:15:38 -0600 | [diff] [blame^] | 173 | return bc_main(flags, filec, filev); |
Gavin Howard | dec399f | 2018-01-23 12:09:11 -0700 | [diff] [blame] | 174 | } |