comparison libinterp/corefcn/syscalls.cc @ 20616:fd0efcdb3718

use new string_value method to handle value extraction errors * dirfns.cc, file-io.cc, gammainc.cc, help.cc, load-path.cc, octave-link.cc, qz.cc, regexp.cc, strfns.cc, syscalls.cc, time.cc, variables.cc: Use new string_value method.
author John W. Eaton <jwe@octave.org>
date Thu, 08 Oct 2015 19:00:51 -0400
parents 56fee8f84fe7
children
comparison
equal deleted inserted replaced
20615:729a85dafba8 20616:fd0efcdb3718
187 187
188 int nargin = args.length (); 188 int nargin = args.length ();
189 189
190 if (nargin == 1 || nargin == 2) 190 if (nargin == 1 || nargin == 2)
191 { 191 {
192 if (args(0).is_string ()) 192 std::string exec_file = args(0).string_value ("exec: FILE must be a string");
193
194 string_vector exec_args;
195
196 if (nargin == 2)
193 { 197 {
194 std::string exec_file = args(0).string_value (); 198 string_vector tmp = args(1).all_strings ();
195 199
196 string_vector exec_args; 200 if (! error_state)
197
198 if (nargin == 2)
199 { 201 {
200 string_vector tmp = args(1).all_strings (); 202 int len = tmp.numel ();
201 203
202 if (! error_state) 204 exec_args.resize (len + 1);
203 { 205
204 int len = tmp.numel (); 206 exec_args[0] = exec_file;
205 207
206 exec_args.resize (len + 1); 208 for (int i = 0; i < len; i++)
207 209 exec_args[i+1] = tmp[i];
208 exec_args[0] = exec_file;
209
210 for (int i = 0; i < len; i++)
211 exec_args[i+1] = tmp[i];
212 }
213 else
214 error ("exec: all arguments must be strings");
215 } 210 }
216 else 211 else
217 { 212 error ("exec: all arguments must be strings");
218 exec_args.resize (1);
219
220 exec_args[0] = exec_file;
221 }
222
223 octave_history_write_timestamp ();
224
225 if (! command_history::ignoring_entries ())
226 command_history::clean_up_and_save ();
227
228 std::string msg;
229
230 int status = octave_syscalls::execvp (exec_file, exec_args, msg);
231
232 retval(1) = msg;
233 retval(0) = status;
234 } 213 }
235 else 214 else
236 error ("exec: FILE must be a string"); 215 {
216 exec_args.resize (1);
217
218 exec_args[0] = exec_file;
219 }
220
221 octave_history_write_timestamp ();
222
223 if (! command_history::ignoring_entries ())
224 command_history::clean_up_and_save ();
225
226 std::string msg;
227
228 int status = octave_syscalls::execvp (exec_file, exec_args, msg);
229
230 retval(1) = msg;
231 retval(0) = status;
237 } 232 }
238 else 233 else
239 print_usage (); 234 print_usage ();
240 235
241 return retval; 236 return retval;
296 291
297 int nargin = args.length (); 292 int nargin = args.length ();
298 293
299 if (nargin >= 1 && nargin <= 3) 294 if (nargin >= 1 && nargin <= 3)
300 { 295 {
301 if (args(0).is_string ()) 296 std::string exec_file = args(0).string_value ("popen2: COMMAND argument must be a string");
297
298 string_vector arg_list;
299
300 if (nargin >= 2)
302 { 301 {
303 std::string exec_file = args(0).string_value (); 302 string_vector tmp = args(1).all_strings ();
304
305 string_vector arg_list;
306
307 if (nargin >= 2)
308 {
309 string_vector tmp = args(1).all_strings ();
310
311 if (! error_state)
312 {
313 int len = tmp.numel ();
314
315 arg_list.resize (len + 1);
316
317 arg_list[0] = exec_file;
318
319 for (int i = 0; i < len; i++)
320 arg_list[i+1] = tmp[i];
321 }
322 else
323 error ("popen2: all arguments must be strings");
324 }
325 else
326 {
327 arg_list.resize (1);
328
329 arg_list[0] = exec_file;
330 }
331 303
332 if (! error_state) 304 if (! error_state)
333 { 305 {
334 bool sync_mode = (nargin == 3 ? args(2).bool_value () : false); 306 int len = tmp.numel ();
335 307
336 int fildes[2]; 308 arg_list.resize (len + 1);
337 std::string msg; 309
338 pid_t pid; 310 arg_list[0] = exec_file;
339 311
340 pid = octave_syscalls::popen2 (exec_file, arg_list, sync_mode, 312 for (int i = 0; i < len; i++)
341 fildes, msg, interactive); 313 arg_list[i+1] = tmp[i];
342 if (pid >= 0)
343 {
344 FILE *ifile = fdopen (fildes[1], "r");
345 FILE *ofile = fdopen (fildes[0], "w");
346
347 std::string nm;
348
349 octave_stream is = octave_stdiostream::create (nm, ifile,
350 std::ios::in);
351
352 octave_stream os = octave_stdiostream::create (nm, ofile,
353 std::ios::out);
354
355 Cell file_ids (1, 2);
356
357 retval(2) = pid;
358 retval(1) = octave_stream_list::insert (is);
359 retval(0) = octave_stream_list::insert (os);
360 }
361 else
362 error (msg.c_str ());
363 } 314 }
364 else 315 else
365 error ("popen2: all arguments must be strings"); 316 error ("popen2: all arguments must be strings");
366 } 317 }
367 else 318 else
368 error ("popen2: COMMAND argument must be a string"); 319 {
320 arg_list.resize (1);
321
322 arg_list[0] = exec_file;
323 }
324
325 if (! error_state)
326 {
327 bool sync_mode = (nargin == 3 ? args(2).bool_value () : false);
328
329 int fildes[2];
330 std::string msg;
331 pid_t pid;
332
333 pid = octave_syscalls::popen2 (exec_file, arg_list, sync_mode,
334 fildes, msg, interactive);
335 if (pid >= 0)
336 {
337 FILE *ifile = fdopen (fildes[1], "r");
338 FILE *ofile = fdopen (fildes[0], "w");
339
340 std::string nm;
341
342 octave_stream is = octave_stdiostream::create (nm, ifile,
343 std::ios::in);
344
345 octave_stream os = octave_stdiostream::create (nm, ofile,
346 std::ios::out);
347
348 Cell file_ids (1, 2);
349
350 retval(2) = pid;
351 retval(1) = octave_stream_list::insert (is);
352 retval(0) = octave_stream_list::insert (os);
353 }
354 else
355 error (msg.c_str ());
356 }
357 else
358 error ("popen2: all arguments must be strings");
369 } 359 }
370 else 360 else
371 print_usage (); 361 print_usage ();
372 362
373 return retval; 363 return retval;
784 { 774 {
785 octave_value_list retval; 775 octave_value_list retval;
786 776
787 if (args.length () == 1) 777 if (args.length () == 1)
788 { 778 {
789 std::string fname = args(0).string_value (); 779 std::string fname = args(0).string_value ("lstat: NAME must be a string");
790 780
791 file_stat fs (fname, false); 781 file_stat fs (fname, false);
792 782
793 retval = mk_stat_result (fs); 783 retval = mk_stat_result (fs);
794 } 784 }
851 841
852 int nargin = args.length (); 842 int nargin = args.length ();
853 843
854 if (nargin == 2) 844 if (nargin == 2)
855 { 845 {
856 if (args(0).is_string ()) 846 std::string name = args(0).string_value ("mkfifo: FILE must be a string");
847
848 int octal_mode = args(1).int_value ();
849
850 if (! error_state)
857 { 851 {
858 std::string name = args(0).string_value (); 852 if (octal_mode < 0)
859 853 error ("mkfifo: MODE must be a positive integer value");
860 int octal_mode = args(1).int_value (); 854 else
861
862 if (! error_state)
863 { 855 {
864 if (octal_mode < 0) 856 int mode = convert (octal_mode, 8, 10);
865 error ("mkfifo: MODE must be a positive integer value"); 857
866 else 858 std::string msg;
867 { 859
868 int mode = convert (octal_mode, 8, 10); 860 int status = octave_mkfifo (name, mode, msg);
869 861
870 std::string msg; 862 retval(0) = status;
871 863
872 int status = octave_mkfifo (name, mode, msg); 864 if (status < 0)
873 865 retval(1) = msg;
874 retval(0) = status;
875
876 if (status < 0)
877 retval(1) = msg;
878 }
879 } 866 }
880 else
881 error ("mkfifo: MODE must be an integer");
882 } 867 }
883 else 868 else
884 error ("mkfifo: FILE must be a string"); 869 error ("mkfifo: MODE must be an integer");
885 } 870 }
886 else 871 else
887 print_usage (); 872 print_usage ();
888 873
889 return retval; 874 return retval;
1065 1050
1066 retval = mk_stat_result (fs); 1051 retval = mk_stat_result (fs);
1067 } 1052 }
1068 else 1053 else
1069 { 1054 {
1070 std::string fname = args(0).string_value (); 1055 std::string fname = args(0).string_value ("stat: NAME must be a string");
1071 1056
1072 file_stat fs (fname); 1057 file_stat fs (fname);
1073 1058
1074 retval = mk_stat_result (fs); 1059 retval = mk_stat_result (fs);
1075 } 1060 }
1345 1330
1346 int nargin = args.length (); 1331 int nargin = args.length ();
1347 1332
1348 if (nargin == 1) 1333 if (nargin == 1)
1349 { 1334 {
1350 if (args(0).is_string ()) 1335 std::string name = args(0).string_value ("unlink: FILE must be a string");
1351 { 1336
1352 std::string name = args(0).string_value (); 1337 std::string msg;
1353 1338
1354 std::string msg; 1339 int status = octave_unlink (name, msg);
1355 1340
1356 int status = octave_unlink (name, msg); 1341 retval(1) = msg;
1357 1342 retval(0) = status;
1358 retval(1) = msg;
1359 retval(0) = status;
1360 }
1361 else
1362 error ("unlink: FILE must be a string");
1363 } 1343 }
1364 else 1344 else
1365 print_usage (); 1345 print_usage ();
1366 1346
1367 return retval; 1347 return retval;
1666 { 1646 {
1667 octave_value_list retval; 1647 octave_value_list retval;
1668 1648
1669 if (args.length () == 1) 1649 if (args.length () == 1)
1670 { 1650 {
1671 if (args(0).is_string ()) 1651 std::string name = args(0).string_value ("canonicalize_file_name: NAME must be a string");
1672 { 1652 std::string msg;
1673 std::string name = args(0).string_value (); 1653
1674 std::string msg; 1654 std::string result = octave_canonicalize_file_name (name, msg);
1675 1655
1676 std::string result = octave_canonicalize_file_name (name, msg); 1656 retval(2) = msg;
1677 1657 retval(1) = msg.empty () ? 0 : -1;
1678 retval(2) = msg; 1658 retval(0) = result;
1679 retval(1) = msg.empty () ? 0 : -1;
1680 retval(0) = result;
1681 }
1682 else
1683 error ("canonicalize_file_name: NAME must be a string");
1684 } 1659 }
1685 else 1660 else
1686 print_usage (); 1661 print_usage ();
1687 1662
1688 return retval; 1663 return retval;