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 char email_address [80]="tgud@tochnapc2.technion.ac.il";
84 /* We just call the parser to get commands from the user. We quit when parser returns. */
87 if (!init ()) return (0); /* Perform some initial initialization */
90 parser (); /* Get and parse user commands */
92 prepare_to_close (); /* Do some cleanup */
93 printf ("Quitting ...\n");
94 return (1); /* And quit */
102 This function asks the user for a command and calls the dispatcher function, dispatch, to analyze it.
103 We use the readline library function readline to read the command, hence all the usual readline keys
105 The new command is saved both in the readline's history and in our tiny one-command cache, so that
106 only the enter key is needed to retype it.
111 char *ptr,command_line [80];
116 if (redraw_request) { /* Terminal screen size has changed */
117 dispatch ("redraw");dispatch ("show");redraw_request=0;
120 wmove (command_win,0,0);wclrtoeol (command_win);refresh_command_win ();
122 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0); /* At last ! I spent ** days ** on this one */
124 /* The ncurses library optimizes cursor movement by */
125 /* keeping track of the cursor position. However, by */
126 /* using the readline library I'm breaking its */
127 /* assumptions. The double -1 arguments tell ncurses */
128 /* to disable cursor movement optimization this time. */
130 ptr=readline ("ext2ed > "); /* Read the user's command line. */
133 strcpy (command_line,ptr); /* Readline allocated the buffer - Copy the string */
134 free (ptr); /* and free the allocated buffer */
136 if (*command_line != 0)
137 add_history (command_line); /* Add the non-empty command to the command histroy */
139 if (*command_line==0) /* If only enter was pressed, recall the last command */
140 strcpy (command_line,last_command_line);
142 /* Emulate readline's actions for ncurses */
144 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0); /* Again, needed for correct integration of the */
145 /* ncurses and readline libraries */
147 werase (command_win);
148 wprintw (command_win,"ext2ed > ");wprintw (command_win,command_line);
149 wprintw (command_win,"\n");refresh_command_win ();
151 strcpy (last_command_line,command_line); /* Save this command in our tiny cache */
153 quit=dispatch (command_line); /* And call dispatch to do the actual job */
158 int dispatch (char *command_line)
162 This is a very important function. Its task is to recieve a command name and link it to a C function.
163 There are three type of commands:
165 1. General commands - Always available and accessed through general_commands.
166 2. Ext2 specific commands - Available when editing an ext2 filesystem, accessed through ext2_commands.
167 3. Type specific commands - Those are changing according to the current type. The global
168 variable current_type points to the current object definition (of type struct_descriptor).
169 In it, the struct_commands entry contains the type specific commands links.
171 Overriding is an important feature - Much like in C++ : The same command name can dispatch to different
172 functions. The overriding priority is 3,2,1; That is - A type specific command will always override a
173 general command. This is used through the program to allow fine tuned operation.
175 When an handling function is found, it is called along with the command line that was passed to us. The handling
176 function is then free to interpert the arguments in its own style.
185 parse_word (command_line,command);
187 if (strcasecmp (command,"quit")==0) return (1);
189 /* 1. Search for type specific commands FIRST - Allows overriding of a general command */
191 if (current_type != NULL)
192 for (i=0;i<=current_type->type_commands.last_command && !found;i++) {
193 if (strcasecmp (command,current_type->type_commands.names [i])==0) {
194 (*current_type->type_commands.callback [i]) (command_line);
199 /* 2. Now search for ext2 filesystem general commands */
202 for (i=0;i<=ext2_commands.last_command && !found;i++) {
203 if (strcasecmp (command,ext2_commands.names [i])==0) {
204 (*ext2_commands.callback [i]) (command_line);
210 /* 3. If not found, search the general commands */
213 for (i=0;i<=general_commands.last_command && !found;i++) {
214 if (strcasecmp (command,general_commands.names [i])==0) {
215 (*general_commands.callback [i]) (command_line);
220 /* 4. If not found, issue an error message and return */
223 wprintw (command_win,"Error: Unknown command\n");
224 refresh_command_win ();
230 char *parse_word (char *source,char *dest)
234 This function copies the next word in source to the variable dest, ignoring whitespaces.
235 It returns a pointer to the next word in source.
236 It is used to split the command line into command and arguments.
241 char ch,*source_ptr,*target_ptr;
248 source_ptr=source;target_ptr=dest;
251 } while (! (ch>' ' && ch<='z') && ch!=0);
253 while (ch>' ' && ch<='z') {
263 } while (! (ch>' ' && ch<='z') && ch!=0);
265 return (--source_ptr);
268 char *complete_command (char *text,int state)
272 text is the partial command entered by the user; We assume that it is a part of a command - I didn't write code
273 for smarter completion.
275 The state variable is an index which tells us how many possible completions we already returned to readline.
277 We return only one possible completion or (char *) NULL if there are no more completions. This
278 function will be called by readline over and over until we tell it to stop.
280 While scanning for possible completions, we use the same priority definition which was used in dispatch.
290 /* Is the command type specific ? */
292 if (current_type != NULL)
293 for (i=0;i<=current_type->type_commands.last_command;i++) {
294 if (strncmp (current_type->type_commands.names [i],text,len)==0) {
296 if (state==state_index) {
297 return (dupstr (current_type->type_commands.names [i]));
302 /* No, pehaps ext2 specific command then ? */
304 for (i=0;i<=ext2_commands.last_command;i++) {
305 if (strncmp (ext2_commands.names [i],text,len)==0) {
307 if (state==state_index)
308 return (dupstr (ext2_commands.names [i]));
313 /* Check for a general command */
315 for (i=0;i<=general_commands.last_command;i++) {
316 if (strncmp (general_commands.names [i],text,len)==0) {
318 if (state==state_index)
319 return (dupstr (general_commands.names [i]));
323 /* quit is handled differently */
325 if (strncmp ("quit",text,len)==0) {
327 if (state==state_index)
328 return (dupstr ("quit"));
331 /* No more completions */
333 return ((char *) NULL);
336 char *dupstr (char *src)
340 Nothing special - Just allocates enough space and copy the string.
347 ptr=(char *) malloc (strlen (src)+1);
354 void internal_error (char *description,char *source_name,char *function_name)
358 This function reports an internal error. It is almost not used. One place in which I do check for internal
361 We just report the error, and try to continue ...
366 wprintw (command_win,"Internal error - Found by source: %s.c , function: %s\n",source_name,function_name);
367 wprintw (command_win,"\t%s\n",description);
368 wprintw (command_win,"Press enter to (hopefully) continue\n");
369 refresh_command_win ();getch ();werase (command_win);