Ejemplo n.º 1
0
t_uchar		run_setenv(char ***env, char **argv)
{
  char		*tmp;

  if (count_args(argv) < 2)
    {
      my_printenv(*env, '\n');
      return (0);
    }
  else if (count_args(argv) > 3 || !my_str_isalpha(argv[1]) ||
	   (!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0])))
    {
      if (count_args(argv) > 3)
	my_dprintf(STDERR, "setenv: Too many arguments.\n");
      else if ((!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0])))
	my_dprintf(STDERR, "setenv: Variable name must begin with a letter.\n");
      else if (!my_str_isalpha(argv[1]))
	my_dprintf(STDERR, ERALPH);
      return (1);
    }
  if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL)
    my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n");
  tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1]));
  tmp = my_strncat(tmp, "=", 1);
  my_setenv(env, tmp, argv[2]);
  free(tmp);
  return (0);
}
Ejemplo n.º 2
0
Archivo: exit.c Proyecto: Linezek/42sh
t_uchar		run_exit(char **env,
			 char **path,
			 t_command *command,
			 t_builtin_ptr **builtins)
{
  char		exit_status;

  if (count_args(command->argv_tmp) > 2)
    {
      my_dprintf(STDERR, "exit: Expression Syntax.\n");
      return (EXIT_FAILURE);
    }
  if (command->argv_tmp == NULL || count_args(command->argv_tmp) == 1)
    exit_status = command->last_ret;
  else if (my_str_isnum(command->argv_tmp[1]) == FALSE)
    {
      my_dprintf(STDERR, "exit: Expression Syntax.\n");
      return (EXIT_FAILURE);
    }
  else
    exit_status = my_atoi(command->argv_tmp[1]);
  if (command->interactive == TRUE)
    my_dprintf(STDERR, "exit\n");
  my_free_2d_tab(env);
  my_free_2d_tab(path);
  my_free_2d_tab(command->argv);
  free(command->argv_tmp);
  free_builtins(builtins);
  exit(exit_status);
  return (EXIT_SUCCESS);
}
Ejemplo n.º 3
0
int func_optimize(tree* my_tree, bool* is_optimized)
{
	assert(my_tree);
	if (my_tree -> type != TR_F) return TREE_OK;
	
	assert(is_optimized);
	assert(my_tree -> right);

	

	int argc = 0;
	int ret = 0;
	int number_of_func = 0;

	CALL(count_args(my_tree, &argc));
	CALL(verify_func(my_tree, argc, &number_of_func));

	if (is_possible_func_optimize(my_tree, argc) == false) return TREE_OK;

	double argv[MAXVARS] = {};

	CALL(get_args(my_tree, argv, (const int)argc));

	double value = 0;

	CALL((math_funcs[number_of_func].func)(argv, &value));
	
	CALL(tree_destr(my_tree -> right));
	CALL(tree_clear(my_tree));
	CALL(tree_burn(my_tree, TR_N, value));
	*is_optimized = true;

	return TREE_OK;
}
Ejemplo n.º 4
0
int irc_parse_action(irc_t *irc) {
    // :msg_prefix msg_irc_command * :msg_command msg_suffix
    if (!(strchr(irc->servbuf, ' '))) {
    	return 0; // empty msg
    }
    irc_log_message(irc);
    char* ptr = strtok(irc->servbuf, " ");
    if (ptr != NULL && strncmp(ptr, "PING", 4) == 0) {
        return sck_sendf(irc->s, "PONG :%s\r\n", ptr);
    }
    ptr = strtok(NULL, " ");
    if (ptr != NULL && strncmp(ptr, "433", 3) == 0) {
    	irc_prepare(irc);
        return 0;
    }
    ptr = strtok(NULL, ":");
    ptr = strtok(NULL, " ");
    if (ptr != NULL && *ptr == '!') {
    	ptr++; // skips '!'
    	char msg_cmd[strlen(ptr)];
    	strncpy(msg_cmd, ptr, strlen(ptr));
    	ptr = strtok(NULL, "\0");
    	return irc_parse_uaction(irc, msg_cmd, ptr, count_args(ptr));
    }
    return 0;
}
Ejemplo n.º 5
0
static int process_command(char *complete_cmd)
{
    const command_info *i;
    char *cmd_copy;
    char *args;
    int rc = 1;

    if (complete_cmd == NULL)  /* can happen if user hits CTRL-D, etc. */
    {
        printf("\n");
        return(0);
    } /* if */

    cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
    if (cmd_copy == NULL)
    {
        printf("\n\n\nOUT OF MEMORY!\n\n\n");
        return(0);
    } /* if */

    trim_command(complete_cmd, cmd_copy);
    args = strchr(cmd_copy, ' ');
    if (args != NULL)
    {
        *args = '\0';
        args++;
    } /* else */

    if (cmd_copy[0] != '\0')
    {
        for (i = commands; i->cmd != NULL; i++)
        {
            if (strcmp(i->cmd, cmd_copy) == 0)
            {
                if ((i->argcount >= 0) && (count_args(args) != i->argcount))
                    output_usage("usage:", i);
                else
                    rc = i->func(args);
                break;
            } /* if */
        } /* for */

        if (i->cmd == NULL)
            printf("Unknown command. Enter \"help\" for instructions.\n");

#if (defined PHYSFS_HAVE_READLINE)
        add_history(complete_cmd);
        if (history_file)
        {
            fprintf(history_file, "%s\n", complete_cmd);
            fflush(history_file);
        } /* if */
#endif

    } /* if */

    free(cmd_copy);
    return(rc);
} /* process_command */
Ejemplo n.º 6
0
void
op_listpush(void)
{
    Var		list;

    list = pop_args(count_args());
    push(list);
}
Ejemplo n.º 7
0
void
count_params (struct command *cmd)	/* count number of function parameters */
{
    struct symbol *sym;

    sym = get_sym (cmd->symname, syNUMBER, amADD_LOCAL);
    sym->value = abs (count_args (FALSE));
}
Ejemplo n.º 8
0
void	create_corline_wl(t_corline tmp, t_corline *corline, op_t op)
{
  op = checkOp(tmp.args[0]);
  tmp.instruction = op.mnemonique;
  tmp.nbr = count_args(tmp.args);
  tmp.tab_args = get_args(tmp.args + 1, -1);
  tmp.mempos = calc_mem_pos(&tmp, corline);
  tmp.label = NULL;
  create_line(corline, tmp);
}
Ejemplo n.º 9
0
t_uchar		run_setenv(char ***env, char **argv)
{
  char		*tmp;

  if (count_args(argv) < 2)
    {
      my_printenv(*env, '\n');
      return (0);
    }
  else if (count_args(argv) > 3)
    {
      my_dprintf(STDERR, "setenv: Too many arguments.\n");
      return (1);
    }
  if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL)
    my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n");
  tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1]));
  tmp = my_strncat(tmp, "=", 1);
  my_setenv(env, tmp, argv[2]);
  free(tmp);
  return (0);
}
Ejemplo n.º 10
0
int count_args(Node *p){

	int tmp = 0, i = 0;

	if(p->attrib == DECL_VAR_LIST)
		for(; i < p->SUB(0)->SUB(0)->value.sub.num; i++)
			tmp += 4;
	else
		for (i = 0; i < p->value.sub.num && p->SUB(i) != NULL; i++){
			tmp += count_args(p->SUB(i));
		}

	return tmp;
}
Ejemplo n.º 11
0
bool
conftab_bool::convert (const vec<str> &v, const str &l, bool *e)
{
  if (!count_args (v, 2))
    return false;

  if (v[1] == "1") 
    tmp = true;
  else if (v[1] == "0")
    tmp = false;
  else 
    err = true;

  return (!err);
}
Ejemplo n.º 12
0
int argify(const char *line, char ***argv_ptr)
{
  int argc;
  char ** argv;

  argc = count_args (line);
  if (argc == 0)
      return -1;
  argv = (char **)malloc (sizeof (char *) * argc);
  if (!argv) return -1;
  if (copy_args (line, argc, argv) < 0) return -1;
  *argv_ptr = argv;
  
  return argc;
}
Ejemplo n.º 13
0
void	create_corline_l(t_corline tmp, t_corline *corline,
			 t_corlabel *corlabel, op_t op)
{
  t_corlabel	label;

  op = checkOp(tmp.args[1]);
  tmp.instruction = op.mnemonique;
  tmp.nbr = count_args(tmp.args);
  tmp.tab_args = get_args(tmp.args + 2, -1);
  tmp.mempos = calc_mem_pos(&tmp, corline);
  tmp.label = line_is_label(tmp.args[0]);
  label.mempos = tmp.mempos;
  label.name = tmp.label;
  create_label(corlabel, label);
  create_line(corline, tmp);
}
Ejemplo n.º 14
0
int				get_args(char *src_str, char ***dest_args)
{
	int		nb_args;
	int		nb_allocated_args;

	nb_args = count_args(src_str);
	if (nb_args < 0)
		return (-1);
	if ((*dest_args = malloc(sizeof(char *) * nb_args + 1)) == NULL)
		return (-1);
	(*dest_args)[nb_args] = NULL;
	if ((nb_allocated_args = fill_args(*dest_args, src_str)) != nb_args)
	{
		free_args(dest_args, nb_allocated_args);
		return (-1);
	}
	return (nb_args);
}
Ejemplo n.º 15
0
int main(){
  char* commandline;
  char** args;
  int numargs;

  while(1){
    printf("TCKY$: ");
    
    commandline = read_line();
    numargs = count_args(commandline);
    args = separate_line(commandline);

    process(args, numargs);
  
  }
  
  return 1;
}
Ejemplo n.º 16
0
Archivo: irc.c Proyecto: Xe/code
IRCLine * parse_line(char * line) {
	char * parsed, * source, * extparam, * verb;
	int argc, i;

	IRCLine * result;

	if(line[strlen(line)-2] == '\r')
		line[strlen(line)-2] = '\0';

	extparam = parsed = calloc(sizeof(char), strlen(line));
	strncpy(parsed, line, strlen(line));

	result = malloc(sizeof(IRCLine));

	argc = count_args(line);
	char * argv[argc];

	if(line[0] == ':') {
		argc--;
		line++;
		parsed++;
		source = strtok(parsed, " ");
		verb = strtok(NULL, " ");
	} else {
		source = "";
		verb = strtok(parsed, " ");
	}

	for(i = 0; i < argc; i++) {
		argv[i] = NULL;
		argv[i] = strtok(NULL, " ");
	}

	if(*argv[argc-1] == ':') {
		argv[argc-1] = split_out(line, ':');
	}

	line_initialize(result, source, verb, argc, argv);

	free(extparam);

	return result;
}
Ejemplo n.º 17
0
bool
conftab_str::convert (const vec<str> &v, const str &l, bool *e)
{
  if (dest) {
    if (!count_args (v, 2))
      return false;
    else 
      tmp_s = v[1];
  }
  else if (scb) {
    tmp_s = v[1];
  }
  else {
    tmp = v;
  }
  loc = l;
  errp = e;
  return true;
}
Ejemplo n.º 18
0
void
op_message_expr(void)
{
    Var		args;
    Var		msg, dest;

    args = pop_args(count_args());
    msg = pop(); dest = pop();

    if (dest.type != OBJ) {
	var_free(args); var_free(dest); var_free(msg);
	raise(E_INVIND);
    } else if (msg.type != STR) {
	var_free(args); var_free(msg);
	raise(E_TYPE);
    } else {
	send_message_and_block(frame.this, dest.v.obj, msg.v.str,
			       args.v.list, dest.v.obj);
    }
}
Ejemplo n.º 19
0
void
op_message(void)
{
    Var		args;
    Var		dest;
    String	*msg;

    args = pop_args(count_args());
    dest = pop();
    if (dest.type != OBJ) {
	var_free(args); var_free(dest);
	frame.pc++;
	raise(E_INVIND);
    } else {
	msg = string_dup(sym_get(frame.on, frame.m->code[frame.pc]));
	frame.pc++;
        send_message_and_block(frame.this, dest.v.obj, msg,
			       args.v.list, dest.v.obj);
    }
}
Ejemplo n.º 20
0
int			get_arguments(options_t *options, int ac, char **av) {
	char	**rest;
	u_int	cnt;

	options->port = STARCRAFT_PORT;
	
	rest = read_options(options, ac, av, sov_arguments);
	if (!rest)
		return 0;
	
	if (!options->pcap_filter) {
		options->pcap_filter = malloc(SNAP_LEN);
		snprintf(options->pcap_filter, SNAP_LEN, PCAP_FILTER_FORMAT, options->port);
	}
	cnt = count_args(rest);
	if (cnt < 2) {
		fprintf(stderr, "Error: not enough arguments." EOL);
		print_use();
		return 0;
	}
	options->capture_device_name = *rest++;
	options->hosts = rest;
	options->host_count = cnt - 1;

	if (options->inject_device_name)
	{
		if (options->bind_udp_port) {
			fprintf(stderr, "Invalid arguments: cannot bind UDP and use injection" EOL);
			return 0;
		}
#ifdef WIN32
		if (!options->inject_ethernet_source || !options->inject_ethernet_destination) {
			fprintf(stderr, "Invalid arguments: source and destination MAC addresses" EOL
				"are required when using injection under windows." EOL);
			return 0;
		}
#endif
	}

	return 1;
}
Ejemplo n.º 21
0
static void	print_strtab(long reg, int pid)
{
  char		c;
  long		addr;
  int		nb, k, j = 0;

  if ((nb = count_args(reg, pid)) > 3)
    {    fprintf(stderr, "[/* %d vars */]", nb);      return ;    }
  fprintf(stderr, "[");
  while ((addr = ptrace(PTRACE_PEEKTEXT, pid, reg + j, NULL)))
    {
      fprintf(stderr, "\"");
      k = 0;
      while ((c = ptrace(PTRACE_PEEKTEXT, pid, addr + k++, NULL)) && k < 60)
  	fprintf(stderr, (c == '\n') ? "\\n" : "%c", c);
      fprintf(stderr, "\"");
      if (k == 60)
	fprintf(stderr, "...");
      j += sizeof(long);
      if (ptrace(PTRACE_PEEKTEXT, pid, reg + j, NULL))
	fprintf(stderr, ", ");
    }
  fprintf(stderr, "]");
}
Ejemplo n.º 22
0
Archivo: main.c Proyecto: Nukem9/Dune
static uintptr_t
setup_arguments(uintptr_t sp, const char *path,
		char *argv[], char*envv[],
		struct elf_data data)
{
	char *data_ptr;
	uintptr_t *arg_ptr;
	Elf64_auxv_t *aux_ptr;
	size_t tmp, len = 0;
	int argc, envc, i;

