1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
/*
/usr/src/ext2ed/main.c
A part of the extended file system 2 disk editor.
------------
Main program
------------
This file mostly contains:
1. A list of global variables used through the entire program.
2. The parser, which asks the command line from the user.
3. The dispatcher, which analyzes the command line and calls the appropriate handler function.
4. A command pattern matcher which is used along with the readline completion feature.
5. A function which tells the user that an internal error has occurred.
First written on: March 30 1995
Copyright (C) 1995 Gadi Oxman
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#ifdef HAVE_READLINE
#include <readline.h>
#include <history.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern int optind;
extern char *optarg;
#endif
#include "ext2ed.h"
/* Global variables */
/*
Configuration file options
The following variables will be set by init.c to the values selected in the user configuration file.
They are initialized below to some logical defaults.
*/
char Ext2Descriptors [200]="ext2.descriptors"; /* The location of the ext2 filesystem object definition */
char AlternateDescriptors [200]=""; /* We allow the user to define additional structures */
char LogFile [200]="ext2ed.log"; /* The location of the log file - Each write will be logged there */
int LogChanges=1; /* 1 enables logging, 0 disables logging */
int AllowChanges=0; /* When set, the enablewrite command will fail */
int AllowMountedRead=0; /* Behavior when trying to open a mounted filesystem read-only */
int ForceExt2=0; /* When set, ext2 autodetection is overridden */
int DefaultBlockSize=1024;
unsigned long DefaultTotalBlocks=2097151;
unsigned long DefaultBlocksInGroup=8192; /* The default values are used when an ext2 filesystem is not */
int ForceDefault=0; /* detected, or ForceDefault is set */
char last_command_line [80]; /* A simple one command cache, in addition to the readline history */
char device_name [80]; /* The location of the filesystem */
FILE *device_handle=NULL; /* This is passed to the fopen / fread ... commands */
long device_offset; /* The current position in the filesystem */
/* Note that we have a 2 GB limitation */
int mounted=0; /* This is set when we find that the filesystem is mounted */
struct struct_commands general_commands,ext2_commands; /* Used to define the general and ext2 commands */
struct struct_descriptor *first_type,*last_type,*current_type; /* Used to access the double linked list */
struct struct_type_data type_data; /* The current data is sometimes stored here */
struct struct_file_system_info file_system_info; /* Essential information on the filesystem */
struct struct_file_info file_info,first_file_info; /* Used by file_com.c to access files */
struct struct_group_info group_info; /* Used by group_com.c */
struct struct_super_info super_info; /* Used by super_com.c */
struct struct_remember_lifo remember_lifo; /* A circular memory of objects */
struct struct_block_bitmap_info block_bitmap_info; /* Used by blockbitmap_com.c */
struct struct_inode_bitmap_info inode_bitmap_info; /* Used by inodebitmap_com.c */
int redraw_request=0; /* Is set by a signal handler to handle terminal */
/* screen size change. */
/*
* We just call the parser to get commands from the user. We quit when
* parser returns.
*/
int main (int argc, char **argv)
{
int write_priv = 0;
int c;
char *buf;
if (!init ())
return (1);
while ((c = getopt (argc, argv, "w")) != EOF) {
switch (c) {
case 'w':
write_priv++;
break;
}
}
if (optind < argc) {
buf = malloc(strlen(argv[optind]) + 32);
if (!buf) {
fprintf(stderr, "Couldn't allocate filename buffer\n");
exit(1);
}
strcpy(buf, "set_device ");
strcat(buf, argv[optind]);
set_device(buf);
free(buf);
if (write_priv) {
wprintw (command_win,"\n");
enable_write("enable_write");
}
}
parser (); /* Get and parse user commands */
prepare_to_close(); /* Do some cleanup */
printf("Quitting ...\n");
return(0);
}
/*
* Read a character from the command window
*/
int command_read_key()
{
int key = 0;
while (!key) {
if (redraw_request) {
redraw_all();
redraw_request=0;
}
key = wgetch(command_win);
switch (key) {
case 0x1A:
key = 0;
kill(getpid(), SIGTSTP);
break;
case KEY_NPAGE:
pgdn("");
refresh_command_win ();
break;
case KEY_PPAGE:
pgup("");
refresh_command_win ();
break;
case ERR:
key = 0;
break;
case KEY_BACKSPACE:
key = '\b';
}
if ((key < 32 && key != '\b' && key != '\n') ||
(key > 127))
key = 0;
}
return key;
}
#ifdef HAVE_READLINE
int rl_getc_replacement(FILE *f)
{
int key = command_read_key();
if (key == '\b') {
if (rl_point > 0)
wprintw(command_win, "\b \b");
} else
wprintw(command_win, "%c", key);
return key;
}
/*
* This function asks the user for a command and calls the dispatcher
* function, dispatch, to analyze it. We use the readline library
* function readline to read the command, hence all the usual readline
* keys are available. The new command is saved both in the
* readline's history and in our tiny one-command cache, so that only
* the enter key is needed to retype it.
*/
void parser (void)
{
char *ptr,command_line [80];
int quit=0;
#if 0
noecho();
cbreak();
keypad(command_win, 1);
wtimeout(command_win, 100);
rl_getc_function = rl_getc_replacement;
#endif
while (!quit) {
/* Terminal screen size has changed */
if (redraw_request) {
redraw_all();
redraw_request=0;
}
wmove (command_win,0,0);
wclrtoeol (command_win);
wprintw (command_win,"ext2ed > ");
refresh_command_win ();
/*
* The ncurses library optimizes cursor movement by
* keeping track of the cursor position. However, by
* using the readline library I'm breaking its
* assumptions. The double -1 arguments tell ncurses
* to disable cursor movement optimization this
* time.
*/
mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);
/* echo (); */
ptr=readline ("ext2ed > ");
/* noecho (); */
/*
* Readline allocated the buffer - Copy the string
* and free the allocated buffer
* XXX WHY???
*/
strcpy (command_line,ptr);
free (ptr);
if (*command_line != 0)
add_history (command_line);
/* If only enter was pressed, recall the last command */
if (*command_line==0)
strcpy (command_line,last_command_line);
/* Emulate readline's actions for ncurses */
mvcur (-1,-1,LINES-COMMAND_WIN_LINES,0);
werase (command_win);
wprintw (command_win,"ext2ed > ");
wprintw (command_win,command_line);
wprintw (command_win,"\n");
refresh_command_win ();
/* Save this command in our tiny cache */
strcpy (last_command_line,command_line);
/* And call dispatch to do the actual job */
quit=dispatch (command_line);
}
}
#else
void read_line(char * foo) {
char * chptr = foo;
int ch;
int done = 0;
while (!done && (ch = command_read_key())) {
switch (ch) {
case '\n':
done = 1;
break;
case '\b':
if (chptr > foo) {
wprintw(command_win, "\b \b");
chptr--;
}
break;
default:
if (ch > 256)
break;
if (ch == '\n') break;
*chptr++ = ch;
wprintw(command_win, "%c", ch);
break;
}
}
*chptr = '\0';
}
void parser (void)
{
char command_line [80];
int quit=0;
noecho();
cbreak();
wtimeout(command_win, 100);
keypad(command_win, 1);
while (!quit) {
/* Terminal screen size has changed */
if (redraw_request) {
redraw_all();
redraw_request=0;
}
wmove (command_win,0,0);wclrtoeol (command_win);
wmove(command_win, 0, 0);
wprintw(command_win, "ext2ed > ");
read_line(command_line);
/* If only enter was pressed, recall the last command */
if (*command_line==0)
strcpy (command_line,last_command_line);
mvcur (-1,-1,LINES-COMMAND_WIN_LINES + 1,0);
strcpy (last_command_line,command_line); /* Save this command in our tiny cache */
/* And call dispatch to do the actual job */
quit=dispatch (command_line);
}
}
#endif
/*
* This is a very important function. Its task is to receive a command
* name and link it to a C function. There are three types of commands:
*
* 1. General commands - Always available and accessed through
* general_commands.
* 2. Ext2 specific commands - Available when editing an ext2
* filesystem, accessed through ext2_commands.
* 3. Type specific commands - Those are changing according to the
* current type. The global variable current_type points to the
* current object definition (of type struct_descriptor). In it, the
* struct_commands entry contains the type specific commands links.
*
* Overriding is an important feature - Much like in C++ : The same
* command name can dispatch to different functions. The overriding
* priority is 3,2,1; That is - A type specific command will always
* override a general command. This is used through the program to
* allow fine tuned operation.
*
* When an handling function is found, it is called along with the
* command line that was passed to us. The handling function is then
* free to interpret the arguments in its own style.
*/
int dispatch (char *command_line)
{
int i,found=0;
char command [80];
parse_word (command_line,command);
if (strcasecmp (command,"quit")==0) return (1);
/* 1. Search for type specific commands FIRST - Allows
overriding of a general command */
if (current_type != NULL)
for (i=0;
i<=current_type->type_commands.last_command && !found;
i++) {
if (strcasecmp (command,current_type->type_commands.names [i])==0) {
(*current_type->type_commands.callback [i]) (command_line);
found=1;
}
}
/* 2. Now search for ext2 filesystem general commands */
if (!found)
for (i=0;i<=ext2_commands.last_command && !found;i++) {
if (strcasecmp (command,ext2_commands.names [i])==0) {
(*ext2_commands.callback [i]) (command_line);
found=1;
}
}
/* 3. If not found, search the general commands */
if (!found)
for (i=0;i<=general_commands.last_command && !found;i++) {
if (strcasecmp (command,general_commands.names [i])==0) {
(*general_commands.callback [i]) (command_line);
found=1;
}
}
/* 4. If not found, issue an error message and return */
if (!found) {
wprintw (command_win,"Error: Unknown command\n");
refresh_command_win ();
}
return (0);
}
/*
*
* This function copies the next word in source to the variable dest,
* ignoring whitespaces. It returns a pointer to the next word in
* source. It is used to split the command line into command and arguments.
*/
char *parse_word (char *source,char *dest)
{
char ch,*source_ptr,*target_ptr;
if (*source==0) {
*dest=0;
return (source);
};
source_ptr=source;target_ptr=dest;
do {
ch=*source_ptr++;
} while (! (ch>' ' && ch<='z') && ch!=0);
while (ch>' ' && ch<='z') {
*target_ptr++=ch;
ch=*source_ptr++;
}
*target_ptr=0;
source_ptr--;
do {
ch=*source_ptr++;
} while (! (ch>' ' && ch<='z') && ch!=0);
return (--source_ptr);
}
/*
* text is the partial command entered by the user; We assume that it
* is a part of a command - I didn't write code for smarter completion.
*
* The state variable is an index which tells us how many possible
* completions we already returned to readline.
*
* We return only one possible completion or (char *) NULL if there
* are no more completions. This function will be called by readline
* over and over until we tell it to stop.
*
* While scanning for possible completions, we use the same priority
* definition which was used in dispatch.
*/
#if HAVE_READLINE
char *complete_command (char *text,int state)
{
int state_index=-1;
int i,len;
len=strlen (text);
/* Is the command type specific ? */
if (current_type != NULL)
for (i=0;i<=current_type->type_commands.last_command;i++) {
if (strncmp (current_type->type_commands.names [i],text,len)==0) {
state_index++;
if (state==state_index) {
return (dupstr (current_type->type_commands.names [i]));
}
}
}
/* No, perhaps ext2 specific command then ? */
for (i=0;i<=ext2_commands.last_command;i++) {
if (strncmp (ext2_commands.names [i],text,len)==0) {
state_index++;
if (state==state_index)
return (dupstr (ext2_commands.names [i]));
}
}
/* Check for a general command */
for (i=0;i<=general_commands.last_command;i++) {
if (strncmp (general_commands.names [i],text,len)==0) {
state_index++;
if (state==state_index)
return (dupstr (general_commands.names [i]));
}
}
/* quit is handled differently */
if (strncmp ("quit",text,len)==0) {
state_index++;
if (state==state_index)
return (dupstr ("quit"));
}
/* No more completions */
return ((char *) NULL);
}
#endif
/*
* Nothing special - Just allocates enough space and copy the string.
*/
char *dupstr (char *src)
{
char *ptr;
ptr=(char *) malloc (strlen (src)+1);
if (!ptr)
return NULL;
strcpy (ptr,src);
return (ptr);
}
#ifdef DEBUG
/*
* This function reports an internal error. It is almost not used. One
* place in which I do check for internal errors is disk.c.
*
* We just report the error, and try to continue ...
*/
void internal_error (char *description,char *source_name,char *function_name)
{
wprintw (command_win,"Internal error - Found by source: %s.c , function: %s\n",source_name,function_name);
wprintw (command_win,"\t%s\n",description);
wprintw (command_win,"Press enter to (hopefully) continue\n");
refresh_command_win ();getch ();werase (command_win);
}
#endif
|