2
0

iolib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. **
  5. ** Waldemar Celes Filho
  6. ** TeCGraf - PUC-Rio
  7. ** 19 May 93
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #ifdef __GNUC__
  14. #include <floatingpoint.h>
  15. #endif
  16. #include "lua.h"
  17. static FILE *in=stdin, *out=stdout;
  18. /*
  19. ** Open a file to read.
  20. ** LUA interface:
  21. ** status = readfrom (filename)
  22. ** where:
  23. ** status = 1 -> success
  24. ** status = 0 -> error
  25. */
  26. static void io_readfrom (void)
  27. {
  28. lua_Object o = lua_getparam (1);
  29. if (o == NULL) /* restore standart input */
  30. {
  31. if (in != stdin)
  32. {
  33. fclose (in);
  34. in = stdin;
  35. }
  36. lua_pushnumber (1);
  37. }
  38. else
  39. {
  40. if (!lua_isstring (o))
  41. {
  42. lua_error ("incorrect argument to function 'readfrom`");
  43. lua_pushnumber (0);
  44. }
  45. else
  46. {
  47. FILE *fp = fopen (lua_getstring(o),"r");
  48. if (fp == NULL)
  49. {
  50. lua_pushnumber (0);
  51. }
  52. else
  53. {
  54. if (in != stdin) fclose (in);
  55. in = fp;
  56. lua_pushnumber (1);
  57. }
  58. }
  59. }
  60. }
  61. /*
  62. ** Open a file to write.
  63. ** LUA interface:
  64. ** status = writeto (filename)
  65. ** where:
  66. ** status = 1 -> success
  67. ** status = 0 -> error
  68. */
  69. static void io_writeto (void)
  70. {
  71. lua_Object o = lua_getparam (1);
  72. if (o == NULL) /* restore standart output */
  73. {
  74. if (out != stdout)
  75. {
  76. fclose (out);
  77. out = stdout;
  78. }
  79. lua_pushnumber (1);
  80. }
  81. else
  82. {
  83. if (!lua_isstring (o))
  84. {
  85. lua_error ("incorrect argument to function 'writeto`");
  86. lua_pushnumber (0);
  87. }
  88. else
  89. {
  90. FILE *fp = fopen (lua_getstring(o),"w");
  91. if (fp == NULL)
  92. {
  93. lua_pushnumber (0);
  94. }
  95. else
  96. {
  97. if (out != stdout) fclose (out);
  98. out = fp;
  99. lua_pushnumber (1);
  100. }
  101. }
  102. }
  103. }
  104. /*
  105. ** Read a variable. On error put nil on stack.
  106. ** LUA interface:
  107. ** variable = read ([format])
  108. **
  109. ** O formato pode ter um dos seguintes especificadores:
  110. **
  111. ** s ou S -> para string
  112. ** f ou F, g ou G, e ou E -> para reais
  113. ** i ou I -> para inteiros
  114. **
  115. ** Estes especificadores podem vir seguidos de numero que representa
  116. ** o numero de campos a serem lidos.
  117. */
  118. static void io_read (void)
  119. {
  120. lua_Object o = lua_getparam (1);
  121. if (o == NULL) /* free format */
  122. {
  123. int c;
  124. char s[256];
  125. while (isspace(c=fgetc(in)))
  126. ;
  127. if (c == '\"')
  128. {
  129. if (fscanf (in, "%[^\"]\"", s) != 1)
  130. {
  131. lua_pushnil ();
  132. return;
  133. }
  134. }
  135. else if (c == '\'')
  136. {
  137. if (fscanf (in, "%[^\']\'", s) != 1)
  138. {
  139. lua_pushnil ();
  140. return;
  141. }
  142. }
  143. else
  144. {
  145. char *ptr;
  146. double d;
  147. ungetc (c, in);
  148. if (fscanf (in, "%s", s) != 1)
  149. {
  150. lua_pushnil ();
  151. return;
  152. }
  153. d = strtod (s, &ptr);
  154. if (!(*ptr))
  155. {
  156. lua_pushnumber (d);
  157. return;
  158. }
  159. }
  160. lua_pushstring (s);
  161. return;
  162. }
  163. else /* formatted */
  164. {
  165. char *e = lua_getstring(o);
  166. char t;
  167. int m=0;
  168. while (isspace(*e)) e++;
  169. t = *e++;
  170. while (isdigit(*e))
  171. m = m*10 + (*e++ - '0');
  172. if (m > 0)
  173. {
  174. char f[80];
  175. char s[256];
  176. sprintf (f, "%%%ds", m);
  177. fscanf (in, f, s);
  178. switch (tolower(t))
  179. {
  180. case 'i':
  181. {
  182. long int l;
  183. sscanf (s, "%ld", &l);
  184. lua_pushnumber(l);
  185. }
  186. break;
  187. case 'f': case 'g': case 'e':
  188. {
  189. float f;
  190. sscanf (s, "%f", &f);
  191. lua_pushnumber(f);
  192. }
  193. break;
  194. default:
  195. lua_pushstring(s);
  196. break;
  197. }
  198. }
  199. else
  200. {
  201. switch (tolower(t))
  202. {
  203. case 'i':
  204. {
  205. long int l;
  206. fscanf (in, "%ld", &l);
  207. lua_pushnumber(l);
  208. }
  209. break;
  210. case 'f': case 'g': case 'e':
  211. {
  212. float f;
  213. fscanf (in, "%f", &f);
  214. lua_pushnumber(f);
  215. }
  216. break;
  217. default:
  218. {
  219. char s[256];
  220. fscanf (in, "%s", s);
  221. lua_pushstring(s);
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. /*
  229. ** Write a variable. On error put 0 on stack, otherwise put 1.
  230. ** LUA interface:
  231. ** status = write (variable [,format])
  232. **
  233. ** O formato pode ter um dos seguintes especificadores:
  234. **
  235. ** s ou S -> para string
  236. ** f ou F, g ou G, e ou E -> para reais
  237. ** i ou I -> para inteiros
  238. **
  239. ** Estes especificadores podem vir seguidos de:
  240. **
  241. ** [?][m][.n]
  242. **
  243. ** onde:
  244. ** ? -> indica justificacao
  245. ** < = esquerda
  246. ** | = centro
  247. ** > = direita (default)
  248. ** m -> numero maximo de campos (se exceder estoura)
  249. ** n -> indica precisao para
  250. ** reais -> numero de casas decimais
  251. ** inteiros -> numero minimo de digitos
  252. ** string -> nao se aplica
  253. */
  254. static char *buildformat (char *e, lua_Object o)
  255. {
  256. static char buffer[512];
  257. static char f[80];
  258. char *string = &buffer[255];
  259. char t, j='r';
  260. int m=0, n=0, l;
  261. while (isspace(*e)) e++;
  262. t = *e++;
  263. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  264. while (isdigit(*e))
  265. m = m*10 + (*e++ - '0');
  266. e++; /* skip point */
  267. while (isdigit(*e))
  268. n = n*10 + (*e++ - '0');
  269. sprintf(f,"%%");
  270. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  271. if (m != 0) sprintf(strchr(f,0),"%d", m);
  272. if (n != 0) sprintf(strchr(f,0),".%d", n);
  273. sprintf(strchr(f,0), "%c", t);
  274. switch (tolower(t))
  275. {
  276. case 'i': t = 'i';
  277. sprintf (string, f, (long int)lua_getnumber(o));
  278. break;
  279. case 'f': case 'g': case 'e': t = 'f';
  280. sprintf (string, f, (float)lua_getnumber(o));
  281. break;
  282. case 's': t = 's';
  283. sprintf (string, f, lua_getstring(o));
  284. break;
  285. default: return "";
  286. }
  287. l = strlen(string);
  288. if (m!=0 && l>m)
  289. {
  290. int i;
  291. for (i=0; i<m; i++)
  292. string[i] = '*';
  293. string[i] = 0;
  294. }
  295. else if (m!=0 && j=='|')
  296. {
  297. int i=l-1;
  298. while (isspace(string[i])) i--;
  299. string -= (m-i) / 2;
  300. i=0;
  301. while (string[i]==0) string[i++] = ' ';
  302. string[l] = 0;
  303. }
  304. return string;
  305. }
  306. static void io_write (void)
  307. {
  308. lua_Object o1 = lua_getparam (1);
  309. lua_Object o2 = lua_getparam (2);
  310. if (o1 == NULL) /* new line */
  311. {
  312. fprintf (out, "\n");
  313. lua_pushnumber(1);
  314. }
  315. else if (o2 == NULL) /* free format */
  316. {
  317. int status=0;
  318. if (lua_isnumber(o1))
  319. status = fprintf (out, "%g", lua_getnumber(o1));
  320. else if (lua_isstring(o1))
  321. status = fprintf (out, "%s", lua_getstring(o1));
  322. lua_pushnumber(status);
  323. }
  324. else /* formated */
  325. {
  326. if (!lua_isstring(o2))
  327. {
  328. lua_error ("incorrect format to function `write'");
  329. lua_pushnumber(0);
  330. return;
  331. }
  332. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  333. }
  334. }
  335. /*
  336. ** Execute a executable program using "sustem".
  337. ** On error put 0 on stack, otherwise put 1.
  338. */
  339. void io_execute (void)
  340. {
  341. lua_Object o = lua_getparam (1);
  342. if (o == NULL || !lua_isstring (o))
  343. {
  344. lua_error ("incorrect argument to function 'execute`");
  345. lua_pushnumber (0);
  346. }
  347. else
  348. {
  349. system(lua_getstring(o));
  350. lua_pushnumber (1);
  351. }
  352. return;
  353. }
  354. /*
  355. ** Remove a file.
  356. ** On error put 0 on stack, otherwise put 1.
  357. */
  358. void io_remove (void)
  359. {
  360. lua_Object o = lua_getparam (1);
  361. if (o == NULL || !lua_isstring (o))
  362. {
  363. lua_error ("incorrect argument to function 'execute`");
  364. lua_pushnumber (0);
  365. }
  366. else
  367. {
  368. if (remove(lua_getstring(o)) == 0)
  369. lua_pushnumber (1);
  370. else
  371. lua_pushnumber (0);
  372. }
  373. return;
  374. }
  375. /*
  376. ** Open io library
  377. */
  378. void iolib_open (void)
  379. {
  380. lua_register ("readfrom", io_readfrom);
  381. lua_register ("writeto", io_writeto);
  382. lua_register ("read", io_read);
  383. lua_register ("write", io_write);
  384. lua_register ("execute", io_execute);
  385. lua_register ("remove", io_remove);
  386. }