MercyMenu
From Pickwiki
HomePage>>SourceCode>>ExternalSource>>C Programs>>MercyMenu
/* [[MercyMenu]] program, written in 'c' language */ /* Reads in main.menu file for path, security access on VOC, help, etc */ /* Draws box around perimeter of screen, fills with menu options. */ /* Prompts for input, if there is more than one option, or does the one.*/ /* Sets screen to raw() mode, resets to endwin(). */ /* IF num of options < [[MAX_LINES]], prints center column. */ /* If [[MAX_LINES]] < #optns < 2*[[MAX_LINES]], prints 2 columns. */ /* If # optns > 2*[[MAX_LINES]], prints 2 columns single spaced. */ /* Capacity is 31 options. Options, q,r,s are alread used. */ /* Maximum capacity is [[MAX_LINES]] = 9 * 4 = 32 +3 = 35, will need dump 'q'*/ /* One copy works on all hosts, no variations needed. */ /* */ /* cc -lHcurses [[MercyMenu]].c -o [[MercyMenu]] [10.20] */ /* cc -lcur_colr [[MercyMenu]].c -o [[MercyMenu]] [11.00] */ /* */ /* Modifications: */ /* 10/21/97 rsi added multicolumn, single space, single option xeq */ /* 10/23/97 rsi support for getstr instead of getch */ /* 01/02/98 rsi added Reflection window title reprogramming. */ /* 06/07/01 rsi added menu options 025 thru 029. */ /* */ #include <stdlib.h> #include <unistd.h> #include </usr/include/sys/getaccess.h> /* #define _[[XOPEN_SOURCE_EXTENDED]] */ #include </usr/include/curses.h> #define [[MAX_LINE_LEN]] 128 #define TITLE "Mercy Health Care System" #define [[MENU_FILE]] "/usr/local/bin/main.menu" #define SARS "sar" #define [[MAX_LINES]] 8 #define YSTART 2 #define YBOTTOM 21 typedef struct { char key; /* Key character for command types.*/ char szDesc[[[MAX_LINE_LEN]] + 1]; /* Description of command. */ char **szScript; /* Strings to make a shell script. */ char szCommand[[[MAX_LINE_LEN]] + 1]; /* The Command. */ char sz[[HelpFile]][[[MAX_LINE_LEN]] + 1]; /* Name of Help file for option. */ char szDirectory[[[MAX_LINE_LEN]] + 1] ; } menu_ent; menu_ent [[MasterMenu]][[[MAX_LINES]]*4] ; int n[[NbrLines]] ; /* number of lines in the menu. */ int bSupervisor ; /* are we supervisor? */ WINDOW *stdscr ; [[ReadFile]]() { /* read the data from our menu file */ FILE *fp ; int i, iLen, nPos, nMode; char *ptr ; char sz[[FileLine]][([[MAX_LINE_LEN]] * 3) + 4] ; char sz[[TempDir]][[[MAX_LINE_LEN]] + 1] ; char test[222] ; iLen = ([[MAX_LINE_LEN]] * 3) + 4 ; nPos = 0 ; if ((fp = fopen([[MENU_FILE]],"r")) == NULL) { printf("Error Opening the Menu File\n") ; exit (0) ; } n[[NbrLines]] = 0 ; while(!feof(fp)) { fgets(sz[[FileLine]], iLen - 1, fp) ; ptr = strtok(sz[[FileLine]], "|") ; strcpy(sz[[TempDir]], ptr) ; /* account directory */ /* see if we have access */ nMode = getaccess(sz[[TempDir]],[[UID_EUID]], [[NGROUPS_EGID_SUPP]], (int *) 0, (void*) 0, (void *) 0) ; if ((nMode >= 0) && (nMode & [[W_OK]])) { /* we have write access to this directory, add to menu */ strcpy([[MasterMenu]][nPos].szDirectory, ptr) ; /* Directory */ ptr = strtok(NULL, "|") ; strcpy([[MasterMenu]][nPos].szCommand, ptr) ; /* command */ ptr = strtok(NULL, "|") ; strcpy([[MasterMenu]][nPos].szDesc, ptr) ; /* description */ ptr = strtok(NULL, "|") ; strcpy([[MasterMenu]][nPos].sz[[HelpFile]], ptr) ; /* Help */ nPos++ ; } } /* n[[NbrLines]] = nPos - 1 ; */ n[[NbrLines]] = nPos ; fclose(fp) ; } debug(string) char *string ; { move(1,25) ; addstr(string) ; refresh() ; getch() ; } [[DrawTitle]]() { move(YSTART,25) ; addstr(TITLE) ; refresh() ; } [[DrawPrompts]]() { int l, i, j, nYpos, flg, flg2, strt, btm ; char szLine[[[MAX_LINE_LEN]] + 1] ; nYpos = YSTART + 2 ; flg2= 2; flg = 0; strt = 25; btm = n[[NbrLines]]; if (n[[NbrLines]] > [[MAX_LINES]]) { flg = 1; strt = 4; btm = [[MAX_LINES]]; } if (n[[NbrLines]] > 2*[[MAX_LINES]]) { flg2= 1; strt = 4; btm = 2*[[MAX_LINES]]; } for (i = 0 ; i < btm ; i++) { if ([[MasterMenu]][i].szDesc[0] != '\0') { sprintf(szLine ,"%3d) %s\0", i + 1 , [[MasterMenu]][i].szDesc) ; move(nYpos,strt) ; addstr(szLine) ; nYpos += flg2 ; } } if (flg) { nYpos = YSTART + 2 ; for (j = 0 ; j < btm ; j++) { i = j + btm ; if ([[MasterMenu]][i].szDesc[0] != '\0') { sprintf(szLine ,"%3d) %s\0", i +1 , [[MasterMenu]][i].szDesc) ; move(nYpos,strt+40) ; addstr(szLine) ; nYpos += flg2 ; } } } refresh() ; } int [[BottomPrompt]]() { char szPrompt[[[MAX_LINE_LEN]] + 1] ; int iLen, ch ; char chx[4] ; /* sprintf(szPrompt, "Enter Choice or (q)uit: \0") ; */ sprintf(szPrompt, "Enter Choice or (q)uit: Choice or (q)uit \0") ; iLen = strlen(szPrompt) ; move(YBOTTOM, 2) ; /* clrtoeol() ; */ addstr(szPrompt) ; refresh() ; getstr(chx) ; if ( chx[0] < 'q' ) ch = atoi(chx) -1 ; else ch = chx[0] ; return (ch) ; } Doit() { /* Only one choice, so just do it, and skip the menu. */ char szCommand[128] ; sprintf(szCommand,"%s\0", [[MasterMenu]][0].szCommand) ; if (szCommand[0] != NULL) { execl("/bin/ksh","", szCommand); exit(1) ; } else { exit(1) ; } } [[StartUp]]() { raw() ; } main() { int adj, y, x1, x2 ; int bDone = FALSE ; char chx[4] ; int ch ; int chi ; int lnLabel ; char *ptr ; char szCommand[128] ; char szLabel[128] ; char szHost[8] ; char izt[8] ; char sz[[TermType]][64] ; pid_t pPid ; bSupervisor = FALSE ; /* check our terminal */ ptr = sz[[TermType]] ; ptr = getenv("TERM") ; if (!strcmp(ptr,"unknown")) { printf("setting term type\n") ; putenv("TERM=vt100") ; } else { printf("term type set as %s\n", ptr) ; } /* Read main.menu into memory. */ [[ReadFile]]() ; /* If only one option, then just doit. */ if ( n[[NbrLines]] == 1 ) Doit() ; /* initialize our screen */ stdscr = initscr() ; [[StartUp]]() ; /* notimeout(stdscr,0); timeout(30000); */ /* draw a box */ standout() ; box(stdscr,'*','-'); standend() ; [[DrawTitle]]() ; /* put up the menu */ [[DrawPrompts]]() ; szCommand[0] = '\n' ; szLabel[0] = '\n' ; while(!bDone) { ch = [[BottomPrompt]]() ; switch(ch) { case 33: case 32: case 31: case 30: case 29: case 28: case 27: case 26: case 25: case 24: case 23: case 22: case 21: case 20: case 19: case 18: case 17: case 16: case 15: case 14: case 13: case 12: case 11: case 10: case 9: case 8: case 7: case 6: case 5: case 4: case 3: case 2: case 1: case 0: bDone = TRUE ; sprintf(szCommand,"%s\0", [[MasterMenu]][(int) (ch)].szCommand) ; sprintf(szLabel,"%s\0", [[MasterMenu]][(int) (ch)].szDesc) ; if (ptr = strstr(szCommand,SARS) != NULL) { /* special case for sars */ system("/usr/local/bin/runsars") ; exit(1) ; } break ; case 's': bDone = TRUE ; bSupervisor = TRUE ; break ; case 'q': bDone = TRUE ; break ; default: beep() ; break ; } } if (szCommand[0] != NULL && szCommand[0] != 0x00a) { printf(" this is a label: %s \n",szLabel); if (strlen(szLabel) > 5) { gethostname(szHost,6); /* Change hostname to upper case. *\ szHost[0] = toupper(szHost[0]); szHost[1] = toupper(szHost[1]); szHost[2] = toupper(szHost[2]); szHost[3] = toupper(szHost[3]); szHost[4] = toupper(szHost[4]); /* Send command upstream to Reflections telnet session, */ /* sets the title bar string to selected value. */ printf("\^[P2000;;{Sub Main\n"); printf(" With Application\n"); printf(" .Caption = \" %s %s\"\n", szHost, szLabel); printf(" End With\n"); printf("End Sub \^[\\\n" ) ; } endwin(); /* Resets raw mode back to origional settings. */ /* execl("/bin/sh","-c",szCommand) ; */ execl("/bin/ksh","", szCommand); exit(1) ; } else { endwin(); /* Resets raw mode back to origional settings. */ if (bSupervisor) { /* exit(1) ; */ } else { /* kill our process */ /* pPid = getppid() ; kill(pPid,4) ; */ exit(1) ; } } }