comparison libinterp/corefcn/toplev.cc @ 20793:ba2b07c13913

use new string_value method to handle value extraction errors * __dispatch__.cc, balance.cc, colloc.cc, conv2.cc, data.cc, debug.cc, graphics.cc, input.cc, matrix_type.cc, oct-hist.cc, schur.cc, spparms.cc, symtab.cc, sysdep.cc, toplev.cc, utils.cc: Use new string_value method.
author John W. Eaton <jwe@octave.org>
date Fri, 09 Oct 2015 10:06:39 -0400
parents f90c8372b7ba
children
comparison
equal deleted inserted replaced
20792:fd0efcdb3718 20793:ba2b07c13913
1035 1035
1036 system_exec_type type = et_sync; 1036 system_exec_type type = et_sync;
1037 1037
1038 if (nargin == 3) 1038 if (nargin == 3)
1039 { 1039 {
1040 if (args(2).is_string ()) 1040 std::string type_str = args(2).string_value ("system: TYPE must be a string");
1041 { 1041
1042 std::string type_str = args(2).string_value (); 1042 if (type_str == "sync")
1043 1043 type = et_sync;
1044 if (type_str == "sync") 1044 else if (type_str == "async")
1045 type = et_sync; 1045 type = et_async;
1046 else if (type_str == "async")
1047 type = et_async;
1048 else
1049 {
1050 error ("system: TYPE must be \"sync\" or \"async\"");
1051 return retval;
1052 }
1053 }
1054 else 1046 else
1055 { 1047 {
1056 error ("system: TYPE must be a string"); 1048 error ("system: TYPE must be \"sync\" or \"async\"");
1057 return retval; 1049 return retval;
1058 } 1050 }
1059 } 1051 }
1060 1052
1061 if (nargin > 1) 1053 if (nargin > 1)
1073 { 1065 {
1074 error ("system: can't return output from commands run asynchronously"); 1066 error ("system: can't return output from commands run asynchronously");
1075 return retval; 1067 return retval;
1076 } 1068 }
1077 1069
1078 std::string cmd_str = args(0).string_value (); 1070 std::string cmd_str = args(0).string_value ("system: expecting string as first argument");
1079 1071
1080 if (! error_state)
1081 {
1082 #if defined (__WIN32__) && ! defined (__CYGWIN__) 1072 #if defined (__WIN32__) && ! defined (__CYGWIN__)
1083 // Work around weird double-quote handling on Windows systems. 1073 // Work around weird double-quote handling on Windows systems.
1084 if (type == et_sync) 1074 if (type == et_sync)
1085 cmd_str = "\"" + cmd_str + "\""; 1075 cmd_str = "\"" + cmd_str + "\"";
1086 #endif 1076 #endif
1087 1077
1088 if (type == et_async) 1078 if (type == et_async)
1079 {
1080 // FIXME: maybe this should go in sysdep.cc?
1081 #ifdef HAVE_FORK
1082 pid_t pid = fork ();
1083
1084 if (pid < 0)
1085 error ("system: fork failed -- can't create child process");
1086 else if (pid == 0)
1089 { 1087 {
1090 // FIXME: maybe this should go in sysdep.cc? 1088 // FIXME: should probably replace this
1091 #ifdef HAVE_FORK 1089 // call with something portable.
1092 pid_t pid = fork (); 1090
1093 1091 execl (SHELL_PATH, "sh", "-c", cmd_str.c_str (),
1094 if (pid < 0) 1092 static_cast<void *> (0));
1095 error ("system: fork failed -- can't create child process"); 1093
1096 else if (pid == 0) 1094 panic_impossible ();
1097 { 1095 }
1098 // FIXME: should probably replace this 1096 else
1099 // call with something portable. 1097 retval(0) = pid;
1100
1101 execl (SHELL_PATH, "sh", "-c", cmd_str.c_str (),
1102 static_cast<void *> (0));
1103
1104 panic_impossible ();
1105 }
1106 else
1107 retval(0) = pid;
1108 #elif defined (__WIN32__) 1098 #elif defined (__WIN32__)
1109 STARTUPINFO si; 1099 STARTUPINFO si;
1110 PROCESS_INFORMATION pi; 1100 PROCESS_INFORMATION pi;
1111 ZeroMemory (&si, sizeof (si)); 1101 ZeroMemory (&si, sizeof (si));
1112 ZeroMemory (&pi, sizeof (pi)); 1102 ZeroMemory (&pi, sizeof (pi));
1113 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length ()+1); 1103 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length ()+1);
1114 strcpy (xcmd_str, cmd_str.c_str ()); 1104 strcpy (xcmd_str, cmd_str.c_str ());
1115 1105
1116 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi)) 1106 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi))
1117 error ("system: CreateProcess failed -- can't create child process"); 1107 error ("system: CreateProcess failed -- can't create child process");
1118 else
1119 {
1120 retval(0) = pi.dwProcessId;
1121 CloseHandle (pi.hProcess);
1122 CloseHandle (pi.hThread);
1123 }
1124 #else
1125 error ("asynchronous system calls are not supported");
1126 #endif
1127 }
1128 else if (return_output)
1129 retval = run_command_and_return_output (cmd_str);
1130 else 1108 else
1131 { 1109 {
1132 int status = system (cmd_str.c_str ()); 1110 retval(0) = pi.dwProcessId;
1133 1111 CloseHandle (pi.hProcess);
1134 // The value in status is as returned by waitpid. If 1112 CloseHandle (pi.hThread);
1135 // the process exited normally, extract the actual exit
1136 // status of the command. Otherwise, return 127 as a
1137 // failure code.
1138
1139 if (octave_wait::ifexited (status))
1140 status = octave_wait::exitstatus (status);
1141
1142 retval(0) = status;
1143 } 1113 }
1144 } 1114 #else
1115 error ("asynchronous system calls are not supported");
1116 #endif
1117 }
1118 else if (return_output)
1119 retval = run_command_and_return_output (cmd_str);
1145 else 1120 else
1146 error ("system: expecting string as first argument"); 1121 {
1122 int status = system (cmd_str.c_str ());
1123
1124 // The value in status is as returned by waitpid. If
1125 // the process exited normally, extract the actual exit
1126 // status of the command. Otherwise, return 127 as a
1127 // failure code.
1128
1129 if (octave_wait::ifexited (status))
1130 status = octave_wait::exitstatus (status);
1131
1132 retval(0) = status;
1133 }
1147 } 1134 }
1148 else 1135 else
1149 print_usage (); 1136 print_usage ();
1150 1137
1151 return retval; 1138 return retval;
1233 1220
1234 int nargin = args.length (); 1221 int nargin = args.length ();
1235 1222
1236 if (nargin == 1 || nargin == 2) 1223 if (nargin == 1 || nargin == 2)
1237 { 1224 {
1238 if (args(0).is_string ()) 1225 std::string arg = args(0).string_value ("atexit: FCN argument must be a string");
1239 { 1226
1240 std::string arg = args(0).string_value (); 1227 bool add_mode = true;
1241 1228
1242 bool add_mode = true; 1229 if (nargin == 2)
1243 1230 {
1244 if (nargin == 2) 1231 add_mode = args(1).bool_value ();
1245 { 1232
1246 add_mode = args(1).bool_value (); 1233 if (error_state)
1247 1234 error ("atexit: FLAG argument must be a logical value");
1248 if (error_state) 1235 }
1249 error ("atexit: FLAG argument must be a logical value"); 1236
1250 } 1237 if (add_mode)
1251 1238 octave_add_atexit_function (arg);
1252 if (add_mode)
1253 octave_add_atexit_function (arg);
1254 else
1255 {
1256 bool found = octave_remove_atexit_function (arg);
1257
1258 if (nargout > 0)
1259 retval(0) = found;
1260 }
1261 }
1262 else 1239 else
1263 error ("atexit: FCN argument must be a string"); 1240 {
1241 bool found = octave_remove_atexit_function (arg);
1242
1243 if (nargout > 0)
1244 retval(0) = found;
1245 }
1264 } 1246 }
1265 else 1247 else
1266 print_usage (); 1248 print_usage ();
1267 1249
1268 return retval; 1250 return retval;