home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / text / jed / src / jed.lha / commandline.c < prev    next >
C/C++ Source or Header  |  1993-01-03  |  5KB  |  240 lines

  1.  
  2. /*
  3.  * COMMANDLINE.C
  4.  * (c) 1992 J.Harper
  5.  */
  6.  
  7. #include "jed.h"
  8. #include "jed_protos.h"
  9.  
  10. Prototype   STRPTR  docmdline        (STRPTR);
  11. Prototype   VOID    killprompthist  (VOID);
  12. Prototype   VALUE * cmd_cli        (LONG, VALUE *);
  13. Prototype   VALUE * cmd_getstr        (LONG, VALUE *);
  14. Prototype   VALUE * cmd_getnum        (LONG, VALUE *);
  15.  
  16. Local        STRPTR  LastPrompted;   /* one line history */
  17.  
  18. /*
  19.  * returns a string got from the command line
  20.  * it (the string) must be freestring()'ed after use.
  21.  * cursor should be off.
  22.  */
  23. STRPTR
  24. docmdline(STRPTR prompt)
  25. {
  26.     STRPTR cmdline;
  27.     LONG cmdlen = 1;
  28.     WORD promptlen = strlen(prompt);
  29.  
  30.     if(!unsleep())
  31.     return(FALSE);
  32.     if(cmdline = savestring(""))
  33.     {
  34.     VW *vw = CurrVW;
  35.     TX *tx = vw->vw_Tx;
  36.     POS oldcursor = vw->vw_CursorPos;
  37.     WORD oldstartcol = vw->vw_StartCol;
  38.     WORD prevx = 0;
  39.     WORD keepgoing = 0;
  40.  
  41.     vw->vw_CursorPos.pos_Col = promptlen;
  42.     vw->vw_CursorPos.pos_Line = vw->vw_StartCol + vw->vw_MaxY - 1;
  43.     vw->vw_StartCol = 0;
  44.  
  45.     redrawcmdline(prompt, cmdline, promptlen, 0);
  46.     cmdlncursor(ON);
  47.  
  48.     while(!keepgoing)
  49.     {
  50.         struct IntuiMessage *imsg;
  51.  
  52.         Wait(1 << vw->vw_Window->UserPort->mp_SigBit);
  53.         while((!keepgoing) && (imsg = GetWinIMsg(vw->vw_Window)))
  54.         {
  55.         struct IntuiMessage imsgcopy = *imsg;
  56.         ReplyMsg((struct Message *)imsg);
  57.         if(imsgcopy.Class == IDCMP_RAWKEY)
  58.         {
  59.             cmdlncursor(OFF);
  60.             switch(imsgcopy.Code)
  61.             {
  62.             case 0x41:  /* delete */
  63.                 if(cmdlen > 1)
  64.                 {
  65.                 STRPTR newline;
  66.                 vw->vw_CursorPos.pos_Col--;
  67.                 cmdlen--;
  68.                 if(newline = savestringn(cmdline, cmdlen - 1))
  69.                 {
  70.                     freestring(cmdline);
  71.                     cmdline = newline;
  72.                 }
  73.                 }
  74.                 break;
  75.             case 0x44:  /* return */
  76.                 keepgoing = 1;
  77.                 break;
  78.             case 0x45:  /* escape */
  79.                 keepgoing = -1;
  80.                 break;
  81.             case 0x4c:  /* up */
  82.             case 0x4d:  /* down */
  83.                 if(LastPrompted)
  84.                 {
  85.                 STRPTR temp = cmdline;
  86.                 cmdline = LastPrompted;
  87.                 LastPrompted = temp;
  88.                 cmdlen = strlen(cmdline) + 1;
  89.                 vw->vw_CursorPos.pos_Col = cmdlen + promptlen - 1;
  90.                 prevx = 20000;
  91.                 }
  92.                 break;
  93.             default:
  94.                 if(!(imsgcopy.Code & IECODE_UP_PREFIX))
  95.                 {
  96.                 struct InputEvent ie;
  97.                 UBYTE buff[256];
  98.                 LONG len;
  99.  
  100.                 ie.ie_Class = IECLASS_RAWKEY;
  101.                 ie.ie_SubClass = 0;
  102.                 ie.ie_Code = imsgcopy.Code;
  103.                 ie.ie_Qualifier = imsgcopy.Qualifier;
  104.                 ie.ie_EventAddress = *((APTR *)imsgcopy.IAddress);
  105.                 if((len = MapRawKey(&ie, buff, 255, NULL)) != -1)
  106.                 {
  107.                     STRPTR newline;
  108.                     buff[len] = 0;
  109.                     if(newline = savestringn(cmdline, cmdlen + len))
  110.                     {
  111.                     strcat(newline, buff);
  112.                     freestring(cmdline);
  113.                     cmdline = newline;
  114.                     cmdlen += len;
  115.                     vw->vw_CursorPos.pos_Col += len;
  116.                     }
  117.                 }
  118.                 }
  119.                 break;
  120.             }
  121.         }
  122.         else if(imsgcopy.Class == IDCMP_NEWSIZE)
  123.         {
  124.             cmdlncursor(OFF);
  125.             vw->vw_StartCol = oldstartcol;
  126.             updatedimensions(CurrVW);
  127.             redrawall();
  128.             prevx = 20000;
  129. /*            redrawcmdline(prompt, cmdline, promptlen, cmdlen - 1); */
  130.         }
  131.         else
  132.             continue;
  133.  
  134.         if(vw->vw_CursorPos.pos_Col >= vw->vw_MaxX)
  135.         {
  136.             vw->vw_StartCol = vw->vw_CursorPos.pos_Col - vw->vw_MaxX + 1;
  137.             redrawcmdline(prompt, cmdline, promptlen, cmdlen - 1);
  138.         }
  139.         else
  140.         {
  141.             vw->vw_StartCol = 0;
  142.             if(prevx >= vw->vw_MaxX)
  143.             redrawcmdline(prompt, cmdline, promptlen, cmdlen - 1);
  144.             else if(prevx < vw->vw_CursorPos.pos_Col)
  145.             redrawcmdlinefrom(prompt, cmdline, promptlen, cmdlen - 1, prevx);
  146.             else
  147.             redrawcmdlinefrom(prompt, cmdline, promptlen, cmdlen - 1, vw->vw_CursorPos.pos_Col);
  148.         }
  149.         prevx = vw->vw_CursorPos.pos_Col;
  150.         cmdlncursor(ON);
  151.         }
  152.     }
  153.     if(keepgoing < 0)
  154.     {
  155.         freestring(cmdline);
  156.         cmdline = NULL;
  157.     }
  158.     else
  159.     {
  160.         freestring(LastPrompted);
  161.         LastPrompted = savestring(cmdline);
  162.     }
  163.     cmdlncursor(OFF);
  164.     vw->vw_CursorPos = oldcursor;
  165.     vw->vw_StartCol = oldstartcol;
  166.     restorecmdline();
  167.     return(cmdline);
  168.     }
  169.     return(FALSE);
  170. }
  171.  
  172. VOID
  173. killprompthist(VOID)
  174. {
  175.     freestring(LastPrompted);
  176.     LastPrompted = NULL;
  177. }
  178.  
  179. /*
  180.  * cli type command
  181.  * (cli)
  182.  */
  183. VALUE *
  184. cmd_cli(LONG argc, VALUE *argv)
  185. {
  186.     if(!CurrVW->vw_Sleeping || unsleep())
  187.     {
  188.     STRPTR cmd;
  189.     VALUE result;
  190.  
  191.     if(cmd = docmdline("cmd> "))
  192.     {
  193.         execstr(cmd, &RES, FALSE, 0, NULL);
  194.         freestring(cmd);
  195.     }
  196.     }
  197.     return(&RES);
  198. }
  199.  
  200. /*
  201.  * command to get a string from a prompt at the bottom of the window
  202.  * (getstr `prompt')
  203.  */
  204. VALUE *
  205. cmd_getstr(LONG argc, VALUE *argv)
  206. {
  207.     if((TPLATE1(VTF_STRING)) && (!CurrVW->vw_Sleeping || unsleep()))
  208.     {
  209.     STRPTR str;
  210.     if(str = docmdline(ARG1.val_Value.String))
  211.     {
  212.         setstrres(str);
  213.     }
  214.     }
  215.     return(&RES);
  216. }
  217.  
  218. /*
  219.  * command to get a number from the prompt
  220.  * (getnum `prompt')
  221.  */
  222. VALUE *
  223. cmd_getnum(LONG argc, VALUE *argv)
  224. {
  225.     if((TPLATE1(VTF_STRING)) && (!CurrVW->vw_Sleeping || unsleep()))
  226.     {
  227.     STRPTR string;
  228.     LONG number;
  229.     STRPTR dummy;
  230.     if(string = docmdline(ARG1.val_Value.String))
  231.     {
  232.         number = strtol(string, &dummy, 0);
  233.         freestring(string);
  234.         setnumres(number);
  235.     }
  236.     }
  237.     return(&RES);
  238. }
  239.  
  240.