iolib.c 8.2 KB

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