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"
43 #include "have_string.h"
54 BOOL allow_custom
= FALSE
; /* TRUE => custom builtins allowed */
58 * custom - custom callout function
62 custom(char *name
, int count
, VALUE
**vals
)
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) {
77 if (count
< p
->minargs
) {
78 math_error("Too few arguments for custom "
79 "function \"%s\"", p
->name
);
82 if (count
> p
->maxargs
) {
83 math_error("Too many arguments for custom "
84 "function \"%s\"", p
->name
);
88 /* call the custom function */
89 return p
->func(name
, count
, vals
);
94 * no such custom function
96 return error_value(E_UNK_CUSTOM
);
101 "%sCalc was built with custom functions disabled\n",
102 (conf
->tab_ok
? "\t" : ""));
103 if (conf
->calc_debug
& CALCDBG_CUSTOM
) {
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
);
116 * showcustom - display the names and brief descriptins of custom functions
124 CONST
struct custom
*p
; /* current function */
127 * disable custom functions unless -C was given
131 "%sCalc must be run with a -C argument to "
132 "show custom functions\n",
133 (conf
->tab_ok
? "\t" : ""));
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
);
148 printf("%d-%-4d", p
->minargs
, p
->maxargs
);
149 printf("%s\n", p
->desc
);
156 "%sCalc was built with custom functions disabled\n",
157 (conf
->tab_ok
? "\t" : ""));
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.
178 * name name of the custom help file to directly access
182 customhelp(char *name
)
186 char *customname
; /* a string of the form: custom/name */
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");
203 sprintf(customname
, "custhelp/%s", name
);
206 * give help directly to the custom file
208 givehelp(customname
);
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
));