2
0

iolib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: iolib.c,v 1.20 1995/02/02 18:54:58 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. /*
  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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT) /* 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 == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
  169. {
  170. int c;
  171. char s[256];
  172. while (isspace(c=fgetc(in)))
  173. ;
  174. if (c == '\"')
  175. {
  176. int n=0;
  177. while((c = fgetc(in)) != '\"')
  178. {
  179. if (c == EOF)
  180. {
  181. lua_pushnil ();
  182. return;
  183. }
  184. s[n++] = c;
  185. }
  186. s[n] = 0;
  187. }
  188. else if (c == '\'')
  189. {
  190. int n=0;
  191. while((c = fgetc(in)) != '\'')
  192. {
  193. if (c == EOF)
  194. {
  195. lua_pushnil ();
  196. return;
  197. }
  198. s[n++] = c;
  199. }
  200. s[n] = 0;
  201. }
  202. else
  203. {
  204. double d;
  205. ungetc (c, in);
  206. if (fscanf (in, "%s", s) != 1)
  207. {
  208. lua_pushnil ();
  209. return;
  210. }
  211. if (sscanf(s, "%lf %*c", &d) == 1)
  212. {
  213. lua_pushnumber (d);
  214. return;
  215. }
  216. }
  217. lua_pushstring (s);
  218. return;
  219. }
  220. else /* formatted */
  221. {
  222. char *e = lua_getstring(o);
  223. char t;
  224. int m=0;
  225. while (isspace(*e)) e++;
  226. t = *e++;
  227. while (isdigit(*e))
  228. m = m*10 + (*e++ - '0');
  229. if (m > 0)
  230. {
  231. char f[80];
  232. char s[256];
  233. sprintf (f, "%%%ds", m);
  234. if (fgets (s, m, in) == NULL)
  235. {
  236. lua_pushnil();
  237. return;
  238. }
  239. else
  240. {
  241. if (s[strlen(s)-1] == '\n')
  242. s[strlen(s)-1] = 0;
  243. }
  244. switch (tolower(t))
  245. {
  246. case 'i':
  247. {
  248. long int l;
  249. sscanf (s, "%ld", &l);
  250. lua_pushnumber(l);
  251. }
  252. break;
  253. case 'f': case 'g': case 'e':
  254. {
  255. float fl;
  256. sscanf (s, "%f", &fl);
  257. lua_pushnumber(fl);
  258. }
  259. break;
  260. default:
  261. lua_pushstring(s);
  262. break;
  263. }
  264. }
  265. else
  266. {
  267. switch (tolower(t))
  268. {
  269. case 'i':
  270. {
  271. long int l;
  272. if (fscanf (in, "%ld", &l) == EOF)
  273. lua_pushnil();
  274. else lua_pushnumber(l);
  275. }
  276. break;
  277. case 'f': case 'g': case 'e':
  278. {
  279. float f;
  280. if (fscanf (in, "%f", &f) == EOF)
  281. lua_pushnil();
  282. else lua_pushnumber(f);
  283. }
  284. break;
  285. default:
  286. {
  287. char s[256];
  288. if (fscanf (in, "%s", s) == EOF)
  289. lua_pushnil();
  290. else lua_pushstring(s);
  291. }
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. /*
  298. ** Read characters until a given one. The delimiter is not read.
  299. */
  300. static void io_readuntil (void)
  301. {
  302. int n=255,m=0;
  303. int c,d;
  304. char *s;
  305. lua_Object lo = lua_getparam(1);
  306. if (!lua_isstring(lo))
  307. d = EOF;
  308. else
  309. d = *lua_getstring(lo);
  310. s = (char *)malloc(n+1);
  311. while((c = fgetc(in)) != EOF && c != d)
  312. {
  313. if (m==n)
  314. {
  315. n *= 2;
  316. s = (char *)realloc(s, n+1);
  317. }
  318. s[m++] = c;
  319. }
  320. if (c != EOF) ungetc(c,in);
  321. s[m] = 0;
  322. lua_pushstring(s);
  323. free(s);
  324. }
  325. /*
  326. ** Write a variable. On error put 0 on stack, otherwise put 1.
  327. ** LUA interface:
  328. ** status = write (variable [,format])
  329. **
  330. ** O formato pode ter um dos seguintes especificadores:
  331. **
  332. ** s ou S -> para string
  333. ** f ou F, g ou G, e ou E -> para reais
  334. ** i ou I -> para inteiros
  335. **
  336. ** Estes especificadores podem vir seguidos de:
  337. **
  338. ** [?][m][.n]
  339. **
  340. ** onde:
  341. ** ? -> indica justificacao
  342. ** < = esquerda
  343. ** | = centro
  344. ** > = direita (default)
  345. ** m -> numero maximo de campos (se exceder estoura)
  346. ** n -> indica precisao para
  347. ** reais -> numero de casas decimais
  348. ** inteiros -> numero minimo de digitos
  349. ** string -> nao se aplica
  350. */
  351. static char *buildformat (char *e, lua_Object o)
  352. {
  353. static char buffer[2048];
  354. static char f[80];
  355. char *string = &buffer[255];
  356. char *fstart=e, *fspace, *send;
  357. char t, j='r';
  358. int m=0, n=-1, l;
  359. while (isspace(*e)) e++;
  360. fspace = e;
  361. t = *e++;
  362. if (*e == '<' || *e == '|' || *e == '>') j = *e++;
  363. while (isdigit(*e))
  364. m = m*10 + (*e++ - '0');
  365. if (*e == '.') e++; /* skip point */
  366. while (isdigit(*e))
  367. if (n < 0) n = (*e++ - '0');
  368. else n = n*10 + (*e++ - '0');
  369. sprintf(f,"%%");
  370. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  371. if (m > 0) sprintf(strchr(f,0),"%d", m);
  372. if (n >= 0) sprintf(strchr(f,0),".%d", n);
  373. switch (t)
  374. {
  375. case 'i': case 'I': t = 'd';
  376. sprintf(strchr(f,0), "%c", t);
  377. sprintf (string, f, (long int)lua_getnumber(o));
  378. break;
  379. case 'f': case 'g': case 'e': case 'G': case 'E':
  380. sprintf(strchr(f,0), "%c", t);
  381. sprintf (string, f, (float)lua_getnumber(o));
  382. break;
  383. case 'F': t = 'f';
  384. sprintf(strchr(f,0), "%c", t);
  385. sprintf (string, f, (float)lua_getnumber(o));
  386. break;
  387. case 's': case 'S': t = 's';
  388. sprintf(strchr(f,0), "%c", t);
  389. sprintf (string, f, lua_getstring(o));
  390. break;
  391. default: return "";
  392. }
  393. l = strlen(string);
  394. send = string+l;
  395. if (m!=0 && l>m)
  396. {
  397. int i;
  398. for (i=0; i<m; i++)
  399. string[i] = '*';
  400. string[i] = 0;
  401. }
  402. else if (m!=0 && j=='|')
  403. {
  404. int k;
  405. int i=l-1;
  406. while (isspace(string[i]) || string[i]==0) i--;
  407. string -= (m-i)/2;
  408. for(k=0; k<(m-i)/2; k++)
  409. string[k] = ' ';
  410. }
  411. /* add space characteres */
  412. while (fspace != fstart)
  413. {
  414. string--;
  415. fspace--;
  416. *string = *fspace;
  417. }
  418. while (isspace(*e)) *send++ = *e++;
  419. *send = 0;
  420. return string;
  421. }
  422. static void io_write (void)
  423. {
  424. lua_Object o1 = lua_getparam (1);
  425. lua_Object o2 = lua_getparam (2);
  426. if (o1 == LUA_NOOBJECT) /* new line */
  427. {
  428. fprintf (out, "\n");
  429. lua_pushnumber(1);
  430. }
  431. else if (o2 == LUA_NOOBJECT) /* free format */
  432. {
  433. int status=0;
  434. if (lua_isnumber(o1))
  435. status = fprintf (out, "%g", lua_getnumber(o1));
  436. else if (lua_isstring(o1))
  437. status = fprintf (out, "%s", lua_getstring(o1));
  438. lua_pushnumber(status);
  439. }
  440. else /* formated */
  441. {
  442. if (!lua_isstring(o2))
  443. {
  444. lua_error ("incorrect format to function `write'");
  445. lua_pushnumber(0);
  446. return;
  447. }
  448. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  449. }
  450. }
  451. /*
  452. ** Execute a executable program using "system".
  453. ** Return the result of execution.
  454. */
  455. static void io_execute (void)
  456. {
  457. lua_Object o = lua_getparam (1);
  458. if (o == LUA_NOOBJECT || !lua_isstring (o))
  459. {
  460. lua_error ("incorrect argument to function 'execute`");
  461. lua_pushnumber (0);
  462. }
  463. else
  464. {
  465. int res = system(lua_getstring(o));
  466. lua_pushnumber (res);
  467. }
  468. return;
  469. }
  470. /*
  471. ** Remove a file.
  472. ** On error put 0 on stack, otherwise put 1.
  473. */
  474. static void io_remove (void)
  475. {
  476. lua_Object o = lua_getparam (1);
  477. if (o == LUA_NOOBJECT || !lua_isstring (o))
  478. {
  479. lua_error ("incorrect argument to function 'execute`");
  480. lua_pushnumber (0);
  481. }
  482. else
  483. {
  484. if (remove(lua_getstring(o)) == 0)
  485. lua_pushnumber (1);
  486. else
  487. lua_pushnumber (0);
  488. }
  489. return;
  490. }
  491. /*
  492. ** To get a environment variables
  493. */
  494. static void io_getenv (void)
  495. {
  496. lua_Object s = lua_getparam(1);
  497. if (!lua_isstring(s))
  498. lua_pushnil();
  499. else
  500. {
  501. char *env = getenv(lua_getstring(s));
  502. if (env == NULL) lua_pushnil();
  503. else lua_pushstring(env);
  504. }
  505. }
  506. /*
  507. ** Return time: hour, min, sec
  508. */
  509. static void io_time (void)
  510. {
  511. time_t t;
  512. struct tm *s;
  513. time(&t);
  514. s = localtime(&t);
  515. lua_pushnumber(s->tm_hour);
  516. lua_pushnumber(s->tm_min);
  517. lua_pushnumber(s->tm_sec);
  518. }
  519. /*
  520. ** Return date: dd, mm, yyyy
  521. */
  522. static void io_date (void)
  523. {
  524. time_t t;
  525. struct tm *s;
  526. time(&t);
  527. s = localtime(&t);
  528. lua_pushnumber(s->tm_mday);
  529. lua_pushnumber(s->tm_mon+1);
  530. lua_pushnumber(s->tm_year+1900);
  531. }
  532. /*
  533. ** Beep
  534. */
  535. static void io_beep (void)
  536. {
  537. printf("\a");
  538. }
  539. /*
  540. ** To exit
  541. */
  542. static void io_exit (void)
  543. {
  544. lua_Object o = lua_getparam(1);
  545. if (lua_isstring(o))
  546. printf("%s\n", lua_getstring(o));
  547. exit(1);
  548. }
  549. /*
  550. ** To debug a lua program. Start a dialog with the user, interpreting
  551. lua commands until an 'cont'.
  552. */
  553. static void io_debug (void)
  554. {
  555. while (1)
  556. {
  557. char buffer[250];
  558. fprintf(stderr, "lua_debug> ");
  559. if (gets(buffer) == 0) return;
  560. if (strcmp(buffer, "cont") == 0) return;
  561. lua_dostring(buffer);
  562. }
  563. }
  564. /*
  565. ** Open io library
  566. */
  567. void iolib_open (void)
  568. {
  569. lua_register ("readfrom", io_readfrom);
  570. lua_register ("writeto", io_writeto);
  571. lua_register ("appendto", io_appendto);
  572. lua_register ("read", io_read);
  573. lua_register ("readuntil",io_readuntil);
  574. lua_register ("write", io_write);
  575. lua_register ("execute", io_execute);
  576. lua_register ("remove", io_remove);
  577. lua_register ("getenv", io_getenv);
  578. lua_register ("time", io_time);
  579. lua_register ("date", io_date);
  580. lua_register ("beep", io_beep);
  581. lua_register ("exit", io_exit);
  582. lua_register ("debug", io_debug);
  583. }