/* generate a CASE statment */ void bash_case (buffer_t * buf, char *str, size_t len) { static char err_msg[] = "echo 'error: missing expression for case'\nexit 99\n"; static char case_start[] = "case "; static char case_end[] = " in\n"; /* create a bogus case condition, nul+esc+eof, so nl+;;+nl can be prepended to each when/otherwise/endcase, which eliminates the need for ;; or <% ;; %> in the page source */ static char case_bogus[] = "\"\\000\\040\\004\") :\n"; if (len == 0) { buffer_add (buf, err_msg, strlen (err_msg)); } else { buffer_add (buf, case_start, strlen (case_start)); buffer_add (buf, str, len); buffer_add (buf, case_end, strlen (case_end)); buffer_add (buf, case_bogus, strlen (case_bogus)); } }
void subshell_translate(buffer_t* buf, char* str, size_t len) { static char echo_start[] = "echo -n \""; static char echo_end[] = "\"\n"; short hash; lstr* i; char* text = NULL; if(len == 0) { return; } str = trim(str); if(!*str) { return; } if(language != NULL && translations > 0) { hash = generateHash(str); i = ltable[hash]; while(text == NULL && i != NULL) { if(strcmp(str, i->msgid) == 0) { text = i->msgstr; } else { i = i->next; } } } if(text == NULL) { text = str; } buffer_add(buf, echo_start, strlen(echo_start)); buffer_add(buf, text, strlen(text)); buffer_add(buf, echo_end, strlen(echo_end)); }
/* Run the echo command in a subshell */ void lua_echo (buffer_t * buf, char *str, size_t len) { static char echo_start[] = " io.write("; char quote[200] = "]=]"; /* 197 nested comments is a problem */ if (len == 0) return; /* figure out if the string uses ]] ]=] ]==] etc in it */ while ((strstr (str, quote)) && (strlen (quote) < 198)) { memmove (quote + strlen (quote) - 1, quote + strlen (quote) - 2, 3); } /* As of 5.1, nested comments are depreciated... sigh */ quote[0] = '['; quote[strlen (quote) - 1] = quote[0]; while ((strstr (str, quote)) && (strlen (quote) < 198)) { memmove (quote + strlen (quote) - 1, quote + strlen (quote) - 2, 3); } buffer_add (buf, echo_start, strlen (echo_start)); buffer_add (buf, quote, strlen (quote)); buffer_add (buf, str, len); quote[0] = ']'; quote[strlen (quote) - 1] = quote[0]; buffer_add (buf, quote, strlen (quote)); buffer_add (buf, ")\n", 2); }
/** * Main function for this unit test * * * @return 0 if test passed */ int main() { t_buffer *buffer; t_buffer buffer2; buffer = buffer_create(NULL); XTEST(buffer != NULL); XTEST(buffer_add(buffer, "123456789", 9) == 9); check_buf(buffer, 123456789); buffer_erase(buffer, buffer_size(buffer)); XTEST(buffer_add(buffer, "1", 1) == 1); check_buf(buffer, 1); buffer_erase(buffer, buffer_size(buffer)); XTEST(buffer_add(buffer, "-12345678", 9) == 9); check_buf(buffer, -12345678); buffer_erase(buffer, buffer_size(buffer)); buffer_destroy(buffer); strtobuffer(&buffer2, "987654321"); check_buf(&buffer2, 987654321); strtobuffer(&buffer2, "0"); check_buf(&buffer2, 0); strtobuffer(&buffer2, "-987654321"); check_buf(&buffer2, -987654321); XPASS(); }
char * http_encode_uri(const char *uri, size_t len, int space_as_plus) { struct buffer *buf = buffer_new(); if (buf == NULL) return (NULL); const char *p, *end; char *result; if (len >= 0) end = uri+len; else end = uri+strlen(uri); for (p = uri; p < end; p++) { if (CHAR_IS_UNRESERVED(*p)) { buffer_add(buf, p, 1); } else if (*p == ' ' && space_as_plus) { buffer_add(buf, "+", 1); } else { buffer_add_printf(buf, "%%%02X", (unsigned char)(*p)); } } buffer_add(buf, "", 1); /* NUL-terminator. */ result = malloc(buf->len); if (!result) return NULL; buffer_remove(buf, result, buf->len); buffer_free(buf); return (result); }
void pcp_keysig2blob(Buffer *b, pcp_keysig_t *s) { buffer_add8(b, s->type); buffer_add32be(b, s->size); buffer_add(b, s->id, 17); buffer_add(b, s->checksum, LSHA); buffer_add(b, s->blob, s->size); }
/* do an evaluation */ void lua_eval (buffer_t * buf, char *str, size_t len) { static char start[] = " io.write(tostring("; static char end[] = "))\n"; if (len == 0) return; buffer_add (buf, start, strlen (start)); buffer_add (buf, str, len); buffer_add (buf, end, strlen (end)); }
/* do an evaluation in a subshell */ void bash_eval (buffer_t * buf, char *str, size_t len) { static char echo_start[] = "echo -n "; static char echo_end[] = "\n"; if (len == 0) return; buffer_add (buf, echo_start, strlen (echo_start)); buffer_add (buf, str, len); buffer_add (buf, echo_end, strlen (echo_end)); }
static int oilsAuthLoginVerifyPassword(const osrfMethodContext* ctx, int user_id, const char* username, const char* password) { // build the cache key growing_buffer* gb = buffer_init(64); // free me buffer_add(gb, OILS_AUTH_CACHE_PRFX); buffer_add(gb, username); buffer_add(gb, OILS_AUTH_COUNT_SFFX); char* countkey = buffer_release(gb); // free me jsonObject* countobject = osrfCacheGetObject(countkey); // free me long failcount = 0; if (countobject) { failcount = (long) jsonObjectGetNumber(countobject); if (failcount >= _oilsAuthBlockCount) { // User is blocked. Don't waste any more CPU cycles on them. osrfLogInfo(OSRF_LOG_MARK, "oilsAuth found too many recent failures for '%s' : %i, " "forcing failure state.", username, failcount); jsonObjectFree(countobject); free(countkey); return 0; } } int verified = oilsAuthLoginCheckPassword(user_id, password); if (!verified) { // login failed. increment failure counter. failcount++; if (countobject) { // append to existing counter jsonObjectSetNumber(countobject, failcount); } else { // first failure, create a new counter countobject = jsonNewNumberObject((double) failcount); } osrfCachePutObject(countkey, countobject, _oilsAuthBlockTimeout); } jsonObjectFree(countobject); // NULL OK free(countkey); return verified; }
void subshell_eval(buffer_t* buf, char* str, size_t len) { static char echo_start[] = "echo -n "; static char echo_end[] = "\n"; if(len == 0) { return; } str = trim(str); if(!*str) { return; } buffer_add(buf, echo_start, strlen(echo_start)); buffer_add(buf, str, len); buffer_add(buf, echo_end, strlen(echo_end)); }
static char* load_query( const char* filename ) { FILE* fp; // Sanity check if( ! filename || ! *filename ) { fprintf( stderr, "Name of query file is empty or missing\n" ); return NULL; } // Open query file, or use standard input if( ! strcmp( filename, "-" ) ) fp = stdin; else { fp = fopen( filename, "r" ); if( !fp ) { fprintf( stderr, "Unable to open query file \"%s\"\n", filename ); return NULL; } } // Load file into a growing_buffer size_t num_read; char buf[ BUFSIZ + 1 ]; growing_buffer* gb = buffer_init( sizeof( buf ) ); while( ( num_read = fread( buf, 1, sizeof( buf ) - 1, fp ) ) ) { buf[ num_read ] = '\0'; buffer_add( gb, buf ); } if( fp != stdin ) fclose( fp ); return buffer_release( gb ); }
void *producer_function(void *b) { buffer *buff = (buffer *)b; int i; for (i = 0; i < 100; i++) { pthread_mutex_lock(buff->mut); if(buff->full) { printf("buffer full, waiting...\n"); pthread_cond_wait(buff->notFull, buff->mut); } int random = generate_random_number(RANDOM_NUMBER_UPPER_LIMIT); buffer_add(buff, random); printf("produced %d\n", random); pthread_mutex_unlock(buff->mut); /* Sleep either 1s or 4s (50% chance for each) */ if (generate_random_number(99) < 50) { usleep(1000000); } else { usleep(4000000); } } return NULL; }
static char* _escape_xml (const char* text) { growing_buffer* b = buffer_init(256); int len = strlen(text); int i; for (i = 0; i < len; i++) { if (text[i] == '&') buffer_add(b,"&"); else if (text[i] == '<') buffer_add(b,"<"); else if (text[i] == '>') buffer_add(b,">"); else buffer_add_char(b,text[i]); } return buffer_release(b); }
static void producer_operation(int sfd) { while(status_on) { int peer_sfd = accept(sfd,NULL,NULL); if(peer_sfd<0) { print_log(WARNING,"\nACCEPT FAILED MAY BE BECAUSE OF INTERRUPT"); continue; } if(pthread_mutex_lock(&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is error in lock prod"); while(is_buffer_full()) { if(pthread_cond_wait(&buffer_not_full_cond,&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is error in wait prod"); } buffer_add(peer_sfd); if(pthread_cond_broadcast(&buffer_not_empty_cond)!=0) print_log(WARNING,"\nthere is error in broadcast"); if(pthread_mutex_unlock(&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is erorr in unlock"); } destroy_buffer(); }
static void connection_on_read_callback(uv_stream_t *stream, ssize_t nRead, const uv_buf_t *buf) { Client *client = stream->data; assert(client != NULL); if(nRead == UV_EOF) { // TODO: exit client due to connection closed, probably get consolidated // with below client_free(client); return; } else if(nRead < 0) { // TODO: exit client due to read error client_free(client); return; } buffer_add(client->ReadBuffer, buf->base, (size_t)nRead); client_process_read_buffer(client); Free(buf->base); }
int labelset_add(unsigned char* labelset, unsigned long* labelset_len, const unsigned long labelset_maxlen, const unsigned char* label, const unsigned char label_len) { unsigned char* bufptr; if (labelset_len == NULL) return -1; if (*labelset_len > LABELSETMAXLEN || labelset_maxlen > LABELSETMAXLEN) return -1; if (*labelset_len >= labelset_maxlen || *labelset_len + label_len + 1 > labelset_maxlen) return -1; if (*labelset_len < 3 || labelset_maxlen < 4) return -1; if (label_len > LABELMAXLEN) return -1; labelset[0]++; labelset[*labelset_len] = label_len; bufptr = labelset + *labelset_len + 1; bufptr = buffer_add(bufptr, labelset + labelset_maxlen, label, label_len); if (bufptr == NULL) return -1; if (bufptr - labelset >= labelset_maxlen) return -1; if (bufptr - labelset != *labelset_len + 1 + label_len) return -1; *labelset_len += (1 + label_len); return 0; }
/* Fill buffers from socket based on poll results. */ int buffer_poll(struct pollfd *pfd, struct buffer *in, struct buffer *out) { ssize_t n; if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP)) return (-1); if (pfd->revents & POLLIN) { buffer_ensure(in, BUFSIZ); n = read(pfd->fd, BUFFER_IN(in), BUFFER_FREE(in)); if (n == 0) return (-1); if (n == -1) { if (errno != EINTR && errno != EAGAIN) return (-1); } else buffer_add(in, n); } if (BUFFER_USED(out) > 0 && pfd->revents & POLLOUT) { n = write(pfd->fd, BUFFER_OUT(out), BUFFER_USED(out)); if (n == -1) { if (errno != EINTR && errno != EAGAIN) return (-1); } else buffer_remove(out, n); } return (0); }
/* s is the file descriptor of a client that has * pending data. Turn it back into a struct client * using the lookup table. * Read data up to 256 characters from the socket. * Returns the status of the client, true for alive * or false for dead. */ int client_handle(int s) { client *c; char buf[256]; int r; c = clients[s]; /* Read up to 256 bytes from the client */ r = socket_read(s, buf, 256); if(r == 0) { /* 0 bytes read means the client has disconnected */ client_destroy(s); return 0; } /* Returns whether the buffer has something useful in it */ r = buffer_add(c->buffer, buf, r); if(r) { // printf("Client %d said '%s'\n", s, buffer_get(c->buffer)); parse(s, c); } return 1; }
/** @brief Add a message to an output buffer. @param outbuf Pointer to the output buffer. @param msg Pointer to the message to be added, in the form of a JSON string. Since the output buffer is in the form of a JSON array, prepend a left bracket to the first message, and a comma to subsequent ones. Used only by servers to respond to clients. */ static inline void append_msg( growing_buffer* outbuf, const char* msg ) { if( outbuf && msg ) { char prefix = buffer_length( outbuf ) > 0 ? ',' : '['; buffer_add_char( outbuf, prefix ); buffer_add( outbuf, msg ); } }
/* * Trunk an initial buffer into several pieces upon two separators * Careful returned list must then be free with list_free() */ list *list_explode_start_end(char separator_start, char separator_end, buffer * value) { list *l; size_t i; buffer *buf; assert(value); l = list_init(); /* If first char doesn't match separator, list contains only one element */ if (value->buf[0] != separator_start) { list_add_by_copy(l, value); return l; } buf = buffer_init(); for (i = 1 ; i < value->use ; i++) if (value->buf[i] == separator_end) { list_add(l, buf); } else if (value->buf[i] != separator_start) { buffer_add(buf, value->buf[i]); } else /* separator_start */ { buf = buffer_init(); } return l; }
/* Store an 8-bit value. */ void buffer_write8(struct buffer *b, uint8_t n) { buffer_ensure(b, 1); BUFFER_IN(b)[0] = n; buffer_add(b, 1); }
/* Write a line to the io write buffer from a va_list. */ void io_vwriteline(struct io *io, const char *fmt, va_list ap) { int n; va_list aq; if (io->error != NULL) return; IO_DEBUG(io, "in: wr: used=%zu, free=%zu", BUFFER_USED(io->wr), BUFFER_FREE(io->wr)); if (fmt != NULL) { va_copy(aq, ap); n = xvsnprintf(NULL, 0, fmt, aq); va_end(aq); buffer_ensure(io->wr, n + 1); xvsnprintf(BUFFER_IN(io->wr), n + 1, fmt, ap); buffer_add(io->wr, n); } else n = 0; io_write(io, io->eol, strlen(io->eol)); IO_DEBUG(io, "out: %zu bytes, wr: used=%zu, free=%zu", n + strlen(io->eol), BUFFER_USED(io->wr), BUFFER_FREE(io->wr)); }
/* Send data, if all of it couldn't be sent immediately, it will be resent automatically after a while. Returns -1 if some unrecoverable error occured. */ int net_sendbuffer_send(NET_SENDBUF_REC *rec, const void *data, int size) { int ret; g_return_val_if_fail(rec != NULL, -1); g_return_val_if_fail(data != NULL, -1); if (size <= 0) return 0; if (rec->buffer == NULL) { /* nothing in buffer - transmit immediately */ ret = net_transmit(rec->handle, data, size); if (ret < 0) return -1; size -= ret; data = ((const char *) data) + ret; } if (size <= 0) return 0; /* everything couldn't be sent. */ if (rec->send_tag == -1) { rec->send_tag = g_input_add(rec->handle, G_INPUT_WRITE, (GInputFunction) sig_sendbuffer, rec); } return buffer_add(rec, data, size) ? 0 : -1; }
void ekg_debug_handler(int level, const char *format, va_list ap) { static GString *line = NULL; char *tmp = NULL; char *theme_format; int is_UI = 0; if (!config_debug) return; if (line) { g_string_append_vprintf(line, format, ap); if (line->len == 0 || line->str[line->len - 1] != '\n') return; line->str[line->len - 1] = '\0'; /* remove '\n' */ tmp = g_string_free(line, FALSE); line = NULL; } else { int tmplen = g_vasprintf(&tmp, format, ap); if (tmplen < 0 || !tmp) /* OutOfMemory? */ return; if (tmplen == 0 || tmp[tmplen - 1] != '\n') { line = g_string_new_len(tmp, tmplen); g_free(tmp); return; } tmp[tmplen - 1] = 0; /* remove '\n' */ } switch(level) { case 0: theme_format = "debug"; break; case DEBUG_IO: theme_format = "iodebug"; break; case DEBUG_IORECV: theme_format = "iorecvdebug"; break; case DEBUG_FUNCTION: theme_format = "fdebug"; break; case DEBUG_ERROR: theme_format = "edebug"; break; case DEBUG_WHITE: theme_format = "wdebug"; break; case DEBUG_WARN: theme_format = "warndebug"; break; case DEBUG_OK: theme_format = "okdebug"; break; default: theme_format = "debug"; break; } ekg_fix_utf8(tmp); /* debug message can contain random data */ buffer_add(&buffer_debug, theme_format, tmp); query_emit(NULL, "ui-is-initialized", &is_UI); if (is_UI && window_debug) { print_window_w(window_debug, EKG_WINACT_NONE, theme_format, tmp); } #ifdef STDERR_DEBUG /* STDERR debug */ else fprintf(stderr, "%s\n", tmp); #endif xfree(tmp); }
/* Store a 16-bit value. */ void buffer_write16(struct buffer *b, uint16_t n) { buffer_ensure(b, 2); BUFFER_IN(b)[0] = n & 0xff; BUFFER_IN(b)[1] = n >> 8; buffer_add(b, 2); }
void nsq_ready(struct Buffer *buf, int count) { char b[16]; size_t n; n = sprintf(b, "RDY %d\n", count); buffer_add(buf, b, n); }
void nsq_requeue(struct Buffer *buf, const char *id, int timeout_ms) { char b[128]; size_t n; n = sprintf(b, "REQ %s %d\n", id, timeout_ms); buffer_add(buf, b, n); }
void nsq_subscribe(struct Buffer *buf, const char *topic, const char *channel) { char b[128]; size_t n; n = sprintf(b, "SUB %s %s\n", topic, channel); buffer_add(buf, b, n); }
void nsq_finish(struct Buffer *buf, const char *id) { char b[48]; size_t n; n = sprintf(b, "FIN %s\n", id); buffer_add(buf, b, n); }
yylex () { int c; buffer_init(); /* skip white spaces */ while (isspace((c = getchar()))) ; /* quotations ... */ if (c == '\'') return c; if (c == ')' || c=='(') return c; if (c == EOF) return 0; /* symbols or numbers */ if (!isspace(c) && c != '(' && c != ')') { int number_have_point = 0; int parse_as_symbol = 0; while (!isspace(c) && c != '(' && c != ')') { if (!parse_as_symbol) { if (c != '.' && !isdigit(c)) parse_as_symbol = 1; else if (c == '.') { if(number_have_point) parse_as_symbol = 1; number_have_point = 1; } } buffer_add(c); c = getchar(); } ungetc(c, stdin); yylval = buffer_end(); if (!strcmp(yylval, ".")) return SYMBOL; return parse_as_symbol ? SYMBOL : NUMBER; } fprintf(stderr, "%s : invalid input '%c' (%d). please email the author\n", program_name, c, c); exit(EXIT_FAILURE); }