File: files.gd

package info (click to toggle)
gap 4r4p10-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 29,224 kB
  • ctags: 7,084
  • sloc: ansic: 98,591; sh: 3,284; perl: 2,263; makefile: 467; awk: 6
file content (422 lines) | stat: -rw-r--r-- 15,360 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#############################################################################
##
#W  files.gd                    GAP Library                      Frank Celler
##
#H  @(#)$Id: files.gd,v 4.36 2003/09/30 15:55:58 gap Exp $
##
#Y  Copyright (C)  1996,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
#Y  (C) 1998 School Math and Comp. Sci., University of St.  Andrews, Scotland
#Y  Copyright (C) 2002 The GAP Group
##
##  This file contains the operations for files and directories.
##
Revision.files_gd :=
    "@(#)$Id: files.gd,v 4.36 2003/09/30 15:55:58 gap Exp $";


#############################################################################
##
#C  IsDirectory	. . . . . . . . . . . . . . . . . . . category of directories
##
DeclareCategory( "IsDirectory", IsObject );


#############################################################################
##
#V  DirectoriesFamily . . . . . . . . . . . . . . . . . family of directories
##
BIND_GLOBAL( "DirectoriesFamily", NewFamily( "DirectoriesFamily" ) );


#############################################################################
##
#F  USER_HOME_EXPAND . . . . . . . . . . . . .  expand leading ~ in file name
##
DeclareGlobalFunction("USER_HOME_EXPAND");


#############################################################################
##
#O  Directory( <string> ) . . . . . . . . . . . . . . .  new directory object
##
##  returns a directory object for the string <string>.
##  `Directory' understands `.' for ``current directory'', that is, the
##  directory in which {\GAP} was started.
##  It also understands absolute paths.
##
##  If the variable `GAPInfo.UserHome' is defined (this may depend on the
##  operating system) then `Directory' understands a string with a leading
##  `~' character for a path relative to the user's home directory.
##
##  Paths are otherwise taken relative to the current directory.
##
DeclareOperation( "Directory", [ IsString ] );


#############################################################################
##
#O  Filename( <dir>, <name> ) . . . . . . . . . . . . . . . . . . find a file
#O  Filename( <list-of-dirs>, <name> )  . . . . . . . . . . . . . find a file
##
##  If the first argument is a directory object <dir>, `Filename' returns the
##  (system dependent) filename as a string for the file with name <name>  in
##  the directory <dir>.
##  `Filename' returns the filename regardless of whether the directory
##  contains a file with name <name> or not.
##
##  If the first argument is a list <list-of-dirs> (possibly of length 1) of
##  directory objects, then `Filename' searches the directories in order, and
##  returns the filename for the file <name> in the first directory which
##  contains a file <name> or `fail' if no directory contains a file <name>.
##
DeclareOperation( "Filename", [ IsList, IsString ] );


#############################################################################
##
#O  Read( <name-file> ) . . . . . . . . . . . . . . . . . . . . . read a file
##
##  reads the input from the file with the filename <name-file>, which must
##  be given as a string.
##
##  `Read' first opens the file <name-file>.  If the file does not exist, or
##  if {\GAP} cannot open it, e.g., because of access restrictions,
##  an error is signalled.
##
##  Then the contents of the file are read and evaluated, but the results are
##  not printed.  The reading and evaluations happens exactly as described
##  for the main loop (see "Main Loop").
##
##  If a statement in the file causes an error a break loop is entered
##  (see~"Break Loops").
##  The input for this break loop is not taken from the file, but from the
##  input connected to the `stderr' output of {\GAP}.
##  If `stderr' is not connected to a terminal, no break loop is entered.
##  If this break loop is left with `quit' (or `<ctr>-D'), {\GAP} exits from
##  the `Read' command, and from all enclosing `Read' commands,
##  so that control is normally returned to an interactive prompt.
##  The `QUIT' statement (see~"Leaving GAP") can also be used in the break
##  loop to exit {\GAP} immediately.
##
##  Note that a statement must not begin in one file and end in another.
##  I.e., <eof> (`end-of-file') is not treated as whitespace,
##  but as a special symbol that must not appear inside any statement.
##
##  Note that one file may very well contain a read statement causing another
##  file to be read, before input is again taken from the first file.
##  There is an operating system dependent maximum on the number of files
##  that may be open simultaneously.  Usually it is 15.
##
DeclareOperation( "Read", [ IsString ] );


#############################################################################
##
#O  ReadTest( <string> )  . . . . . . . . . . . . . . . . .  read a test file
##
DeclareOperation( "ReadTest", [ IsString ] );


#############################################################################
##
#O  ReadAsFunction( <name-file> ) . . . . . . . . . . read a file as function
##
##  reads the file with filename <name-file> as a function and returns this
##  function.
##
DeclareOperation( "ReadAsFunction", [ IsString ] );