	// determine data pointer
	count_args(argv, &argc, &tmp);
	len += tmp;
	count_args(envv, &envc, &tmp);
	len += tmp;
	len += strlen(path) + 1;
	data_ptr = (char *) sp - len;

	// determine aux pointer
	aux_ptr = (Elf64_auxv_t *) &envv[envc + 1];
	len += sizeof(Elf64_auxv_t) * NUM_AUX;

	// determine argument pointer
	len += (argc + envc + 4) * sizeof(uintptr_t);

	// The System V AMD64 ABI requires 16-byte stack alignment. We go
	// with 32-bytes to be extra conservative (e.g. in case of AVX)
	len = (len + 0x1f) & ~0x1f;

	arg_ptr = (uintptr_t *) (sp - len);
	*arg_ptr = argc + 1;
	arg_ptr++;

	// setup application name argument
	*arg_ptr = (uintptr_t) data_ptr;
	arg_ptr++;
	data_ptr = stpcpy(data_ptr, path) + 1;

	// setup remaining arguments
	for (i = 0; i < argc; i++) {
		*arg_ptr = (uintptr_t) data_ptr;
		arg_ptr++;
		data_ptr = stpcpy(data_ptr, argv[i]) + 1;
	}
	*arg_ptr = (uintptr_t) NULL; // arg end
	arg_ptr++;

