Example #1
0
File: file.c Project: nielssp/ctodo
void write_todolist(STREAM *output, TODOLIST *todolist) {
  OPTION *opt = NULL;
  TASK *task = NULL;
  int width = 0;
  stream_printf(output, "%s\n", todolist->title);
  task = todolist->first;
  while (task) {
    stream_printf(output, "[%c] %s\n",
        task->done ? 'X' : ' ', task->message);
    task = task->next;
  }
  opt = todolist->first_option;
  if (opt) {
    width += stream_printf(output, "#");
    while (opt) {
      if (width >= 80) {
        stream_printf(output, "\n#");
        width = 1;
      }
      width += 2;
      stream_putc(' ', output);
      width += escape_string(output, opt->key);
      stream_putc('=', output);
      width += escape_string(output, opt->value);
      opt = opt->next;
    }
    stream_printf(output, "\n");
  }
}
Example #2
0
/**
 * Return information 
 */
static void _dump_events(gds_stream_t * stream, sched_t * self)
{
  sched_static_t * sched= (sched_static_t *) self;
  _event_t * event;
  uint32_t depth;
  uint32_t max_depth;
  uint32_t start;
  unsigned int index;

  depth= sched->events->current_depth;
  max_depth= sched->events->max_depth;
  start= sched->events->start_index;
  stream_printf(stream, "Number of events queued: %u (%u)\n",
	     depth, max_depth);
  for (index= 0; index < depth; index++) {
    event= (_event_t *) sched->events->items[(start+index) % max_depth];
    stream_printf(stream, "(%d) ", (start+index) % max_depth);
    stream_flush(stream);
    if (event->ops->dump != NULL) {
      event->ops->dump(stream, event->ctx);
    } else {
      stream_printf(stream, "unknown");
    }
    stream_printf(stream, "\n");
  }
}
Example #3
0
// -----[ _ipip_proto_dump_msg ]-------------------------------------
static void _ipip_proto_dump_msg(gds_stream_t * stream, net_msg_t * msg)
{
  net_msg_t * encap_msg= (net_msg_t *) msg->payload;
  stream_printf(stream, "msg:[");
  message_dump(stream, encap_msg);
  stream_printf(stream, "]");
}
Example #4
0
// -----[ aslevel_perror ]-------------------------------------------
void aslevel_perror(gds_stream_t * stream, int error)
{
  char * error_msg= aslevel_strerror(error);
  if (error_msg != NULL)
    stream_printf(stream, error_msg);
  else
    stream_printf(stream, "unknown error (%i)", error);
}
Example #5
0
/**
 * Dump an error message for the given error code.
 */
