1 /* 2 Helps create those menu files. 3 4 -- Roy Keene [0230220002019] rkeene@netfueldesign.com 5 */ 6 7 #include "billing.h" 8 #include <unistd.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <ctype.h> 12 #include <string.h> 13 #include <sys/types.h> 14 #include <sys/stat.h> 15 #include <fcntl.h> 16 17 extern char *optarg; 18 extern int optind, opterr, optopt; 19 20 void print_help(char *argv0) { 21 printf("Usage: %s -f filename -m mode [-k hotkey] [-t type] [-n name]\n\t[-c command] [-T title] [-h]\n\n", argv0); 22 printf(" -f\tSelect file to modify\n"); 23 printf(" -m\tSelect mode {add,delete}\n"); 24 printf(" -k\tChoose hotkey\n"); 25 printf(" -t\tChoose type of entry\n"); 26 printf(" -n\tChoose name of entry\n"); 27 printf(" -c\tSelect command to execute\n"); 28 printf(" -T\tSelect title of menu\n"); 29 printf(" -h\tHelp\n\n"); 30 return; 31 } 32 33 34 int main(int argc, char **argv) { 35 char arg; 36 char title[TITLE_LEN]; 37 struct menulist menuitem; 38 struct menulist readitem; 39 struct stat finfo; 40 char *file=NULL; 41 char mode='a'; 42 int fd, i, num_ent; 43 menuitem.hotkey=0; 44 menuitem.exec_cmd.type=255; 45 menuitem.name[0]=0; 46 menuitem.exec_cmd.exec_file[0]=0; 47 menuitem.value[0]=0; 48 title[0]=0; 49 while ( (arg=getopt(argc, argv, "hf:k:t:c:m:n:T:"))!=-1 ) { 50 switch (arg) { 51 case 'f': 52 file=optarg; 53 break; 54 case 'k': 55 menuitem.hotkey=toupper(optarg[0]); 56 break; 57 case 't': 58 menuitem.exec_cmd.type=(unsigned char) (atoi(optarg)); 59 break; 60 case 'm': 61 mode=tolower(optarg[0]); 62 break; 63 case 'c': 64 strncpy(menuitem.exec_cmd.exec_file,optarg,sizeof(menuitem.exec_cmd.exec_file)); 65 break; 66 case 'n': 67 strncpy(menuitem.name, optarg, sizeof(menuitem.name)); 68 break; 69 case 'T': 70 strncpy(title, optarg, sizeof(title)); 71 break; 72 default: 73 print_help(argv[0]); 74 return(-1); 75 break; 76 } 77 } 78 if (file==NULL) { 79 printf("Must specify file.\n"); 80 print_help(argv[0]); 81 return(-1); 82 } 83 if (mode!='a' && mode!='d') { 84 printf("Invalid mode. Must be ADD or DELETE.\n"); 85 print_help(argv[0]); 86 return(-1); 87 } 88 if (menuitem.hotkey==0) { 89 printf("Must specify hotkey.\n"); 90 print_help(argv[0]); 91 return(-1); 92 } 93 if (mode=='a' && menuitem.exec_cmd.type==255) { 94 printf("Must specify type when adding.\n"); 95 print_help(argv[0]); 96 return(-1); 97 } 98 if (mode=='a' && menuitem.name[0]==0) { 99 printf("Must specify menu entry name when adding.\n"); 100 print_help(argv[0]); 101 return(-1); 102 } 103 if (mode=='a' && menuitem.exec_cmd.exec_file[0]==0) { 104 printf("Must specify command file when adding.\n"); 105 print_help(argv[0]); 106 return(-1); 107 } 108 if ((fd=open(file, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) { 109 printf("Cannot open %s for writing, aborting.\n", file); 110 perror("menumake: open"); 111 return(-1); 112 } 113 fstat(fd, &finfo); 114 if (finfo.st_size<TITLE_LEN) { 115 if (title[0]==0) { 116 close(fd); 117 printf("Must specify title for new menu.\n"); 118 print_help(argv[0]); 119 return(-1); 120 } 121 num_ent=0; 122 } else { 123 num_ent=(int) ((finfo.st_size-TITLE_LEN)/sizeof(struct menulist)); 124 } 125 if (title[0]==0) { 126 read(fd, &title, sizeof(title)); 127 } else { 128 write(fd, &title, sizeof(title)); 129 lseek(fd, TITLE_LEN, SEEK_SET); 130 } 131 if (mode=='a') { 132 for (i=0;i<num_ent;i++) { 133 read(fd, &readitem, sizeof(readitem)); 134 if (readitem.hotkey==menuitem.hotkey) { 135 lseek(fd, -sizeof(menuitem), SEEK_CUR); 136 write(fd, &menuitem, sizeof(menuitem)); 137 close(fd); 138 return(0); 139 } 140 } 141 lseek(fd, 0, SEEK_END); 142 write(fd, &menuitem, sizeof(menuitem)); 143 } else { 144 /* Need to write the deletion stuff */ 145 } 146 close(fd); 147 return(0); 148 } |