Example #1
0
File: cc.c Project: Fedjmike/mini-c
void object () {
    factor();

    while (true) {
        if (try_match("(")) {
            fputs("push eax\n", output);

            int arg_no = 0;

            if (waiting_for(")")) {
                //cdecl requires arguments to be pushed on backwards
                
                int start_label = new_label();
                int end_label = new_label();
                int prev_label = end_label;

                fprintf(output, "jmp _%08d\n", start_label);

                do {
                    int next_label = emit_label(new_label());
                    expr(0);
                    fprintf(output, "push eax\n"
                                    "jmp _%08d\n", prev_label);
                    arg_no++;

                    prev_label = next_label;
                } while (try_match(","));

                fprintf(output, "_%08d:\n", start_label);
                fprintf(output, "jmp _%08d\n", prev_label);
                fprintf(output, "_%08d:\n", end_label);
            }

            match(")");

            fprintf(output, "call dword ptr [esp+%d]\n", arg_no*word_size);
            fprintf(output, "add esp, %d\n", (arg_no+1)*word_size);

        } else if (try_match("[")) {
            fputs("push eax\n", output);

            expr(0);
            match("]");

            if (see("=") || see("++") || see("--"))
                lvalue = true;

            fprintf(output, "pop ebx\n"
                            "%s eax, [eax*%d+ebx]\n", lvalue ? "lea" : "mov", word_size);

        } else
            return;
    }
}
Example #2
0
int send_file(int work_sockfd, int sockfd, char* fileName) {
	char buffer[8192];
	FILE *fp;
	int len;
	
	bzero(buffer, 8192);
	if ((fp = fopen(fileName, "rb")) == NULL) {
		printf("fopen() error!\n");
		close(sockfd);
		return 1;
	}
	
	char *tmp_str = waiting_for(work_sockfd, "");
	char num[16];
	strcpy(num, split(tmp_str, " ", 2)[0]);
	if (strcmp(num, "150") != 0) {
		fclose(fp);
		close(sockfd);
		return 1;
	}
	sprintf(tmp_str, "(%d bytes).\r\n", get_file_size(fileName));
	printf("%s", tmp_str);
	
	len = 0;
	while((len = fread(buffer, 1, 8192, fp)) > 0) {
		int send_len;
		if ((send_len = send(sockfd, buffer, len, 0)) < 0) {
			printf("Error send(): %s(%d)\n", strerror(errno), errno);
		}
		bzero(buffer, 8192);
	}
	
	fclose(fp);
	close(sockfd);
	
	waiting_for(work_sockfd, "");
	
	return STATUS_OK;
}
Example #3
0
int main(int argc, char **argv) {
	int sockfd;
	struct sockaddr_in addr;
	char sentence[8192];
	int len;
	char ip[1024];
	int port[2];
	char** tmp_str;
	char command[8192], arguments[8192];
	int port_pasv = -1;
	int port_listenfd;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
		printf("Error socket(): %s(%d)\n", strerror(errno), errno);
		return 1;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = 21;
	if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) <= 0) {
		printf("Error inet_pton(): %s(%d)\n", strerror(errno), errno);
		return 1;
	}

	if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
		printf("Error connect(): %s(%d)\n", strerror(errno), errno);
		return 1;
	}
	
	waiting_for(sockfd, "220 FTP server ready.\r\n");
	
	while (1) {
		char tmp_sentence[8192];
	
		fgets(sentence, 4096, stdin);
		len = strlen(sentence);
		
		if (sentence[0] == '\n') {
			continue;
		}
		
		sentence[len - 1] = '\0';
		strcpy(tmp_sentence, sentence);
		tmp_str = split(tmp_sentence, " ", 2);
		strcpy(command, tmp_str[0]);
		strcpy(arguments, tmp_str[1]);
		
		if (strcmp(command, "STOR") == 0) {
			char filePath[] = "./";
			strcat(filePath, arguments);
			FILE *tmp_fp = fopen(filePath, "rb");
			if (tmp_fp == NULL) {
				printf("No such file!\n");
				continue;
			}
		} else if (strcmp(command, "PORT") == 0) {
			char tmp_str[128];
			sprintf(tmp_str, "(%s)", arguments);
			extract_ip_port(tmp_str, ip, port);
			if (port_open_port(sockfd, port[0] * 256 + port[1], &port_listenfd) == 1) {
				continue;
			}
		}
			
		if (send(sockfd, sentence, len, 0) == -1) {
			printf("Error send()\n");
		}
			
		if (strcmp(command, "RETR") == 0
			|| strcmp(command, "STOR") == 0) {
			int retr_stor;
			char filePath[256] = "./";
			strcat(filePath, arguments);
			
			if (strcmp(command, "RETR") == 0) {
				retr_stor = 0;
			} else {
				retr_stor = 1;
			}
			
			if (port_pasv == 0) {
				port_handler(sockfd, port_listenfd, port[0] * 256 + port[1], filePath, retr_stor);
			} else if (port_pasv == 1) {
				pasv_handler(sockfd, ip, port[0] * 256 + port[1], filePath, retr_stor);
			} else {
				waiting_for(sockfd, "");
			}
			continue;
		} else if (strcmp(command, "PASV") == 0) {
			port_pasv = 1;
			char *tmp = waiting_for(sockfd, "");
			extract_ip_port(tmp, ip, port);
			continue;
		} else if (strcmp(command, "PORT") == 0) {
			port_pasv = 0;
			waiting_for(sockfd, "");
			continue;
		}
		
		if ((len = recv(sockfd, sentence, 8191, 0)) == -1) {
			printf("Error recv()\n");
		}
		
		if (len > 0) {
			sentence[len] = '\0';
			printf("%s", sentence);
		}
		
		if (sentence[0] == '2' && sentence[1] == '2' && sentence[2] == '1') {
			break;
		}
	}

	close(sockfd);

	return 0;
}
Example #4
0
/**
 * Atom =   ( "(" [ Expr [{ "," Expr }] ] ")" )
 *        | ( "[" [{ Expr }] "]" )
 *        | FnLit | Path | <Str> | <Symbol>
 */
