/* * Write a log message out to all output channels. */ static void log_output(Logger *logger) { LOG_Output *out; for (out = listHead(&logger->outputs); out; out = listNext(out)) { pthread_mutex_lock(&out->output); switch(out->type) { case LOG_OT_UDP: case LOG_OT_TCP: case LOG_OT_FD: tcpWrite(out->u.fd, bufGet(&logger->scratch), bufLen(&logger->scratch)); break; case LOG_OT_FILE: case LOG_OT_FP: fwrite(bufGet(&logger->scratch), bufLen(&logger->scratch), 1, out->u.fp); fflush(out->u.fp); break; case LOG_OT_SYSLOG: syslog(out->u.priority, "%s", bufGet(&logger->scratch)); break; } pthread_mutex_unlock(&out->output); } }
/* * Return -1, 1 or 0 if <left> is smaller than, greater than or equal to <right>, either in size, or * (when both have the same size) according to memcmp(). */ int bufCompare(const Buffer *left, const Buffer *right) { int len1 = bufLen(left); int len2 = bufLen(right); if (len1 < len2) return -1; else if (len2 > len1) return 1; else return memcmp(bufGet(left), bufGet(right), len1); }
/* * Add an output channel to <logger> that writes to the file specified with * <fmt> and the subsequent parameters. If the file could not be opened, -1 is * returned and the channel is not created. */ int logToFile(Logger *logger, const char *fmt, ...) { va_list ap; FILE *fp; Buffer filename = { 0 }; va_start(ap, fmt); bufSetV(&filename, fmt, ap); va_end(ap); if ((fp = fopen(bufGet(&filename), "w")) == NULL) { bufReset(&filename); return -1; } else { pthread_mutex_lock(&logger->access); LOG_Output *out = log_create_output(logger, LOG_OT_FILE); out->u.fp = fp; pthread_mutex_unlock(&logger->access); bufReset(&filename); return 0; } }
/* * Handle writable file descriptor <fd>. */ void disHandleWritable(Dispatcher *dis, int fd) { int r; DIS_File *file = paGet(&dis->files, fd); dbgAssert(stderr, file != NULL, "unknown file descriptor: %d\n", fd); r = write(fd, bufGet(&file->outgoing), bufLen(&file->outgoing)); if (r > 0) { bufTrim(&file->outgoing, r, 0); } }
static void ns_handle_data(Dispatcher *dis, int fd, void *udata) { char data[9000]; NS *ns = (NS *) dis; NS_Connection *conn = paGet(&ns->connections, fd); int n; P dbgPrint(stderr, "Reading from fd %d...\n", fd); n = read(fd, data, sizeof(data)); P dbgPrint(stderr, "read() returned %d.\n", n); if (n > 0) { P dbgPrint(stderr, "Adding to incoming buffer.\n"); bufAdd(&conn->incoming, data, n); if (ns->on_socket_cb != NULL) { P dbgPrint(stderr, "Calling on_socket_cb.\n"); ns->on_socket_cb(ns, fd, bufGet(&conn->incoming), bufLen(&conn->incoming), ns->on_socket_udata); } } else if (n == 0) { P dbgPrint(stderr, "End of file, disconnecting.\n"); nsDisconnect(ns, fd); if (ns->on_disconnect_cb != NULL) { P dbgPrint(stderr, "Calling on_disconnect_cb.\n"); ns->on_disconnect_cb(ns, fd, ns->on_disconnect_udata); } } else { P dbgPrint(stderr, "Error, disconnecting.\n"); nsDisconnect(ns, fd); if (ns->on_error_cb != NULL) { P dbgPrint(stderr, "Calling on_error_cb.\n"); ns->on_error_cb(ns, fd, errno, ns->on_error_udata); } } }
/*********************************************************************************** * @fn usbInProcess * * @brief Handle traffic flow from RF to USB. * * @param none * * @return none */ static void usbInProcess(void) { uint8 length; // USB ready to accept new IN packet halIntOff(); oldEndpoint = USBFW_GET_SELECTED_ENDPOINT(); USBFW_SELECT_ENDPOINT(4); // The IN endpoint is ready to accept data if ( USBFW_IN_ENDPOINT_DISARMED() ) { // Number of bytes present in RF buffer length= bufNumBytes(&rbTxBuf); if (length>0) { // Limit the size if (length > USB_MAX_PACKET_SIZE) { length = USB_MAX_PACKET_SIZE; } // Read from UART TX buffer bufGet(&rbTxBuf,buffer,length); // Write to USB FIFO usbfwWriteFifo(&USBF4, length, buffer); // Flag USB IN buffer as not ready (disarming EP4) USBFW_SELECT_ENDPOINT(4); USBFW_ARM_IN_ENDPOINT(); // Send data to the host } } USBFW_SELECT_ENDPOINT(oldEndpoint); halIntOn(); }
/************************************************************************************ * @fn halUartRead * * @brief Read data from UART Rx buffer * * @param uint8* buf - buffer with data to read in to * uint16 length - number of bytes to read * * @return none */ uint16 halUartRead(uint8* buf, uint16 length) { return bufGet(&rbRxBuf, (uint8 *)buf, length); }
/* * Return a pointer to the start of the incoming buffer. */ const char *nsIncoming(NS *ns, int fd) { NS_Connection *conn = paGet(&ns->connections, fd); return bufGet(&conn->incoming); }
int main(int argc, char *argv[]) { Buffer buf1 = { 0 }; Buffer buf2 = { 0 }; Buffer *buf3; bufClear(&buf1); make_sure_that(bufLen(&buf1) == 0); make_sure_that(bufIsEmpty(&buf1)); bufAdd(&buf1, "ABCDEF", 3); make_sure_that(bufLen(&buf1) == 3); make_sure_that(!bufIsEmpty(&buf1)); make_sure_that(strcmp(bufGet(&buf1), "ABC") == 0); bufAddC(&buf1, 'D'); make_sure_that(bufLen(&buf1) == 4); make_sure_that(strcmp(bufGet(&buf1), "ABCD") == 0); bufAddF(&buf1, "%d", 1234); make_sure_that(bufLen(&buf1) == 8); make_sure_that(strcmp(bufGet(&buf1), "ABCD1234") == 0); bufAddS(&buf1, "XYZ"); make_sure_that(bufLen(&buf1) == 11); make_sure_that(strcmp(bufGet(&buf1), "ABCD1234XYZ") == 0); bufSet(&buf1, "ABCDEF", 3); make_sure_that(bufLen(&buf1) == 3); make_sure_that(strcmp(bufGet(&buf1), "ABC") == 0); bufSetC(&buf1, 'D'); make_sure_that(bufLen(&buf1) == 1); make_sure_that(strcmp(bufGet(&buf1), "D") == 0); bufSetF(&buf1, "%d", 1234); make_sure_that(bufLen(&buf1) == 4); make_sure_that(strcmp(bufGet(&buf1), "1234") == 0); bufSetS(&buf1, "ABCDEF"); make_sure_that(bufLen(&buf1) == 6); make_sure_that(strcmp(bufGet(&buf1), "ABCDEF") == 0); bufClear(&buf1); make_sure_that(bufLen(&buf1) == 0); make_sure_that(strcmp(bufGet(&buf1), "") == 0); bufSet(&buf1, "ABC", 3); bufSet(&buf2, "DEF", 3); buf3 = bufCat(&buf1, &buf2); make_sure_that(&buf1 == buf3); make_sure_that(bufLen(&buf1) == 6); make_sure_that(strcmp(bufGet(&buf1), "ABCDEF") == 0); make_sure_that(bufLen(&buf2) == 3); make_sure_that(strcmp(bufGet(&buf2), "DEF") == 0); bufSetF(&buf1, "ABCDEF"); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 0, 0)), "ABCDEF") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 1, 0)), "BCDEF") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 0, 1)), "BCDE") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 1, 1)), "CD") == 0); make_sure_that(strcmp(bufGet(bufTrim(&buf1, 3, 3)), "") == 0); bufPack(&buf1, PACK_INT8, 0x01, PACK_INT16, 0x0123, PACK_INT32, 0x01234567, PACK_INT64, 0x0123456789ABCDEF, PACK_FLOAT, 0.0, PACK_DOUBLE, 0.0, PACK_STRING, "Hoi1", PACK_DATA, "Hoi2", 4, PACK_RAW, "Hoi3", 4, END); make_sure_that(bufLen(&buf1) == 47); make_sure_that(memcmp(bufGet(&buf1), "\x01" "\x01\x23" "\x01\x23\x45\x67" "\x01\x23\x45\x67\x89\xAB\xCD\xEF" "\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x04Hoi1" "\x00\x00\x00\x04Hoi2" "Hoi3", 47) == 0); { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; float f32; double f64; char *string, *data, raw[4]; int data_size; bufUnpack(&buf1, PACK_INT8, &u8, PACK_INT16, &u16, PACK_INT32, &u32, PACK_INT64, &u64, PACK_FLOAT, &f32, PACK_DOUBLE, &f64, PACK_STRING, &string, PACK_DATA, &data, &data_size, PACK_RAW, &raw, sizeof(raw), END); make_sure_that(u8 == 0x01); make_sure_that(u16 == 0x0123); make_sure_that(u32 == 0x01234567); make_sure_that(u64 == 0x0123456789ABCDEF); make_sure_that(f32 == 0.0); make_sure_that(f64 == 0.0); make_sure_that(memcmp(string, "Hoi1", 5) == 0); make_sure_that(memcmp(data, "Hoi2", 4) == 0); make_sure_that(data_size == 4); make_sure_that(memcmp(raw, "Hoi3", 4) == 0); } { bufUnpack(&buf1, PACK_INT8, NULL, PACK_INT16, NULL, PACK_INT32, NULL, PACK_INT64, NULL, PACK_FLOAT, NULL, PACK_DOUBLE, NULL, PACK_STRING, NULL, PACK_DATA, NULL, NULL, PACK_RAW, NULL, 4, END); } { const char *name[] = { "Mills", "Berry", "Buck", "Stipe" }; bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, TRUE, "%s", name[0]); make_sure_that(strcmp(bufGet(&buf1), "Mills") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[1]); make_sure_that(strcmp(bufGet(&buf1), "Mills and Berry") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[1]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[2]); make_sure_that(strcmp(bufGet(&buf1), "Mills, Berry and Buck") == 0); bufClear(&buf1); bufList(&buf1, ", ", " and ", TRUE, FALSE, "%s", name[0]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[1]); bufList(&buf1, ", ", " and ", FALSE, FALSE, "%s", name[2]); bufList(&buf1, ", ", " and ", FALSE, TRUE, "%s", name[3]); make_sure_that(strcmp(bufGet(&buf1), "Mills, Berry, Buck and Stipe") == 0); } bufReset(&buf1); bufReset(&buf2); return errors; }
/* * Concatenate <addition> onto <base> and return <base>. */ Buffer *bufCat(Buffer *base, const Buffer *addition) { bufAdd(base, bufGet(addition), bufLen(addition)); return base; }
int main(int argc, char *argv[]) { FILE *in_fp = NULL, *out_fp = NULL; int i, r; char *input = NULL, *output = NULL; Buffer cmd = { 0 }; bufSetS(&cmd, "cpp -C"); for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-") == 0) { input = "-"; } else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--cpp") == 0) { use_cpp = TRUE; } else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) { output = argv[++i]; } else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--comments") == 0) { include_comment = TRUE; } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--statics") == 0) { include_static = TRUE; } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(NULL, argv[0], 0); } else if (argv[i][0] == '-') { bufAddC(&cmd, ' '); bufAddS(&cmd, argv[i]); } else if (input != NULL) { usage("Multiple input files specified\n", argv[0], -1); } else { input = argv[i]; } } if (use_cpp) { if (input != NULL && strcmp(input, "-") != 0) { bufAddC(&cmd, ' '); bufAddS(&cmd, input); } else { input = "<stdin>"; } if ((in_fp = popen(bufGet(&cmd), "r")) == NULL) { perror("popen"); exit(1); } } else { if (input == NULL || strcmp(input, "-") == 0) { in_fp = stdin; input = "<stdin>"; } else if ((in_fp = fopen(input, "r")) == NULL) { perror(input); exit(1); } } bufReset(&cmd); if (output == NULL) { out_fp = stdout; } else if ((out_fp = fopen(output, "w")) == NULL) { perror(output); exit(1); } r = process(input, in_fp, out_fp); if (out_fp != stdout) fclose(out_fp); if (use_cpp) pclose(in_fp); else if (in_fp != stdin) fclose(in_fp); return r; }
/* * Process input from <in> and write the result to <out>. The name of the original file from which * prototypes are to be extracted is in <input>. */ static int process(const char *input, FILE *in, FILE *out) { char *current_file = strdup(input); Buffer comment = { 0 }, declaration = { 0 }; int c; while ((c = fgetc(in)) != EOF) { if (c == '#') { handle_preprocessor_line(in, ¤t_file); } else if (c == '/') { bufSetC(&comment, c); handle_comment(in, &comment); } else if (!isspace(c) && c != ';') { bufSetC(&declaration, c); handle_declaration(in, &declaration); if (strchr(bufGet(&declaration), '(') != NULL && strcmp(current_file, input) == 0) { const char *str; int len; while (TRUE) { str = bufGet(&declaration); len = bufLen(&declaration); if (isspace(str[0])) bufTrim(&declaration, 1, 0); else break; } while (TRUE) { str = bufGet(&declaration); len = bufLen(&declaration); if (isspace(str[len - 1])) bufTrim(&declaration, 0, 1); else break; } if (include_static == TRUE || strncmp(str, "static", 6) != 0) { fputc('\n', out); if (include_comment && bufLen(&comment) > 0) { fputs(bufGet(&comment), out); fputc('\n', out); } fputs(str, out); if (str[len - 1] != ';') fputc(';', out); fputc('\n', out); } } bufClear(&comment); } } bufReset(&comment); bufReset(&declaration); free(current_file); return 0; }