Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/13/07 21:41
Modified:
  05/13/07 21:41

Read: times


 
#139192 - throwing in something to chew on... :-)
Responding to: ???'s previous message
From my (unpublished yet) Basic52 rewrite:
//////////////////////////////////////////////////////////////////
//////////////////////////  LINE EDITOR //////////////////////////
//////////////////////////////////////////////////////////////////

#define prn0left putch0(0x1B); putch0(0x5B); putch0(0x44);
#define prn0right putch0(0x1B); putch0(0x5B); putch0(0x43);




void line_editor(void) {
  xdata unsigned char *p1, *p2;
  unsigned char c, esc;
  unsigned char len;

  prn0(line_buf);             // print the line
  p1 = line_buf;
  while (*p1 != 0) p1++;      // init p1 to the endmark
  len = p1 - line_buf;        //  and also get the real length
  esc = 0;                    // init esc to watch escapesequences
  c = 0;
  
  while (c != 0x0D) {                 // the main init loop
    while (!(UART0INT & 0x02));        //getch0
    c = UART0BUF;                      //
                              // now the parser
    if (c == 0x0D) {                     // CR -> exit
      putch0(0x0D);
      putch0(0x0A);
    } else if (esc != 0) {                    // escape-sequence decoding
      if (esc == 0x1B) {
        esc = c;
      } else if (esc == 0x5B) {
        if ((c == 0x43) && (*p1 != 0)) {                 // 1B 5B 43 arrow right
          p1++;
          prn0right;
          esc = 0;
        } else if ((c == 0x44) && (p1 != line_buf)) {    // 1B 5B 44 arrow left
          p1--;
          prn0left;
          esc = 0;
        } else {
          esc = 0;                              // unrecognized 1b 5B xx escapesequence
        }
      } else {
        esc = 0;                                // unrecognized 1b xx xx escapesequence
      }
    } else if (c == 0x1B) {                     // ESC - escape sequence starts
      esc = c;
    } else {
      esc = 0;                         // nonESC
      if (c == 0x7F) {                 // delete
        if (*p1 != 0) {                    // treated as backspace
          p1++;
          prn0right;
        }
        c = 0x08;
      }
      if ((c == 0x08) && (len != 0)) {                 // backspace
        if (p1 != line_buf) {
          p1--;
          prn0left;
        }
        p2 = p1;
        do {
          p2++;
          c = *p2;
          p2--;
          *p2 = c;
          if (c == 0) {
            putch0(' ');
          } else {
            putch0(c);
          }
          p2++;
        } while (c != 0);
        while (p2 > p1) {
          p2--;
          prn0left;
        }
        len--;
      } else if ((c >= 0x20) && (c <= 0x7E)) {
        if (len < line_length) {       // normal characters
          p2 = p1;
          do {
            esc = *p2;
            *p2++ = c;
            putch0(c);
            c = esc;
          } while (esc != 0);               //!! trick - esc recycled for temporary storage, 
                                            //           and here implicitly gets zeroed!!!
          *p2 = c;
          p1++;
          while (p2 > p1) {
            p2--;
            prn0left;
          }
          len++;
        } else {
        putch0(0x07);                     // too much chars? -> bell
        }
      }
    }
    
  }
}



Handles both BS and DEL, and also cursor movement (for VT100-compatible terminals).
This is for the Ramtron Versa, that's why the funny (nonstandard) names of UART SFRs -- UART0INT for SCON (roughly) and UART0BUF for SBUF. putch0 and prn0 do what would be expected from them to do.

I know it's nasty, but remember, I am still C-hater. I wrote that Basic for the Ramtron competition during the nights of the last months of last year and was in big hurry, that's why the comments are ehm sparse.

For those who prefer asm I have a very similar in functionality for my current mod of the original intel's B52 (for the RD2's), but that's even less readable.

Enjoy!

JW


List of 44 messages in thread
TopicAuthorDate
Weekend Quiz - easy            01/01/70 00:00      
   Dumbbbbbb            01/01/70 00:00      
      I know of 3, and it is \"synthtetic\"...            01/01/70 00:00      
   Hi Jan!            01/01/70 00:00      
      2 out of 3            01/01/70 00:00      
   Comment lies!            01/01/70 00:00      
      that makes 4... embarrassing            01/01/70 00:00      
         #4            01/01/70 00:00      
            N-th            01/01/70 00:00      
               Similar            01/01/70 00:00      
         Thats the one I saw first too !            01/01/70 00:00      
   Another lyin\' comment            01/01/70 00:00      
      Bingo!            01/01/70 00:00      
   seems solved - so now for the equivalent in C?            01/01/70 00:00      
      Oops            01/01/70 00:00      
         Gee ... it was just an 'x' ...            01/01/70 00:00      
            Yup ...            01/01/70 00:00      
            "just" an x ?            01/01/70 00:00      
   writing SBUF without checking?            01/01/70 00:00      
      well...            01/01/70 00:00      
      the REAL mistake is using an HLL rather than ASM            01/01/70 00:00      
         Not so            01/01/70 00:00      
   Shall we continue this???            01/01/70 00:00      
      the spec itself is problematic            01/01/70 00:00      
         How would you fix the spec?            01/01/70 00:00      
            handle DEL (0x7f) and BS?            01/01/70 00:00      
               throwing in something to chew on... :-)            01/01/70 00:00      
               Are DEL and BS equivalent ???            01/01/70 00:00      
                  relax.            01/01/70 00:00      
      ReceiveString, rev.1            01/01/70 00:00      
         ReceiveString, rev. 2            01/01/70 00:00      
            there are many ways...            01/01/70 00:00      
               Comments on comments on ...            01/01/70 00:00      
                  (comments on)^3            01/01/70 00:00      
                  Caller-saves            01/01/70 00:00      
                     Caller-save vs. Callee-save            01/01/70 00:00      
                        Compiler trade-off            01/01/70 00:00      
      ReceiveString, rev. 3            01/01/70 00:00      
   Sunday Challenge (rev 4)            01/01/70 00:00      
      hard to beat...            01/01/70 00:00      
         Just one more byte ...            01/01/70 00:00      
            want to spare bytes?            01/01/70 00:00      
               CALL vs ACALL            01/01/70 00:00      
                  it depends            01/01/70 00:00      

Back to Subject List