static ast* parseAtom (parserCtx* ctx) {
    ast* node;

    if (try_match(ctx, "(")) {
        /*Empty brackets => unit literal*/
        if (see(ctx, ")"))
            node = astCreateUnitLit();

        else {
            node = parseExpr(ctx);

            /*Tuple literal*/
            if (see(ctx, ",")) {
                vector(ast*) nodes = vectorInit(3, malloc);
                vectorPush(&nodes, node);

                while (try_match(ctx, ","))
                    vectorPush(&nodes, parseExpr(ctx));

                node = astCreateTupleLit(nodes);
            }
        }

        match(ctx, ")");

    /*List literal*/
    } else if (try_match(ctx, "[")) {
        vector(ast*) nodes = vectorInit(4, malloc);

        if (waiting_for(ctx, "]")) do {
            vectorPush(&nodes, parseExpr(ctx));
        } while (try_match(ctx, ","));

        node = astCreateListLit(nodes);

        match(ctx, "]");

    } else if (see(ctx, "\\")) {
        node = parseFnLit(ctx);

    } else if (see(ctx, "true") || see(ctx, "false")) {
        node = astCreateBoolLit(see(ctx, "true"));
        accept(ctx);

    } else if (see_kind(ctx, tokenIntLit)) {
        node = astCreateIntLit(atoi(ctx->current.buffer));
        accept(ctx);

    } else if (see_kind(ctx, tokenStrLit)) {
        node = astCreateStrLit(ctx->current.buffer);
        accept(ctx);

    } else if (see_kind(ctx, tokenNormal)) {
        sym* symbol;

        if (isPathToken(ctx->current.buffer))
            node = parsePath(ctx);

        else if ((symbol = symLookup(ctx->scope, ctx->current.buffer)))
            node = astCreateSymbol(symbol);

        else
            node = astCreateFileLit(ctx->current.buffer);

        accept(ctx);

    } else {
        expected(ctx, "expression");
        node = astCreateInvalid();
    }

    return node;
}
Example #5
0
File: cc.c Project: Fedjmike/mini-c
void decl (int kind) {
    //A C declaration comes in three forms:
    // - Local decls, which end in a semicolon and can have an initializer.
    // - Parameter decls, which do not and cannot.
    // - Module decls, which end in a semicolon unless there is a function body.

    bool fn = false;
    bool fn_impl = false;
    int local;

    next();

    while (try_match("*"))
        ;

    //Owned (freed) by the symbol table
    char* ident = strdup(buffer);
    next();

    //Functions
    if (try_match("(")) {
        if (kind == decl_module)
            new_scope();

        //Params
        if (waiting_for(")")) do {
            decl(decl_param);
        } while (try_match(","));

        match(")");

        new_fn(ident);
        fn = true;

        //Body
        if (see("{")) {
            require(kind == decl_module, "a function implementation is illegal here\n");

            fn_impl = true;
            function(ident);
        }

    //Add it to the symbol table
    } else {
        if (kind == decl_local) {
            local = new_local(ident);

        } else
            (kind == decl_module ? new_global : new_param)(ident);
    }

    //Initialization

    if (see("="))
        require(!fn && kind != decl_param,
                fn ? "cannot initialize a function\n" : "cannot initialize a parameter\n");

    if (kind == decl_module) {
        fputs(".section .data\n", output);

        if (try_match("=")) {
            if (token == token_int)
                fprintf(output, "%s: .quad %d\n", ident, atoi(buffer));

            else
                error("expected a constant expression, found '%s'\n");

            next();

        //Static data defaults to zero if no initializer
        } else if (!fn)
            fprintf(output, "%s: .quad 0\n", ident);

        fputs(".section .text\n", output);

    } else if (try_match("=")) {
        expr(0);
        fprintf(output, "mov dword ptr [ebp%+d], eax\n", offsets[local]);
    }

    if (!fn_impl && kind != decl_param)
        match(";");
}