	// setup environment
	for (i = 0; i < envc; i++) {
		*arg_ptr = (uintptr_t) data_ptr;
		arg_ptr++;
		data_ptr = stpcpy(data_ptr, envv[i]) + 1;
	}
	*arg_ptr = (uintptr_t) NULL; // env end
	arg_ptr++;

	// setup aux entries
	setup_aux(aux_ptr, (Elf64_auxv_t *) arg_ptr, data);

	return sp - len;
}
Ejemplo n.º 23
0
prolog(FILE *outfile, register chainp p)
#endif
{
	int addif, addif0, i, nd;
	ftnint size;
	int *ac;
	register Namep q;
	register struct Dimblock *dp;
	chainp p0, p1;

	if(procclass == CLBLOCK)
		return;
	p0 = p;
	p1 = p = argsort(p);
	wrote_comment = 0;
	comment_file = outfile;
	ac = 0;

/* Compute the base addresses and offsets for the array parameters, and
   assign these values to local variables */

	addif = addif0 = nentry > 1;
	for(; p ; p = p->nextp)
	{
	    q = (Namep) p->datap;
	    if(dp = q->vdim)	/* if this param is an array ... */
	    {
		expptr Q, expr;

		/* See whether to protect the following with an if. */
		/* This only happens when there are multiple entries. */

		nd = dp->ndim - 1;
		if (addif0) {
			if (!ac)
				ac = count_args();
			if (ac[q->argno] == nentry)
				addif = 0;
			else if (dp->basexpr
				    || dp->baseoffset->constblock.Const.ci)
				addif = 1;
			else for(addif = i = 0; i <= nd; i++)
				if (dp->dims[i].dimexpr
				&& (i < nd || !q->vlastdim)) {
					addif = 1;
					break;
					}
			if (addif) {
				write_comment();
				nice_printf(outfile, "if (%s) {\n", /*}*/
						q->cvarname);
				next_tab(outfile);
				}
			}
		for(i = 0 ; i <= nd; ++i)

/* Store the variable length of each dimension (which is fixed upon
   runtime procedure entry) into a local variable */

		    if ((Q = dp->dims[i].dimexpr)
			&& (i < nd || !q->vlastdim)) {
			expr = (expptr)cpexpr(Q);
			write_comment();
			out_and_free_statement (outfile, mkexpr (OPASSIGN,
				fixtype(cpexpr(dp->dims[i].dimsize)), expr));
		    } /* if dp -> dims[i].dimexpr */

/* size   will equal the size of a single element, or -1 if the type is
   variable length character type */

		size = typesize[ q->vtype ];
		if(q->vtype == TYCHAR)
		    if( ISICON(q->vleng) )
			size *= q->vleng->constblock.Const.ci;
		    else
			size = -1;

		/* Fudge the argument pointers for arrays so subscripts
		 * are 0-based. Not done if array bounds are being checked.
		 */
		if(dp->basexpr) {

/* Compute the base offset for this procedure */

		    write_comment();
		    out_and_free_statement (outfile, mkexpr (OPASSIGN,
			    cpexpr(fixtype(dp->baseoffset)),
			    cpexpr(fixtype(dp->basexpr))));
		} /* if dp -> basexpr */

		if(! checksubs) {
		    if(dp->basexpr) {
			expptr tp;

/* If the base of this array has a variable adjustment ... */

			tp = (expptr) cpexpr (dp -> baseoffset);
			if(size < 0 || q -> vtype == TYCHAR)
			    tp = mkexpr (OPSTAR, tp, cpexpr (q -> vleng));

			write_comment();
			tp = mkexpr (OPMINUSEQ,
				mkconv (TYADDR, (expptr)p->datap),
				mkconv(TYINT, fixtype
				(fixtype (tp))));
/* Avoid type clash by removing the type conversion */
			tp = prune_left_conv (tp);
			out_and_free_statement (outfile, tp);
		    } else if(dp->baseoffset->constblock.Const.ci != 0) {

/* if the base of this array has a nonzero constant adjustment ... */

			expptr tp;

			write_comment();
			if(size > 0 && q -> vtype != TYCHAR) {
			    tp = prune_left_conv (mkexpr (OPMINUSEQ,
				    mkconv (TYADDR, (expptr)p->datap),
				    mkconv (TYINT, fixtype
				    (cpexpr (dp->baseoffset)))));
			    out_and_free_statement (outfile, tp);
			} else {
			    tp = prune_left_conv (mkexpr (OPMINUSEQ,
				    mkconv (TYADDR, (expptr)p->datap),
				    mkconv (TYINT, fixtype
				    (mkexpr (OPSTAR, cpexpr (dp -> baseoffset),
				    cpexpr (q -> vleng))))));
			    out_and_free_statement (outfile, tp);
			} /* else */
		    } /* if dp -> baseoffset -> const */
		} /* if !checksubs */

		if (addif) {
			nice_printf(outfile, /*{*/ "}\n");
			prev_tab(outfile);
			}
	    }
	}
	if (wrote_comment)
	    nice_printf (outfile, "\n/* Function Body */\n");
	if (ac)
		free((char *)ac);
	if (p0 != p1)
		frchain(&p1);
} /* prolog */
Ejemplo n.º 24
0
int main(int argc, char *argv[]) {
	
	//Checks that only ./whoosh is entered to invoke shell
	if(argc != 1) {
		error_msg();
		exit(1);
	}
	
	int CHAR_LIMIT = 129;
	int BUFFER_LIMIT = 1024;
	char buffer[BUFFER_LIMIT];
	
	int path_num = 1;
	char* s_path[BUFFER_LIMIT];
	s_path[0] = "/bin";
	
	while(1) {
		printf("whoosh> ");
		fflush(stdout);		
		fgets(buffer, sizeof(buffer), stdin);
		
		get_line(buffer);
		
		int line_size = strlen(buffer);
		
		if(line_size > CHAR_LIMIT) {
			error_msg();
			continue;
		}
		
		//Get num of args in command 
		int num_args = count_args(buffer);
		
		if(num_args != 0) {
			
			char* myargv[num_args + 1];
			int r_count = 0;
			int last_r = 0;
			char* r_path = NULL;
			int i;
			char buf[PATH_MAX + 1];
			char *cwd;
			char *r_output;
			
			//Put each argument in myargv array
			get_args(buffer, myargv, num_args);
			
			//Check for redirect
			check_redirect(myargv, num_args, &r_count, &last_r);
		
			//Redirect error handling
			if(r_count > 1) {
				error_msg();
				continue;
			}
			else if (r_count == 1) {
				if(last_r != num_args - 2) {
					error_msg();
					continue;
				}
				if(myargv[num_args - 1][0] == '/') {
					if(chdir(myargv[num_args - 1]) == 0) {
						r_path = strdup(myargv[num_args - 1]);
					}
					else {
						error_msg();
						continue;
					}
				}
				
				r_output = malloc(strlen(myargv[num_args - 1]) + strlen(".out"));
				sprintf(r_output, "%s", myargv[num_args - 1]);	
				//Take off > and path from arguments
				num_args = num_args - 2;
			}
			
			//Set last element in myargv array to NULL
			myargv[num_args] = NULL;
	
			//Check if exit was entered
			check_exit(myargv);
			
			//Check if any built-in commands were called
			if(check_built_in(myargv, num_args, s_path, &path_num) == 0) {
				continue;
			}
			else{
				int file_exist = 0;
				
				cwd = getcwd(buf, PATH_MAX + 1);
				for(i=0; i<path_num; i++) {
					chdir(s_path[i]);
					struct stat path_buff;
					if(stat(myargv[0], &path_buff) == 0) {
						file_exist = 1;
						char *exec_path = malloc(strlen(s_path[i]) + strlen("/") + strlen(myargv[0]) + 1);
						sprintf(exec_path, "%s/%s", s_path[i], myargv[i]);
						myargv[0] = exec_path;
						chdir(cwd);
						break;
					}
				}	
				if(file_exist == 0) {
					error_msg();
					continue;
				}
				else {
					exec_cmd(myargv, r_output, r_path, r_count);
				}
			}
		}	
	}
}
Ejemplo n.º 25
0
void* setup_main_stack(const char* command_line, void* stack_top)
{
  /* Variable "esp" stores an address, and at the memory loaction
   * pointed out by that address a "struct main_args" is found.
   * That is: "esp" is a pointer to "struct main_args" */
  struct main_args* esp;
  int argc;
  int total_size;
  int line_size;
  /* "cmd_line_on_stack" and "ptr_save" are variables that each store
   * one address, and at that address (the first) char (of a possible
   * sequence) can be found. */
  char* cmd_line_on_stack;
  char* ptr_save;
  int i = 0;
  
  /* calculate the bytes needed to store the command_line */
  line_size = strlen(command_line) + 1;
  STACK_DEBUG("# line_size = %d\n", line_size);

  /* round up to make it even divisible by 4 */

  line_size = line_size + (4 - line_size%4);
  STACK_DEBUG("# line_size (aligned) = %d\n", line_size);

  /* calculate how many words the command_line contain */
  argc = count_args(command_line," ") ;
  STACK_DEBUG("# argc = %d\n", argc);

  /* calculate the size needed on our simulated stack */
  total_size = line_size + (argc * 4) + (4 * 4) ;
  STACK_DEBUG("# total_size = %d\n", total_size);
  

  /* calculate where the final stack top will be located */
  esp = stack_top - total_size;
  
  /* setup return address and argument count */
  esp->ret = NULL ;
  esp->argc = argc ;

  /* calculate where in the memory the argv array starts */
  esp->argv = (char**)((unsigned)esp + 3*4);
  
  /* calculate where in the memory the words is stored */
  cmd_line_on_stack = (char*)((unsigned)esp + 4*4 + argc*4) ;

  /* copy the command_line to where it should be in the stack */
  bool new_word_flag = false;
  int argv_c = 0;

  for(i; i < line_size && argv_c <= argc; ++i)
  {

    if(new_word_flag || (i == 0 && command_line[i] != ' ')){
      esp->argv[argv_c] = (char*) cmd_line_on_stack + i;
      ++argv_c;
    }

    if(command_line[i] != ' '){
      cmd_line_on_stack[i] = command_line[i];
      new_word_flag = false;
    }
    else{
      cmd_line_on_stack[i] = '\0';
      new_word_flag = true;
     }
    
  } 
  
  /* build argv array and insert null-characters after each word */
  
  return esp; /* the new stack top */
}
Ejemplo n.º 26
0
static void eval(Node *p){

	int i, lbl1, lbl2, size, temp = 0, attrib = 1;
	char *name;

	if (p == 0) return;
	switch(p->attrib){

		case CONTINUE:
		/* JMP cond*/
			break;		
		case BREAK:
		/* JMP fim */
			break;		
		case ELIF:
		
			break;

		case FOR:
			eval(p->SUB(0));
			fprintf(out, pfLABEL, mklbl(lbl1 = ++lbl));
			eval(p->SUB(1));
			fprintf(out, pfJZ, mklbl(lbl2 = ++lbl));
			IDpush();
			eval(p->SUB(3));
			eval(p->SUB(2));
			IDpop();
			fprintf(out, pfJMP, mklbl(lbl1));
			fprintf(out, pfLABEL, mklbl(lbl2));
			break;

		case ALLOCA:
			eval(p->SUB(1));
			fprintf(out, pfALLOC);
			fprintf(out, pfSP);
			eval(p->SUB(0));
			fprintf(out, pfSTORE);
			break;

		case EXTERN:						/* extern declarations        */
			for (i = 0; i < p->value.sub.num; i++){
				name = p->SUB(i)->SUB(0)->value.s;
				IDnew(EXTERN, name, p->SUB(i)->value.sub.num);
				fprintf(out, pfEXTRN, name);
			}
			break;
		case INIT:
			name = p->SUB(0)->SUB(0)->value.s;
			IDnew(INIT, name, p->SUB(1)->value.sub.num);
			fprintf(out, pfDATA);
			fprintf(out, pfALIGN);				/* make sure we are aligned   */
			
			fprintf(out, pfGLOBL, name, pfOBJ);
			fprintf(out, pfLABEL, name);
			for(i = 0; i < p->SUB(1)->value.sub.num; i++){
				fprintf(out, pfCONST, p->SUB(1)->SUB(i)->value.i);
			}
			fprintf(out, pfTEXT);
			fprintf(out, pfALIGN);				/* make sure we are aligned   */
			break;
		case CONST:
			fprintf(out, pfRODATA);
			fprintf(out, pfALIGN);

			name = p->SUB(0)->SUB(0)->value.s;
			IDnew(CONST, name, p->SUB(1)->value.sub.num);				
			fprintf(out, pfLABEL, name);			/* name variable location     */
			for (temp = 0; temp < p->SUB(1)->value.sub.num; temp++)
				fprintf(out, pfCONST, p->SUB(1)->SUB(temp)->value.i);
			fprintf(out, pfTEXT);
			fprintf(out, pfALIGN);
			break;
			
		case LVALUE2:
			for (i = 0; i < p->value.sub.num; i++)
				eval(p->SUB(i));
			break;
		case LVALUE:
  			name = p->SUB(0)->SUB(0)->value.s;

			if(p->SUB(0)->SUB(0)->attrib == IDLVALUE)
				IDnew(INT_NUMBER, name, offset);
			else if (p->SUB(0)->SUB(0)->attrib == IDARRAY)
				IDnew(ARRAY, name, offset);
			else{
				name = p->SUB(0)->SUB(0)->SUB(0)->value.s;
				offset -= 4 * p->SUB(0)->SUB(1)->value.i;
				IDnew(ARRAYINDEX, name, offset);
				break;
			}
			offset -= 4;
			break;
		case VAR:
			for (i = 0; i < p->value.sub.num; i++){
				eval(p->SUB(i));
			}
			break;

		case ARRAYINDEX:
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfINT, 4);				/* push an integer	      */
			fprintf(out, pfMUL);
			eval(p->SUB(0));
			fprintf(out, pfADD);

			break;
		case ARRAY:
			eval(p->SUB(0));	
			break;

		case FNNAME:
			name = p->SUB(0)->SUB(0)->value.s;
			if(p->value.sub.num == 2)
				temp = p->SUB(1)->value.sub.num;
			IDnew(FNNAME, name, temp);
			IDpush();
			IDnew(INT_NUMBER, name, -4);
			
			offset = -8;
			fprintf(out, pfTEXT);
			fprintf(out, pfALIGN);
			fprintf(out, pfGLOBL, name, pfFUNC);
			fprintf(out, pfLABEL, name);
			fprintf(out, pfENTER, args + 4);
			break;
		case FUNCTION:
			args = count_args(p);
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			fprintf(out, pfLOCAL, -4);
			fprintf(out, pfLOAD);
			fprintf(out, pfPOP);
			fprintf(out, pfLEAVE);
			fprintf(out, pfRET);
			IDpop();
			break;

		case INT_NUMBER:
			fprintf(out, pfINT, p->value.i);		/* push an integer	      */
			break;
		case ID:						/* */
			name = p->SUB(0)->value.s;
			switch(p->SUB(0)->attrib){
				case IDLVALUE:
				case IDARRAY:
				case IDARRAYINDEX:
					temp = IDfind(name, &attrib);
					fprintf(out, pfLOCAL, attrib);
					break;
				default:
					fprintf(out, pfADDR, name);
					break;
			}
			break;	
		case DO:
			fprintf(out, pfLABEL, mklbl(lbl1 = ++lbl));
			IDpush();
			eval(p->SUB(0));
			IDpop();
			fprintf(out, pfLABEL, mklbl(lbl2 = ++lbl));
			eval(p->SUB(1));
			fprintf(out, pfJNZ, mklbl(lbl1));
			if (p->value.sub.num > 2){			/* do else 			*/
				IDpush();
				eval(p->SUB(2));
				IDpop();
			}
			break;		
		case WHILE:
			fprintf(out, pfLABEL, mklbl(lbl1 = ++lbl));
			eval(p->SUB(0));
			fprintf(out, pfJZ, mklbl(lbl2 = ++lbl));
			IDpush();
			eval(p->SUB(1));
			IDpop();
			fprintf(out, pfJMP, mklbl(lbl1));
			fprintf(out, pfLABEL, mklbl(lbl2));
			if (p->value.sub.num > 2){			/* while else 			*/
				IDpush();
				eval(p->SUB(2));
				IDpop();
			}
			break;
		case CALL:
			name = p->SUB(0)->value.s;
			if(p->value.sub.num == 2) {	
				for(i = p->SUB(1)->value.sub.num; i > 0; i--){
					eval(p->SUB(i));		/* evaluate argument	      */
					if (p->SUB(i)->attrib != INT_NUMBER)
						fprintf(out, pfLOAD);
				}
			}
			fprintf(out, pfCALL, name);
			fprintf(out, pfTRASH, i);			/* remove the return value    */
			fprintf(out, pfPUSH);
			break;	

		case STR:						/* generate the string	      */			 
			fprintf(out, pfRODATA);				/* strings are DATA readonly  */
			fprintf(out, pfALIGN);				/* make sure we are aligned   */
			fprintf(out, pfLABEL, mklbl(lbl1 = ++lbl));	/* give the string a name     */
			fprintf(out, pfSTR, p->value.s); 		/* output string characters   */
				/* make the call */
			fprintf(out, pfTEXT);				/* return to the TEXT segment */
			fprintf(out, pfALIGN);				/* make sure we are aligned   */
			fprintf(out, pfADDR, mklbl(lbl1));
			break;

		case PRINTSTUFF:
			eval(p->SUB(0));
			if(p->SUB(0)->attrib == ID){
				fprintf(out, pfLOAD);
				fprintf(out, pfCALL, "printi");		/* call the print function    */
			}
			else if(p->SUB(0)->attrib == STR){
				fprintf(out, pfCALL, "prints");		/* call the print function    */
			}
			else if(p->SUB(0)->attrib == INT_NUMBER){
				fprintf(out, pfCALL, "printi");		/* call the print function    */
			}
			print = 0;
			break;
		case '!':
			print = 1;
			eval(p->SUB(0));				/* determine the value        */
			fprintf(out, pfTRASH, 4);			/* delete the printed value   */
			break;		
		case OUT:
			print = 1;
			eval(p->SUB(0));				/* determine the value        */
			fprintf(out, pfCALL, "println");		/* print a newline	      */
			fprintf(out, pfTRASH, 4);			/* delete the printed value   */
			break;
		case IF:
			eval(p->SUB(0));
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfJZ, mklbl(lbl1 = ++lbl));
			IDpush();
			eval(p->SUB(1));
			IDpop();
			if (p->value.sub.num > 2){			/* if else 			*/
				fprintf(out, pfJMP, mklbl(lbl2 = ++lbl));
				fprintf(out, pfLABEL, mklbl(lbl1));
				IDpush();
				eval(p->SUB(2));
				IDpop();
				lbl1 = lbl2;
			}
			fprintf(out, pfLABEL, mklbl(lbl1));
			break;

		case BONUS:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;	
		case COMPOUND_STAT:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case CONST_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;	
		case DECL_VAR_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;	
		case EXPRESSION_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case EXTRA_STAT_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case NUM_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;	
		case PROGRAM:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case STATEMENT:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case STATEMENT_LIST:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;
		case ULTRA:
			for (i = 0; i < p->value.sub.num; i++)
	  			eval(p->SUB(i));
			break;		

		case AND_OP:
			eval(p->SUB(0));				/* evaluate first argument    */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfDUP);
			fprintf(out, pfJZ, mklbl(lbl1 = ++lbl));
			fprintf(out, pfTRASH, 4);
			eval(p->SUB(1));				/* evaluate second argument   */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfLABEL, mklbl(lbl1));
			break;
		case OR_OP:
			eval(p->SUB(0));				/* evaluate first argument    */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfDUP);
			fprintf(out, pfJNZ, mklbl(lbl1 = ++lbl));
			fprintf(out, pfTRASH, 4);
			eval(p->SUB(1));				/* evaluate second argument   */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfLABEL, mklbl(lbl1));
			break;
		case NOT:
			eval(p->SUB(0));				/* evaluate argument	      */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfINT, 0);
			fprintf(out, pfEQ);
			break;
		case '?':
			fprintf(out, pfCALL, "readi");
			fprintf(out, pfPUSH);
			eval(p->SUB(0));
			fprintf(out, pfSTORE);
			break;
		case '=':
			eval(p->SUB(1));				/* determine the new value    */
			if(p->SUB(1)->attrib == '='){
				eval(p->SUB(1)->SUB(0));
				fprintf(out, pfLOAD);
			}
			eval(p->SUB(0));	 			/* determine the new address  */
			fprintf(out, pfSTORE);				/* store the value at address */
			break;
		case '@':
			eval(p->SUB(0));				/* evaluate argument	      */
			break;
		case SIMETRIC:
			eval(p->SUB(0));				/* evaluate argument	      */
			if (p->SUB(0)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfNEG);				/* make the 2-compliment      */
			break;
		case INC:
			eval(p->SUB(0));				/* evaluate argument	      */
			fprintf(out, pfINCR, 1);
			break;		
		case DEC:
			eval(p->SUB(0));				/* evaluate argument	      */
			fprintf(out, pfDECR, 1);
			break;
		case ADDEDATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfADD);

			eval(p->SUB(0));				/* determine the new address  */
			fprintf(out, pfSTORE);				/* store the value at address */			
			break;

