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