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 void help (char *command_line)
27 char argument [80],*ptr;
29 werase (show_pad);wmove (show_pad,0,0);
31 ptr=parse_word (command_line,argument);
34 ptr=parse_word (ptr,argument);
36 detailed_help (argument);
41 if (current_type!=NULL) {
43 wprintw (show_pad,"Type %s specific commands:\n",current_type->name);max_line++;
45 if (current_type->type_commands.last_command==-1) {
46 wprintw (show_pad,"\nnone\n");max_line+=2;
49 for (i=0;i<=current_type->type_commands.last_command;i++) {
51 wprintw (show_pad,"\n");max_line++;
53 wprintw (show_pad,"%-13s",current_type->type_commands.names [i]);
55 wprintw (show_pad,"; ");
58 wprintw (show_pad,"\n\n");max_line+=2;
61 if (ext2_commands.last_command != -1) {
62 wprintw (show_pad,"ext2 filesystem general commands: \n");max_line++;
63 for (i=0;i<=ext2_commands.last_command;i++) {
65 wprintw (show_pad,"\n");max_line++;
67 wprintw (show_pad,"%-13s",ext2_commands.names [i]);
69 wprintw (show_pad,"; ");
72 wprintw (show_pad,"\n\n");max_line+=2;
75 wprintw (show_pad,"General commands: \n");
77 for (i=0;i<=general_commands.last_command;i++) {
79 wprintw (show_pad,"\n");max_line++;
81 wprintw (show_pad,"%-13s",general_commands.names [i]);
83 wprintw (show_pad,"; ");
86 wprintw (show_pad,"\n\n");max_line+=2;
88 wprintw (show_pad,"EXT2ED ver %d.%d (%s)\n",version_major,version_minor,revision_date);
89 wprintw (show_pad,"Copyright (C) 1995 Gadi Oxman\n");
90 wprintw (show_pad,"EXT2ED is hereby placed under the terms of the GNU General Public License.\n\n");
91 wprintw (show_pad,"EXT2ED was programmed as a student project in the software laboratory\n");
92 wprintw (show_pad,"of the faculty of electrical engineering in the\n");
93 wprintw (show_pad,"Technion - Israel Institute of Technology\n");
94 wprintw (show_pad,"with the guide of Avner Lottem and Dr. Ilana David.\n");
98 wprintw (show_pad,"\n\n");max_line+=2;
100 wprintw (show_pad,"Please feel free to mail me at (currently) %s\n",email_address);
101 wprintw (show_pad,"with any commet, suggestion, and of-course, bug report concerning EXT2ED.\n");
105 show_pad_info.line=0;show_pad_info.max_line=max_line;
107 werase (show_win);wmove (show_win,0,0);
108 wprintw (show_win,"EXT2ED help");
114 void detailed_help (char *text)
119 if (current_type != NULL)
120 for (i=0;i<=current_type->type_commands.last_command;i++) {
121 if (strcmp (current_type->type_commands.names [i],text)==0) {
122 wprintw (show_pad,"%s - %s\n",text,current_type->type_commands.descriptions [i]);
123 refresh_show_pad ();return;
127 for (i=0;i<=ext2_commands.last_command;i++) {
128 if (strcmp (ext2_commands.names [i],text)==0) {
129 wprintw (show_pad,"%s - %s\n",text,ext2_commands.descriptions [i]);
130 refresh_show_pad ();return;
134 for (i=0;i<=general_commands.last_command;i++) {
135 if (strcmp (general_commands.names [i],text)==0) {
136 wprintw (show_pad,"%s - %s\n",text,general_commands.descriptions [i]);
137 refresh_show_pad ();return;
141 if (strcmp ("quit",text)==0) {
142 wprintw (show_pad,"quit - Exists EXT2ED");
143 refresh_show_pad ();return;
146 wprintw (show_pad,"Error - Command %s not aviable now\n",text);
147 refresh_show_pad ();return;
152 void set_device (char *command_line)
155 char *ptr,new_device [80];
157 ptr=parse_word (command_line,new_device);
159 wprintw (command_win,"Error - Device name not specified\n");
160 refresh_command_win ();return;
162 parse_word (ptr,new_device);
163 check_mounted (new_device);
164 if (mounted && !AllowMountedRead) {
165 wprintw (command_win,"Error - Filesystem is mounted, aborting\n");
166 wprintw (command_win,"You may wish to use the AllowMountedRead on configuration option\n");
167 refresh_command_win ();return;
170 if (mounted && AllowMountedRead) {
171 wprintw (command_win,"Warning - Filesystem is mounted. Displayed data may be unreliable.\n");
172 refresh_command_win ();
175 if (device_handle!=NULL)
176 fclose (device_handle);
178 if ( (device_handle=fopen (new_device,"rb"))==NULL) {
179 wprintw (command_win,"Error - Can not open device %s\n",new_device);refresh_command_win ();
183 strcpy (device_name,new_device);
184 write_access=0; /* Write access disabled */
185 current_type=NULL; /* There is no type now */
186 remember_lifo.entries_count=0; /* Empty Object memory */
187 free_user_commands (&ext2_commands); /* Free filesystem specific objects */
188 free_struct_descriptors ();
189 if (!set_file_system_info ()) { /* Error while getting info --> abort */
190 free_user_commands (&ext2_commands);
191 free_struct_descriptors ();
192 fclose (device_handle);
193 device_handle=NULL; /* Notice that our device is still not set up */
197 if (*AlternateDescriptors) /* Check if user defined objects exist */
198 set_struct_descriptors (AlternateDescriptors);
199 dispatch ("setoffset 0");
200 dispatch ("help"); /* Show help screen */
201 wprintw (command_win,"Device changed to %s",device_name);refresh_command_win ();
205 void set_offset (char *command_line)
210 char *ptr,new_offset_buffer [80];
212 if (device_handle==NULL) {
213 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
217 ptr=parse_word (command_line,new_offset_buffer);
220 wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();
224 ptr=parse_word (ptr,new_offset_buffer);
226 if (strcmp (new_offset_buffer,"block")==0) {
227 mult=file_system_info.block_size;
228 ptr=parse_word (ptr,new_offset_buffer);
231 if (strcmp (new_offset_buffer,"type")==0) {
232 if (current_type==NULL) {
233 wprintw (command_win,"Error - No type set\n");refresh_command_win ();
237 mult=current_type->length;
238 ptr=parse_word (ptr,new_offset_buffer);
241 if (*new_offset_buffer==0) {
242 wprintw (command_win,"Error - No offset specified\n");refresh_command_win ();
246 if (new_offset_buffer [0]=='+') {
247 if (device_offset==-1) {
248 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
251 new_offset=device_offset+atol (new_offset_buffer+1)*mult;
254 else if (new_offset_buffer [0]=='-') {
255 if (device_offset==-1) {
256 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
259 new_offset=device_offset-atol (new_offset_buffer+1)*mult;
260 if (new_offset<0) new_offset=0;
264 new_offset=atol (new_offset_buffer)*mult;
266 if ( (fseek (device_handle,new_offset,SEEK_SET))==-1) {
267 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",new_offset,device_name);
268 refresh_command_win ();
271 device_offset=new_offset;
272 wprintw (command_win,"Device offset changed to %ld\n",device_offset);refresh_command_win ();
274 type_data.offset_in_block=0;
277 void set (char *command_line)
280 unsigned short *int_ptr;
281 unsigned char *char_ptr;
282 unsigned long *long_ptr,offset=0;
284 char *ptr,buffer [80],variable [80],value [80];
286 if (device_handle==NULL) {
287 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
291 if (current_type==NULL) {
292 hex_set (command_line);
296 ptr=parse_word (command_line,buffer);
297 if (ptr==NULL || *ptr==0) {
298 wprintw (command_win,"Error - Missing arguments\n");refresh_command_win ();
301 parse_word (ptr,buffer);
302 ptr=strchr (buffer,'=');
304 wprintw (command_win,"Error - Bad syntax\n");refresh_command_win ();return;
306 strncpy (variable,buffer,ptr-buffer);variable [ptr-buffer]=0;
307 strcpy (value,++ptr);
309 if (current_type==NULL) {
310 wprintw (command_win,"Sorry, not yet supported\n");refresh_command_win ();return;
313 for (i=0;i<current_type->fields_num && !found;i++) {
314 if (strcmp (current_type->field_names [i],variable)==0) {
316 ptr=type_data.u.buffer+offset;
317 switch (current_type->field_lengths [i]) {
319 char_ptr=(unsigned char *) ptr;
320 *char_ptr=(char) atoi (value);
321 wprintw (command_win,"Variable %s set to %u\n",variable,*char_ptr);refresh_command_win ();
324 int_ptr=(unsigned short *) ptr;
325 *int_ptr=atoi (value);
326 wprintw (command_win,"Variable %s set to %u\n",variable,*int_ptr);refresh_command_win ();
330 long_ptr=(unsigned long *) ptr;
331 *long_ptr=atol (value);
332 wprintw (command_win,"Variable %s set to %lu\n",variable,*long_ptr);refresh_command_win ();
336 offset+=current_type->field_lengths [i];
341 wprintw (command_win,"Error - Variable %s not found\n",variable);
342 refresh_command_win ();
346 void hex_set (char *command_line)
350 char *ptr,buffer [80],*ch_ptr;
353 ptr=parse_word (command_line,buffer);
355 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
358 ptr=parse_word (ptr,buffer);
360 if (strcasecmp (buffer,"text")==0) {
365 else if (strcasecmp (buffer,"hex")==0) {
367 ptr=parse_word (ptr,buffer);
371 wprintw (command_win,"Error - Data not specified\n");refresh_command_win ();return;
376 tmp=(unsigned char) strtol (buffer,NULL,16);
377 type_data.u.buffer [type_data.offset_in_block]=tmp;
378 type_data.offset_in_block++;
379 ptr=parse_word (ptr,buffer);
380 if (type_data.offset_in_block==file_system_info.block_size) {
382 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
383 refresh_command_win ();
385 type_data.offset_in_block--;
393 tmp=(unsigned char) *ch_ptr++;
394 type_data.u.buffer [type_data.offset_in_block]=tmp;
395 type_data.offset_in_block++;
396 if (type_data.offset_in_block==file_system_info.block_size) {
398 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
399 refresh_command_win ();
401 type_data.offset_in_block--;
406 strcpy (buffer,"show");dispatch (buffer);
411 void set_type (char *command_line)
414 struct struct_descriptor *descriptor_ptr;
415 char *ptr,buffer [80],tmp_buffer [80];
418 if (!load_type_data ())
421 ptr=parse_word (command_line,buffer);
422 parse_word (ptr,buffer);
424 if (strcmp (buffer,"none")==0 || strcmp (buffer,"hex")==0) {
425 wprintw (command_win,"Data will be shown as hex dump\n");refresh_command_win ();
427 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
431 descriptor_ptr=first_type;
432 while (descriptor_ptr!=NULL && !found) {
433 if (strcmp (descriptor_ptr->name,buffer)==0)
436 descriptor_ptr=descriptor_ptr->next;
439 wprintw (command_win,"Structure type set to %s\n",buffer);refresh_command_win ();
440 current_type=descriptor_ptr;
441 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
444 wprintw (command_win,"Error - %s is not a valid type\n",buffer);refresh_command_win ();
449 void show (char *command_line)
452 unsigned int i,l,temp_int;
453 unsigned long offset=0,temp_long;
454 unsigned char temp_char,*ch_ptr;
457 if (device_handle==NULL)
460 show_pad_info.line=0;
462 if (current_type==NULL) {
463 wmove (show_pad,0,0);
464 ch_ptr=type_data.u.buffer;
465 for (l=0;l<file_system_info.block_size/16;l++) {
466 wprintw (show_pad,"%08ld : ",offset);
468 if (type_data.offset_in_block==offset+i)
469 wattrset (show_pad,A_REVERSE);
471 if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
472 wprintw (show_pad,"%c",ch_ptr [i]);
474 wprintw (show_pad,".");
475 if (type_data.offset_in_block==offset+i)
476 wattrset (show_pad,A_NORMAL);
478 wprintw (show_pad," ");
480 if (type_data.offset_in_block==offset+i)
481 wattrset (show_pad,A_REVERSE);
483 wprintw (show_pad,"%02x",ch_ptr [i]);
485 if (type_data.offset_in_block==offset+i) {
486 wattrset (show_pad,A_NORMAL);
487 show_pad_info.line=l-l % show_pad_info.display_lines;
490 wprintw (show_pad," ");
492 wprintw (show_pad,"\n");
496 show_pad_info.max_line=l-1;show_pad_info.max_col=COLS-1;
497 refresh_show_pad ();show_info ();
500 wmove (show_pad,0,0);l=0;
501 for (i=0;i<current_type->fields_num;i++) {
502 wprintw (show_pad,"%-20s = ",current_type->field_names [i]);
503 ptr=type_data.u.buffer+offset;
504 switch (current_type->field_lengths [i]) {
506 temp_char=*((unsigned char *) ptr);
507 wprintw (show_pad,"%3u (0x%02x",temp_char,temp_char);
508 if (temp_char>=' ' && temp_char<='z')
509 wprintw (show_pad," , %c)\n",temp_char);
511 wprintw (show_pad,")\n");
516 temp_int=*((unsigned short *) ptr);
517 wprintw (show_pad,"%u (0x%x)\n",temp_int,temp_int);
521 temp_long=*((unsigned long *) ptr);
522 wprintw (show_pad,"%lu\n",temp_long);
526 /* offset+=current_type->field_lengths [i]; */
528 current_type->length=offset;
529 show_pad_info.max_line=l-1;
530 refresh_show_pad ();show_info ();
534 void next (char *command_line)
538 char *ptr,buffer [80];
540 ptr=parse_word (command_line,buffer);
543 ptr=parse_word (ptr,buffer);
544 offset*=atol (buffer);
547 if (current_type!=NULL) {
548 sprintf (buffer,"setoffset type +%ld",offset);
553 if (type_data.offset_in_block+offset < file_system_info.block_size) {
554 type_data.offset_in_block+=offset;
555 sprintf (buffer,"show");dispatch (buffer);
559 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
563 void prev (char *command_line)
567 char *ptr,buffer [80];
569 ptr=parse_word (command_line,buffer);
572 ptr=parse_word (ptr,buffer);
573 offset*=atol (buffer);
576 if (current_type!=NULL) {
577 sprintf (buffer,"setoffset type -%ld",offset);
582 if (type_data.offset_in_block-offset >= 0) {
583 type_data.offset_in_block-=offset;
584 sprintf (buffer,"show");dispatch (buffer);
588 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
592 void pgdn (char *commnad_line)
595 show_pad_info.line+=show_pad_info.display_lines;
596 refresh_show_pad ();refresh_show_win ();
599 void pgup (char *command_line)
602 show_pad_info.line-=show_pad_info.display_lines;
603 refresh_show_pad ();refresh_show_win ();
606 void redraw (char *command_line)
613 void remember (char *command_line)
617 char *ptr,buffer [80];
619 if (device_handle==NULL) {
620 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
624 ptr=parse_word (command_line,buffer);
627 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
631 ptr=parse_word (ptr,buffer);
633 entry_num=remember_lifo.entries_count++;
634 if (entry_num>REMEMBER_COUNT-1) {
636 remember_lifo.entries_count--;
639 remember_lifo.offset [entry_num]=device_offset;
640 remember_lifo.type [entry_num]=current_type;
641 strcpy (remember_lifo.name [entry_num],buffer);
643 if (current_type!=NULL)
644 wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",current_type->name,device_offset,buffer);
646 wprintw (command_win,"Offset %ld remembered as %s\n",device_offset,buffer);
648 refresh_command_win ();
651 void recall (char *command_line)
654 char *ptr,buffer [80];
657 if (device_handle==NULL) {
658 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
662 ptr=parse_word (command_line,buffer);
665 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
669 ptr=parse_word (ptr,buffer);
672 for (entry_num=remember_lifo.entries_count-1;entry_num>=0;entry_num--) {
673 if (strcmp (remember_lifo.name [entry_num],buffer)==0)
678 wprintw (command_win,"Error - Can not recall %s\n",buffer);refresh_command_win ();
682 sprintf (buffer,"setoffset %ld",remember_lifo.offset [entry_num]);dispatch (buffer);
683 if (remember_lifo.type [entry_num] != NULL) {
684 sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);
688 sprintf (buffer,"settype none");dispatch (buffer);
691 wprintw (command_win,"Object %s in Offset %ld recalled\n",current_type->name,device_offset);
692 refresh_command_win ();
695 void enable_write (char *command_line)
700 if (device_handle==NULL) {
701 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
706 wprintw (command_win,"Sorry, write access is not allowed\n");
711 wprintw (command_win,"Error - Filesystem is mounted\n");
715 if ( (fp=fopen (device_name,"r+b"))==NULL) {
716 wprintw (command_win,"Error - Can not open device %s for reading and writing\n",device_name);refresh_command_win ();
719 fclose (device_handle);
720 device_handle=fp;write_access=1;
721 wprintw (command_win,"Write access enabled - Be careful\n");refresh_command_win ();
724 void disable_write (char *command_line)
729 if (device_handle==NULL) {
730 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
734 if ( (fp=fopen (device_name,"rb"))==NULL) {
735 wprintw (command_win,"Error - Can not open device %s\n",device_name);refresh_command_win ();
739 fclose (device_handle);
740 device_handle=fp;write_access=0;
741 wprintw (command_win,"Write access disabled\n");refresh_command_win ();
744 void write_data (char *command_line)