38 #include <sys/select.h>
39 #include <sys/types.h>
85 #define SHELL_PATH "/bin/sh"
155 while (p !=
cs.begin ())
181 while (p !=
cs.begin ())
209 size_t frame = cs[curr_frame].prev;
213 size_t k = cs.size ();
233 curr_user_frame = retval - curr_user_frame - 1;
245 while (p !=
cs.begin ())
269 "column",
"scope",
"context", 0
285 size_t nframes = nskip <= user_code_frames ? user_code_frames - nskip : 0;
288 curr_user_frame = nframes - curr_user_frame - 1;
315 scope(k) = elt.
scope;
320 if (parent_fcn_name == std::string ())
321 name(k) = f->
name ();
355 std::string nm = f ? f->
name () : std::string (
"<unknown>");
358 <<
" at line " << elt.
line
359 <<
" column " << elt.
column
385 if ((incr < 0 && frame == 0) || (incr > 0 && frame ==
cs.size () - 1))
410 std::ostringstream buf;
413 buf <<
"stopped in " << f->
name ()
414 <<
" at line " << elt.
line << std::endl;
416 buf <<
"at top level" << std::endl;
439 bool skipped =
false;
443 frame =
cs[frame].prev;
492 error (
"called from:");
501 std::string fcn_name =
"?unknown?";
507 if (fcn_name.empty ())
508 fcn_name = fcn->
name ();
511 error (
" %s at line %d, column %d",
563 retval = parser.
run ();
609 catch (octave_interrupt_exception)
616 catch (octave_execution_exception)
619 std::cerr <<
"error: unhandled execution exception -- trying to return to prompt"
622 catch (std::bad_alloc)
625 std::cerr <<
"error: out of memory -- trying to return to prompt"
644 static bool deja_vu =
false;
648 while (! octave_atexit_functions.empty ())
650 std::string fcn = octave_atexit_functions.front ();
652 octave_atexit_functions.pop_front ();
747 gnulib::sleep (86400);
753 (*octave_exit) (retval);
759 @deftypefn {Built-in Function} {} exit (@var{status})\n\
760 @deftypefnx {Built-in Function} {} quit (@var{status})\n\
761 Exit the current Octave session. If the optional integer value\n\
762 @var{status} is supplied, pass that value to the operating system as the\n\
763 Octave's exit status. The default value is zero.\n\
769 error (
"quit: not supported in embedded mode");
772 if (args.length () > 0)
774 int tmp = args(0).nint_value ();
803 @deftypefn {Built-in Function} {} warranty ()\n\
804 Describe the conditions for copying and distributing Octave.\n\
811 GNU Octave free software; you can redistribute it and/or modify\n\
812 it under the terms of the GNU General Public License as published by\n\
813 the Free Software Foundation; either version 3 of the License, or\n\
814 (at your option) any later version.\n\
816 GNU Octave is distributed in the hope that it will be useful,\n\
817 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
818 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
819 GNU General Public License for more details.\n\
821 You should have received a copy of the GNU General Public License\n\
822 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\
835 #if defined (HAVE_SELECT)
843 retval = gnulib::select (FD_SETSIZE, &
set, 0, 0, 0);
867 std::ostringstream output_buf;
877 if (! cmd->eof () && errno == EAGAIN)
889 int cmd_status = cmd->
close ();
896 retval(1) = output_buf.str ();
897 retval(0) = cmd_status;
900 error (
"unable to start subprocess for '%s'", cmd_str.c_str ());
907 DEFUN (system, args, nargout,
909 @deftypefn {Built-in Function} {} system (\"@var{string}\")\n\
910 @deftypefnx {Built-in Function} {} system (\"@var{string}\", @var{return_output})\n\
911 @deftypefnx {Built-in Function} {} system (\"@var{string}\", @var{return_output}, @var{type})\n\
912 @deftypefnx {Built-in Function} {[@var{status}, @var{output}] =} system (@dots{})\n\
913 Execute a shell command specified by @var{string}.\n\
914 If the optional argument @var{type} is @qcode{\"async\"}, the process\n\
915 is started in the background and the process ID of the child process\n\
916 is returned immediately. Otherwise, the child process is started and\n\
917 Octave waits until it exits. If the @var{type} argument is omitted, it\n\
918 defaults to the value @qcode{\"sync\"}.\n\
920 If @var{system} is called with one or more output arguments, or if the\n\
921 optional argument @var{return_output} is true and the subprocess is started\n\
922 synchronously, then the output from the command is returned as a variable. \n\
923 Otherwise, if the subprocess is executed synchronously, its output is sent\n\
924 to the standard output. To send the output of a command executed with\n\
925 @code{system} through the pager, use a command like\n\
929 [output, text] = system (\"cmd\");\n\
938 printf (\"%s\\n\", nthargout (2, \"system\", \"cmd\"));\n\
941 The @code{system} function can return two values. The first is the\n\
942 exit status of the command and the second is any output from the\n\
943 command that was written to the standard output stream. For example,\n\
946 [status, output] = system (\"echo foo; exit 2\");\n\
950 will set the variable @code{output} to the string @samp{foo}, and the\n\
951 variable @code{status} to the integer @samp{2}.\n\
953 For commands run asynchronously, @var{status} is the process id of the\n\
954 command shell that is started to run the command.\n\
955 @seealso{unix, dos}\n\
962 int nargin = args.length ();
964 if (nargin > 0 && nargin < 4)
966 bool return_output = (nargin == 1 && nargout > 1);
972 std::string type_str = args(2).string_value ();
976 if (type_str ==
"sync")
978 else if (type_str ==
"async")
982 error (
"system: TYPE must be \"sync\" or \"async\"");
988 error (
"system: TYPE must be a character string");
995 return_output = args(1).is_true ();
999 error (
"system: RETURN_OUTPUT must be boolean value true or false");
1004 if (return_output && type ==
et_async)
1006 error (
"system: can't return output from commands run asynchronously");
1010 std::string cmd_str = args(0).string_value ();
1014 #if defined (__WIN32__) && ! defined (__CYGWIN__)
1017 cmd_str =
"\"" + cmd_str +
"\"";
1024 pid_t pid = fork ();
1027 error (
"system: fork failed -- can't create child process");
1033 execl (
SHELL_PATH,
"sh",
"-c", cmd_str.c_str (),
1034 static_cast<void *
> (0));
1040 #elif defined (__WIN32__)
1042 PROCESS_INFORMATION pi;
1043 ZeroMemory (&si,
sizeof (si));
1044 ZeroMemory (&pi,
sizeof (pi));
1046 strcpy (xcmd_str, cmd_str.c_str ());
1048 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi))
1049 error (
"system: CreateProcess failed -- can't create child process");
1052 retval(0) = pi.dwProcessId;
1053 CloseHandle (pi.hProcess);
1054 CloseHandle (pi.hThread);
1057 error (
"asynchronous system calls are not supported");
1060 else if (return_output)
1064 int status = system (cmd_str.c_str ());
1078 error (
"system: expecting string as first argument");
1101 octave_atexit_functions.push_front (fname);
1109 for (std::list<std::string>::iterator p = octave_atexit_functions.begin ();
1110 p != octave_atexit_functions.end (); p++)
1114 octave_atexit_functions.erase (p);
1124 DEFUN (atexit, args, nargout,
1126 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\
1127 @deftypefnx {Built-in Function} {} atexit (@var{fcn}, @var{flag})\n\
1128 Register a function to be called when Octave exits. For example,\n\
1132 function last_words ()\n\
1133 disp (\"Bye bye\");\n\
1135 atexit (\"last_words\");\n\
1140 will print the message @qcode{\"Bye bye\"} when Octave exits.\n\
1142 The additional argument @var{flag} will register or unregister\n\
1143 @var{fcn} from the list of functions to be called when Octave\n\
1144 exits. If @var{flag} is true, the function is registered, and if\n\
1145 @var{flag} is false, it is unregistered. For example,\n\
1146 after registering the function @code{last_words} above,\n\
1149 atexit (\"last_words\", false);\n\
1153 will remove the function from the list and Octave will not call\n\
1154 @code{last_words} when it exits.\n\
1156 Note that @code{atexit} only removes the first occurrence of a function\n\
1157 from the list, so if a function was placed in the list multiple\n\
1158 times with @code{atexit}, it must also be removed from the list\n\
1164 int nargin = args.
length ();
1166 if (nargin == 1 || nargin == 2)
1168 std::string
arg = args(0).string_value ();
1172 bool add_mode =
true;
1176 add_mode = args(1).bool_value ();
1179 error (
"atexit: FLAG argument must be a logical value");
1196 error (
"atexit: FCN argument must be a string");
1204 DEFUN (octave_config_info, args, ,
1206 @deftypefn {Built-in Function} {} octave_config_info ()\n\
1207 @deftypefnx {Built-in Function} {} octave_config_info (@var{option})\n\
1208 Return a structure containing configuration and installation\n\
1209 information for Octave.\n\
1211 If @var{option} is a string, return the configuration information for the\n\
1212 specified option.\n\
1218 #if defined (ENABLE_DYNAMIC_LINKING)
1219 bool octave_supports_dynamic_linking =
true;
1221 bool octave_supports_dynamic_linking =
false;
1227 struct conf_info_struct
1234 static const conf_info_struct conf_info[] =
1423 m.
assign (
"float_format",
1426 m.
assign (
"words_big_endian",
1429 m.
assign (
"words_little_endian",
1438 const conf_info_struct& elt = conf_info[i++];
1440 const char *key = elt.key;
1453 bool unix_system =
true;
1454 bool mac_system =
false;
1455 bool windows_system =
false;
1458 windows_system =
true;
1459 #if !defined (__CYGWIN__)
1460 unix_system =
false;
1464 #if defined (OCTAVE_USE_OS_X_API)
1475 int nargin = args.length ();
1479 std::string
arg = args(0).string_value ();
1488 error (
"octave_config_info: no info for '%s'", arg.c_str ());
1493 error (
"octave_config_info: invalid parameter '%s'", arg.c_str ());
1496 else if (nargin == 0)
1514 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE)
1516 int debug_new_delete = 0;
1519 extern vfp __new_handler;
1522 __builtin_new (
size_t sz)
1529 p = gnulib::malloc (sz);
1532 (*__new_handler) ();
1533 p = gnulib::malloc (sz);
1536 if (debug_new_delete)
1537 std::cerr <<
"__builtin_new: " << p << std::endl;
1543 __builtin_delete (
void *ptr)
1545 if (debug_new_delete)
1546 std::cerr <<
"__builtin_delete: " << ptr << std::endl;