Back to index

radiance  4R0+20100331
editline.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: editline.c,v 2.8 2004/03/30 16:13:01 schorsch Exp $";
00003 #endif
00004 /*
00005  *  editline.c - routine for editing raw input for rview.
00006  *
00007  *  External symbols declared in driver.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00015 #include "color.h"
00016 
00017 #include "driver.h"
00018 
00019 #define iscntrl(c)   ((c) < ' ')
00020 #define isblank(c)   ((c) == ' ')
00021 #define iserase(c)   ((c) == '\b' || (c) == 127)
00022 #define iswerase(c)  ((c) == 'W'-'@')
00023 #define iskill(c)    ((c) == 'U'-'@' || (c) == 'X'-'@')
00024 
00025 
00026 extern void
00027 editline(     /* edit input line */
00028        char  *buf,
00029        dr_getchf_t *c_get,
00030        dr_comoutf_t *s_put
00031 )
00032 {
00033        static char  erases[] = "\b \b";
00034        static char  obuf[4];
00035        register int  i;
00036        register int  c;
00037        
00038        i = 0;
00039        while ((c = (*c_get)()&0177) != '\n' && c != '\r')
00040               if (iserase(c)) {           /* single char erase */
00041                      if (i > 0) {
00042                             (*s_put)(erases);
00043                             --i;
00044                      }
00045               } else if (iswerase(c)) {   /* word erase */
00046                      while (i > 0 && isblank(buf[i-1])) {
00047                             (*s_put)(erases);
00048                             --i;
00049                      }
00050                      while (i > 0 && !isblank(buf[i-1])) {
00051                             (*s_put)(erases);
00052                             --i;
00053                      }
00054               } else if (iskill(c)) {            /* kill line */
00055                      while (i > 0) {
00056                             (*s_put)(erases);
00057                             --i;
00058                      }
00059               } else if (iscntrl(c)) {           /* control char */
00060                      i = 0;
00061                      buf[i++] = c;
00062                      obuf[0] = '^'; obuf[1] = c|0100; obuf[2] = '\0';
00063                      (*s_put)(obuf);
00064                      break;
00065               } else {                           /* regular char */
00066                      buf[i++] = c;
00067                      obuf[0] = c; obuf[1] = '\0';
00068                      (*s_put)(obuf);
00069               }
00070        buf[i] = '\0';
00071        (*s_put)("\n");
00072 }
00073 
00074 
00075 static char  mybuf[512];
00076 
00077 
00078 void
00079 tocombuf(b, d)                            /* add command(s) to my buffer */
00080 register char  *b;
00081 register struct driver  *d;
00082 {
00083        register char  *cp;
00084        char  *comstart;
00085 
00086        for (cp = mybuf; *cp; cp++)
00087               ;
00088        comstart = cp;
00089        while ( (*cp++ = *b) )
00090               if (cp >= mybuf+sizeof(mybuf)) {
00091                      *comstart = '\0';
00092                      return;              /* what should I do about this? */
00093               } else if (*b++ == '\n') {
00094                      d->inpready++;
00095                      comstart = cp;
00096               }
00097 }
00098 
00099 
00100 int
00101 fromcombuf(b, d)                   /* get command from my buffer */
00102 char  *b;
00103 struct driver  *d;
00104 {
00105        register char *cp;
00106                                           /* get next command */
00107        for (cp = mybuf; *cp != '\n'; cp++)
00108               if (!*cp)
00109                      return(0);
00110        *cp++ = '\0';
00111 #ifdef DEBUG
00112        (*d->comout)(mybuf);               /* echo my command */
00113        (*d->comout)("\n");
00114 #endif
00115                                           /* send it as reply */
00116        strcpy(b, mybuf);
00117        d->inpready--;
00118                                           /* advance commands */
00119        strcpy(mybuf, cp);
00120        return(1);
00121 }