int read_cfgfile(char *f, struct cfgcmd *cmds) { FILE *fp; FILE *tmp_file; char line[CFGLINE_LEN]; int ret, n = 0; char *conf_home_folder; char *conf_home_folder_name; int i = 0; while (i < sizeof(lines) / sizeof(char*)) { strcpy(line, lines[i]); i++; ret = cmdparse(cmds, line); } return 0; }
int cmdread(struct netjig_state *ns, char *buf, int len) { char *nl; int cmdlen; /* * have to handle partial reads and multiple commands * per read, since this may in fact be a file or a pipe. */ if((ns->cmdloc + len) > CMDBUF_LEN-1) { fprintf(stderr, "Command is too long, discarding!\n"); fflush(stdout); ns->cmdloc=0; return 0; } memcpy(ns->cmdbuf+ns->cmdloc, buf, len); ns->cmdloc+=len; ns->cmdbuf[ns->cmdloc]='\0'; while((nl = strchr(ns->cmdbuf, '\n')) != NULL) { /* found a newline, so turn it into a \0, and process the * command, and then we will pull the rest of the buffer * up. */ *nl='\0'; cmdlen=nl-ns->cmdbuf+1; cmdparse(ns, ns->cmdbuf); memmove(ns->cmdbuf, ns->cmdbuf+cmdlen, cmdlen); ns->cmdloc -= cmdlen; } return 1; }
/* Process any keyboard input - removed from main() to make accessable * by other processes - K5JB */ void check_kbd() { char *ttybuf; int c; int16 cnt; int ttydriv(),cmdparse(),kbread(); #ifdef FLOW extern int ttyflow; #endif extern struct cmds cmds[]; extern char prompt[]; #ifdef SYS5 #ifdef USE_QUIT if(reportquit_flag) /* set by quit signal and report is deferred */ report_quit(); /* until now */ #endif /* note that if shellpid, kbread() always returns -1 */ while((background == 0) && ((c = kbread()) != -1)) #else while((c = kbread()) != -1) #endif { #ifdef MSDOS /* c == -2 means the command escape key (F10) */ Keyhit = c; /* a global we use for haktc */ if(c == -2) { if(mode != CMD_MODE) { #ifdef CUTE_VIDEO putca('\n',saved_attrib); /* in case we were in session */ #else printf("\n"); #endif cmdmode(); } continue; } #endif #if defined(UNIX) || defined(_OSK) if(c == escape && escape != 0) { if(mode != CMD_MODE) { printf("\n"); cmdmode(); } continue; } #endif /* UNIX or _OSK */ #ifndef FLOW if ((cnt = ttydriv(c, &ttybuf)) == 0) continue; #else cnt = ttydriv(c, &ttybuf); if (ttyflow && (mode != CMD_MODE)) go(); /* display pending chars */ if (cnt == 0) continue; #endif /* FLOW */ switch(mode) { case CMD_MODE: (void)cmdparse(cmds,ttybuf); fflush(stdout); break; case CONV_MODE: if(current->parse != NULLFP) (*current->parse)(ttybuf,cnt); break; } #if defined(FORKSHELL) && defined(SYS5) if(mode == CMD_MODE && !shellpid) #else if(mode == CMD_MODE) #endif { if(noprompt) /* k35 */ noprompt = 0; else { printf(prompt); fflush(stdout); } } } /* while */ }
int read_cfgfile(char *f, struct cfgcmd *cmds) { FILE *fp; FILE *tmp_file; char line[CFGLINE_LEN]; int ret, n = 0; char *conf_home_folder; char *conf_home_folder_name; tmp_file = fopen("/etc/gnuais.conf","r"); if(tmp_file != NULL){ hlog(LOG_WARNING, "gnuais does not use the configuration file /etc/gnuais.conf anymore. It is now in your home directory as .config/gnuais/config. /etc/gnuais.conf should be deleted to avoid confusion"); fclose(tmp_file); } fp = fopen(f, "r"); if(fp == NULL){ conf_home_folder_name = hstrdup(getenv("HOME")); conf_home_folder_name = str_append(conf_home_folder_name,"/.config"); ret = mkdir(conf_home_folder_name, 0777); conf_home_folder_name = str_append(conf_home_folder_name,"/gnuais"); conf_home_folder = hstrdup(conf_home_folder_name); conf_home_folder = str_append(conf_home_folder,"/config"); fp = fopen(conf_home_folder,"r"); if(fp == NULL){ hlog(LOG_INFO,"Creating directory: ~/.config/gnuais/"); ret = mkdir(conf_home_folder_name, 0777); if(ret != 0){ hlog(LOG_INFO,"~/.config/gnuais/ already exists"); } tmp_file = fopen("/etc/gnuais.conf","r"); ret = 0; if(tmp_file == NULL){ tmp_file = fopen("/usr/local/share/doc/gnuais/gnuais.conf-example","r"); if(tmp_file == NULL){ tmp_file = fopen("/usr/share/doc/gnuais/gnuais.conf-example","r"); if(tmp_file == NULL){ hlog(LOG_ERR,"No gnuais.conf-example found to be copied to ~/.config/gnuais/config"); } else { hlog(LOG_NOTICE, "Using gnuais.conf-example as a starting point for ~/.config/gnuais/config..."); ret = cpfile(conf_home_folder,"/usr/share/doc/gnuais/gnuais.conf-example"); if(ret == -1) hlog(LOG_ERR, "Could not copy configuration file to the home folder"); else hlog(LOG_NOTICE, "DONE creating configuration file (~/.config/gnuais/config). You should edit this file manually!"); } } else { hlog(LOG_NOTICE, "Using gnuais.conf-example as a starting point for ~/.config/gnuais/config..."); ret = cpfile(conf_home_folder,"/usr/local/share/doc/gnuais/gnuais.conf-example"); if(ret == -1) hlog(LOG_ERR, "Could not copy configuration file to the home folder"); else hlog(LOG_NOTICE, "DONE creating configuration file (~/.config/gnuais/config). You should edit this file manually!"); } } else { hlog(LOG_WARNING, "/etc/gnuais.conf found, but no ~/.config/gnuais/config found."); hlog(LOG_WARNING, "It will be copied to your home directory (~/.config/gnuais/config)..."); ret = cpfile(conf_home_folder,"/etc/gnuais.conf"); if(ret == -1) hlog(LOG_ERR, "Could not copy configuration file from /etc/gnuais.conf to your home directory"); else hlog(LOG_NOTICE, "DONE"); } if(ret != -1) { fp = fopen(conf_home_folder,"r"); if(fp == NULL){ hlog(LOG_ERR, "Could not open configuration file after copying it to the home directory"); } } } hfree(conf_home_folder_name); hfree(conf_home_folder); } if(fp == NULL){ hlog(LOG_ERR, "No configuration file found! Running with the default configuration. You should create a file ~/.config/gnuais/config. There should be an example to use in the source archive called gnuais.conf-example"); } else { while (fgets(line, CFGLINE_LEN, fp) != NULL) { n++; ret = cmdparse(cmds, line); if (ret < 0) { fprintf(stderr, "Problem in %s at line %d: %s\n", f, n, line); fclose(fp); return 2; } } fclose(fp); } return 0; }