Makefile
,
which should permit us to compile your program simply by typing
``make
.'' A basic discussion of ``make
''
can be found in Section 11.17 of Kelley and Pohl.
I have also placed online a tutorial on make
,
in Postscript form
or ASCII text form. The program you write for this exercise will probably be between 400 and 500 lines of C. Whereas last week's exercise could be done fairly easily using just arrays, for the new exercise you will probably find it advantageous to make full use of structures and pointers.
view
.
When you start view
, it enters command mode.
In command mode, view
prints a prompt
and waits for you to enter a command line. When you enter a command line,
view
processes that command and then returns to command mode.
There are nine commands, which have extremely simple formats:
view
will read the entire
contents of the file named filename
into memory.
The first line of the file becomes the current line.
view
will write out its
in-core data to the file named filename
. Any previous
contents of that file are lost.
view
to type out the contents of
the current line on the display.
ENTER
key
causes view
to type out the line just after the current
line. The line typed becomes the new current line.
If there is no next line, then the command has no effect.
view
to type out the contents of the
line just preceding the current line.
The line typed becomes the new current line.
If there is no preceding line, then the command has no effect.
view
to terminate execution.
view
deletes the current line.
If there is a line following the current line, that line becomes
the new current line. Otherwise, if there is a line preceding
the current line, then that line becomes the new current line.
view
that you want to
insert lines
of new text before the current line.
In response to this command, view
enters
text mode.
In text mode, view
reads lines of text from the terminal
and inserts them into memory before the current line.
When the user generates an end-of-file at the terminal
(by typing CTRL-D
), then
text mode ends and view
returns to command mode.
The last line of the new text becomes the new current line.
view
that you want to append
lines of new text after the current line.
In response to this command, view
enters
text mode.
In text mode, view
reads lines of text from the terminal
and appends them in memory after the current line.
When the user generates an end-of-file at the terminal, then
text mode ends and view
returns to command mode.
The last line of the new text becomes the new current line.
view
program should work correctly no matter how long the
lines are, in the files that are read in, or in the input supplied
interactively by the user. This means that your program will have to allocate
memory on the fly from the heap area. When your program is unable to allocate
enough memory from the heap area, it should fail in a graceful manner.
view
.
The ?
character is view
's prompt while in
command mode. There is no prompt while view
is in text mode.
current_line
is either
NULL
,
if there are no lines of text currently in memory, or else it is
a pointer to a line
structure containing the current line.
The field current_line->text
is a pointer to a string
containing the actual text in the line.
The fields current_line->next
and
current_line->prev
are pointers to the next and previous lines if there are any,
otherwise NULL
.
buffer.c
to functions that are
responsible for manipulating the doubly linked list.
Include the following functions:
next_line()
and previous_line()
functions
simply adjust the value of the current_line
variable to
move forward or backward in the file.
The delete_line()
function deletes the current line
(freeing the storage that it occupied using free()
).
The functions insert_line()
and append_line()
take a string as their argment and either insert that line
before the current line or append it after the current line,
as appropriate. These functions have to allocate (using
malloc()
) a line
structure for the inserted
line and link it in place in the list.
fileio.c
to functions that
are responsible for reading and writing data to files.
Include functions:
The read_file()
function takes a file name as an argument,
opens the file for reading (mode "r"
), then repeatedly
uses get_line()
to read each line of the file and calls
append_line()
to insert that line into the doubly linked
list. When end of file is encountered, the file is closed.
The number of lines read is returned.
The write_file()
function takes a file name as an argument,
opens the file for writing (mode "w"
), then writes each
line of data from memory into the file. When all lines have been
written, the file is closed.
The number of lines written is returned.
The get_line()
function takes a FILE
pointer
as an argument and reads a line of text from that file, allocating
memory as necessary (using malloc()
and
realloc()
)
to hold the line as a string. A pointer to the string is returned.
command.c
to the reading and
processing of commands from the user. Include functions:
read_command()
function reads a command line from the
user and returns a pointer to a string containing that line.
The process_command()
function takes a command line as
an argument, uses the first character of the line to determine
what type of command it is, and then performs the indicated
processing. Most of the work should be done by calling functions
from other source files.
main
should be very simple:
it should perhaps perform a little initialization, then just
consist of a loop that repeatedly calls read_command()
and process_command()
.