#############################################################################
##
#F  DirectoryContents(<name>)
## 
##  This function returns a list of filenames/directory names that reside in
##  the directory with name <name> (given as a string). It is an error, if
##  such a directory does not exist. 
DeclareGlobalFunction("DirectoryContents");


#############################################################################
##
#F  DirectoriesLibrary(  )  . . . . . . . . . . .  directories of the library
#F  DirectoriesLibrary( <name> )  . . . . . . . .  directories of the library
##
##  returns the directory objects for the {\GAP} library <name> as a list.
##  <name> must be one of `"lib"' (the default), `"grp"', `"prim"',
##  and so on.
##  The string `""' is also legal and with this argument `DirectoriesLibrary'
##  returns the list of {\GAP} root directories; the return value of
##  `DirectoriesLibrary("");' differs from `GAPInfo.RootPaths' in that the
##  former is a list of directory objects and the latter a list of strings.
##
##  The directory <name> must exist in at least one of the root directories,
##  otherwise `fail' is returned.
#T why the hell was this defined that way?
#T returning an empty list would be equally good!
##
##  As the files in the {\GAP} root directory (see~"GAP Root Directory") can
##  be distributed into different directories in the filespace a list of
##  directories is returned.  In order to find an existing file in a {\GAP}
##  root directory you should pass that list to `Filename' (see~"Filename")
##  as the first argument.
##  In order to create a filename for a new file inside a {\GAP} root
##  directory you should pass the first entry of that list.
##  However, creating files inside the {\GAP} root directory is not
##  recommended, you should use `DirectoryTemporary' instead.
##
BIND_GLOBAL( "DirectoriesLibrary", function( arg )
    local   name,  dirs,  dir,  path;

    if 0 = Length(arg)  then
        name := "lib";
    elif 1 = Length(arg)  then
        name := arg[1];
    else
        Error( "usage: DirectoriesLibrary( [<name>] )" );
    fi;

    if '\\' in name or ':' in name  then
        Error( "<name> must not contain '\\' or ':'" );
    fi;
    if not IsBound( GAPInfo.DirectoriesLibrary.( name ) )  then
        dirs := [];
        for dir  in GAPInfo.RootPaths  do
            path := Concatenation( dir, name );
            if IsDirectoryPath(path) = true  then
                Add( dirs, Directory(path) );
            fi;
        od;
        if 0 < Length(dirs)  then
            GAPInfo.DirectoriesLibrary.( name ) := Immutable(dirs);
        else
            return fail;
        fi;
    fi;

    return GAPInfo.DirectoriesLibrary.( name );
end );



#############################################################################
##
#F  DirectoriesSystemPrograms() . . . . .  directories of the system programs
##
##  `DirectoriesSystemPrograms' returns the directory objects for the list of
##  directories where the system programs reside as a list.  Under UNIX this
##  would usually represent `\$PATH'.
##
BIND_GLOBAL( "DirectoriesSystemPrograms", function()
    if GAPInfo.DirectoriesPrograms = false  then
        GAPInfo.DirectoriesPrograms :=
            List( GAPInfo.DirectoriesSystemPrograms, Directory );
    fi;
    return GAPInfo.DirectoriesPrograms;
end );


#############################################################################
##
#F  DirectoryTemporary( <hint> )  . . . . . . .  create a temporary directory
#F  DirectoryTemporary()  . . . . . . . . . . .  create a temporary directory
##
##  returns  a directory  object in the   category `IsDirectory' for a  *new*
##  temporary directory.   This is guaranteed to  be  newly created and empty
##  immediately  after the call to `DirectoryTemporary'.   {\GAP} will make a
##  reasonable effort   to *remove* this   directory  either  when a  garbage
##  collection  collects the directory   object  or upon termination  of  the
##  {\GAP}   job that   created  the  directory.     <hint> can  be  used  by
##  `DirectoryTemporary' to construct    the  name  of the    directory   but
##  `DirectoryTemporary' is free to use only a  part of <hint> or even ignore
##  it completely.
##
##  If `DirectoryTemporary' is  unable to create a  new  directory, `fail' is
##  returned.  In this case `LastSystemError' can be  used to get information
##  about the error.
##
BIND_GLOBAL( "DirectoryTemporary", function( arg )
    local   dir;

    # check arguments
    if 1 < Length(arg)  then
        Error( "usage: DirectoryTemporary( [<hint>] )" );
    fi;

    # create temporary directory
    dir := TmpDirectory();
    if dir = fail  then
        return fail;
    fi;

    # remember directory name and return
    Add( GAPInfo.DirectoriesTemporary, dir );
    return Directory(dir);
end );


#T THIS IS A HACK UNTIL `RemoveDirectory' IS AVAILABLE
InputTextNone := "2b defined";
OutputTextNone := "2b defined";
Process := "2b defined";