void network_perror(gds_stream_t * stream, int error)
{
  const char * error_msg= network_strerror(error);
  if (error_msg != NULL)
    stream_printf(stream, error_msg);
  else
    stream_printf(stream, "unknown error (%i)", error);    
}
Example #6
0
// -----[ bgp_input_perror ]-----------------------------------------
void bgp_input_perror(gds_stream_t * stream,
		      bgp_input_error_t error)
{
  const char * msg= bgp_input_strerror(error);
  if (msg != NULL)
    stream_printf(stream, "%s", msg);
  else
    stream_printf(stream, "unknown error code (%d)", error);
}
Example #7
0
// -----[ net_iface_dump ]-------------------------------------------
void net_iface_dump(gds_stream_t * stream, net_iface_t * iface, int with_dest)
{
  net_iface_dump_type(stream, iface);
  stream_printf(stream, "\t");
  net_iface_dump_id(stream, iface);
  if (with_dest) {
    stream_printf(stream, "\t");
    net_iface_dump_dest(stream, iface);
  }
}
Example #8
0
static void
send_challenge(dico_stream_t str, char *data)
{
    if (data[0]) { 
	 stream_printf(str, "130 challenge follows\n");
	 /* FIXME: use dicod_ostream_create */
	 stream_writez(str, data);
	 stream_writez(str, "\n.\n");
    }
    stream_printf(str, "330 send response\n");
}    
Example #9
0
void target_usage (stream_t *out)
{
  int i;
  stream_printf (out,
    "Usage: \n"
    "  target clear     Remove all targets\n"
    "  target <target>  Add a target\n"
    "\n"
    "Available targets:\n");
  for (i = 0; i < g_n_targets; ++i)
    stream_printf (out, "  %20s  %s\n", g_target[i].name, g_target[i].help);
}
Example #10
0
void filter_usage (stream_t *out)
{
  int i;
  stream_printf (out,
    "Usage: \n"
    "  filter clear      Remove all filters\n"
    "  filter <filter>   Add a filter\n"
    "\n"
    "Available filters:\n");
  for (i = 0; i < g_n_filters; ++i)
    stream_printf (out, "  %20s  %s\n", g_filter[i].name, g_filter[i].help);
}
Example #11
0
static void _set_log_progress(sched_t * self, const char * filename)
{
  sched_static_t * sched= (sched_static_t *) self;

  if (sched->pProgressLogStream != NULL)
    stream_destroy(&sched->pProgressLogStream);

  if (filename != NULL) {
    sched->pProgressLogStream= stream_create_file(filename);
    stream_printf(sched->pProgressLogStream, "# C-BGP Queue Progress\n");
    stream_printf(sched->pProgressLogStream, "# <step> <time (us)> <depth>\n");
  }
}
Example #12
0
static void
dicod_markup(dico_stream_t str, int argc, char **argv)
{
    const char *p;
    if (argc == 2) {
        /* Report current markup type */
        stream_printf(str, "280 %s is current markup type\n",
                      dico_markup_type);
    } else if ((p = dico_markup_lookup(argv[2]))) {
        dico_markup_type = p;
        stream_printf(str, "250 markup type set to %s\n", dico_markup_type);
    } else
        stream_writez(str, "500 invalid argument\n");
}
Example #13
0
static int _net_flow_src_ip_handler(flow_t * flow, flow_field_map_t * map,
				    void * ctx)
{
  flow_stats_t * stats= (flow_stats_t *) ctx;
  ip_trace_t * trace= NULL;
  net_error_t result;
  ip_opt_t opts;

  net_node_t * src_node= network_find_node(network_get_default(), flow->src_addr);
  if (src_node == NULL) {
    printf("Source node not found\n");
    return -1;
  }

  ip_options_init(&opts);
  if (flow_field_map_isset(map, FLOW_FIELD_DST_MASK)) {
    ip_pfx_t pfx= {
      .network=flow->dst_addr,
      .mask=flow->dst_mask,
    };
    ip_options_alt_dest(&opts, pfx);
  }

  result= node_load_flow(src_node, NET_ADDR_ANY, flow->dst_addr, flow->bytes,
			 stats, &trace, &opts);
  if (result < 0)
    return 0;

  if (trace != NULL) {
    stream_printf(gdsout, "TRACE=[");
    ip_trace_dump(gdsout, trace, 0);
    stream_printf(gdsout, "]\n");
    ip_trace_destroy(&trace);
  }
  return 0;
}

static int _net_flow_src_asn_handler(flow_t * flow, flow_field_map_t * map,
				     void * ctx)
{
  // Find source based on source ASN
  as_level_topo_t * topo= aslevel_get_topo();
  if (topo == NULL) {
    printf("no AS-level topology loaded\n");
    return -1;
  }
  flow->src_addr= topo->addr_mapper(flow->src_asn);
  return _net_flow_src_ip_handler(flow, map, ctx);
}
Example #14
0
/**
 * context: {iface}
 * tokens: {}
 */
