1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/message.c Mon Jan 29 20:22:01 2007 +0000
1.3 @@ -0,0 +1,129 @@
1.4 +/*
1.5 + * $Id: message.c,v 1.5 2004/12/30 09:07:47 ali Exp $
1.6 + * Copyright (C) 2007 J. Ali Harlow
1.7 + * Min Hack may be freely redistributed. See license for details.
1.8 + */
1.9 +
1.10 +#include <stdlib.h>
1.11 +#include <stdio.h>
1.12 +#include <string.h>
1.13 +#include "minhack.h"
1.14 +
1.15 +char minhack_yn_function(const char *ques, const char *choices, char def,
1.16 + int *count)
1.17 +{
1.18 + int i,x,ch,n=0,cnt=0;
1.19 + waddstr(windows[message_win].win,ques);
1.20 + if (*choices)
1.21 + {
1.22 + waddstr(windows[message_win].win," [");
1.23 + for(i=0;choices[i] && choices[i]!='\033';i++)
1.24 + waddch(windows[message_win].win,choices[i]);
1.25 + waddstr(windows[message_win].win,"] ");
1.26 + if (def)
1.27 + {
1.28 + char buf[5];
1.29 + sprintf(buf,"(%c) ",def);
1.30 + waddstr(windows[message_win].win,buf);
1.31 + }
1.32 + wclrtoeol(windows[message_win].win);
1.33 + getyx(windows[message_win].win,i,x);
1.34 + for(;;)
1.35 + {
1.36 + ch=wgetch(windows[message_win].win);
1.37 + switch(ch)
1.38 + {
1.39 + case '\033':
1.40 + if (strchr(choices,'q'))
1.41 + return 'q';
1.42 + else if (strchr(choices,'n'))
1.43 + return 'n';
1.44 + else
1.45 + return def;
1.46 + break;
1.47 + case ' ':
1.48 + case '\r':
1.49 + case '\n':
1.50 + return def;
1.51 + break;
1.52 + default:
1.53 + if (strchr(choices,'#') && strchr("0123456789",ch))
1.54 + {
1.55 + waddch(windows[message_win].win,ch);
1.56 + n++;
1.57 + cnt*=10;
1.58 + cnt+=ch-'0';
1.59 + }
1.60 + else if (strchr(choices,ch))
1.61 + {
1.62 + if (n)
1.63 + *count=cnt;
1.64 + return ch;
1.65 + }
1.66 + else if (cnt && (ch=='\b' || ch==127))
1.67 + {
1.68 + n--;
1.69 + wmove(windows[message_win].win,0,n+x);
1.70 + wclrtoeol(windows[message_win].win);
1.71 + cnt/=10;
1.72 + }
1.73 + break;
1.74 + }
1.75 + }
1.76 + }
1.77 + else
1.78 + return wgetch(windows[message_win].win);
1.79 +}
1.80 +
1.81 +char *minhack_getlin(const char *query)
1.82 +{
1.83 + int ch,n=0,x,i,j;
1.84 + static char line[80];
1.85 + nhproxy_cb_flush_screen();
1.86 + minhack_putstr(message_win,0,query);
1.87 + wclrtoeol(windows[message_win].win);
1.88 + getyx(windows[message_win].win,i,x);
1.89 + for(;;)
1.90 + {
1.91 + ch=wgetch(windows[message_win].win);
1.92 + if (ch>=' ' && ch<='~' && n<sizeof(line)-1)
1.93 + {
1.94 + waddch(windows[message_win].win,ch);
1.95 + line[n++]=ch;
1.96 + }
1.97 + else if (n && (ch=='\b' || ch==127))
1.98 + {
1.99 + n--;
1.100 + wmove(windows[message_win].win,0,n+x);
1.101 + wclrtoeol(windows[message_win].win);
1.102 + }
1.103 + else if (ch=='\n' || ch=='\r')
1.104 + {
1.105 + line[n]='\0';
1.106 + return line;
1.107 + }
1.108 + else if (ch=='\033')
1.109 + {
1.110 + line[0]='\033';
1.111 + line[1]='\0';
1.112 + return line;
1.113 + }
1.114 + }
1.115 +}
1.116 +
1.117 +int minhack_get_ext_cmd(void)
1.118 +{
1.119 + int i,cmd=-1;
1.120 + char *line;
1.121 + struct nhproxy_cb_get_extended_commands_res *commands;
1.122 + commands=nhproxy_cb_get_extended_commands();
1.123 + line=minhack_getlin("# ");
1.124 + for(i=0;i<commands->n_commands;i++)
1.125 + if (!strcmp(commands->commands[i],line))
1.126 + {
1.127 + cmd=i;
1.128 + break;
1.129 + }
1.130 + nhproxy_cb_free_extended_commands(commands);
1.131 + return cmd;
1.132 +}