changed command to take char * and char **

master
bx 2022-04-03 09:05:25 +01:00
parent de01f1cf91
commit b7a4fec162
11 changed files with 38 additions and 31 deletions

View File

@ -1,5 +1,5 @@
void
c_copy(char *c) {
c_copy(char *c, char **argv) {
c += 2;
int start = atoi(c);
for(; *c != '\0' && *c != ' '; c++);
@ -23,7 +23,7 @@ c_copy(char *c) {
}
void
c_paste(char *c) {
c_paste(char *c, char **argv) {
Row *line = b_getline(buffer, atoi(c + 2));
if (line == NULL) {puts("invalid line."); return; }
if (clipboard == NULL) {puts("empty clipboard"); return; }
@ -33,3 +33,4 @@ c_paste(char *c) {
}
}

View File

@ -1,5 +1,5 @@
void
c_delete(char *c) {
c_delete(char *c, char **argv) {
Row *line = b_getline(buffer, atoi(c + 2));
Row *prev_line = b_getline(buffer, atoi(c + 2) - 1);
if (line == NULL || prev_line == NULL) {puts ("invalid line"); return; }
@ -10,3 +10,4 @@ c_delete(char *c) {
}

View File

@ -1,5 +1,5 @@
void
c_open(char *file) {
c_open(char *file, char **argv) {
FILE *f = fopen(file + 2, "r");
if(f == NULL) { puts("couldnt open file."); return; }
b_truncate(buffer);
@ -20,7 +20,7 @@ c_open(char *file) {
}
void
c_save(char *file) {
c_save(char *file, char **argv) {
FILE *f = fopen(file + 2, "w");
if(f == NULL) { puts("couldnt open file."); return; }
Row *row = buffer;
@ -37,3 +37,4 @@ c_save(char *file) {
}

View File

@ -1,5 +1,5 @@
void
c_indent(char *c) {
c_indent(char *c, char **argv) {
Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; }
@ -11,7 +11,7 @@ c_indent(char *c) {
}
void
c_unindent(char *c) {
c_unindent(char *c, char **argv) {
Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; }
@ -22,3 +22,4 @@ c_unindent(char *c) {
}

View File

@ -1,5 +1,5 @@
void
c_insertline(char *c) {
c_insertline(char *c, char **argv) {
c += 2;
int line = atoi(c);
for(; *c != 0 && *c != ' '; c++);
@ -20,3 +20,4 @@ c_insertline(char *c) {
r->text[i] = 0;
}
}

View File

@ -8,13 +8,16 @@ else printf("%c", row->text[i]);
}
void
c_print(char *c) {
Row *row = buffer;
c_print(char *c, char **argv) {
Row *row = buffer;
int line = 1;
if (atoi(c + 2) != 0) line = atoi(c + 2);
row = b_getline(buffer, line);
if (row == NULL) {puts("invalid line."); return;}
for (;; line++) {
printf("%3.i| ", line);
print_with_fixed_tabs(row);
puts("");
printf("%3.i| ", line);
print_with_fixed_tabs(row);
puts("");
if (row->n == NULL) break;
row = row -> n;
}
@ -24,3 +27,5 @@ puts("");

View File

@ -1,5 +1,6 @@
void
c_quit(char *b) {
c_quit(char *b, char **argv) {
exit(0);
}

View File

@ -1,5 +1,5 @@
void
c_writeline(char *c) {
c_writeline(char *c, char **argv) {
c += 2;
int line = atoi(c);
for(; *c != 0 && *c != ' '; c++);
@ -17,3 +17,4 @@ c_writeline(char *c) {
r->text[i] = 0;
}
}

View File

@ -1,4 +1,4 @@
void (*commands[256])(char *) = {
void (*commands[256])(char *, char **) = {
NULL,
['O'] = c_open,
['w'] = c_writeline,
@ -15,3 +15,4 @@ NULL,

14
efn.h
View File

@ -1,22 +1,18 @@
#include <stdlib.h>
#include "globals.h"
/*
extern void (*commands[256])(char *);
*/
void c_quit(char *);
void c_open(char *);
#include <string.h>
void b_truncate(Row *);
void c_print(char *);
Row *b_insert(Row *);
Row *b_getline(Row *, int);
void c_writeline(char *);
void c_save(char*);
void c_insertline(char *);
void c_indent(char *);
void c_unindent(char *);
void c_delete(char *);
extern void (*commands[256])(char *, char **);

10
main.c
View File

@ -1,4 +1,4 @@
void
char **
parse_command(char *input) {
static char com[COM_BUF_SIZE];
memcpy(com, input, COM_BUF_SIZE);
@ -15,10 +15,7 @@ parse_command(char *input) {
com[i] = '\0';
next_start++;
}
for (int i = 0; starts[i] != NULL; i++) {
puts(starts[i]);
}
return &starts[0];
}
int
@ -32,10 +29,11 @@ main(int argc, char **argv) {
if (buf[i] == '\n')
buf[i] = '\0';
if (com != NULL) {
if (commands[com[0]] != NULL) commands[com[0]](buf);
if (commands[com[0]] != NULL) commands[com[0]](buf, parse_command(com));
else puts("invalid command.");
}
}
return 0;
}