Whamcloud - gitweb
5ea116bd3c5ce7b02a1ca5cc36a3c336dda2ea1f
[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 char email_address [80]="tgud@tochnapc2.technion.ac.il";
81
82 int main (void)
83
84 /* We just call the parser to get commands from the user. We quit when parser returns. */
85
86 {
87         if (!init ()) return (0);                               /* Perform some initial initialization */
88                                                                 /* Quit if failed */
89
90         parser ();                                              /* Get and parse user commands */
91         
92         prepare_to_close ();                                    /* Do some cleanup */
93         printf ("Quitting ...\n");
94         return (1);                                             /* And quit */
95 }
96
97
98 void parser (void)
99
100 /*
101
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
104 are available.
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.
107
108 */
109
110 {
111         char *ptr,command_line [80];
112         int quit=0;
113
114         while (!quit) {
115                 
116                 if (redraw_request) {                           /* Terminal screen size has changed */
117                         dispatch ("redraw");dispatch ("show");redraw_request=0;
118                 }
119
120                 wmove (command_win,0,0);wclrtoeol (command_win);refresh_command_win ();
121
122                 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);        /* At last ! I spent ** days ** on this one */
123
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. */
129                 //echo ();
130                 ptr=readline ("ext2ed > ");                     /* Read the user's command line. */
131                 //noecho ();
132
133                 strcpy (command_line,ptr);                      /* Readline allocated the buffer - Copy the string */
134                 free (ptr);                                     /* and free the allocated buffer */
135
136                 if (*command_line != 0)
137                         add_history (command_line);             /* Add the non-empty command to the command histroy */
138
139                 if (*command_line==0)                           /* If only enter was pressed, recall the last command */
140                         strcpy (command_line,last_command_line);
141                 
142                                                                 /* Emulate readline's actions for ncurses */
143
144                 mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);        /* Again, needed for correct integration of the */
145                                                                 /* ncurses and readline libraries */
146
147                 werase (command_win);
148                 wprintw (command_win,"ext2ed > ");wprintw (command_win,command_line);
149                 wprintw (command_win,"\n");refresh_command_win ();
150
151                 strcpy (last_command_line,command_line);        /* Save this command in our tiny cache */
152
153                 quit=dispatch (command_line);                   /* And call dispatch to do the actual job */
154         }               
155 }
156
157
158 int dispatch (char *command_line)
159
160 /*
161
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:
164
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.
170         
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.
174
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.
177
178 */
179
180 {
181         int i,found=0;
182         
183         char command [80];
184
185         parse_word (command_line,command);
186                         
187         if (strcasecmp (command,"quit")==0) return (1); 
188
189         /* 1. Search for type specific commands FIRST - Allows overriding of a general command */
190
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);
195                                 found=1;
196                         }
197                 }
198
199         /* 2. Now search for ext2 filesystem general commands */
200
201         if (!found)
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);
205                                 found=1;
206                         }
207                 }
208
209         
210         /* 3. If not found, search the general commands */
211         
212         if (!found)
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);
216                                 found=1;
217                         }
218                 }
219
220         /* 4. If not found, issue an error message and return */
221         
222         if (!found) {
223                 wprintw (command_win,"Error: Unknown command\n");
224                 refresh_command_win ();
225         }
226         
227         return (0);
228 }
229
230 char *parse_word (char *source,char *dest)
231
232 /*
233
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.
237
238 */
239
240 {
241         char ch,*source_ptr,*target_ptr;
242         
243         if (*source==0) {
244                 *dest=0;
245                 return (source);
246         };
247         
248         source_ptr=source;target_ptr=dest;
249         do {
250                 ch=*source_ptr++;
251         } while (! (ch>' ' && ch<='z') && ch!=0);
252
253         while (ch>' ' && ch<='z') {
254                 *target_ptr++=ch;
255                 ch=*source_ptr++;       
256         }
257
258         *target_ptr=0;
259
260         source_ptr--;
261         do {
262                 ch=*source_ptr++;
263         } while (! (ch>' ' && ch<='z') && ch!=0);
264
265         return (--source_ptr);
266 }
267
268 char *complete_command (char *text,int state)
269
270 /*
271
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.
274
275 The state variable is an index which tells us how many possible completions we already returned to readline.
276
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.
279
280 While scanning for possible completions, we use the same priority definition which was used in dispatch.
281
282 */
283
284 {
285         int state_index=-1;
286         int i,len;
287         
288         len=strlen (text);
289
290         /* Is the command type specific ? */
291
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) {
295                                 state_index++;
296                                 if (state==state_index) {
297                                         return (dupstr (current_type->type_commands.names [i]));
298                                 }
299                         }
300                 }
301
302         /* No, pehaps ext2 specific command then ? */
303         
304         for (i=0;i<=ext2_commands.last_command;i++) {
305                 if (strncmp (ext2_commands.names [i],text,len)==0) {
306                         state_index++;
307                         if (state==state_index)
308                         return (dupstr (ext2_commands.names [i]));
309                 }
310         }
311
312         
313         /* Check for a general command */
314         
315         for (i=0;i<=general_commands.last_command;i++) {
316                 if (strncmp (general_commands.names [i],text,len)==0) {
317                                 state_index++;
318                                 if (state==state_index)
319                                         return (dupstr (general_commands.names [i]));
320                 }
321         }
322
323         /* quit is handled differently */
324         
325         if (strncmp ("quit",text,len)==0) {
326                 state_index++;
327                 if (state==state_index)
328                         return (dupstr ("quit"));       
329         }
330
331         /* No more completions */
332         
333         return ((char *) NULL);
334 }
335
336 char *dupstr (char *src)
337
338 /* 
339
340 Nothing special - Just allocates enough space and copy the string.
341
342 */
343
344 {
345         char *ptr;
346         
347         ptr=(char *) malloc (strlen (src)+1);
348         strcpy (ptr,src);
349         return (ptr);
350 }
351
352 #ifdef DEBUG
353
354 void internal_error (char *description,char *source_name,char *function_name)
355
356 /*
357
358 This function reports an internal error. It is almost not used. One place in which I do check for internal
359 errors is disk.c.
360
361 We just report the error, and try to continue ...
362
363 */
364
365 {
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);
370 }
371
372 #endif