iolib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: iolib.c,v 1.7 1994/08/17 15:10:04 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 "mm.h"
  15. #include "lua.h"
  16. #include "lualib.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. ** Open a file to write appended.
  106. ** LUA interface:
  107. ** status = appendto (filename)
  108. ** where:
  109. ** status = 2 -> success (already exist)
  110. ** status = 1 -> success (new file)
  111. ** status = 0 -> error
  112. */
  113. static void io_appendto (void)
  114. {
  115. lua_Object o = lua_getparam (1);
  116. if (o == NULL) /* restore standart output */
  117. {
  118. if (out != stdout)
  119. {
  120. fclose (out);
  121. out = stdout;
  122. }
  123. lua_pushnumber (1);
  124. }
  125. else
  126. {
  127. if (!lua_isstring (o))
  128. {
  129. lua_error ("incorrect argument to function 'appendto`");
  130. lua_pushnumber (0);
  131. }
  132. else
  133. {
  134. int r;
  135. FILE *fp;
  136. struct stat st;
  137. if (stat(lua_getstring(o), &st) == -1) r = 1;
  138. else r = 2;
  139. fp = fopen (lua_getstring(o),"a");
  140. if (fp == NULL)
  141. {
  142. lua_pushnumber (0);
  143. }
  144. else
  145. {
  146. if (out != stdout) fclose (out);
  147. out = fp;
  148. lua_pushnumber (r);
  149. }
  150. }
  151. }
  152. }
  153. /*
  154. ** Read a variable. On error put nil on stack.
  155. ** LUA interface:
  156. ** variable = read ([format])
  157. **
  158. ** O formato pode ter um dos seguintes especificadores:
  159. **
  160. ** s ou S -> para string
  161. ** f ou F, g ou G, e ou E -> para reais
  162. ** i ou I -> para inteiros
  163. **
  164. ** Estes especificadores podem vir seguidos de numero que representa
  165. ** o numero de campos a serem lidos.
  166. */
  167. static void io_read (void)
  168. {
  169. lua_Object o = lua_getparam (1);
  170. if (o == NULL || !lua_isstring(o)) /* free format */
  171. {
  172. int c;
  173. char s[256];
  174. while (isspace(c=fgetc(in)))
  175. ;
  176. if (c == '\"')
  177. {
  178. int c, n=0;
  179. while((c = fgetc(in)) != '\"')
  180. {
  181. if (c == EOF)
  182. {
  183. lua_pushnil ();
  184. return;
  185. }
  186. s[n++] = c;
  187. }
  188. s[n] = 0;
  189. }
  190. else if (c == '\'')
  191. {
  192. int c, n=0;
  193. while((c = fgetc(in)) != '\'')
  194. {
  195. if (c == EOF)
  196. {
  197. lua_pushnil ();
  198. return;
  199. }
  200. s[n++] = c;
  201. }
  202. s[n] = 0;
  203. }
  204. else
  205. {
  206. char *ptr;
  207. double d;
  208. ungetc (c, in);
  209. if (fscanf (in, "%s", s) != 1)
  210. {
  211. lua_pushnil ();
  212. return;
  213. }
  214. d = strtod (s, &ptr);
  215. if (!(*ptr))
  216. {
  217. lua_pushnumber (d);
  218. return;
  219. }
  220. }
  221. lua_pushstring (s);
  222. return;
  223. }
  224. else /* formatted */
  225. {
  226. char *e = lua_getstring(o);
  227. char t;
  228. int m=0;
  229. while (isspace(*e)) e++;
  230. t = *e++;
  231. while (isdigit(*e))
  232. m = m*10 + (*e++ - '0');
  233. if (m > 0)
  234. {
  235. char f[80];
  236. char s[256];
  237. sprintf (f, "%%%ds", m);
  238. if (fgets (s, m, in) == NULL)
  239. {
  240. lua_pushnil();
  241. return;
  242. }
  243. else
  244. {
  245. if (s[strlen(s)-1] == '\n')
  246. s[strlen(s)-1] = 0;
  247. }
  248. switch (tolower(t))
  249. {
  250. case 'i':
  251. {
  252. long int l;
  253. sscanf (s, "%ld", &l);
  254. lua_pushnumber(l);
  255. }
  256. break;
  257. case 'f': case 'g': case 'e':
  258. {
  259. float f;
  260. sscanf (s, "%f", &f);
  261. lua_pushnumber(f);
  262. }
  263. break;
  264. default:
  265. lua_pushstring(s);
  266. break;
  267. }
  268. }
  269. else
  270. {
  271. switch (tolower(t))
  272. {
  273. case 'i':
  274. {
  275. long int l;
  276. if (fscanf (in, "%ld", &l) == EOF)
  277. lua_pushnil();
  278. else lua_pushnumber(l);
  279. }
  280. break;
  281. case 'f': case 'g': case 'e':
  282. {
  283. float f;
  284. if (fscanf (in, "%f", &f) == EOF)
  285. lua_pushnil();
  286. else lua_pushnumber(f);
  287. }
  288. break;
  289. default:
  290. {
  291. char s[256];
  292. if (fscanf (in, "%s", s) == EOF)
  293. lua_pushnil();
  294. else lua_pushstring(s);
  295. }
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. /*
  302. ** Write a variable. On error put 0 on stack, otherwise put 1.
  303. ** LUA interface:
  304. ** status = write (variable [,format])
  305. **
  306. ** O formato pode ter um dos seguintes especificadores:
  307. **
  308. ** s ou S -> para string
  309. ** f ou F, g ou G, e ou E -> para reais
  310. ** i ou I -> para inteiros
  311. **
  312. ** Estes especificadores podem vir seguidos de:
  313. **
  314. ** [?][m][.n]
  315. **
  316. ** onde:
  317. ** ? -> indica justificacao
  318. ** < = esquerda
  319. ** | = centro
  320. ** > = direita (default)
  321. ** m -> numero maximo de campos (se exceder estoura)
  322. ** n -> indica precisao para
  323. ** reais -> numero de casas decimais
  324. ** inteiros -> numero minimo de digitos
  325. ** string -> nao se aplica
  326. */
  327. static char *buildformat (char *e, lua_Object o)
  328. {
  329. static char buffer[512];
  330. static char f[80];
  331. char *string = &buffer[255];
  332. char *fstart=e, *fspace;
  333. char t, j='r';
  334. int m=0, n=0, l;
  335. while (isspace(*e)) e++;
  336. fspace = e;
  337. t = *e++;
  338. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  339. while (isdigit(*e))
  340. m = m*10 + (*e++ - '0');
  341. e++; /* skip point */
  342. while (isdigit(*e))
  343. n = n*10 + (*e++ - '0');
  344. sprintf(f,"%%");
  345. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  346. if (m != 0) sprintf(strchr(f,0),"%d", m);
  347. if (n != 0) sprintf(strchr(f,0),".%d", n);
  348. switch (t)
  349. {
  350. case 'i': case 'I': t = 'd';
  351. sprintf(strchr(f,0), "%c", t);
  352. sprintf (string, f, (long int)lua_getnumber(o));
  353. break;
  354. case 'f': case 'g': case 'e': case 'G': case 'E':
  355. sprintf(strchr(f,0), "%c", t);
  356. sprintf (string, f, (float)lua_getnumber(o));
  357. break;
  358. case 'F': t = 'f';
  359. sprintf(strchr(f,0), "%c", t);
  360. sprintf (string, f, (float)lua_getnumber(o));
  361. break;
  362. case 's': case 'S': t = 's';
  363. sprintf(strchr(f,0), "%c", t);
  364. sprintf (string, f, lua_getstring(o));
  365. break;
  366. default: return "";
  367. }
  368. l = strlen(string);
  369. if (m!=0 && l>m)
  370. {
  371. int i;
  372. for (i=0; i<m; i++)
  373. string[i] = '*';
  374. string[i] = 0;
  375. }
  376. else if (m!=0 && j=='|')
  377. {
  378. int k;
  379. int i=l-1;
  380. while (isspace(string[i]) || string[i]==0) i--;
  381. string -= (m-i)/2;
  382. for(k=0; k<(m-i)/2; k++)
  383. string[k] = ' ';
  384. }
  385. /* add space characteres */
  386. while (fspace != fstart)
  387. {
  388. string--;
  389. fspace--;
  390. *string = *fspace;
  391. }
  392. while (isspace(*e)) string[l++] = *e++;
  393. string[l] = 0;
  394. return string;
  395. }
  396. static void io_write (void)
  397. {
  398. lua_Object o1 = lua_getparam (1);
  399. lua_Object o2 = lua_getparam (2);
  400. if (o1 == NULL) /* new line */
  401. {
  402. fprintf (out, "\n");
  403. lua_pushnumber(1);
  404. }
  405. else if (o2 == NULL) /* free format */
  406. {
  407. int status=0;
  408. if (lua_isnumber(o1))
  409. status = fprintf (out, "%g", lua_getnumber(o1));
  410. else if (lua_isstring(o1))
  411. status = fprintf (out, "%s", lua_getstring(o1));
  412. lua_pushnumber(status);
  413. }
  414. else /* formated */
  415. {
  416. if (!lua_isstring(o2))
  417. {
  418. lua_error ("incorrect format to function `write'");
  419. lua_pushnumber(0);
  420. return;
  421. }
  422. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  423. }
  424. }
  425. /*
  426. ** Execute a executable program using "system".
  427. ** Return the result of execution.
  428. */
  429. static void io_execute (void)
  430. {
  431. lua_Object o = lua_getparam (1);
  432. if (o == NULL || !lua_isstring (o))
  433. {
  434. lua_error ("incorrect argument to function 'execute`");
  435. lua_pushnumber (0);
  436. }
  437. else
  438. {
  439. int res = system(lua_getstring(o));
  440. lua_pushnumber (res);
  441. }
  442. return;
  443. }
  444. /*
  445. ** Remove a file.
  446. ** On error put 0 on stack, otherwise put 1.
  447. */
  448. static void io_remove (void)
  449. {
  450. lua_Object o = lua_getparam (1);
  451. if (o == NULL || !lua_isstring (o))
  452. {
  453. lua_error ("incorrect argument to function 'execute`");
  454. lua_pushnumber (0);
  455. }
  456. else
  457. {
  458. if (remove(lua_getstring(o)) == 0)
  459. lua_pushnumber (1);
  460. else
  461. lua_pushnumber (0);
  462. }
  463. return;
  464. }
  465. /*
  466. ** To get a environment variables
  467. */
  468. static void io_getenv (void)
  469. {
  470. lua_Object s = lua_getparam(1);
  471. if (!lua_isstring(s))
  472. lua_pushnil();
  473. else
  474. {
  475. char *env = getenv(lua_getstring(s));
  476. if (env == NULL) lua_pushnil();
  477. else lua_pushstring(env);
  478. }
  479. }
  480. /*
  481. ** To abort
  482. */
  483. static void io_abort (void)
  484. {
  485. lua_Object o = lua_getparam(1);
  486. if (lua_isstring(o))
  487. printf("%s\n", lua_getstring(o));
  488. exit(1);
  489. }
  490. /*
  491. ** To debug a lua program. Start a dialog with the user, interpreting
  492. lua commands until an 'cont'.
  493. */
  494. static void io_debug (void)
  495. {
  496. while (1)
  497. {
  498. char buffer[250];
  499. printf("lua_debug> ");
  500. if (gets(buffer) == 0) return;
  501. if (strcmp(buffer, "cont") == 0) return;
  502. lua_dostring(buffer);
  503. }
  504. }
  505. /*
  506. ** Open io library
  507. */
  508. void iolib_open (void)
  509. {
  510. lua_register ("readfrom", io_readfrom);
  511. lua_register ("writeto", io_writeto);
  512. lua_register ("appendto", io_appendto);
  513. lua_register ("read", io_read);
  514. lua_register ("write", io_write);
  515. lua_register ("execute", io_execute);
  516. lua_register ("remove", io_remove);
  517. lua_register ("getenv", io_getenv);
  518. lua_register ("abort", io_abort);
  519. lua_register ("debug", io_debug);
  520. }