Beispiel #1
0
struct cmd_result *parse_command(char * buf,struct cmd_result *(*treat_func)(),void *sdata) {

  //allocate the command structure to fill with data from xml
  struct cmd *toparse=malloc(sizeof(struct cmd));
  toparse->name=malloc(1024*sizeof(char));
  bzero(toparse->name,1024);
  toparse->nb_params=0;
  toparse->params=malloc(MAX_PARAMS*sizeof(char *));
  memset(toparse->params,0,MAX_PARAMS*sizeof(char*));
  struct cmd_result *result;
  
  XML_Parser parser = XML_ParserCreate(NULL);
  XML_SetUserData(parser,toparse);
  XML_SetElementHandler(parser,cmd_handler_start,cmd_handler_end);
  XML_SetCharacterDataHandler(parser,cmd_handler_data);

  if (!XML_Parse(parser,buf,strlen(buf),1)) {
    printf("error in command XML_Parse : error=%d\n",XML_GetErrorCode(parser));
    printf("%s\n",XML_ErrorString(XML_GetErrorCode(parser)));
    printf("buffer=%s\n",buf);
    result=malloc(sizeof(struct cmd_result));
    result->retcode=0;
    result->str=NULL;
  } else {
    result=treat_func(toparse,sdata);
  }
  free_cmd(toparse);
  XML_ParserFree(parser);
  if (result->str==NULL)
    result->str=strdup("Malformed command");
  return result;
}
Beispiel #2
0
static void end_cmd(struct nullb_cmd *cmd)
{
	struct request_queue *q = NULL;

	if (cmd->rq)
		q = cmd->rq->q;

	switch (queue_mode)  {
	case NULL_Q_MQ:
		blk_mq_end_request(cmd->rq, 0);
		return;
	case NULL_Q_RQ:
		INIT_LIST_HEAD(&cmd->rq->queuelist);
		blk_end_request_all(cmd->rq, 0);
		break;
	case NULL_Q_BIO:
		bio_endio(cmd->bio);
		break;
	}

	free_cmd(cmd);

	/* Restart queue if needed, as we are freeing a tag */
	if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
		unsigned long flags;

		spin_lock_irqsave(q->queue_lock, flags);
		blk_start_queue_async(q);
		spin_unlock_irqrestore(q->queue_lock, flags);
	}
}
static void run_test(void)
{
	unsigned int i, j;

	for (i = 0; i < NR_CMDS; i++) {
		const struct alloc_cmd *cmd = &cmds[i];

		if (cmd->size) {
			printk("ALLOC: %zu bytes marker=%d\n",
			       cmd->size, cmd->marker);
			ptrs[i] = __alloc_percpu(cmd->size,
					__alignof__(unsigned long long));
			if (ptrs[i])
				fill_area(ptrs[i], cmd->size, i);
			else
				printk("failed to allocate %zu bytes\n",
				       cmd->size);
			continue;
		}

		printk("FREE: marker=%d\n", cmd->marker);
		pcpu_dump_chunk_slots();
		for (j = 0; j < i; j++)
			if (cmds[j].marker == cmd->marker)
				free_cmd(j);
		printk("FREE: done\n");
		pcpu_dump_chunk_slots();
	}
Beispiel #4
0
//recursively frees a command
void free_cmd(command_t cmd) {
	
  //free input/output strings if they are not null
  if(cmd->input!=NULL){
    free(cmd->input);
	}
  if(cmd->output!=NULL){
    free(cmd->output);
}

  //check the command type
	//printf("type:%d", (int)cmd->type);
  switch (cmd->type) {
    case AND_COMMAND:
    case SEQUENCE_COMMAND:
    case OR_COMMAND:
    case PIPE_COMMAND:
    {
    	if(cmd->u.command[0]!=NULL || cmd->u.command[1]!=NULL) {
	        free_cmd(cmd->u.command[0]);
	        free_cmd(cmd->u.command[1]);
    	}
        break;
    }
    case SUBSHELL_COMMAND:
    	if(cmd->u.subshell_command!=NULL){
      		free_cmd(cmd->u.subshell_command);
  		}
      break;
    default: //free simple command
    {
    	if(cmd->u.word!=NULL) {
	      char** ptr = cmd->u.word;
	      char* str = *ptr;
		    if(ptr!=NULL) {
		      	if(str!=NULL)
		      		free(str);
			    free(ptr);
	  	    }
	  	}
      break;
    }
  }
    //free the command
  if(cmd!=NULL)
    free(cmd);
}
Beispiel #5
0
void free_cmdline(cmd_line *cl)
{
  if(cl  != NULL){
    free_cmd(cl->c);
    free_cmdline(cl->next);
    free(cl);
    return;
  }else {
    return;
  }
}
Beispiel #6
0
void free_cmd(cmd *c)
{
  if(c != NULL){
    free_rtoks(c->t);
    free_cmd(c->next);
    free(c);
    return;
  }else {
    return;
  } 
}
Beispiel #7
0
Datei: error.c Projekt: y0ja/21sh
void	free_all(t_all *all)
{
	if (all->env.env)
		ft_2dchardel(&all->env.env);
	if (all->env.path_dir)
		ft_2dchardel(&all->env.path_dir);
	if (all->cmd.args)
		free_cmd(&all->cmd);
	if (all->env.cmd2exec)
		ft_2dchardel(&all->env.cmd2exec);
}
Beispiel #8
0
static void end_cmd(struct nullb_cmd *cmd)
{
	if (cmd->rq) {
		if (queue_mode == NULL_Q_MQ)
			blk_mq_end_io(cmd->rq, 0);
		else {
			INIT_LIST_HEAD(&cmd->rq->queuelist);
			blk_end_request_all(cmd->rq, 0);
		}
	} else if (cmd->bio)
		bio_endio(cmd->bio, 0);

	if (queue_mode != NULL_Q_MQ)
		free_cmd(cmd);
}
Beispiel #9
0
/* Free all storage allocated by the parser.
   If ABORTING is set, terminate all tasks.  */
static void
cleanup (int aborting)
{
  int i;

  for (i = 0; i < cmds_index; i++)
    free_cmd (cmds[i], aborting);
  boot_script_free (cmds, sizeof cmds[0] * cmds_alloc);
  cmds = 0;
  cmds_index = cmds_alloc = 0;

  for (i = 0; i < symtab_index; i++)
    boot_script_free (symtab[i], sizeof *symtab[i]);
  boot_script_free (symtab, sizeof symtab[0] * symtab_alloc);
  symtab = 0;
  symtab_index = symtab_alloc = 0;
}
Beispiel #10
0
static void end_cmd(struct nullb_cmd *cmd)
{
	switch (queue_mode)  {
	case NULL_Q_MQ:
		blk_mq_end_request(cmd->rq, 0);
		return;
	case NULL_Q_RQ:
		INIT_LIST_HEAD(&cmd->rq->queuelist);
		blk_end_request_all(cmd->rq, 0);
		break;
	case NULL_Q_BIO:
		bio_endio(cmd->bio, 0);
		break;
	}

	free_cmd(cmd);
}
Beispiel #11
0
int			handle_cmd(\
			char *cmd_buffer)
{
	t_cmd	cmd;
	int		ret;

	ret = 1;
	cmd = parse_cmd(cmd_buffer);
	debug_cmd(cmd);
	if (cmd.not_found)
		nofound(cmd.arg_v[0]);
	else if (cmd.builtin)
		ret = cmd.built(cmd.arg_v);
	else
		exec_binary(cmd);
	free_cmd(&cmd);
	return (ret);
}
Beispiel #12
0
void cleanup(command_stream_t cs) {
  remove_globs();
  cs->iterator = cs->commands;
  if(cs->commands == NULL) {
    free(cs);
    return;
  }
  //iterate through the linked list of cmd_nodes
  cmd_node* it = cs->iterator;
  while(it) {
  	if(it->cmd!=NULL)
    	free_cmd(it->cmd);
    //save the next node and free the current node
    cmd_node* next_node = it->next;
    free(it); 
    it = next_node;
  }
  if(cs!=NULL)
  	free(cs); 
}
Beispiel #13
0
Datei: main.c Projekt: y0ja/21sh
int		main(void)
{
	t_all	all;
	int		i;

	f_all(&all);
	init_shell(&all, environ);
	while (42)
	{
		i = 0;
		display_prompt(&all);
		all.env.cmd2exec = scan_for_input(&all);
		while (all.env.cmd2exec[i])
		{
			if (parse_cmd(&all, all.env.cmd2exec[i]))
				exec_cmd_input(&all);
			free_cmd(&all.cmd);
			i++;
		}
		free(all.env.cmd2exec[i]);
		ft_memdel((void **)&all.env.cmd2exec);
	}
	return (0);
}
Beispiel #14
0
extern "C" int handler(void *c, const char* section, const char* name, const char* value){
	#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
	dbgpf("BOOT: [%s] %s=%s\n", section, name, value);

	if(MATCH(current_section, "configure_console")){
		configure_console(value);
	}else if(MATCH(current_section, "load")){
		char *name, *params;
		if(split(value, ',', &name, &params)){
			dbgpf("BOOT: %s,%s\n", name, params);
			module_load(name, params);
			free(name);
			free(params);
		}else module_load((char*)value, NULL);

	}else if(MATCH(current_section, "run")){
		cmdLine cmd = parse_cmd(value);
		wait(spawn(cmd.cmd, cmd.argc, cmd.argv));
		free_cmd(cmd);
    }else if(MATCH(current_section, "spawn")){
		spawn("INIT:/SPAWN.ELX", 1, (char**)&value);
	}else if(MATCH(current_section, "service")){
		char *varname, *cmd;
		if(split(value, ' ', &varname, &cmd)){
			cmdLine cmdL = parse_cmd(cmd);
			pid_t pid = spawn(cmdL.cmd, cmdL.argc, cmdL.argv);
			free_cmd(cmdL);
			char buf[64] = {0};
			sprintf(buf, "%i", (int)pid);
			setenv(varname, buf, 0, 0);
			free(varname);
			free(cmd);
		}
	}else if(MATCH(current_section, "mount")){
		char *path, *rest;
		if(split(value, ',', &path, &rest)){
			char *name, *fs;
			if(split(rest, ',', &name, &fs)){
				if(!mount(name, path, fs)){
					char errormsg[128];
					sprintf(errormsg, "(BOOT) Could not mount %s.\n", path);
					panic(errormsg);
				}
				free(name);
				free(fs);
			}
			free(path);
			free(rest);
		}
	}else if(MATCH(current_section, "setwait")){
		setenv(value, "-", ENV_Global, 0);
	}else if(MATCH(current_section, "waitfor")){
		char *v = getenv(value, 0);
		while(strlen(v) <= 1){
			yield();
			v = getenv(value, 0);
		}
		setenv(value, "", ENV_Global, 0);
	}else if(strcmp(section, current_section) == 0 && starts_with("set ", name)){
        char *set, *varname;
        if(split(name, ' ', &set, &varname)){
            setenv(varname, (char*)value, 0, 0);
            free(set);
            free(varname);
        }
    }else if(strcmp(section, current_section) == 0 && starts_with("kset ", name)){
        char *set, *varname;
        if(split(name, ' ', &set, &varname)){
            set_kvar(varname, (char*)value);
            free(set);
            free(varname);
        }
	}
	return 1;
}
Beispiel #15
0
/* Parse the command line CMDLINE.  */
int
boot_script_parse_line (void *hook, char *cmdline)
{
  char *p, *q;
  int error;
  struct cmd *cmd;
  struct arg *arg;

  /* Extract command name.  Ignore line if it lacks a command.  */
  for (p = cmdline; *p == ' ' || *p == '\t'; p++)
    ;
  if (*p == '#')
    /* Ignore comment line.  */
    return 0;

#if 0
  if (*p && *p != ' ' && *p != '\t' && *p != '\n')
    {
      printf ("(bootstrap): %s\n", cmdline);
    }
#endif

  for (q = p; *q && *q != ' ' && *q != '\t' && *q != '\n'; q++)
    ;
  if (p == q)
      return 0;

  *q = '\0';

  /* Allocate a command structure.  */
  cmd = boot_script_malloc (sizeof (struct cmd));
  if (! cmd)
    return BOOT_SCRIPT_NOMEM;
  memset (cmd, 0, sizeof (struct cmd));
  cmd->hook = hook;
  cmd->path = p;
  p = q + 1;

  for (arg = 0;;)
    {
      if (! arg)
	{
	  /* Skip whitespace.  */
	  while (*p == ' ' || *p == '\t')
	    p++;

	  /* End of command line.  */
	  if (! *p || *p == '\n')
	    {
	      /* Add command to list.  */
	      if (add_list (cmd, (void ***) &cmds,
			    &cmds_alloc, &cmds_index, 10))
		{
		  error = BOOT_SCRIPT_NOMEM;
		  goto bad;
		}
	      return 0;
	    }
	}

      /* Look for a symbol.  */
      if (arg || (*p == '$' && (*(p + 1) == '{' || *(p + 1) == '(')))
	{
	  char end_char = (*(p + 1) == '{') ? '}' : ')';
	  struct sym *sym = 0;

	  for (p += 2;;)
	    {
	      char c;
	      int i, val, type;
	      struct sym *s;

	      /* Parse symbol name.  */
	      for (q = p; *q && *q != '\n' && *q != end_char && *q != '='; q++)
		;
	      if (p == q || ! *q || *q == '\n'
		  || (end_char == '}' && *q != '}'))
		{
		  error = BOOT_SCRIPT_SYNTAX_ERROR;
		  goto bad;
		}
	      c = *q;
	      *q = '\0';

	      /* See if this is a builtin symbol.  */
	      for (i = 0; i < NUM_BUILTIN; i++)
		if (! strcmp (p, builtin_symbols[i].name))
		  break;

	      if (i < NUM_BUILTIN)
		s = &builtin_symbols[i];
	      else
		{
		  /* Look up symbol in symbol table.
		     If no entry exists, create one.  */
		  s = sym_lookup (p);
		  if (! s)
		    {
		      s = sym_enter (p);
		      if (! s)
			{
			  error = BOOT_SCRIPT_NOMEM;
			  goto bad;
			}
		    }
		}

	      /* Only values are allowed in ${...} constructs.  */
	      if (end_char == '}' && s->type == VAL_FUNC)
		return BOOT_SCRIPT_INVALID_SYM;

	      /* Check that assignment is valid.  */
	      if (c == '=' && s->type == VAL_FUNC)
		{
		  error = BOOT_SCRIPT_INVALID_ASG;
		  goto bad;
		}

	      /* For function symbols, execute the function.  */
	      if (s->type == VAL_FUNC)
		{
		  if (! s->run_on_exec)
		    {
		      (error
		       = ((*((int (*) (struct cmd *, int *)) s->val))
			  (cmd, &val)));
		      if (error)
			goto bad;
		      type = s->ret_type;
		    }
		  else
		    {
		      if (add_list (s, (void ***) &cmd->exec_funcs,
				    &cmd->exec_funcs_alloc,
				    &cmd->exec_funcs_index, 5))
			{
			  error = BOOT_SCRIPT_NOMEM;
			  goto bad;
			}
		      type = VAL_NONE;
		      goto out;
		    }
		}
	      else if (s->type == VAL_NONE)
		{
		  type = VAL_SYM;
		  val = (int) s;
 		}
	      else
		{
		  type = s->type;
		  val = s->val;
		}

	      if (sym)
		{
		  sym->type = type;
		  sym->val = val;
		}
	      else if (arg)
		{
		  arg->type = type;
		  arg->val = val;
		}

	    out:
	      p = q + 1;
	      if (c == end_char)
		{
		  /* Create an argument if necessary.
		     We create an argument if the symbol appears
		     in the expression by itself.

		     NOTE: This is temporary till the boot filesystem
		     servers support arguments.  When that happens,
		     symbol values will only be printed if they're
		     associated with an argument.  */
		  if (! arg && end_char == '}')
		    {
		      if (! add_arg (cmd, 0, type, val))
			{
			  error = BOOT_SCRIPT_NOMEM;
			  goto bad;
			}
		    }
		  arg = 0;
		  break;
		}
	      if (s->type != VAL_FUNC)
		sym = s;
	    }
	}
      else
	{
	  char c;

	  /* Command argument; just copy the text.  */
	  for (q = p;; q++)
	    {
	      if (! *q || *q == ' ' || *q == '\t' || *q == '\n')
		break;
	      if (*q == '$' && *(q + 1) == '{')
		break;
	    }
	  c = *q;
	  *q = '\0';

	  /* Add argument to list.  */
	  arg = add_arg (cmd, p, VAL_NONE, 0);
	  if (! arg)
	    {
	      error = BOOT_SCRIPT_NOMEM;
	      goto bad;
	    }
	  if (c == '$')
	    p = q;
	  else
	    {
	      if (c)
		p = q + 1;
	      else
		p = q;
	      arg = 0;
	    }
	}
    }


 bad:
  free_cmd (cmd, 1);
  cleanup (1);
  return error;
}
Beispiel #16
0
int main(int argc, char **argv)
{
	char **exec_argv;
	pid_t pid;
	int mode;
	int cmd_in, cmd_out;
	struct pipe_i pipe_out, pipe_err;
	int std_in, std_out, std_err;  /* */
	int skip_n;

	init(NULL);
	init_pipe_n_table();
	std_in  = STDIN_FILENO;
	std_out = STDOUT_FILENO;
	std_err = STDERR_FILENO;

	while(11 == 11)
	{
		fprintf(stdout, "%% ");
fflush(stdout);

		while(11 == 11)
		{
			if((mode = cmd(&cmd_in, &cmd_out, &skip_n)) == EOF)
			{
				fprintf(stdout, "\n");
fflush(stdout);

				goto EXIT;
			}

/* fprintf(stdout, "[DEBUG] mode = %d \n", mode); */
			exec_argv = get_cmd_argv();
			/* fprintf(stdout, "%s\n", exec_argv[0]); */
/*
char **argp = exec_argv;
while(*argp != NULL)
{
	fprintf(stderr, "%s\n", *argp);
	++argp;
}
*/

			/* Shell command */
			if(strcmp(exec_argv[0], "printenv") == 0)
			{
				printenv(exec_argv[1]);

				break;
			}
			else if(strcmp(exec_argv[0], "setenv") == 0)
			{
				if(exec_argv[1] != NULL)
					if(exec_argv[2] == NULL)
						_setenv(exec_argv[1], "");
					else
						_setenv(exec_argv[1], exec_argv[2]);
				else
				{
					fprintf(stdout, "usage\n");
					fflush(stdout);
				}

				break;
			}
			else if(strcmp(exec_argv[0], "exit") == 0)
			{
				if(exec_argv[1] != NULL)
				{
					fprintf(stdout, "usage\n");
					fflush(stdout);
				}
				else
					goto EXIT;
			}
			else  /* Normal command */
			{
/*
				if(mode == '|')
				{
*/
					if(pipe((int *)&pipe_out) == -1)
						perror("pipe (std_out)");

					std_out = pipe_out.wr;
					/* check cmd_out */
/*
				}
*/

/*
				if(cmd_in != STDIN_FILENO)
					std_in = cmd_in;   check pipe settings 
				if(cmd_out != STDOUT_FILENO)
					std_out = cmd_out;   L92 
*/

/*
fprintf(stderr, "[DEBUG] mode = %c\n", mode);
fprintf(stderr, "[DEBUG] pipe_out.rd = %d, pipe_out.wr = %d\n", pipe_out.rd, pipe_out.wr);
fprintf(stderr, "[DEBUG] std_in = %d, std_out = %d, std_err = %d\n", std_in, std_out, std_err);
*/
				if((pid = fork()) == 0)  /* Child process */
				{
/*
fprintf(stderr, "[CHILD] forked\n");
*/
/*
					if(mode == '|')
*/
						close(pipe_out.rd);

					if(std_in != STDIN_FILENO)
					{
						if(dup2(std_in, STDIN_FILENO) == -1)
							perror("dup2 (std_in)");

						close(std_in);
					}

					if(std_out != STDOUT_FILENO)
					{
						if(dup2(std_out, STDOUT_FILENO) == -1)
							perror("dup2 (std_out)");

						close(std_out);
					}

					if(std_err != STDERR_FILENO)
					{
						if(dup2(std_err, STDERR_FILENO) == -1)
							perror("dup2 (std_err)");

						close(std_err);
					}

					if(execvp(exec_argv[0], exec_argv) == -1)
					{
						perror("execvp");

						exit(EXIT_FAILURE);
					}
				}
				else
				{
/*
fprintf(stderr, "[PARENT] forked\n");
*/
/*
					if(mode == '|')
*/
						close(pipe_out.wr);  /* equivalent to close(std_out); */

					if(std_in != STDIN_FILENO)
						close(std_in);

					/************* FOR NEXT EXECUTION *************/

					std_in  = STDIN_FILENO;
					std_out = STDOUT_FILENO;
					std_err = STDERR_FILENO;

/*
					if(mode == '|')
					{
*/
						std_in = pipe_out.rd;  /* Prepare the fd of stdin for the next cmd */

						if(skip_n != 0)  /* TODO: Eliminate some redundent code */
						{
							if(pipe((int *)&pipe_out) == -1)
								perror("[clone] pipe (pipe_out)");

							/* std_out = pipe_out.wr; */

							if(pipe((int *)&pipe_err) == -1)
								perror("[clone] pipe (std_err)");

							/* std_err = pipe_err.wr; */

							if(fork() == 0)
							{
								close(pipe_out.rd);
								close(pipe_err.rd);

								if(dup2(std_in, STDIN_FILENO) == -1)
									perror("[clone] dup2 (std_in)");
								close(std_in);

								if(dup2(pipe_out.wr, STDOUT_FILENO) == -1)
									perror("[clone] dup2 (pipe_out)");

								close(pipe_out.wr);

								if(dup2(pipe_err.wr, STDERR_FILENO) == -1)
									perror("dup2 (pipe_err)");

								close(pipe_err.wr);
							
								if(execlp("clone", "clone", NULL) == -1)
								{
									perror("execlp");

									exit(EXIT_FAILURE);
								}
							}
							else
							{
								close(pipe_out.wr);
								close(pipe_err.wr);
/*
fprintf(stderr, "skip_n = %d\n", skip_n);
*/
								add_pipe_n_entry(skip_n, pipe_err.rd);

								close(std_in);
								std_in = pipe_out.rd;
							}
						}
/*
					}
*/
					/* Merge multiple stream */
					struct pipe_n_entry *zero = get_pipe_n_entry();
/*
fprintf(stderr, "zero->len = %u\n", zero->len);
*/
					if(zero->len != 0)
					{
						int i, pipe_n_fd;
						char buf[1024];
						ssize_t n;

						if(pipe((int *)&pipe_out) == -1)
							perror("[MERGE] pipe (pipe_out)");
						for(i = 0; i < zero->len; ++i)
						{
							pipe_n_fd = zero->pipes[i];

							while((n = read(pipe_n_fd, buf, sizeof(buf))) != 0)
								write(pipe_out.wr, buf, n);
							close(pipe_n_fd);
						}

						while((n = read(std_in, buf, sizeof(buf))) != 0)
							write(pipe_out.wr, buf, n);
						close(std_in);

						close(pipe_out.wr);
						std_in = pipe_out.rd;
					}
					rotate_pipe_n_table();  /* */

					if(mode == '\n')  /* END */
					{
						char buf[1024];
						ssize_t n;

						if(cmd_out != STDOUT_FILENO)
							std_out = cmd_out;

						while((n = read(std_in, buf, sizeof(buf))) != 0)
							write(std_out, buf, n);
						close(std_in);

						int i, j, max = get_pipe_n_max() + 1;
/*
fprintf(stderr, "max = %d\n", max);
*/
						int len, pipe_n_fd;
						struct pipe_n_entry *zero;
						for(i = 0; i < max; ++i)
						{
							zero = get_pipe_n_entry();
/*
fprintf(stderr, "[AFTER] zero->len = %u\n", zero->len);
*/
							len = zero->len;

							for(j = 0; j < len; ++j)
							{
/*
fprintf(stderr, "i = %d, j = %d\n", i, j);
*/
								close(zero->pipes[j]);
/*
								pipe_n_fd = zero->pipes[j];

								while((n = read(pipe_n_fd, buf, sizeof(buf))) != 0)
									write(std_out, buf, n);
*/
							}

							rotate_pipe_n_table();
						}

						std_in  = STDIN_FILENO;
						std_out = STDOUT_FILENO;
						std_err = STDERR_FILENO;
						
						waitpid(pid, NULL, 0);  /* Can be removed since the above snippet will confirm the termination of last command */
						while(waitpid(-1, NULL, WNOHANG) > 0)
						{
						}

						break;
					}
				}
			}

			free_cmd();
		}
		free_cmd();
	}
EXIT:

	return 0;
}
static int ui_android_read(UI *ui, UI_STRING *uis)
{
    int ok = 0;
    int num = 0;
    const char *str;
    char **args;

    switch (UI_get_string_type(uis)) {
	case UIT_BOOLEAN:
	    android_log(PRG_DEBUG, "called ui_android_read() output boolean %s",
	                UI_get0_output_string(uis));
	    android_log(PRG_DEBUG, "called ui_android_read() action boolean %s",
	                UI_get0_action_string(uis));
	    //			UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO
	case UIT_PROMPT:
        str = UI_get0_output_string(uis);
	    android_log(PRG_DEBUG, "called ui_android_read() output prompt %s",
                    str);
	    break;
	case UIT_VERIFY:
        str = UI_get0_output_string(uis);
	    android_log(PRG_DEBUG, "called ui_android_read() verify output %s",
                    str);
	    break;
	default:
        return 1;
	    break;
    }

    strlcat(ui_buf, "=X=", sizeof(ui_buf));
    strlcat(ui_buf, str, sizeof(ui_buf));

process:
    if (send_req(ui_buf) < 0) {
        android_log(PRG_ERR, "send_req() failed");
        goto out;
    }

    if (recv_cmd(&num, &args) < 0) {
        android_log(PRG_ERR, "recv_cmd() failed");
        goto out;
    }
    if (num != 1) {
        android_log(PRG_ERR, "parameter number mismatch");
        goto out;
    }

    android_log(PRG_DEBUG, "ui_android_read() cmd: %s %d", args[0], num);

    send_ack(num);

    UI_set_result(ui, uis, args[0]);

    ok = 1;

out:
    if (num)
        free_cmd(num, args);
    ui_buf[0] = '\0';
    return ok;
}
Beispiel #18
0
int main(int argc, char *argv[])
{
	int ret;
	struct tsdev *ts;
	lua_State *L;

	myfb = myfb_open();
	set_vfb_buf(1);
	ts = ts_init();

	L = luaL_newstate();
	luaL_openlibs(L);

	lua_register(L, "SET_COUNT", lua_set_icon_count);
	lua_register(L, "SET_ICON", lua_set_icon_info);
	lua_register(L, "SET_CMD", lua_set_cmd_list);
	lua_register(L, "SET_BGCOLOR", lua_set_bgcolor);
	lua_register(L, "SET_BGIMAGE", lua_set_bgimage);

	luaL_dofile(L, file_ui);
	lua_close(L);

	while (1) {
		int c, da_count, old_da_count, pid, state = 0;
		struct ts_sample samp;
		struct icon *i;
		struct cmd_list *j;

		da_count = get_DeviceAttached();
		if (!da_count)
			da_count = 0;

		if (!old_da_count)
			old_da_count = da_count;
		else {
			if (old_da_count != da_count) {
				clear_screen();
				put_string_center(myfb->fbvar.xres/2, myfb->fbvar.yres/2 - 10, "Attached Info is changed!", white);
				put_string_center(myfb->fbvar.xres/2, myfb->fbvar.yres/2 + 10, "Touch the screen!", white);
				old_da_count = da_count;
				ret = ts_read(ts, &samp, 1);
				continue;
			}
		}

		set_bgimage();
		set_bgcolor();
		for (c=0; c<=da_count; c++)
			draw_block(myfb->fbvar.xres-12*c-12, 3);
		
		if (head == NULL) {
			put_string_center(myfb->fbvar.xres/2, myfb->fbvar.yres/2, "Sorry, No Apps. registered!", white);
#ifdef DEBUG
			fprintf(stderr, "No Apps!\n");
#endif
			break;
		}

		for (i=head; i != NULL; i=i->next)
			draw_icon(i);

		ret = ts_read(ts, &samp, 1);
		if (ret < 0) {
			perror("ts_read");
			continue;
		}
		if (ret != 1)
			continue;

		if (samp.x > 310 && samp.y >230)
			break;

		for (i=head, j=cmdlist; i != NULL; i=i->next, j=j->next) {
			if (icon_handle(i, &samp) > 0) {
				if (chat_count < 20)
					continue;
				chat_count = 0;

				pid = fork();
				if (pid == 0) {
#ifdef DEBUG
					fprintf(stderr, " *** This is CHILD! ***\n");
#endif
					if (j)
						ret = execl(j->path, j->args, 0);
					if (ret < 0) {
						perror("execl");
						exit(1);
					}
				} else {
					sleep(1);
					wait(&state);
					no_count = 0;
					put_string_center(myfb->fbvar.xres/2, myfb->fbvar.yres/2, "End of Program!", white);
#ifdef DEBUG
					fprintf(stderr, " *** End of CHILD! ***\n");
#endif
				}
			}
		}
	}

	clear_screen();
	put_string_center(myfb->fbvar.xres/2, myfb->fbvar.yres/2, "Thanks for Use!", white);

	free_icon();
	free_cmd();
	ts_close(ts);
	free_vfb_buf(1);
	myfb_close();
	return 0;
}