Exemple #1
0
int main(int argc, char *argv[])
{
	init();
	if(init_setting() != 0){
		print_errmsg("ERROR: SETTING init fail. \n");
		return 1;	
	}
	if(gc_init() != 0){
		print_errmsg("ERROR: G_CODE init fail. \n");
		return 1;
	}
	if (cm_init() == false)
	{
		print_errmsg("ERROR: USB-DEV init fail. \n");
		return 1;
	}
  
	if (cmd_init() == false)
	{
		print_errmsg("ERROR: G code init fail.\n");
		cm_close();
		return 1;
	}
	
	if (tp_init() == false)
	{
		print_errmsg("ERROR: Temperature library init fail. \n");
		cmd_close();
		cm_close();
		return 1;
	}

	plan_init();
	Config_ResetDefault();
	st_init();

	while(1) {
		LCD(10UL);
		getCommand(10UL);
		process_commands(50UL);
		stepper(1000L);
		manageHeater(10UL);
	}

	st_close();
	plan_close();
	tp_close();
	cmd_close();
	cm_close();
	//debug
	//sfp_close();
	return 0;
}
/*
 * cmd_escapestring: certain strings sent to a database should be properly
 *  escaped -- for instance, quotes need to be escaped to insure that 
 *  a query string is properly formatted.  cmd_escapestring does whatever
 *  is necessary to escape the special characters in a string. 
 *
 * Inputs:
 *  cmd->argv[0]: connection name
 *  cmd->argv[1]: string to escape
 *
 * Returns:
 *  this command CANNOT fail.  The return string is null-terminated and 
 *  stored in the data field of the modret_t structure.
 *
 * Notes:
 *  Different languages may escape different characters in different ways.
 *  A backend should handle this correctly, where possible.  If there is
 *  no client library function to do the string conversion, it is strongly
 *  recommended that the backend module writer do whatever is necessry (read
 *  the database documentation and figure it out) to do the conversion
 *  themselves in this function.
 *
 *  A backend MUST supply a working escapestring implementation.  Simply
 *  copying the data from argv[0] into the data field of the modret allows
 *  for possible SQL injection attacks when this backend is used.
 */
MODRET cmd_escapestring(cmd_rec * cmd) {
  conn_entry_t *entry = NULL;
  db_conn_t *conn = NULL;
  modret_t *cmr = NULL;
  char *unescaped = NULL;
  char *escaped = NULL;
  cmd_rec *close_cmd;
  size_t unescaped_len = 0;
#ifdef HAVE_POSTGRES_PQESCAPESTRINGCONN
  int pgerr = 0;
#endif

  sql_log(DEBUG_FUNC, "%s", "entering \tpostgres cmd_escapestring");

  _sql_check_cmd(cmd, "cmd_escapestring");

  if (cmd->argc != 2) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_escapestring");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION, "badly formed request");
  }

  /* get the named connection */
  entry = _sql_get_connection(cmd->argv[0]);
  if (!entry) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_escapestring");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION,
      "unknown named connection");
  }

  conn = (db_conn_t *) entry->data;

  /* Make sure the connection is open. */
  cmr = cmd_open(cmd);
  if (MODRET_ERROR(cmr)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_escapestring");
    return cmr;
  }

  unescaped = cmd->argv[1];
  unescaped_len = strlen(unescaped);
  escaped = (char *) pcalloc(cmd->tmp_pool, sizeof(char) *
    (unescaped_len * 2) + 1);

#ifdef HAVE_POSTGRES_PQESCAPESTRINGCONN
  PQescapeStringConn(conn->postgres, escaped, unescaped, unescaped_len, &pgerr);
  if (pgerr != 0) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_escapestring");
    return _build_error(cmd, conn);
  }
#else
  PQescapeString(escaped, unescaped, unescaped_len);
