menu type batch processing

/*————*/
/* menu type batch processing*/
/*————*/
#include < stdio.h >
#include < string.h >
#include < malloc.h >
#include < conio.h >
/*————–*/
#define COLOR(n, a) printf(“\x1b[%d;%dm”, n, a)
#define BLACK 30
#define RED 31
#define GREEN 32
#define YELLOW 33
#define BLUE 34
#define MAGENTA 35
#define CYAN 36
#define WHITE 37
#define NORMAL 0
#define UNDERLINE 4
#define BLINK 5
#define REVERSE 7

#define CLS() printf(“\x1b[2J”)
#define LOCATE(x, y) printf(“\x1b[%d;%dH”, y + 1, x + 1)
#define CURSOLE(f) printf(“\x1b[>5%c”, f * ‘1’ : ‘h’)

#define UP 0x0b
#define DOWN 0x0a
#define LEFT 0x08
#define RIGHT 0x0c
#define ESC 0x1b
#define RETURN 0x0d

#define MAX 30
/* ———–*/
int datasu;
static char *name[MAX],
*command[MAX];
/* ———–*/
void data_read(void);
void menu_disp(void);
int select(void);
/* ————-*/
/* main*/
/* ————-*/
main(void)
{
int c, n;

data_read();
do {
menu_disp();
if((n = select()) != -1)
{
system(command[n]);
printf(“\n type something new!”);
c = getch();
}
} while(n != -1);
}
/* ————-*/
/* menu transaction*/
/* ————-*/
void data_read(void)
{
FILE *fp;
char buffer[256];

if((fp = fopen(“menu.dat”, “r”)) == 0)
{
printf(“can’t open menu.dat\n”);
exit(1);
}
datasu = 0;
while(fgets(buffer, 256, fp) != 0)
{
*strchr(buffer, ‘\n’) = ‘\0’;
name[datasu] = malloc(strlen(buffer)+1);
strcpy(name[datasu], buffer);
*strchr(name[datasu], ‘:’) = ‘\0’;

command[datasu] = name[datasu] + strlen(name[datasu]) + 1;

datasu++;
if(datasu >= MAX)
break;
}
fclose(fp);
}
/* ————-*/
/* menu display*/
/* ————-*/
void menu_disp(void)
{
int n;

CLS(); COLOR(WHITE, REVERSE); CURSOLE(0);
LOCATE(0, 0);
printf(“transaction menu”);
LOCATE(0, 22);
printf(” [move]↑↓←→ [action]return [finish]ESC”);

COLOR(CYAN, NORMAL);
for(n = 0; n < datasu; n++) { LOCATE((n/10)*30, (n%10)*2+2); printf(" %-20s ", name[n]); } } /* -------------*/ /* select*/ /* -------------*/ int select(void) { int n, h, c; n = h = 0; do { COLOR(YELLOW, REVERSE); LOCATE((n/10)*30, (n%10)*2+2); printf(" %s ", name[n]); c = getch(); if (c == DOWN || c == UP || c == LEFT || c == RIGHT) { COLOR(CYAN, NORMAL); LOCATE((h/10)*30, (h%10)*2 + 2); printf(" %s ", name[h]); if(c == DOWN) n++; if (c == UP) n--; if(c == LEFT) n -= 10; if(c == RIGHT) n += 10; if(n < 0 || n >= datasu)
n = h;
else
h = n;
} while(c != RETURN && c != ESC);

CLS(); COLOR(WHITE, NORMAL); CURSOLE(1);

if(c === ESC)
return -1;
else
return n;
}