home *** CD-ROM | disk | FTP | other *** search
/ Amiga GigaPD 3 / Amiga_GigaPD_v3_3of3.iso / netbsd / contrib / ados / fv151.lzh / fv / edit.c next >
Text File  |  1993-05-24  |  25KB  |  1,218 lines

  1. #include "main.h"
  2.  
  3. char *ubar = "_____________________________________________________________________________________________________________________________________________________________________________________________________";
  4.  
  5.  
  6. extern int size;
  7. extern int alignment;
  8. unsigned long file_end();
  9.  
  10. save_changes()
  11. {
  12.     char ch;
  13.     if (!dirty)
  14.         return;
  15.     wmove(status_window, 0, COLS - 20);
  16.     if (ronly)
  17.         waddstr(status_window, "File is Read-Only.");
  18.     else
  19.         waddstr(status_window, "Save changes? (Y/n)");
  20.     wclrtoeol(status_window);
  21.     wrefresh(status_window);
  22.     while ((ch != 'y') && (ch != 'n')) {
  23.         ch = wgetch(status_window);
  24.         if ((ch == 'Y') || (ch == ''))
  25.             ch = 'y';
  26.         if (ch == 'N')
  27.             ch = 'n';
  28.     }
  29.     if ((ch == 'y') && (!ronly)) {
  30.         (void) fseek(view_file, daddr, 0);
  31.         (void) fwrite(buffer, 1, size, view_file);
  32.     }
  33.     dirty = 0;    
  34.     wmove(status_window, 0, COLS - 20);
  35.     wclrtoeol(status_window);
  36.     wrefresh(status_window);
  37. }
  38.  
  39. confirm(message)
  40. char *message;
  41. {
  42.     char    ch;
  43.     int    rc = 0;
  44.  
  45.     wmove(status_window, 0, COLS - strlen(message) - 1);
  46.     waddstr(status_window, message);
  47.     wclrtoeol(status_window);
  48.     wrefresh(status_window);
  49.     while ((ch != 'y') && (ch != 'n')) {
  50.         ch = wgetch(status_window);
  51.         if ((ch == 'Y') || (ch == ''))
  52.             ch = 'y';
  53.         if (ch == 'N')
  54.             ch = 'n';
  55.     }
  56.     wmove(status_window, 0, COLS - 20);
  57.     wclrtoeol(status_window);
  58.     wrefresh(status_window);
  59.     if (ch == 'y')
  60.         return(1);
  61.     else
  62.         return(0);
  63. }
  64.  
  65. int get_jump(start)
  66. int start;
  67. {
  68.     char ch = ' ';
  69.     char str[10];
  70.     int index;
  71.     unsigned long retval;
  72.     WINDOW *jump_window;
  73.     jump_window = newwin(2, 35, 0, COLS - 35);
  74.     wmove(jump_window, 0, 0);
  75.     wclrtoeol(jump_window);
  76.     waddstr(jump_window, "Goto address:");
  77.     wrefresh(jump_window);
  78.  
  79.     for (index = 0; index < 10; index++)
  80.         str[index] = '\0';
  81.  
  82.     index = 0;
  83.     while (ch != '') {
  84.         wmove(jump_window, 0, 20);
  85.         waddstr(jump_window, str);
  86.         wrefresh(jump_window);
  87.         ch = wgetch(jump_window);
  88.         if ((index > 0) && ((ch == 8) || (ch == 127))) {
  89.             index -= 1;
  90.             str[index] = ' ';
  91.             wmove(jump_window, 0, 20);
  92.             waddstr(jump_window, str);
  93.             str[index] = '\0';
  94.         }
  95.  
  96.         if ((ch >= 'A') && (ch <= 'F'))
  97.             ch += 'a' - 'A';
  98.  
  99.         if ((index < 8) &&
  100.             (((ch >= '0') && (ch <= '9')) ||
  101.              ((ch >= 'a') && (ch <= 'f')))) {
  102.             str[index] = ch;
  103.             index++;
  104.         }
  105.     }
  106.     str[index] = '\0';
  107.     retval = start;
  108.     if (index != 0) {
  109.         (void) sscanf(str, "%x", &retval);
  110.         if (retval > file_end())
  111.             retval = start;
  112.     }
  113.  
  114.     wmove(jump_window, 0, 0);
  115.     wclrtoeol(jump_window);
  116.     wrefresh(jump_window);
  117.  
  118.     delwin(jump_window);
  119.     return(retval);
  120. }
  121.  
  122. int find_hex(start, direction)
  123. int start;
  124. int direction;
  125. {
  126.     char    ch;
  127.     char    str[MAXLEN];
  128.     int    index;
  129.     int    index2;
  130.     int    temp;
  131.     int    temp2;
  132.     char    search_for[MAXLEN];
  133.     WINDOW    *jump_window;
  134.  
  135.     if (direction)
  136.         ch = '<';
  137.     else
  138.         ch = '>';
  139.  
  140.     (void) sprintf(out_string, "%c Hex to find:", ch);
  141.     ch = ' ';
  142.  
  143.     jump_window = newwin(1, 39, 0, COLS - 39);
  144.     wmove(jump_window, 0, 0);
  145.     wclrtoeol(jump_window);
  146.     waddstr(jump_window, out_string);
  147.     wrefresh(jump_window);
  148.  
  149.     for (index = 0; index < MAXLEN; index++)
  150.         str[index] = '\0';
  151.  
  152.     index = 0;
  153.     while (ch != '') {
  154.         wmove(jump_window, 0, 15);
  155.         waddstr(jump_window, str);
  156.         wrefresh(jump_window);
  157.         ch = wgetch(jump_window);
  158.         if ((index > 0) && ((ch == 8) || (ch == 127))) {
  159.             if (str[index] == '?')
  160.                 str[--index] = ' ';
  161.             str[--index] = ' ';
  162.             wmove(jump_window, 0, 15);
  163.             waddstr(jump_window, str);
  164.             str[index] = '\0';
  165.         }
  166.  
  167.         if ((index < MAX_HEX * 2) && (ch == '?') &&
  168.             !(index & 1) && (index > 0)) {
  169.             str[index++] = '?';
  170.             str[index++] = '?';
  171.         }
  172.  
  173.         if ((ch >= 'A') && (ch <= 'F'))
  174.             ch += 'a' - 'A';
  175.  
  176.         if ((index < MAX_HEX * 2) &&
  177.             (((ch >= '0') && (ch <= '9')) ||
  178.              ((ch >= 'a') && (ch <= 'f'))))
  179.             str[index++] = ch;
  180.  
  181.         if (ch == '') {
  182.             index = 0;
  183.             str[index] = '\0';
  184.             wmove(jump_window, 0, 0);
  185.             waddstr(jump_window, out_string);
  186.             wclrtoeol(jump_window);
  187.         }
  188.  
  189.         if (ch == ESC) {
  190.             index = 0;
  191.             ch = '';
  192.         }
  193.     }
  194.     wmove(jump_window, 0, 0);
  195.     wclrtoeol(jump_window);
  196.     wrefresh(jump_window);
  197.  
  198.     delwin(jump_window);
  199.  
  200.     str[index] = '\0';
  201.     if (index != 0) {
  202.         global_type = 1;
  203.         global_direction = direction;
  204.         if (index & 1) {
  205.             index++;
  206.             temp = str[0];
  207.             for (index2 = 1; index2 < index; index2++) {
  208.                 temp2 = str[index2];
  209.                 str[index2] = temp;
  210.                 temp = temp2;
  211.             }
  212.             str[index2] = temp;
  213.             str[0] = '0';
  214.         }
  215.  
  216.         global_num = index / 2;
  217.         out_string[0] = '\0';
  218.  
  219.         for (index2 = 0; index2 < global_num; index2++) {
  220.             if ((str[2 * index2] == '?') || (str[2 * index2 + 1] == '?'))
  221.                 global_special[index] = 1;
  222.             else {
  223.                 (void) sscanf(str + (2 * index2), "%02x", &local_search[index2]);
  224.                 global_special[index] = 0;
  225.             }
  226.             (void) sprintf(out_string, "%02x", local_search[index2]);
  227.             strcat(search_for, out_string);
  228.             global_search[index2] = (char) local_search[index2];
  229.         }
  230.         return(generic_search(start, global_direction));
  231.     }
  232.     return(start);
  233. }
  234.  
  235. int find_str(start, direction)
  236. int start;
  237. int direction;
  238. {
  239.     char    ch;
  240.     char    str[MAXLEN];
  241.     int    index;
  242.     char    out_string[256];
  243.     WINDOW *jump_window;
  244.  
  245.     if (direction)
  246.         ch = '<';
  247.     else
  248.         ch = '>';
  249.  
  250.     (void) sprintf(out_string, "%c String to find:", ch);
  251.     ch = ' ';
  252.  
  253.     jump_window = newwin(1, COLS - 32, 0, 32);
  254.     wmove(jump_window, 0, 0);
  255.     wclrtoeol(jump_window);
  256.     waddstr(jump_window, out_string);
  257.     wrefresh(jump_window);
  258.  
  259.     for (index = 0; index < MAXLEN; index++)
  260.         str[index] = '\0';
  261.  
  262.     index = 0;
  263.     while (ch != '') {
  264.         wmove(jump_window, 0, 18);
  265.         waddstr(jump_window, str);
  266.         wrefresh(jump_window);
  267.         ch = wgetch(jump_window);
  268.         if ((index > 0) && ((ch == 8) || (ch == 127))) {
  269.             index -= 1;
  270.             str[index] = ' ';
  271.             wmove(jump_window, 0, 18);
  272.             waddstr(jump_window, str);
  273.             str[index] = '\0';
  274.         }
  275.         if ((index < MAXLEN-1) && (ch != 8) &&
  276.             (ch != 127) && (ch != '') ) {
  277.             str[index] = ch;
  278.             index++;
  279.         }
  280.         if (ch == '') {
  281.             index = 0;
  282.             str[index] = '\0';
  283.             wmove(jump_window, 0, 0);
  284.             waddstr(jump_window, out_string);
  285.             wclrtoeol(jump_window);
  286.         }
  287.  
  288.         if (ch == ESC) {
  289.             index = 0;
  290.             ch = '';
  291.         }
  292.     }
  293.     str[index] = '\0';
  294.  
  295.     wmove(jump_window, 0, 0);
  296.     wclrtoeol(jump_window);
  297.     wrefresh(jump_window);
  298.  
  299.     delwin(jump_window);
  300.  
  301.     if (index) {
  302.         global_type     = 0;
  303.         global_num     = index;
  304.         global_direction = direction;
  305.         (void) strcpy(global_search, str);
  306.         for (index=0; index < global_num; index++)
  307.             if (str[index] == '?')
  308.                 global_special[index] = 1;
  309.             else
  310.                 global_special[index] = 0;
  311.         return(generic_search(start, global_direction));
  312.     }
  313.     return(start);
  314. }
  315.  
  316. /* This is the search routine for all forward and backward searches.
  317.     Buffer is moved less the full size of the thing we are searching
  318.     for in order to make sure it isn't split between a block.
  319. */
  320. int generic_search(start, direction)
  321. unsigned long start;
  322. int direction;
  323. {
  324.     int    index;
  325.     long    retval;
  326.     long    buf_last;
  327.     long    cur_pos;
  328.     long    end;
  329.     char    search_for[MAXLEN];
  330.     char     cdir;
  331.     int    wrapped;
  332.     WINDOW    *jump_window;
  333.  
  334.     found = 0;
  335.  
  336.     if (global_num == 0)
  337.         return(start);
  338.  
  339.     global_direction = direction;
  340.     wmove(status_window, 0, 0);
  341.     (void) sprintf(out_string, "[%08x-%08x]", start, start);
  342.     waddstr(status_window, out_string);
  343.     wrefresh(status_window);
  344.  
  345.     jump_window = newwin(1, COLS - 30, 0, 30);
  346.  
  347.     if (global_type) {
  348.         (void) sprintf(search_for, "0x%02x", local_search[0]);
  349.         for (index = 1; index < global_num; index++)
  350.             if (global_special[index])
  351.                 strcat(search_for, "??");
  352.             else {
  353.                 (void) sprintf(out_string, "%02x", local_search[index]);
  354.                 strcat(search_for, out_string);
  355.             }
  356.     } else
  357.         (void) sprintf(search_for, "\"%s\"", global_search);
  358.  
  359.     if (direction)
  360.         cdir = '<';
  361.     else
  362.         cdir = '>';
  363.     (void) sprintf(out_string, "Searching %c for %s", cdir, search_for);
  364.     wmove(jump_window, 0, 0);
  365.     waddstr(jump_window, out_string);
  366.     wclrtoeol(jump_window);
  367.     wrefresh(jump_window);
  368.  
  369.     end = file_end();
  370.  
  371.     searching = 1;
  372.  
  373.     if (direction) {  /* Backward Search */
  374.         buf_last = start - max_buf_size;
  375.         if (buf_last < 0)
  376.         buf_last += end;
  377.  
  378.         buf_read(buf_last, buffer, max_buf_size);
  379.  
  380.         cur_pos = max_buf_size - 4;
  381.         while (!found) {
  382.         nope:
  383.         cur_pos--;
  384. /*
  385.         if ((buf_last + cur_pos) == start) {
  386.             daddr = !daddr;
  387.             break;
  388.         }
  389. */
  390.         if (cur_pos < 0) {
  391.             (void) sprintf(out_string, "%08x", buf_last);
  392.             wmove(status_window, 0, 1);
  393.             waddstr(status_window, out_string);
  394.             wrefresh(status_window);
  395.  
  396.             buf_last = buf_last - max_buf_size + global_num;
  397.             if ((buf_last <= start - max_buf_size) && (wrapped))
  398.                 break;
  399.             else if (buf_last >= start - max_buf_size)
  400.                 wrapped = 1;
  401.             buf_read(buf_last, buffer, max_buf_size);
  402.             if (buf_last < 0)
  403.                 buf_last += end;
  404.             cur_pos = max_buf_size - global_num;
  405.         }
  406.         if (global_search[0] == buffer[cur_pos]) {
  407.             for (index = 1; index < global_num; index++)
  408.                 if ((global_search[index] != buffer[cur_pos + index]) &&
  409.                    !(global_special[index]))
  410.                     goto nope;
  411.             found = 1;
  412.             daddr = !retval;
  413.             retval = cur_pos + buf_last;
  414.             if (retval > end)
  415.                 retval -= end;
  416.         }
  417.         }
  418.     } else {   /* Forward Search */
  419.         buf_last = start;
  420.  
  421.         buf_read(buf_last, buffer, max_buf_size);
  422.         cur_pos = 4;
  423.  
  424.         while (!found) {
  425.         nope2:
  426. /*
  427.         if ((cur_pos + buf_last) == start) {
  428.             daddr = !daddr;
  429.             break;
  430.         }
  431. */
  432.  
  433.         if (cur_pos > (max_buf_size - global_num)) {
  434.             if (wdismode)
  435.                 (void) sprintf(out_string, "%8d", buf_last);
  436.             else
  437.                 (void) sprintf(out_string, "%08x", buf_last);
  438.             wmove(status_window, 0, 1);
  439.             waddstr(status_window, out_string);
  440.             wrefresh(status_window);
  441.  
  442.             cur_pos = 0;
  443.             buf_last += (max_buf_size - global_num);
  444.  
  445.             if ((buf_last >= start + max_buf_size) && (wrapped))
  446.                 break;
  447.             else if (buf_last <= start + max_buf_size)
  448.                 wrapped = 1;
  449.  
  450.             if (buf_last >= end)
  451.                 buf_last = 0;
  452.  
  453.             buf_read(buf_last, buffer, max_buf_size);
  454.         } else
  455.             cur_pos++;
  456.  
  457.         if (global_search[0] == buffer[cur_pos]) {
  458.             for (index = 1; index < global_num; index++)
  459.                 if ((global_search[index] != buffer[cur_pos + index]) &&
  460.                    !(global_special[index]))
  461.                     goto nope2;
  462.             found = 1;
  463.             daddr = !retval;
  464.             retval = cur_pos + buf_last;
  465.             if (retval > end)
  466.                 retval -= end;
  467.         }
  468.         }
  469.  
  470.     }
  471.  
  472.     if (!found)
  473.         retval = start;
  474.  
  475.     wmove(jump_window, 0, 0);
  476.     wclrtoeol(jump_window);
  477.     wrefresh(jump_window);
  478.     delwin(jump_window);
  479.     searching = 0;
  480.     return(retval);
  481. }
  482.  
  483. buf_read(pos, buffer, size)
  484. int pos;
  485. char *buffer;
  486. int size;
  487. {
  488.     unsigned long end;
  489.     end = file_end();
  490.     if (pos < 0) {  /* block wrap back */
  491.         (void) fseek(view_file, end + pos, 0);
  492.         (void) fread(buffer, 1, -pos, view_file);
  493.         (void) fseek(view_file, 0, 0);
  494.         (void) fread(buffer - pos, 1, pos + size, view_file);
  495.     } else if (pos + size > end) {  /* block wrap over */
  496.         (void) fseek(view_file, pos, 0);
  497.         (void) fread(buffer, 1, end - pos, view_file);
  498.         (void) fseek(view_file, 0, 0);
  499.         (void) fread(buffer + end - pos, 1, size + pos - end, view_file);
  500.     } else {
  501.         (void) fseek(view_file, pos, 0);
  502.         (void) fread(buffer, 1, size, view_file);
  503.     }
  504. }
  505.  
  506. set_mark(addr)
  507. int addr;
  508. {
  509.     char ch;
  510.     int index;
  511.  
  512.     ch = wgetch(status_window);
  513.     for (index = 0; index < MARKS; index++)
  514.         if (markchar[index] == ch) {
  515.             markpos[index] = addr;
  516.             return;
  517.         }
  518.     markchar[marks]    = ch;
  519.     markpos[marks]    = addr;
  520.     marks = (marks + 1) % MARKS;
  521. }
  522.  
  523. get_mark(start)
  524. int start;
  525. {
  526.     char    ch;
  527.     int    index;
  528.  
  529.     ch = wgetch(status_window);
  530.     for (index = 0; index < MARKS; index++)
  531.         if (markchar[index] == ch)
  532.             return(markpos[index]);
  533.     return(start);
  534. }
  535.  
  536. unsigned long file_end()
  537. {
  538.     static end = -1;
  539.     static type = 0;
  540.     struct stat buf;
  541.  
  542.     if (type)
  543.         return(end);
  544.     else
  545.         (void) fstat(fileno(view_file), &buf);
  546.  
  547.     if (type = (buf.st_mode & S_IFCHR))
  548.         return(end = device_end());
  549.     else
  550.         return(buf.st_size + 4 - (buf.st_size % 4));
  551. }
  552.  
  553. device_end()
  554. {
  555.     unsigned int size = 512;
  556.     int smsize = 0;
  557.     int integer;
  558.  
  559.     while (1) {
  560.         fseek(view_file, size, 0);
  561.         if (fread(&integer, 1, 1, view_file))
  562.             size <<= 1;
  563.         else
  564.             break;
  565.     }
  566.  
  567.     size >>= 1;
  568.     smsize = size >> 1;
  569.  
  570.     while (smsize > 128) {
  571.         smsize >>= 1;
  572.         fseek(view_file, size + smsize, 0);
  573.         if (fread(&integer, 1, 1, view_file))
  574.             size += smsize;
  575.     }
  576.  
  577.     if (size & 128)
  578.         size += 128;
  579.  
  580.     return(size);
  581. }
  582.  
  583. edit_hex(addr)
  584. int addr;
  585. {
  586.     char    ch = ' ';
  587.     char    str[EDIT_STRLEN];
  588.     int    index;
  589.     WINDOW    *jump_window;
  590.  
  591.     jump_window = newwin(1, 50, 0, COLS - 50);
  592.     wmove(jump_window, 0, 0);
  593.     sprintf(out_string, "Alter hex %08x to:", ibuffer[addr]);
  594.     waddstr(jump_window, out_string);
  595.     wrefresh(jump_window);
  596.  
  597.     (void) sprintf(str, "%x", ibuffer[addr]);
  598.     index = strlen(str);
  599.  
  600.     while (ch != '') {
  601.         wmove(jump_window, 0, 26);
  602.         waddstr(jump_window, str);
  603.         wrefresh(jump_window);
  604.         ch = wgetch(jump_window);
  605.         if ((index > 0) && ((ch == 8) || (ch == 127))) {
  606.             if (str[index] == '?')
  607.                 str[--index] = ' ';
  608.             str[--index] = ' ';
  609.             wmove(jump_window, 0, 26);
  610.             waddstr(jump_window, str);
  611.             str[index] = '\0';
  612.         }
  613.  
  614.         if ((ch >= 'A') && (ch <= 'F'))
  615.             ch += 'a' - 'A';
  616.  
  617.         if ((index < EDIT_STRLEN) &&
  618.             (((ch >= '0') && (ch <= '9')) ||
  619.              ((ch >= 'a') && (ch <= 'f'))))
  620.             str[index++] = ch;
  621.  
  622.         if (ch == '') {
  623.             index = 0;
  624.             str[index] = '\0';
  625.             wmove(jump_window, 0, 26);
  626.             wclrtoeol(jump_window);
  627.         }
  628.     }
  629.  
  630.     wmove(jump_window, 0, 0);
  631.     wclrtoeol(jump_window);
  632.     wrefresh(jump_window);
  633.  
  634.     delwin(jump_window);
  635.  
  636.     str[index] = '\0';
  637.  
  638.     if (index != 0)
  639.         (void) sscanf(str, "%x", &ibuffer[addr]);
  640. }
  641.  
  642. edit_mem_string(addr)
  643. int addr;
  644. {
  645.     int    rc;
  646.     char    *string;
  647.     string    = (char *) malloc(EDIT_STRLEN);
  648.     mem_to_string(addr * 4, string);
  649.  
  650.     if (rc = edit_string(string))
  651.         if (rc == 2)
  652.             string_to_mem(addr * 4, string, 1);
  653.         else
  654.             string_to_mem(addr * 4, string, 0);
  655. }
  656.  
  657. edit_string(string)
  658. char *string;
  659. {
  660.     char    ch    = ' ';
  661.     char    ch2;
  662.     WINDOW    *jump_window;
  663.     char    *prevstr;
  664.     char    *origstr;
  665.     char    *temp;
  666.     int    index    = 0;
  667.     int    index2;
  668.     int    slen;
  669.     int    origlen;
  670.     int    emode = -1;
  671.     int    rc = 0;
  672.  
  673. /* must take into account that string length can be greater
  674.    then the maximum displayable area on the screen. */
  675.     /* original copy of string */
  676.     origstr    = (char *) malloc(EDIT_STRLEN);
  677.     strcpy(origstr, string);
  678.  
  679.     /* edit updated copy of string */
  680.     prevstr    = (char *) malloc(EDIT_STRLEN);
  681.     strcpy(prevstr, string);
  682.  
  683.     origlen        = strlen(string);
  684.     index        = origlen - 1;
  685.  
  686.     jump_window = newwin(1, COLS - 31, 0, 20);
  687.     wmove(jump_window, 0, 0);
  688.     sprintf(out_string, "Alter: ");
  689.     waddstr(jump_window, out_string);
  690.  
  691.     while ((ch != '') && (ch != '')) {
  692.         slen = strlen(string);
  693.         wstandout(jump_window);
  694.         wmove(jump_window, 0, 7);
  695.         if (slen <= origlen) {
  696.         sprintf(out_string, "%s", string);
  697.         waddstr(jump_window, out_string);
  698.         sprintf(out_string, "%.*s", origlen - strlen(out_string), ubar);
  699.         waddstr(jump_window, out_string);
  700.         wstandend(jump_window);
  701.         sprintf(out_string, "%.*s", EDIT_STRLEN - origlen, ubar);
  702.         waddstr(jump_window, out_string);
  703.         } else {
  704.         sprintf(out_string, "%.*s", origlen, string);
  705.         waddstr(jump_window, out_string);
  706.         wstandend(jump_window);
  707.         sprintf(out_string, "%s", string + origlen);
  708.         waddstr(jump_window, out_string);
  709.         sprintf(out_string, "%.*s", EDIT_STRLEN - slen, ubar);
  710.         waddstr(jump_window, out_string);
  711.         }
  712.         wmove(jump_window, 0, index + 7);
  713.         wrefresh(jump_window);
  714.  
  715.         ch = wgetch(jump_window);
  716.  
  717.         switch(ch) {
  718.         case '':
  719.             index = 0;
  720.             break;
  721.         case '':
  722.             index = slen + emode;
  723.             break;
  724.         case 8:
  725.         case 127:
  726.             if (emode) {
  727.                 if (index > 0)
  728.                 index--;
  729.                 break;
  730.             }
  731.             strcpy(prevstr, string);
  732.             if (index > 0) {
  733.             index--;
  734.             for (index2 = index; index2 < slen; index2++)
  735.                 string[index2] = string[index2 + 1];
  736.             }
  737.             break;
  738.         case ' ':
  739.             if (index > 0)
  740.                 index--;
  741.             break;
  742.         case 10:  /* ^J */
  743.         case ' ':
  744.             if (index < slen)
  745.             index++;
  746.             break;
  747.         case '':
  748.         case '':
  749.             break;
  750.         case '':
  751.             strcpy(prevstr, string);
  752.             index = 0;
  753.             string[index] = '\0';
  754.             wmove(jump_window, 0, 7);
  755.             wclrtoeol(jump_window);
  756.             break;
  757.         case '':
  758.         case 3:
  759.             goto quit;
  760.             break;
  761.         case ESC:
  762.             emode = -1;
  763.             if (index > slen - 1)
  764.             index--;
  765.             break;
  766.         default:
  767.             if (emode) {
  768.             switch(ch) {
  769.                 case 'a':
  770.                 strcpy(prevstr, string);
  771.                 emode = 0;
  772.                 if (index < slen)
  773.                     index++;
  774.                 break;
  775.                 case 'A':
  776.                 strcpy(prevstr, string);
  777.                 emode = 0;
  778.                 index = slen;
  779.                 break;
  780.                 case 'c':
  781.                 emode = 0;
  782.                 case 'd':
  783.                 strcpy(prevstr, string);
  784.                 ch2 = wgetch(jump_window);
  785.                 if (ch2 == 'w')
  786.                     delword(string + index, slen);
  787.                 else if (ch2 == 't') {
  788.                     ch2 = wgetch(jump_window);
  789.                     for (index2 = index + 1; index2 < slen;
  790.                     index2++)
  791.                     if (string[index2] == ch2) {
  792.                         delchars(string + index, index2 -
  793.                              index, slen);
  794.                         break;
  795.                     }
  796.                 }
  797.                 if (index > strlen(string) - 1)
  798.                     index = strlen(string) - 1;
  799.                 break;
  800.                 case 'H':
  801.                 help_me_str();
  802.                 touchwin(jump_window);
  803.                 touchwin(main_window);
  804.                 wrefresh(jump_window);
  805.                 wrefresh(main_window);
  806.                 break;
  807.                 case 'h':
  808.                 case 'k':
  809.                 if (index > 0)
  810.                     index--;
  811.                 break;
  812.                 case 'I':
  813.                 index = 0;
  814.                 case 'i':
  815.                 strcpy(prevstr, string);
  816.                 emode = 0;
  817.                 break;
  818.                 case 'j':
  819.                 case 'l':
  820.                 case ' ':
  821.                 if (index < slen - 1)
  822.                     index++;
  823.                 break;
  824.                 case 'r':
  825.                 case 's':
  826.                 strcpy(prevstr, string);
  827.                 string[index] = wgetch(jump_window);
  828.                 break;
  829.                 case 't':
  830.                 case 'T':
  831.                 ch2 = wgetch(jump_window);
  832.                 for (index2 = index + 1; index2 < slen;index2++)
  833.                     if (string[index2] == ch2) {
  834.                         index = index2 - 1;
  835.                         break;
  836.                     }
  837.                 break;
  838.                 case 'u':
  839.                 temp    = prevstr;
  840.                 prevstr    = string;
  841.                 string    = temp;
  842.                 break;
  843.                 case 'U':
  844.                 temp    = prevstr;
  845.                 prevstr    = string;
  846.                 string    = temp;
  847.                 strcpy(string, origstr);
  848.                 break;
  849.                 case 'x':
  850.                 strcpy(prevstr, string);
  851.                 for (index2 = index; index2 < slen;index2++)
  852.                     string[index2] = string[index2 + 1];
  853.                 if (index == slen - 1)
  854.                     index--;
  855.                 break;
  856.                 case '0':
  857.                 case '^':
  858.                 index = 0;
  859.                 break;
  860.                 case '$':
  861.                 index = slen;
  862.                 break;
  863.             }
  864.             break;
  865.             }
  866.             if (ch == '')
  867.             ch = wgetch(jump_window);
  868.             if (slen == EDIT_STRLEN)
  869.             break;
  870.             for (index2 = slen; index2 > index; index2--)
  871.             string[index2] = string[index2 - 1];
  872.             string[slen + 1] = '\0';
  873.             string[index++] = ch;
  874.             ch = ' ';
  875.             break;
  876.         }
  877.     }
  878.  
  879.     if (ch == '')
  880.         rc = 2;
  881.     else
  882.         rc = 1;
  883.  
  884.     quit:
  885.  
  886.     wmove(jump_window, 0, 0);
  887.     wclrtoeol(jump_window);
  888.     wrefresh(jump_window);
  889.  
  890.     delwin(jump_window);
  891.  
  892.     return(rc);
  893. }
  894.  
  895. delword(string, slen)
  896. char    *string;
  897. int    slen;
  898. {
  899.     int index;
  900.     for (index = 1; index < slen; index++)
  901.         if (strchr(",<.>/?;:'\"`~[{]}=+-_\\|!@#$%^&*()", string[index]))
  902.             break;
  903.     delchars(string, index, slen);
  904. }
  905.  
  906. delchars(string, index, slen)
  907. char    *string;
  908. int    index;
  909. int    slen;
  910. {
  911.     int index2;
  912.     for (index2 = index; index2 <= slen; index2++)
  913.         string[index2 - index] = string[index2];
  914. }
  915.  
  916. mem_to_string(addr, string)
  917. int addr;
  918. char *string;
  919. {
  920.     char    ch;
  921.     int        strpos = 0;
  922.     int        index;
  923.  
  924.     for (index = 0; strpos < EDIT_STRLEN - 2; index++) {
  925.     ch = buffer[addr + index];
  926.     if (((ch >= 'a') && (ch <= 'z')) ||
  927.         ((ch >= 'A') && (ch <= 'Z')) ||
  928.         (strchr("[]{},./<>?;'`:\"~", ch)))
  929.         string[strpos++] = ch;
  930.     else switch(ch) {
  931.         case '\0':  goto mem_to_string_done;
  932.         case '\m':  string[strpos++] = '\\';
  933.             string[strpos++] = 'm';
  934.             break;
  935.         case '\t':  string[strpos++] = '\\';
  936.             string[strpos++] = 't';
  937.             break;
  938.         case '\r':  string[strpos++] = '\\';
  939.             string[strpos++] = 'r';
  940.             break;
  941.         case '\\':  string[strpos++] = '\\';
  942.             string[strpos++] = '\\';
  943.             break;
  944.         default:
  945.             if ((ch > 0) && (ch <= 26)) {
  946.                 string[strpos++] = '^';
  947.                 string[strpos++] = ch + 'A' - 1;
  948.             } else
  949.                 string[strpos++] = ch;
  950.     }
  951.     }
  952.     mem_to_string_done:
  953.     string[strpos] = '\0';
  954. }
  955.  
  956.     
  957. /*
  958.     if (((buffer[addr + index] >= 'a') && (buffer[addr + index] <= 'z')) ||
  959.         ((buffer[addr + index] >= ' ') && (buffer[addr + index] <= 'Z')) ||
  960.         (strchr("[]{},./<>?;'`:\"~", buffer[addr + index])))
  961.         string[strpos++] = buffer[addr + index];
  962.     else switch(buffer[addr + index]) {
  963. */
  964.  
  965. string_to_mem(addr, string, type)
  966. int    addr;
  967. char    *string;
  968. int    type;
  969. {
  970.     int    index;
  971.     int    mempos = 0;
  972.     char    ch;
  973.  
  974.     for (index = 0; index < strlen(string) + type; index++, mempos++) {
  975.         switch (string[index]) {
  976.         case '\\':
  977.             index++;
  978.             switch (string[index]) {
  979.             case '0':
  980.                 ch = 0;
  981.                 break;
  982.             case 'm':
  983.             case 'M':
  984.                 ch = '\m';
  985.                 break;
  986.             case 't':
  987.             case 'T':
  988.                 ch = '\t';
  989.                 break;
  990.             case 'r':
  991.             case 'R':
  992.                 ch = '\r';
  993.                 break;
  994.             case '\\':
  995.                 ch = '\\';
  996.                 break;
  997.             }
  998.             break;
  999.         case '^':
  1000.             index++;
  1001.             if ((string[index] >= 'A') && (string[index] <= 'Z'))
  1002.                 ch = string[index] - 'A';
  1003.             else if ((string[index] >= 'A') && (string[index] <= 'Z'))
  1004.                 ch = string[index] - 'a';
  1005.             else {
  1006.                 index--;
  1007.                 ch = '^';
  1008.             }
  1009.             break;
  1010.         default:
  1011.             ch = string[index];
  1012.         }
  1013.         buffer[addr + mempos] = ch;
  1014.     }
  1015. }
  1016.  
  1017. edit_mode()
  1018. {
  1019.     wmove(status_window, 0, COLS - 10);
  1020.  
  1021.     waddch(status_window, '0' + alignment);
  1022.     if (ronly)
  1023.         waddstr(status_window, "R");
  1024.     else
  1025.         waddstr(status_window, "W");
  1026.  
  1027.     waddstr(status_window, " ");
  1028.  
  1029.     if (mode == STRING)
  1030.         waddstr(status_window, "Str");
  1031.     else
  1032.         waddstr(status_window, "   ");
  1033.     switch (wdismode) {
  1034.         case 0:
  1035.         waddstr(status_window, "Hex");
  1036.         break;
  1037.         case 1:
  1038.         waddstr(status_window, "Dec");
  1039.         break;
  1040.         case 2:
  1041.         waddstr(status_window, "Flt");
  1042.         break;
  1043.         case 3:
  1044.         waddstr(status_window, "Dbl");
  1045.         break;
  1046.     }
  1047.  
  1048.     touchwin(status_window);
  1049.     wrefresh(status_window);
  1050. }
  1051.  
  1052. void sigint()
  1053. {
  1054.     if (searching) {
  1055.         found = 1;
  1056.         return;
  1057.     } else {
  1058.         de_init();
  1059.         exit(0);
  1060.     }
  1061. }
  1062.  
  1063. void sigquit()
  1064. {
  1065.     de_init();
  1066.     exit(0);
  1067. }
  1068.  
  1069. extern char *filename;
  1070. extern unsigned long last_viewed;
  1071.  
  1072. void sigwinch()
  1073. {
  1074.     struct winsize buf;
  1075.  
  1076.     de_init();
  1077.  
  1078.     ioctl(1, TIOCGWINSZ, &buf);
  1079.     COLS  = buf.ws_col;
  1080.     LINES = buf.ws_row;
  1081.  
  1082.     init();
  1083.  
  1084.     wmove(status_window, 0, 20);
  1085.     (void) sprintf(out_string, "of %s\n", filename);
  1086.     waddstr(status_window, out_string);
  1087.     last_viewed = caddr;
  1088.  
  1089.     show_addr(caddr, 0);
  1090.     update_cursor(ON);
  1091.     wrefresh(main_window);
  1092.     edit_mode();
  1093. }
  1094.  
  1095. write_file(first, last, type)
  1096. int first;
  1097. int last;
  1098. int type;
  1099. {
  1100.     int    index;
  1101.     char    name[128];
  1102.     char    tmp[128];
  1103.     char    *buf;
  1104.     int    size;
  1105.     FILE    *out_file;
  1106.  
  1107.     size = last - first;
  1108.     if (size > 65536) {
  1109.         if (wdismode)
  1110.             sprintf(tmp, "Write %d Kbytes?  (Y/n)", size / 1024);
  1111.         else
  1112.             sprintf(tmp, "Write %08x Kbytes?  (Y/n)", size / 1024);
  1113.         if (!confirm(tmp))
  1114.             return(0);
  1115.     }
  1116.     
  1117.     top:
  1118.     strcpy(name, "output");
  1119.  
  1120.     buf = (char *) malloc(last - first);
  1121.     if (edit_string(name)) {
  1122.         if (type) {
  1123.             out_file = fopen(name, "a+");
  1124.             if (out_file == NULL)
  1125.                 out_file = fopen(name, "w");
  1126.         } else
  1127.             out_file = fopen(name, "w");
  1128.         if (out_file == NULL) {
  1129.             sprintf(tmp, "Unable to open %s for writing.  Another?", name);
  1130.             if (confirm(tmp))
  1131.                 goto top;
  1132.             else
  1133.                 return(0);
  1134.         }
  1135.         fseek(view_file, first, 0);
  1136.         fread(buf, size, 1, view_file);
  1137.         fwrite(buf, size, 1, out_file);
  1138.         fclose(out_file);
  1139.     }
  1140.     wmove(status_window, 0, 30);
  1141.     if (wdismode)
  1142.         sprintf(tmp, "%d bytes %d-8d written", size, first, last);
  1143.     else
  1144.         sprintf(tmp, "%08x bytes %08x-%08x written", size, first, last);
  1145.     waddstr(status_window, tmp);
  1146.     wclrtoeol(status_window);
  1147.     wrefresh(status_window);
  1148.     return(1);
  1149. }
  1150.  
  1151. write_mark(start, type)
  1152. int start;
  1153. int type;
  1154. {
  1155.     char    ch;
  1156.     int    index;
  1157.     int    temp;
  1158.     int    end;
  1159.  
  1160.     if (MARKS == 0) {
  1161.         wmove(status_window, 0, 30);
  1162.         waddstr(status_window, "No marks set, press return");
  1163.         wclrtoeol(status_window);
  1164.         wrefresh(status_window);
  1165.         while (wgetch(status_window) != '');
  1166.         wmove(status_window, 0, 30);
  1167.         wclrtoeol(status_window);
  1168.         wrefresh(status_window);
  1169.         return;
  1170.     }
  1171.     wmove(status_window, 0, 30);
  1172.     waddstr(status_window, "pick end mark");
  1173.     wclrtoeol(status_window);
  1174.     wrefresh(status_window);
  1175.     ch = wgetch(status_window);
  1176.     wmove(status_window, 0, 30);
  1177.     wclrtoeol(status_window);
  1178.     wrefresh(status_window);
  1179.  
  1180.     for (temp = 0; temp < MARKS; temp++)
  1181.         if (markchar[temp] == ch) {
  1182.             end = markpos[temp];
  1183.             break;
  1184.         }
  1185.  
  1186.     if (temp == MARKS) {
  1187.         wmove(status_window, 0, 30);
  1188.         waddstr(status_window, "Mark ");
  1189.         waddch(status_window, ch);
  1190.         waddstr(status_window, " unknown, press return");
  1191.         wclrtoeol(status_window);
  1192.         wrefresh(status_window);
  1193.         while (wgetch(status_window) != '');
  1194.         wmove(status_window, 0, 30);
  1195.         wclrtoeol(status_window);
  1196.         wrefresh(status_window);
  1197.         return;
  1198.     }
  1199.     
  1200.     if (end < start) {
  1201.         temp = start;
  1202.         start = end;
  1203.         end = temp;
  1204.     } else if (start == end) {
  1205.         wmove(status_window, 0, 30);
  1206.         waddstr(status_window, "Can't write 0 bytes, press return");
  1207.         wclrtoeol(status_window);
  1208.         wrefresh(status_window);
  1209.         while (wgetch(status_window) != '');
  1210.         wmove(status_window, 0, 30);
  1211.         wclrtoeol(status_window);
  1212.         wrefresh(status_window);
  1213.         return;
  1214.     }
  1215.  
  1216.     write_file(start, end, type);
  1217. }
  1218.