5 A part of the extended file system 2 disk editor.
11 This file mostly contains:
13 1. A list of global variables used through the entire program.
14 2. The parser, which asks the command line from the user.
15 3. The dispatcher, which analyzes the command line and calls the appropriate handler function.
16 4. A command pattern matcher which is used along with the readline completion feature.
17 5. A function which tells the user that an internal error has occured.
19 First written on: March 30 1995
21 Copyright (C) 1995 Gadi Oxman
34 /* Global variables */
38 Configuration file options
40 The following variables will be set by init.c to the values selected in the user configuration file.
41 They are initialized below to some logical defaults.
46 char Ext2Descriptors [200]="ext2.descriptors"; /* The location of the ext2 filesystem object definition */
47 char AlternateDescriptors [200]=""; /* We allow the user to define additional structures */
48 char LogFile [200]="ext2ed.log"; /* The location of the log file - Each write will be logged there */
49 int LogChanges=1; /* 1 enables logging, 0 diables logging */
50 int AllowChanges=0; /* When set, the enablewrite command will fail */
51 int AllowMountedRead=0; /* Behavior when trying to open a mounted filesystem read-only */
52 int ForceExt2=0; /* When set, ext2 autodetection is overridden */
53 int DefaultBlockSize=1024;
54 unsigned long DefaultTotalBlocks=2097151;
55 unsigned long DefaultBlocksInGroup=8192; /* The default values are used when an ext2 filesystem is not */
56 int ForceDefault=0; /* detected, or ForceDefault is set */
58 char last_command_line [80]; /* A simple one command cache, in addition to the readline history */
60 char device_name [80]; /* The location of the filesystem */
61 FILE *device_handle=NULL; /* This is passed to the fopen / fread ... commands */
62 long device_offset; /* The current position in the filesystem */
63 /* Note that we have a 2 GB limitation */
65 int mounted=0; /* This is set when we find that the filesystem is mounted */
67 struct struct_commands general_commands,ext2_commands; /* Used to define the general and ext2 commands */
68 struct struct_descriptor *first_type,*last_type,*current_type; /* Used to access the double linked list */
69 struct struct_type_data type_data; /* The current data is sometimes stored here */
70 struct struct_file_system_info file_system_info; /* Essential information on the filesystem */
71 struct struct_file_info file_info,first_file_info; /* Used by file_com.c to access files */
72 struct struct_group_info group_info; /* Used by group_com.c */
73 struct struct_super_info super_info; /* Used by super_com.c */
74 struct struct_remember_lifo remember_lifo; /* A circular memory of objects */
75 struct struct_block_bitmap_info block_bitmap_info; /* Used by blockbitmap_com.c */
76 struct struct_inode_bitmap_info inode_bitmap_info; /* Used by inodebitmap_com.c */
78 int redraw_request=0; /* Is set by a signal handler to handle terminal */
79 /* screen size change. */
80 int version_major=0,version_minor=2;
81 char revision_date [80]="April 5 2001";
82 char email_address [80]="tgud@tochnapc2.technion.ac.il";
86 /* We just call the parser to get commands from the user. We quit when parser returns. */
89 if (!init ()) return (0); /* Perform some initial initialization */
92 parser (); /* Get and parse user commands */
94 prepare_to_close (); /* Do some cleanup */
95 printf ("Quitting ...\n");
96 return (1); /* And quit */
104 This function asks the user for a command and calls the dispatcher function, dispatch, to analyze it.
105 We use the readline library function readline to read the command, hence all the usual readline keys
107 The new command is saved both in the readline's history and in our tiny one-command cache, so that
108 only the enter key is needed to retype it.
113 char *ptr,command_line [80];
118 if (redraw_request) { /* Terminal screen size has changed */
119 dispatch ("redraw");dispatch ("show");redraw_request=0;
122 wmove (command_win,0,0);wclrtoeol (command_win);refresh_command_win ();
124 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0); /* At last ! I spent ** days ** on this one */
126 /* The ncurses library optimizes cursor movement by */
127 /* keeping track of the cursor position. However, by */
128 /* using the readline library I'm breaking its */
129 /* assumptions. The double -1 arguments tell ncurses */
130 /* to disable cursor movement optimization this time. */
132 ptr=readline ("ext2ed > "); /* Read the user's command line. */
135 strcpy (command_line,ptr); /* Readline allocated the buffer - Copy the string */
136 free (ptr); /* and free the allocated buffer */
138 if (*command_line != 0)
139 add_history (command_line); /* Add the non-empty command to the command histroy */
141 if (*command_line==0) /* If only enter was pressed, recall the last command */
142 strcpy (command_line,last_command_line);
144 /* Emulate readline's actions for ncurses */
146 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0); /* Again, needed for correct integration of the */
147 /* ncurses and readline libraries */
149 werase (command_win);
150 wprintw (command_win,"ext2ed > ");wprintw (command_win,command_line);
151 wprintw (command_win,"\n");refresh_command_win ();
153 strcpy (last_command_line,command_line); /* Save this command in our tiny cache */
155 quit=dispatch (command_line); /* And call dispatch to do the actual job */
160 int dispatch (char *command_line)
164 This is a very important function. Its task is to recieve a command name and link it to a C function.
165 There are three type of commands:
167 1. General commands - Always available and accessed through general_commands.
168 2. Ext2 specific commands - Available when editing an ext2 filesystem, accessed through ext2_commands.
169 3. Type specific commands - Those are changing according to the current type. The global
170 variable current_type points to the current object definition (of type struct_descriptor).
171 In it, the struct_commands entry contains the type specific commands links.
173 Overriding is an important feature - Much like in C++ : The same command name can dispatch to different
174 functions. The overriding priority is 3,2,1; That is - A type specific command will always override a
175 general command. This is used through the program to allow fine tuned operation.
177 When an handling function is found, it is called along with the command line that was passed to us. The handling
178 function is then free to interpert the arguments in its own style.
187 parse_word (command_line,command);
189 if (strcasecmp (command,"quit")==0) return (1);
191 /* 1. Search for type specific commands FIRST - Allows overriding of a general command */
193 if (current_type != NULL)
194 for (i=0;i<=current_type->type_commands.last_command && !found;i++) {
195 if (strcasecmp (command,current_type->type_commands.names [i])==0) {
196 (*current_type->type_commands.callback [i]) (command_line);
201 /* 2. Now search for ext2 filesystem general commands */
204 for (i=0;i<=ext2_commands.last_command && !found;i++) {
205 if (strcasecmp (command,ext2_commands.names [i])==0) {
206 (*ext2_commands.callback [i]) (command_line);
212 /* 3. If not found, search the general commands */
215 for (i=0;i<=general_commands.last_command && !found;i++) {
216 if (strcasecmp (command,general_commands.names [i])==0) {
217 (*general_commands.callback [i]) (command_line);
222 /* 4. If not found, issue an error message and return */
225 wprintw (command_win,"Error: Unknown command\n");
226 refresh_command_win ();
232 char *parse_word (char *source,char *dest)
236 This function copies the next word in source to the variable dest, ignoring whitespaces.
237 It returns a pointer to the next word in source.
238 It is used to split the command line into command and arguments.
243 char ch,*source_ptr,*target_ptr;
250 source_ptr=source;target_ptr=dest;
253 } while (! (ch>' ' && ch<='z') && ch!=0);
255 while (ch>' ' && ch<='z') {
265 } while (! (ch>' ' && ch<='z') && ch!=0);
267 return (--source_ptr);
270 char *complete_command (char *text,int state)
274 text is the partial command entered by the user; We assume that it is a part of a command - I didn't write code
275 for smarter completion.
277 The state variable is an index which tells us how many possible completions we already returned to readline.
279 We return only one possible completion or (char *) NULL if there are no more completions. This
280 function will be called by readline over and over until we tell it to stop.
282 While scanning for possible completions, we use the same priority definition which was used in dispatch.
292 /* Is the command type specific ? */
294 if (current_type != NULL)
295 for (i=0;i<=current_type->type_commands.last_command;i++) {
296 if (strncmp (current_type->type_commands.names [i],text,len)==0) {
298 if (state==state_index) {
299 return (dupstr (current_type->type_commands.names [i]));
304 /* No, pehaps ext2 specific command then ? */
306 for (i=0;i<=ext2_commands.last_command;i++) {
307 if (strncmp (ext2_commands.names [i],text,len)==0) {
309 if (state==state_index)
310 return (dupstr (ext2_commands.names [i]));
315 /* Check for a general command */
317 for (i=0;i<=general_commands.last_command;i++) {
318 if (strncmp (general_commands.names [i],text,len)==0) {
320 if (state==state_index)
321 return (dupstr (general_commands.names [i]));
325 /* quit is handled differently */
327 if (strncmp ("quit",text,len)==0) {
329 if (state==state_index)
330 return (dupstr ("quit"));
333 /* No more completions */
335 return ((char *) NULL);
338 char *dupstr (char *src)
342 Nothing special - Just allocates enough space and copy the string.
349 ptr=(char *) malloc (strlen (src)+1);
356 void internal_error (char *description,char *source_name,char *function_name)
360 This function reports an internal error. It is almost not used. One place in which I do check for internal
363 We just report the error, and try to continue ...
368 wprintw (command_win,"Internal error - Found by source: %s.c , function: %s\n",source_name,function_name);
369 wprintw (command_win,"\t%s\n",description);
370 wprintw (command_win,"Press enter to (hopefully) continue\n");
371 refresh_command_win ();getch ();werase (command_win);