iolib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. ** iolib.c
  3. ** Input/output library to LUA
  4. */
  5. char *rcs_iolib="$Id: iolib.c,v 1.11 1994/10/13 17:18:32 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. #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. ** 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 = calloc(n+1, sizeof(char));
  315. while((c = fgetc(in)) != EOF && c != d)
  316. {
  317. if (m==n)
  318. {
  319. n *= 2;
  320. s = realloc(s, (n+1)*sizeof(char));
  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. n = n*10 + (*e++ - '0');
  372. sprintf(f,"%%");
  373. if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
  374. if (m > 0) sprintf(strchr(f,0),"%d", m);
  375. if (n >= 0) sprintf(strchr(f,0),".%d", n);
  376. switch (t)
  377. {
  378. case 'i': case 'I': t = 'd';
  379. sprintf(strchr(f,0), "%c", t);
  380. sprintf (string, f, (long int)lua_getnumber(o));
  381. break;
  382. case 'f': case 'g': case 'e': case 'G': case 'E':
  383. sprintf(strchr(f,0), "%c", t);
  384. sprintf (string, f, (float)lua_getnumber(o));
  385. break;
  386. case 'F': t = 'f';
  387. sprintf(strchr(f,0), "%c", t);
  388. sprintf (string, f, (float)lua_getnumber(o));
  389. break;
  390. case 's': case 'S': t = 's';
  391. sprintf(strchr(f,0), "%c", t);
  392. sprintf (string, f, lua_getstring(o));
  393. break;
  394. default: return "";
  395. }
  396. l = strlen(string);
  397. send = string+l;
  398. if (m!=0 && l>m)
  399. {
  400. int i;
  401. for (i=0; i<m; i++)
  402. string[i] = '*';
  403. string[i] = 0;
  404. }
  405. else if (m!=0 && j=='|')
  406. {
  407. int k;
  408. int i=l-1;
  409. while (isspace(string[i]) || string[i]==0) i--;
  410. string -= (m-i)/2;
  411. for(k=0; k<(m-i)/2; k++)
  412. string[k] = ' ';
  413. }
  414. /* add space characteres */
  415. while (fspace != fstart)
  416. {
  417. string--;
  418. fspace--;
  419. *string = *fspace;
  420. }
  421. while (isspace(*e)) *send++ = *e++;
  422. *send = 0;
  423. return string;
  424. }
  425. static void io_write (void)
  426. {
  427. lua_Object o1 = lua_getparam (1);
  428. lua_Object o2 = lua_getparam (2);
  429. if (o1 == NULL) /* new line */
  430. {
  431. fprintf (out, "\n");
  432. lua_pushnumber(1);
  433. }
  434. else if (o2 == NULL) /* free format */
  435. {
  436. int status=0;
  437. if (lua_isnumber(o1))
  438. status = fprintf (out, "%g", lua_getnumber(o1));
  439. else if (lua_isstring(o1))
  440. status = fprintf (out, "%s", lua_getstring(o1));
  441. lua_pushnumber(status);
  442. }
  443. else /* formated */
  444. {
  445. if (!lua_isstring(o2))
  446. {
  447. lua_error ("incorrect format to function `write'");
  448. lua_pushnumber(0);
  449. return;
  450. }
  451. lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
  452. }
  453. }
  454. /*
  455. ** Execute a executable program using "system".
  456. ** Return the result of execution.
  457. */
  458. static void io_execute (void)
  459. {
  460. lua_Object o = lua_getparam (1);
  461. if (o == NULL || !lua_isstring (o))
  462. {
  463. lua_error ("incorrect argument to function 'execute`");
  464. lua_pushnumber (0);
  465. }
  466. else
  467. {
  468. int res = system(lua_getstring(o));
  469. lua_pushnumber (res);
  470. }
  471. return;
  472. }
  473. /*
  474. ** Remove a file.
  475. ** On error put 0 on stack, otherwise put 1.
  476. */
  477. static void io_remove (void)
  478. {
  479. lua_Object o = lua_getparam (1);
  480. if (o == NULL || !lua_isstring (o))
  481. {
  482. lua_error ("incorrect argument to function 'execute`");
  483. lua_pushnumber (0);
  484. }
  485. else
  486. {
  487. if (remove(lua_getstring(o)) == 0)
  488. lua_pushnumber (1);
  489. else
  490. lua_pushnumber (0);
  491. }
  492. return;
  493. }
  494. /*
  495. ** To get a environment variables
  496. */
  497. static void io_getenv (void)
  498. {
  499. lua_Object s = lua_getparam(1);
  500. if (!lua_isstring(s))
  501. lua_pushnil();
  502. else
  503. {
  504. char *env = getenv(lua_getstring(s));
  505. if (env == NULL) lua_pushnil();
  506. else lua_pushstring(env);
  507. }
  508. }
  509. /*
  510. ** To abort
  511. */
  512. static void io_abort (void)
  513. {
  514. lua_Object o = lua_getparam(1);
  515. if (lua_isstring(o))
  516. printf("%s\n", lua_getstring(o));
  517. exit(1);
  518. }
  519. /*
  520. ** To debug a lua program. Start a dialog with the user, interpreting
  521. lua commands until an 'cont'.
  522. */
  523. static void io_debug (void)
  524. {
  525. while (1)
  526. {
  527. char buffer[250];
  528. printf("lua_debug> ");
  529. if (gets(buffer) == 0) return;
  530. if (strcmp(buffer, "cont") == 0) return;
  531. lua_dostring(buffer);
  532. }
  533. }
  534. /*
  535. ** Open io library
  536. */
  537. void iolib_open (void)
  538. {
  539. lua_register ("readfrom", io_readfrom);
  540. lua_register ("writeto", io_writeto);
  541. lua_register ("appendto", io_appendto);
  542. lua_register ("read", io_read);
  543. lua_register ("readuntil",io_readuntil);
  544. lua_register ("write", io_write);
  545. lua_register ("execute", io_execute);
  546. lua_register ("remove", io_remove);
  547. lua_register ("getenv", io_getenv);
  548. lua_register ("abort", io_abort);
  549. lua_register ("debug", io_debug);
  550. }