1 /*
2 * Copyright (C) 2009-2012 Christian Heckendorf <heckendorfc@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "portal.h"
19 #include "defs.h"
20 #include "util.h"
21
cleanString(char * ostr)22 void cleanString(char *ostr){
23 char *str=ostr;
24 while(*str && *str!='\n')str++;
25 *str=0;
26 char temp[500];
27 db_clean(temp,ostr,250);
28 db_safe(ostr,temp,250);
29 //strcpy(*ostr,temp);
30 }
31
editWarn(char * warn)32 int editWarn(char *warn){
33 char c[10];
34 printf("%s\nDo you wish to continue (y/n)?",warn);
35 while(*(fgets(c,sizeof(c),stdin))=='\n'){
36 printf("Do you wish to continue (y/n)?");
37 }
38 if(*c=='y' || *c=='Y')return 1;
39 return 0;
40 }
41
getStdArgs(char * args,char * prompt)42 int getStdArgs(char *args,char *prompt){
43 int x;
44 for(x=1;x<PORTAL_ARG_LEN && args[x] && args[x]==' ';x++);
45 if(!args[x]){
46 printf("%s",prompt);
47 if(!fgets(args,PORTAL_ARG_LEN,stdin))return -1;
48 if(*args=='\n'){
49 printf("Aborted\n");
50 return -1;
51 }
52 cleanString(args);
53 return 0;
54 }
55 else{
56 args+=x;
57 cleanString(args);
58 return x;
59 }
60 }
61
portal(struct commandOption * portalOptions,const char * prefix)62 int portal(struct commandOption *portalOptions, const char *prefix){
63 char *choice;
64 if(!(choice=malloc(sizeof(char)*PORTAL_ARG_LEN))){
65 debug(2,"Malloc failed (portal choice).");
66 return 0;
67 }
68 int x,ret=0;
69
70 while(printf("%s> ",prefix) && fgets(choice,PORTAL_ARG_LEN,stdin)){
71 for(x=0;choice[x] && choice[x]!='\n';x++);
72 choice[x]=0;
73 for(x=0;portalOptions[x].opt && portalOptions[x].opt!=*choice;x++);
74 if(!portalOptions[x].opt){
75 switch(*choice){
76 case 'q':free(choice);return PORTAL_RET_QUIT;
77 case 'p':free(choice);return PORTAL_RET_PREV;
78 case '?':
79 default:
80 printf("Local:\n");
81 for(x=0;portalOptions[x].opt;x++)printf("%c\t%s\n",portalOptions[x].opt,portalOptions[x].help);
82 printf("\nGlobal:\nq\tQuit\np\tPrevious menu\n?\tPrint help\n\n");
83 break;
84 }
85 }
86 else{
87 ret=portalOptions[x].function(choice,portalOptions[x].data);
88 if(ret<PORTAL_RET_PREV)break;
89 }
90 }
91 free(choice);
92 return ret;
93 }
94
95