Whamcloud - gitweb
367c4eae8a24a11ad9975593280d49ed4c0d4fe7
[tools/e2fsprogs.git] / ext2ed / main.c
1 /*
2
3 /usr/src/ext2ed/main.c
4
5 A part of the extended file system 2 disk editor.
6
7 ------------
8 Main program
9 ------------
10
11 This file mostly contains:
12
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.
18
19 First written on: March 30 1995
20
21 Copyright (C) 1995 Gadi Oxman
22
23 */
24  
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <readline.h>
30 #include <history.h>
31
32 #include "ext2ed.h"
33
34 /* Global variables */
35
36 /*
37
38 Configuration file options
39
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.
42
43 */
44
45
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 */
57
58 char last_command_line [80];                    /* A simple one command cache, in addition to the readline history */
59
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 */
64                                         
65 int mounted=0;                                  /* This is set when we find that the filesystem is mounted */
66
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 */
77
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";
83
84 int main (void)
85
86 /* We just call the parser to get commands from the user. We quit when parser returns. */
87
88 {
89         if (!init ()) return (0);                               /* Perform some initial initialization */
90                                                                 /* Quit if failed */
91
92         parser ();                                              /* Get and parse user commands */
93         
94         prepare_to_close ();                                    /* Do some cleanup */
95         printf ("Quitting ...\n");
96         return (1);                                             /* And quit */
97 }
98
99
100 void parser (void)
101
102 /*
103
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
106 are available.
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.
109
110 */
111
112 {
113         char *ptr,command_line [80];
114         int quit=0;
115
116         while (!quit) {
117                 
118                 if (redraw_request) {                           /* Terminal screen size has changed */
119                         dispatch ("redraw");dispatch ("show");redraw_request=0;
120                 }
121
122                 wmove (command_win,0,0);wclrtoeol (command_win);refresh_command_win ();
123
124                 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);        /* At last ! I spent ** days ** on this one */
125
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. */
131                 //echo ();
132                 ptr=readline ("ext2ed > ");                     /* Read the user's command line. */
133                 //noecho ();
134
135                 strcpy (command_line,ptr);                      /* Readline allocated the buffer - Copy the string */
136                 free (ptr);                                     /* and free the allocated buffer */
137
138                 if (*command_line != 0)
139                         add_history (command_line);             /* Add the non-empty command to the command histroy */
140
141                 if (*command_line==0)                           /* If only enter was pressed, recall the last command */
142                         strcpy (command_line,last_command_line);
143                 
144                                                                 /* Emulate readline's actions for ncurses */
145
146                 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);        /* Again, needed for correct integration of the */
147                                                                 /* ncurses and readline libraries */
148
149                 werase (command_win);
150                 wprintw (command_win,"ext2ed > ");wprintw (command_win,command_line);
151                 wprintw (command_win,"\n");refresh_command_win ();
152
153                 strcpy (last_command_line,command_line);        /* Save this command in our tiny cache */
154
155                 quit=dispatch (command_line);                   /* And call dispatch to do the actual job */
156         }               
157 }
158
159
160 int dispatch (char *command_line)
161
162 /*
163
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:
166
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.
172         
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.
176
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.
179
180 */
181
182 {
183         int i,found=0;
184         
185         char command [80];
186
187         parse_word (command_line,command);
188                         
189         if (strcasecmp (command,"quit")==0) return (1); 
190
191         /* 1. Search for type specific commands FIRST - Allows overriding of a general command */
192
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);
197                                 found=1;
198                         }
199                 }
200
201         /* 2. Now search for ext2 filesystem general commands */
202
203         if (!found)
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);
207                                 found=1;
208                         }
209                 }
210
211         
212         /* 3. If not found, search the general commands */
213         
214         if (!found)
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);
218                                 found=1;
219                         }
220                 }
221
222         /* 4. If not found, issue an error message and return */
223         
224         if (!found) {
225                 wprintw (command_win,"Error: Unknown command\n");
226                 refresh_command_win ();
227         }
228         
229         return (0);
230 }
231
232 char *parse_word (char *source,char *dest)
233
234 /*
235
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.
239
240 */
241
242 {
243         char ch,*source_ptr,*target_ptr;
244         
245         if (*source==0) {
246                 *dest=0;
247                 return (source);
248         };
249         
250         source_ptr=source;target_ptr=dest;
251         do {
252                 ch=*source_ptr++;
253         } while (! (ch>' ' && ch<='z') && ch!=0);
254
255         while (ch>' ' && ch<='z') {
256                 *target_ptr++=ch;
257                 ch=*source_ptr++;       
258         }
259
260         *target_ptr=0;
261
262         source_ptr--;
263         do {
264                 ch=*source_ptr++;
265         } while (! (ch>' ' && ch<='z') && ch!=0);
266
267         return (--source_ptr);
268 }
269
270 char *complete_command (char *text,int state)
271
272 /*
273
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.
276
277 The state variable is an index which tells us how many possible completions we already returned to readline.
278
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.
281
282 While scanning for possible completions, we use the same priority definition which was used in dispatch.
283
284 */
285
286 {
287         int state_index=-1;
288         int i,len;
289         
290         len=strlen (text);
291
292         /* Is the command type specific ? */
293
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) {
297                                 state_index++;
298                                 if (state==state_index) {
299                                         return (dupstr (current_type->type_commands.names [i]));
300                                 }
301                         }
302                 }
303
304         /* No, pehaps ext2 specific command then ? */
305         
306         for (i=0;i<=ext2_commands.last_command;i++) {
307                 if (strncmp (ext2_commands.names [i],text,len)==0) {
308                         state_index++;
309                         if (state==state_index)
310                         return (dupstr (ext2_commands.names [i]));
311                 }
312         }
313
314         
315         /* Check for a general command */
316         
317         for (i=0;i<=general_commands.last_command;i++) {
318                 if (strncmp (general_commands.names [i],text,len)==0) {
319                                 state_index++;
320                                 if (state==state_index)
321                                         return (dupstr (general_commands.names [i]));
322                 }
323         }
324
325         /* quit is handled differently */
326         
327         if (strncmp ("quit",text,len)==0) {
328                 state_index++;
329                 if (state==state_index)
330                         return (dupstr ("quit"));       
331         }
332
333         /* No more completions */
334         
335         return ((char *) NULL);
336 }
337
338 char *dupstr (char *src)
339
340 /* 
341
342 Nothing special - Just allocates enough space and copy the string.
343
344 */
345
346 {
347         char *ptr;
348         
349         ptr=(char *) malloc (strlen (src)+1);
350         strcpy (ptr,src);
351         return (ptr);
352 }
353
354 #ifdef DEBUG
355
356 void internal_error (char *description,char *source_name,char *function_name)
357
358 /*
359
360 This function reports an internal error. It is almost not used. One place in which I do check for internal
361 errors is disk.c.
362
363 We just report the error, and try to continue ...
364
365 */
366
367 {
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);
372 }
373
374 #endif