/*		case MINUSATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfSUB);

			eval(p->SUB(0));	 			
			fprintf(out, pfSTORE);							
			break;

		case MULATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfMUL);

			eval(p->SUB(0));	 			
			fprintf(out, pfSTORE);							
			break;

		case DIVATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfDIV);
			fprintf(out, pfADD);

			eval(p->SUB(0));	 			
			fprintf(out, pfSTORE);							
			break;

		case MODATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			fprintf(out, pfMOD);

			eval(p->SUB(0));	 			
			fprintf(out, pfSTORE);							
			break;

		case EXPATTRIB:
			eval(p->SUB(0));
			fprintf(out, pfLOAD);
			eval(p->SUB(1));
			if (p->SUB(1)->attrib != INT_NUMBER)
				fprintf(out, pfLOAD);
			pow_flag = 1;
			fprintf(out, pfCALL, "power");
			fprintf(out, pfTRASH, 8);
			fprintf(out, pfPUSH);

			eval(p->SUB(0));	 			
			fprintf(out, pfSTORE);							
			break;
*/
		default:	
			if(print){
				if(p->attrib == '+'){
					for (i = 0; i < p->value.sub.num; i++){
	  					eval(p->SUB(i));

						if(p->SUB(i)->attrib == ID){
							fprintf(out, pfLOAD);
							fprintf(out, pfCALL, "printi");		/* call the print function    */
						}
						else if(p->SUB(i)->attrib == INT_NUMBER){
							fprintf(out, pfCALL, "printi");		/* call the print function    */
						}
	  					else if (p->SUB(i)->attrib == STR){
							fprintf(out, pfCALL, "prints");			/* call the print function    */
						}
						else if(p->SUB(i)->attrib == '*'){
							eval(p->SUB(i)->SUB(1));				/* determine the value        */
							if (p->SUB(i)->SUB(1)->attrib != INT_NUMBER)
								fprintf(out, pfLOAD);

							eval(p->SUB(i)->SUB(0));				/* determine the value        */

							printer_flag = 1;
							fprintf(out, pfCALL, "printer");		/* print a newline	      */
							fprintf(out, pfTRASH, 4);			/* remove the return value    */	
						}
					}
				}
				else{
					eval(p->SUB(1));				/* determine the value        */
					if (p->SUB(1)->attrib != INT_NUMBER)
						fprintf(out, pfLOAD);

					eval(p->SUB(0));				/* determine the value        */

					printer_flag = 1;
					fprintf(out, pfCALL, "printer");		/* print a newline	      */
					fprintf(out, pfTRASH, 4);			/* remove the return value    */	
				}
			}
			else{

				eval(p->SUB(1));				/* evaluate first argument    */
				if (p->SUB(1)->attrib != INT_NUMBER)
					fprintf(out, pfLOAD);

				switch(p->attrib){	 			/* make the operation ...     */
					case '+':
						eval(p->SUB(0));				/* determine the new value    */
						if (verif(p->SUB(0)))
							fprintf(out, pfLOAD);				
			
						fprintf(out, pfADD);
						break;
					case '-':	
						eval(p->SUB(0));				/* determine the new value    */
						if (p->SUB(0)->attrib != INT_NUMBER && p->SUB(0)->attrib != '-')
							fprintf(out, pfLOAD);	

						fprintf(out, pfSUB); 
						break;
					case '*':
						eval(p->SUB(0));				/* determine the new value    */
						if (p->SUB(0)->attrib != INT_NUMBER && p->SUB(0)->attrib != '*')
							fprintf(out, pfLOAD);		

						fprintf(out, pfMUL);
						break;
					case '/':
						eval(p->SUB(0));				/* determine the new value    */
						if (p->SUB(0)->attrib != INT_NUMBER && p->SUB(0)->attrib != '/')
							fprintf(out, pfLOAD);	

						fprintf(out, pfDIV);
						break;
					case '%':
						eval(p->SUB(0));				/* determine the new value    */
						if (p->SUB(0)->attrib != INT_NUMBER && p->SUB(0)->attrib != '%')
							fprintf(out, pfLOAD);	

						fprintf(out, pfMOD);
						break;
					case EXP:
						eval(p->SUB(0));				/* determine the new value    */
						if (p->SUB(0)->attrib != INT_NUMBER && p->SUB(0)->attrib != EXP)
							fprintf(out, pfLOAD);	

						pow_flag = 1;
						fprintf(out, pfCALL, "power");
						fprintf(out, pfTRASH, 8);
						fprintf(out, pfPUSH);
						break;
					case '<':
						fprintf(out, pfLT); break;
					case '>':
						fprintf(out, pfGT); break;
					case GE_OP:
						fprintf(out, pfGE); break;
					case LE_OP:
						fprintf(out, pfLE); break;
					case NE_OP:
						fprintf(out, pfNE); break;
					case EQ_OP:
						fprintf(out, pfEQ); break;
					case IMPLICA:
						eval(p->SUB(0));
						fprintf(out, pfNOT);
						eval(p->SUB(1));
						fprintf(out, pfOR);
						break;

					default:
						printf("WTF? Unknown %d ('%c') !\n", p->attrib, p->attrib);
				}
			}
	}
}
Ejemplo n.º 27
0
char *validate_instruction(instruction *instr) {
	int argc = symbol_value(argcounts, instr->instruction);
	int cnt = count_args(instr);

	int idx;

	char *err_msg;

	argument *arg;

	if (Is_Meta) {
		argc = cnt;

		err_msg = Meta->validate(instr);
		if (err_msg)
			return err_msg;
	}

	if (cnt != argc) {
		sprintf(
			err_buf,
			"Wrong number of arguments for \"%s\".  Expected %i, parsed %i",
			instr->instruction,
			argc,
			cnt
		);
		return err_buf;
	}

	for (idx = 0, arg = instr->args; idx < cnt; idx++, arg = arg->next) {
		if ((Mode == mode_string) && ! String_Allowed) {
			sprintf(
				err_buf,
				"String arguments are not allowed for \"%s\"",
				instr->instruction
			);
			return err_buf;
		}

		if (Data_Required && ! mode_is_data(Mode)) {
			sprintf(
				err_buf,
				"\"%s\" accepts constant data arguments only",
				instr->instruction
			);
			return err_buf;
		}

		err_msg = validate_argument(arg);
		if (err_msg) {
			sprintf(
				err_buf,
				"%s arg: %s",
				Ordinal(idx),
				err_msg
			);
			return err_buf;
		}

	}

	return NULL;
}