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
23 #include "../version.h"
25 void help (char *command_line)
29 char argument [80],*ptr;
31 werase (show_pad);wmove (show_pad,0,0);
33 ptr=parse_word (command_line,argument);
36 ptr=parse_word (ptr,argument);
38 detailed_help (argument);
43 if (current_type!=NULL) {
45 wprintw (show_pad,"Type %s specific commands:\n",current_type->name);max_line++;
47 if (current_type->type_commands.last_command==-1) {
48 wprintw (show_pad,"\nnone\n");max_line+=2;
51 for (i=0;i<=current_type->type_commands.last_command;i++) {
53 wprintw (show_pad,"\n");max_line++;
55 wprintw (show_pad,"%-13s",current_type->type_commands.names [i]);
57 wprintw (show_pad,"; ");
60 wprintw (show_pad,"\n\n");max_line+=2;
63 if (ext2_commands.last_command != -1) {
64 wprintw (show_pad,"ext2 filesystem general commands: \n");max_line++;
65 for (i=0;i<=ext2_commands.last_command;i++) {
67 wprintw (show_pad,"\n");max_line++;
69 wprintw (show_pad,"%-13s",ext2_commands.names [i]);
71 wprintw (show_pad,"; ");
74 wprintw (show_pad,"\n\n");max_line+=2;
77 wprintw (show_pad,"General commands: \n");
79 for (i=0;i<=general_commands.last_command;i++) {
81 wprintw (show_pad,"\n");max_line++;
83 wprintw (show_pad,"%-13s",general_commands.names [i]);
85 wprintw (show_pad,"; ");
88 wprintw (show_pad,"\n\n");max_line+=2;
90 wprintw (show_pad,"EXT2ED ver %s (%s)\n",E2FSPROGS_VERSION, E2FSPROGS_DATE);
91 wprintw (show_pad,"Copyright (C) 1995 Gadi Oxman\n");
92 wprintw (show_pad,"Reviewed 2001 Christian Bac\n");
93 wprintw (show_pad,"Modified and enchanced by Theodore Ts'o, 2002\n");
94 wprintw (show_pad,"EXT2ED is hereby placed under the terms of the GNU General Public License.\n\n");
95 wprintw (show_pad,"EXT2ED was programmed as a student project in the software laboratory\n");
96 wprintw (show_pad,"of the faculty of electrical engineering in the\n");
97 wprintw (show_pad,"Technion - Israel Institute of Technology\n");
98 wprintw (show_pad,"with the guide of Avner Lottem and Dr. Ilana David.\n");
102 show_pad_info.line=0;show_pad_info.max_line=max_line;
104 werase (show_win);wmove (show_win,0,0);
105 wprintw (show_win,"EXT2ED help");
111 void detailed_help (char *text)
116 if (current_type != NULL)
117 for (i=0;i<=current_type->type_commands.last_command;i++) {
118 if (strcmp (current_type->type_commands.names [i],text)==0) {
119 wprintw (show_pad,"%s - %s\n",text,current_type->type_commands.descriptions [i]);
120 refresh_show_pad ();return;
124 for (i=0;i<=ext2_commands.last_command;i++) {
125 if (strcmp (ext2_commands.names [i],text)==0) {
126 wprintw (show_pad,"%s - %s\n",text,ext2_commands.descriptions [i]);
127 refresh_show_pad ();return;
131 for (i=0;i<=general_commands.last_command;i++) {
132 if (strcmp (general_commands.names [i],text)==0) {
133 wprintw (show_pad,"%s - %s\n",text,general_commands.descriptions [i]);
134 refresh_show_pad ();return;
138 if (strcmp ("quit",text)==0) {
139 wprintw (show_pad,"quit - Exists EXT2ED");
140 refresh_show_pad ();return;
143 wprintw (show_pad,"Error - Command %s not aviable now\n",text);
144 refresh_show_pad ();return;
149 void set_device (char *command_line)
152 char *ptr,new_device [80];
154 ptr=parse_word (command_line,new_device);
156 wprintw (command_win,"Error - Device name not specified\n");
157 refresh_command_win ();return;
159 parse_word (ptr,new_device);
160 check_mounted (new_device);
161 if (mounted && !AllowMountedRead) {
162 wprintw (command_win,"Error - Filesystem is mounted, aborting\n");
163 wprintw (command_win,"You may wish to use the AllowMountedRead on configuration option\n");
164 refresh_command_win ();return;
167 if (mounted && AllowMountedRead) {
168 wprintw (command_win,"Warning - Filesystem is mounted. Displayed data may be unreliable.\n");
169 refresh_command_win ();
172 if (device_handle!=NULL)
173 fclose (device_handle);
175 if ( (device_handle=fopen (new_device,"rb"))==NULL) {
176 wprintw (command_win,"Error - Can not open device %s\n",new_device);refresh_command_win ();
180 strcpy (device_name,new_device);
181 write_access=0; /* Write access disabled */
182 current_type=NULL; /* There is no type now */
183 remember_lifo.entries_count=0; /* Empty Object memory */
184 free_user_commands (&ext2_commands); /* Free filesystem specific objects */
185 free_struct_descriptors ();
186 if (!set_file_system_info ()) { /* Error while getting info --> abort */
187 free_user_commands (&ext2_commands);
188 free_struct_descriptors ();
189 fclose (device_handle);
190 device_handle=NULL; /* Notice that our device is still not set up */
194 if (*AlternateDescriptors) /* Check if user defined objects exist */
195 set_struct_descriptors (AlternateDescriptors);
196 dispatch ("setoffset 0");
197 dispatch ("help"); /* Show help screen */
198 wprintw (command_win,"Device changed to %s",device_name);refresh_command_win ();
202 void set_offset (char *command_line)
207 char *ptr,new_offset_buffer [80];
209 if (device_handle==NULL) {
210 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
214 ptr=parse_word (command_line,new_offset_buffer);
217 wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();
221 ptr=parse_word (ptr,new_offset_buffer);
223 if (strcmp (new_offset_buffer,"block")==0) {
224 mult=file_system_info.block_size;
225 ptr=parse_word (ptr,new_offset_buffer);
228 if (strcmp (new_offset_buffer,"type")==0) {
229 if (current_type==NULL) {
230 wprintw (command_win,"Error - No type set\n");refresh_command_win ();
234 mult=current_type->length;
235 ptr=parse_word (ptr,new_offset_buffer);
238 if (*new_offset_buffer==0) {
239 wprintw (command_win,"Error - No offset specified\n");refresh_command_win ();
243 if (new_offset_buffer [0]=='+') {
244 if (device_offset==-1) {
245 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
248 new_offset=device_offset+atol (new_offset_buffer+1)*mult;
251 else if (new_offset_buffer [0]=='-') {
252 if (device_offset==-1) {
253 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
256 new_offset=device_offset-atol (new_offset_buffer+1)*mult;
257 if (new_offset<0) new_offset=0;
261 new_offset=atol (new_offset_buffer)*mult;
263 if ( (fseek (device_handle,new_offset,SEEK_SET))==-1) {
264 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",new_offset,device_name);
265 refresh_command_win ();
268 device_offset=new_offset;
269 wprintw (command_win,"Device offset changed to %ld\n",device_offset);refresh_command_win ();
271 type_data.offset_in_block=0;
274 void set_int(short len, void *ptr, char *name, char *value)
282 v = strtol(value, &tmp, 0);
284 wprintw( command_win, "Bad value - %s\n", value);
289 char_ptr = (char *) ptr;
293 short_ptr = (short *) ptr;
297 long_ptr = (long *) ptr;
301 wprintw (command_win,
302 "set_int: unsupported length: %d\n", len);
305 wprintw (command_win, "Variable %s set to %s\n",
309 void set_uint(short len, void *ptr, char *name, char *value)
311 unsigned char *char_ptr;
312 unsigned short *short_ptr;
313 unsigned long *long_ptr;
317 v = strtoul(value, &tmp, 0);
319 wprintw( command_win, "Bad value - %s\n", value);
324 char_ptr = (unsigned char *) ptr;
328 short_ptr = (unsigned short *) ptr;
332 long_ptr = (unsigned long *) ptr;
336 wprintw (command_win,
337 "set_uint: unsupported length: %d\n", len);
340 wprintw (command_win, "Variable %s set to %s\n",
344 void set_char(short len, void *ptr, char *name, char *value)
346 if (strlen(value)+1 > len) {
347 wprintw( command_win, "Value %s too big for field\n",
352 strcpy((char *) ptr, value);
353 wprintw (command_win, "Variable %s set to %s\n",
358 void set (char *command_line)
361 unsigned short *int_ptr;
362 unsigned char *char_ptr;
363 unsigned long *long_ptr,offset=0;
365 char *ptr,buffer [80],variable [80],value [80];
367 if (device_handle==NULL) {
368 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
372 if (current_type==NULL) {
373 hex_set (command_line);
377 ptr=parse_word (command_line,buffer);
378 if (ptr==NULL || *ptr==0) {
379 wprintw (command_win,"Error - Missing arguments\n");refresh_command_win ();
382 parse_word (ptr,buffer);
383 ptr=strchr (buffer,'=');
385 wprintw (command_win,"Error - Bad syntax\n");refresh_command_win ();return;
387 strncpy (variable,buffer,ptr-buffer);variable [ptr-buffer]=0;
388 strcpy (value,++ptr);
390 if (current_type==NULL) {
391 wprintw (command_win,"Sorry, not yet supported\n");refresh_command_win ();return;
394 for (i=0;i<current_type->fields_num && !found;i++) {
395 if (strcmp (current_type->field_names [i],variable)==0) {
397 ptr=type_data.u.buffer+offset;
398 len = current_type->field_lengths [i];
399 switch (current_type->field_types [i]) {
401 set_int(len, ptr, variable, value);
403 case FIELD_TYPE_UINT:
404 set_uint(len, ptr, variable, value);
406 case FIELD_TYPE_CHAR:
407 set_char(len, ptr, variable, value);
410 wprintw (command_win,
411 "set: unhandled type %d\n",
412 current_type->field_types [i]);
415 refresh_command_win ();
417 offset+=current_type->field_lengths [i];
422 wprintw (command_win,"Error - Variable %s not found\n",variable);
423 refresh_command_win ();
427 void hex_set (char *command_line)
431 char *ptr,buffer [80],*ch_ptr;
434 ptr=parse_word (command_line,buffer);
436 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
439 ptr=parse_word (ptr,buffer);
441 if (strcasecmp (buffer,"text")==0) {
446 else if (strcasecmp (buffer,"hex")==0) {
448 ptr=parse_word (ptr,buffer);
452 wprintw (command_win,"Error - Data not specified\n");refresh_command_win ();return;
457 tmp=(unsigned char) strtol (buffer,NULL,16);
458 type_data.u.buffer [type_data.offset_in_block]=tmp;
459 type_data.offset_in_block++;
460 ptr=parse_word (ptr,buffer);
461 if (type_data.offset_in_block==file_system_info.block_size) {
463 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
464 refresh_command_win ();
466 type_data.offset_in_block--;
474 tmp=(unsigned char) *ch_ptr++;
475 type_data.u.buffer [type_data.offset_in_block]=tmp;
476 type_data.offset_in_block++;
477 if (type_data.offset_in_block==file_system_info.block_size) {
479 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
480 refresh_command_win ();
482 type_data.offset_in_block--;
487 strcpy (buffer,"show");dispatch (buffer);
492 void set_type (char *command_line)
495 struct struct_descriptor *descriptor_ptr;
496 char *ptr,buffer [80],tmp_buffer [80];
499 if (!load_type_data ())
502 ptr=parse_word (command_line,buffer);
503 parse_word (ptr,buffer);
505 if (strcmp (buffer,"none")==0 || strcmp (buffer,"hex")==0) {
506 wprintw (command_win,"Data will be shown as hex dump\n");refresh_command_win ();
508 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
512 descriptor_ptr=first_type;
513 while (descriptor_ptr!=NULL && !found) {
514 if (strcmp (descriptor_ptr->name,buffer)==0)
517 descriptor_ptr=descriptor_ptr->next;
520 wprintw (command_win,"Structure type set to %s\n",buffer);refresh_command_win ();
521 current_type=descriptor_ptr;
522 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
525 wprintw (command_win,"Error - %s is not a valid type\n",buffer);refresh_command_win ();
529 void show_int(short len, void *ptr)
536 temp = *((char *) ptr);
537 format = "%3d (0x%02x)\n";
540 temp = *((short *) ptr);
541 format = "%d (0x%x)\n";
544 temp = *((long *) ptr);
548 wprintw (show_pad, "unimplemented\n");
551 wprintw(show_pad, format, temp, temp);
554 void show_uint(short len, void *ptr)
561 temp = *((unsigned char *) ptr);
563 format = "%3u (0x%02x)\n";
566 temp = *((unsigned short *) ptr);
567 temp = temp & 0xFFFF;
568 format = "%u (0x%x)\n";
571 temp = (unsigned long) *((unsigned long *) ptr);
575 wprintw (show_pad, "unimplemented\n");
578 wprintw(show_pad, format, temp, temp);
581 void show_char(short len, void *ptr)
583 unsigned char *cp = (unsigned char *) ptr;
587 wprintw(show_pad, "\"");
589 for (i=0; i < len; i++) {
592 for (j=i+1; j < len; j++)
599 wprintw(show_pad, "M-");
602 if ((ch < 32) || (ch == 0x7f)) {
603 wprintw(show_pad, "^");
604 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
606 wprintw(show_pad, "%c", ch);
609 wprintw(show_pad, "\"\n");
614 void show (char *command_line)
617 unsigned int i,l,len,temp_int;
618 unsigned long offset=0,temp_long;
619 unsigned char temp_char,*ch_ptr;
622 if (device_handle==NULL)
625 show_pad_info.line=0;
627 if (current_type==NULL) {
628 wmove (show_pad,0,0);
629 ch_ptr=type_data.u.buffer;
630 for (l=0;l<file_system_info.block_size/16;l++) {
631 wprintw (show_pad,"%08ld : ",offset);
633 if (type_data.offset_in_block==offset+i)
634 wattrset (show_pad,A_REVERSE);
636 if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
637 wprintw (show_pad,"%c",ch_ptr [i]);
639 wprintw (show_pad,".");
640 if (type_data.offset_in_block==offset+i)
641 wattrset (show_pad,A_NORMAL);
643 wprintw (show_pad," ");
645 if (type_data.offset_in_block==offset+i)
646 wattrset (show_pad,A_REVERSE);
648 wprintw (show_pad,"%02x",ch_ptr [i]);
650 if (type_data.offset_in_block==offset+i) {
651 wattrset (show_pad,A_NORMAL);
652 show_pad_info.line=l-l % show_pad_info.display_lines;
655 wprintw (show_pad," ");
657 wprintw (show_pad,"\n");
661 show_pad_info.max_line=l-1;show_pad_info.max_col=COLS-1;
662 refresh_show_pad ();show_info ();
665 wmove (show_pad,0,0);l=0;
666 for (i=0;i<current_type->fields_num;i++) {
667 wprintw (show_pad,"%-20s = ",current_type->field_names [i]);
668 ptr=type_data.u.buffer+offset;
669 len = current_type->field_lengths[i];
670 switch (current_type->field_types[i]) {
674 case FIELD_TYPE_UINT:
677 case FIELD_TYPE_CHAR:
681 wprintw (show_pad, "unimplemented\n");
687 current_type->length=offset;
688 show_pad_info.max_line=l-1;
689 refresh_show_pad ();show_info ();
693 void next (char *command_line)
697 char *ptr,buffer [80];
699 ptr=parse_word (command_line,buffer);
702 ptr=parse_word (ptr,buffer);
703 offset*=atol (buffer);
706 if (current_type!=NULL) {
707 sprintf (buffer,"setoffset type +%ld",offset);
712 if (type_data.offset_in_block+offset < file_system_info.block_size) {
713 type_data.offset_in_block+=offset;
714 sprintf (buffer,"show");dispatch (buffer);
718 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
722 void prev (char *command_line)
726 char *ptr,buffer [80];
728 ptr=parse_word (command_line,buffer);
731 ptr=parse_word (ptr,buffer);
732 offset*=atol (buffer);
735 if (current_type!=NULL) {
736 sprintf (buffer,"setoffset type -%ld",offset);
741 if (type_data.offset_in_block-offset >= 0) {
742 type_data.offset_in_block-=offset;
743 sprintf (buffer,"show");dispatch (buffer);
747 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
751 void pgdn (char *commnad_line)
754 show_pad_info.line+=show_pad_info.display_lines;
755 refresh_show_pad ();refresh_show_win ();
758 void pgup (char *command_line)
761 show_pad_info.line-=show_pad_info.display_lines;
762 refresh_show_pad ();refresh_show_win ();
765 void redraw (char *command_line)
772 void remember (char *command_line)
776 char *ptr,buffer [80];
778 if (device_handle==NULL) {
779 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
783 ptr=parse_word (command_line,buffer);
786 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
790 ptr=parse_word (ptr,buffer);
792 entry_num=remember_lifo.entries_count++;
793 if (entry_num>REMEMBER_COUNT-1) {
795 remember_lifo.entries_count--;
798 remember_lifo.offset [entry_num]=device_offset;
799 remember_lifo.type [entry_num]=current_type;
800 strcpy (remember_lifo.name [entry_num],buffer);
802 if (current_type!=NULL)
803 wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",current_type->name,device_offset,buffer);
805 wprintw (command_win,"Offset %ld remembered as %s\n",device_offset,buffer);
807 refresh_command_win ();
810 void recall (char *command_line)
813 char *ptr,buffer [80];
816 if (device_handle==NULL) {
817 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
821 ptr=parse_word (command_line,buffer);
824 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
828 ptr=parse_word (ptr,buffer);
831 for (entry_num=remember_lifo.entries_count-1;entry_num>=0;entry_num--) {
832 if (strcmp (remember_lifo.name [entry_num],buffer)==0)
837 wprintw (command_win,"Error - Can not recall %s\n",buffer);refresh_command_win ();
841 sprintf (buffer,"setoffset %ld",remember_lifo.offset [entry_num]);dispatch (buffer);
842 if (remember_lifo.type [entry_num] != NULL) {
843 sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);
847 sprintf (buffer,"settype none");dispatch (buffer);
850 wprintw (command_win,"Object %s in Offset %ld recalled\n",current_type->name,device_offset);
851 refresh_command_win ();
854 void enable_write (char *command_line)
859 if (device_handle==NULL) {
860 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
865 wprintw (command_win,"Sorry, write access is not allowed\n");
870 wprintw (command_win,"Error - Filesystem is mounted\n");
874 if ( (fp=fopen (device_name,"r+b"))==NULL) {
875 wprintw (command_win,"Error - Can not open device %s for reading and writing\n",device_name);refresh_command_win ();
878 fclose (device_handle);
879 device_handle=fp;write_access=1;
880 wprintw (command_win,"Write access enabled - Be careful\n");refresh_command_win ();
883 void disable_write (char *command_line)
888 if (device_handle==NULL) {
889 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
893 if ( (fp=fopen (device_name,"rb"))==NULL) {
894 wprintw (command_win,"Error - Can not open device %s\n",device_name);refresh_command_win ();
898 fclose (device_handle);
899 device_handle=fp;write_access=0;
900 wprintw (command_win,"Write access disabled\n");refresh_command_win ();
903 void write_data (char *command_line)