iolib.c 8.1 KB

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