1
|
1 // file-io.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Written by John C. Campbell <jcc@che.utexas.edu>. |
|
25 |
|
26 #ifdef __GNUG__ |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #include <DLList.h> |
|
31 #include <unistd.h> |
|
32 #include <string.h> |
|
33 #include <stdio.h> |
|
34 #include <stdlib.h> |
|
35 #include <strstream.h> |
|
36 #include <ctype.h> |
|
37 |
|
38 #include "statdefs.h" |
|
39 #include "file-io.h" |
|
40 #include "input.h" |
|
41 #include "octave-hist.h" |
|
42 #include "tree-const.h" |
|
43 #include "error.h" |
|
44 #include "utils.h" |
|
45 #include "pager.h" |
|
46 |
|
47 // keeps a count of how many files are open and in the file list |
|
48 static int file_count = 0; |
|
49 |
|
50 // keeps a count of args sent to printf or scanf |
|
51 static int fmt_arg_count = 0; |
|
52 |
|
53 class File_info |
|
54 { |
|
55 public: |
|
56 File_info (void); |
164
|
57 File_info (int num, const char *nm, FILE *t, const char *md); |
1
|
58 File_info (const File_info& f); |
|
59 |
|
60 File_info& operator = (const File_info& f); |
|
61 |
|
62 ~File_info (void); |
|
63 |
|
64 int number (void) const; |
|
65 char *name (void) const; |
|
66 FILE *fptr (void) const; |
|
67 char *mode (void) const; |
|
68 |
|
69 private: |
|
70 int _number; |
|
71 char *_name; |
|
72 FILE *_fptr; |
|
73 char *_mode; |
|
74 }; |
|
75 |
|
76 File_info::File_info (void) |
|
77 { |
|
78 _number = -1; |
|
79 _name = (char *) NULL; |
|
80 _fptr = (FILE *) NULL; |
|
81 _mode = (char *) NULL; |
|
82 } |
|
83 |
|
84 File_info::File_info (const File_info& f) |
|
85 { |
|
86 _number = f._number; |
|
87 _name = strsave (f._name); |
|
88 _fptr = f._fptr; |
|
89 _mode = strsave (f._mode); |
|
90 } |
|
91 |
|
92 File_info& |
|
93 File_info::operator = (const File_info& f) |
|
94 { |
|
95 _number = f._number; |
|
96 _name = strsave (f._name); |
|
97 _fptr = f._fptr; |
|
98 _mode = strsave (f._mode); |
|
99 |
|
100 return *this; |
|
101 } |
|
102 |
|
103 File_info::~File_info (void) |
|
104 { |
|
105 delete [] _name; |
|
106 delete [] _mode; |
|
107 } |
|
108 |
164
|
109 File_info::File_info (int n, const char *nm, FILE *t, const char *md) |
1
|
110 { |
|
111 _number = n; |
|
112 _name = strsave (nm); |
|
113 _fptr = t; |
|
114 _mode = strsave (md); |
|
115 } |
|
116 |
|
117 int |
|
118 File_info::number (void) const |
|
119 { |
|
120 return _number; |
|
121 } |
|
122 |
|
123 char * |
|
124 File_info::name (void) const |
|
125 { |
|
126 return _name; |
|
127 } |
|
128 |
|
129 FILE * |
|
130 File_info::fptr (void) const |
|
131 { |
|
132 return _fptr; |
|
133 } |
|
134 |
|
135 char * |
|
136 File_info::mode (void) const |
|
137 { |
|
138 return _mode; |
|
139 } |
|
140 |
|
141 |
|
142 // double linked list containing relevant information about open files |
|
143 static DLList <File_info> file_list; |
|
144 |
|
145 void |
164
|
146 initialize_file_io (void) |
1
|
147 { |
|
148 File_info _stdin (0, "stdin", stdin, "r"); |
|
149 File_info _stdout (1, "stdout", stdout, "w"); |
|
150 File_info _stderr (2, "stderr", stderr, "w"); |
|
151 |
|
152 file_list.append (_stdin); |
|
153 file_list.append (_stdout); |
|
154 file_list.append (_stderr); |
|
155 |
|
156 file_count = 3; |
|
157 } |
|
158 |
|
159 Pix |
164
|
160 return_valid_file (const tree_constant& arg) |
1
|
161 { |
|
162 if (arg.is_string_type ()) |
|
163 { |
|
164 Pix p = file_list.first (); |
|
165 File_info file; |
|
166 for (int i = 0; i < file_count; i++) |
|
167 { |
|
168 char *file_name = arg.string_value (); |
|
169 file = file_list (p); |
|
170 if (strcmp (file.name (), file_name) == 0) |
|
171 return p; |
|
172 file_list.next (p); |
|
173 } |
|
174 } |
|
175 else if (arg.is_scalar_type ()) |
|
176 { |
|
177 double file_num = arg.double_value (); |
|
178 if ((double) NINT (file_num) != file_num) |
|
179 error ("file number not an integer value"); |
|
180 else |
|
181 { |
|
182 Pix p = file_list.first (); |
|
183 File_info file; |
|
184 for (int i = 0; i < file_count; i++) |
|
185 { |
|
186 file = file_list (p); |
|
187 if (file.number () == file_num) |
|
188 return p; |
|
189 file_list.next (p); |
|
190 } |
|
191 error ("no file with that number"); |
|
192 } |
|
193 } |
|
194 else |
|
195 error ("inapproriate file specifier"); |
|
196 |
|
197 return (Pix) NULL; |
|
198 } |
|
199 |
|
200 static Pix |
164
|
201 fopen_file_for_user (const tree_constant& arg, const char *mode) |
1
|
202 { |
|
203 char *file_name = arg.string_value (); |
|
204 |
|
205 FILE *file_ptr = fopen (file_name, mode); |
|
206 if (file_ptr != (FILE *) NULL) |
|
207 { |
|
208 File_info file (++file_count, file_name, file_ptr, mode); |
|
209 file_list.append (file); |
|
210 |
|
211 Pix p = file_list.first (); |
|
212 File_info file_from_list; |
|
213 |
|
214 for (int i = 0; i < file_count; i++) |
|
215 { |
|
216 file_from_list = file_list (p); |
|
217 if (strcmp (file_from_list.name (), file_name) == 0) |
|
218 return p; |
|
219 file_list.next (p); |
|
220 } |
|
221 } |
|
222 |
|
223 error ("problems automatically opening file for user"); |
|
224 return (Pix) NULL; |
|
225 } |
|
226 |
|
227 |
|
228 tree_constant * |
164
|
229 fclose_internal (const tree_constant *args) |
1
|
230 { |
|
231 tree_constant *retval = NULL_TREE_CONST; |
|
232 |
|
233 Pix p = return_valid_file (args[1]); |
|
234 |
|
235 if (p == (Pix) NULL) |
|
236 return retval; |
|
237 |
|
238 File_info file = file_list (p); |
|
239 |
|
240 if (file.number () < 3) |
|
241 { |
|
242 warning ("fclose: can't close stdin, stdout, or stderr!"); |
|
243 return retval; |
|
244 } |
|
245 |
|
246 int success = fclose (file.fptr ()); |
|
247 file_list.del (p); |
|
248 file_count--; |
|
249 |
|
250 retval = new tree_constant[2]; |
|
251 if (success == 0) |
|
252 retval[0] = tree_constant (1.0); // succeeded |
|
253 else |
|
254 { |
|
255 error ("fclose: error on closing file"); |
|
256 retval[0] = tree_constant (0.0); // failed |
|
257 } |
|
258 |
|
259 return retval; |
|
260 } |
|
261 |
|
262 tree_constant * |
164
|
263 fflush_internal (const tree_constant *args) |
1
|
264 { |
|
265 tree_constant *retval = NULL_TREE_CONST; |
|
266 |
|
267 Pix p = return_valid_file (args[1]); |
|
268 |
|
269 if (p == (Pix) NULL) |
|
270 return retval; |
|
271 |
|
272 File_info file = file_list (p); |
|
273 |
|
274 if (strcmp (file.mode (), "r") == 0) |
|
275 { |
|
276 warning ("can't flush an input stream"); |
|
277 return retval; |
|
278 } |
|
279 |
|
280 int success = 0; |
|
281 if (file.number () == 1) |
|
282 flush_output_to_pager (); |
|
283 else |
|
284 success = fflush (file.fptr ()); |
|
285 |
|
286 retval = new tree_constant[2]; |
|
287 if (success == 0) |
|
288 retval[0] = tree_constant (1.0); // succeeded |
|
289 else |
|
290 { |
|
291 error ("fflush: write error"); |
|
292 retval[0] = tree_constant (0.0); // failed |
|
293 } |
|
294 |
|
295 return retval; |
|
296 } |
|
297 |
|
298 static int |
164
|
299 valid_mode (const char *mode) |
1
|
300 { |
|
301 if (mode != (char *) NULL) |
|
302 { |
|
303 char m = mode[0]; |
|
304 if (m == 'r' || m == 'w' || m == 'a') |
|
305 { |
|
306 m = mode[1]; |
|
307 return (m == '\0' || (m == '+' && mode[2] == '\0')); |
|
308 } |
|
309 } |
|
310 return 0; |
|
311 } |
|
312 |
|
313 tree_constant * |
164
|
314 fgets_internal (const tree_constant *args, int nargout) |
1
|
315 { |
|
316 tree_constant *retval = NULL_TREE_CONST; |
|
317 |
|
318 Pix p = return_valid_file (args[1]); |
|
319 |
|
320 if (p == (Pix) NULL) |
|
321 { |
|
322 if (args[1].is_string_type ()) |
|
323 { |
|
324 struct stat buffer; |
|
325 char *name = args[1].string_value (); |
|
326 if (stat (name, &buffer) == 0 |
|
327 && (buffer.st_mode & S_IFREG) == S_IFREG) |
|
328 { |
|
329 p = fopen_file_for_user (args[1], "r"); |
|
330 } |
|
331 else |
|
332 { |
|
333 error ("fgets: file dosen't exist"); |
|
334 return retval; |
|
335 } |
|
336 } |
|
337 else |
|
338 return retval; |
|
339 } |
|
340 |
|
341 int length = 0; |
|
342 |
|
343 if (args[2].is_scalar_type ()) |
|
344 { |
|
345 length = (int) args[2].double_value (); |
|
346 if ((double) NINT (length) != length) |
|
347 { |
|
348 error ("fgets: length not an integer value"); |
|
349 return retval; |
|
350 } |
|
351 } |
|
352 |
|
353 char string[length+1]; |
|
354 File_info file = file_list (p); |
|
355 char *success = fgets (string, length+1, file.fptr ()); |
|
356 |
|
357 if (success == (char *) NULL) |
|
358 { |
|
359 retval = new tree_constant[2]; |
|
360 retval[0] = tree_constant (-1.0); |
|
361 return retval; |
|
362 } |
|
363 |
|
364 if (nargout == 2) |
|
365 { |
|
366 retval = new tree_constant[3]; |
|
367 retval[1] = tree_constant ((double) strlen (string)); |
|
368 } |
|
369 else |
|
370 retval = new tree_constant[2]; |
|
371 |
|
372 retval[0] = tree_constant (string); |
|
373 |
|
374 return retval; |
|
375 } |
|
376 |
|
377 tree_constant * |
164
|
378 fopen_internal (const tree_constant *args) |
1
|
379 { |
|
380 tree_constant *retval = NULL_TREE_CONST; |
|
381 Pix p; |
|
382 |
|
383 if (! args[1].is_string_type ()) |
|
384 { |
|
385 error ("fopen: file name must be a string"); |
|
386 return retval; |
|
387 } |
|
388 |
|
389 p = return_valid_file (args[1]); |
|
390 |
|
391 if (p != (Pix) NULL) |
|
392 { |
|
393 File_info file = file_list (p); |
|
394 |
|
395 retval = new tree_constant[2]; |
|
396 retval[0] = tree_constant ((double) file.number ()); |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
|
401 if (! args[2].is_string_type ()) |
|
402 { |
|
403 error ("fopen: mode must be a string"); |
|
404 return retval; |
|
405 } |
|
406 |
|
407 char *name = args[1].string_value (); |
|
408 char *mode = args[2].string_value (); |
|
409 |
|
410 if (! valid_mode (mode)) |
|
411 { |
|
412 error ("fopen: invalid mode"); |
|
413 return retval; |
|
414 } |
|
415 |
|
416 struct stat buffer; |
|
417 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
418 { |
|
419 error ("fopen: can't open directory"); |
|
420 return retval; |
|
421 } |
|
422 |
|
423 FILE *file_ptr = fopen (name, mode); |
|
424 |
|
425 if (file_ptr == (FILE *) NULL) |
|
426 { |
|
427 error ("fopen: file does not exist"); |
|
428 return retval; |
|
429 } |
|
430 |
|
431 int number = file_count++; |
|
432 |
|
433 File_info file (number, name, file_ptr, mode); |
|
434 file_list.append (file); |
|
435 |
|
436 retval = new tree_constant[2]; |
|
437 retval[0] = tree_constant ((double) number); |
|
438 |
|
439 return retval; |
|
440 } |
|
441 |
|
442 tree_constant * |
164
|
443 freport_internal (void) |
1
|
444 { |
|
445 tree_constant *retval = NULL_TREE_CONST; |
|
446 Pix p = file_list.first (); |
|
447 |
|
448 ostrstream output_buf; |
|
449 |
|
450 output_buf << "\n number mode name\n\n"; |
|
451 for (int i = 0; i < file_count; i++) |
|
452 { |
|
453 File_info file = file_list (p); |
|
454 output_buf.form ("%7d%6s %s\n", file.number (), file.mode (), |
|
455 file.name ()); |
|
456 file_list.next (p); |
|
457 } |
|
458 |
|
459 output_buf << "\n" << ends; |
|
460 maybe_page_output (output_buf); |
|
461 |
|
462 return retval; |
|
463 } |
|
464 |
|
465 tree_constant * |
164
|
466 frewind_internal (const tree_constant *args) |
1
|
467 { |
|
468 tree_constant *retval = NULL_TREE_CONST; |
|
469 |
|
470 Pix p = return_valid_file (args[1]); |
|
471 if (p == (Pix) NULL) |
|
472 p = fopen_file_for_user (args[1], "a+"); |
|
473 |
|
474 File_info file = file_list (p); |
|
475 rewind (file.fptr ()); |
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
|
480 tree_constant * |
164
|
481 fseek_internal (const tree_constant *args, int nargin) |
1
|
482 { |
|
483 tree_constant *retval = NULL_TREE_CONST; |
|
484 |
|
485 Pix p = return_valid_file (args[1]); |
|
486 |
|
487 if (p == (Pix) NULL) |
|
488 p = fopen_file_for_user (args[1], "a+"); |
|
489 |
|
490 long origin = SEEK_SET; |
|
491 long offset = 0; |
|
492 if (args[2].is_scalar_type ()) |
|
493 { |
|
494 offset = (long) args[2].double_value (); |
|
495 if ((double) NINT (offset) != offset) |
|
496 { |
|
497 error ("fseek: offset not an integer value"); |
|
498 return retval; |
|
499 } |
|
500 } |
|
501 |
|
502 if (nargin == 4 && args[3].is_scalar_type ()) |
|
503 { |
|
504 origin = (long) args[3].double_value (); |
|
505 if (origin == -1) |
|
506 origin = SEEK_CUR; |
|
507 else if (origin == -2) |
|
508 origin = SEEK_END; |
|
509 else |
|
510 { |
|
511 if ((double) NINT (origin) != origin) |
|
512 { |
|
513 error ("fseek: origin not an integer value"); |
|
514 return retval; |
|
515 } |
|
516 } |
|
517 } |
|
518 |
|
519 File_info file = file_list (p); |
|
520 int success = fseek (file.fptr (), offset, origin); |
|
521 retval = new tree_constant[2]; |
|
522 |
|
523 if (success == 0) |
|
524 retval[0] = tree_constant (1.0); // succeeded |
|
525 else |
|
526 { |
|
527 error ("fseek: file error"); |
|
528 retval[0] = tree_constant (0.0); // failed |
|
529 } |
|
530 |
|
531 return retval; |
|
532 } |
|
533 |
|
534 tree_constant * |
164
|
535 ftell_internal (const tree_constant *args) |
1
|
536 { |
|
537 tree_constant *retval = NULL_TREE_CONST; |
|
538 Pix p = return_valid_file (args[1]); |
|
539 |
|
540 if (p == (Pix) NULL) |
|
541 p = fopen_file_for_user (args[1], "a+"); |
|
542 |
|
543 File_info file = file_list (p); |
|
544 long offset = ftell (file.fptr ()); |
|
545 retval = new tree_constant[2]; |
|
546 retval[0] = tree_constant ((double) offset); |
|
547 |
|
548 if (offset == -1L) |
|
549 error ("ftell: write error"); |
|
550 |
|
551 return retval; |
|
552 } |
|
553 |
|
554 void |
164
|
555 close_files (void) |
1
|
556 { |
|
557 Pix p = file_list.first (); |
|
558 |
|
559 for (int i = 0; i < file_count; i++) |
|
560 { |
|
561 File_info file = file_list (p); |
|
562 if (i > 2) // do not close stdin, stdout, stderr! |
|
563 { |
|
564 int success = fclose (file.fptr ()); |
|
565 if (success != 0) |
|
566 error ("closing %s", file.name ()); |
|
567 } |
|
568 file_list.del (p); |
|
569 } |
|
570 } |
|
571 |
|
572 static int |
164
|
573 process_printf_format (const char *s, const tree_constant *args, |
|
574 ostrstream& sb, const char *type, int nargin) |
1
|
575 { |
|
576 ostrstream fmt; |
|
577 |
|
578 fmt << "%"; // do_printf() already blew past this one... |
|
579 |
|
580 tree_constant_rep::constant_type arg_type; |
|
581 |
|
582 int chars_from_fmt_str = 0; |
|
583 |
|
584 again: |
|
585 switch (*s) |
|
586 { |
|
587 case '+': case '-': case ' ': case '0': case '#': |
|
588 chars_from_fmt_str++; |
|
589 fmt << *s++; |
|
590 goto again; |
|
591 |
|
592 case '\0': |
|
593 goto invalid_format; |
|
594 |
|
595 default: |
|
596 break; |
|
597 } |
|
598 |
|
599 if (*s == '*') |
|
600 { |
|
601 if (fmt_arg_count >= nargin) |
|
602 { |
|
603 message (type, "not enough arguments"); |
|
604 return -1; |
|
605 } |
|
606 |
|
607 if (args[fmt_arg_count].const_type () |
|
608 != tree_constant_rep::scalar_constant) |
|
609 { |
|
610 message (type, "`*' must be replaced by an integer"); |
|
611 return -1; |
|
612 } |
|
613 |
66
|
614 fmt << NINT (args[fmt_arg_count++].double_value ()); |
1
|
615 s++; |
|
616 chars_from_fmt_str++; |
|
617 } |
|
618 else |
|
619 { |
|
620 while (*s != '\0' && isdigit (*s)) |
|
621 { |
|
622 chars_from_fmt_str++; |
|
623 fmt << *s++; |
|
624 } |
|
625 } |
|
626 |
|
627 if (*s == '\0') |
|
628 goto invalid_format; |
|
629 |
|
630 if (*s == '.') |
|
631 { |
|
632 chars_from_fmt_str++; |
|
633 fmt << *s++; |
|
634 } |
|
635 |
|
636 if (*s == '*') |
|
637 { |
|
638 if (*(s-1) == '*') |
|
639 goto invalid_format; |
|
640 |
|
641 if (fmt_arg_count >= nargin) |
|
642 { |
|
643 message (type, "not enough arguments"); |
|
644 return -1; |
|
645 } |
|
646 |
|
647 if (args[fmt_arg_count].const_type () |
|
648 != tree_constant_rep::scalar_constant) |
|
649 { |
|
650 message (type, "`*' must be replaced by an integer"); |
|
651 return -1; |
|
652 } |
|
653 |
66
|
654 fmt << NINT (args[fmt_arg_count++].double_value ()); |
1
|
655 s++; |
|
656 chars_from_fmt_str++; |
|
657 } |
|
658 else |
|
659 { |
|
660 while (*s != '\0' && isdigit (*s)) |
|
661 { |
|
662 chars_from_fmt_str++; |
|
663 fmt << *s++; |
|
664 } |
|
665 } |
|
666 |
|
667 if (*s == '\0') |
|
668 goto invalid_format; |
|
669 |
|
670 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
671 { |
|
672 chars_from_fmt_str++; |
|
673 fmt << *s++; |
|
674 } |
|
675 |
|
676 if (*s == '\0') |
|
677 goto invalid_format; |
|
678 |
|
679 if (fmt_arg_count >= nargin) |
|
680 { |
|
681 message (type, "not enough arguments"); |
|
682 return -1; |
|
683 } |
|
684 |
|
685 arg_type = args[fmt_arg_count].const_type (); |
|
686 |
|
687 switch (*s) |
|
688 { |
|
689 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
|
690 |
|
691 if (arg_type != tree_constant_rep::scalar_constant) |
|
692 goto invalid_conversion; |
|
693 else |
|
694 { |
|
695 chars_from_fmt_str++; |
|
696 fmt << *s << ends; |
|
697 double d = args[fmt_arg_count++].double_value (); |
|
698 if ((int) d != d) |
|
699 goto invalid_conversion; |
|
700 else |
|
701 { |
|
702 char *s = fmt.str (); |
|
703 sb.form (s, (int) d); |
|
704 delete [] s; |
|
705 return chars_from_fmt_str; |
|
706 } |
|
707 } |
|
708 |
|
709 case 'e': case 'E': case 'f': case 'g': case 'G': |
|
710 |
|
711 if (arg_type != tree_constant_rep::scalar_constant) |
|
712 goto invalid_conversion; |
|
713 else |
|
714 { |
|
715 chars_from_fmt_str++; |
|
716 fmt << *s << ends; |
|
717 char *s = fmt.str (); |
|
718 sb.form (s, args[fmt_arg_count++].double_value ()); |
|
719 delete [] s; |
|
720 return chars_from_fmt_str; |
|
721 } |
|
722 |
|
723 case 's': |
|
724 |
|
725 if (arg_type != tree_constant_rep::string_constant) |
|
726 goto invalid_conversion; |
|
727 else |
|
728 { |
|
729 chars_from_fmt_str++; |
|
730 fmt << *s << ends; |
|
731 char *s = fmt.str (); |
|
732 sb.form (s, args[fmt_arg_count++].string_value ()); |
|
733 delete [] s; |
|
734 return chars_from_fmt_str; |
|
735 } |
|
736 |
|
737 case 'c': |
|
738 |
|
739 if (arg_type != tree_constant_rep::string_constant) |
|
740 goto invalid_conversion; |
|
741 else |
|
742 { |
|
743 chars_from_fmt_str++; |
|
744 fmt << *s << ends; |
|
745 char *str = args[fmt_arg_count++].string_value (); |
|
746 if (strlen (str) != 1) |
|
747 goto invalid_conversion; |
|
748 else |
|
749 { |
|
750 char *s = fmt.str (); |
|
751 sb.form (s, *str); |
|
752 delete [] s; |
|
753 return chars_from_fmt_str; |
|
754 } |
|
755 } |
|
756 |
|
757 default: |
|
758 goto invalid_format; |
|
759 } |
|
760 |
|
761 invalid_conversion: |
|
762 message (type, "invalid conversion"); |
|
763 return -1; |
|
764 |
|
765 invalid_format: |
|
766 message (type, "invalid format"); |
|
767 return -1; |
|
768 } |
|
769 |
|
770 |
|
771 tree_constant * |
164
|
772 do_printf (const char *type, const tree_constant *args, int nargin, |
|
773 int nargout) |
1
|
774 { |
|
775 tree_constant *retval = NULL_TREE_CONST; |
|
776 fmt_arg_count = 1; |
|
777 char *fmt; |
|
778 File_info file; |
|
779 |
|
780 if (strcmp (type, "fprintf") == 0) |
|
781 { |
|
782 Pix p; |
|
783 |
|
784 if (args[2].is_string_type ()) |
|
785 { |
|
786 fmt = args[2].string_value (); |
|
787 fmt_arg_count++; |
|
788 } |
|
789 else |
|
790 { |
|
791 error ("%s: format must be a string", type); |
|
792 return retval; |
|
793 } |
|
794 |
|
795 if (args[1].is_scalar_type ()) |
|
796 { |
|
797 p = return_valid_file (args[1]); |
|
798 if (p == (Pix) NULL) |
|
799 return retval; |
|
800 } |
|
801 else if (args[1].is_string_type ()) |
|
802 { |
|
803 p = return_valid_file (args[1]); |
|
804 if (p == (Pix) NULL) |
|
805 p = fopen_file_for_user (args[1], "a+"); |
|
806 } |
|
807 else |
|
808 { |
|
809 error ("%s: illegal file specifier", type); |
|
810 return retval; |
|
811 } |
|
812 |
|
813 file = file_list (p); |
|
814 if (file.mode () == "r") |
|
815 { |
|
816 error ("%s: file is read only", type); |
|
817 return retval; |
|
818 } |
|
819 fmt = args[2].string_value (); |
|
820 fmt_arg_count++; |
|
821 } |
|
822 else if (args[1].is_string_type ()) |
|
823 { |
|
824 fmt = args[1].string_value (); |
|
825 fmt_arg_count++; |
|
826 } |
|
827 else |
|
828 { |
|
829 error ("%s: invalid format string", type); |
|
830 return retval; |
|
831 } |
|
832 |
|
833 // Scan fmt for % escapes and print out the arguments. |
|
834 |
|
835 ostrstream output_buf; |
|
836 |
|
837 char *ptr = fmt; |
|
838 |
|
839 for (;;) |
|
840 { |
|
841 char c; |
|
842 while ((c = *ptr++) != '\0' && c != '%') |
|
843 output_buf << c; |
|
844 |
|
845 if (c == '\0') |
|
846 break; |
|
847 |
|
848 if (*ptr == '%') |
|
849 { |
|
850 ptr++; |
|
851 output_buf << c; |
|
852 continue; |
|
853 } |
|
854 |
|
855 // We must be looking at a format specifier. Extract it or fail. |
|
856 |
|
857 |
|
858 int status = process_printf_format (ptr, args, output_buf, type, |
|
859 nargin); |
|
860 |
|
861 if (status < 0) |
|
862 return retval; |
|
863 |
|
864 ptr += status; |
|
865 } |
|
866 |
|
867 output_buf << ends; |
|
868 if (strcmp (type, "printf") == 0 |
|
869 || (strcmp (type, "fprintf") == 0 && file.number () == 1)) |
|
870 { |
|
871 maybe_page_output (output_buf); |
|
872 } |
|
873 else if (strcmp (type, "fprintf") == 0) |
|
874 { |
|
875 char *msg = output_buf.str (); |
|
876 int success = fputs (msg, file.fptr ()); |
|
877 if (success == EOF) |
|
878 error ("%s: writing to file", type); |
|
879 delete [] msg; |
|
880 } |
|
881 else if (strcmp (type, "sprintf") == 0) |
|
882 { |
|
883 retval = new tree_constant [2]; |
|
884 char *msg = output_buf.str (); |
|
885 retval[0] = tree_constant (msg); |
|
886 delete [] msg; |
|
887 } |
|
888 |
|
889 return retval; |
|
890 } |
|
891 |
|
892 static int |
164
|
893 process_scanf_format (const char *s, const tree_constant *args, |
|
894 ostrstream& fmt, const char *type, int nargout, |
|
895 FILE* fptr, tree_constant *values) |
1
|
896 { |
|
897 fmt << "%"; |
|
898 |
|
899 tree_constant_rep::constant_type arg_type; |
|
900 |
|
901 int chars_from_fmt_str = 0; |
|
902 int store_value = 1; |
|
903 int string_width = -1; |
|
904 int success = 1; |
|
905 |
|
906 if (*s == '*') |
|
907 { |
|
908 store_value = 0; |
|
909 s++; |
|
910 chars_from_fmt_str++; |
|
911 } |
|
912 |
|
913 if (isdigit (*s)) |
|
914 { |
|
915 ostrstream str_number; |
|
916 while (*s != '\0' && isdigit (*s)) |
|
917 { |
|
918 chars_from_fmt_str++; |
|
919 str_number << *s; |
|
920 fmt << *s++; |
|
921 } |
|
922 str_number << ends; |
|
923 char *number = str_number.str (); |
|
924 string_width = atoi (number); |
|
925 delete [] number; |
|
926 } |
|
927 |
|
928 if (*s == '\0') |
|
929 goto invalid_format; |
|
930 |
|
931 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
932 { |
|
933 chars_from_fmt_str++; |
|
934 s++; |
|
935 } |
|
936 |
|
937 if (*s == '\0') |
|
938 goto invalid_format; |
|
939 |
|
940 if (fmt_arg_count >= nargout && store_value) |
|
941 { |
|
942 message (type, "not enough arguments"); |
|
943 return -1; |
|
944 } |
|
945 |
|
946 arg_type = args[fmt_arg_count].const_type (); |
|
947 |
|
948 switch (*s) |
|
949 { |
|
950 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
|
951 { |
|
952 chars_from_fmt_str++; |
|
953 fmt << *s << ends; |
|
954 int temp; |
|
955 char *str = fmt.str (); |
|
956 success = fscanf (fptr, str, &temp); |
|
957 delete [] str; |
|
958 if (success > 0 && store_value) |
|
959 values[fmt_arg_count++] = tree_constant ((double) temp); |
|
960 } |
|
961 break; |
|
962 case 'e': case 'E': case 'f': case 'g': case 'G': |
|
963 { |
|
964 chars_from_fmt_str++; |
|
965 fmt << 'l' << *s << ends; |
|
966 double temp; |
|
967 char *str = fmt.str (); |
|
968 success = fscanf (fptr, str, &temp); |
|
969 delete [] str; |
|
970 if (success > 0 && store_value) |
|
971 values[fmt_arg_count++] = tree_constant (temp); |
|
972 } |
|
973 break; |
|
974 case 's': |
|
975 { |
|
976 if (string_width < 1) |
|
977 { |
|
978 string_width = 0; |
|
979 long original_position = ftell (fptr); |
|
980 int c; |
|
981 |
|
982 while ((c = getc (fptr)) != EOF |
|
983 && (c == ' ' || c == '\n' || c != '\t')) |
|
984 ; // Don't count leading whitespace. |
|
985 |
|
986 if (c != EOF) |
|
987 string_width++; |
|
988 |
|
989 for (;;) |
|
990 { |
|
991 c = getc (fptr); |
|
992 if (c != EOF && c != ' ' && c != '\n' && c != '\t') |
|
993 string_width++; |
|
994 else |
|
995 break; |
|
996 } |
|
997 |
|
998 fseek (fptr, original_position, SEEK_SET); |
|
999 } |
|
1000 chars_from_fmt_str++; |
|
1001 char temp[string_width+1]; |
|
1002 fmt << *s << ends; |
|
1003 char *str = fmt.str (); |
|
1004 success = fscanf (fptr, str, temp); |
|
1005 delete [] str; |
|
1006 if (success && store_value) |
|
1007 values[fmt_arg_count++] = tree_constant (temp); |
|
1008 } |
|
1009 break; |
|
1010 case 'c': |
|
1011 { |
|
1012 if (string_width < 1) |
|
1013 string_width = 1; |
|
1014 chars_from_fmt_str++; |
|
1015 char temp[string_width+1]; |
|
1016 memset (temp, '\0', string_width+1); |
|
1017 fmt << *s << ends; |
|
1018 char *str = fmt.str (); |
|
1019 success = fscanf (fptr, str, temp); |
|
1020 delete [] str; |
|
1021 temp[string_width] = '\0'; |
|
1022 if (success > 0 && store_value) |
|
1023 values[fmt_arg_count++] = tree_constant (temp); |
|
1024 } |
|
1025 break; |
|
1026 default: |
|
1027 goto invalid_format; |
|
1028 } |
|
1029 |
|
1030 if (success > 0 || (success == 0 && store_value == 0)) |
|
1031 return chars_from_fmt_str; |
|
1032 |
|
1033 if (success == 0) |
|
1034 message (type, "invalid conversion"); |
|
1035 else if (success == EOF) |
|
1036 { |
|
1037 if (strcmp (type, "fscanf") == 0) |
|
1038 message (type, "end of file reached before final conversion"); |
|
1039 else if (strcmp (type, "sscanf") == 0) |
|
1040 message (type, "end of string reached before final conversion"); |
|
1041 else if (strcmp (type, "scanf") == 0) |
|
1042 message (type, "end of input reached before final conversion"); |
|
1043 } |
|
1044 else |
|
1045 { |
|
1046 invalid_format: |
|
1047 message (type, "invalid format"); |
|
1048 } |
|
1049 |
|
1050 return -1; |
|
1051 } |
|
1052 |
|
1053 tree_constant * |
164
|
1054 do_scanf (const char *type, const tree_constant *args, int nargin, int nargout) |
1
|
1055 { |
|
1056 tree_constant *retval = NULL_TREE_CONST; |
|
1057 char *scanf_fmt = (char *) NULL; |
|
1058 char *tmp_file = (char *) NULL; |
|
1059 int tmp_file_open = 0; |
|
1060 FILE *fptr = (FILE *) NULL; |
|
1061 File_info file; |
|
1062 |
|
1063 fmt_arg_count = 0; |
|
1064 |
|
1065 if (strcmp (type, "scanf") != 0) |
|
1066 { |
|
1067 if ( args[2].is_string_type ()) |
|
1068 scanf_fmt = args[2].string_value (); |
|
1069 else |
|
1070 { |
|
1071 error ("%s: format must be a string", type); |
|
1072 return retval; |
|
1073 } |
|
1074 } |
|
1075 |
|
1076 int doing_fscanf = (strcmp (type, "fscanf") == 0); |
|
1077 |
|
1078 if (doing_fscanf) |
|
1079 { |
|
1080 Pix p; |
|
1081 if (args[1].is_scalar_type () |
|
1082 || args[1].is_string_type ()) |
|
1083 { |
|
1084 p = return_valid_file (args[1]); |
|
1085 if (p == (Pix) NULL) |
|
1086 return retval; |
|
1087 } |
|
1088 else |
|
1089 { |
|
1090 error ("%s: illegal file specifier", type); |
|
1091 return retval; |
|
1092 } |
|
1093 |
|
1094 file = file_list (p); |
|
1095 |
|
1096 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0) |
|
1097 { |
|
1098 error ("%s: this file is opened for writing only", type); |
|
1099 return retval; |
|
1100 } |
|
1101 |
|
1102 fptr = file.fptr (); |
|
1103 } |
|
1104 |
|
1105 if (args[1].is_string_type () || (doing_fscanf && file.number () == 0)) |
|
1106 { |
|
1107 char *string; |
|
1108 |
|
1109 if (strcmp (type, "scanf") == 0) |
|
1110 scanf_fmt = args[1].string_value (); |
|
1111 |
|
1112 if (strcmp (type, "scanf") == 0 |
|
1113 || (doing_fscanf && file.number () == 0)) |
|
1114 { |
|
1115 string = gnu_readline (""); |
|
1116 if (string && *string) |
|
1117 maybe_save_history (string); |
|
1118 } |
|
1119 else |
|
1120 string = args[1].string_value (); |
|
1121 |
|
1122 tmp_file = tmpnam ((char *) NULL); |
|
1123 |
|
1124 fptr = fopen (tmp_file, "w+"); |
|
1125 if (fptr == (FILE *) NULL) |
|
1126 { |
|
1127 error ("%s: error opening temporary file", type); |
|
1128 return retval; |
|
1129 } |
|
1130 tmp_file_open = 1; |
|
1131 unlink (tmp_file); |
|
1132 |
|
1133 if (string == (char *) NULL) |
|
1134 panic_impossible (); |
|
1135 |
|
1136 int success = fputs (string, fptr); |
|
1137 fflush (fptr); |
|
1138 rewind (fptr); |
|
1139 |
|
1140 if (success < 0) |
|
1141 { |
|
1142 error ("%s: trouble writing temporary file", type); |
|
1143 fclose (fptr); |
|
1144 return retval; |
|
1145 } |
|
1146 } |
|
1147 else if (! doing_fscanf) |
|
1148 { |
|
1149 error ("%s: first argument must be a string", type); |
|
1150 return retval; |
|
1151 } |
|
1152 |
|
1153 // Scan scanf_fmt for % escapes and assign the arguments. |
|
1154 |
|
1155 retval = new tree_constant[nargout+1]; |
|
1156 |
|
1157 char *ptr = scanf_fmt; |
|
1158 |
|
1159 for (;;) |
|
1160 { |
|
1161 ostrstream fmt; |
|
1162 char c; |
|
1163 while ((c = *ptr++) != '\0' && c != '%') |
|
1164 fmt << c; |
|
1165 |
|
1166 if (c == '\0') |
|
1167 break; |
|
1168 |
|
1169 if (*ptr == '%') |
|
1170 { |
|
1171 ptr++; |
|
1172 fmt << c; |
|
1173 continue; |
|
1174 } |
|
1175 |
|
1176 // We must be looking at a format specifier. Extract it or fail. |
|
1177 |
|
1178 int status = process_scanf_format (ptr, args, fmt, type, |
|
1179 nargout, fptr, retval); |
|
1180 |
|
1181 if (status < 0) |
|
1182 { |
|
1183 if (fmt_arg_count == 0) |
|
1184 { |
|
1185 delete [] retval; |
|
1186 retval = NULL_TREE_CONST; |
|
1187 } |
|
1188 break; |
|
1189 } |
|
1190 |
|
1191 ptr += status; |
|
1192 } |
|
1193 |
|
1194 if (tmp_file_open) |
|
1195 fclose (fptr); |
|
1196 |
|
1197 return retval; |
|
1198 } |
|
1199 |
|
1200 /* |
|
1201 ;;; Local Variables: *** |
|
1202 ;;; mode: C++ *** |
|
1203 ;;; page-delimiter: "^/\\*" *** |
|
1204 ;;; End: *** |
|
1205 */ |