home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / MSWSRC35 / FILES.CPP < prev    next >
C/C++ Source or Header  |  1993-09-25  |  13KB  |  602 lines

  1. /*
  2.  *      files.c         logo file management module             dvb
  3.  *
  4.  *    Copyright (C) 1989 The Regents of the University of California
  5.  *    This Software may be copied and distributed for educational,
  6.  *    research, and not for profit purposes provided that this
  7.  *    copyright and statement are included in all such copies.
  8.  *
  9.  */
  10.  
  11. #include "logo.h"
  12. #include "globals.h"
  13. #ifdef unix
  14. #include <sgtty.h>
  15. #endif
  16.  
  17. #ifdef mac
  18. #include <console.h>
  19. #endif
  20. #ifdef ibm
  21. #include <bios.h>
  22. #ifndef __ZTC__
  23. #include <alloc.h>
  24. #endif
  25. #ifdef __ZTC__
  26. #include <conio.h>
  27. #endif
  28. #endif
  29.  
  30. #include <conio.h>
  31.  
  32. #ifndef TIOCSTI
  33. #include <setjmp.h>
  34. extern jmp_buf iblk_buf;
  35. #endif
  36.  
  37. NODE *file_list = NULL;
  38. NODE *reader_name = NIL, *writer_name = NIL;
  39.  
  40. FILE *open_file(NODE *arg, char *access)
  41. {
  42.     char *fnstr;
  43.     FILE *tstrm;
  44.  
  45.     ref(arg);
  46.     arg = reref(arg, cnv_node_to_strnode(arg));
  47.     if (arg == UNBOUND) return(NULL);
  48.     fnstr = (char *) malloc((size_t)getstrlen(arg) + 1);
  49.     strnzcpy(fnstr, getstrptr(arg), getstrlen(arg));
  50.     tstrm = fopen(fnstr, access);
  51.     deref(arg);
  52.     free(fnstr);
  53.     return(tstrm);
  54. }
  55.  
  56. NODE *ldribble(NODE *arg)
  57. {
  58.     if (dribblestream != NULL)
  59.     err_logo(ALREADY_DRIBBLING, NIL);
  60.     else {
  61.     dribblestream = open_file(car(arg), "w+");
  62.     if (dribblestream == NULL) err_logo(FILE_ERROR, NIL);
  63.     }
  64.     return(UNBOUND);
  65. }
  66.  
  67. NODE *lnodribble()
  68. {
  69.     if (dribblestream != NULL) {
  70.     fclose(dribblestream);
  71.     dribblestream = NULL;
  72.     }
  73.     return(UNBOUND);
  74. }
  75.  
  76. FILE *find_file(NODE *arg, BOOLEAN remove)
  77. {
  78.     NODE *t, *prev = NIL;
  79.     FILE *fp = NULL;
  80.  
  81.     t = file_list;
  82.     while (t != NIL) {
  83.     if (compare_node(arg, car(t), FALSE) == 0) {
  84.         fp = (FILE *)t->n_obj;
  85.         if (remove) {
  86.         t->n_obj = NIL;
  87.         if (prev == NIL)
  88.             file_list = reref(file_list, cdr(t));
  89.         else
  90.             setcdr(prev, cdr(t));
  91.         }
  92.         break;
  93.     }
  94.     prev = t;
  95.     t = cdr(t);
  96.     }
  97.     return fp;
  98. }
  99.  
  100. NODE *lopen(NODE *arg, char *mode)
  101. {
  102.     FILE *tmp;
  103.  
  104.     arg = car(arg);
  105.     if (find_file(arg, FALSE) != NULL)
  106.     err_logo(FILE_ERROR, make_static_strnode("File already open"));
  107.     else if ((tmp = open_file(arg, mode)) != NULL) {
  108.     push(arg, file_list);
  109.     file_list->n_obj = (NODE *) tmp;
  110.     }
  111.     else
  112.     err_logo(FILE_ERROR, make_static_strnode("I can't open that file"));
  113.     return(UNBOUND);
  114. }
  115.  
  116. NODE *lopenread(NODE *arg)
  117. {
  118.     return(lopen(arg,"r"));
  119. }
  120.  
  121. NODE *lopenwrite(NODE *arg)
  122. {
  123.     return(lopen(arg,"w"));
  124. }
  125.  
  126. NODE *lopenappend(NODE *arg)
  127. {
  128.     return(lopen(arg,"a"));
  129. }
  130.  
  131. NODE *lopenupdate(NODE *arg)
  132. {
  133.     return(lopen(arg,"a+"));
  134. }
  135.  
  136. NODE *lallopen()
  137. {
  138.     return(file_list);
  139. }
  140.  
  141. NODE *lclose(NODE *arg)
  142. {
  143.     FILE *tmp;
  144.  
  145.     if ((tmp = find_file(car(arg), TRUE)) == NULL)
  146.     err_logo(FILE_ERROR, make_static_strnode("File not open"));
  147.     else
  148.     fclose(tmp);
  149.     return(UNBOUND);
  150. }
  151.  
  152. NODE *lsetwrite(NODE *arg)
  153. {
  154.     FILE *tmp;
  155.  
  156.     if (car(arg) == NIL) {
  157.     writestream = stdout;
  158.     deref(writer_name);
  159.     writer_name = NIL;
  160.     }
  161.     else if ((tmp = find_file(car(arg), FALSE)) != NULL) {
  162.     writestream = tmp;
  163.     writer_name = reref(writer_name, car(arg));
  164.     }
  165.     else
  166.     err_logo(FILE_ERROR, make_static_strnode("File not open"));
  167.     return(UNBOUND);
  168. }
  169.  
  170. NODE *lsetread(NODE *arg)
  171. {
  172.     FILE *tmp;
  173.  
  174.     if (car(arg) == NIL) {
  175.     readstream = stdin;
  176.     deref(reader_name);
  177.     reader_name = NIL;
  178.     }
  179.     else if ((tmp = find_file(car(arg), FALSE)) != NULL) {
  180.     readstream = tmp;
  181.     reader_name = reref(reader_name, car(arg));
  182.     }
  183.     else
  184.     err_logo(FILE_ERROR, make_static_strnode("File not open"));
  185.     return(UNBOUND);
  186. }
  187.  
  188. NODE *lreader()
  189. {
  190.     return(reader_name);
  191. }
  192.  
  193. NODE *lwriter()
  194. {
  195.     return(writer_name);
  196. }
  197.  
  198. NODE *lerasefile(NODE *arg)
  199. {
  200.     char *fnstr;
  201.  
  202.     arg = cnv_node_to_strnode(car(arg));
  203.     if (arg == UNBOUND) return(UNBOUND);
  204.     fnstr = (char *)malloc((size_t)getstrlen(arg) + 1);
  205.     strnzcpy(fnstr, getstrptr(arg), getstrlen(arg));
  206.     unlink(fnstr);
  207.     free(fnstr);
  208.     return(UNBOUND);
  209. }
  210.  
  211. NODE *lsave(NODE *arg)
  212. {
  213.     FILE *tmp;
  214.     int save_yield_flag;
  215.  
  216.     lprint(arg);
  217.  
  218.     tmp = writestream;
  219.     writestream = open_file(car(arg), "w+");
  220.     if (writestream != NULL) {
  221.  
  222.         save_yield_flag = yield_flag;
  223.         yield_flag = 0;
  224.         lsetcursorwait();
  225.  
  226.     setcar(arg, cons(lcontents(), NIL));
  227.     lpo(car(arg));
  228.     fclose(writestream);
  229.         IsDirty = 0;
  230.  
  231.         lsetcursorarrow();
  232.         yield_flag = save_yield_flag;
  233.     }
  234.     else
  235.     err_logo(FILE_ERROR, make_static_strnode("Could not open file"));
  236.     writestream = tmp;
  237.     return(UNBOUND);
  238. }
  239.  
  240. void filesave(char *temp)
  241. {
  242.     FILE *tmp;
  243.     NODE *arg;
  244.     int save_yield_flag;
  245.  
  246.     arg = cons(make_strnode(temp, NULL, strlen(temp), STRING, strnzcpy), NIL);
  247.  
  248.     tmp = writestream;
  249.     writestream = open_file(car(arg), "w+");
  250.     if (writestream != NULL) {
  251.  
  252.         save_yield_flag = yield_flag;
  253.         yield_flag = 0;
  254.         lsetcursorwait();
  255.  
  256.     setcar(arg, cons(lcontents(), NIL));
  257.     lpo(car(arg));
  258.     fclose(writestream);
  259.         IsDirty = 0;
  260.  
  261.         lsetcursorarrow();
  262.         yield_flag = save_yield_flag;
  263.  
  264.     }
  265.     else
  266.     err_logo(FILE_ERROR, make_static_strnode("Could not open file"));
  267.     writestream = tmp;
  268. }
  269.  
  270. void runstartup(NODE *oldst)
  271. {
  272.     NODE *st;
  273.  
  274.     st = valnode__caseobj(Startup);
  275.     if (st != oldst && st != NIL && is_list(st)) {
  276.     val_status = 0;
  277.     eval_driver(st);
  278.     }
  279. }
  280.  
  281. void silent_load(NODE *arg, char *prefix)
  282. {
  283.     FILE *tmp_stream;
  284.     NODE *tmp_line, *exec_list;
  285.     char load_path[200];
  286.     NODE *st = valnode__caseobj(Startup);
  287.     int sv_val_status = val_status;
  288.     int IsDirtySave;
  289.     int save_yield_flag;
  290.  
  291.     /* This procedure is called three ways:
  292.      *    silent_load(NIL,*argv)    loads *argv
  293.      *    silent_load(proc,logolib)     loads logolib/proc
  294.      *    silent_load(proc,NULL)    loads proc.lg
  295.      * The "/" or ".lg" is supplied by this procedure as needed.
  296.      */
  297.  
  298.     IsDirtySave = IsDirty;
  299.     if (prefix == NULL && arg == NIL) return;
  300.     strcpy(load_path, (prefix == NULL ? "" : prefix));
  301.     if (arg != NIL) {
  302.     arg = cnv_node_to_strnode(arg);
  303.     if (arg == UNBOUND) return;
  304. #ifdef unix
  305.     if (prefix != NULL) strcat(load_path, "/");
  306. #endif
  307.     noparitylow_strnzcpy(load_path + (int)strlen(load_path),
  308.                  getstrptr(arg), getstrlen(arg));
  309.     if (prefix == NULL) strcat(load_path, ".lg");
  310.     gcref(arg);
  311.     }
  312.     tmp_stream = loadstream;
  313.     tmp_line = vref(current_line);
  314.     loadstream = fopen(load_path, "r");
  315.     if (loadstream != NULL) {
  316.  
  317.         save_yield_flag = yield_flag;
  318.         yield_flag = 0;
  319.         lsetcursorwait();
  320.  
  321.     while (!feof(loadstream) && NOT_THROWING) {
  322.         current_line = reref(current_line, reader(loadstream, ""));
  323.         exec_list =parser(current_line, TRUE);
  324.         val_status = 0;
  325.         if (exec_list != NIL) eval_driver(exec_list);
  326.     }
  327.  
  328.         lsetcursorarrow();
  329.         yield_flag = save_yield_flag;
  330.  
  331.     fclose(loadstream);
  332.     runstartup(st);
  333.     val_status = sv_val_status;
  334.     } else if (arg == NIL) ndprintf(stdout,"File not found: %t\n", prefix);
  335.     loadstream = tmp_stream;
  336.     deref(current_line);
  337.     current_line = tmp_line;
  338.     IsDirty = IsDirtySave;
  339. }
  340.  
  341. NODE *lload(NODE *arg)
  342. {
  343.     FILE *tmp;
  344.     NODE *tmp_line, *exec_list;
  345.     NODE *st = valnode__caseobj(Startup);
  346.     int sv_val_status = val_status;
  347.     int IsDirtySave;
  348.     int save_yield_flag;
  349.  
  350.     IsDirtySave = IsDirty;
  351.     tmp = loadstream;
  352.     tmp_line = vref(current_line);
  353.     loadstream = open_file(car(arg), "r");
  354.     if (loadstream != NULL) {
  355.  
  356.         save_yield_flag = yield_flag;
  357.         yield_flag = 0;
  358.         lsetcursorwait();
  359.  
  360.     while (!feof(loadstream) && NOT_THROWING) {
  361.         current_line = reref(current_line, reader(loadstream, ""));
  362.         exec_list = parser(current_line, TRUE);
  363.         val_status = 0;
  364.         if (exec_list != NIL) eval_driver(exec_list);
  365.     }
  366.     fclose(loadstream);
  367.  
  368.         lsetcursorarrow();
  369.         yield_flag = save_yield_flag;
  370.  
  371.     runstartup(st);
  372.     val_status = sv_val_status;
  373.     } else
  374.     err_logo(FILE_ERROR, make_static_strnode("Could not open file"));
  375.     loadstream = tmp;
  376.     deref(current_line);
  377.     current_line = tmp_line;
  378.     IsDirty = IsDirtySave;
  379.     return(UNBOUND);
  380. }
  381.  
  382. void fileload(char *temp)
  383. {
  384.     FILE *tmp;
  385.     NODE *tmp_line, *exec_list, *arg;
  386.     NODE *st = valnode__caseobj(Startup);
  387.     int sv_val_status = val_status;
  388.     int IsDirtySave;
  389.     int save_yield_flag;
  390.  
  391.     arg = make_strnode(temp, NULL, strlen(temp), STRING, strnzcpy);
  392.  
  393.     IsDirtySave = IsDirty;
  394.     tmp = loadstream;
  395.     tmp_line = vref(current_line);
  396.     loadstream = open_file(arg, "r");
  397.     if (loadstream != NULL) {
  398.  
  399.         save_yield_flag = yield_flag;
  400.         yield_flag = 0;
  401.         lsetcursorwait();
  402.  
  403.     while (!feof(loadstream) && NOT_THROWING) {
  404.         current_line = reref(current_line, reader(loadstream, ""));
  405.         exec_list = parser(current_line, TRUE);
  406.         val_status = 0;
  407.         if (exec_list != NIL) eval_driver(exec_list);
  408.     }
  409.     fclose(loadstream);
  410.  
  411.         lsetcursorarrow();
  412.         yield_flag = save_yield_flag;
  413.  
  414.     runstartup(st);
  415.     val_status = sv_val_status;
  416.     } else
  417.     err_logo(FILE_ERROR, make_static_strnode("Could not open file"));
  418.     loadstream = tmp;
  419.     deref(current_line);
  420.     current_line = tmp_line;
  421.     IsDirty = IsDirtySave;
  422. }
  423.  
  424. NODE *lreadlist()
  425. {
  426.     NODE *val;
  427.  
  428.     input_mode = LIST_MODE;
  429.     val = parser(reader(readstream, ""), FALSE);
  430.     input_mode = NO_MODE;
  431.     if (feof(readstream)) {
  432.     gcref(val);
  433.     return(Null_Word);
  434.     }
  435.     return(val);
  436. }
  437.  
  438. NODE *lreadword()
  439. {
  440.     NODE *val;
  441.  
  442.     val = reader(readstream, "");
  443.     if (feof(readstream)) {
  444.     gcref(val);
  445.     return(NIL);
  446.     }
  447.     return(val);
  448. }
  449.  
  450. NODE *lreadchar()
  451. {
  452.     char c;
  453.  
  454.     charmode_on();
  455.     input_blocking++;
  456. #ifndef TIOCSTI
  457.     if (!setjmp(iblk_buf))
  458. #endif
  459. #ifdef mac
  460.     csetmode(C_RAW, stdin);
  461.     while ((c = (char)getc(readstream)) == EOF && readstream == stdin);
  462.     csetmode(C_ECHO, stdin);
  463. #else
  464. #ifdef ibm
  465.     if (interactive && readstream==stdin)
  466.     c = (char)rd_getc(stdin);
  467.     else
  468.     c = (char)getc(readstream);
  469.  
  470.     if (c == 17) { /* control-q */
  471.     to_pending = 0;
  472.     err_logo(STOP_ERROR,NIL);
  473.     }
  474.     if (c == 23) { /* control-w */
  475.     logo_pause(0);
  476.     return(lreadchar());
  477.     }
  478. #else
  479.     c = (char)getc(readstream);
  480. #endif
  481. #endif
  482.     input_blocking = 0;
  483.     if (feof(readstream)) {
  484.     return(NIL);
  485.     }
  486.     return(make_strnode(&c, (char *)NULL, 1,
  487.         (getparity(c) ? STRING : BACKSLASH_STRING), strnzcpy));
  488. }
  489.  
  490. NODE *lreadchars(NODE *args)
  491. {
  492.     unsigned int c, i;
  493.     char *strhead, *strptr;
  494.     NODETYPES type = STRING;
  495.  
  496.     c = (unsigned int)getint(pos_int_arg(args));
  497.     if (stopping_flag == THROWING) return UNBOUND;
  498.     charmode_on();
  499.     input_blocking++;
  500. #ifndef TIOCSTI
  501.     if (!setjmp(iblk_buf))
  502. #endif
  503.     {
  504.     strhead = (char *)malloc((size_t)(c + 2));
  505.     strptr = strhead + 1;
  506.     fread(strptr, 1, (int)c, readstream);
  507.     setstrrefcnt(strhead, 0);
  508.     }
  509.     input_blocking = 0;
  510. #ifndef TIOCSTI
  511.     if (stopping_flag == THROWING) return(UNBOUND);
  512. #endif
  513.     if (feof(readstream)) {
  514.     free(strhead);
  515.     return(NIL);
  516.     }
  517.     for (i = 0; i < c; i++)
  518.     if (getparity(strptr[i])) type = BACKSLASH_STRING;
  519.     return(make_strnode(strptr, strhead, (int)c, type, strnzcpy));
  520. }
  521.  
  522. NODE *leofp()
  523. {
  524.     ungetc(getc(readstream),readstream);
  525.     if (feof(readstream))
  526.     return(Truex);
  527.     else
  528.     return(Falsex);
  529. }
  530.  
  531. NODE *lkeyp()
  532. {
  533. //    long nc;
  534. #ifdef mac
  535.     int c;
  536. #endif
  537.  
  538.     if (readstream == stdin && interactive) {
  539.     charmode_on();
  540. //    fflush(stdout);
  541. #ifdef __ZTC__
  542.     zflush();
  543. #endif
  544. #if defined(mac)
  545.     csetmode(C_RAW, stdin);
  546.     c = ungetc(getc(readstream), readstream);
  547.     csetmode(C_ECHO, stdin);
  548.     return(c == EOF ? Falsex : Truex);
  549. #elif defined(ibm)
  550.     return(Truex
  551. //kbhit() ? Truex : Falsex
  552. );
  553. #else
  554. #ifdef FIONREAD
  555.     ioctl(0,FIONREAD,(char *)(&nc));
  556. #else
  557.     ndprintf(stdout,"Can't KEYP, no FIONREAD on this system\n");
  558.     nc = 1;    /* pretend there's a char so we don't loop */
  559. #endif
  560.     if (nc > 0)
  561.         return(Truex);
  562.     else
  563.         return(Falsex);
  564. #endif
  565.     }
  566.     ungetc(getc(readstream),readstream);
  567.     if (feof(readstream))
  568.     return(Truex);
  569.     else
  570.     return(Falsex);
  571. }
  572.  
  573. NODE *lreadpos()
  574. {
  575.     return(make_intnode(ftell(readstream)));
  576. }
  577.  
  578. NODE *lsetreadpos(NODE *arg)
  579. {
  580.     NODE *val = pos_int_arg(arg);
  581.  
  582.     if (NOT_THROWING) {
  583.     fseek(readstream,getint(val),0);
  584.     }
  585.     return(UNBOUND);
  586. }
  587.  
  588. NODE *lwritepos()
  589. {
  590.     return(make_intnode(ftell(writestream)));
  591. }
  592.  
  593. NODE *lsetwritepos(NODE *arg)
  594. {
  595.     NODE *val = pos_int_arg(arg);
  596.  
  597.     if (NOT_THROWING) {
  598.     fseek(writestream,getint(val),0);
  599.     }
  600.     return(UNBOUND);
  601. }
  602.