// anilinmen.c --- A simple Ncurses menu for Linux with menuname,variable args support. /*+---------------------------------------------+*/ /*|Copyright (C) 2003 Anirudh Sasikumar |*/ /*| |*/ /*|Author: Anirudh Sasikumar |*/ /*|Created: January 3 2003 |*/ /*+---------------------------------------------+*/ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* This program is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* GNU General Public License for more details. */ /* You should have received a copy of the GNU General Public License */ /* along with this program; if not, write to the Free Software */ /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Commentary: */ /* This was written during my first experimentations with ncurses and */ /* linux programming and completed within half an hour in the lab. */ /* Haven't polished it. Read and realize the evil ways of poor*/ /* commenting!*/ /* Make sure you pass -lncurses to gcc while compiling.*/ #include #include #include #include #define MAXMENU 25 //Maximum number of entries in the menu #define ENTERKEY 10 #define UPKEY 66 #define DOWNKEY 65 //Convert string to uppercase and return allocated string char *touppers(const char *s) { int i=0,size=strlen(s)+1; char *buf; buf=(char *)calloc(size,sizeof(char)); strncpy(buf,s,size); while(i='a'&&buf[i]<='z') buf[i]=buf[i]+'A'-'a'; i++; } buf[i]='\0'; return buf; } int displaymenu(char *name,char *list1,char *list2,...) { int choice=0,currkey=0,currpos=0; int i=0,size=1,strcnt=0; int xpos=1,ypos=1,maxx=1,maxy=20; char *stringlist[MAXMENU]; char *temp=0,*temp1; va_list menulist; noecho(); refresh(); getmaxyx(stdscr,maxy,maxx); temp=touppers(list2); stringlist[0]=strdup(list1); if(strcmp(temp,"EXIT")!=0) //create stringlist if there are more than 2 menu items { va_start(menulist,list2); temp1=touppers(temp); temp=list2; //loop to copy variable args to a SAFE stringlist //Have had bad experiences in DOS. Though in Linux I guess this is unnecessary. //There ought to be a more elegant way to this!! for(;strcmp(temp1,"EXIT")!=0;temp=va_arg(menulist,char *),temp1=touppers(temp)) { free(temp1); if(size>=MAXMENU) break; strcnt=strlen(temp); if(strcnt) { stringlist[size]=(char *)calloc(strcnt+1,sizeof(char)); for(i=0;i=maxy) clear(); do { move(ypos,xpos); refresh(); attron(A_UNDERLINE); printw("%s\n",name); attroff(A_UNDERLINE); for(i=0;i='1'&&currkey<=('0'+size)) { choice=currkey; currpos=currkey-'1'; //to clear any error messages printed printw("\n\n"); } else if (currkey==ENTERKEY) { printw("\n\n"); break; } else if (currkey==DOWNKEY) { printw("\n\n"); if (currpos==0) currpos=size-1; else currpos--; } else if (currkey==UPKEY) { printw("\n\n"); if (currpos==size-1) currpos=0; else currpos++; } else { printw("\nInvalid option... Please press keys (1-%d)\n",size); } } while(currkey!=10); //to clean up stringlist for(i=0;i