if ARCH_IS_UNIX() then
  # as we use `rm' this will only run under UNIX.
  InstallAtExit( function()
      local    i,  input,  output,  tmp,  rm,  proc;

      input  := InputTextNone();
      output := OutputTextNone();
      tmp    := Directory("/tmp");
      rm     := Filename( DirectoriesSystemPrograms(), "rm" );
      if rm = fail  then
	  Print("#W  cannot execute `rm' to remove temporary directories\n");
	  return;
      fi;

      for i  in GAPInfo.DirectoriesTemporary  do
	  proc := Process( tmp, rm, input, output, [ "-rf", i ] );
      od;

  end );
fi;


#############################################################################
##
#F  DirectoryCurrent()  . . . . . . . . . . . . . . . . . . current directory
##
##  returns the directory object for the current directory.
#T  THIS IS A HACK (will not work if SetDirectoryCurrent is implemented)
##
BIND_GLOBAL( "DirectoryCurrent", function()
    if IsBool( GAPInfo.DirectoryCurrent )  then
        GAPInfo.DirectoryCurrent := Directory("./");
    fi;
    return GAPInfo.DirectoryCurrent;
end );


#############################################################################
##
#F  CrcFile( <name-file> )  . . . . . . . . . . . . . . . .  create crc value
##
##  computes a checksum value for the file with filename <name-file> and
##  returns this value as an integer.
##  See Section~"CRC Numbers" for an example.
##  The function returns `fail' if a system error occurred, say, for example,
##  if <name-file> does not exist.
##  In this case the function `LastSystemError' (see~"LastSystemError")
##  can be used to get information about the error.
##
BIND_GLOBAL( "CrcFile", function( name )
    if IsReadableFile(name) <> true  then
        return fail;
    fi;
    return GAP_CRC(name);
end );


#############################################################################
##
#F  LoadDynamicModule( <filename> [, <crc> ] )  . . . . . . . . load a module
##
BIND_GLOBAL( "LoadDynamicModule", function( arg )

    if Length(arg) = 1  then
        if not LOAD_DYN( arg[1], false )  then
            Error( "no support for dynamic loading" );
        fi;
    elif Length(arg) = 2  then
        if not LOAD_DYN( arg[1], arg[2] )  then
            Error( "<crc> mismatch (or no support for dynamic loading)" );
        fi;
    else
        Error( "usage: LoadDynamicModule( <filename> [, <crc> ] )" );
    fi;

end );

#############################################################################
##
#F  LoadStaticModule( <filename> [, <crc> ] )   . . . . . . . . load a module
##
BIND_GLOBAL( "LoadStaticModule", function( arg )

    if Length(arg) = 1  then
        if not arg[1] in SHOW_STAT() then
            Error( "unknown static module ", arg[1] );
        fi;

        if not LOAD_STAT( arg[1], false )  then
            Error( "loading static module ", arg[1], " failed" );
        fi;
    elif Length(arg) = 2  then
        if not arg[1] in SHOW_STAT() then
            Error( "unknown static module ", arg[1] );
        fi;

        if not LOAD_STAT( arg[1], arg[2] )  then
            Error( "loading static module ", arg[1],
                   " failed, possible crc mismatch" );
        fi;
    else
        Error( "usage: LoadStaticModule( <filename> [, <crc> ] )" );
    fi;

end );


#############################################################################
##

#V  EDITOR  . . . . . . . . . . . . . . . . . . . . default editor for `Edit'
##
EDITOR := "vi";


#############################################################################
##
#F  Edit( <filename> )  . . . . . . . . . . . . . . . . .  edit and read file
##
##  `Edit' starts an editor with the file whose filename is given by the
##  string <filename>, and reads the file back into {\GAP} when you exit the
##  editor again.
##  You should set the {\GAP} variable `EDITOR' to the name of
##  the editor that you usually use, e.g., `/usr/ucb/vi'.
##  This can for example be done in your `.gaprc' file (see the sections on
##  operating system dependent features in Chapter~"Installing GAP").
##
DeclareGlobalFunction( "Edit" );


#############################################################################
##

#F  CreateCompletionFiles() . . . . . . . . . . . create "lib/readX.co" files
#F  CreateCompletionFiles( <path> ) . . . . . . . create "lib/readX.co" files
##
##  To create  completion files you must  have write permissions to `<path>',
##  which defaults to the  first root directory.   Start {\GAP} with the `-N'
##  option (to  suppress the reading  of any existing completion files), then
##  execute the command `CreateCompletionFiles( <path> );', where <path> is a
##  string giving a   path to the home   directory of  {\GAP} (the  directory
##  containing the `lib' directory).
##
##  This produces, in addition to lots of informational output,
##  the completion files.
##
DeclareGlobalFunction( "CreateCompletionFiles" );


#############################################################################
##
#O  CheckCompletionFiles()  . . . . . . . . . . .  check the completion files
##
DeclareGlobalFunction("CheckCompletionFiles");


#############################################################################
##

#E