5751030 [rkeene@sledge /home/rkeene/devel/old/billing]$ cat -n billing.c
  1 /*
  2     Created for Netfuel Design (http://www.netfueldesign.com/) 
  3     To do user management and billing.
  4 
  5     -- Roy Keene [0200220002117] rkeene@netfueldesign.com
  6 */
  7 
  8 #include "encrypt.h"
  9 #include "screenio.h"
 10 #include "billing.h"
 11 #include "variable.h"
 12 #include <termios.h>
 13 #include <unistd.h>
 14 #include <sys/types.h>
 15 #include <sys/stat.h>
 16 #include <fcntl.h>
 17 #include <sys/ioctl.h>
 18 #include <stdio.h>
 19 #include <signal.h>
 20 #include <ctype.h>
 21 #include <string.h>
 22 #ifndef NO_TCL
 23 #include <tclExtend.h>
 24 #include <tcl.h>
 25 #endif
 26 
 27 #ifndef GO32
 28 int tty_fd=1, old_clflags;
 29 #endif
 30 
 31 void resetdisplay(int signal) {
 32 #ifndef GO32
 33         struct termios terminfo;
 34         tcgetattr(tty_fd, &terminfo);
 35         terminfo.c_lflag=old_clflags;
 36         tcsetattr(tty_fd, TCSADRAIN, &terminfo);
 37 #endif
 38     return;
 39 }
 40 
 41 
 42 int main(int argc, char **argv) {
 43 #ifndef GO32
 44     struct termios terminfo;
 45 #endif
 46     if ( signal(SIGTERM, (void *) resetdisplay)==SIG_ERR || \
 47         signal(SIGINT, (void *) resetdisplay)==SIG_ERR) {
 48         perror("main: signal");
 49         printf("Something might happen to your display.\n");
 50     }
 51     clear();
 52 #ifndef GO32
 53     tty_fd=open("/dev/tty", O_RDWR);
 54     ioctl(tty_fd, TCGETS, &terminfo);
 55     old_clflags=terminfo.c_lflag;
 56     terminfo.c_lflag=0;
 57     ioctl(tty_fd, TCSETS, &terminfo);
 58 #endif
 59 
 60     playmenu("main.mnu");
 61         resetdisplay(-1);
 62 #ifndef GO32
 63         close(tty_fd);
 64 #endif
 65     return(0);
 66 }
 67 
 68 int drawmenu(char *title, struct menulist menuitems[], int numitems, int selected) {
 69     int i;
 70     gohome();
 71     printf("%40s\n\n", title);
 72     for (i=0; i<numitems; i++) {
 73         if (menuitems[i].exec_cmd.type==BILLING_EXEC_INPT) {
 74             printf("\033[%im%c - %-20s [%29s] \033[0m\n",(i==selected)*7,menuitems[i].hotkey, menuitems[i].name,
	menuitems[i].value);
 75         } else {
 76             printf("\033[%im%c - %-50s\033[0m\n", (i==selected)*7, menuitems[i].hotkey, menuitems[i].name);
 77         }
 78     }
 79     return(0);
 80 }
 81 
 82 /*
 83     Menu file format:
 84     [title (TITLE_LEN bytes)]([menu (sizeof struct menulist)]*)
 85 */
 86 int playmenu(char *file) {
 87     struct menulist menuitems[10];
 88     struct stat finfo;
 89     char title[TITLE_LEN];
 90     char ch, tmp_key;
 91     char tmp_buf[50];
 92     int selected=0, oselected=0, fd, item_cnt=0, i,x;
 93 #ifndef NO_TCL
 94     Tcl_Interp *interp=NULL;
 95 #endif
 96     fd=open(file, O_RDONLY);
 97     fstat(fd, &finfo);
 98     if (finfo.st_size<TITLE_LEN) {
 99         item_cnt=0;
100     } else {
101         item_cnt=(int) ((finfo.st_size-TITLE_LEN)/sizeof(struct menulist));
102     }
103     read(fd, &title, TITLE_LEN);
104     for (i=0;i<item_cnt;i++) {
105         read(fd, &menuitems[i], sizeof(struct menulist));
106     }
107     close(fd);
108     clear();
109     drawmenu(title, menuitems, item_cnt, selected);
110     while(1) {
111         read(STDIN_FILENO, &ch, 1);
112         if (ch==65) selected--;
113         if (ch==66) selected++;
114         if (ch==3) return(0);
115         if (selected<0) selected=0;
116         if (selected>(item_cnt-1)) selected=(item_cnt-1);
117         if (ch==13 || ch==10) ch=tolower(menuitems[selected].hotkey);
118         ch=tolower(ch);
119         for (i=0;i<item_cnt;i++) {
120             if (ch==tolower(menuitems[i].hotkey)) {
121 #ifdef DEBUG
122                 printf("%i -- %s\n", menuitems[i].exec_cmd.type, menuitems[i].exec_cmd.exec_file);
123 #endif
124                 switch (menuitems[i].exec_cmd.type) {
125                     case BILLING_EXEC_MENU:
126                         playmenu(menuitems[i].exec_cmd.exec_file);
127                         clear();
128                         oselected=-1;
129                         break;
130                     case BILLING_EXEC_TCL:
131 #ifndef NO_TCL
132                         if (interp==NULL) {
133                             interp=Tcl_CreateInterp();
134                         }
135 
136 /* We must export our variables and re-import them here. */
137 
138                         Tcl_EvalFile(interp, menuitems[i].exec_cmd.exec_file);
139                         printf("Retval=\"%s\"\n",interp->result);
140 #endif
141                         break;
142                     case BILLING_EXEC_QUIT:
143                         return(0);
144                         break;
145                     case BILLING_EXEC_INPT:
146                         locate(i+3,27);
147                         memset(&tmp_buf, '_', 29);
148                         write(STDOUT_FILENO, &tmp_buf, 29);
149                         fsync(STDOUT_FILENO);
150                         locate(i+3,27);
151                         x=0;
152                         while (1) {
153                             tmp_key=getchar();
154                             if (tmp_key==10 || tmp_key==13 || x==29) { 
155                                 locate(item_cnt+3,1);
156                                 tmp_buf[x]=0;
157                                 strcpy(menuitems[i].value,tmp_buf);
158                                 setvar(menuitems[i].exec_cmd.exec_file,tmp_buf);
159                                 drawmenu(title, menuitems, item_cnt, selected);
160                                 break; 
161                             }
162                             write(STDOUT_FILENO, &tmp_key, 1);
163                             fsync(STDOUT_FILENO);
164                             tmp_buf[x++]=tmp_key;
165                         }
166                         break;
167                 }
168             }
169         }
170 
171         if (selected!=oselected) {
172             gohome();
173             drawmenu(title, menuitems, item_cnt, selected);
174             oselected=selected;
175         }
176 
177     }
178 }
5751031 [rkeene@sledge /home/rkeene/devel/old/billing]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2000-04-01 00:58:54