diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-11-03 13:43:50 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-11-03 13:43:50 -0200 |
commit | f9a9bd77e419992a4744aa84ef2f453cd44d5a53 (patch) | |
tree | 78f1de0db84dc2797132368193fef118669adca1 | |
parent | 63b8a6fd2090985daba51d1f7bf019b15ee0ae30 (diff) | |
download | lua-f9a9bd77e419992a4744aa84ef2f453cd44d5a53.tar.gz lua-f9a9bd77e419992a4744aa84ef2f453cd44d5a53.tar.bz2 lua-f9a9bd77e419992a4744aa84ef2f453cd44d5a53.zip |
print_stack now gets a parameter (file to print);
small changes.
-rw-r--r-- | iolib.c | 179 |
1 files changed, 64 insertions, 115 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Input/output library to LUA | 3 | ** Input/output library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_iolib="$Id: iolib.c,v 1.25 1995/10/23 13:53:48 roberto Exp roberto $"; | 6 | char *rcs_iolib="$Id: iolib.c,v 1.26 1995/10/26 14:21:56 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <ctype.h> | 9 | #include <ctype.h> |
@@ -25,6 +25,22 @@ static FILE *in=stdin, *out=stdout; | |||
25 | #define pclose(x) (-1) | 25 | #define pclose(x) (-1) |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | static void str_error(char *funcname) | ||
29 | { | ||
30 | char buff[250]; | ||
31 | sprintf(buff, "incorrect arguments to function `%s'", funcname); | ||
32 | lua_error(buff); | ||
33 | } | ||
34 | |||
35 | static char *check_and_get_string (int numArg, char *funcname) | ||
36 | { | ||
37 | lua_Object o = lua_getparam(numArg); | ||
38 | if (!(lua_isstring(o) || lua_isnumber(o))) | ||
39 | str_error(funcname); | ||
40 | return lua_getstring(o); | ||
41 | } | ||
42 | |||
43 | |||
28 | static void closeread (void) | 44 | static void closeread (void) |
29 | { | 45 | { |
30 | if (in != stdin) | 46 | if (in != stdin) |
@@ -55,34 +71,23 @@ static void closewrite (void) | |||
55 | */ | 71 | */ |
56 | static void io_readfrom (void) | 72 | static void io_readfrom (void) |
57 | { | 73 | { |
58 | lua_Object o = lua_getparam (1); | 74 | if (lua_getparam (1) == LUA_NOOBJECT) |
59 | if (o == LUA_NOOBJECT) /* restore standart input */ | 75 | { /* restore standart input */ |
60 | { | ||
61 | closeread(); | 76 | closeread(); |
62 | lua_pushnumber (1); | 77 | lua_pushnumber (1); |
63 | } | 78 | } |
64 | else | 79 | else |
65 | { | 80 | { |
66 | if (!lua_isstring (o)) | 81 | char *s = check_and_get_string(1, "readfrom"); |
67 | { | ||
68 | lua_error ("incorrect argument to function 'readfrom`"); | ||
69 | lua_pushnumber (0); | ||
70 | } | ||
71 | else | ||
72 | { | ||
73 | char *s = lua_getstring(o); | ||
74 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 82 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
75 | if (fp == NULL) | 83 | if (fp == NULL) |
76 | { | ||
77 | lua_pushnumber (0); | 84 | lua_pushnumber (0); |
78 | } | ||
79 | else | 85 | else |
80 | { | 86 | { |
81 | closeread(); | 87 | closeread(); |
82 | in = fp; | 88 | in = fp; |
83 | lua_pushnumber (1); | 89 | lua_pushnumber (1); |
84 | } | 90 | } |
85 | } | ||
86 | } | 91 | } |
87 | } | 92 | } |
88 | 93 | ||
@@ -97,34 +102,23 @@ static void io_readfrom (void) | |||
97 | */ | 102 | */ |
98 | static void io_writeto (void) | 103 | static void io_writeto (void) |
99 | { | 104 | { |
100 | lua_Object o = lua_getparam (1); | 105 | if (lua_getparam (1) == LUA_NOOBJECT) /* restore standart output */ |
101 | if (o == LUA_NOOBJECT) /* restore standart output */ | ||
102 | { | 106 | { |
103 | closewrite(); | 107 | closewrite(); |
104 | lua_pushnumber (1); | 108 | lua_pushnumber (1); |
105 | } | 109 | } |
106 | else | 110 | else |
107 | { | 111 | { |
108 | if (!lua_isstring (o)) | 112 | char *s = check_and_get_string(1, "writeto"); |
109 | { | ||
110 | lua_error ("incorrect argument to function 'writeto`"); | ||
111 | lua_pushnumber (0); | ||
112 | } | ||
113 | else | ||
114 | { | ||
115 | char *s = lua_getstring(o); | ||
116 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | 113 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); |
117 | if (fp == NULL) | 114 | if (fp == NULL) |
118 | { | ||
119 | lua_pushnumber (0); | 115 | lua_pushnumber (0); |
120 | } | ||
121 | else | 116 | else |
122 | { | 117 | { |
123 | closewrite(); | 118 | closewrite(); |
124 | out = fp; | 119 | out = fp; |
125 | lua_pushnumber (1); | 120 | lua_pushnumber (1); |
126 | } | 121 | } |
127 | } | ||
128 | } | 122 | } |
129 | } | 123 | } |
130 | 124 | ||
@@ -140,30 +134,17 @@ static void io_writeto (void) | |||
140 | */ | 134 | */ |
141 | static void io_appendto (void) | 135 | static void io_appendto (void) |
142 | { | 136 | { |
143 | lua_Object o = lua_getparam (1); | 137 | char *s = check_and_get_string(1, "appendto"); |
144 | if (!lua_isstring (o)) | 138 | struct stat st; |
145 | { | 139 | int r = (stat(s, &st) == -1) ? 1 : 2; |
146 | lua_error ("incorrect argument to function 'appendto`"); | 140 | FILE *fp = fopen (s, "a"); |
147 | lua_pushnumber (0); | 141 | if (fp == NULL) |
148 | } | 142 | lua_pushnumber (0); |
149 | else | 143 | else |
150 | { | 144 | { |
151 | int r; | 145 | if (out != stdout) fclose (out); |
152 | FILE *fp; | 146 | out = fp; |
153 | struct stat st; | 147 | lua_pushnumber (r); |
154 | if (stat(lua_getstring(o), &st) == -1) r = 1; | ||
155 | else r = 2; | ||
156 | fp = fopen (lua_getstring(o),"a"); | ||
157 | if (fp == NULL) | ||
158 | { | ||
159 | lua_pushnumber (0); | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | if (out != stdout) fclose (out); | ||
164 | out = fp; | ||
165 | lua_pushnumber (r); | ||
166 | } | ||
167 | } | 148 | } |
168 | } | 149 | } |
169 | 150 | ||
@@ -185,30 +166,17 @@ static void io_appendto (void) | |||
185 | static void io_read (void) | 166 | static void io_read (void) |
186 | { | 167 | { |
187 | lua_Object o = lua_getparam (1); | 168 | lua_Object o = lua_getparam (1); |
188 | if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */ | 169 | if (o == LUA_NOOBJECT) /* free format */ |
189 | { | 170 | { |
190 | int c; | 171 | int c; |
191 | char s[256]; | 172 | char s[256]; |
192 | while (isspace(c=fgetc(in))) | 173 | while (isspace(c=fgetc(in))) |
193 | ; | 174 | ; |
194 | if (c == '\"') | 175 | if (c == '\"' || c == '\'') |
195 | { | 176 | { |
177 | int del = c; | ||
196 | int n=0; | 178 | int n=0; |
197 | while((c = fgetc(in)) != '\"') | 179 | while((c = fgetc(in)) != del) |
198 | { | ||
199 | if (c == EOF) | ||
200 | { | ||
201 | lua_pushnil (); | ||
202 | return; | ||
203 | } | ||
204 | s[n++] = c; | ||
205 | } | ||
206 | s[n] = 0; | ||
207 | } | ||
208 | else if (c == '\'') | ||
209 | { | ||
210 | int n=0; | ||
211 | while((c = fgetc(in)) != '\'') | ||
212 | { | 180 | { |
213 | if (c == EOF) | 181 | if (c == EOF) |
214 | { | 182 | { |
@@ -239,7 +207,7 @@ static void io_read (void) | |||
239 | } | 207 | } |
240 | else /* formatted */ | 208 | else /* formatted */ |
241 | { | 209 | { |
242 | char *e = lua_getstring(o); | 210 | char *e = check_and_get_string(1, "read"); |
243 | char t; | 211 | char t; |
244 | int m=0; | 212 | int m=0; |
245 | while (isspace(*e)) e++; | 213 | while (isspace(*e)) e++; |
@@ -483,18 +451,7 @@ static void io_write (void) | |||
483 | */ | 451 | */ |
484 | static void io_execute (void) | 452 | static void io_execute (void) |
485 | { | 453 | { |
486 | lua_Object o = lua_getparam (1); | 454 | lua_pushnumber(system(check_and_get_string(1, "execute"))); |
487 | if (o == LUA_NOOBJECT || !lua_isstring (o)) | ||
488 | { | ||
489 | lua_error ("incorrect argument to function 'execute`"); | ||
490 | lua_pushnumber (0); | ||
491 | } | ||
492 | else | ||
493 | { | ||
494 | int res = system(lua_getstring(o)); | ||
495 | lua_pushnumber (res); | ||
496 | } | ||
497 | return; | ||
498 | } | 455 | } |
499 | 456 | ||
500 | /* | 457 | /* |
@@ -503,20 +460,10 @@ static void io_execute (void) | |||
503 | */ | 460 | */ |
504 | static void io_remove (void) | 461 | static void io_remove (void) |
505 | { | 462 | { |
506 | lua_Object o = lua_getparam (1); | 463 | if (remove(check_and_get_string(1, "remove")) == 0) |
507 | if (o == LUA_NOOBJECT || !lua_isstring (o)) | ||
508 | { | ||
509 | lua_error ("incorrect argument to function 'execute`"); | ||
510 | lua_pushnumber (0); | ||
511 | } | ||
512 | else | ||
513 | { | ||
514 | if (remove(lua_getstring(o)) == 0) | ||
515 | lua_pushnumber (1); | 464 | lua_pushnumber (1); |
516 | else | 465 | else |
517 | lua_pushnumber (0); | 466 | lua_pushnumber (0); |
518 | } | ||
519 | return; | ||
520 | } | 467 | } |
521 | 468 | ||
522 | 469 | ||
@@ -525,15 +472,9 @@ static void io_remove (void) | |||
525 | */ | 472 | */ |
526 | static void io_getenv (void) | 473 | static void io_getenv (void) |
527 | { | 474 | { |
528 | lua_Object s = lua_getparam(1); | 475 | char *env = getenv(check_and_get_string(1, "getenv")); |
529 | if (!lua_isstring(s)) | 476 | if (env == NULL) lua_pushnil(); |
530 | lua_pushnil(); | 477 | else lua_pushstring(env); |
531 | else | ||
532 | { | ||
533 | char *env = getenv(lua_getstring(s)); | ||
534 | if (env == NULL) lua_pushnil(); | ||
535 | else lua_pushstring(env); | ||
536 | } | ||
537 | } | 478 | } |
538 | 479 | ||
539 | /* | 480 | /* |
@@ -602,26 +543,23 @@ static void io_debug (void) | |||
602 | } | 543 | } |
603 | 544 | ||
604 | 545 | ||
605 | static void print_message (void) | 546 | void lua_printstack (FILE *f) |
606 | { | 547 | { |
607 | lua_Object o = lua_getparam(1); | ||
608 | char *s = lua_isstring(o) ? lua_getstring(o) : "(no messsage)"; | ||
609 | int level = 0; | 548 | int level = 0; |
610 | lua_Object func; | 549 | lua_Object func; |
611 | fprintf(stderr, "lua: %s\n", s); | 550 | fprintf(f, "Active Stack:\n"); |
612 | fprintf(stderr, "Active Stack:\n"); | ||
613 | while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) | 551 | while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) |
614 | { | 552 | { |
615 | char *name; | 553 | char *name; |
616 | int currentline; | 554 | int currentline; |
617 | fprintf(stderr, "\t"); | 555 | fprintf(f, "\t"); |
618 | switch (*getobjname(func, &name)) | 556 | switch (*getobjname(func, &name)) |
619 | { | 557 | { |
620 | case 'g': | 558 | case 'g': |
621 | fprintf(stderr, "function %s", name); | 559 | fprintf(f, "function %s", name); |
622 | break; | 560 | break; |
623 | case 'f': | 561 | case 'f': |
624 | fprintf(stderr, "fallback %s", name); | 562 | fprintf(f, "fallback %s", name); |
625 | break; | 563 | break; |
626 | default: | 564 | default: |
627 | { | 565 | { |
@@ -629,19 +567,29 @@ static void print_message (void) | |||
629 | int linedefined; | 567 | int linedefined; |
630 | lua_funcinfo(func, &filename, &linedefined); | 568 | lua_funcinfo(func, &filename, &linedefined); |
631 | if (linedefined == 0) | 569 | if (linedefined == 0) |
632 | fprintf(stderr, "main of %s", filename); | 570 | fprintf(f, "main of %s", filename); |
633 | else if (linedefined < 0) | 571 | else if (linedefined < 0) |
634 | fprintf(stderr, "%s", filename); | 572 | fprintf(f, "%s", filename); |
635 | else | 573 | else |
636 | fprintf(stderr, "function (%s:%d)", filename, linedefined); | 574 | fprintf(f, "function (%s:%d)", filename, linedefined); |
637 | } | 575 | } |
638 | } | 576 | } |
639 | if ((currentline = lua_currentline(func)) > 0) | 577 | if ((currentline = lua_currentline(func)) > 0) |
640 | fprintf(stderr, " at line %d", currentline); | 578 | fprintf(f, " at line %d", currentline); |
641 | fprintf(stderr, "\n"); | 579 | fprintf(f, "\n"); |
642 | } | 580 | } |
643 | } | 581 | } |
644 | 582 | ||
583 | |||
584 | static void errorfb (void) | ||
585 | { | ||
586 | lua_Object o = lua_getparam(1); | ||
587 | char *s = lua_isstring(o) ? lua_getstring(o) : "(no messsage)"; | ||
588 | fprintf(stderr, "lua: %s\n", s); | ||
589 | lua_printstack(stderr); | ||
590 | } | ||
591 | |||
592 | |||
645 | /* | 593 | /* |
646 | ** Open io library | 594 | ** Open io library |
647 | */ | 595 | */ |
@@ -661,5 +609,6 @@ void iolib_open (void) | |||
661 | lua_register ("beep", io_beep); | 609 | lua_register ("beep", io_beep); |
662 | lua_register ("exit", io_exit); | 610 | lua_register ("exit", io_exit); |
663 | lua_register ("debug", io_debug); | 611 | lua_register ("debug", io_debug); |
664 | lua_setfallback("error", print_message); | 612 | lua_register ("print_stack", errorfb); |
613 | lua_setfallback("error", errorfb); | ||
665 | } | 614 | } |