#endif

  close_cmd = _sql_make_cmd(cmd->tmp_pool, 1, entry->name);
  cmd_close(close_cmd);
  SQL_FREE_CMD(close_cmd);

  sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_escapestring");
  return mod_create_data(cmd, (void *) escaped);
}
Exemple #3
0
Fichier : open.c Projet : lb1a/avfs
static int do_open(const char *path, int flags, mode_t mode, char *pathbuf)
{
    int serverfh;
    int holderfd;
    int fh;

    serverfh = cmd_open(path, flags, mode, pathbuf, &holderfd);
    if(serverfh < 0) {
        real_close(holderfd, 1);
        return serverfh;
    }

    fh = get_handle();
    if(fh < 0) {
        cmd_close(serverfh);
        real_close(holderfd, 1);
        return fh;
    }
    
    fcntl(holderfd, F_SETFD, FD_CLOEXEC);

    __av_dtable[fh].serverfh = serverfh;
    __av_dtable[fh].holderfd = holderfd;

    return fh;
}
Exemple #4
0
static int
stroke_rect_element(svg_state_t *s, htsmsg_t *attribs)
{
  const char *str_width  = htsmsg_get_str(attribs, "width");
  const char *str_height = htsmsg_get_str(attribs, "height");
  const char *str_x      = htsmsg_get_str(attribs, "x");
  const char *str_y      = htsmsg_get_str(attribs, "y");
  
  if(str_width == NULL || str_height == NULL || str_x == NULL || str_y == NULL)
    return -1;

  float width  = my_str2double(str_width,  NULL);
  float height = my_str2double(str_height, NULL);
  float x      = my_str2double(str_x,      NULL);
  float y      = my_str2double(str_y,      NULL);

  float v[2];

  v[0] = x;
  v[1] = y;
  cmd_move_abs(s, v);
  v[0] += width;
  cmd_lineto_abs(s, v);
  v[1] += height;
  cmd_lineto_abs(s, v);
  v[0] = x;
  cmd_lineto_abs(s, v);
  cmd_close(s);
  return 0;
}
/* use common shell processing for all commands - this prevent static linking
   of EXTCMD or VDISK modules for shl_ramdisk/shl_mem functions. */
static void shell_cmd(const char *cmd, const char *args) {
   char   *cstr = strcat_dyn(sprintf_dyn(cmd), args);
   cmd_state cs = cmd_init(cstr, 0);
   cmd_run(cs, CMDR_ECHOOFF);
   cmd_close(cs);
   free(cstr);
}
/*
 * cmd_exit: closes all open connections.
 *
 * Inputs:
 *  None
 *
 * Returns:
 *  A simple non-error modret_t.
 */
static modret_t *cmd_exit(cmd_rec *cmd) {
  register unsigned int i = 0;

  sql_log(DEBUG_FUNC, "%s", "entering \tpostgres cmd_exit");

  for (i = 0; i < conn_cache->nelts; i++) {
    conn_entry_t *entry = ((conn_entry_t **) conn_cache->elts)[i];

    if (entry->connections > 0) {
      cmd_rec *close_cmd = _sql_make_cmd(conn_pool, 2, entry->name, "1");
      cmd_close(close_cmd);
      destroy_pool(close_cmd->pool);
    }
  }

  sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_exit");

  return PR_HANDLED(cmd);
}
/*
 * sql_timer_cb: when a timer goes off, this is the function that gets called.
 * This function makes assumptions about the db_conn_t members.
 */
