fix: using file descriptors instead of FILE*
we can directly write to a socket using dprintf, no need for a buffer
This commit is contained in:
@@ -2,4 +2,4 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int pasm_run_script(const char *filename, char **file, size_t lines, FILE* _fstream);
|
int pasm_run_script(const char *filename, char **file, size_t lines, int _fstream);
|
||||||
|
|||||||
10
src/api.c
10
src/api.c
@@ -11,18 +11,18 @@ void api_put() {
|
|||||||
int mode = state->STACK[state->STACK_IDX--]; // 1 for char, 2 for num
|
int mode = state->STACK[state->STACK_IDX--]; // 1 for char, 2 for num
|
||||||
if (mode != 1 && mode != 2) return;
|
if (mode != 1 && mode != 2) return;
|
||||||
|
|
||||||
FILE *f = fstream;
|
int f = fstream;
|
||||||
if (f == stderr)
|
if (f == fileno(stderr))
|
||||||
f = stdout;
|
f = fileno(stdout);
|
||||||
|
|
||||||
if (mode == 1) {
|
if (mode == 1) {
|
||||||
char c = state->STACK[state->STACK_IDX--];
|
char c = state->STACK[state->STACK_IDX--];
|
||||||
if (c == '\0') c = ' ';
|
if (c == '\0') c = ' ';
|
||||||
|
|
||||||
fprintf(f, "%c", c); //using printf and not write because of the buffer
|
dprintf(f, "%c", c); //using printf and not write because of the buffer
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(f, "%d", state->STACK[state->STACK_IDX--]);
|
dprintf(f, "%d", state->STACK[state->STACK_IDX--]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ size_t line_count = 0;
|
|||||||
int read_script(const char *filename, char ***buf, size_t *lines) {
|
int read_script(const char *filename, char ***buf, size_t *lines) {
|
||||||
FILE *script = fopen(filename, "r");
|
FILE *script = fopen(filename, "r");
|
||||||
if (script == NULL) {
|
if (script == NULL) {
|
||||||
fprintf(stderr, "Could not open %s.\n", filename);
|
dprintf(fstream, "Could not open %s.\n", filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,14 +23,14 @@ int read_script(const char *filename, char ***buf, size_t *lines) {
|
|||||||
char *line_copy = strdup(line);
|
char *line_copy = strdup(line);
|
||||||
char **temp = realloc(*buf, (line_count + 1) * sizeof(char*));
|
char **temp = realloc(*buf, (line_count + 1) * sizeof(char*));
|
||||||
if (temp == NULL) {
|
if (temp == NULL) {
|
||||||
fprintf(stderr, "Error allocating memory.\n");
|
dprintf(fstream, "Error allocating memory.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
*buf = temp;
|
*buf = temp;
|
||||||
(*buf)[line_count++] = line_copy;
|
(*buf)[line_count++] = line_copy;
|
||||||
}
|
}
|
||||||
if (line_count == 0) {
|
if (line_count == 0) {
|
||||||
fprintf(stderr, "%s is an empty file.\n", filename);
|
dprintf(fstream, "%s is an empty file.\n", filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,12 +83,12 @@ LABEL_ERR add_label(char *label, int line) {
|
|||||||
|
|
||||||
char *line_copy = strdup(label);
|
char *line_copy = strdup(label);
|
||||||
if (line_copy == NULL) {
|
if (line_copy == NULL) {
|
||||||
fprintf(fstream, "Error allocating memory.\n");
|
dprintf(fstream, "Error allocating memory.\n");
|
||||||
return LABEL_ERROR;
|
return LABEL_ERROR;
|
||||||
}
|
}
|
||||||
char **temp = realloc(state->labels, (state->num_labels + 1) * sizeof(char*));
|
char **temp = realloc(state->labels, (state->num_labels + 1) * sizeof(char*));
|
||||||
if (temp == NULL) {
|
if (temp == NULL) {
|
||||||
fprintf(fstream, "Error allocating memory.\n");
|
dprintf(fstream, "Error allocating memory.\n");
|
||||||
return LABEL_ERROR;
|
return LABEL_ERROR;
|
||||||
}
|
}
|
||||||
state->labels = temp;
|
state->labels = temp;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#define MAX_ARG_SIZE 25
|
#define MAX_ARG_SIZE 25
|
||||||
#define STACK_SIZE 50
|
#define STACK_SIZE 50
|
||||||
|
|
||||||
extern FILE *fstream;
|
extern int fstream;
|
||||||
|
|
||||||
typedef enum cmp_return_codes {
|
typedef enum cmp_return_codes {
|
||||||
CMP_ERROR = -1, //unused
|
CMP_ERROR = -1, //unused
|
||||||
|
|||||||
30
src/pasm.c
30
src/pasm.c
@@ -5,18 +5,18 @@
|
|||||||
#include "interpreter_states.h"
|
#include "interpreter_states.h"
|
||||||
#include "instructions.h"
|
#include "instructions.h"
|
||||||
|
|
||||||
FILE *fstream = NULL;
|
int fstream = 0;
|
||||||
void show_error(size_t line, char *line_) {
|
void show_error(size_t line, char *line_) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
int wrote = fprintf(fstream, "%llu| ", line + 1);
|
int wrote = dprintf(fstream, "%llu| ", line + 1);
|
||||||
#else
|
#else
|
||||||
int wrote = fprintf(fstream, "%ld| ", line + 1);
|
int wrote = dprintf(fstream, "%ld| ", line + 1);
|
||||||
#endif
|
#endif
|
||||||
fprintf(fstream, "%s\n", line_);
|
dprintf(fstream, "%s\n", line_);
|
||||||
fprintf(fstream, "%*s\n", wrote + 1, "^");
|
dprintf(fstream, "%*s\n", wrote + 1, "^");
|
||||||
fprintf(fstream, "%*s\n", wrote + 1, "|");
|
dprintf(fstream, "%*s\n", wrote + 1, "|");
|
||||||
for (int i = 0; i < wrote; i++)
|
for (int i = 0; i < wrote; i++)
|
||||||
fprintf(fstream, " ");
|
dprintf(fstream, " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_states() {
|
void show_states() {
|
||||||
@@ -39,13 +39,13 @@ int check_errors(char *line) {
|
|||||||
|
|
||||||
switch ((int)state->last_check_args_code) {
|
switch ((int)state->last_check_args_code) {
|
||||||
case WRONG_NUMBER :
|
case WRONG_NUMBER :
|
||||||
fprintf(fstream, "wrong number of arguments on line %d\n", state->curr_line + 1);
|
dprintf(fstream, "wrong number of arguments on line %d\n", state->curr_line + 1);
|
||||||
break;
|
break;
|
||||||
case ARG1_WRONG :
|
case ARG1_WRONG :
|
||||||
fprintf(fstream, "arg1 is invalid on line %d\n", state->curr_line + 1);
|
dprintf(fstream, "arg1 is invalid on line %d\n", state->curr_line + 1);
|
||||||
break;
|
break;
|
||||||
case ARG2_WRONG :
|
case ARG2_WRONG :
|
||||||
fprintf(fstream, "arg2 is invalid on line %d\n", state->curr_line + 1);
|
dprintf(fstream, "arg2 is invalid on line %d\n", state->curr_line + 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -53,26 +53,26 @@ int check_errors(char *line) {
|
|||||||
|
|
||||||
if (state->last_jmp_code) {
|
if (state->last_jmp_code) {
|
||||||
show_error(state->curr_line, line);
|
show_error(state->curr_line, line);
|
||||||
fprintf(fstream, "%s is not a valid label/api\n", state->args->arg1);
|
dprintf(fstream, "%s is not a valid label/api\n", state->args->arg1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state->last_stack_code != STACK_OK) {
|
if (state->last_stack_code != STACK_OK) {
|
||||||
show_error(state->curr_line, line);
|
show_error(state->curr_line, line);
|
||||||
fprintf(fstream, "stack %s on line %d\n",
|
dprintf(fstream, "stack %s on line %d\n",
|
||||||
state->last_stack_code == OVERFLOW ? "overflow" : "underflow", state->curr_line + 1);
|
state->last_stack_code == OVERFLOW ? "overflow" : "underflow", state->curr_line + 1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pasm_run_script(const char *filename, char **file, size_t lines, FILE *_fstream) {
|
int pasm_run_script(const char *filename, char **file, size_t lines, int _fstream) {
|
||||||
fstream = _fstream;
|
fstream = _fstream;
|
||||||
|
|
||||||
if (filename && read_script(filename, &file, &lines) == 1)
|
if (filename && read_script(filename, &file, &lines) == 1)
|
||||||
return 1;
|
return 1;
|
||||||
if (init_state() == 1) {
|
if (init_state() == 1) {
|
||||||
fprintf(fstream, "Failed to initialize the interpreter.\n");
|
dprintf(fstream, "Failed to initialize the interpreter.\n");
|
||||||
free_script(file);
|
free_script(file);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ int pasm_run_script(const char *filename, char **file, size_t lines, FILE *_fstr
|
|||||||
if (com == NULL || com->fptr == NULL) {
|
if (com == NULL || com->fptr == NULL) {
|
||||||
if (file[state->curr_line][strlen(line) - 1] != ':') {
|
if (file[state->curr_line][strlen(line) - 1] != ':') {
|
||||||
show_error(state->curr_line, file[state->curr_line]);
|
show_error(state->curr_line, file[state->curr_line]);
|
||||||
fprintf(fstream, "%s \"%s\"\n", "unknown expression", strtok(file[state->curr_line], " "));
|
dprintf(fstream, "%s \"%s\"\n", "unknown expression", strtok(file[state->curr_line], " "));
|
||||||
set_exit_state(-1);
|
set_exit_state(-1);
|
||||||
free(line);
|
free(line);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
SRC = lib_use.c \
|
SRC = lib_use.c
|
||||||
fmemopen.c
|
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
NAME = lib_use
|
NAME = lib_use
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
#include "fmemopen.h"
|
|
||||||
|
|
||||||
typedef int make_iso_compilers_happy; //...
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#include <share.h>
|
|
||||||
#include <io.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
/* https://github.com/Arryboom/fmemopen_windows */
|
|
||||||
|
|
||||||
FILE *fmemopen(void *buf, size_t len, __attribute__((unused)) const char *type)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
FILE *fp;
|
|
||||||
char tp[MAX_PATH - 13];
|
|
||||||
char fn[MAX_PATH + 1];
|
|
||||||
int * pfd = &fd;
|
|
||||||
int retner = -1;
|
|
||||||
char tfname[] = "MemTF_";
|
|
||||||
if (!GetTempPathA(sizeof(tp), tp))
|
|
||||||
return NULL;
|
|
||||||
if (!GetTempFileNameA(tp, tfname, 0, fn))
|
|
||||||
return NULL;
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration" //msys not happy
|
|
||||||
retner = _sopen_s(pfd, fn, _O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY | _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYRW, _S_IREAD | _S_IWRITE);
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
if (retner != 0)
|
|
||||||
return NULL;
|
|
||||||
if (fd == -1)
|
|
||||||
return NULL;
|
|
||||||
fp = _fdopen(fd, "wb+");
|
|
||||||
if (!fp) {
|
|
||||||
_close(fd);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/*File descriptors passed into _fdopen are owned by the returned FILE * stream.If _fdopen is successful, do not call _close on the file descriptor.Calling fclose on the returned FILE * also closes the file descriptor.*/
|
|
||||||
fwrite(buf, len, 1, fp);
|
|
||||||
rewind(fp);
|
|
||||||
return fp;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <stdio.h>
|
|
||||||
FILE *fmemopen(void *buf, size_t len, const char *type);
|
|
||||||
#endif
|
|
||||||
@@ -8,5 +8,5 @@ int main(int argc, char **argv) {
|
|||||||
fprintf(stderr, "Usage : %s filename\n", argv[0]);
|
fprintf(stderr, "Usage : %s filename\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return pasm_run_script(argv[1], 0, 0, stderr);
|
return pasm_run_script(argv[1], 0, 0, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,52 @@
|
|||||||
#include "../include/pasm.h"
|
#include "../include/pasm.h"
|
||||||
#include "fmemopen.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
int main(void) {
|
#define SERVER_IP "192.168.1.16"
|
||||||
char output[1024] = {0};
|
#define SERVER_PORT 1337
|
||||||
FILE *memfile = fmemopen(output, sizeof(output), "w+");
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
if (memfile != NULL) {
|
int main() {
|
||||||
pasm_run_script("../examples/test.pasm", 0, 0, memfile);
|
int clientSocket;
|
||||||
#ifdef _WIN32
|
struct sockaddr_in serverAddr;
|
||||||
fseek(memfile, 0, SEEK_SET);
|
char buffer[BUFFER_SIZE];
|
||||||
fread(output, sizeof(char), sizeof(output), memfile);
|
|
||||||
printf("%s", output);
|
if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||||
fclose(memfile);
|
fprintf(stderr, "Socket creation failed");
|
||||||
#else
|
return 1;
|
||||||
fclose(memfile);
|
|
||||||
printf("%s", output);
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
printf("null\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(SERVER_PORT);
|
||||||
|
if (inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr) <= 0) {
|
||||||
|
fprintf(stderr, "Invalid address/ Address not supported");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
|
||||||
|
fprintf(stderr, "Connection failed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
// Receive data from the server
|
||||||
|
int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
|
||||||
|
if (bytesRead == -1) {
|
||||||
|
fprintf(stderr, "Receive failed");
|
||||||
|
close(clientSocket);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[bytesRead] = '\0'; // Null-terminate the received data
|
||||||
|
if (bytesRead > 0 && buffer[bytesRead - 1] == '\n')
|
||||||
|
buffer[bytesRead - 1] = '\0'; //deletes \n
|
||||||
|
printf("script path: %s\n", buffer);
|
||||||
|
pasm_run_script(buffer, 0, 0, clientSocket); //considering buffer is the path of a valid .pasm script
|
||||||
|
}
|
||||||
|
|
||||||
|
close(clientSocket);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user