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
45 /* Global variables */
49 Configuration file options
51 The following variables will be set by init.c to the values selected in the user configuration file.
52 They are initialized below to some logical defaults.
57 char Ext2Descriptors [200]="ext2.descriptors"; /* The location of the ext2 filesystem object definition */
58 char AlternateDescriptors [200]=""; /* We allow the user to define additional structures */
59 char LogFile [200]="ext2ed.log"; /* The location of the log file - Each write will be logged there */
60 int LogChanges=1; /* 1 enables logging, 0 diables logging */
61 int AllowChanges=0; /* When set, the enablewrite command will fail */
62 int AllowMountedRead=0; /* Behavior when trying to open a mounted filesystem read-only */
63 int ForceExt2=0; /* When set, ext2 autodetection is overridden */
64 int DefaultBlockSize=1024;
65 unsigned long DefaultTotalBlocks=2097151;
66 unsigned long DefaultBlocksInGroup=8192; /* The default values are used when an ext2 filesystem is not */
67 int ForceDefault=0; /* detected, or ForceDefault is set */
69 char last_command_line [80]; /* A simple one command cache, in addition to the readline history */
71 char device_name [80]; /* The location of the filesystem */
72 FILE *device_handle=NULL; /* This is passed to the fopen / fread ... commands */
73 long device_offset; /* The current position in the filesystem */
74 /* Note that we have a 2 GB limitation */
76 int mounted=0; /* This is set when we find that the filesystem is mounted */
78 struct struct_commands general_commands,ext2_commands; /* Used to define the general and ext2 commands */
79 struct struct_descriptor *first_type,*last_type,*current_type; /* Used to access the double linked list */
80 struct struct_type_data type_data; /* The current data is sometimes stored here */
81 struct struct_file_system_info file_system_info; /* Essential information on the filesystem */
82 struct struct_file_info file_info,first_file_info; /* Used by file_com.c to access files */
83 struct struct_group_info group_info; /* Used by group_com.c */
84 struct struct_super_info super_info; /* Used by super_com.c */
85 struct struct_remember_lifo remember_lifo; /* A circular memory of objects */
86 struct struct_block_bitmap_info block_bitmap_info; /* Used by blockbitmap_com.c */
87 struct struct_inode_bitmap_info inode_bitmap_info; /* Used by inodebitmap_com.c */
89 int redraw_request=0; /* Is set by a signal handler to handle terminal */
90 /* screen size change. */
94 * We just call the parser to get commands from the user. We quit when
97 int main (int argc, char **argv)
105 while ((c = getopt (argc, argv, "w")) != EOF) {
113 buf = malloc(strlen(argv[optind]) + 32);
115 fprintf(stderr, "Couldn't allocate filename buffer\n");
118 strcpy(buf, "set_device ");
119 strcat(buf, argv[optind]);
123 wprintw (command_win,"\n");
124 enable_write("enable_write");
127 parser (); /* Get and parse user commands */
128 prepare_to_close(); /* Do some cleanup */
129 printf("Quitting ...\n");
135 * Read a character from the command window
137 int command_read_key()
142 if (redraw_request) {
146 key = wgetch(command_win);
150 kill(getpid(), SIGTSTP);
155 refresh_command_win ();
160 refresh_command_win ();
169 if ((key < 32 && key != '\b' && key != '\n') ||
177 int rl_getc_replacement(FILE *f)
179 int key = command_read_key();
183 wprintw(command_win, "\b \b");
185 wprintw(command_win, "%c", key);
190 * This function asks the user for a command and calls the dispatcher
191 * function, dispatch, to analyze it. We use the readline library
192 * function readline to read the command, hence all the usual readline
193 * keys are available. The new command is saved both in the
194 * readline's history and in our tiny one-command cache, so that only
195 * the enter key is needed to retype it.
199 char *ptr,command_line [80];
205 keypad(command_win, 1);
206 wtimeout(command_win, 100);
208 rl_getc_function = rl_getc_replacement;
212 /* Terminal screen size has changed */
213 if (redraw_request) {
218 wmove (command_win,0,0);
219 wclrtoeol (command_win);
220 wprintw (command_win,"ext2ed > ");
221 refresh_command_win ();
224 * The ncurses library optimizes cursor movement by
225 * keeping track of the cursor position. However, by
226 * using the readline library I'm breaking its
227 * assumptions. The double -1 arguments tell ncurses
228 * to disable cursor movement optimization this
231 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);
234 ptr=readline ("ext2ed > ");
238 * Readline allocated the buffer - Copy the string
239 * and free the allocated buffer
242 strcpy (command_line,ptr);
245 if (*command_line != 0)
246 add_history (command_line);
248 /* If only enter was pressed, recall the last command */
249 if (*command_line==0)
250 strcpy (command_line,last_command_line);
252 /* Emulate readline's actions for ncurses */
253 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);
254 werase (command_win);
255 wprintw (command_win,"ext2ed > ");
256 wprintw (command_win,command_line);
257 wprintw (command_win,"\n");
258 refresh_command_win ();
260 /* Save this command in our tiny cache */
261 strcpy (last_command_line,command_line);
263 /* And call dispatch to do the actual job */
264 quit=dispatch (command_line);
268 void read_line(char * foo) {
273 while (!done && (ch = command_read_key())) {
281 wprintw(command_win, "\b \b");
289 if (ch == '\n') break;
291 wprintw(command_win, "%c", ch);
300 char command_line [80];
305 wtimeout(command_win, 100);
306 keypad(command_win, 1);
309 /* Terminal screen size has changed */
310 if (redraw_request) {
315 wmove (command_win,0,0);wclrtoeol (command_win);
317 wmove(command_win, 0, 0);
318 wprintw(command_win, "ext2ed > ");
319 read_line(command_line);
321 /* If only enter was pressed, recall the last command */
322 if (*command_line==0)
323 strcpy (command_line,last_command_line);
325 mvcur (-1,-1,LINES-COMMAND_WIN_LINES + 1,0);
327 strcpy (last_command_line,command_line); /* Save this command in our tiny cache */
329 /* And call dispatch to do the actual job */
330 quit=dispatch (command_line);
337 * This is a very important function. Its task is to recieve a command
338 * name and link it to a C function. There are three types of commands:
340 * 1. General commands - Always available and accessed through
342 * 2. Ext2 specific commands - Available when editing an ext2
343 * filesystem, accessed through ext2_commands.
344 * 3. Type specific commands - Those are changing according to the
345 * current type. The global variable current_type points to the
346 * current object definition (of type struct_descriptor). In it, the
347 * struct_commands entry contains the type specific commands links.
349 * Overriding is an important feature - Much like in C++ : The same
350 * command name can dispatch to different functions. The overriding
351 * priority is 3,2,1; That is - A type specific command will always
352 * override a general command. This is used through the program to
353 * allow fine tuned operation.
355 * When an handling function is found, it is called along with the
356 * command line that was passed to us. The handling function is then
357 * free to interpert the arguments in its own style.
359 int dispatch (char *command_line)
365 parse_word (command_line,command);
367 if (strcasecmp (command,"quit")==0) return (1);
369 /* 1. Search for type specific commands FIRST - Allows
370 overriding of a general command */
372 if (current_type != NULL)
374 i<=current_type->type_commands.last_command && !found;
376 if (strcasecmp (command,current_type->type_commands.names [i])==0) {
377 (*current_type->type_commands.callback [i]) (command_line);
382 /* 2. Now search for ext2 filesystem general commands */
385 for (i=0;i<=ext2_commands.last_command && !found;i++) {
386 if (strcasecmp (command,ext2_commands.names [i])==0) {
387 (*ext2_commands.callback [i]) (command_line);
393 /* 3. If not found, search the general commands */
396 for (i=0;i<=general_commands.last_command && !found;i++) {
397 if (strcasecmp (command,general_commands.names [i])==0) {
398 (*general_commands.callback [i]) (command_line);
403 /* 4. If not found, issue an error message and return */
406 wprintw (command_win,"Error: Unknown command\n");
407 refresh_command_win ();
416 * This function copies the next word in source to the variable dest,
417 * ignoring whitespaces. It returns a pointer to the next word in
418 * source. It is used to split the command line into command and arguments.
420 char *parse_word (char *source,char *dest)
422 char ch,*source_ptr,*target_ptr;
429 source_ptr=source;target_ptr=dest;
432 } while (! (ch>' ' && ch<='z') && ch!=0);
434 while (ch>' ' && ch<='z') {
444 } while (! (ch>' ' && ch<='z') && ch!=0);
446 return (--source_ptr);
450 * text is the partial command entered by the user; We assume that it
451 * is a part of a command - I didn't write code for smarter completion.
453 * The state variable is an index which tells us how many possible
454 * completions we already returned to readline.
456 * We return only one possible completion or (char *) NULL if there
457 * are no more completions. This function will be called by readline
458 * over and over until we tell it to stop.
460 * While scanning for possible completions, we use the same priority
461 * definition which was used in dispatch.
464 char *complete_command (char *text,int state)
471 /* Is the command type specific ? */
473 if (current_type != NULL)
474 for (i=0;i<=current_type->type_commands.last_command;i++) {
475 if (strncmp (current_type->type_commands.names [i],text,len)==0) {
477 if (state==state_index) {
478 return (dupstr (current_type->type_commands.names [i]));
483 /* No, pehaps ext2 specific command then ? */
485 for (i=0;i<=ext2_commands.last_command;i++) {
486 if (strncmp (ext2_commands.names [i],text,len)==0) {
488 if (state==state_index)
489 return (dupstr (ext2_commands.names [i]));
494 /* Check for a general command */
496 for (i=0;i<=general_commands.last_command;i++) {
497 if (strncmp (general_commands.names [i],text,len)==0) {
499 if (state==state_index)
500 return (dupstr (general_commands.names [i]));
504 /* quit is handled differently */
506 if (strncmp ("quit",text,len)==0) {
508 if (state==state_index)
509 return (dupstr ("quit"));
512 /* No more completions */
514 return ((char *) NULL);
520 * Nothing special - Just allocates enough space and copy the string.
522 char *dupstr (char *src)
526 ptr=(char *) malloc (strlen (src)+1);
533 * This function reports an internal error. It is almost not used. One
534 * place in which I do check for internal errors is disk.c.
536 * We just report the error, and try to continue ...
538 void internal_error (char *description,char *source_name,char *function_name)
540 wprintw (command_win,"Internal error - Found by source: %s.c , function: %s\n",source_name,function_name);
541 wprintw (command_win,"\t%s\n",description);
542 wprintw (command_win,"Press enter to (hopefully) continue\n");
543 refresh_command_win ();getch ();werase (command_win);