diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-26 17:39:10 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-26 17:39:10 -0300 |
commit | da585783e33f92f8dff6fd47a89671494adc11e0 (patch) | |
tree | aa1a0f59eea602c6622427b573e9b85e94f79b66 /iolib.c | |
parent | e81f1841644405845e7de6bd70a0f074cced3d81 (diff) | |
download | lua-da585783e33f92f8dff6fd47a89671494adc11e0.tar.gz lua-da585783e33f92f8dff6fd47a89671494adc11e0.tar.bz2 lua-da585783e33f92f8dff6fd47a89671494adc11e0.zip |
new method to handle current files, with global variables
_INPUT and _OUTPUT.
Diffstat (limited to 'iolib.c')
-rw-r--r-- | iolib.c | 89 |
1 files changed, 56 insertions, 33 deletions
@@ -10,8 +10,6 @@ | |||
10 | #include "lualib.h" | 10 | #include "lualib.h" |
11 | 11 | ||
12 | 12 | ||
13 | FILE *lua_infile, *lua_outfile; | ||
14 | |||
15 | int lua_tagio; | 13 | int lua_tagio; |
16 | 14 | ||
17 | 15 | ||
@@ -39,59 +37,80 @@ static void pushresult (int i) | |||
39 | } | 37 | } |
40 | 38 | ||
41 | 39 | ||
42 | static void closefile (FILE *f) | 40 | |
41 | static FILE *getfile (char *name) | ||
43 | { | 42 | { |
44 | if (f == stdin || f == stdout) | 43 | lua_Object f = lua_getglobal(name); |
45 | return; | 44 | if (lua_tag(f) != lua_tagio) |
46 | if (f == lua_infile) | 45 | luaL_verror("global variable %s is not a file handle", name); |
47 | lua_infile = stdin; | 46 | return lua_getuserdata(f); |
48 | if (f == lua_outfile) | 47 | } |
49 | lua_outfile = stdout; | 48 | |
49 | |||
50 | static void closefile (char *name) | ||
51 | { | ||
52 | FILE *f = getfile(name); | ||
53 | if (f == stdin || f == stdout) return; | ||
50 | if (pclose(f) == -1) | 54 | if (pclose(f) == -1) |
51 | fclose(f); | 55 | fclose(f); |
52 | } | 56 | } |
53 | 57 | ||
54 | 58 | ||
59 | static void setfile (FILE *f, char *name) | ||
60 | { | ||
61 | lua_pushusertag(f, lua_tagio); | ||
62 | lua_setglobal(name); | ||
63 | } | ||
64 | |||
65 | |||
66 | static void setreturn (FILE *f, char *name) | ||
67 | { | ||
68 | setfile(f, name); | ||
69 | lua_pushusertag(f, lua_tagio); | ||
70 | } | ||
71 | |||
55 | 72 | ||
56 | static void io_readfrom (void) | 73 | static void io_readfrom (void) |
57 | { | 74 | { |
75 | FILE *current; | ||
58 | lua_Object f = lua_getparam(1); | 76 | lua_Object f = lua_getparam(1); |
59 | if (f == LUA_NOOBJECT) | 77 | if (f == LUA_NOOBJECT) { |
60 | closefile(lua_infile); /* restore standart input */ | 78 | closefile("_INPUT"); |
79 | current = stdin; | ||
80 | } | ||
61 | else if (lua_tag(f) == lua_tagio) | 81 | else if (lua_tag(f) == lua_tagio) |
62 | lua_infile = lua_getuserdata(f); | 82 | current = lua_getuserdata(f); |
63 | else { | 83 | else { |
64 | char *s = luaL_check_string(1); | 84 | char *s = luaL_check_string(1); |
65 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 85 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
66 | if (fp) | 86 | if (current == NULL) { |
67 | lua_infile = fp; | ||
68 | else { | ||
69 | pushresult(0); | 87 | pushresult(0); |
70 | return; | 88 | return; |
71 | } | 89 | } |
72 | } | 90 | } |
73 | lua_pushusertag(lua_infile, lua_tagio); | 91 | setreturn(current, "_INPUT"); |
74 | } | 92 | } |
75 | 93 | ||
76 | 94 | ||
77 | static void io_writeto (void) | 95 | static void io_writeto (void) |
78 | { | 96 | { |
97 | FILE *current; | ||
79 | lua_Object f = lua_getparam(1); | 98 | lua_Object f = lua_getparam(1); |
80 | if (f == LUA_NOOBJECT) | 99 | if (f == LUA_NOOBJECT) { |
81 | closefile(lua_outfile); /* restore standart output */ | 100 | closefile("_OUTPUT"); |
101 | current = stdout; | ||
102 | } | ||
82 | else if (lua_tag(f) == lua_tagio) | 103 | else if (lua_tag(f) == lua_tagio) |
83 | lua_outfile = lua_getuserdata(f); | 104 | current = lua_getuserdata(f); |
84 | else { | 105 | else { |
85 | char *s = luaL_check_string(1); | 106 | char *s = luaL_check_string(1); |
86 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | 107 | current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); |
87 | if (fp) | 108 | if (current == NULL) { |
88 | lua_outfile = fp; | ||
89 | else { | ||
90 | pushresult(0); | 109 | pushresult(0); |
91 | return; | 110 | return; |
92 | } | 111 | } |
93 | } | 112 | } |
94 | lua_pushusertag(lua_outfile, lua_tagio); | 113 | setreturn(current, "_OUTPUT"); |
95 | } | 114 | } |
96 | 115 | ||
97 | 116 | ||
@@ -99,10 +118,8 @@ static void io_appendto (void) | |||
99 | { | 118 | { |
100 | char *s = luaL_check_string(1); | 119 | char *s = luaL_check_string(1); |
101 | FILE *fp = fopen (s, "a"); | 120 | FILE *fp = fopen (s, "a"); |
102 | if (fp != NULL) { | 121 | if (fp != NULL) |
103 | lua_outfile = fp; | 122 | setreturn(fp, "_OUTPUT"); |
104 | lua_pushusertag(lua_outfile, lua_tagio); | ||
105 | } | ||
106 | else | 123 | else |
107 | pushresult(0); | 124 | pushresult(0); |
108 | } | 125 | } |
@@ -112,6 +129,7 @@ static void io_appendto (void) | |||
112 | 129 | ||
113 | static void io_read (void) | 130 | static void io_read (void) |
114 | { | 131 | { |
132 | FILE *f = getfile("_INPUT"); | ||
115 | char *buff; | 133 | char *buff; |
116 | char *p = luaL_opt_string(1, "[^\n]*{\n}"); | 134 | char *p = luaL_opt_string(1, "[^\n]*{\n}"); |
117 | int inskip = 0; /* to control {skips} */ | 135 | int inskip = 0; /* to control {skips} */ |
@@ -131,7 +149,7 @@ static void io_read (void) | |||
131 | else { | 149 | else { |
132 | char *ep = luaL_item_end(p); /* get what is next */ | 150 | char *ep = luaL_item_end(p); /* get what is next */ |
133 | int m; /* match result */ | 151 | int m; /* match result */ |
134 | if (c == NEED_OTHER) c = getc(lua_infile); | 152 | if (c == NEED_OTHER) c = getc(f); |
135 | m = (c == EOF) ? 0 : luaL_singlematch((char)c, p); | 153 | m = (c == EOF) ? 0 : luaL_singlematch((char)c, p); |
136 | if (m) { | 154 | if (m) { |
137 | if (inskip == 0) luaI_addchar(c); | 155 | if (inskip == 0) luaI_addchar(c); |
@@ -152,7 +170,7 @@ static void io_read (void) | |||
152 | } | 170 | } |
153 | } break_while: | 171 | } break_while: |
154 | if (c >= 0) /* not EOF nor NEED_OTHER? */ | 172 | if (c >= 0) /* not EOF nor NEED_OTHER? */ |
155 | ungetc(c, lua_infile); | 173 | ungetc(c, f); |
156 | buff = luaI_addchar(0); | 174 | buff = luaI_addchar(0); |
157 | if (*buff != 0 || *p == 0) /* read something or did not fail? */ | 175 | if (*buff != 0 || *p == 0) /* read something or did not fail? */ |
158 | lua_pushstring(buff); | 176 | lua_pushstring(buff); |
@@ -161,11 +179,12 @@ static void io_read (void) | |||
161 | 179 | ||
162 | static void io_write (void) | 180 | static void io_write (void) |
163 | { | 181 | { |
182 | FILE *f = getfile("_OUTPUT"); | ||
164 | int arg = 1; | 183 | int arg = 1; |
165 | int status = 1; | 184 | int status = 1; |
166 | char *s; | 185 | char *s; |
167 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) | 186 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) |
168 | status = status && (fputs(s, lua_outfile) != EOF); | 187 | status = status && (fputs(s, f) != EOF); |
169 | pushresult(status); | 188 | pushresult(status); |
170 | } | 189 | } |
171 | 190 | ||
@@ -300,7 +319,11 @@ static struct luaL_reg iolib[] = { | |||
300 | void iolib_open (void) | 319 | void iolib_open (void) |
301 | { | 320 | { |
302 | lua_tagio = lua_newtag(); | 321 | lua_tagio = lua_newtag(); |
303 | lua_infile=stdin; lua_outfile=stdout; | 322 | setfile(stdin, "_INPUT"); |
323 | setfile(stdout, "_OUTPUT"); | ||
324 | setfile(stdin, "_STDIN"); | ||
325 | setfile(stdout, "_STDOUT"); | ||
326 | setfile(stderr, "_STDERR"); | ||
304 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 327 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
305 | lua_pushcfunction(errorfb); | 328 | lua_pushcfunction(errorfb); |
306 | lua_seterrormethod(); | 329 | lua_seterrormethod(); |