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,"Reviewed 2001 Christian Bac\n");
91 wprintw (show_pad,"EXT2ED is hereby placed under the terms of the GNU General Public License.\n\n");
92 wprintw (show_pad,"EXT2ED was programmed as a student project in the software laboratory\n");
93 wprintw (show_pad,"of the faculty of electrical engineering in the\n");
94 wprintw (show_pad,"Technion - Israel Institute of Technology\n");
95 wprintw (show_pad,"with the guide of Avner Lottem and Dr. Ilana David.\n");
99 wprintw (show_pad,"\n\n");max_line+=2;
101 wprintw (show_pad,"Please feel free to mail me at (currently) %s\n",email_address);
102 wprintw (show_pad,"with any commet, suggestion, and of-course, bug report concerning EXT2ED.\n");
106 show_pad_info.line=0;show_pad_info.max_line=max_line;
108 werase (show_win);wmove (show_win,0,0);
109 wprintw (show_win,"EXT2ED help");
115 void detailed_help (char *text)
120 if (current_type != NULL)
121 for (i=0;i<=current_type->type_commands.last_command;i++) {
122 if (strcmp (current_type->type_commands.names [i],text)==0) {
123 wprintw (show_pad,"%s - %s\n",text,current_type->type_commands.descriptions [i]);
124 refresh_show_pad ();return;
128 for (i=0;i<=ext2_commands.last_command;i++) {
129 if (strcmp (ext2_commands.names [i],text)==0) {
130 wprintw (show_pad,"%s - %s\n",text,ext2_commands.descriptions [i]);
131 refresh_show_pad ();return;
135 for (i=0;i<=general_commands.last_command;i++) {
136 if (strcmp (general_commands.names [i],text)==0) {
137 wprintw (show_pad,"%s - %s\n",text,general_commands.descriptions [i]);
138 refresh_show_pad ();return;
142 if (strcmp ("quit",text)==0) {
143 wprintw (show_pad,"quit - Exists EXT2ED");
144 refresh_show_pad ();return;
147 wprintw (show_pad,"Error - Command %s not aviable now\n",text);
148 refresh_show_pad ();return;
153 void set_device (char *command_line)
156 char *ptr,new_device [80];
158 ptr=parse_word (command_line,new_device);
160 wprintw (command_win,"Error - Device name not specified\n");
161 refresh_command_win ();return;
163 parse_word (ptr,new_device);
164 check_mounted (new_device);
165 if (mounted && !AllowMountedRead) {
166 wprintw (command_win,"Error - Filesystem is mounted, aborting\n");
167 wprintw (command_win,"You may wish to use the AllowMountedRead on configuration option\n");
168 refresh_command_win ();return;
171 if (mounted && AllowMountedRead) {
172 wprintw (command_win,"Warning - Filesystem is mounted. Displayed data may be unreliable.\n");
173 refresh_command_win ();
176 if (device_handle!=NULL)
177 fclose (device_handle);
179 if ( (device_handle=fopen (new_device,"rb"))==NULL) {
180 wprintw (command_win,"Error - Can not open device %s\n",new_device);refresh_command_win ();
184 strcpy (device_name,new_device);
185 write_access=0; /* Write access disabled */
186 current_type=NULL; /* There is no type now */
187 remember_lifo.entries_count=0; /* Empty Object memory */
188 free_user_commands (&ext2_commands); /* Free filesystem specific objects */
189 free_struct_descriptors ();
190 if (!set_file_system_info ()) { /* Error while getting info --> abort */
191 free_user_commands (&ext2_commands);
192 free_struct_descriptors ();
193 fclose (device_handle);
194 device_handle=NULL; /* Notice that our device is still not set up */
198 if (*AlternateDescriptors) /* Check if user defined objects exist */
199 set_struct_descriptors (AlternateDescriptors);
200 dispatch ("setoffset 0");
201 dispatch ("help"); /* Show help screen */
202 wprintw (command_win,"Device changed to %s",device_name);refresh_command_win ();
206 void set_offset (char *command_line)
211 char *ptr,new_offset_buffer [80];
213 if (device_handle==NULL) {
214 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
218 ptr=parse_word (command_line,new_offset_buffer);
221 wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();
225 ptr=parse_word (ptr,new_offset_buffer);
227 if (strcmp (new_offset_buffer,"block")==0) {
228 mult=file_system_info.block_size;
229 ptr=parse_word (ptr,new_offset_buffer);
232 if (strcmp (new_offset_buffer,"type")==0) {
233 if (current_type==NULL) {
234 wprintw (command_win,"Error - No type set\n");refresh_command_win ();
238 mult=current_type->length;
239 ptr=parse_word (ptr,new_offset_buffer);
242 if (*new_offset_buffer==0) {
243 wprintw (command_win,"Error - No offset specified\n");refresh_command_win ();
247 if (new_offset_buffer [0]=='+') {
248 if (device_offset==-1) {
249 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
252 new_offset=device_offset+atol (new_offset_buffer+1)*mult;
255 else if (new_offset_buffer [0]=='-') {
256 if (device_offset==-1) {
257 wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
260 new_offset=device_offset-atol (new_offset_buffer+1)*mult;
261 if (new_offset<0) new_offset=0;
265 new_offset=atol (new_offset_buffer)*mult;
267 if ( (fseek (device_handle,new_offset,SEEK_SET))==-1) {
268 wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",new_offset,device_name);
269 refresh_command_win ();
272 device_offset=new_offset;
273 wprintw (command_win,"Device offset changed to %ld\n",device_offset);refresh_command_win ();
275 type_data.offset_in_block=0;
278 void set (char *command_line)
281 unsigned short *int_ptr;
282 unsigned char *char_ptr;
283 unsigned long *long_ptr,offset=0;
285 char *ptr,buffer [80],variable [80],value [80];
287 if (device_handle==NULL) {
288 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
292 if (current_type==NULL) {
293 hex_set (command_line);
297 ptr=parse_word (command_line,buffer);
298 if (ptr==NULL || *ptr==0) {
299 wprintw (command_win,"Error - Missing arguments\n");refresh_command_win ();
302 parse_word (ptr,buffer);
303 ptr=strchr (buffer,'=');
305 wprintw (command_win,"Error - Bad syntax\n");refresh_command_win ();return;
307 strncpy (variable,buffer,ptr-buffer);variable [ptr-buffer]=0;
308 strcpy (value,++ptr);
310 if (current_type==NULL) {
311 wprintw (command_win,"Sorry, not yet supported\n");refresh_command_win ();return;
314 for (i=0;i<current_type->fields_num && !found;i++) {
315 if (strcmp (current_type->field_names [i],variable)==0) {
317 ptr=type_data.u.buffer+offset;
318 switch (current_type->field_lengths [i]) {
320 char_ptr=(unsigned char *) ptr;
321 *char_ptr=(char) atoi (value);
322 wprintw (command_win,"Variable %s set to %u\n",variable,*char_ptr);refresh_command_win ();
325 int_ptr=(unsigned short *) ptr;
326 *int_ptr=atoi (value);
327 wprintw (command_win,"Variable %s set to %u\n",variable,*int_ptr);refresh_command_win ();
331 long_ptr=(unsigned long *) ptr;
332 *long_ptr=atol (value);
333 wprintw (command_win,"Variable %s set to %lu\n",variable,*long_ptr);refresh_command_win ();
337 offset+=current_type->field_lengths [i];
342 wprintw (command_win,"Error - Variable %s not found\n",variable);
343 refresh_command_win ();
347 void hex_set (char *command_line)
351 char *ptr,buffer [80],*ch_ptr;
354 ptr=parse_word (command_line,buffer);
356 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
359 ptr=parse_word (ptr,buffer);
361 if (strcasecmp (buffer,"text")==0) {
366 else if (strcasecmp (buffer,"hex")==0) {
368 ptr=parse_word (ptr,buffer);
372 wprintw (command_win,"Error - Data not specified\n");refresh_command_win ();return;
377 tmp=(unsigned char) strtol (buffer,NULL,16);
378 type_data.u.buffer [type_data.offset_in_block]=tmp;
379 type_data.offset_in_block++;
380 ptr=parse_word (ptr,buffer);
381 if (type_data.offset_in_block==file_system_info.block_size) {
383 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
384 refresh_command_win ();
386 type_data.offset_in_block--;
394 tmp=(unsigned char) *ch_ptr++;
395 type_data.u.buffer [type_data.offset_in_block]=tmp;
396 type_data.offset_in_block++;
397 if (type_data.offset_in_block==file_system_info.block_size) {
399 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
400 refresh_command_win ();
402 type_data.offset_in_block--;
407 strcpy (buffer,"show");dispatch (buffer);
412 void set_type (char *command_line)
415 struct struct_descriptor *descriptor_ptr;
416 char *ptr,buffer [80],tmp_buffer [80];
419 if (!load_type_data ())
422 ptr=parse_word (command_line,buffer);
423 parse_word (ptr,buffer);
425 if (strcmp (buffer,"none")==0 || strcmp (buffer,"hex")==0) {
426 wprintw (command_win,"Data will be shown as hex dump\n");refresh_command_win ();
428 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
432 descriptor_ptr=first_type;
433 while (descriptor_ptr!=NULL && !found) {
434 if (strcmp (descriptor_ptr->name,buffer)==0)
437 descriptor_ptr=descriptor_ptr->next;
440 wprintw (command_win,"Structure type set to %s\n",buffer);refresh_command_win ();
441 current_type=descriptor_ptr;
442 sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
445 wprintw (command_win,"Error - %s is not a valid type\n",buffer);refresh_command_win ();
450 void show (char *command_line)
453 unsigned int i,l,temp_int;
454 unsigned long offset=0,temp_long;
455 unsigned char temp_char,*ch_ptr;
458 if (device_handle==NULL)
461 show_pad_info.line=0;
463 if (current_type==NULL) {
464 wmove (show_pad,0,0);
465 ch_ptr=type_data.u.buffer;
466 for (l=0;l<file_system_info.block_size/16;l++) {
467 wprintw (show_pad,"%08ld : ",offset);
469 if (type_data.offset_in_block==offset+i)
470 wattrset (show_pad,A_REVERSE);
472 if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
473 wprintw (show_pad,"%c",ch_ptr [i]);
475 wprintw (show_pad,".");
476 if (type_data.offset_in_block==offset+i)
477 wattrset (show_pad,A_NORMAL);
479 wprintw (show_pad," ");
481 if (type_data.offset_in_block==offset+i)
482 wattrset (show_pad,A_REVERSE);
484 wprintw (show_pad,"%02x",ch_ptr [i]);
486 if (type_data.offset_in_block==offset+i) {
487 wattrset (show_pad,A_NORMAL);
488 show_pad_info.line=l-l % show_pad_info.display_lines;
491 wprintw (show_pad," ");
493 wprintw (show_pad,"\n");
497 show_pad_info.max_line=l-1;show_pad_info.max_col=COLS-1;
498 refresh_show_pad ();show_info ();
501 wmove (show_pad,0,0);l=0;
502 for (i=0;i<current_type->fields_num;i++) {
503 wprintw (show_pad,"%-20s = ",current_type->field_names [i]);
504 ptr=type_data.u.buffer+offset;
505 switch (current_type->field_lengths [i]) {
507 temp_char=*((unsigned char *) ptr);
508 wprintw (show_pad,"%3u (0x%02x",temp_char,temp_char);
509 if (temp_char>=' ' && temp_char<='z')
510 wprintw (show_pad," , %c)\n",temp_char);
512 wprintw (show_pad,")\n");
517 temp_int=*((unsigned short *) ptr);
518 wprintw (show_pad,"%u (0x%x)\n",temp_int,temp_int);
522 temp_long=*((unsigned long *) ptr);
523 wprintw (show_pad,"%lu\n",temp_long);
527 /* offset+=current_type->field_lengths [i]; */
529 current_type->length=offset;
530 show_pad_info.max_line=l-1;
531 refresh_show_pad ();show_info ();
535 void next (char *command_line)
539 char *ptr,buffer [80];
541 ptr=parse_word (command_line,buffer);
544 ptr=parse_word (ptr,buffer);
545 offset*=atol (buffer);
548 if (current_type!=NULL) {
549 sprintf (buffer,"setoffset type +%ld",offset);
554 if (type_data.offset_in_block+offset < file_system_info.block_size) {
555 type_data.offset_in_block+=offset;
556 sprintf (buffer,"show");dispatch (buffer);
560 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
564 void prev (char *command_line)
568 char *ptr,buffer [80];
570 ptr=parse_word (command_line,buffer);
573 ptr=parse_word (ptr,buffer);
574 offset*=atol (buffer);
577 if (current_type!=NULL) {
578 sprintf (buffer,"setoffset type -%ld",offset);
583 if (type_data.offset_in_block-offset >= 0) {
584 type_data.offset_in_block-=offset;
585 sprintf (buffer,"show");dispatch (buffer);
589 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
593 void pgdn (char *commnad_line)
596 show_pad_info.line+=show_pad_info.display_lines;
597 refresh_show_pad ();refresh_show_win ();
600 void pgup (char *command_line)
603 show_pad_info.line-=show_pad_info.display_lines;
604 refresh_show_pad ();refresh_show_win ();
607 void redraw (char *command_line)
614 void remember (char *command_line)
618 char *ptr,buffer [80];
620 if (device_handle==NULL) {
621 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
625 ptr=parse_word (command_line,buffer);
628 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
632 ptr=parse_word (ptr,buffer);
634 entry_num=remember_lifo.entries_count++;
635 if (entry_num>REMEMBER_COUNT-1) {
637 remember_lifo.entries_count--;
640 remember_lifo.offset [entry_num]=device_offset;
641 remember_lifo.type [entry_num]=current_type;
642 strcpy (remember_lifo.name [entry_num],buffer);
644 if (current_type!=NULL)
645 wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",current_type->name,device_offset,buffer);
647 wprintw (command_win,"Offset %ld remembered as %s\n",device_offset,buffer);
649 refresh_command_win ();
652 void recall (char *command_line)
655 char *ptr,buffer [80];
658 if (device_handle==NULL) {
659 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
663 ptr=parse_word (command_line,buffer);
666 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
670 ptr=parse_word (ptr,buffer);
673 for (entry_num=remember_lifo.entries_count-1;entry_num>=0;entry_num--) {
674 if (strcmp (remember_lifo.name [entry_num],buffer)==0)
679 wprintw (command_win,"Error - Can not recall %s\n",buffer);refresh_command_win ();
683 sprintf (buffer,"setoffset %ld",remember_lifo.offset [entry_num]);dispatch (buffer);
684 if (remember_lifo.type [entry_num] != NULL) {
685 sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);
689 sprintf (buffer,"settype none");dispatch (buffer);
692 wprintw (command_win,"Object %s in Offset %ld recalled\n",current_type->name,device_offset);
693 refresh_command_win ();
696 void enable_write (char *command_line)
701 if (device_handle==NULL) {
702 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
707 wprintw (command_win,"Sorry, write access is not allowed\n");
712 wprintw (command_win,"Error - Filesystem is mounted\n");
716 if ( (fp=fopen (device_name,"r+b"))==NULL) {
717 wprintw (command_win,"Error - Can not open device %s for reading and writing\n",device_name);refresh_command_win ();
720 fclose (device_handle);
721 device_handle=fp;write_access=1;
722 wprintw (command_win,"Write access enabled - Be careful\n");refresh_command_win ();
725 void disable_write (char *command_line)
730 if (device_handle==NULL) {
731 wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
735 if ( (fp=fopen (device_name,"rb"))==NULL) {
736 wprintw (command_win,"Error - Can not open device %s\n",device_name);refresh_command_win ();
740 fclose (device_handle);
741 device_handle=fp;write_access=0;
742 wprintw (command_win,"Write access disabled\n");refresh_command_win ();
745 void write_data (char *command_line)