3 /usr/src/ext2ed/general_com.c
5 A part of the extended file system 2 disk editor.
11 First written on: April 9 1995
13 Copyright (C) 1995 Gadi Oxman
22 #include "../version.h"
24 void help (char *command_line)
28 char argument [80],*ptr;
30 werase (show_pad);wmove (show_pad,0,0);
32 ptr=parse_word (command_line,argument);
35 ptr=parse_word (ptr,argument);
37 detailed_help (argument);
42 if (current_type!=NULL) {
44 wprintw (show_pad,"Type %s specific commands:\n",current_type->name);max_line++;
46 if (current_type->type_commands.last_command==-1) {
47 wprintw (show_pad,"\nnone\n");max_line+=2;
50 for (i=0;i<=current_type->type_commands.last_command;i++) {
52 wprintw (show_pad,"\n");max_line++;
54 wprintw (show_pad,"%-13s",current_type->type_commands.names [i]);
56 wprintw (show_pad,"; ");
59 wprintw (show_pad,"\n\n");max_line+=2;
62 if (ext2_commands.last_command != -1) {
63 wprintw (show_pad,"ext2 filesystem general commands: \n");max_line++;
64 for (i=0;i<=ext2_commands.last_command;i++) {
66 wprintw (show_pad,"\n");max_line++;
68 wprintw (show_pad,"%-13s",ext2_commands.names [i]);
70 wprintw (show_pad,"; ");
73 wprintw (show_pad,"\n\n");max_line+=2;
76 wprintw (show_pad,"General commands: \n");
78 for (i=0;i<=general_commands.last_command;i++) {
80 wprintw (show_pad,"\n");max_line++;
82 wprintw (show_pad,"%-13s",general_commands.names [i]);
84 wprintw (show_pad,"; ");
87 wprintw (show_pad,"\n\n");max_line+=2;
89 wprintw (show_pad,"EXT2ED ver %s (%s)\n",E2FSPROGS_VERSION, E2FSPROGS_DATE);
90 wprintw (show_pad,"Copyright (C) 1995 Gadi Oxman\n");
91 wprintw (show_pad,"Reviewed 2001 Christian Bac\n");
92 wprintw (show_pad,"Modified and enchanced by Theodore Ts'o, 2002\n");
93 wprintw (show_pad,"EXT2ED is hereby placed under the terms of the GNU General Public License.\n\n");
94 wprintw (show_pad,"EXT2ED was programmed as a student project in the software laboratory\n");
95 wprintw (show_pad,"of the faculty of electrical engineering in the\n");
96 wprintw (show_pad,"Technion - Israel Institute of Technology\n");
97 wprintw (show_pad,"with the guide of Avner Lottem and Dr. Ilana David.\n");
101 show_pad_info.line=0;show_pad_info.max_line=max_line;
103 werase (show_win);wmove (show_win,0,0);
104 wprintw (show_win,"EXT2ED help");
110 void detailed_help (char *text)
115 if (current_type != NULL)
116 for (i=0;i<=current_type->type_commands.last_command;i++) {
117 if (strcmp (current_type->type_commands.names [i],text)==0) {
118 wprintw (show_pad,"%s - %s\n",text,current_type->type_commands.descriptions [i]);
119 refresh_show_pad ();return;
123 for (i=0;i<=ext2_commands.last_command;i++) {
124 if (strcmp (ext2_commands.names [i],text)==0) {
125 wprintw (show_pad,"%s - %s\n",text,ext2_commands.descriptions [i]);
126 refresh_show_pad ();return;
130 for (i=0;i<=general_commands.last_command;i++) {
131 if (strcmp (general_commands.names [i],text)==0) {
132 wprintw (show_pad,"%s - %s\n",text,general_commands.descriptions [i]);
133 refresh_show_pad ();return;
137 if (strcmp ("quit",text)==0) {
138 wprintw (show_pad,"quit - Exists EXT2ED");
139 refresh_show_pad ();return;
142 wprintw (show_pad,"Error - Command %s not aviable now\n",text);
143 refresh_show_pad ();return;
148 void set_device (char *command_line)
151 char *ptr,new_device [80];
153 ptr=parse_word (command_line,new_device);
155 wprintw (command_win,"Error - Device name not specified\n");
156 refresh_command_win ();return;
158 parse_word (ptr,new_device);
159 check_mounted (new_device);
160 if (mounted && !AllowMountedRead) {
161 wprintw (command_win,"Error - Filesystem is mounted, aborting\n");
162 wprintw (command_win,"You may wish to use the AllowMountedRead on configuration option\n");
163 refresh_command_win ();return;
166 if (mounted && AllowMountedRead) {
167 wprintw (command_win,"Warning - Filesystem is mounted. Displayed data may be unreliable.\n");
168 refresh_command_win ();
171 if (device_handle!=NULL)
172 fclose (device_handle);
174 if ( (device_handle=fopen (new_device,"rb"))==NULL) {
175 wprintw (command_win,"Error - Can not open device %s\n",new_device);refresh_command_win ();
179 strcpy (device_name,new_device);
180 write_access=0; /* Write access disabled */
181 current_type=NULL; /* There is no type now */
182 remember_lifo.entries_count=0; /* Empty Object memory */
183 free_user_commands (&ext2_commands); /* Free filesystem specific objects */
184 free_struct_descriptors ();
185 if (!set_file_system_info ()) { /* Error while getting info --> abort */
186 free_user_commands (&ext2_commands);
187 free_struct_descriptors ();
188 fclose (device_handle);
189 device_handle=NULL; /* Notice that our device is still not set up */
193 if (*AlternateDescriptors) /* Check if user defined objects exist */
194 set_struct_descriptors (AlternateDescriptors);
195 dispatch ("setoffset 0");
196 dispatch ("help"); /* Show help screen */
197 wprintw (command_win,"Device changed to %s",device_name);refresh_command_win ();
201 void set_offset (char *command_line)
206 char *ptr,new_offset_buffer [80];
208 if (device_handle==NULL) {
209 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
213 ptr=parse_word (command_line,new_offset_buffer);
216 wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();
220 ptr=parse_word (ptr,new_offset_buffer);
222 if (strcmp (new_offset_buffer,"block")==0) {
223 mult=file_system_info.block_size;
224 ptr=parse_word (ptr,new_offset_buffer);
227 if (strcmp (new_offset_buffer,"type")==0) {
228 if (current_type==NULL) {
229 wprintw (command_win,"Error - No type set\n");refresh_command_win ();
233 mult=current_type->length;
234 ptr=parse_word (ptr,new_offset_buffer);
237 if (*new_offset_buffer==0) {
238 wprintw (command_win,"Error - No offset specified\n");refresh_command_win ();
242 if (new_offset_buffer [0]=='+') {
243 if (device_offset==-1) {
244 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
247 new_offset=device_offset+atol (new_offset_buffer+1)*mult;
250 else if (new_offset_buffer [0]=='-') {
251 if (device_offset==-1) {
252 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
255 new_offset=device_offset-atol (new_offset_buffer+1)*mult;
256 if (new_offset<0) new_offset=0;
260 new_offset=atol (new_offset_buffer)*mult;
262 if ( (fseek (device_handle,new_offset,SEEK_SET))==-1) {
263 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",new_offset,device_name);
264 refresh_command_win ();
267 device_offset=new_offset;
268 wprintw (command_win,"Device offset changed to %ld\n",device_offset);refresh_command_win ();
270 type_data.offset_in_block=0;
273 void set_int(short len, void *ptr, char *name, char *value)
281 v = strtol(value, &tmp, 0);
283 wprintw( command_win, "Bad value - %s\n", value);
288 char_ptr = (char *) ptr;
292 short_ptr = (short *) ptr;
296 long_ptr = (long *) ptr;
300 wprintw (command_win,
301 "set_int: unsupported length: %d\n", len);
304 wprintw (command_win, "Variable %s set to %s\n",
308 void set_uint(short len, void *ptr, char *name, char *value)
310 unsigned char *char_ptr;
311 unsigned short *short_ptr;
312 unsigned long *long_ptr;
316 v = strtoul(value, &tmp, 0);
318 wprintw( command_win, "Bad value - %s\n", value);
323 char_ptr = (unsigned char *) ptr;
327 short_ptr = (unsigned short *) ptr;
331 long_ptr = (unsigned long *) ptr;
335 wprintw (command_win,
336 "set_uint: unsupported length: %d\n", len);
339 wprintw (command_win, "Variable %s set to %s\n",
343 void set_char(short len, void *ptr, char *name, char *value)
345 if (strlen(value)+1 > len) {
346 wprintw( command_win, "Value %s too big for field\n",
351 strcpy((char *) ptr, value);
352 wprintw (command_win, "Variable %s set to %s\n",
357 void set (char *command_line)
360 unsigned short *int_ptr;
361 unsigned char *char_ptr;
362 unsigned long *long_ptr,offset=0;
364 char *ptr,buffer [80],variable [80],value [80];
366 if (device_handle==NULL) {
367 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
371 if (current_type==NULL) {
372 hex_set (command_line);
376 ptr=parse_word (command_line,buffer);
377 if (ptr==NULL || *ptr==0) {
378 wprintw (command_win,"Error - Missing arguments\n");refresh_command_win ();
381 parse_word (ptr,buffer);
382 ptr=strchr (buffer,'=');
384 wprintw (command_win,"Error - Bad syntax\n");refresh_command_win ();return;
386 strncpy (variable,buffer,ptr-buffer);variable [ptr-buffer]=0;
387 strcpy (value,++ptr);
389 if (current_type==NULL) {
390 wprintw (command_win,"Sorry, not yet supported\n");refresh_command_win ();return;
393 for (i=0;i<current_type->fields_num && !found;i++) {
394 if (strcmp (current_type->field_names [i],variable)==0) {
396 ptr=type_data.u.buffer+offset;
397 len = current_type->field_lengths [i];
398 switch (current_type->field_types [i]) {
400 set_int(len, ptr, variable, value);
402 case FIELD_TYPE_UINT:
403 set_uint(len, ptr, variable, value);
405 case FIELD_TYPE_CHAR:
406 set_char(len, ptr, variable, value);
409 wprintw (command_win,
410 "set: unhandled type %d\n",
411 current_type->field_types [i]);
414 refresh_command_win ();
416 offset+=current_type->field_lengths [i];
421 wprintw (command_win,"Error - Variable %s not found\n",variable);
422 refresh_command_win ();
426 void hex_set (char *command_line)
430 char *ptr,buffer [80],*ch_ptr;
433 ptr=parse_word (command_line,buffer);
435 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
438 ptr=parse_word (ptr,buffer);
440 if (strcasecmp (buffer,"text")==0) {
445 else if (strcasecmp (buffer,"hex")==0) {
447 ptr=parse_word (ptr,buffer);
451 wprintw (command_win,"Error - Data not specified\n");refresh_command_win ();return;
456 tmp=(unsigned char) strtol (buffer,NULL,16);
457 type_data.u.buffer [type_data.offset_in_block]=tmp;
458 type_data.offset_in_block++;
459 ptr=parse_word (ptr,buffer);
460 if (type_data.offset_in_block==file_system_info.block_size) {
462 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
463 refresh_command_win ();
465 type_data.offset_in_block--;
473 tmp=(unsigned char) *ch_ptr++;
474 type_data.u.buffer [type_data.offset_in_block]=tmp;
475 type_data.offset_in_block++;
476 if (type_data.offset_in_block==file_system_info.block_size) {
478 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
479 refresh_command_win ();
481 type_data.offset_in_block--;
486 strcpy (buffer,"show");dispatch (buffer);
491 void set_type (char *command_line)
494 struct struct_descriptor *descriptor_ptr;
495 char *ptr,buffer [80],tmp_buffer [80];
498 if (!load_type_data ())
501 ptr=parse_word (command_line,buffer);
502 parse_word (ptr,buffer);
504 if (strcmp (buffer,"none")==0 || strcmp (buffer,"hex")==0) {
505 wprintw (command_win,"Data will be shown as hex dump\n");refresh_command_win ();
507 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
511 descriptor_ptr=first_type;
512 while (descriptor_ptr!=NULL && !found) {
513 if (strcmp (descriptor_ptr->name,buffer)==0)
516 descriptor_ptr=descriptor_ptr->next;
519 wprintw (command_win,"Structure type set to %s\n",buffer);refresh_command_win ();
520 current_type=descriptor_ptr;
521 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
524 wprintw (command_win,"Error - %s is not a valid type\n",buffer);refresh_command_win ();
528 void show_int(short len, void *ptr)
535 temp = *((char *) ptr);
536 format = "%3d (0x%02x)\n";
539 temp = *((short *) ptr);
540 format = "%d (0x%x)\n";
543 temp = *((long *) ptr);
547 wprintw (show_pad, "unimplemented\n");
550 wprintw(show_pad, format, temp, temp);
553 void show_uint(short len, void *ptr)
560 temp = *((unsigned char *) ptr);
562 format = "%3u (0x%02x)\n";
565 temp = *((unsigned short *) ptr);
566 temp = temp & 0xFFFF;
567 format = "%u (0x%x)\n";
570 temp = (unsigned long) *((unsigned long *) ptr);
574 wprintw (show_pad, "unimplemented\n");
577 wprintw(show_pad, format, temp, temp);
580 void show_char(short len, void *ptr)
582 unsigned char *cp = (unsigned char *) ptr;
586 wprintw(show_pad, "\"");
588 for (i=0; i < len; i++) {
591 for (j=i+1; j < len; j++)
598 wprintw(show_pad, "M-");
601 if ((ch < 32) || (ch == 0x7f)) {
602 wprintw(show_pad, "^");
603 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
605 wprintw(show_pad, "%c", ch);
608 wprintw(show_pad, "\"\n");
613 void show (char *command_line)
616 unsigned int i,l,len,temp_int;
617 unsigned long offset=0,temp_long;
618 unsigned char temp_char,*ch_ptr;
621 if (device_handle==NULL)
624 show_pad_info.line=0;
626 if (current_type==NULL) {
627 wmove (show_pad,0,0);
628 ch_ptr=type_data.u.buffer;
629 for (l=0;l<file_system_info.block_size/16;l++) {
630 wprintw (show_pad,"%08ld : ",offset);
632 if (type_data.offset_in_block==offset+i)
633 wattrset (show_pad,A_REVERSE);
635 if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
636 wprintw (show_pad,"%c",ch_ptr [i]);
638 wprintw (show_pad,".");
639 if (type_data.offset_in_block==offset+i)
640 wattrset (show_pad,A_NORMAL);
642 wprintw (show_pad," ");
644 if (type_data.offset_in_block==offset+i)
645 wattrset (show_pad,A_REVERSE);
647 wprintw (show_pad,"%02x",ch_ptr [i]);
649 if (type_data.offset_in_block==offset+i) {
650 wattrset (show_pad,A_NORMAL);
651 show_pad_info.line=l-l % show_pad_info.display_lines;
654 wprintw (show_pad," ");
656 wprintw (show_pad,"\n");
660 show_pad_info.max_line=l-1;show_pad_info.max_col=COLS-1;
661 refresh_show_pad ();show_info ();
664 wmove (show_pad,0,0);l=0;
665 for (i=0;i<current_type->fields_num;i++) {
666 wprintw (show_pad,"%-20s = ",current_type->field_names [i]);
667 ptr=type_data.u.buffer+offset;
668 len = current_type->field_lengths[i];
669 switch (current_type->field_types[i]) {
673 case FIELD_TYPE_UINT:
676 case FIELD_TYPE_CHAR:
680 wprintw (show_pad, "unimplemented\n");
686 current_type->length=offset;
687 show_pad_info.max_line=l-1;
688 refresh_show_pad ();show_info ();
692 void next (char *command_line)
696 char *ptr,buffer [80];
698 ptr=parse_word (command_line,buffer);
701 ptr=parse_word (ptr,buffer);
702 offset*=atol (buffer);
705 if (current_type!=NULL) {
706 sprintf (buffer,"setoffset type +%ld",offset);
711 if (type_data.offset_in_block+offset < file_system_info.block_size) {
712 type_data.offset_in_block+=offset;
713 sprintf (buffer,"show");dispatch (buffer);
717 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
721 void prev (char *command_line)
725 char *ptr,buffer [80];
727 ptr=parse_word (command_line,buffer);
730 ptr=parse_word (ptr,buffer);
731 offset*=atol (buffer);
734 if (current_type!=NULL) {
735 sprintf (buffer,"setoffset type -%ld",offset);
740 if (type_data.offset_in_block-offset >= 0) {
741 type_data.offset_in_block-=offset;
742 sprintf (buffer,"show");dispatch (buffer);
746 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
750 void pgdn (char *commnad_line)
753 show_pad_info.line+=show_pad_info.display_lines;
754 refresh_show_pad ();refresh_show_win ();
757 void pgup (char *command_line)
760 show_pad_info.line-=show_pad_info.display_lines;
761 refresh_show_pad ();refresh_show_win ();
764 void redraw (char *command_line)
771 void remember (char *command_line)
775 char *ptr,buffer [80];
777 if (device_handle==NULL) {
778 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
782 ptr=parse_word (command_line,buffer);
785 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
789 ptr=parse_word (ptr,buffer);
791 entry_num=remember_lifo.entries_count++;
792 if (entry_num>REMEMBER_COUNT-1) {
794 remember_lifo.entries_count--;
797 remember_lifo.offset [entry_num]=device_offset;
798 remember_lifo.type [entry_num]=current_type;
799 strcpy (remember_lifo.name [entry_num],buffer);
801 if (current_type!=NULL)
802 wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",current_type->name,device_offset,buffer);
804 wprintw (command_win,"Offset %ld remembered as %s\n",device_offset,buffer);
806 refresh_command_win ();
809 void recall (char *command_line)
812 char *ptr,buffer [80];
815 if (device_handle==NULL) {
816 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
820 ptr=parse_word (command_line,buffer);
823 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
827 ptr=parse_word (ptr,buffer);
830 for (entry_num=remember_lifo.entries_count-1;entry_num>=0;entry_num--) {
831 if (strcmp (remember_lifo.name [entry_num],buffer)==0)
836 wprintw (command_win,"Error - Can not recall %s\n",buffer);refresh_command_win ();
840 sprintf (buffer,"setoffset %ld",remember_lifo.offset [entry_num]);dispatch (buffer);
841 if (remember_lifo.type [entry_num] != NULL) {
842 sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);
846 sprintf (buffer,"settype none");dispatch (buffer);
849 wprintw (command_win,"Object %s in Offset %ld recalled\n",current_type->name,device_offset);
850 refresh_command_win ();
853 void enable_write (char *command_line)
858 if (device_handle==NULL) {
859 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
864 wprintw (command_win,"Sorry, write access is not allowed\n");
869 wprintw (command_win,"Error - Filesystem is mounted\n");
873 if ( (fp=fopen (device_name,"r+b"))==NULL) {
874 wprintw (command_win,"Error - Can not open device %s for reading and writing\n",device_name);refresh_command_win ();
877 fclose (device_handle);
878 device_handle=fp;write_access=1;
879 wprintw (command_win,"Write access enabled - Be careful\n");refresh_command_win ();
882 void disable_write (char *command_line)
887 if (device_handle==NULL) {
888 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
892 if ( (fp=fopen (device_name,"rb"))==NULL) {
893 wprintw (command_win,"Error - Can not open device %s\n",device_name);refresh_command_win ();
897 fclose (device_handle);
898 device_handle=fp;write_access=0;
899 wprintw (command_win,"Write access disabled\n");refresh_command_win ();
902 void write_data (char *command_line)