src/message.c
author ali@juiblex.co.uk
Mon Jan 29 20:22:01 2007 +0000 (2007-01-29)
changeset 0 acc07f1f4b1e
permissions -rw-r--r--
Initial check-in. Mostly sort of works. Ish.
     1 /*
     2  * $Id: message.c,v 1.5 2004/12/30 09:07:47 ali Exp $
     3  * Copyright (C) 2007 J. Ali Harlow
     4  * Min Hack may be freely redistributed.  See license for details. 
     5  */
     6 
     7 #include <stdlib.h>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include "minhack.h"
    11 
    12 char minhack_yn_function(const char *ques, const char *choices, char def,
    13   int *count)
    14 {
    15     int i,x,ch,n=0,cnt=0;
    16     waddstr(windows[message_win].win,ques);
    17     if (*choices)
    18     {
    19 	waddstr(windows[message_win].win," [");
    20 	for(i=0;choices[i] && choices[i]!='\033';i++)
    21 	    waddch(windows[message_win].win,choices[i]);
    22 	waddstr(windows[message_win].win,"] ");
    23 	if (def)
    24 	{
    25 	    char buf[5];
    26 	    sprintf(buf,"(%c) ",def);
    27 	    waddstr(windows[message_win].win,buf);
    28 	}
    29 	wclrtoeol(windows[message_win].win);
    30 	getyx(windows[message_win].win,i,x);
    31 	for(;;)
    32 	{
    33 	    ch=wgetch(windows[message_win].win);
    34 	    switch(ch)
    35 	    {
    36 		case '\033':
    37 		    if (strchr(choices,'q'))
    38 			return 'q';
    39 		    else if (strchr(choices,'n'))
    40 			return 'n';
    41 		    else
    42 			return def;
    43 		    break;
    44 		case ' ':
    45 		case '\r':
    46 		case '\n':
    47 		    return def;
    48 		    break;
    49 		default:
    50 		    if (strchr(choices,'#') && strchr("0123456789",ch))
    51 		    {
    52 			waddch(windows[message_win].win,ch);
    53 			n++;
    54 			cnt*=10;
    55 			cnt+=ch-'0';
    56 		    }
    57 		    else if (strchr(choices,ch))
    58 		    {
    59 			if (n)
    60 			    *count=cnt;
    61 			return ch;
    62 		    }
    63 		    else if (cnt && (ch=='\b' || ch==127))
    64 		    {
    65 			n--;
    66 			wmove(windows[message_win].win,0,n+x);
    67 			wclrtoeol(windows[message_win].win);
    68 			cnt/=10;
    69 		    }
    70 		    break;
    71 	    }
    72 	}
    73     }
    74     else
    75 	return wgetch(windows[message_win].win);
    76 }
    77 
    78 char *minhack_getlin(const char *query)
    79 {
    80     int ch,n=0,x,i,j;
    81     static char line[80];
    82     nhproxy_cb_flush_screen();
    83     minhack_putstr(message_win,0,query);
    84     wclrtoeol(windows[message_win].win);
    85     getyx(windows[message_win].win,i,x);
    86     for(;;)
    87     {
    88 	ch=wgetch(windows[message_win].win);
    89 	if (ch>=' ' && ch<='~' && n<sizeof(line)-1)
    90 	{
    91 	    waddch(windows[message_win].win,ch);
    92 	    line[n++]=ch;
    93 	}
    94 	else if (n && (ch=='\b' || ch==127))
    95 	{
    96 	    n--;
    97 	    wmove(windows[message_win].win,0,n+x);
    98 	    wclrtoeol(windows[message_win].win);
    99 	}
   100 	else if (ch=='\n' || ch=='\r')
   101 	{
   102 	    line[n]='\0';
   103 	    return line;
   104 	}
   105 	else if (ch=='\033')
   106 	{
   107 	    line[0]='\033';
   108 	    line[1]='\0';
   109 	    return line;
   110 	}
   111     }
   112 }
   113 
   114 int minhack_get_ext_cmd(void)
   115 {
   116     int i,cmd=-1;
   117     char *line;
   118     struct nhproxy_cb_get_extended_commands_res *commands;
   119     commands=nhproxy_cb_get_extended_commands();
   120     line=minhack_getlin("# ");
   121     for(i=0;i<commands->n_commands;i++)
   122 	if (!strcmp(commands->commands[i],line))
   123 	{
   124 	    cmd=i;
   125 	    break;
   126 	}
   127     nhproxy_cb_free_extended_commands(commands);
   128     return cmd;
   129 }