static int sql_timer_cb(CALLBACK_FRAME) {
  conn_entry_t *entry = NULL;
  int i = 0;
  cmd_rec *cmd = NULL;
 
  for (i = 0; i < conn_cache->nelts; i++) {
    entry = ((conn_entry_t **) conn_cache->elts)[i];

    if (entry->timer == p2) {
      sql_log(DEBUG_INFO, "timer expired for connection '%s'", entry->name);
      cmd = _sql_make_cmd( conn_pool, 2, entry->name, "1" );
      cmd_close( cmd );
      SQL_FREE_CMD(cmd);
      entry->timer = 0;
    }
  }

  return 0;
}
Exemple #8
0
Fichier : open.c Projet : lb1a/avfs
static int virt_close(int fd, int undersc)
{
    int res;

    if(!FD_OK(fd) || !ISVIRTUAL(fd))
        res =  real_close(fd, undersc);
    else {
        int errnosave = errno;
        res = cmd_close(SERVERFH(fd));
        real_close(__av_dtable[fd].holderfd, 1);
        free_handle(fd);
        if(res < 0) 
            errno = -res, res = -1;
        else
            errno = errnosave;
    }

    return res;
}
Exemple #9
0
string builder::flush() {
  if (m_lazy) {
    while (m_counters[syntaxtag::A] > 0) cmd_close(true);
    while (m_counters[syntaxtag::B] > 0) background_close(true);
    while (m_counters[syntaxtag::F] > 0) color_close(true);
    while (m_counters[syntaxtag::T] > 0) font_close(true);
    while (m_counters[syntaxtag::U] > 0) line_color_close(true);
    while (m_counters[syntaxtag::u] > 0) underline_close(true);
    while (m_counters[syntaxtag::o] > 0) overline_close(true);
  }

  string output = m_output.data();

  // reset values
  m_output.clear();
  for (auto& counter : m_counters) counter.second = 0;
  for (auto& value : m_colors) value.second = "";
  m_fontindex = 1;

  return string_util::replace_all(output, string{BUILDER_SPACE_TOKEN}, " ");
}
Exemple #10
0
/*
 * cmd_insert: executes an INSERT query, properly constructing the query
 *  based on the inputs.
 *
 * cmd_insert takes either exactly two inputs, or exactly four.  If only
 *  two inputs are given, the second is a monolithic query string.  See 
 *  the examples below.
 *
 * Inputs:
 *  cmd->argv[0]: connection name
 *  cmd->argv[1]: table
 *  cmd->argv[2]: field string
 *  cmd->argv[3]: value string
 *
 * Returns:
 *  either a properly filled error modret_t if the insert failed, or a 
 *  simple non-error modret_t.
 *
 * Example:
 *  These are example queries that would be executed for Postgres; other
 *  backends will have different SQL syntax.
 *  
 *  argv[] = "default","log","userid, date, count", "'aah', now(), 2"
 *  query  = "INSERT INTO log (userid, date, count) VALUES ('aah', now(), 2)"
 *
 *  argv[] = "default"," INTO foo VALUES ('do','re','mi','fa')"
 *  query  = "INSERT INTO foo VALUES ('do','re','mi','fa')"
 *
 * Notes:
 *  none
 */