static int cli_iface_load_show(cli_ctx_t * ctx, cli_cmd_t * cmd)
{
  net_iface_t * iface= _iface_from_context(ctx);
  net_link_dump_load(gdsout, iface);
  stream_printf(gdsout, "\n");
  return CLI_SUCCESS;
}
Example #15
0
static void
unparse_stmt_catch(Stream * str, struct Stmt_Catch catchstmt, int indent)
{
    Except_Arm *ex;

    stream_add_string(str, "try");
    output(str);
    unparse_stmt(catchstmt.body, indent + 2);
    for (ex = catchstmt.excepts; ex; ex = ex->next) {
	indent_stmt(str, indent);
	stream_add_string(str, "except ");
	if (ex->id >= 0)
	    stream_printf(str, "%s ", prog->var_names[ex->id]);
	stream_add_char(str, '(');
	if (ex->codes)
	    unparse_arglist(str, ex->codes);
	else
	    stream_add_string(str, "ANY");
	stream_add_char(str, ')');
	output(str);
	unparse_stmt(ex->stmt, indent + 2);
    }
    indent_stmt(str, indent);
    stream_add_string(str, "endtry");
    output(str);
}
enum proto_accept_error
proto_accept_connection(int listener_fd, int *read_fd, int *write_fd,
			const char **name)
{
    int timeout = server_int_option("name_lookup_timeout", 5);
    int fd;
    struct sockaddr_in address;
    size_t addr_length = sizeof(address);
    static Stream *s = 0;

    if (!s)
	s = new_stream(100);

    fd = accept(listener_fd, (struct sockaddr *) &address, &addr_length);
    if (fd < 0) {
	if (errno == EMFILE)
	    return PA_FULL;
	else {
	    log_perror("Accepting new network connection");
	    return PA_OTHER;
	}
    }
    *read_fd = *write_fd = fd;
    stream_printf(s, "%s, port %d",
		  lookup_name_from_addr(&address, timeout),
		  (int) ntohs(address.sin_port));
    *name = reset_stream(s);
    return PA_OKAY;
}
Example #17
0
// -----[ _node_netflow_handler ]------------------------------------
static int _node_netflow_handler(flow_t * flow, flow_field_map_t * map,
				 void * context)
{
  _netflow_ctx_t * ctx= (_netflow_ctx_t *) context;
  net_node_t * node= ctx->target_node;
  ip_trace_t * trace;

  if (ctx->options & NET_NODE_NETFLOW_OPTIONS_DETAILS) {
    stream_printf(gdsout, "src:");
    ip_address_dump(gdsout, flow->src_addr);
    stream_printf(gdsout, " dst:");
    ip_address_dump(gdsout, flow->dst_addr);
    if (flow_field_map_isset(map, FLOW_FIELD_DST_MASK))
      stream_printf(gdsout, "/%u", flow->dst_mask);
    stream_printf(gdsout, " octets:%u ", flow->bytes);
  }

  if (node_load_flow(node, flow->src_addr, flow->dst_addr,
		     flow->bytes, ctx->stats, &trace, NULL) < 0) {
    if (ctx->options & NET_NODE_NETFLOW_OPTIONS_DETAILS)
      stream_printf(gdsout, "failed\n");
    return NETFLOW_ERROR;
  }

  if (ctx->options & NET_NODE_NETFLOW_OPTIONS_DETAILS) {
    stream_printf(gdsout, "status: ");
    network_perror(gdsout, trace->status);
    stream_printf(gdsout, "\n");
  }

  ip_trace_destroy(&trace);
  return NETFLOW_SUCCESS;
}
Example #18
0
void display_message(editor_t *ed, char *fmt, ...)
  {
  gotoxy(ed, 0, ed->lines);
  outstr(ed, STATUS_COLOR);

  stream_printf(ed->console_out, fmt, printf_args, (&fmt)+1);

  outstr(ed, CLREOL TEXT_COLOR);
  }
Example #19
0
/* Just to show how easy it is to write your own:
   here is specifier function which writes pig latin to a STREAM. */
