static int describe_command(t_connection * c, char const * comm) { char * line; int i; /* ok. the client requested help for a specific command */ while ((line=file_get_line(hfd))!=NULL) { for (i=0;line[i]==' ' && line[i]!='\0';i++); /* skip spaces in front of %command */ if (line[i]=='%') /* is this a command ? */ { char *p; int al; /* ok. now we must see if there are any aliases */ p=line+i; do { al=0; for (i=1;p[i]!=' ' && p[i]!='\0' && p[i]!='#';i++); /* skip command */ if (p[i]==' ') al=1; /* we have something after the command.. must remember that */ p[i]='\0'; /* end the string at the end of the command */ if (strcasecmp(comm,p+1)==0) /* is this the command the user asked for help ? */ { while ((line=file_get_line(hfd))!=NULL) { /* write everything until we get another % or EOF */ for (i=0;line[i]==' ';i++); /* skip spaces in front of a possible % */ if (line[i]=='%') { break; /* we reached another command */ } if (line[0]!='#') { /* is this a whole line comment ? */ /* truncate the line when a comment starts */ for (;line[i]!='\0' && line[i]!='#';i++); if (line[i]=='#') line[i]='\0'; message_send_text(c,message_type_info,c,line); } } return 0; } if (al) { for (;p[i+1]==' ' && p[i+1]!='\0';i++); /* skip spaces */ if (p[i+1]=='\0') { al=0; continue; } p+=i; /* jump to the next command */ } } while (al); } } file_get_line(NULL); // clear file_get_line buffer return -1; }
static int list_commands(t_connection * c) { char * line; int i; message_send_text(c, message_type_info, c, "Chat commands:"); std::rewind(hfd); while ((line = file_get_line(hfd)) != NULL) { for (i = 0; line[i] == ' ' && line[i] != '\0'; i++); /* skip spaces in front of %command */ if (line[i] == '%') /* is this a command ? */ { char *p, *buffer; int al; int skip; unsigned int length, position; /* ok. now we must see if there are any aliases */ length = MAX_COMMAND_LEN + 1; position = 0; buffer = (char*)xmalloc(length + 1); /* initial memory allocation = pretty fair */ p = line + i; do { al = 0; skip = 0; for (i = 1; p[i] != ' ' && p[i] != '\0' && p[i] != '#'; i++); /* skip command */ if (p[i] == ' ') al = 1; /* we have something after the command.. must remember that */ p[i] = '\0'; /* end the string at the end of the command */ p[0] = '/'; /* change the leading character (% or space) read from the help file to / */ if (!(command_get_group(p) & account_get_command_groups(conn_get_account(c)))) skip = 1; if (length < std::strlen(p) + position + 1) /* if we don't have enough space in the buffer then get some */ length = std::strlen(p) + position + 1; /* the new length */ buffer = (char*)xrealloc(buffer, length + 1); buffer[position++] = ' '; /* put a space before each alias */ /* add the alias to the output string */ std::strcpy(buffer + position, p); position += std::strlen(p); if (al) { for (; p[i + 1] == ' ' && p[i + 1] != '\0' && p[i + 1] != '#'; i++); /* skip spaces */ if (p[i + 1] == '\0' || p[i + 1] == '#') { al = 0; continue; } p += i; /* jump to the next command */ } } while (al); if (!skip) message_send_text(c, message_type_info, c, buffer); /* print out the buffer */ xfree(buffer); } } file_get_line(NULL); // clear file_get_line buffer return 0; }
extern int support_check_files(char const * supportfile) { FILE *fp; char *buff; unsigned int line; int filedirlen; char * namebuff; if (!(supportfile)) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL supportfile"); return -1; } if (!(fp = fopen(supportfile,"r"))) { eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",supportfile,pstrerror(errno)); eventlog(eventlog_level_error,__FUNCTION__,"can't guarantee that everything will run smooth"); return 0; } filedirlen = strlen(prefs_get_filedir()); for (line=1; (buff = file_get_line(fp)); line++) { if (buff[0]=='#' || buff[0]=='\0') { continue; } namebuff = (char*)xmalloc(filedirlen + 1 + strlen(buff) + 1); sprintf(namebuff,"%s/%s",prefs_get_filedir(),buff); if (access(namebuff, F_OK) < 0) { eventlog(eventlog_level_fatal,__FUNCTION__,"necessary file \"%s\" missing",namebuff); xfree((void *)namebuff); fclose(fp); return -1; } xfree((void *)namebuff); } file_get_line(NULL); // clear file_get_line buffer fclose(fp); return 0; }
static int get_interactive_line (void *p) { ExecState *s = (ExecState *) p; const char *prompt = get_prompt(s); int err = 0; #ifdef HAVE_READLINE rl_gets(&line_read, prompt); if (line_read == NULL) { strcpy(s->line, "quit"); } else if (strlen(line_read) > MAXLINE - 2) { err = E_TOOLONG; } else { *s->line = '\0'; strncat(s->line, line_read, MAXLINE - 2); strcat(s->line, "\n"); } #else printf("%s", prompt); fflush(stdout); file_get_line(s); /* note: "file" = stdin here */ #endif return err; }
int main(int arc,char *arg[]) { char cmd[128]; int ret; setvbuf(stdout, NULL, _IOLBF, 0 ); signal(SIGCHLD,sig_handler); lxdm_auth_init(&a); while(file_get_line(cmd,sizeof(cmd),stdin)>=0) { //fprintf(stderr,"begin %s\n",cmd); if(!strcmp(cmd,"auth")) { char temp[8],user[64],pass[64]; int type; ret=file_get_line(temp,sizeof(temp),stdin); if(ret<0) break; type=atoi(temp); ret=file_get_line(user,sizeof(user),stdin); if(ret<0) break; if(type==AUTH_TYPE_NORMAL) { ret=file_get_line(pass,sizeof(pass),stdin); if(ret<0) break; ret=lxdm_auth_user_authenticate(&a,user,pass,type); } else { ret=lxdm_auth_user_authenticate(&a,user,NULL,type); } printf("%d\n",ret); if(ret==AUTH_SUCCESS) { printf("%d\n",a.pw.pw_uid); printf("%d\n",a.pw.pw_gid); printf("%s\n",a.pw.pw_gecos?:""); printf("%s\n",a.pw.pw_dir); printf("%s\n",a.pw.pw_shell); } }
static int cli_get_input_line (ExecState *s) { int err = 0; if (runit || batch) { /* reading from script file */ err = file_get_line(s); } else { /* interactive use */ err = get_interactive_line(s); } return err; }
uint8* pasm_compile(const char *file, uint *opcodelen) { struct pasm_program *prog = pasm_program_create(); FILE *fp = NULL; compile_status.error = 0; char buf[80]; uint line_no = 1; *opcodelen = 0; fp = fopen(file, "r"); if (!fp) { panglog(LOG_CRITICAL, "Unable to open file"); return NULL; } while (file_get_line(buf, 80, fp) == 0) { char *line = pasm_clean_line(buf); if (strlen(line) == 0) { continue; } pasm_program_add_line(prog, line); // Handle compilation errors if (compile_status.error) { char err[128]; sprintf(err, "Error at line %u:\n %s", line_no, compile_status.emsg); panglog(LOG_CRITICAL, err); return NULL; } line_no++; } uint8 *opcode; opcode = pasm_program_compile(prog, opcodelen); panglog(LOG_VERBOSE, "Compilation complete"); return opcode; }
static int plain_read_attrs(const char *filename, t_read_attr_func cb, void *data) { std::FILE * accountfile; unsigned int line; char const * buff; unsigned int len; char * esckey; char * escval; char * key; char * val; if (!(accountfile = std::fopen(filename,"r"))) { eventlog(eventlog_level_error, __FUNCTION__,"could not open account file \"%s\" for reading (std::fopen: %s)", filename, std::strerror(errno)); return -1; } for (line=1; (buff=file_get_line(accountfile)); line++) { if (buff[0]=='#' || buff[0]=='\0') { continue; } if (std::strlen(buff)<6) /* "?"="" */ { eventlog(eventlog_level_error, __FUNCTION__, "malformed line %d of account file \"%s\"", line, filename); continue; } len = std::strlen(buff)-5+1; /* - ""="" + NUL */ esckey = (char*)xmalloc(len); escval = (char*)xmalloc(len); if (std::sscanf(buff,"\"%[^\"]\" = \"%[^\"]\"",esckey,escval)!=2) { if (std::sscanf(buff,"\"%[^\"]\" = \"\"",esckey)!=1) /* hack for an empty value field */ { eventlog(eventlog_level_error, __FUNCTION__,"malformed entry on line %d of account file \"%s\"", line, filename); xfree(escval); xfree(esckey); continue; } escval[0] = '\0'; } key = unescape_chars(esckey); val = unescape_chars(escval); /* eventlog(eventlog_level_debug,__FUNCTION__,"std::strlen(esckey)=%u (%c), len=%u",std::strlen(esckey),esckey[0],len);*/ xfree(esckey); xfree(escval); if (cb(key,val,data)) eventlog(eventlog_level_error, __FUNCTION__, "got error from callback (key: '%s' val:'%s')", key, val); if (key) xfree((void *)key); /* avoid warning */ if (val) xfree((void *)val); /* avoid warning */ } file_get_line(NULL); // clear file_get_line buffer if (std::fclose(accountfile)<0) eventlog(eventlog_level_error, __FUNCTION__, "could not close account file \"%s\" after reading (std::fclose: %s)", filename, std::strerror(errno)); return 0; }
t_list * realmlist_load(char const * filename) { FILE * fp; unsigned int line; unsigned int pos; unsigned int len; t_addr * raddr; char * temp, *temp2; char * buff; char * name; char * desc; t_realm * realm; t_list * list_head = NULL; if (!filename) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename"); return NULL; } if (!(fp = fopen(filename,"r"))) { eventlog(eventlog_level_error,__FUNCTION__,"could not open realm file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno)); return NULL; } list_head = list_create(); for (line=1; (buff = file_get_line(fp)); line++) { for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++); if (buff[pos]=='\0' || buff[pos]=='#') { continue; } if ((temp = strrchr(buff,'#'))) { unsigned int endpos; *temp = '\0'; len = strlen(buff)+1; for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--); buff[endpos+1] = '\0'; } /* skip any separators */ for (temp = buff; *temp && (*temp == ' ' || *temp == '\t');temp++); if (*temp != '"') { eventlog(eventlog_level_error,__FUNCTION__,"malformed line %u in file \"%s\" (no realmname)",line,filename); continue; } temp2 = temp + 1; /* find the next " */ for (temp = temp2; *temp && *temp != '"';temp++); if (*temp != '"' || temp == temp2) { eventlog(eventlog_level_error,__FUNCTION__,"malformed line %u in file \"%s\" (no realmname)",line,filename); continue; } /* save the realmname */ *temp = '\0'; name = xstrdup(temp2); /* eventlog(eventlog_level_trace, __FUNCTION__,"found realmname: %s",name); */ /* skip any separators */ for(temp = temp + 1; *temp && (*temp == '\t' || *temp == ' ');temp++); if (*temp == '"') { /* we have realm description */ temp2 = temp + 1; /* find the next " */ for(temp = temp2;*temp && *temp != '"';temp++); if (*temp != '"' || temp == temp2) { eventlog(eventlog_level_error,__FUNCTION__,"malformed line %u in file \"%s\" (no valid description)",line,filename); xfree(name); continue; } /* save the description */ *temp = '\0'; desc = xstrdup(temp2); /* eventlog(eventlog_level_trace, __FUNCTION__,"found realm desc: %s",desc); */ /* skip any separators */ for(temp = temp + 1; *temp && (*temp == ' ' || *temp == '\t');temp++); } else desc = xstrdup("\0"); temp2 = temp; /* find out where address ends */ for(temp = temp2 + 1; *temp && *temp != ' ' && *temp != '\t';temp++); if (*temp) *temp++ = '\0'; /* if is not the end of the file, end addr and move forward */ /* eventlog(eventlog_level_trace, __FUNCTION__,"found realm ip: %s",temp2); */ if (!(raddr = addr_create_str(temp2,0,BNETD_REALM_PORT))) /* 0 means "this computer" */ { eventlog(eventlog_level_error,__FUNCTION__,"invalid address value for field 3 on line %u in file \"%s\"",line,filename); xfree(name); xfree(desc); continue; } if (!(realm = realm_create(name,desc,addr_get_ip(raddr),addr_get_port(raddr)))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create realm"); addr_destroy(raddr); xfree(name); xfree(desc); continue; } addr_destroy(raddr); xfree(name); xfree(desc); list_prepend_data(list_head,realm); } file_get_line(NULL); // clear file_get_line buffer if (fclose(fp)<0) eventlog(eventlog_level_error,__FUNCTION__,"could not close realm file \"%s\" after reading (fclose: %s)",filename,pstrerror(errno)); return list_head; }
/* * load the lexicon configuration file. * and load all the valid lexicon from the configuration file. * * @param friso friso instance * @param config friso_config instance * @param _path dictionary directory * @param _limitts words length limit */ FRISO_API void friso_dic_load_from_ifile( friso_t friso, friso_config_t config, fstring _path, uint_t _limits ) { //1.parse the configuration file. FILE *__stream; char __chars__[1024], __key__[30], *__line__; uint_t __length__, i, t; friso_lex_t lex_t; string_buffer_t sb; //get the lexicon configruation file path sb = new_string_buffer(); string_buffer_append( sb, _path ); string_buffer_append( sb, __FRISO_LEX_IFILE__ ); //printf("%s\n", sb->buffer); if ( ( __stream = fopen( sb->buffer, "rb" ) ) != NULL ) { while ( ( __line__ = file_get_line( __chars__, __stream ) ) != NULL ) { //comment filter. if ( __line__[0] == '#' ) continue; if ( __line__[0] == '\0' ) continue; __length__ = strlen( __line__ ); //item start if ( __line__[ __length__ - 1 ] == '[' ) { //get the type key for ( i = 0; i < __length__ && ( __line__[i] == ' ' || __line__[i] == '\t' ); i++ ); for ( t = 0; i < __length__; i++,t++ ) { if ( __line__[i] == ' ' || __line__[i] == '\t' || __line__[i] == ':' ) break; __key__[t] = __line__[i]; } __key__[t] = '\0'; //get the lexicon type lex_t = get_lexicon_type_with_constant(__key__); if ( lex_t == -1 ) continue; //printf("key=%s, type=%d\n", __key__, lex_t ); while ( ( __line__ = file_get_line( __chars__, __stream ) ) != NULL ) { //comments filter. if ( __line__[0] == '#' ) continue; if ( __line__[0] == '\0' ) continue; __length__ = strlen( __line__ ); if ( __line__[ __length__ - 1 ] == ']' ) break; for ( i = 0; i < __length__ && ( __line__[i] == ' ' || __line__[i] == '\t' ); i++ ); for ( t = 0; i < __length__; i++,t++ ) { if ( __line__[i] == ' ' || __line__[i] == '\t' || __line__[i] == ';' ) break; __key__[t] = __line__[i]; } __key__[t] = '\0'; //load the lexicon item from the lexicon file. string_buffer_clear( sb ); string_buffer_append( sb, _path ); string_buffer_append( sb, __key__ ); //printf("key=%s, type=%d\n", __key__, lex_t); friso_dic_load( friso, config, lex_t, sb->buffer, _limits ); } } } //end while fclose( __stream ); } else { printf("Warning: Fail to open the lexicon configuration file %s\n", sb->buffer); } free_string_buffer(sb); }
/** * load all the valid wors from a specified lexicon file . * * @param dic friso dictionary instance (A hash array) * @param lex the lexicon type * @param lex_file the path of the lexicon file * @param length the maximum length of the word item */ FRISO_API void friso_dic_load( friso_t friso, friso_config_t config, friso_lex_t lex, fstring lex_file, uint_t length ) { FILE * _stream; char __char[1024], _buffer[512]; fstring _line; string_split_entry sse; fstring _word; char _sbuffer[512]; fstring _syn; friso_array_t sywords; uint_t _fre; if ( ( _stream = fopen( lex_file, "rb" ) ) != NULL ) { while ( ( _line = file_get_line( __char, _stream ) ) != NULL ) { //clear up the notes //make sure the length of the line is greater than 1. //like the single '#' mark in stopwords dictionary. if ( _line[0] == '#' && strlen(_line) > 1 ) continue; //handle the stopwords. if ( lex == __LEX_STOPWORDS__ ) { //clean the chinese words that its length is greater than max length. if ( ((int)_line[0]) < 0 && strlen( _line ) > length ) continue; friso_dic_add( friso->dic, __LEX_STOPWORDS__, string_copy_heap( _line, strlen(_line) ), NULL ); continue; } //split the fstring with '/'. string_split_reset( &sse, "/", _line); if ( string_split_next( &sse, _buffer ) == NULL ) continue; //1. get the word. _word = string_copy_heap( _buffer, strlen(_buffer) ); if ( string_split_next( &sse, _buffer ) == NULL ) { //normal lexicon type, //add them to the dictionary directly friso_dic_add( friso->dic, lex, _word, NULL ); continue; } /* * filter out the words that its length is larger * than the specified limit. * but not for __LEX_ECM_WORDS__ and english __LEX_STOPWORDS__ * and __LEX_CEM_WORDS__. */ if ( ! ( lex == __LEX_ECM_WORDS__ || lex == __LEX_CEM_WORDS__ ) && strlen( _word ) > length ) { FRISO_FREE(_word); continue; } //2. get the synonyms words. _syn = NULL; if ( strcmp( _buffer, "null" ) != 0 ) _syn = string_copy( _buffer, _sbuffer, strlen(_buffer) ); //3. get the word frequency if it available. _fre = 0; if ( string_split_next( &sse, _buffer ) != NULL ) _fre = atoi( _buffer ); /** * Here: * split the synonyms words with mark "," * and put them in a array list if the synonyms is not NULL */ sywords = NULL; if ( config->add_syn && _syn != NULL ) { string_split_reset( &sse, ",", _sbuffer ); sywords = new_array_list_with_opacity(5); while ( string_split_next( &sse, _buffer ) != NULL ) { if ( strlen(_buffer) > length ) continue; array_list_add( sywords, string_copy_heap(_buffer, strlen(_buffer)) ); } sywords = array_list_trim( sywords ); } //4. add the word item friso_dic_add_with_fre( friso->dic, lex, _word, sywords, _fre ); } fclose( _stream ); } else { printf("Warning: Fail to open lexicon file %s\n", lex_file); } }
extern int realmlist_create(char const * filename) { FILE * fp; unsigned int line; unsigned int pos; unsigned int len; t_addr * raddr; char * temp; char * buff; char * name; char * desc; char * addr; t_realm * realm; if (!filename) { eventlog(eventlog_level_error,"realmlist_create","got NULL filename"); return -1; } if (!(realmlist_head = list_create())) { eventlog(eventlog_level_error,"realmlist_create","could not create list"); return -1; } if (!(fp = fopen(filename,"r"))) { eventlog(eventlog_level_error,"realmlist_create","could not open realm file \"%s\" for reading (fopen: %s)",filename,strerror(errno)); list_destroy(realmlist_head); realmlist_head = NULL; return -1; } for (line=1; (buff = file_get_line(fp)); line++) { for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++); if (buff[pos]=='\0' || buff[pos]=='#') { free(buff); continue; } if ((temp = strrchr(buff,'#'))) { unsigned int endpos; *temp = '\0'; len = strlen(buff)+1; for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--); buff[endpos+1] = '\0'; } len = strlen(buff)+1; if (!(name = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for name"); free(buff); continue; } if (!(desc = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for desc"); free(name); free(buff); continue; } if (!(addr = malloc(len))) { eventlog(eventlog_level_error,"realmlist_create","could not allocate memory for desc"); free(desc); free(name); free(buff); continue; } if (sscanf(buff," \"%[^\"]\" \"%[^\"]\" %s",name,desc,addr)!=3) { if (sscanf(buff," \"%[^\"]\" \"\" %s",name,addr)==2) desc[0] = '\0'; else { eventlog(eventlog_level_error,"realmlist_create","malformed line %u in file \"%s\"",line,filename); free(addr); free(desc); free(name); free(buff); continue; } } free(buff); if (!(raddr = addr_create_str(addr,0,BNETD_REALM_PORT))) /* 0 means "this computer" */ { eventlog(eventlog_level_error,"realmlist_create","invalid address value for field 3 on line %u in file \"%s\"",line,filename); free(addr); free(desc); free(name); continue; } free(addr); if (!(realm = realm_create(name,desc,addr_get_ip(raddr),addr_get_port(raddr)))) { eventlog(eventlog_level_error,"realmlist_create","could not create realm"); addr_destroy(raddr); free(desc); free(name); continue; } addr_destroy(raddr); free(desc); free(name); if (list_prepend_data(realmlist_head,realm)<0) { eventlog(eventlog_level_error,"realmlist_create","could not prepend realm"); realm_destroy(realm); continue; } } if (fclose(fp)<0) eventlog(eventlog_level_error,"realmlist_create","could not close realm file \"%s\" after reading (fclose: %s)",filename,strerror(errno)); return 0; }
//creat a new friso with initialize item from a configuration file. __EXTERN_API__ friso_t friso_new_from_ifile( string __ifile ) { FILE *__stream; char __chars__[256], __key__[128], *__line__, __lexi__[128]; uint_t i, t, __hit__ = 0, __length__; friso_t e = friso_new(); if ( ( __stream = fopen( __ifile, "rb" ) ) != NULL ) { //initialize the entry with the value from the ifile. while ( ( __line__ = file_get_line( __chars__, __stream ) ) != NULL ) { //comments filter. if ( __line__[0] == '#' ) continue; if ( __line__[0] == '\t' ) continue; if ( __line__[0] == ' ' || __line__[0] == '\0' ) continue; __length__ = strlen( __line__ ); for ( i = 0; i < __length__; i++ ) { if ( __line__[i] == ' ' || __line__[i] == '\t' || __line__[i] == '=' ) break; __key__[i] = __line__[i]; } __key__[i] = '\0'; //position the euqals char '='. if ( __line__[i] == ' ' || __line__[i] == '\t' ) { for ( i++ ; i < __length__; i++ ) if ( __line__[i] == '=' ) break; } //clear the left whitespace of the value. for ( i++; i < __length__ && ( __line__[i] == ' ' || __line__[i] == '\t' ); i++ ); for ( t = 0; i < __length__; i++, t++ ) { if ( __line__[i] == ' ' || __line__[i] == '\t' ) break; __line__[t] = __line__[i]; } __line__[t] = '\0'; //printf("key=%s, value=%s\n", __key__, __line__ ); if ( strcmp( __key__, "friso.lex_dir" ) == 0 ) { /* * here copy the value of the lex_dir. * cause we need the value of friso.max_len to finish all * the work when we call function friso_dic_load_from_ifile to * initiliaze the friso dictionary. */ if ( __hit__ == 0 ) { __hit__ = t; for ( t = 0; t < __hit__; t++ ) { __lexi__[t] = __line__[t]; } __lexi__[t] = '\0'; } } else if ( strcmp( __key__, "friso.max_len" ) == 0 ) { e->max_len = atoi( __line__ ); } else if ( strcmp( __key__, "friso.r_name" ) == 0 ) { e->r_name = atoi( __line__ ); } else if ( strcmp( __key__, "friso.mix_len" ) == 0 ) { e->mix_len = atoi( __line__ ); } else if ( strcmp( __key__, "friso.lna_len" ) == 0 ) { e->lna_len = atoi( __line__ ); } else if ( strcmp( __key__, "friso.nthreshold" ) == 0 ) { e->nthreshold = atoi( __line__ ); } else if ( strcmp( __key__, "friso.mode" ) == 0 ) { e->mode = ( friso_mode_t ) atoi( __line__ ); } } /* * intialize the friso dictionary here. * use the setting from the ifile parse above. * we copied the value in the __lexi__. */ if ( __hit__ != 0 ) { e->dic = friso_dic_new(); friso_dic_load_from_ifile( e->dic, __lexi__, e->max_len * 3 ); } fclose( __stream ); } return e; }
extern int gametrans_load(char const * filename) { FILE * fp; unsigned int line; unsigned int pos; char * buff; char * temp; char const * viewer; char const * client; char const * output; char const * exclude; t_gametrans * entry; if (!filename) { eventlog(eventlog_level_error,"gametrans_load","got NULL filename"); return -1; } if (!(gametrans_head = list_create())) { eventlog(eventlog_level_error,"gametrans_load","could not create list"); return -1; } if (!(fp = fopen(filename,"r"))) { eventlog(eventlog_level_error,"gametrans_load","could not open file \"%s\" for reading (fopen: %s)",filename,strerror(errno)); list_destroy(gametrans_head); gametrans_head = NULL; return -1; } for (line=1; (buff = file_get_line(fp)); line++) { for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++); if (buff[pos]=='\0' || buff[pos]=='#') { free(buff); continue; } if ((temp = strrchr(buff,'#'))) { unsigned int len; unsigned int endpos; *temp = '\0'; len = strlen(buff)+1; for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--); buff[endpos+1] = '\0'; } if (!(viewer = strtok(buff," \t"))) /* strtok modifies the string it is passed */ { eventlog(eventlog_level_error,"gametrans_load","missing viewer on line %u of file \"%s\"",line,filename); free(buff); continue; } if (!(client = strtok(NULL," \t"))) { eventlog(eventlog_level_error,"gametrans_load","missing client on line %u of file \"%s\"",line,filename); free(buff); continue; } if (!(output = strtok(NULL," \t"))) { eventlog(eventlog_level_error,"gametrans_load","missing output on line %u of file \"%s\"",line,filename); free(buff); continue; } if (!(exclude = strtok(NULL," \t"))) exclude = "0.0.0.0/0"; /* no excluded network address */ if (!(entry = malloc(sizeof(t_gametrans)))) { eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for entry"); free(buff); continue; } if (!(entry->viewer = addr_create_str(viewer,0,0))) { eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for viewer address"); free(entry); free(buff); continue; } if (!(entry->client = addr_create_str(client,0,6112))) { eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for client address"); addr_destroy(entry->viewer); free(entry); free(buff); continue; } if (!(entry->output = addr_create_str(output,0,6112))) { eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for output address"); addr_destroy(entry->client); addr_destroy(entry->viewer); free(entry); free(buff); continue; } if (!(entry->exclude = netaddr_create_str(exclude))) { eventlog(eventlog_level_error,"gametrans_load","could not allocate memory for exclude address"); addr_destroy(entry->output); addr_destroy(entry->client); addr_destroy(entry->viewer); free(entry); free(buff); continue; } free(buff); if (list_append_data(gametrans_head,entry)<0) { eventlog(eventlog_level_error,"gametrans_load","could not append item"); netaddr_destroy(entry->exclude); addr_destroy(entry->output); addr_destroy(entry->client); addr_destroy(entry->viewer); free(entry); } } if (fclose(fp)<0) eventlog(eventlog_level_error,"gametrans_load","could not close gametrans file \"%s\" after reading (fclose: %s)",filename,strerror(errno)); return 0; }
extern int describe_command(t_connection * c, char const * cmd) { char * line; int i; /* ok. the client requested help for a specific command */ std::rewind(hfd); while ((line = file_get_line(hfd)) != NULL) { for (i = 0; line[i] == ' ' && line[i] != '\0'; i++); /* skip spaces in front of %command */ if (line[i] == '%') /* is this a command ? */ { char *p; int al; /* ok. now we must see if there are any aliases */ p = line + i; do { al = 0; for (i = 1; p[i] != ' ' && p[i] != '\0' && p[i] != '#'; i++); /* skip command */ if (p[i] == ' ') al = 1; /* we have something after the command.. must remember that */ p[i] = '\0'; /* end the string at the end of the command */ if (strcasecmp(cmd, p + 1) == 0) /* is this the command the user asked for help ? */ { while ((line = file_get_line(hfd)) != NULL) { /* write everything until we get another % or EOF */ for (i = 0; line[i] == ' '; i++); /* skip spaces in front of a possible % */ if (line[i] == '%') { break; /* we reached another command */ } if (line[0] != '#' && line[i] != '\0') { /* is this a whole line comment ? */ /* truncate the line when a comment starts */ for (; line[i] != '\0' && line[i] != '#'; i++); if (line[i] == '#') line[i] = '\0'; // replace tabs with 3 spaces line = (char*)str_replace(line, "\t", " "); // if text starts with slash then make it colored int j = 0; for (; line[j] == ' ' || line[j] == '\t'; j++); if (line[j] == '/') message_send_text(c, message_type_error, c, line); else message_send_text(c, message_type_info, c, line); } } return 0; } if (al) { for (; p[i + 1] == ' ' && p[i + 1] != '\0'; i++); /* skip spaces */ if (p[i + 1] == '\0') { al = 0; continue; } p += i; /* jump to the next command */ } } while (al); } } file_get_line(NULL); // clear file_get_line buffer /* no description was found for this command. inform the user */ message_send_text(c, message_type_error, c, "No help available for that command"); return -1; }
extern int versioncheck_load(char const * filename) { std::FILE * fp; unsigned int line; unsigned int pos; char * buff; char * temp; char const * eqn; char const * mpqfile; char const * archtag; char const * clienttag; char const * exeinfo; char const * versionid; char const * gameversion; char const * checksum; char const * versiontag; t_versioninfo * vi; if (!filename) { eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); return -1; } if (!(versioninfo_head = list_create())) { eventlog(eventlog_level_error, __FUNCTION__, "could create list"); return -1; } if (!(fp = std::fopen(filename, "r"))) { eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"%s\" for reading (std::fopen: %s)", filename, std::strerror(errno)); list_destroy(versioninfo_head); versioninfo_head = NULL; return -1; } line = 1; for (; (buff = file_get_line(fp)); line++) { for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++); if (buff[pos] == '\0' || buff[pos] == '#') { continue; } if ((temp = std::strrchr(buff, '#'))) { unsigned int len; unsigned int endpos; *temp = '\0'; len = std::strlen(buff) + 1; for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--); buff[endpos + 1] = '\0'; } if (!(eqn = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing eqn near line %u of file \"%s\"", line, filename); continue; } line++; if (!(mpqfile = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing mpqfile near line %u of file \"%s\"", line, filename); continue; } line++; if (!(archtag = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing archtag near line %u of file \"%s\"", line, filename); continue; } line++; if (!(clienttag = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing clienttag near line %u of file \"%s\"", line, filename); continue; } line++; if (!(exeinfo = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing exeinfo near line %u of file \"%s\"", line, filename); continue; } line++; if (!(versionid = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing versionid near line %u of file \"%s\"", line, filename); continue; } line++; if (!(gameversion = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing gameversion near line %u of file \"%s\"", line, filename); continue; } line++; if (!(checksum = next_token(buff, &pos))) { eventlog(eventlog_level_error, __FUNCTION__, "missing checksum near line %u of file \"%s\"", line, filename); continue; } line++; if (!(versiontag = next_token(buff, &pos))) { versiontag = NULL; } vi = (t_versioninfo*)xmalloc(sizeof(t_versioninfo)); vi->eqn = xstrdup(eqn); vi->mpqfile = xstrdup(mpqfile); if (std::strlen(archtag) != 4) { eventlog(eventlog_level_error, __FUNCTION__, "invalid arch tag on line %u of file \"%s\"", line, filename); xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } if (!tag_check_arch((vi->archtag = tag_str_to_uint(archtag)))) { eventlog(eventlog_level_error, __FUNCTION__, "got unknown archtag \"%s\"", archtag); xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } if (std::strlen(clienttag) != 4) { eventlog(eventlog_level_error, __FUNCTION__, "invalid client tag on line %u of file \"%s\"", line, filename); xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } if (!tag_check_client((vi->clienttag = tag_str_to_uint(clienttag)))) { eventlog(eventlog_level_error, __FUNCTION__, "got unknown clienttag\"%s\"", clienttag); xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } if (std::strcmp(exeinfo, "NULL") == 0) vi->parsed_exeinfo = NULL; else { if (!(vi->parsed_exeinfo = parse_exeinfo(exeinfo))) { eventlog(eventlog_level_error, __FUNCTION__, "encountered an error while parsing exeinfo"); xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } } vi->versionid = std::strtoul(versionid, NULL, 0); if (verstr_to_vernum(gameversion, &vi->gameversion) < 0) { eventlog(eventlog_level_error, __FUNCTION__, "malformed version on line %u of file \"%s\"", line, filename); xfree((void *)vi->parsed_exeinfo); /* avoid warning */ xfree((void *)vi->mpqfile); /* avoid warning */ xfree((void *)vi->eqn); /* avoid warning */ xfree(vi); continue; } vi->checksum = std::strtoul(checksum, NULL, 0); if (versiontag) vi->versiontag = xstrdup(versiontag); else vi->versiontag = NULL; list_append_data(versioninfo_head, vi); } file_get_line(NULL); // clear file_get_line buffer if (std::fclose(fp) < 0) eventlog(eventlog_level_error, __FUNCTION__, "could not close versioncheck file \"%s\" after reading (std::fclose: %s)", filename, std::strerror(errno)); return 0; }
extern int autoupdate_load(char const * filename) { unsigned int line; unsigned int pos; char * buff; char * temp; char const * archtag; char const * clienttag; char const * mpqfile; char const * versiontag; t_autoupdate * entry; if (!filename) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename"); return -1; } if (!(fp = fopen(filename,"r"))) { eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno)); return -1; } autoupdate_head = list_create(); for (line=1; (buff = file_get_line(fp)); line++) { for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++); if (buff[pos]=='\0' || buff[pos]=='#') { continue; } if ((temp = strrchr(buff,'#'))) { unsigned int len; unsigned int endpos; *temp = '\0'; len = strlen(buff)+1; for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--); buff[endpos+1] = '\0'; } /* FIXME: use next_token instead of strtok */ if (!(archtag = strtok(buff, " \t"))) { /* strtok modifies the string it is passed */ eventlog(eventlog_level_error,__FUNCTION__,"missing archtag on line %u of file \"%s\"",line,filename); continue; } if (!(clienttag = strtok(NULL," \t"))) { eventlog(eventlog_level_error,__FUNCTION__,"missing clienttag on line %u of file \"%s\"",line,filename); continue; } if (!(versiontag = strtok(NULL, " \t"))) { eventlog(eventlog_level_error,__FUNCTION__,"missing versiontag on line %u of file \"%s\"",line,filename); continue; } if (!(mpqfile = strtok(NULL," \t"))) { eventlog(eventlog_level_error,__FUNCTION__,"missing mpqfile on line %u of file \"%s\"",line,filename); continue; } entry = (t_autoupdate*)xmalloc(sizeof(t_autoupdate)); if (!tag_check_arch((entry->archtag = tag_str_to_uint(archtag)))) { eventlog(eventlog_level_error,__FUNCTION__,"got unknown archtag"); xfree(entry); continue; } if (!tag_check_client((entry->clienttag = tag_str_to_uint(clienttag)))) { eventlog(eventlog_level_error,__FUNCTION__,"got unknown clienttag"); xfree(entry); continue; } entry->versiontag = xstrdup(versiontag); entry->mpqfile = xstrdup(mpqfile); eventlog(eventlog_level_debug,__FUNCTION__,"update '%s' version '%s' with file %s",clienttag,versiontag,mpqfile); list_append_data(autoupdate_head,entry); } file_get_line(NULL); // clear file_get_line buffer fclose(fp); return 0; }
extern int conf_load_file(std::FILE *fd, t_conf_entry *conftab) { char *buff, *directive, *value, *cp; unsigned cflag, lineno; if (!fd) { eventlog(eventlog_level_error, __FUNCTION__, "got NULL file"); return -1; } if (!conftab) { eventlog(eventlog_level_error, __FUNCTION__, "got NULL conftab"); return -1; } /* restore defaults */ _conf_reset_defaults(conftab); for (lineno = 1; (buff = file_get_line(fd)); lineno++) { cflag = 1; for (cp = buff; *cp; cp++) { switch (*cp) { case '\\': if (!*(++cp)) cflag = 0; break; case '"': switch (cflag) { case 1: cflag = 2; break; case 2: cflag = 1; break; } break; case '#': if (cflag == 1) cflag = 0; break; } if (!cflag) break; } if (*cp == '#') *cp = '\0'; cp = str_skip_space(buff); if (!*cp) continue; directive = cp; cp = str_skip_word(cp + 1); if (*cp) *(cp++) = '\0'; cp = str_skip_space(cp); if (*cp != '=') { eventlog(eventlog_level_error, __FUNCTION__, "missing = on line %u", lineno); continue; } cp = str_skip_space(cp + 1); if (!*cp) { eventlog(eventlog_level_error, __FUNCTION__, "missing value at line %u", lineno); continue; } value = _get_value(cp, lineno); if (!value) continue; _process_option(directive, value, conftab); } return 0; }
extern int irc_welcome(t_connection * conn) { char temp[MAX_IRC_MESSAGE_LEN]; time_t temptime; char const * tempname; char const * temptimestr; char const * filename; FILE *fp; char * line, * formatted_line; char send_line[MAX_IRC_MESSAGE_LEN]; char motd_failed = 0; if (!conn) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return -1; } tempname = conn_get_loggeduser(conn); if ((34+strlen(tempname)+1)<=MAX_IRC_MESSAGE_LEN) sprintf(temp,":Welcome to the %s IRC Network %s",prefs_get_irc_network_name(), tempname); else sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_WELCOME,temp); if ((14+strlen(server_get_hostname())+10+strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+1)<=MAX_IRC_MESSAGE_LEN) sprintf(temp,":Your host is %s, running "PVPGN_SOFTWARE" "PVPGN_VERSION,server_get_hostname()); else sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_YOURHOST,temp); temptime = server_get_starttime(); /* FIXME: This should be build time */ temptimestr = ctime(&temptime); if ((25+strlen(temptimestr)+1)<=MAX_IRC_MESSAGE_LEN) sprintf(temp,":This server was created %s",temptimestr); /* FIXME: is ctime() portable? */ else sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_CREATED,temp); /* we don't give mode information on MYINFO we give it on ISUPPORT */ if ((strlen(server_get_hostname())+7+strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+9+1)<=MAX_IRC_MESSAGE_LEN) sprintf(temp,"%s "PVPGN_SOFTWARE" "PVPGN_VERSION" - -",server_get_hostname()); else sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_MYINFO,temp); if((conn_get_wol(conn) == 1)) sprintf(temp,"NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX="CHANNEL_PREFIX" CHANTYPES="CHANNEL_TYPE" NETWORK=%s IRCD="PVPGN_SOFTWARE, WOL_NICKNAME_LEN, MAX_TOPIC_LEN, CHANNEL_NAME_LEN, prefs_get_irc_network_name()); else sprintf(temp,"NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX="CHANNEL_PREFIX" CHANTYPES="CHANNEL_TYPE" NETWORK=%s IRCD="PVPGN_SOFTWARE, CHAR_NAME_LEN, MAX_TOPIC_LEN, CHANNEL_NAME_LEN, prefs_get_irc_network_name()); if((strlen(temp))<=MAX_IRC_MESSAGE_LEN) irc_send(conn,RPL_ISUPPORT,temp); else { sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_ISUPPORT,temp); } if ((3+strlen(server_get_hostname())+22+1)<=MAX_IRC_MESSAGE_LEN) sprintf(temp,":- %s, "PVPGN_SOFTWARE" "PVPGN_VERSION", built on %s",server_get_hostname(),temptimestr); else sprintf(temp,":Maximum length exceeded"); irc_send(conn,RPL_MOTDSTART,temp); if ((filename = prefs_get_motdfile())) { if ((fp = fopen(filename,"r"))) { while ((line=file_get_line(fp))) { if ((formatted_line = message_format_line(conn,line))) { formatted_line[0]=' '; sprintf(send_line,":-%s",formatted_line); irc_send(conn,RPL_MOTD,send_line); xfree(formatted_line); } } file_get_line(NULL); // clear file_get_line buffer fclose(fp); } else motd_failed = 1; } else motd_failed = 1; if (motd_failed) { irc_send(conn,RPL_MOTD,":- Failed to load motd, sending default motd "); irc_send(conn,RPL_MOTD,":- ====================================================== "); irc_send(conn,RPL_MOTD,":- http://www.pvpgn.org "); irc_send(conn,RPL_MOTD,":- ====================================================== "); } irc_send(conn,RPL_ENDOFMOTD,":End of /MOTD command"); irc_send_cmd(conn,"NOTICE",":This is an experimental service."); conn_set_state(conn,conn_state_bot_password); if (connlist_find_connection_by_accountname(conn_get_loggeduser(conn))) { irc_send_cmd(conn,"NOTICE","This account is already logged in, use another account."); return -1; } if (conn_get_ircpass(conn)) { irc_send_cmd(conn,"NOTICE",":Trying to authenticate with PASS ..."); irc_authenticate(conn,conn_get_ircpass(conn)); } else { irc_send_cmd(conn,"NOTICE",":No PASS command received. Please identify yourself by /msg NICKSERV identify <password>."); } return 0; }