wcalc  2.5
About: Wcalc is a natural-expression command-line calculator.
  Fossies Dox: wcalc-2.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

explain.c
Go to the documentation of this file.
1 /*
2  * stdin
3  * Copyright (c) 2013 Micron Technology, Inc.
4  *
5  */
6 #ifdef HAVE_CONFIG_H
7 # include "config.h"
8 #endif
9 
10 /* System Headers */
11 #include <string.h>
12 #include <ctype.h> /* for isspace() */
13 
14 /* Internal Headers */
15 #include "number.h"
16 #include "uint32_max.h"
17 #include "variables.h"
18 #include "calculator.h"
19 #include "list.h"
20 #include "extract_vars.h"
21 #include "evalvar.h"
22 #include "iscmd.h"
23 #include "isconst.h"
24 #include "isfunc.h"
25 #include "explain.h"
26 #include "output.h"
27 
28 static int explain_command(const char *,
29  int);
30 static void explain_variable(const char *);
31 static void explain_constant(const char *);
32 static void explain_function(const char *);
33 
34 void explain(const char *str)
35 { /*{{{ */
36  size_t curs;
37  char *mystr;
38 
39  if ((str == NULL) || (str[0] == 0)) {
40  str = "\\explain";
41  }
42  mystr = strdup(str);
43  /* for sanity's sake, remove any trailing whitespace */
44  curs = strlen(mystr) - 1;
45  while (isspace(mystr[curs]) && curs > 0) {
46  mystr[curs] = 0;
47  curs--;
48  }
49  if (!strcmp(mystr, "q")) {
50  display_explanation("Quits the program.");
51  } else if (!strcmp(mystr, "constants")) {
52  display_explanation("Wcalc understands many standard physical constants.\n"
53  "Here is the list of the ones currently understood;\n"
54  "each can be explained in detail individually (some\n"
55  "mean the same thing).\n");
57  } else if (*mystr == '\\') { // it's a command
58  explain_command(mystr, 0);
59  } else if (isconst(mystr)) { // it's a constant
60  explain_constant(mystr);
61  } else if (isfunc(mystr)) { // it's a function
62  explain_function(mystr);
63  } else if (varexists(mystr)) { // it's a variable
64  explain_variable(mystr);
65  } else { // it's a call for help
66  int len = strlen(mystr) + 2;
67  char *add_a_slash = calloc(len, sizeof(char));
68 
69  add_a_slash[0] = '\\';
70  add_a_slash[1] = 0;
71  strncat(add_a_slash, mystr, len - 2);
72  if (explain_command(add_a_slash, 1) == -1) {
73  report_error("%s is neither a command, constant, function, or variable.\n", mystr);
74  }
75  free(add_a_slash);
76  }
77  free(mystr);
78 } /*}}} */
79 
80 static inline int scan_exps(const char *str,
81  const struct name_with_exp *exps)
82 {
83  const struct name_with_exp *curs;
84 
85  for (curs = exps; curs->explanation; curs++) {
86  const char * const * const names = curs->names;
87  for (unsigned i = 0; names[i]; i++) {
88  if (!strcmp(str, names[i])) {
90  return 0;
91  }
92  }
93  }
94  return -1;
95 }
96 
97 static int explain_command(const char *str,
98  int test)
99 { /*{{{ */
100  str++;
101  if (!strcmp(str, "bits")) {
102  display_explanation("Sets the number of bits used internally to represent numbers. Used like this: \\bitsX where X is a number that must be above %li and below %li.",
103  (long int)NUM_PREC_MIN, (long int)NUM_PREC_MAX);
104  return 0;
105  } else {
106  if (scan_exps(str, commands) == 0) {
107  return 0;
108  }
109  }
110  if (test == 0) {
111  report_error("Undefined command.");
112  }
113  return -1;
114 } /*}}} */
115 
116 static void explain_variable(const char *str)
117 { /*{{{ */
118  struct answer var;
119 
120  var = getvar_full(str);
121  if (var.err) {
122  report_error("An error was encountered!\n");
123  return;
124  }
125  if (var.exp) { // it's an expression (i.e. a function)
126  List strings;
127 
128  strings = extract_vars(var.exp);
129  display_expvar_explanation(str, var.exp, strings, var.desc);
130  freeList(&strings);
131  } else {
132  display_valvar_explanation(str, &var.val, var.desc);
133  }
134 } /*}}} */
135 
136 static void explain_constant(const char *str)
137 { /*{{{ */
138  (void)scan_exps(str, consts);
139 } /*}}} */
140 
141 static void explain_function(const char *str)
142 { /*{{{ */
143  (void)scan_exps(str, funcs);
144 } /*}}} */
145 
146 /* vim:set expandtab: */
name_with_exp::explanation
const char * explanation
Definition: explain.h:6
explain.h
explain_constant
static void explain_constant(const char *)
Definition: explain.c:136
scan_exps
static int scan_exps(const char *str, const struct name_with_exp *exps)
Definition: explain.c:80
NUM_PREC_MAX
#define NUM_PREC_MAX
Definition: number.h:25
uint32_max.h
name_with_exp::names
const char * names[10]
Definition: explain.h:5
isfunc
int isfunc(const char *str)
Definition: isfunc.c:88
display_expvar_explanation
void display_expvar_explanation(const char *str, const char *exp, List subvars, const char *desc)
Definition: main.c:1587
calculator.h
isfunc.h
free
void free(void *)
isconst.h
extract_vars
List extract_vars(char *str)
Definition: extract_vars.c:16
variables.h
explain_variable
static void explain_variable(const char *)
Definition: explain.c:116
_list
Definition: list.c:19
explain_function
static void explain_function(const char *)
Definition: explain.c:141
answer
Definition: variables.h:29
explain_command
static int explain_command(const char *, int)
Definition: explain.c:97
display_consts
void display_consts(void)
Definition: main.c:1665
commands
commands
Definition: calculator.h:100
name_with_exp
Definition: explain.h:4
display_explanation
void display_explanation(const char *exp,...)
Definition: main.c:1644
evalvar.h
explain
void explain(const char *str)
Definition: explain.c:34
getvar_full
struct answer getvar_full(const char *key)
Definition: variables.c:160
consts
const struct name_with_exp consts[]
Definition: isconst.c:13
display_valvar_explanation
void display_valvar_explanation(const char *str, Number *val, const char *desc)
Definition: main.c:1631
iscmd.h
freeList
void freeList(List *)
Definition: list.c:135
varexists
int varexists(const char *key)
Definition: variables.c:184
isconst
int isconst(const char *str)
Definition: isconst.c:122
NUM_PREC_MIN
#define NUM_PREC_MIN
Definition: number.h:24
extract_vars.h
report_error
void report_error(const char *err_fmt,...)
Definition: calculator.c:541
list.h
number.h
funcs
const struct name_with_exp funcs[]
Definition: isfunc.c:11
output.h