MODRET cmd_insert(cmd_rec *cmd) {
  conn_entry_t *entry = NULL;
  db_conn_t *conn = NULL;
  modret_t *cmr = NULL;
  modret_t *dmr = NULL;
  char *query = NULL;
  cmd_rec *close_cmd;

  sql_log(DEBUG_FUNC, "%s", "entering \tpostgres cmd_insert");

  _sql_check_cmd(cmd, "cmd_insert");

  if ((cmd->argc != 2) && (cmd->argc != 4)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_insert");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION, "badly formed request");
  }

  /* get the named connection */
  entry = _sql_get_connection(cmd->argv[0]);
  if (!entry) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_insert");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION,
      "unknown named connection");
  }

  conn = (db_conn_t *) entry->data;

  cmr = cmd_open(cmd);
  if (MODRET_ERROR(cmr)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_insert");
    return cmr;
  }

  /* construct the query string */
  if (cmd->argc == 2) {
    query = pstrcat(cmd->tmp_pool, "INSERT ", cmd->argv[1], NULL);
  } else {
    query = pstrcat( cmd->tmp_pool, "INSERT INTO ", cmd->argv[1], " (",
		     cmd->argv[2], ") VALUES (", cmd->argv[3], ")",
		     NULL );
  }

  /* log the query string */
  sql_log(DEBUG_INFO, "query \"%s\"", query);

  /* perform the query.  if it doesn't work, log the error, close the
   * connection then return the error from the query processing.
   */
  if (!(conn->result = PQexec(conn->postgres, query)) ||
      (PQresultStatus(conn->result) != PGRES_COMMAND_OK)) {
    dmr = _build_error( cmd, conn );

    if (conn->result) PQclear(conn->result);

    close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
    cmd_close(close_cmd);
    SQL_FREE_CMD(close_cmd);

    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_insert");
    return dmr;
  }

  PQclear(conn->result);

  /* close the connection and return HANDLED. */
  close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
  cmd_close(close_cmd);
  SQL_FREE_CMD(close_cmd);

  sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_insert");
  return PR_HANDLED(cmd);
}
Exemple #11
0
/*
 * cmd_select: executes a SELECT query. properly constructing the query
 *  based on the inputs.  See mod_sql.h for the definition of the _sql_data
 *  structure which is used to return the result data.
 *
 * cmd_select takes either exactly two inputs, or more than two.  If only
 *  two inputs are given, the second is a monolithic query string.  See 
 *  the examples below.
 *
 * Inputs:
 *  cmd->argv[0]: connection name
 *  cmd->argv[1]: table 
 *  cmd->argv[2]: select string
 * Optional:
 *  cmd->argv[3]: where clause 
 *  cmd->argv[4]: requested number of return rows (LIMIT)
 *  
 *  etc.        : other options, such as "GROUP BY", "ORDER BY",
 *                and "DISTINCT" will start at cmd->arg[5].  All 
 *                backends MUST support 'DISTINCT', the other
 *                arguments are optional (but encouraged).         
 *
 * Returns:
 *  either a properly filled error modret_t if the select failed, or a 
 *  modret_t with the result data filled in.
 *
 * Example:
 *  These are example queries that would be executed for Postgres; other
 *  backends will have different SQL syntax.
 *  
 *  argv[] = "default","user","userid, count", "userid='aah'","2"
 *  query  = "SELECT userid, count FROM user WHERE userid='aah' LIMIT 2"
 *
 *  argv[] = "default","usr1, usr2","usr1.foo, usr2.bar"
 *  query  = "SELECT usr1.foo, usr2.bar FROM usr1, usr2"
 *
 *  argv[] = "default","usr1","foo",,,"DISTINCT"
 *  query  = "SELECT DISTINCT foo FROM usr1"
 *
 *  argv[] = "default","bar FROM usr1 WHERE tmp=1 ORDER BY bar"
 *  query  = "SELECT bar FROM usr1 WHERE tmp=1 ORDER BY bar"
 *
 * Notes:
 *  certain selects could return huge amounts of data.  do whatever is
 *  possible to minimize the amount of data copying here.
 */
