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