1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 #  include "config.h"
28 #endif
29 
30 #include <string>
31 
32 #include "defun.h"
33 #include "dynamic-ld.h"
34 #include "error.h"
35 #include "help.h"
36 #include "ov.h"
37 #include "ov-builtin.h"
38 #include "ov-dld-fcn.h"
39 #include "ov-fcn.h"
40 #include "ov-mex-fcn.h"
41 #include "ov-usr-fcn.h"
42 #include "ovl.h"
43 #include "oct-lvalue.h"
44 #include "pager.h"
45 #include "interpreter-private.h"
46 #include "interpreter.h"
47 #include "symtab.h"
48 #include "variables.h"
49 #include "parse.h"
50 
51 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
52 void
print_usage(void)53 print_usage (void)
54 {
55   octave::tree_evaluator& tw = octave::__get_evaluator__ ("print_usage");
56 
57   const octave_function *cur = tw.current_function ();
58 
59   if (cur)
60     print_usage (cur->name ());
61   else
62     error ("print_usage: invalid function");
63 }
64 
65 void
print_usage(const std::string & name)66 print_usage (const std::string& name)
67 {
68   octave::feval ("print_usage", octave_value (name), 0);
69 }
70 
71 void
check_version(const std::string & version,const std::string & fcn)72 check_version (const std::string& version, const std::string& fcn)
73 {
74   if (version != OCTAVE_API_VERSION)
75     {
76       error ("API version %s found in .oct file function '%s'\n"
77              "       does not match the running Octave (API version %s)\n"
78              "       this can lead to incorrect results or other failures\n"
79              "       you can fix this problem by recompiling this .oct file",
80              version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
81     }
82 }
83 
84 // Install variables and functions in the symbol tables.
85 
86 void
install_dld_function(octave_dld_function::fcn f,const std::string & name,const octave::dynamic_library & shl,const std::string & doc,bool relative)87 install_dld_function (octave_dld_function::fcn f, const std::string& name,
88                       const octave::dynamic_library& shl, const std::string& doc,
89                       bool relative)
90 {
91   octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
92 
93   if (relative)
94     fcn->mark_relative ();
95 
96   octave_value fval (fcn);
97 
98   octave::symbol_table& symtab
99     = octave::__get_symbol_table__ ("install_dld_function");
100 
101   symtab.install_built_in_function (name, fval);
102 }
103 
104 void
install_dld_function(octave_dld_function::meth m,const std::string & name,const octave::dynamic_library & shl,const std::string & doc,bool relative)105 install_dld_function (octave_dld_function::meth m, const std::string& name,
106                       const octave::dynamic_library& shl, const std::string& doc,
107                       bool relative)
108 {
109   octave_dld_function *fcn = new octave_dld_function (m, shl, name, doc);
110 
111   if (relative)
112     fcn->mark_relative ();
113 
114   octave_value fval (fcn);
115 
116   octave::symbol_table& symtab
117     = octave::__get_symbol_table__ ("install_dld_function");
118 
119   symtab.install_built_in_function (name, fval);
120 }
121 
122 void
install_mex_function(void * fptr,bool fmex,const std::string & name,const octave::dynamic_library & shl,bool relative)123 install_mex_function (void *fptr, bool fmex, const std::string& name,
124                       const octave::dynamic_library& shl, bool relative)
125 {
126   octave_mex_function *fcn = new octave_mex_function (fptr, fmex, shl, name);
127 
128   if (relative)
129     fcn->mark_relative ();
130 
131   octave_value fval (fcn);
132 
133   octave::symbol_table& symtab
134     = octave::__get_symbol_table__ ("install_mex_function");
135 
136   symtab.install_built_in_function (name, fval);
137 }
138 
139 octave::dynamic_library
get_current_shlib(void)140 get_current_shlib (void)
141 {
142   octave::dynamic_library retval;
143 
144   octave::tree_evaluator& tw = octave::__get_evaluator__ ("get_current_shlib");
145 
146   octave_function *curr_fcn = tw.current_function ();
147 
148   if (curr_fcn)
149     {
150       if (curr_fcn->is_dld_function ())
151         {
152           octave_dld_function *dld
153             = dynamic_cast<octave_dld_function *> (curr_fcn);
154           retval = dld->get_shlib ();
155         }
156       else if (curr_fcn->is_mex_function ())
157         {
158           octave_mex_function *mex
159             = dynamic_cast<octave_mex_function *> (curr_fcn);
160           retval = mex->get_shlib ();
161         }
162     }
163 
164   return retval;
165 }
166