iolib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: iolib.c,v 1.21 1995/02/06 19:36:13 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. #include "lua.h"
  14. #include "lualib.h"
  15. static FILE *in=stdin, *out=stdout;
  16. #ifndef POPEN
  17. #define popen(x,y) NULL /* that is, popen always fails */
  18. #define pclose(x) (-1)
  19. #endif
  20. static void closeread (void)
  21. {
  22. if (in != stdin)
  23. {
  24. if (pclose(in) == -1)
  25. fclose(in);
  26. in = stdin;
  27. }
  28. }
  29. static void closewrite (void)
  30. {
  31. if (out != stdout)
  32. {
  33. if (pclose(out) == -1)
  34. fclose(out);
  35. out = stdout;
  36. }
  37. }
  38. /*
  39. ** Open a file to read.
  40. ** LUA interface:
  41. ** status = readfrom (filename)
  42. ** where:
  43. ** status = 1 -> success
  44. ** status = 0 -> error
  45. */
  46. static void io_readfrom (void)
  47. {
  48. lua_Object o = lua_getparam (1);
  49. if (o == LUA_NOOBJECT) /* restore standart input */
  50. {
  51. closeread();
  52. lua_pushnumber (1);
  53. }
  54. else
  55. {
  56. if (!lua_isstring (o))
  57. {
  58. lua_error ("incorrect argument to function 'readfrom`");
  59. lua_pushnumber (0);
  60. }
  61. else
  62. {
  63. char *s = lua_getstring(o);
  64. FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
  65. if (fp == NULL)
  66. {
  67. lua_pushnumber (0);
  68. }
  69. else
  70. {
  71. closeread();
  72. in = fp;
  73. lua_pushnumber (1);
  74. }
  75. }
  76. }
  77. }
  78. /*
  79. ** Open a file to write.
  80. ** LUA interface:
  81. ** status = writeto (filename)
  82. ** where:
  83. ** status = 1 -> success
  84. ** status = 0 -> error
  85. */
  86. static void io_writeto (void)
  87. {
  88. lua_Object o = lua_getparam (1);
  89. if (o == LUA_NOOBJECT) /* restore standart output */
  90. {
  91. closewrite();
  92. lua_pushnumber (1);
  93. }
  94. else
  95. {
  96. if (!lua_isstring (o))
  97. {
  98. lua_error ("incorrect argument to function 'writeto`");
  99. lua_pushnumber (0);
  100. }
  101. else
  102. {
  103. char *s = lua_getstring(o);
  104. FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
  105. if (fp == NULL)
  106. {
  107. lua_pushnumber (0);
  108. }
  109. else
  110. {
  111. closewrite();
  112. out = fp;
  113. lua_pushnumber (1);
  114. }
  115. }
  116. }
  117. }
  118. /*
  119. ** Open a file to write appended.
  120. ** LUA interface:
  121. ** status = appendto (filename)
  122. ** where:
  123. ** status = 2 -> success (already exist)
  124. ** status = 1 -> success (new file)
  125. ** status = 0 -> error
  126. */
  127. static void io_appendto (void)
  128. {
  129. lua_Object o = lua_getparam (1);
  130. if (!lua_isstring (o))
  131. {
  132. lua_error ("incorrect argument to function 'appendto`");
  133. lua_pushnumber (0);
  134. }
  135. else
  136. {
  137. int r;
  138. FILE *fp;
  139. struct stat st;
  140. if (stat(lua_getstring(o), &st) == -1) r = 1;
  141. else r = 2;
  142. fp = fopen (lua_getstring(o),"a");
  143. if (fp == NULL)
  144. {
  145. lua_pushnumber (0);
  146. }
  147. else
  148. {
  149. if (out != stdout) fclose (out);
  150. out = fp;
  151. lua_pushnumber (r);
  152. }
  153. }
  154. }
  155. /*
  156. ** Read a variable. On error put nil on stack.
  157. ** LUA interface:
  158. ** variable = read ([format])
  159. **
  160. ** O formato pode ter um dos seguintes especificadores:
  161. **
  162. ** s ou S -> para string
  163. ** f ou F, g ou G, e ou E -> para reais
  164. ** i ou I -> para inteiros
  165. **
  166. ** Estes especificadores podem vir seguidos de numero que representa
  167. ** o numero de campos a serem lidos.
  168. */
  169. static void io_read (void)
  170. {
  171. lua_Object o = lua_getparam (1);
  172. if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
  173. {
  174. int c;
  175. char s[256];
  176. while (isspace(c=fgetc(in)))
  177. ;
  178. if (c == '\"')
  179. {
  180. int n=0;
  181. while((c = fgetc(in)) != '\"')
  182. {
  183. if (c == EOF)
  184. {
  185. lua_pushnil ();
  186. return;
  187. }
  188. s[n++] = c;
  189. }
  190. s[n] = 0;
  191. }
  192. else if (c == '\'')
  193. {
  194. int n=0;
  195. while((c = fgetc(in)) != '\'')
  196. {
  197. if (c == EOF)
  198. {
  199. lua_pushnil ();
  200. return;
  201. }
  202. s[n++] = c;
  203. }
  204. s[n] = 0;
  205. }
  206. else
  207. {
  208. double d;
  209. ungetc (c, in);
  210. if (fscanf (in, "%s", s) != 1)
  211. {
  212. lua_pushnil ();
  213. return;
  214. }
  215. if (sscanf(s, "%lf %*c", &d) == 1)
  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 fl;
  260. sscanf (s, "%f", &fl);
  261. lua_pushnumber(fl);
  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. ** Read characters until a given one. The delimiter is not read.
  303. */
  304. static void io_readuntil (void)
  305. {
  306. int n=255,m=0;
  307. int c,d;
  308. char *s;
  309. lua_Object lo = lua_getparam(1);
  310. if (!lua_isstring(lo))
  311. d = EOF;
  312. else
  313. d = *lua_getstring(lo);
  314. s = (char *)malloc(n+1);
  315. while((c = fgetc(in)) != EOF && c != d)
  316. {
  317. if (m==n)
  318. {
  319. n *= 2;
  320. s = (char *)realloc(s, n+1);
  321. }
  322. s[m++] = c;
  323. }
  324. if (c != EOF) ungetc(c,in);
  325. s[m] = 0;
  326. lua_pushstring(s);
  327. free(s);
  328. }
  329. /*
  330. ** Write a variable. On error put 0 on stack, otherwise put 1.
  331. ** LUA interface:
  332. ** status = write (variable [,format])
  333. **
  334. ** O formato pode ter um dos seguintes especificadores:
  335. **
  336. ** s ou S -> para string
  337. ** f ou F, g ou G, e ou E -> para reais
  338. ** i ou I -> para inteiros
  339. **
  340. ** Estes especificadores podem vir seguidos de:
  341. **
  342. ** [?][m][.n]
  343. **
  344. ** onde:
  345. ** ? -> indica justificacao
  346. ** < = esquerda
  347. ** | = centro
  348. ** > = direita (default)
  349. ** m -> numero maximo de campos (se exceder estoura)
  350. ** n -> indica precisao para
  351. ** reais -> numero de casas decimais
  352. ** inteiros -> numero minimo de digitos
  353. ** string -> nao se aplica
  354. */
  355. static char *buildformat (char *e, lua_Object o)
  356. {
  357. static char buffer[2048];
  358. static char f[80];
  359. char *string = &buffer[255];
  360. char *fstart=e, *fspace, *send;
  361. char t, j='r';
  362. int m=0, n=-1, l;
  363. while (isspace(*e)) e++;
  364. fspace = e;
  365. t = *e++;
  366. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  367. while (isdigit(*e))
  368. m = m*10 + (*e++ - '0');
  369. if (*e == '.') e++; /* skip point */
  370. while (isdigit(*e))
  371. if (n < 0) n = (*e++ - '0');
  372. else n = n*10 + (*e++ - '0');
  373. sprintf(f,"%%");
  374. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  375. if (m > 0) sprintf(strchr(f,0),"%d", m);
  376. if (n >= 0) sprintf(strchr(f,0),".%d", n);
  377. switch (t)
  378. {
  379. case 'i': case 'I': t = 'd';
  380. sprintf(strchr(f,0), "%c", t);
  381. sprintf (string, f, (long int)lua_getnumber(o));
  382. break;
  383. case 'f': case 'g': case 'e': case 'G': case 'E':
  384. sprintf(strchr(f,0), "%c", t);
  385. sprintf (string, f, (float)lua_getnumber(o));
  386. break;
  387. case 'F': t = 'f';
  388. sprintf(strchr(f,0), "%c", t);
  389. sprintf (string, f, (float)lua_getnumber(o));
  390. break;
  391. case 's': case 'S': t = 's';
  392. sprintf(strchr(f,0), "%c", t);
  393. sprintf (string, f, lua_getstring(o));
  394. break;
  395. default: return "";
  396. }
  397. l = strlen(string);
  398. send = string+l;
  399. if (m!=0 && l>m)
  400. {
  401. int i;
  402. for (i=0; i<m; i++)
  403. string[i] = '*';
  404. string[i] = 0;
  405. }
  406. else if (m!=0 && j=='|')
  407. {
  408. int k;
  409. int i=l-1;
  410. while (isspace(string[i]) || string[i]==0) i--;
  411. string -= (m-i)/2;
  412. for(k=0; k<(m-i)/2; k++)
  413. string[k] = ' ';
  414. }
  415. /* add space characteres */
  416. while (fspace != fstart)
  417. {
  418. string--;
  419. fspace--;
  420. *string = *fspace;
  421. }
  422. while (isspace(*e)) *send++ = *e++;
  423. *send = 0;
  424. return string;
  425. }
  426. static void io_write (void)
  427. {
  428. lua_Object o1 = lua_getparam (1);
  429. lua_Object o2 = lua_getparam (2);
  430. if (o1 == LUA_NOOBJECT) /* new line */
  431. {
  432. fprintf (out, "\n");
  433. lua_pushnumber(1);
  434. }
  435. else if (o2 == LUA_NOOBJECT) /* free format */
  436. {
  437. int status=0;
  438. if (lua_isnumber(o1))
  439. status = fprintf (out, "%g", lua_getnumber(o1));
  440. else if (lua_isstring(o1))
  441. status = fprintf (out, "%s", lua_getstring(o1));
  442. lua_pushnumber(status);
  443. }
  444. else /* formated */
  445. {
  446. if (!lua_isstring(o2))
  447. {
  448. lua_error ("incorrect format to function `write'");
  449. lua_pushnumber(0);
  450. return;
  451. }
  452. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  453. }
  454. }
  455. /*
  456. ** Execute a executable program using "system".
  457. ** Return the result of execution.
  458. */
  459. static void io_execute (void)
  460. {
  461. lua_Object o = lua_getparam (1);
  462. if (o == LUA_NOOBJECT || !lua_isstring (o))
  463. {
  464. lua_error ("incorrect argument to function 'execute`");
  465. lua_pushnumber (0);
  466. }
  467. else
  468. {
  469. int res = system(lua_getstring(o));
  470. lua_pushnumber (res);
  471. }
  472. return;
  473. }
  474. /*
  475. ** Remove a file.
  476. ** On error put 0 on stack, otherwise put 1.
  477. */
  478. static void io_remove (void)
  479. {
  480. lua_Object o = lua_getparam (1);
  481. if (o == LUA_NOOBJECT || !lua_isstring (o))
  482. {
  483. lua_error ("incorrect argument to function 'execute`");
  484. lua_pushnumber (0);
  485. }
  486. else
  487. {
  488. if (remove(lua_getstring(o)) == 0)
  489. lua_pushnumber (1);
  490. else
  491. lua_pushnumber (0);
  492. }
  493. return;
  494. }
  495. /*
  496. ** To get a environment variables
  497. */
  498. static void io_getenv (void)
  499. {
  500. lua_Object s = lua_getparam(1);
  501. if (!lua_isstring(s))
  502. lua_pushnil();
  503. else
  504. {
  505. char *env = getenv(lua_getstring(s));
  506. if (env == NULL) lua_pushnil();
  507. else lua_pushstring(env);
  508. }
  509. }
  510. /*
  511. ** Return time: hour, min, sec
  512. */
  513. static void io_time (void)
  514. {
  515. time_t t;
  516. struct tm *s;
  517. time(&t);
  518. s = localtime(&t);
  519. lua_pushnumber(s->tm_hour);
  520. lua_pushnumber(s->tm_min);
  521. lua_pushnumber(s->tm_sec);
  522. }
  523. /*
  524. ** Return date: dd, mm, yyyy
  525. */
  526. static void io_date (void)
  527. {
  528. time_t t;
  529. struct tm *s;
  530. time(&t);
  531. s = localtime(&t);
  532. lua_pushnumber(s->tm_mday);
  533. lua_pushnumber(s->tm_mon+1);
  534. lua_pushnumber(s->tm_year+1900);
  535. }
  536. /*
  537. ** Beep
  538. */
  539. static void io_beep (void)
  540. {
  541. printf("\a");
  542. }
  543. /*
  544. ** To exit
  545. */
  546. static void io_exit (void)
  547. {
  548. lua_Object o = lua_getparam(1);
  549. if (lua_isstring(o))
  550. printf("%s\n", lua_getstring(o));
  551. exit(1);
  552. }
  553. /*
  554. ** To debug a lua program. Start a dialog with the user, interpreting
  555. lua commands until an 'cont'.
  556. */
  557. static void io_debug (void)
  558. {
  559. while (1)
  560. {
  561. char buffer[250];
  562. fprintf(stderr, "lua_debug> ");
  563. if (gets(buffer) == 0) return;
  564. if (strcmp(buffer, "cont") == 0) return;
  565. lua_dostring(buffer);
  566. }
  567. }
  568. /*
  569. ** Open io library
  570. */
  571. void iolib_open (void)
  572. {
  573. lua_register ("readfrom", io_readfrom);
  574. lua_register ("writeto", io_writeto);
  575. lua_register ("appendto", io_appendto);
  576. lua_register ("read", io_read);
  577. lua_register ("readuntil",io_readuntil);
  578. lua_register ("write", io_write);
  579. lua_register ("execute", io_execute);
  580. lua_register ("remove", io_remove);
  581. lua_register ("getenv", io_getenv);
  582. lua_register ("time", io_time);
  583. lua_register ("date", io_date);
  584. lua_register ("beep", io_beep);
  585. lua_register ("exit", io_exit);
  586. lua_register ("debug", io_debug);
  587. }