diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-04 10:53:10 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-10-04 10:53:10 -0300 |
| commit | ec785a1d6517a3c247f4e6682deb1c9e2610db0e (patch) | |
| tree | 7b940e2c123efdb77ac41a9e840810053c562387 | |
| parent | e0621e6115366f2cd041aa118df145d6c9ba5965 (diff) | |
| download | lua-ec785a1d6517a3c247f4e6682deb1c9e2610db0e.tar.gz lua-ec785a1d6517a3c247f4e6682deb1c9e2610db0e.tar.bz2 lua-ec785a1d6517a3c247f4e6682deb1c9e2610db0e.zip | |
new option for "writeto" and "readfrom", to allow piped I/O on
machines that support it.
Diffstat (limited to '')
| -rw-r--r-- | iolib.c | 69 |
1 files changed, 38 insertions, 31 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.20 1995/02/02 18:54:58 roberto Exp roberto $"; | 6 | char *rcs_iolib="$Id: iolib.c,v 1.21 1995/02/06 19:36:13 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <stdio.h> | 8 | #include <stdio.h> |
| 9 | #include <ctype.h> | 9 | #include <ctype.h> |
| @@ -18,6 +18,32 @@ char *rcs_iolib="$Id: iolib.c,v 1.20 1995/02/02 18:54:58 roberto Exp roberto $"; | |||
| 18 | 18 | ||
| 19 | static FILE *in=stdin, *out=stdout; | 19 | static FILE *in=stdin, *out=stdout; |
| 20 | 20 | ||
| 21 | |||
| 22 | #ifndef POPEN | ||
| 23 | #define popen(x,y) NULL /* that is, popen always fails */ | ||
| 24 | #define pclose(x) (-1) | ||
| 25 | #endif | ||
| 26 | |||
| 27 | static void closeread (void) | ||
| 28 | { | ||
| 29 | if (in != stdin) | ||
| 30 | { | ||
| 31 | if (pclose(in) == -1) | ||
| 32 | fclose(in); | ||
| 33 | in = stdin; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | static void closewrite (void) | ||
| 38 | { | ||
| 39 | if (out != stdout) | ||
| 40 | { | ||
| 41 | if (pclose(out) == -1) | ||
| 42 | fclose(out); | ||
| 43 | out = stdout; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 21 | /* | 47 | /* |
| 22 | ** Open a file to read. | 48 | ** Open a file to read. |
| 23 | ** LUA interface: | 49 | ** LUA interface: |
| @@ -31,11 +57,7 @@ static void io_readfrom (void) | |||
| 31 | lua_Object o = lua_getparam (1); | 57 | lua_Object o = lua_getparam (1); |
| 32 | if (o == LUA_NOOBJECT) /* restore standart input */ | 58 | if (o == LUA_NOOBJECT) /* restore standart input */ |
| 33 | { | 59 | { |
| 34 | if (in != stdin) | 60 | closeread(); |
| 35 | { | ||
| 36 | fclose (in); | ||
| 37 | in = stdin; | ||
| 38 | } | ||
| 39 | lua_pushnumber (1); | 61 | lua_pushnumber (1); |
| 40 | } | 62 | } |
| 41 | else | 63 | else |
| @@ -47,14 +69,15 @@ static void io_readfrom (void) | |||
| 47 | } | 69 | } |
| 48 | else | 70 | else |
| 49 | { | 71 | { |
| 50 | FILE *fp = fopen (lua_getstring(o),"r"); | 72 | char *s = lua_getstring(o); |
| 73 | FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | ||
| 51 | if (fp == NULL) | 74 | if (fp == NULL) |
| 52 | { | 75 | { |
| 53 | lua_pushnumber (0); | 76 | lua_pushnumber (0); |
| 54 | } | 77 | } |
| 55 | else | 78 | else |
| 56 | { | 79 | { |
| 57 | if (in != stdin) fclose (in); | 80 | closeread(); |
| 58 | in = fp; | 81 | in = fp; |
| 59 | lua_pushnumber (1); | 82 | lua_pushnumber (1); |
| 60 | } | 83 | } |
| @@ -76,11 +99,7 @@ static void io_writeto (void) | |||
| 76 | lua_Object o = lua_getparam (1); | 99 | lua_Object o = lua_getparam (1); |
| 77 | if (o == LUA_NOOBJECT) /* restore standart output */ | 100 | if (o == LUA_NOOBJECT) /* restore standart output */ |
| 78 | { | 101 | { |
| 79 | if (out != stdout) | 102 | closewrite(); |
| 80 | { | ||
| 81 | fclose (out); | ||
| 82 | out = stdout; | ||
| 83 | } | ||
| 84 | lua_pushnumber (1); | 103 | lua_pushnumber (1); |
| 85 | } | 104 | } |
| 86 | else | 105 | else |
| @@ -92,14 +111,15 @@ static void io_writeto (void) | |||
| 92 | } | 111 | } |
| 93 | else | 112 | else |
| 94 | { | 113 | { |
| 95 | FILE *fp = fopen (lua_getstring(o),"w"); | 114 | char *s = lua_getstring(o); |
| 115 | FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | ||
| 96 | if (fp == NULL) | 116 | if (fp == NULL) |
| 97 | { | 117 | { |
| 98 | lua_pushnumber (0); | 118 | lua_pushnumber (0); |
| 99 | } | 119 | } |
| 100 | else | 120 | else |
| 101 | { | 121 | { |
| 102 | if (out != stdout) fclose (out); | 122 | closewrite(); |
| 103 | out = fp; | 123 | out = fp; |
| 104 | lua_pushnumber (1); | 124 | lua_pushnumber (1); |
| 105 | } | 125 | } |
| @@ -120,24 +140,13 @@ static void io_writeto (void) | |||
| 120 | static void io_appendto (void) | 140 | static void io_appendto (void) |
| 121 | { | 141 | { |
| 122 | lua_Object o = lua_getparam (1); | 142 | lua_Object o = lua_getparam (1); |
| 123 | if (o == LUA_NOOBJECT) /* restore standart output */ | 143 | if (!lua_isstring (o)) |
| 124 | { | 144 | { |
| 125 | if (out != stdout) | 145 | lua_error ("incorrect argument to function 'appendto`"); |
| 126 | { | 146 | lua_pushnumber (0); |
| 127 | fclose (out); | ||
| 128 | out = stdout; | ||
| 129 | } | ||
| 130 | lua_pushnumber (1); | ||
| 131 | } | 147 | } |
| 132 | else | 148 | else |
| 133 | { | 149 | { |
| 134 | if (!lua_isstring (o)) | ||
| 135 | { | ||
| 136 | lua_error ("incorrect argument to function 'appendto`"); | ||
| 137 | lua_pushnumber (0); | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | int r; | 150 | int r; |
| 142 | FILE *fp; | 151 | FILE *fp; |
| 143 | struct stat st; | 152 | struct stat st; |
| @@ -154,12 +163,10 @@ static void io_appendto (void) | |||
| 154 | out = fp; | 163 | out = fp; |
| 155 | lua_pushnumber (r); | 164 | lua_pushnumber (r); |
| 156 | } | 165 | } |
| 157 | } | ||
| 158 | } | 166 | } |
| 159 | } | 167 | } |
| 160 | 168 | ||
| 161 | 169 | ||
| 162 | |||
| 163 | /* | 170 | /* |
| 164 | ** Read a variable. On error put nil on stack. | 171 | ** Read a variable. On error put nil on stack. |
| 165 | ** LUA interface: | 172 | ** LUA interface: |