static void
pig_latin (STREAM *stream, struct printf_info *const pinfo, union printf_arg const *args)
{
  const char *string = args->pa_string;
  int len = strlen (string);

  /* printf extravaganza :-) */
  stream_printf (stream, "%c%.*say", string[len - 1], len - 1, string);
}
enum error
proto_make_listener(Var desc, int *fd, Var * canon, const char **name)
{
    struct sockaddr_in address;
    int s, port, option = 1;
    static Stream *st = 0;

    if (!st)
	st = new_stream(20);

    if (desc.type != TYPE_INT)
	return E_TYPE;

    port = desc.v.num;
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0) {
	log_perror("Creating listening socket");
	return E_QUOTA;
    }
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
		   (char *) &option, sizeof(option)) < 0) {
	log_perror("Setting listening socket options");
	close(s);
	return E_QUOTA;
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = bind_local_ip;
    address.sin_port = htons(port);
    if (bind(s, (struct sockaddr *) &address, sizeof(address)) < 0) {
	enum error e = E_QUOTA;

	log_perror("Binding listening socket");
	if (errno == EACCES)
	    e = E_PERM;
	close(s);
	return e;
    }
    if (port == 0) {
	size_t length = sizeof(address);

	if (getsockname(s, (struct sockaddr *) &address, &length) < 0) {
	    log_perror("Discovering local port number");
	    close(s);
	    return E_QUOTA;
	}
	canon->type = TYPE_INT;
	canon->v.num = ntohs(address.sin_port);
    } else
	*canon = var_ref(desc);

    stream_printf(st, "port %d", canon->v.num);
    *name = reset_stream(st);

    *fd = s;
    return E_NONE;
}
Example #21
0
// ----- node_links_save --------------------------------------------
void node_links_save(gds_stream_t * stream, net_node_t * node)
{
  gds_enum_t * ifaces= net_links_get_enum(node->ifaces);
  net_iface_t * iface;

  while (enum_has_next(ifaces)) {
    iface= *((net_iface_t **) enum_get_next(ifaces));
  
    // Skip tunnels
    if ((iface->type == NET_IFACE_VIRTUAL) ||
	(iface->type == NET_IFACE_LOOPBACK))
      continue;
  
    // This side of the link
    ip_address_dump(stream, node->rid);
    stream_printf(stream, "\t");
    net_iface_dump_id(stream, iface);
    stream_printf(stream, "\t");

    // Other side of the link
    switch (iface->type) {
    case NET_IFACE_RTR:
      ip_address_dump(stream, iface->dest.iface->owner->rid);
      stream_printf(stream, "\t");
      ip_address_dump(stream, node->rid);
      stream_printf(stream, "/32");
      break;
    case NET_IFACE_PTP:
      ip_address_dump(stream, iface->dest.iface->owner->rid);
      stream_printf(stream, "\t");
      ip_address_dump(stream, node->rid);
      stream_printf(stream, "/32");      
      break;
    case NET_IFACE_PTMP:
      ip_prefix_dump(stream, iface->dest.subnet->prefix);
      stream_printf(stream, "\t---");
      break;
    default:
      abort();
    }

    // Link load
    stream_printf(stream, "\t%u", iface->phys.load);
    stream_printf(stream, "\t%u\n", iface->phys.capacity);
  }
    
  enum_destroy(&ifaces);
}
Example #22
0
static int _target_record (data_collector_t *dc, const ramsey_t *ram, stream_t *out)
{
  long depth = ram->get_length (ram);
  struct _target_priv *priv = (struct _target_priv *) dc;

  (void) out;

  assert (dc != NULL);
  assert (ram != NULL);

  if (out && depth == priv->fork_depth)
    {
      stream_printf (out, "search %ss ", ram->get_type (ram));
      ram->print (ram, out);
      stream_printf (out, "\n");
      return 1;
    }
  return 0;
}
Example #23
0
File: file.c Project: nielssp/ctodo
char *stringify_todolist(TODOLIST *todolist) {
  size_t size = 100;
  char *str = (char *)malloc(size);
  STREAM *output = stream_buffer(str, size);
  write_todolist(output, todolist);
  stream_printf(output, "\0");
  str = stream_get_content(output);
  stream_close(output);
  return str;
}
Example #24
0
/* Function called by printf to send data to created stream */
int stream_printf(unsigned char var, FILE *stream) {
    if (var == '\n') 
    {
        stream_printf('\r', 0);                 // Translate \n to \r for terminal
    }

    usart_transmit(var);                        // Transmit data
    
    return 0;
}
Example #25
0
void net_path_dump(gds_stream_t * stream, net_path_t * path)
{
  unsigned int index;

  for (index= 0; index < uint32_array_size(path); index++) {
    if (index > 0)
      stream_printf(stream, " ");
    ip_address_dump(stream, path->data[index]);
  }
}
Example #26
0
void node_info(gds_stream_t * stream, net_node_t * node)
{
  unsigned int index;

  stream_printf(stream, "id       : ");
  ip_address_dump(stream, node->rid);
  stream_printf(stream, "\n");
  stream_printf(stream, "domain   :");
  for (index= 0; index < uint16_array_size(node->domains); index++) {
    stream_printf(stream, " %d", node->domains->data[index]);
  }
  stream_printf(stream, "\n");
  if (node->name != NULL)
    stream_printf(stream, "name     : %s\n", node->name);
  stream_printf(stream, "addresses: ");
  node_addresses_dump(stream, node);
  stream_printf(stream, "\n");
  stream_printf(stream, "latitude : %f\n", node->coord.latitude);
  stream_printf(stream, "longitude: %f\n", node->coord.longitude);
}
Example #27
0
/**
 * This function shows the list of interfaces of a given node's, along
 * with their type.
 */
void node_ifaces_dump(gds_stream_t * stream, net_node_t * node)
{
  unsigned int index;
  net_iface_t * iface;

  // Show network interfaces
  for (index= 0; index < net_ifaces_size(node->ifaces); index++) {
    iface= net_ifaces_at(node->ifaces, index);
    net_iface_dump(stream, iface, 0);
    stream_printf(stream, "\n");
  }
}
Example #28
0
static void
unparse_stmt_list(Stream * str, struct Stmt_List list, int indent)
{
    stream_printf(str, "for %s in (", prog->var_names[list.id]);
    unparse_expr(str, list.expr);
    stream_add_char(str, ')');
    output(str);
    unparse_stmt(list.body, indent + 2);
    indent_stmt(str, indent);
    stream_add_string(str, "endfor");
    output(str);
}
Example #29
0
static const char *
fmt_verb_name(void *data)
{
    db_verb_handle *h = data;
    static Stream *s = 0;

    if (!s)
	s = new_stream(40);

    stream_printf(s, "#%d:%s", db_verb_definer(*h), db_verb_names(*h));
    return reset_stream(s);
}
Example #30
0
static inline void ___ipip_debug(const char * msg, ...)
{
#ifdef IPIP_DEBUG
  va_list ap;

  va_start(ap, msg);
  stream_printf(gdsout, "IPIP_DBG::");
  str_format_for_each(gdsout, _debug_for_each, &ap, msg);
  stream_flush(gdsout);
  va_end(ap);
#endif /* IPIP_DEBUG */
}