MODRET cmd_select(cmd_rec *cmd) {
  conn_entry_t *entry = NULL;
  db_conn_t *conn = NULL;
  modret_t *cmr = NULL;
  modret_t *dmr = NULL;
  char *query = NULL;
  int cnt = 0;
  cmd_rec *close_cmd;

  sql_log(DEBUG_FUNC, "%s", "entering \tpostgres cmd_select");

  _sql_check_cmd(cmd, "cmd_select");

  if (cmd->argc < 2) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION, "badly formed request");
  }

  /* get the named connection */
  entry = _sql_get_connection(cmd->argv[0]);
  if (!entry) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
    return PR_ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION,
      "unknown named connection");
  }
  
  conn = (db_conn_t *) entry->data;

  cmr = cmd_open(cmd);
  if (MODRET_ERROR(cmr)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
    return cmr;
  }

  /* construct the query string */
  if (cmd->argc == 2) {
    query = pstrcat(cmd->tmp_pool, "SELECT ", cmd->argv[1], NULL);
  } else {
    query = pstrcat( cmd->tmp_pool, cmd->argv[2], " FROM ", 
		     cmd->argv[1], NULL );
    if ((cmd->argc > 3) && (cmd->argv[3]))
      query = pstrcat( cmd->tmp_pool, query, " WHERE ", cmd->argv[3], NULL );
    if ((cmd->argc > 4) && (cmd->argv[4]))
      query = pstrcat( cmd->tmp_pool, query, " LIMIT ", cmd->argv[4], NULL );
    if (cmd->argc > 5) {

      /* handle the optional arguments -- they're rare, so in this case
       * we'll play with the already constructed query string, but in 
       * general we should probably take optional arguments into account 
       * and put the query string together later once we know what they are.
       */
    
      for (cnt=5; cnt < cmd->argc; cnt++) {
	if ((cmd->argv[cnt]) && !strcasecmp("DISTINCT",cmd->argv[cnt])) {
	  query = pstrcat( cmd->tmp_pool, "DISTINCT ", query, NULL);
	}
      }
    }

    query = pstrcat( cmd->tmp_pool, "SELECT ", query, NULL);    
  }

  /* log the query string */
  sql_log(DEBUG_INFO, "query \"%s\"", query);

  /* perform the query.  if it doesn't work, log the error, close the
   * connection then return the error from the query processing.
   */
  if (!(conn->result = PQexec(conn->postgres, query)) ||
      (PQresultStatus(conn->result) != PGRES_TUPLES_OK)) {
    dmr = _build_error( cmd, conn );

    if (conn->result) PQclear(conn->result);

    close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
    cmd_close(close_cmd);
    SQL_FREE_CMD(close_cmd);

    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
    return dmr;
  }

  /* get the data. if it doesn't work, log the error, close the
   * connection then return the error from the data processing.
   */
  dmr = _build_data( cmd, conn );

  PQclear(conn->result);

  if (MODRET_ERROR(dmr)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");

    close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
    cmd_close(close_cmd);
    SQL_FREE_CMD(close_cmd);

    return dmr;
  }    

  /* close the connection, return the data. */
  close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
  cmd_close(close_cmd);
  SQL_FREE_CMD(close_cmd);

  sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
  return dmr;
}
Exemple #12
0
static void
stroke_path(svg_state_t *state, const char *str)
{
  float values[6];
  int num_params = 0;
  int cur_param = 0;
  void (*cur_cmd)(svg_state_t *state, const float *params) = NULL; 

  while(*str) {
    if(*str < 33) {
      str++;
      continue;
    }

    if(cur_cmd != NULL) {
      const char *endptr;

      double d = my_str2double(str, &endptr);

      if(endptr != str) {
	values[cur_param] = d;
	cur_param++;
	if(cur_param == num_params) {
	  cur_cmd(state, values);
	  cur_param = 0;
	}

	str = endptr;

	while(*str < 33 && *str)
	  str++;
	if(*str == ',')
	  str++;
	while(*str < 33 && *str)
	  str++;
	continue;
      } 
    }

    while(*str < 33 && *str)
      str++;

    char mode = *str++;
    switch(mode) {
    case 'M':
      cur_cmd = cmd_move_abs;
      num_params = 2;
      break;
    case 'm':
      cur_cmd = cmd_move_rel;
      num_params = 2;
      break;
    case 'c':
      cur_cmd = cmd_curveto_rel;
      num_params = 6;
      break;

    case 'C':
      cur_cmd = cmd_curveto_abs;
      num_params = 6;
      break;

    case 'l':
      cur_cmd = cmd_lineto_rel;
      num_params = 2;
      break;

    case 'L':
      cur_cmd = cmd_lineto_abs;
      num_params = 2;
      break;

    case 'z':
    case 'Z':
      cur_cmd = NULL;
      num_params = 0;
      cmd_close(state);
      break;
    default:
      printf("Cant handle mode %c\n", mode);
      return;
    }
  }
}
/*
 * cmd_query: executes a freeform query string, with no syntax checking.
 *
 * cmd_query takes exactly two inputs, the connection and the query string.
 *
 * Inputs:
 *  cmd->argv[0]: connection name
 *  cmd->argv[1]: query string
 *
 * Returns:
 *  depending on the query type, returns a modret_t with data, a non-error
 *  modret_t, or a properly filled error modret_t if the query failed.
 *
 * Example:
 *  None.  The query should be passed directly to the backend database.
 *  
 * Notes:
 *  None.
 */
