iolib.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: iolib.c,v 1.3 1994/03/28 15:14:02 celes 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 || !lua_isstring(o)) /* free format */
  170. {
  171. int c;
  172. char s[256];
  173. while (isspace(c=fgetc(in)))
  174. ;
  175. if (c == '\"')
  176. {
  177. int c, n=0;
  178. while((c = fgetc(in)) != '\"')
  179. {
  180. if (c == EOF)
  181. {
  182. lua_pushnil ();
  183. return;
  184. }
  185. s[n++] = c;
  186. }
  187. s[n] = 0;
  188. }
  189. else if (c == '\'')
  190. {
  191. int c, n=0;
  192. while((c = fgetc(in)) != '\'')
  193. {
  194. if (c == EOF)
  195. {
  196. lua_pushnil ();
  197. return;
  198. }
  199. s[n++] = c;
  200. }
  201. s[n] = 0;
  202. }
  203. else
  204. {
  205. char *ptr;
  206. double d;
  207. ungetc (c, in);
  208. if (fscanf (in, "%s", s) != 1)
  209. {
  210. lua_pushnil ();
  211. return;
  212. }
  213. d = strtod (s, &ptr);
  214. if (!(*ptr))
  215. {
  216. lua_pushnumber (d);
  217. return;
  218. }
  219. }
  220. lua_pushstring (s);
  221. return;
  222. }
  223. else /* formatted */
  224. {
  225. char *e = lua_getstring(o);
  226. char t;
  227. int m=0;
  228. while (isspace(*e)) e++;
  229. t = *e++;
  230. while (isdigit(*e))
  231. m = m*10 + (*e++ - '0');
  232. if (m > 0)
  233. {
  234. char f[80];
  235. char s[256];
  236. sprintf (f, "%%%ds", m);
  237. if (fgets (s, m, in) == NULL)
  238. {
  239. lua_pushnil();
  240. return;
  241. }
  242. else
  243. {
  244. if (s[strlen(s)-1] == '\n')
  245. s[strlen(s)-1] = 0;
  246. }
  247. switch (tolower(t))
  248. {
  249. case 'i':
  250. {
  251. long int l;
  252. sscanf (s, "%ld", &l);
  253. lua_pushnumber(l);
  254. }
  255. break;
  256. case 'f': case 'g': case 'e':
  257. {
  258. float f;
  259. sscanf (s, "%f", &f);
  260. lua_pushnumber(f);
  261. }
  262. break;
  263. default:
  264. lua_pushstring(s);
  265. break;
  266. }
  267. }
  268. else
  269. {
  270. switch (tolower(t))
  271. {
  272. case 'i':
  273. {
  274. long int l;
  275. if (fscanf (in, "%ld", &l) == EOF)
  276. lua_pushnil();
  277. else lua_pushnumber(l);
  278. }
  279. break;
  280. case 'f': case 'g': case 'e':
  281. {
  282. float f;
  283. if (fscanf (in, "%f", &f) == EOF)
  284. lua_pushnil();
  285. else lua_pushnumber(f);
  286. }
  287. break;
  288. default:
  289. {
  290. char s[256];
  291. if (fscanf (in, "%s", s) == EOF)
  292. lua_pushnil();
  293. else lua_pushstring(s);
  294. }
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. /*
  301. ** Write a variable. On error put 0 on stack, otherwise put 1.
  302. ** LUA interface:
  303. ** status = write (variable [,format])
  304. **
  305. ** O formato pode ter um dos seguintes especificadores:
  306. **
  307. ** s ou S -> para string
  308. ** f ou F, g ou G, e ou E -> para reais
  309. ** i ou I -> para inteiros
  310. **
  311. ** Estes especificadores podem vir seguidos de:
  312. **
  313. ** [?][m][.n]
  314. **
  315. ** onde:
  316. ** ? -> indica justificacao
  317. ** < = esquerda
  318. ** | = centro
  319. ** > = direita (default)
  320. ** m -> numero maximo de campos (se exceder estoura)
  321. ** n -> indica precisao para
  322. ** reais -> numero de casas decimais
  323. ** inteiros -> numero minimo de digitos
  324. ** string -> nao se aplica
  325. */
  326. static char *buildformat (char *e, lua_Object o)
  327. {
  328. static char buffer[512];
  329. static char f[80];
  330. char *string = &buffer[255];
  331. char t, j='r';
  332. int m=0, n=0, l;
  333. while (isspace(*e)) e++;
  334. t = *e++;
  335. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  336. while (isdigit(*e))
  337. m = m*10 + (*e++ - '0');
  338. e++; /* skip point */
  339. while (isdigit(*e))
  340. n = n*10 + (*e++ - '0');
  341. sprintf(f,"%%");
  342. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  343. if (m != 0) sprintf(strchr(f,0),"%d", m);
  344. if (n != 0) sprintf(strchr(f,0),".%d", n);
  345. sprintf(strchr(f,0), "%c", t);
  346. switch (tolower(t))
  347. {
  348. case 'i': t = 'i';
  349. sprintf (string, f, (long int)lua_getnumber(o));
  350. break;
  351. case 'f': case 'g': case 'e': t = 'f';
  352. sprintf (string, f, (float)lua_getnumber(o));
  353. break;
  354. case 's': t = 's';
  355. sprintf (string, f, lua_getstring(o));
  356. break;
  357. default: return "";
  358. }
  359. l = strlen(string);
  360. if (m!=0 && l>m)
  361. {
  362. int i;
  363. for (i=0; i<m; i++)
  364. string[i] = '*';
  365. string[i] = 0;
  366. }
  367. else if (m!=0 && j=='|')
  368. {
  369. int i=l-1;
  370. while (isspace(string[i])) i--;
  371. string -= (m-i) / 2;
  372. i=0;
  373. while (string[i]==0) string[i++] = ' ';
  374. string[l] = 0;
  375. }
  376. return string;
  377. }
  378. static void io_write (void)
  379. {
  380. lua_Object o1 = lua_getparam (1);
  381. lua_Object o2 = lua_getparam (2);
  382. if (o1 == NULL) /* new line */
  383. {
  384. fprintf (out, "\n");
  385. lua_pushnumber(1);
  386. }
  387. else if (o2 == NULL) /* free format */
  388. {
  389. int status=0;
  390. if (lua_isnumber(o1))
  391. status = fprintf (out, "%g", lua_getnumber(o1));
  392. else if (lua_isstring(o1))
  393. status = fprintf (out, "%s", lua_getstring(o1));
  394. lua_pushnumber(status);
  395. }
  396. else /* formated */
  397. {
  398. if (!lua_isstring(o2))
  399. {
  400. lua_error ("incorrect format to function `write'");
  401. lua_pushnumber(0);
  402. return;
  403. }
  404. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  405. }
  406. }
  407. /*
  408. ** Execute a executable program using "system".
  409. ** Return the result of execution.
  410. */
  411. void io_execute (void)
  412. {
  413. lua_Object o = lua_getparam (1);
  414. if (o == NULL || !lua_isstring (o))
  415. {
  416. lua_error ("incorrect argument to function 'execute`");
  417. lua_pushnumber (0);
  418. }
  419. else
  420. {
  421. int res = system(lua_getstring(o));
  422. lua_pushnumber (res);
  423. }
  424. return;
  425. }
  426. /*
  427. ** Remove a file.
  428. ** On error put 0 on stack, otherwise put 1.
  429. */
  430. void io_remove (void)
  431. {
  432. lua_Object o = lua_getparam (1);
  433. if (o == NULL || !lua_isstring (o))
  434. {
  435. lua_error ("incorrect argument to function 'execute`");
  436. lua_pushnumber (0);
  437. }
  438. else
  439. {
  440. if (remove(lua_getstring(o)) == 0)
  441. lua_pushnumber (1);
  442. else
  443. lua_pushnumber (0);
  444. }
  445. return;
  446. }
  447. /*
  448. ** Open io library
  449. */
  450. void iolib_open (void)
  451. {
  452. lua_register ("readfrom", io_readfrom);
  453. lua_register ("writeto", io_writeto);
  454. lua_register ("appendto", io_appendto);
  455. lua_register ("read", io_read);
  456. lua_register ("write", io_write);
  457. lua_register ("execute", io_execute);
  458. lua_register ("remove", io_remove);
  459. }