modified: diffout.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / custom.c
blob05d0af639565e5035c63d7318207523e99c71a0e
1 /*
2 * custom - interface for custom software and hardware interfaces
4 * Copyright (C) 1999-2006 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: custom.c,v 30.2 2007/09/21 01:27:27 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/custom.c,v $
24 * Under source code control: 1997/03/03 04:53:08
25 * File existed as early as: 1997
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 /* these include files are needed regardless of CUSTOM */
33 #include "have_const.h"
34 #include "value.h"
35 #include "custom.h"
37 #include <stdio.h>
39 #if defined(CUSTOM)
41 #include "calc.h"
43 #include "have_string.h"
44 #ifdef HAVE_STRING_H
45 # include <string.h>
46 #endif
48 #else /* CUSTOM */
50 #include "config.h"
52 #endif /* CUSTOM */
54 BOOL allow_custom = FALSE; /* TRUE => custom builtins allowed */
58 * custom - custom callout function
60 /*ARGSUSED*/
61 VALUE
62 custom(char *name, int count, VALUE **vals)
64 #if defined(CUSTOM)
66 CONST struct custom *p; /* current function */
69 * search the custom interface table for a function
71 for (p = cust; p->name != NULL; ++p) {
73 /* look for the first match */
74 if (strcmp(name, p->name) == 0) {
76 /* arg count check */
77 if (count < p->minargs) {
78 math_error("Too few arguments for custom "
79 "function \"%s\"", p->name);
80 /*NOTREACHED*/
82 if (count > p->maxargs) {
83 math_error("Too many arguments for custom "
84 "function \"%s\"", p->name);
85 /*NOTREACHED*/
88 /* call the custom function */
89 return p->func(name, count, vals);
94 * no such custom function
96 return error_value(E_UNK_CUSTOM);
98 #else /* CUSTOM */
100 fprintf(stderr,
101 "%sCalc was built with custom functions disabled\n",
102 (conf->tab_ok ? "\t" : ""));
103 if (conf->calc_debug & CALCDBG_CUSTOM) {
104 fprintf(stderr,
105 "%scustom function %s with %d args, %s vals not executed\n",
106 (conf->tab_ok ? "\t" : ""), name, count,
107 (vals == NULL) ? "NULL" : "non-NULL");
109 return error_value(E_NO_CUSTOM);
111 #endif /* CUSTOM */
116 * showcustom - display the names and brief descriptins of custom functions
118 /*ARGSUSED*/
119 void
120 showcustom(void)
122 #if defined(CUSTOM)
124 CONST struct custom *p; /* current function */
127 * disable custom functions unless -C was given
129 if (!allow_custom) {
130 fprintf(stderr,
131 "%sCalc must be run with a -C argument to "
132 "show custom functions\n",
133 (conf->tab_ok ? "\t" : ""));
134 return;
138 * print header
140 printf("\nName\tArgs\tDescription\n\n");
141 for (p = cust; p->name != NULL; ++p) {
142 printf("%-9s ", p->name);
143 if (p->maxargs == MAX_CUSTOM_ARGS)
144 printf("%d+ ", p->minargs);
145 else if (p->minargs == p->maxargs)
146 printf("%-6d", p->minargs);
147 else
148 printf("%d-%-4d", p->minargs, p->maxargs);
149 printf("%s\n", p->desc);
151 printf("\n");
153 #else /* CUSTOM */
155 fprintf(stderr,
156 "%sCalc was built with custom functions disabled\n",
157 (conf->tab_ok ? "\t" : ""));
159 #endif /* CUSTOM */
164 * customhelp - standard help interface to a custom function
166 * This function assumes that a help file with the same name as
167 * the custom function has been installed by the custom/Makefile
168 * (as listed in the CUSTOM_HELP makefile variable) under the
169 * CUSTOMHELPDIR == HELPDIR/custhelp directory.
171 * The help command first does a search in HELPDIR and later
172 * in CUSTOMHELPDIR. If a custom help file has the same name
173 * as a file under HELPDIR then help will display the HELPDIR
174 * file and NOT the custom file. This function will ignore
175 * and HELPDIR file and work directly with the custom help file.
177 * given:
178 * name name of the custom help file to directly access
180 /*ARGSUSED*/
181 void
182 customhelp(char *name)
184 #if defined(CUSTOM)
186 char *customname; /* a string of the form: custom/name */
189 * firewall
191 if (name == NULL) {
192 name = "help";
196 * form the custom help name
198 customname = (char *)malloc(sizeof("custhelp")+1+strlen(name)+1);
199 if (customname == NULL) {
200 math_error("bad malloc of customname");
201 /*NOTREACHED*/
203 sprintf(customname, "custhelp/%s", name);
206 * give help directly to the custom file
208 givehelp(customname);
211 * all done
213 free(customname);
215 #else /* CUSTOM */
217 fprintf(stderr,
218 "%sCalc was built with custom functions disabled\n",
219 (conf->tab_ok ? "\t" : ""));
220 if (conf->calc_debug & CALCDBG_CUSTOM) {
221 fprintf(stderr, "%scustom help for %s unavailable\n",
222 (conf->tab_ok ? "\t" : ""),
223 ((name == NULL) ? "((NULL))" : name));
226 #endif /* CUSTOM */