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