/* * buf_set() * * Set the contents of the buffer <b> at offset <off> to the first <len> * bytes of data found at <src>. If the buffer was not created with * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer. */ ssize_t buf_set(BUF *b, const void *src, size_t len, size_t off) { size_t rlen = 0; if (b->cb_size < (len + off)) { if ((b->cb_flags & BUF_AUTOEXT)) { buf_grow(b, len + off - b->cb_size); rlen = len + off; } else { rlen = b->cb_size - off; } } else { rlen = len; } b->cb_len = rlen; memcpy((b->cb_buf + off), src, rlen); if (b->cb_len == 0) { b->cb_cur = b->cb_buf + off; b->cb_len = rlen; } return (rlen); }
const char *strbuf_str(struct strbuf *buf) { if (!buf_grow(buf, buf->used + 1)) return NULL; buf->bytes[buf->used] = '\0'; return buf->bytes; }
/* join strings in array `arr` with ' ' */ buf_t * space_join(uint8_t **arr, size_t len, buf_t *buf) { int i, str_len, sz; uint8_t *str, *p; for (i=0; i < len; i++) { str = *(arr + i); sz = str_len = strlen(str); /* if not the last array, append a empty space " " */ if (i != len-1) sz += 1; buf_grow(buf, buf->size + sz); p = buf->data + buf->size; strcpy(p, str); /* if not the last array, append a empty space " " */ if (i != len-1) strcpy(p + str_len, " "); buf->size += sz; } return buf; }
/* Return a C string owned by the buffer (invalidated if the buffer is changed). */ static const char *buf_str(struct buffer *buf) { if (!buf_grow(buf, buf->used + 1)) return NULL; buf->bytes[buf->used] = '\0'; return buf->bytes; }
void buf_insert(struct buf *buf, char *s, size_t pos) { size_t len = strlen(s); size_t new_len = buf->len + len; size_t new_cap = buf->cap; if (new_len + 1 >= new_cap) { while (new_len + 1 >= new_cap) { new_cap *= 2; } buf_grow(buf, new_cap); } // Move the bit after the insertion... memmove( buf->buf + pos + len, buf->buf + pos, buf->len - pos); // Stitch the new part in. memmove( buf->buf + pos, s, len); buf->len += len; buf->buf[buf->len] = '\0'; }
bool strbuf_pushchar(struct strbuf *buf, char ch) { if (!buf_grow(buf, buf->used + 1)) return false; buf->bytes[buf->used] = ch; buf->used++; return true; }
static bool buf_pushchar(struct buffer *buf, char ch) { if (!buf_grow(buf, buf->used + 1)) return false; buf->bytes[buf->used] = ch; buf->used++; return true; }
LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz) { lua_assert(sz > sbufsz(sb)); if (LJ_UNLIKELY(sz > LJ_MAX_MEM)) lj_err_mem(sbufL(sb)); buf_grow(sb, sz); return sbufB(sb); }
LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz) { MSize len = sbuflen(sb); lua_assert(sz > sbufleft(sb)); if (LJ_UNLIKELY(sz > LJ_MAX_MEM || len + sz > LJ_MAX_MEM)) lj_err_mem(sbufL(sb)); buf_grow(sb, len + sz); return sbufP(sb); }
/* put one char into buffer object */ int buf_putchar(struct template_buffer *buf, char c) { if( ((buf->fill + 1) >= buf->size) && !buf_grow(buf, 0) ) return 0; *(buf->dptr++) = c; *(buf->dptr) = 0; buf->fill++; return 1; }
/* Make more room in the buffer if needed. */ static void buf_prewrite(struct buf *buf) { if (buf_count(buf) == buf_size(buf)) buf_grow(buf, 0); if (buf_count(buf) > 0 && buf_avail(buf) == 0) { memmove(buf->buf, buf->buf + buf->off, buf_count(buf)); buf->off = 0; } }
struct buf *buf_create(size_t cap) { struct buf *buf = xmalloc(sizeof(*buf)); buf->buf = NULL; buf->cap = 0; buf_grow(buf, cap); buf_clear(buf); buf->cap = cap; return buf; }
/* * Append a single character <c> to the end of the buffer <b>. */ void buf_putc(BUF *b, int c) { u_char *bp; if (SIZE_LEFT(b) == 0) buf_grow(b, BUF_INCR); bp = b->cb_buf + b->cb_len; *bp = (u_char)c; b->cb_len++; }
/* Put a single char to the end of a buffer. */ int buf_putc(struct buf *buf, char ch) { int error = buf_grow(buf, buf->len + 1); if (error == BUF_OK) { buf->data[buf->len] = ch; buf->len += 1; } return error; }
/* Put chars on the end of a buffer */ int buf_put(struct buf *buf, char *data, size_t len) { int error = buf_grow(buf, buf->len + len); if (error == BUF_OK) { memcpy(buf->data + buf->len, data, len); buf->len += len; } return error; }
int main(int argc, char **argv) { progname = argv[0]; buf_init(module, INIT_MODS, 1, struct _module, "modules"); buf_init(dep, INIT_MODS, 1, int, "dependencies"); stack_size = STACK_SIZE; get_options(argc, argv); if (nfiles == 0 && !dump) panic("no input files"); if (stdlib && libdir == NULL) panic("no libdir specified"); if (rtlibdir == NULL) rtlibdir = libdir; make_prim("INTERP"); make_prim("DLTRAP"); #define bind(x) def_global(find_symbol(#x), ABS, x, X_SYM) bind(GC_BASE); bind(GC_REPEAT); bind(GC_BLOCK); bind(GC_MAP); bind(GC_FLEX); bind(GC_END); bind(E_CAST); bind(E_ASSIGN); bind(E_CASE); bind(E_WITH); bind(E_ASSERT); bind(E_RETURN); bind(E_BOUND); bind(E_NULL); bind(E_DIV); bind(E_FDIV); bind(E_STACK); bind(E_GLOB); /* First pass -- check for dependencies */ scan_files(); /* Compute needed modules */ buf_grow(module); module[nmodules].m_dep = ndeps; trace_imports(); if (status != 0) return status; /* Second pass -- link the modules that are needed */ if (!dump) { init_linker(outname, interp); load_needed(); gen_main(); if (rtlibdir != NULL) save_string("LIBDIR", rtlibdir); end_linking(); } if (dump || custom) { printf("/* Primitive table -- generated by oblink */\n\n"); printf("#include \"obx.h\"\n\n"); dump_prims(stdout); } return status; }
/* get_options -- analyse arguments */ static void get_options(int argc, char **argv) { int i; buf_init(file, INIT_MODS, 1, char *, "files"); for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-d") == 0) dflag++; else if (strcmp(argv[i], "-v") == 0) { printf("Oxford Oberon-2 linker version %s\n", PACKAGE_VERSION); exit(0); } else if (strcmp(argv[i], "-s") == 0) { sflag = TRUE; } else if (strcmp(argv[i], "-g") == 0) { gflag = TRUE; } else if (strcmp(argv[i], "-script") == 0) { if (++i == argc) panic("missing argument after -script"); lscript = argv[i]; } else if (strcmp(argv[i], "-custom") == 0) { custom = TRUE; } else if (strcmp(argv[i], "-dump") == 0) { dump = TRUE; } else if (strcmp(argv[i], "-nostdlib") == 0) { stdlib = FALSE; } else if (strcmp(argv[i], "-pl") == 0) { linecount = TRUE; } else if (strcmp(argv[i], "-i") == 0) { if (++i == argc) panic("missing argument after -i"); interp = argv[i]; } else if (strcmp(argv[i], "-L") == 0) { if (++i == argc) panic("missing argument after -L"); libdir = argv[i]; } else if (strcmp(argv[i], "-R") == 0) { if (++i == argc) panic("missing argument after -R"); rtlibdir = argv[i]; } else if (strcmp(argv[i], "-o") == 0) { if (++i == argc) panic("missing argument after -o"); outname = argv[i]; } else if (strcmp(argv[i], "-k") == 0) { if (++i == argc) panic("missing argument after -k"); stack_size = atoi(argv[i]); if (stack_size < MIN_STACK) stack_size = MIN_STACK; } else if (argv[i][0] == '-') { panic("unknown switch %s", argv[i]); } else { buf_grow(file); file[nfiles] = argv[i]; nfiles++; } } }
/* * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the * buffer is too small to accept all data, it will get resized to an * appropriate size to accept all data. */ void buf_append(BUF *b, const void *data, size_t len) { size_t left; u_char *bp; left = SIZE_LEFT(b); if (left < len) buf_grow(b, len - left); bp = b->cb_buf + b->cb_len; memcpy(bp, data, len); b->cb_len += len; }
/* Formatted printing to a buffer. */ int buf_sprintf(struct buf *buf, const char *fmt, ...) { assert(buf != NULL); if (buf->len >= buf->cap && buf_grow(buf, buf->len + 1) != BUF_OK) return BUF_ENOMEM; va_list ap; int num; va_start(ap, fmt); num = vsnprintf(buf->data + buf->len, buf->cap - buf->len, fmt, ap); va_end(ap); if (num < 0) return BUF_EFAILED; size_t size = (size_t)num; if (size >= buf->cap - buf->len) { if (buf_grow(buf, buf->len + size + 1) != BUF_OK) return BUF_ENOMEM; va_start(ap, fmt); num = vsnprintf(buf->data + buf->len, buf->cap - buf->len, fmt, ap); va_end(ap); } if (num < 0) return BUF_EFAILED; buf->len += num; return BUF_OK; }
/* append data to buffer */ static int buf_append(struct template_buffer *buf, unsigned char *s, int len) { while ((buf->fill + len + 1) >= buf->size) { if (!buf_grow(buf)) return 0; } memcpy(buf->dptr, s, len); buf->fill += len; buf->dptr += len; *(buf->dptr) = 0; return len; }
/* Get buffer data as null-terminated chars */ char * buf_str(struct buf *buf) { assert(buf != NULL); if (buf->len < buf->cap && buf->data[buf->len] == '\0') return buf->data; if (buf->len + 1 <= buf->cap || buf_grow(buf, buf->len + 1) == BUF_OK) { buf->data[buf->len] = '\0'; return buf->data; } return NULL; }
/* append data to buffer */ int buf_append(struct template_buffer *buf, const char *s, int len) { if ((buf->fill + len + 1) >= buf->size) { if (!buf_grow(buf, len + 1)) return 0; } memcpy(buf->dptr, s, len); buf->fill += len; buf->dptr += len; *(buf->dptr) = 0; return len; }
/* make_symbol -- create a symbol, but don't put it in the hash table */ symbol make_symbol(const char *name) { symbol s = (symbol) must_alloc(sizeof(struct _symbol), "symbol table entry"); s->s_name = must_strdup(name); s->s_seg = UNDEFINED; s->s_kind = X_NONE; s->s_value = -1; s->s_next = NULL; s->s_index = 0; s->s_uchain = -1; s->s_check = s->s_nlines = 0; s->s_file = NULL; buf_grow(dict); dict[ndict++] = s; return s; }
unsigned strbuf_pushchars(struct strbuf *buf, const char *str) { unsigned int len; assert(str != NULL); assert(buf != NULL); len = strlen(str); if (!buf_grow(buf, buf->used + len)) return 0; memcpy(buf->bytes + buf->used, str, len); buf->used += len; return len; }
void buf_vprintf(struct buf *buf, const char *format, va_list args) { va_list args_copy; va_copy(args_copy, args); // Try once... size_t n = (size_t) vsnprintf(buf->buf, buf->cap, format, args); va_end(args); // vsnprintf returns the required size if it wasn't enough, so grow to that // size and try again. if (n >= buf->cap) { buf_grow(buf, n + 1); n = (size_t) vsnprintf(buf->buf, buf->cap, format, args_copy); va_end(args_copy); } buf->len = n; }
/* read line-by-line a redirected FASTA format file from stdin * extract DNA sequence THREE */ static size_t dna_seq3(struct buf *b) { buf_init(b); while (NULL != fgets(b->str, BUFSZ, stdin)) if ('>' == *b->str && !strncmp(">THREE", b->str, 6)) break; char *curr = b->str; while (NULL != fgets(curr, BUFSZ, stdin)) { if ('>' == *curr) break; size_t len = strlen(curr); if ('\n' == curr[len-1]) len--; buf_grow(b, len); curr = b->str + b->len; } return b->len; }
int main(int argc, const char *argv[]) { /* Read file into buffer */ struct buf *buf = buf_new(NULL); int nread; FILE *fp = fopen("cfg_example.cfg", "r"); if (fp == NULL) return -1; while (1) { if (buf_grow(buf, buf->len + READ_UNIT) != BUF_OK) return BUF_ENOMEM; if ((nread = fread(buf->data + buf->len, sizeof(char), buf->cap - buf->len, fp)) <= 0) break; buf->len += nread; } /* Parse config */ struct cfg cfg; cfg.data = buf->data; /* data to parse */ cfg.len = buf->len; /* data length to parse */ cfg.lineno = 1; /* initialize line number */ int cfg_err; while ((cfg_err = cfg_get(&cfg)) == CFG_OK) { printf("%.*s => %.*s\n", (int)(cfg.key_len), cfg.key, (int)(cfg.val_len), cfg.val); } buf_free(buf); if (cfg_err == CFG_EBADFMT) { printf("bad format on line %ld\n", cfg.lineno); return -1; } return 0; }
static void send_gss_error_cb(void *rock, gss_buffer_t status_string) { mb_t outbuf = (mb_t)rock; int newsize = outbuf->length + status_string->length + 1; char *p; prtmsg("%.*s", (int)status_string->length, (char *)status_string->value); if (buf_grow(outbuf, newsize)) return; p = ((char *)outbuf->value) + outbuf->length; memcpy(p, status_string->value, status_string->length); p += status_string->length; *p++=0; if (p != ((char *)outbuf->value) + newsize) prtmsg("Warning: send_gss_error_cb: pointer mismatch: %p + %d != %p", outbuf->value, newsize, p); outbuf->length = newsize; }
/* scan -- scan a file for MODULE and IMPORT directives */ static void scan(char *name, boolean islib) { FILE *fp; int m = -1, m2, chksum; err_file = must_strdup(name); fp = fopen(name, "r"); if (fp == NULL) { perror(name); exit(2); } while (fgets(line, MAXLINE, fp) != NULL) { nwords = split_line(line, words); if (nwords == 0) continue; if (strcmp(words[0], "MODULE") == 0) { char *mname = words[1]; m = find_module(mname); if (m >= 0) { if (module[m].m_lib) error("%s has the same name as a library module", words[1]); else error("%s is loaded more than once", words[1]); } buf_grow(module); m = nmodules; module[m].m_file = name; module[m].m_name = must_strdup(mname); module[m].m_lib = islib; module[m].m_needed = FALSE; module[m].m_dep = ndeps; module[m].m_check = strtoul(words[2], NULL, 0); nmodules++; } else if (strcmp(words[0], "IMPORT") == 0) { if (m < 0) error("IMPORT appears before MODULE in %s", name); m2 = find_module(words[1]); chksum = strtoul(words[2], NULL, 0); buf_grow(dep); if (m2 < 0) error("%s imports %s -- please load it first", module[m].m_name, words[1]); else { dep[ndeps++] = m2; if (module[m2].m_check != chksum) error("checksum of module %s does not match value" " expected by module %s", words[1], module[m].m_name); } } else if (strcmp(words[0], "PRIM") == 0) { if (islib && !custom #ifdef DYNLINK && *words[1] != '*' #endif ) make_prim(words[1]); } else if (strcmp(words[0], "ENDHDR") == 0) { break; } else { panic("*bad directive %s in file header", words[0]); } } fclose(fp); }
static int ws_events_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { struct lws_context *context = lws_get_context(wsi); ws_t *ws = lws_context_user(context); struct per_session_data__events *pss = user; int i; switch (reason) { case LWS_CALLBACK_ESTABLISHED: pss->ws = ws; pss->cmd = command_new((command_handler_t) ws_events_command, pss); buf_init(&pss->out_buf); pss->id = ws_session_add(ws, pss); log_debug(2, "ws_events_callback LWS_CALLBACK_ESTABLISHED [%04X]", pss->id); break; case LWS_CALLBACK_SERVER_WRITEABLE: i = pss->out_buf.len - LWS_SEND_BUFFER_PRE_PADDING; if (i > 0) { log_debug(2, "ws_events_callback LWS_CALLBACK_SERVER_WRITEABLE [%04X]: %d bytes", pss->id, i); buf_grow(&pss->out_buf, LWS_SEND_BUFFER_POST_PADDING); int ret = lws_write(wsi, pss->out_buf.base+LWS_SEND_BUFFER_PRE_PADDING, i, LWS_WRITE_TEXT); pss->out_buf.len = 0; if (ret < i) { log_str("HTTP ERROR: %d writing to event websocket", ret); return -1; } } else { log_debug(2, "ws_events_callback LWS_CALLBACK_SERVER_WRITEABLE [%04X]: READY", pss->id); } break; case LWS_CALLBACK_RECEIVE: log_debug(2, "ws_events_callback LWS_CALLBACK_RECEIVE [%04X]: %d bytes", pss->id, len); log_debug_data(in, len); /* Make sure send buffer has room for pre-padding */ if (pss->out_buf.len == 0) { buf_append_zero(&pss->out_buf, LWS_SEND_BUFFER_PRE_PADDING); } /* Execute command */ command_recv(pss->cmd, in, len); /* Trig response write */ lws_callback_on_writable(wsi); break; case LWS_CALLBACK_CLOSED: log_debug(2, "ws_events_callback LWS_CALLBACK_CLOSED [%04X]", pss->id); pss->ws = NULL; if (pss->cmd != NULL) { command_destroy(pss->cmd); pss->cmd = NULL; } buf_cleanup(&pss->out_buf); pss->id = -1; ws_session_remove(ws, pss); break; /* * this just demonstrates how to use the protocol filter. If you won't * study and reject connections based on header content, you don't need * to handle this callback */ case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: log_debug(2, "ws_events_callback LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION [%04X]", pss->id); ws_dump_handshake_info(wsi); /* you could return non-zero here and kill the connection */ // return -1; break; case LWS_CALLBACK_PROTOCOL_INIT: log_debug(2, "ws_events_callback LWS_CALLBACK_PROTOCOL_INIT"); break; default: log_debug(2, "ws_events_callback: reason=%d", reason); break; } return 0; }