MODRET cmd_query(cmd_rec *cmd) {
  conn_entry_t *entry = NULL;
  db_conn_t *conn = NULL;
  modret_t *cmr = NULL;
  modret_t *dmr = NULL;
  char *query = NULL;
  cmd_rec *close_cmd;

  sql_log(DEBUG_FUNC, "%s", "entering \tpostgres cmd_query");

  _sql_check_cmd(cmd, "cmd_query");

  if (cmd->argc != 2) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_query");
    return ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION, "badly formed request");
  }

  /* get the named connection */
  entry = _sql_get_connection(cmd->argv[0]);
  if (!entry) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_query");
    return ERROR_MSG(cmd, MOD_SQL_POSTGRES_VERSION, "unknown named connection");
  }

  conn = (db_conn_t *) entry->data;

  cmr = cmd_open(cmd);
  if (MODRET_ERROR(cmr)) {
    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_query");
    return cmr;
  }

  query = pstrcat(cmd->tmp_pool, cmd->argv[1], NULL);

  /* log the query string */
  sql_log( DEBUG_INFO, "query \"%s\"", query); 

  /* perform the query.  if it doesn't work, log the error, close the
   * connection then return the error from the query processing.
   */
  if (!(conn->result = PQexec(conn->postgres, query)) ||
      ((PQresultStatus(conn->result) != PGRES_TUPLES_OK) &&
       (PQresultStatus(conn->result) != PGRES_COMMAND_OK))) {
    dmr = _build_error( cmd, conn );

    if (conn->result) PQclear(conn->result);

    close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
    cmd_close(close_cmd);
    SQL_FREE_CMD(close_cmd);

    sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_select");
    return dmr;
  }

  /* get data if necessary. if it doesn't work, log the error, close the
   * connection then return the error from the data processing.
   */

  if ( PQresultStatus( conn->result ) == PGRES_TUPLES_OK ) {
    dmr = _build_data( cmd, conn );

    PQclear(conn->result);

    if (MODRET_ERROR(dmr)) {
      sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_query");
    }
  } else {
    dmr = HANDLED(cmd);
  }

  /* close the connection, return the data. */
  close_cmd = _sql_make_cmd( cmd->tmp_pool, 1, entry->name );
  cmd_close(close_cmd);
  SQL_FREE_CMD(close_cmd);

  sql_log(DEBUG_FUNC, "%s", "exiting \tpostgres cmd_query");
  return dmr;
}
Exemple #14
0
/**
 * Read user input.
 *
 * @param prompt Prompt
 * @return The read line (dynamically allocated) or NULL
 */
