int price_valid(float num, char endChk, int p) { /*---Local Variable---*/ int i; /*flag for exit of menu in function main*/ /*---Error Conditions---*/ if(p==0) { i=0; printf("Error Input ***Must be numeric***\n"); bufferClear(); } else if(endChk!='\n') { i=0; printf("Error Input ***Trailing characters detected***\n"); bufferClear(); } else if(p<0) { i=0; printf("Error Input ***Value must be non-negative***\n"); } else if(p==2) i=1; return i; }
bool protocolUpdate(int pSocket, fd_set *pSocketSet, int pMaxSocket, int pBufferSize) { UNUSED(pMaxSocket); bufferGrow(&gMessageBuffer, pBufferSize); bufferClear(&gMessageBuffer); infof("Communication on socket %d.\n", pSocket); // send(), recv(), close() // Call FD_CLR(pSocket, pSocketSet) on disconnection bool success = true; int messageSize; int flags = 0; // Receive a message from client messageSize = recv(pSocket, gMessageBuffer->buffer, gMessageBuffer->size, flags); if (0 < messageSize) { // Return the message to the sender networkMessagePrivate(pSocket, gMessageBuffer->buffer); } else if (0 == messageSize) { close(pSocket); FD_CLR(pSocket, pSocketSet); noticef("Client disconnected from socket %d.\n", pSocket); fflush(stdout); } else if (messageSize < 0) { warningf("Failed to receive data from socket %d.", pSocket); success = false; } return success; }
bool flush_output(SOCKET_DATA *dsock) { bool success = TRUE; BUFFER *buf = NULL; // run any hooks prior to flushing our text hookRun("flush", hookBuildInfo("sk", dsock)); // quit if we have no output and don't need/can't have a prompt if(bufferLength(dsock->outbuf) <= 0 && (!dsock->bust_prompt || !socketHasPrompt(dsock))) return success; buf = newBuffer(1); // send our outbound text if(bufferLength(dsock->outbuf) > 0) { hookRun("process_outbound_text", hookBuildInfo("sk", dsock)); hookRun("finalize_outbound_text", hookBuildInfo("sk", dsock)); //success = text_to_socket(dsock, bufferString(dsock->outbuf)); bufferCat(buf, bufferString(dsock->outbuf)); bufferClear(dsock->outbuf); } // send our prompt if(dsock->bust_prompt && success) { socketShowPrompt(dsock); hookRun("process_outbound_prompt", hookBuildInfo("sk", dsock)); hookRun("finalize_outbound_prompt", hookBuildInfo("sk", dsock)); //success = text_to_socket(dsock, bufferString(dsock->outbuf)); bufferCat(buf, bufferString(dsock->outbuf)); bufferClear(dsock->outbuf); dsock->bust_prompt = FALSE; } success = text_to_socket(dsock, bufferString(buf)); deleteBuffer(buf); // return our success return success; }
void next_cmd_from_buffer(SOCKET_DATA *dsock) { // do we have stuff in our input list? If so, use that instead of inbuf dsock->cmd_read = FALSE; if(listSize(dsock->input) > 0) { char *cmd = listPop(dsock->input); bufferClear(dsock->next_command); bufferCat(dsock->next_command, cmd); dsock->cmd_read = TRUE; dsock->bust_prompt = TRUE; free(cmd); } else { int i = 0, cmd_end = -1; // are we building an IAC command? Try to continue it if(bufferLength(dsock->iac_sequence) > 0) i += read_iac_sequence(dsock, 0); // copy over characters until we hit a newline, an IAC command, or \0 for(; dsock->inbuf[i] != '\0' && cmd_end < 0; i++) { switch(dsock->inbuf[i]) { default: // append us to the command bufferCatCh(dsock->next_command, dsock->inbuf[i]); break; case '\n': // command end found cmd_end = ++i; case '\r': // ignore \r ... only pay attention to \n break; case (signed char) IAC: i += read_iac_sequence(dsock, i) - 1; break; } if(cmd_end >= 0) break; } // move the context of inbuf down int begin = 0; while(dsock->inbuf[i] != '\0') dsock->inbuf[begin++] = dsock->inbuf[i++]; dsock->inbuf[begin] = '\0'; // did we find a command? if(cmd_end >= 0) { dsock->cmd_read = TRUE; dsock->bust_prompt = TRUE; } } }
int PySocket_set_outbound_text(PySocket *self, PyObject *value, void *closure) { if(!PyString_Check(value)) { PyErr_Format(PyExc_TypeError, "Outbound text must be in string format."); return -1; } SOCKET_DATA *sock = PySocket_AsSocket((PyObject *)self); if(sock == NULL) return -1; else { bufferClear(socketGetOutbound(sock)); bufferCat(socketGetOutbound(sock), PyString_AsString(value)); return 0; } }
void resetSetArg (RESET_DATA *reset, const char *arg) { bufferClear(reset->arg); bufferCat(reset->arg, arg); }
void input_handler() { LIST_ITERATOR *sock_i = newListIterator(socket_list); SOCKET_DATA *sock = NULL; ITERATE_LIST(sock, sock_i) { // Close sockects we are unable to read from, or if we have no handler // to take in input if ((FD_ISSET(sock->control, &rFd) && !read_from_socket(sock)) || listSize(sock->input_handlers) == 0) { close_socket(sock, FALSE); continue; } /* Ok, check for a new command */ next_cmd_from_buffer(sock); // are we idling? if(!sock->cmd_read) sock->idle += 1.0 / PULSES_PER_SECOND; /* Is there a new command pending ? */ else if (sock->cmd_read) { sock->idle = 0.0; IH_PAIR *pair = listGet(sock->input_handlers, 0); if(pair->python == FALSE) { void (* handler)(SOCKET_DATA *, char *) = pair->handler; char *cmddup = strdup(bufferString(sock->next_command)); handler(sock, cmddup); free(cmddup); } else { PyObject *arglist = Py_BuildValue("Os", socketGetPyFormBorrowed(sock), bufferString(sock->next_command)); PyObject *retval = PyEval_CallObject(pair->handler, arglist); // check for an error: if(retval == NULL) log_pyerr("Error with a Python input handler"); // garbage collection Py_XDECREF(retval); Py_XDECREF(arglist); } // append our last command to the command history. History buffer is // 100 commands, so pop off the earliest command if we're going over listPut(sock->command_hist, strdup(bufferString(sock->next_command))); if(listSize(sock->command_hist) > 100) free(listRemoveNum(sock->command_hist, 100)); bufferClear(sock->next_command); // we save whether or not we read a command until our next call to // input_handler(), at which time it is reset to FALSE if we didn't read // sock->cmd_read = FALSE; } #ifdef MODULE_ALIAS // ACK!! this is so yucky, but I can't think of a better way to do it... // if this command was put in place by an alias, decrement the alias_queue // counter by one. This counter is in place mainly so aliases do not end // up calling eachother and making us get stuck in an infinite loop. if(sock->player) { int alias_queue = charGetAliasesQueued(sock->player); if(alias_queue > 0) charSetAliasesQueued(sock->player, --alias_queue); } #endif } deleteListIterator(sock_i); }
// // read an IAC sequence from the socket's input buffer. When we hit a // termination point, send a hook and clear he buffer. Return how many // characters to skip ahead in the input handler int read_iac_sequence(SOCKET_DATA *dsock, int start) { // are we doing subnegotiation? bool subneg = strchr(bufferString(dsock->iac_sequence), SB) != NULL; int i = start; bool done = FALSE; for(; dsock->inbuf[i] != '\0'; i++) { // are we looking for IAC? if(bufferLength(dsock->iac_sequence) == 0) { // Oops! Something is broken if(dsock->inbuf[i] != (signed char) IAC) return 0; else bufferCatCh(dsock->iac_sequence, IAC); } // are we looking for a command? else if(bufferLength(dsock->iac_sequence) == 1) { // did we find subnegotiation? if(dsock->inbuf[i] == (signed char) SB) { bufferCatCh(dsock->iac_sequence, dsock->inbuf[i]); subneg = TRUE; } // basic three-character command else if(dsock->inbuf[i] == (signed char) WILL || dsock->inbuf[i] == (signed char) WONT || dsock->inbuf[i] == (signed char) DO || dsock->inbuf[i] == (signed char) DONT) bufferCatCh(dsock->iac_sequence, dsock->inbuf[i]); // something went wrong else { bufferClear(dsock->iac_sequence); return 2; } } // are we doing subnegotiation? else if(subneg) { int last_i = bufferLength(dsock->iac_sequence) - 2; signed char last = bufferString(dsock->iac_sequence)[last_i]; bufferCatCh(dsock->iac_sequence, dsock->inbuf[i]); // have hit the end of the subnegotiation? Break out if so if(last == (signed char) IAC && dsock->inbuf[i] == (signed char) SE) { done = TRUE; break; } } // this is the end else { bufferCatCh(dsock->iac_sequence, dsock->inbuf[i]); done = TRUE; break; } } int len = i - start + (dsock->inbuf[i] == '\0' ? 0 : 1); // broadcast the message we parsed, and prepare for the next sequence if(done == TRUE) { hookRun("receive_iac", hookBuildInfo("sk str", dsock,bufferString(dsock->iac_sequence))); bufferClear(dsock->iac_sequence); } return len; }
void expand_dynamic_descs_dict(BUFFER *desc, PyObject *dict,const char *locale){ // make our new buffer BUFFER *new_desc = newBuffer(bufferLength(desc)*2); BUFFER *code = newBuffer(1); // for things we have to evaluate bool proc_cond = FALSE; // are we processing a conditional statement? // copy over all of our description that is not dynamic text int start, end, i, j, size = bufferLength(desc); for(i = 0; i < size; i++) { // figure out when our next dynamic desc is. start = next_letter_in(bufferString(desc) + i, '['); // no more if(start == -1) { // copy the rest and skip to the end of the buffer bprintf(new_desc, "%s", bufferString(desc) + i); i = size - 1; } // we have another desc else { // copy everything up to start while(start > 0) { bprintf(new_desc, "%c", *(bufferString(desc) + i)); start--; i++; } // skip the start marker i++; // find our end end = next_letter_in(bufferString(desc) + i, ']'); // make sure we have it if(end == -1) break; // copy everything between start and end, and format the code bufferClear(code); for(j = 0; j < end; j++) bprintf(code, "%c", *(bufferString(desc) + i + j)); bufferReplace(code, "\n", " ", TRUE); bufferReplace(code, "\r", "", TRUE); // skip i up to the end i = i + end; // are we trying to process a conditional statement? if(!strncasecmp(bufferString(code), "if ", 3)) { // strip out the leading if and whitespace char *code_copy = strdup(bufferString(code)); char *ptr = code_copy + 3; while(isspace(*ptr)) ptr++; // copy over the cleaned-up code, signal we're processing a conditional bufferClear(code); bufferCat(code, ptr); proc_cond = TRUE; // garbage collection free(code_copy); } // evaluate the code PyObject *retval = eval_script(dict, bufferString(code), locale); // did we encounter an error? if(retval == NULL) break; // are we evaluating a conditional or no? else if(proc_cond) { BUFFER *cond_retval = expand_dynamic_conditional(desc, dict, retval, locale, &i); // if we have something to print, expand its embedded python if(*bufferString(cond_retval)) expand_dynamic_descs_dict(cond_retval, dict, locale); bufferCat(new_desc, bufferString(cond_retval)); deleteBuffer(cond_retval); proc_cond = FALSE; } // append the output else if(PyString_Check(retval)) bprintf(new_desc, "%s", PyString_AsString(retval)); else if(PyInt_Check(retval)) bprintf(new_desc, "%ld", PyInt_AsLong(retval)); else if(PyFloat_Check(retval)) bprintf(new_desc, "%lf", PyFloat_AsDouble(retval)); // invalid return type... else if(retval != Py_None) log_string("dynamic desc had invalid evaluation: %s", bufferString(code)); // oddly, freeing retval here corrupt memory. // And not freeing it doesn't cause a memory leak. So bizarre... // Py_XDECREF(retval); } } // copy over our contents bufferCopyTo(new_desc, desc); // garbage collection deleteBuffer(code); deleteBuffer(new_desc); }
// // Tries to expand out a conditional statement. Returns whatever is expanded. // Moves i to the proper place after the conditional has been expanded in the // originating buffer. BUFFER *expand_dynamic_conditional(BUFFER *desc, PyObject *dict, PyObject *retval, const char *locale, int *pos) { BUFFER *new_desc = newBuffer(bufferLength(desc)*2); BUFFER *code = newBuffer(1); // code we have to evaluate int j, i = *pos + 1; // +1 to skip the current closing ] bool match = PyObject_IsTrue(retval); while(TRUE) { // copy over all of our contents for the new desc and increment i while(*(bufferString(desc)+i) && !startswith(bufferString(desc)+i, "[else]") && !startswith(bufferString(desc)+i, "[elif ") && !startswith(bufferString(desc)+i, "[/if]")) { if(match == TRUE) bprintf(new_desc, "%c", *(bufferString(desc) + i)); i++; } // did we terminate? if(match == TRUE || startswith(bufferString(desc)+i, "[/if]")) break; // we haven't had a match yet. Are we trying an else or elif? else if(startswith(bufferString(desc)+i, "[else]")) { match = TRUE; i += 6; } else if(startswith(bufferString(desc)+i, "[elif ")) { // skip the elif and spaces i += 6; while(isspace(*(bufferString(desc)+i))) i++; // find our end int end = next_letter_in(bufferString(desc) + i, ']'); // make sure we have it if(end == -1) break; // copy everything between start and end, and format the code bufferClear(code); for(j = 0; j < end; j++) bprintf(code, "%c", *(bufferString(desc) + i + j)); bufferReplace(code, "\n", "", TRUE); // skip i up to and beyond the ] marker i = i + end + 1; // evaluate the code PyObject *retval = eval_script(dict, bufferString(code), locale); match = retval && PyObject_IsTrue(retval); // did we encounter an error? if(retval == NULL) break; } } // skip everything up to our closing [/if] while(*(bufferString(desc)+i) && !startswith(bufferString(desc)+i, "[/if]")) i++; if(startswith(bufferString(desc)+i, "[/if]")) i += 4; // put us at the closing ], not the end of the ending if block // garbage collection deleteBuffer(code); // return our expansion and move our i *pos = i; return new_desc; }