io.red 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. module io; % Functions for handling input and output of files.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1995 RAND. All rights reserved.
  4. fluid '(!*echo !*int !*reduce4 semic!*);
  5. global '(contl!* curline!* ifl!* ipl!* linelist!* ofl!* opl!* techo!*);
  6. symbolic procedure file!-transform(u,v);
  7. % Performs a transformation on the file u. V is name of function
  8. % used for the transformation.
  9. begin scalar echo,ichan,oldichan,val;
  10. echo := !*echo;
  11. !*echo := nil;
  12. ichan := open(u,'input);
  13. oldichan := rds ichan;
  14. val := errorset!*(list v,t);
  15. !*echo := echo;
  16. close ichan;
  17. rds oldichan;
  18. if not errorp val then return car val
  19. end;
  20. symbolic procedure infile u;
  21. % Loads the single file u into REDUCE without echoing.
  22. begin scalar !*int;
  23. return file!-transform(u,function begin1)
  24. end;
  25. symbolic procedure in u; in_non_empty_list u; % REDUCE 3 hook.
  26. symbolic procedure in_non_empty_list u;
  27. begin scalar echop;
  28. echop := null(semic!* eq '!$); % Record echo character from input.
  29. if null ifl!* then techo!* := !*echo; % Terminal echo status.
  30. if !*reduce4 then u := value u;
  31. for each fl in u do in_list1(fl,echop);
  32. if ipl!* then ifl!* := car ipl!* else ifl!* := nil;
  33. if ifl!* then curline!* := caddr ifl!*;
  34. if !*reduce4 then return mkobject(nil,'noval)
  35. end;
  36. symbolic procedure mkfil!* u;
  37. % Converts file descriptor U into valid system filename.
  38. % Allows for u to have an algebraic scalar value.
  39. begin scalar x;
  40. return if stringp u then u
  41. else if not idp u then typerr(u,"file name")
  42. else if flagp(u,'share) and stringp (x := eval u)
  43. then x
  44. else string!-downcase u
  45. end;
  46. symbolic procedure in_list1(fl,echop);
  47. begin scalar chan,echo,ochan;
  48. echo := !*echo; % Save current echo status.
  49. if !*reduce4 then if type fl neq 'string then typerr(fl,'string)
  50. else fl := value fl;
  51. chan := open(fl := mkfil!* fl,'input);
  52. ochan := rds chan;
  53. if assoc(fl,linelist!*) then nil;
  54. curline!* := 1;
  55. ifl!* := list(fl,chan,1);
  56. ipl!* := ifl!* . ipl!*; % Add to input file stack.
  57. !*echo := echop;
  58. begin1();
  59. rds ochan;
  60. close chan;
  61. !*echo := echo; % Restore echo status.
  62. if null ipl!* and contl!* then return nil
  63. else if null ipl!* or null(fl eq caar ipl!*)
  64. then rederr list("FILE STACK CONFUSION",fl,ipl!*)
  65. else ipl!* := cdr ipl!*
  66. end;
  67. symbolic procedure out u; out_non_empty_list u; % REDUCE 3 hook.
  68. symbolic procedure out_non_empty_list u;
  69. % U is a list of one file.
  70. begin integer n; scalar chan,fl,x;
  71. n := linelength nil;
  72. if !*reduce4 then u := value u;
  73. if null u then return nil;
  74. u := car u;
  75. if !*reduce4 then if type u neq 'string then typerr(u,'string)
  76. else u := value u;
  77. if u eq 't then return <<wrs(ofl!* := nil); nil>>;
  78. fl := mkfil u;
  79. if not (x := assoc(fl,opl!*))
  80. then <<chan := open(fl,'output);
  81. if chan
  82. then <<ofl!*:= fl . chan; opl!*:= ofl!* . opl!*>>>>
  83. else ofl!* := x;
  84. wrs cdr ofl!*;
  85. linelength n;
  86. if !*reduce4 then return mkobject(nil,'noval)
  87. end;
  88. symbolic procedure shut u; shut_non_empty_list u; % REDUCE 3 hook.
  89. symbolic procedure shut_non_empty_list u;
  90. % U is a list of names of files to be shut.
  91. begin scalar fl1;
  92. if !*reduce4 then u := value u;
  93. for each fl in u do
  94. <<if !*reduce4
  95. then if type fl neq 'string then typerr(fl,'string)
  96. else fl := value fl;
  97. if fl1 := assoc((fl := mkfil fl),opl!*)
  98. then <<opl!* := delete(fl1,opl!*);
  99. if fl1=ofl!* then <<ofl!* := nil; wrs nil>>;
  100. close cdr fl1>>
  101. else if not (fl1 := assoc(fl,ipl!*))
  102. then rerror(rlisp,26,list(fl,"not open"))
  103. else if fl1 neq ifl!*
  104. then <<close cadr fl1; ipl!* := delete(fl1,ipl!*)>>
  105. else rerror(rlisp,27,
  106. list("Cannot shut current input file",car fl1))>>;
  107. if !*reduce4 then return mkobject(nil,'noval)
  108. end;
  109. deflist ('((in rlis) (out rlis) (shut rlis)),'stat); % REDUCE 3 only.
  110. flag ('(in out shut),'eval);
  111. flag ('(in out shut),'ignore);
  112. endmodule;
  113. end;