iolib.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: $";
  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. fscanf (in, "%ld", &l);
  263. lua_pushnumber(l);
  264. }
  265. break;
  266. case 'f': case 'g': case 'e':
  267. {
  268. float f;
  269. fscanf (in, "%f", &f);
  270. lua_pushnumber(f);
  271. }
  272. break;
  273. default:
  274. {
  275. char s[256];
  276. fscanf (in, "%s", s);
  277. lua_pushstring(s);
  278. }
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. /*
  285. ** Write a variable. On error put 0 on stack, otherwise put 1.
  286. ** LUA interface:
  287. ** status = write (variable [,format])
  288. **
  289. ** O formato pode ter um dos seguintes especificadores:
  290. **
  291. ** s ou S -> para string
  292. ** f ou F, g ou G, e ou E -> para reais
  293. ** i ou I -> para inteiros
  294. **
  295. ** Estes especificadores podem vir seguidos de:
  296. **
  297. ** [?][m][.n]
  298. **
  299. ** onde:
  300. ** ? -> indica justificacao
  301. ** < = esquerda
  302. ** | = centro
  303. ** > = direita (default)
  304. ** m -> numero maximo de campos (se exceder estoura)
  305. ** n -> indica precisao para
  306. ** reais -> numero de casas decimais
  307. ** inteiros -> numero minimo de digitos
  308. ** string -> nao se aplica
  309. */
  310. static char *buildformat (char *e, lua_Object o)
  311. {
  312. static char buffer[512];
  313. static char f[80];
  314. char *string = &buffer[255];
  315. char t, j='r';
  316. int m=0, n=0, l;
  317. while (isspace(*e)) e++;
  318. t = *e++;
  319. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  320. while (isdigit(*e))
  321. m = m*10 + (*e++ - '0');
  322. e++; /* skip point */
  323. while (isdigit(*e))
  324. n = n*10 + (*e++ - '0');
  325. sprintf(f,"%%");
  326. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  327. if (m != 0) sprintf(strchr(f,0),"%d", m);
  328. if (n != 0) sprintf(strchr(f,0),".%d", n);
  329. sprintf(strchr(f,0), "%c", t);
  330. switch (tolower(t))
  331. {
  332. case 'i': t = 'i';
  333. sprintf (string, f, (long int)lua_getnumber(o));
  334. break;
  335. case 'f': case 'g': case 'e': t = 'f';
  336. sprintf (string, f, (float)lua_getnumber(o));
  337. break;
  338. case 's': t = 's';
  339. sprintf (string, f, lua_getstring(o));
  340. break;
  341. default: return "";
  342. }
  343. l = strlen(string);
  344. if (m!=0 && l>m)
  345. {
  346. int i;
  347. for (i=0; i<m; i++)
  348. string[i] = '*';
  349. string[i] = 0;
  350. }
  351. else if (m!=0 && j=='|')
  352. {
  353. int i=l-1;
  354. while (isspace(string[i])) i--;
  355. string -= (m-i) / 2;
  356. i=0;
  357. while (string[i]==0) string[i++] = ' ';
  358. string[l] = 0;
  359. }
  360. return string;
  361. }
  362. static void io_write (void)
  363. {
  364. lua_Object o1 = lua_getparam (1);
  365. lua_Object o2 = lua_getparam (2);
  366. if (o1 == NULL) /* new line */
  367. {
  368. fprintf (out, "\n");
  369. lua_pushnumber(1);
  370. }
  371. else if (o2 == NULL) /* free format */
  372. {
  373. int status=0;
  374. if (lua_isnumber(o1))
  375. status = fprintf (out, "%g", lua_getnumber(o1));
  376. else if (lua_isstring(o1))
  377. status = fprintf (out, "%s", lua_getstring(o1));
  378. lua_pushnumber(status);
  379. }
  380. else /* formated */
  381. {
  382. if (!lua_isstring(o2))
  383. {
  384. lua_error ("incorrect format to function `write'");
  385. lua_pushnumber(0);
  386. return;
  387. }
  388. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  389. }
  390. }
  391. /*
  392. ** Execute a executable program using "sustem".
  393. ** On error put 0 on stack, otherwise put 1.
  394. */
  395. void io_execute (void)
  396. {
  397. lua_Object o = lua_getparam (1);
  398. if (o == NULL || !lua_isstring (o))
  399. {
  400. lua_error ("incorrect argument to function 'execute`");
  401. lua_pushnumber (0);
  402. }
  403. else
  404. {
  405. system(lua_getstring(o));
  406. lua_pushnumber (1);
  407. }
  408. return;
  409. }
  410. /*
  411. ** Remove a file.
  412. ** On error put 0 on stack, otherwise put 1.
  413. */
  414. void io_remove (void)
  415. {
  416. lua_Object o = lua_getparam (1);
  417. if (o == NULL || !lua_isstring (o))
  418. {
  419. lua_error ("incorrect argument to function 'execute`");
  420. lua_pushnumber (0);
  421. }
  422. else
  423. {
  424. if (remove(lua_getstring(o)) == 0)
  425. lua_pushnumber (1);
  426. else
  427. lua_pushnumber (0);
  428. }
  429. return;
  430. }
  431. /*
  432. ** Open io library
  433. */
  434. void iolib_open (void)
  435. {
  436. lua_register ("readfrom", io_readfrom);
  437. lua_register ("writeto", io_writeto);
  438. lua_register ("appendto", io_appendto);
  439. lua_register ("read", io_read);
  440. lua_register ("write", io_write);
  441. lua_register ("execute", io_execute);
  442. lua_register ("remove", io_remove);
  443. }