char *
readline(const char *prompt)
{
    char *out;
    const int sleep_time_milliseconds = 90;
    volatile struct readline_session_context *ctx;
    wchar_t *buf_p = &g_push_back_buf[0];

    ctx = new_session(prompt);
    if (setjmp(g_readline_loc_info) != 0) {
	session_destroy(ctx);
	mutex_unlock(&g_puts_mutex);
	return NULL;
    }

    g_readline_loop    = true;
    g_resize_requested = false;
    g_hist_next = false;
    g_hist_prev = false;

    mutex_lock(&g_puts_mutex);
    write_cmdprompt(ctx->act, ctx->prompt, ctx->prompt_size);
    mutex_unlock(&g_puts_mutex);

    do {
	wint_t wc;

	ctx->insert_mode = (ctx->bufpos != ctx->n_insert);
	ctx->no_bufspc	 = (ctx->n_insert + 1 >= readline_buffersize);

	if (*buf_p) {
	    wc = *buf_p++;
	} else if (wget_wch(ctx->act, &wc) == ERR) {
	    (void) napms(sleep_time_milliseconds);
	    continue;
	}

	mutex_lock(&g_puts_mutex);

	switch (wc) {
	case CTRL_A:
	    while (ctx->bufpos != 0) {
		case_key_left(ctx);
		ctx->insert_mode = (ctx->bufpos != ctx->n_insert);
	    }
	    break;
	case CTRL_E:
	    while (ctx->insert_mode) {
		case_key_right(ctx);
		ctx->insert_mode = (ctx->bufpos != ctx->n_insert);
	    }
	    break;
	case MY_KEY_DLE:
	    window_select_prev();
	    break; /* CTRL+P */
	case MY_KEY_SO:
	    window_select_next();
	    break; /* CTRL+N */
	case KEY_DOWN:
	    g_hist_next = true;
	    session_destroy(ctx);
	    mutex_unlock(&g_puts_mutex);
	    return NULL;
	case KEY_UP:
	    g_hist_prev = true;
	    session_destroy(ctx);
	    mutex_unlock(&g_puts_mutex);
	    return NULL;
	case KEY_LEFT: case MY_KEY_STX:
	    case_key_left(ctx);
	    break;
	case KEY_RIGHT: case MY_KEY_ACK:
	    case_key_right(ctx);
	    break;
	case KEY_BACKSPACE: case MY_KEY_BS:
	    case_key_backspace(ctx);
	    break;
	case KEY_F(5): case BLINK:
	    handle_key(ctx, btowc(BLINK));
	    break;
	case KEY_F(6): case BOLD_ALIAS:
	    handle_key(ctx, btowc(BOLD));
	    break;
	case KEY_F(7): case COLOR:
	    handle_key(ctx, btowc(COLOR));
	    break;
	case KEY_F(8): case NORMAL:
	    handle_key(ctx, btowc(NORMAL));
	    break;
	case KEY_F(9): case REVERSE:
	    handle_key(ctx, btowc(REVERSE));
	    break;
	case KEY_F(10): case UNDERLINE:
	    handle_key(ctx, btowc(UNDERLINE));
	    break;
	case KEY_F(11):
	    cmd_close("");
	    break;
	case KEY_F(12):
	    window_close_all_priv_conv();
	    break;
	case KEY_DC: case MY_KEY_EOT:
	    case_key_dc(ctx);
	    break;
	case KEY_NPAGE:
	    window_scroll_down(g_active_window);
	    break;
	case KEY_PPAGE:
	    window_scroll_up(g_active_window);
	    break;
	case '\t':
	    break;
	case '\n': case KEY_ENTER: case WINDOWS_KEY_ENTER:
	    g_readline_loop = false;
	    break;
	case KEY_RESIZE:
	case MY_KEY_RESIZE:
	    g_resize_requested = true;
	    /*FALLTHROUGH*/
	case '\a':
	    session_destroy(ctx);
	    mutex_unlock(&g_puts_mutex);
	    return NULL;
	default:
	    if (iswprint(wc)) {
		handle_key(ctx, wc);
	    }
	    break;
	}

	mutex_unlock(&g_puts_mutex);
    } while (g_readline_loop);

    if (ctx->n_insert > 0) {
	ctx->buffer[ctx->n_insert] = 0L;
    } else {
	session_destroy(ctx);
	return NULL;
    }

    mutex_lock(&g_puts_mutex);
    write_cmdprompt(ctx->act, "", 0);
    mutex_unlock(&g_puts_mutex);

    out = finalize_out_string(ctx->buffer);
    session_destroy(ctx);

    return out;
}
Exemple #15
0
static void
svg_parse_element(const svg_state_t *s0, htsmsg_t *element, 
		  int (*element_parser)(svg_state_t *s, htsmsg_t *element))
{
  svg_state_t s = *s0;
  //  FT_Outline ol;
  //  FT_UInt points;
  //  FT_UInt contours;

  int fill_color = 0xffffffff;
  int stroke_color = 0xffffffff;
  int stroke_width = 0;

  htsmsg_t *a = htsmsg_get_map(element, "attrib");
  if(a == NULL)
    return;

  const char *st = htsmsg_get_str(a, "style");
  if(st != NULL) {
    char *style, *tmp = NULL, *n, *attr;
    n = style = strdup(st);

    while((attr = strtok_r(n, ";", &tmp)) != NULL) {
      char *value = strchr(attr, ':');
      if(value != NULL) {
	*value++ = 0;
	while(*value < 33 && *value)
	  value++;

	if(!strcmp(attr, "fill")) {

	  if(!strcmp(value, "none"))
	    fill_color = 0;
	  else {
	    fill_color = (fill_color & 0xff000000) | html_makecolor(value);
	  }
	} else if(!strcmp(attr, "stroke")) {

	  if(!strcmp(value, "none"))
	    stroke_color = 0;
	  else {
	    stroke_color = (stroke_color & 0xff000000) | html_makecolor(value);
	  }
	} else if(!strcmp(attr, "stroke-width")) {
	  stroke_width = atoi(value);
	}
      }
      n = NULL;
    }
    free(style);
  }

  if(s.pm == NULL)
    return;

  const char *transform = htsmsg_get_str(a, "transform");
  if(transform != NULL)
    svg_parse_transform(&s, transform);


  vec_emit_0(s.pm, VC_BEGIN);

  if(fill_color) {
    vec_emit_i1(s.pm, VC_SET_FILL_ENABLE, 1);
    vec_emit_i1(s.pm, VC_SET_FILL_COLOR, fill_color);
  }

  if(stroke_width) {
    vec_emit_i1(s.pm, VC_SET_STROKE_WIDTH, stroke_width);
    vec_emit_i1(s.pm, VC_SET_STROKE_COLOR, stroke_color);
  }

  s.cur[0] = 0;
  s.cur[1] = 0;

  if(element_parser(&s, a))
    return;

  cmd_close(&s);
  vec_emit_0(s.pm, VC_END);
}
Exemple #16
0
void builder::node(string str, bool add_space) {
  string::size_type n, m;
  string s(str);

  while (true) {
    if (s.empty()) {
      break;

    } else if ((n = s.find("%{F-}")) == 0) {
      color_close(!m_lazy);
      s.erase(0, 5);

    } else if ((n = s.find("%{F#")) == 0 && (m = s.find("}")) != string::npos) {
      if (m - n - 4 == 2)
        color_alpha(s.substr(n + 3, m - 3));
      else
        color(s.substr(n + 3, m - 3));
      s.erase(n, m + 1);

    } else if ((n = s.find("%{B-}")) == 0) {
      background_close(!m_lazy);
      s.erase(0, 5);

    } else if ((n = s.find("%{B#")) == 0 && (m = s.find("}")) != string::npos) {
      background(s.substr(n + 3, m - 3));
      s.erase(n, m + 1);

    } else if ((n = s.find("%{T-}")) == 0) {
      font_close(!m_lazy);
      s.erase(0, 5);

    } else if ((n = s.find("%{T")) == 0 && (m = s.find("}")) != string::npos) {
      font(std::atoi(s.substr(n + 3, m - 3).c_str()));
      s.erase(n, m + 1);

    } else if ((n = s.find("%{U-}")) == 0) {
      line_color_close(!m_lazy);
      s.erase(0, 5);

    } else if ((n = s.find("%{U#")) == 0 && (m = s.find("}")) != string::npos) {
      line_color(s.substr(n + 3, m - 3));
      s.erase(n, m + 1);

    } else if ((n = s.find("%{+u}")) == 0) {
      underline();
      s.erase(0, 5);

    } else if ((n = s.find("%{+o}")) == 0) {
      overline();
      s.erase(0, 5);

    } else if ((n = s.find("%{-u}")) == 0) {
      underline_close(true);
      s.erase(0, 5);

    } else if ((n = s.find("%{-o}")) == 0) {
      overline_close(true);
      s.erase(0, 5);

    } else if ((n = s.find("%{A}")) == 0) {
      cmd_close(true);
      s.erase(0, 4);

    } else if ((n = s.find("%{")) == 0 && (m = s.find("}")) != string::npos) {
      append(s.substr(n, m + 1));
      s.erase(n, m + 1);

    } else if ((n = s.find("%{")) > 0) {
      append(s.substr(0, n));
      s.erase(0, n);

    } else
      break;
  }

  if (!s.empty())
    append(s);
  if (add_space)
    space();
}