summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2004-12-14 16:28:30 +0000
committerSascha Hauer <s.hauer@pengutronix.de>2004-12-14 16:28:30 +0000
commit61d2cfe72a3ebb94891bf55684c955de0ca4e5e3 (patch)
tree6fa667f0d23911f334a499e29a2dae9b66fd417e
parente341a29c81700d3c82fc8b9566ba8c0d9adef3fe (diff)
downloadmemedit-61d2cfe72a3ebb94891bf55684c955de0ca4e5e3.tar.gz
memedit-61d2cfe72a3ebb94891bf55684c955de0ca4e5e3.tar.xz
- switched to readline to allow command editing/history
- print ascii too with md.[bwl] command
-rw-r--r--Makefile.am2
-rw-r--r--configure.ac19
-rw-r--r--fpgaedit.c4
-rw-r--r--fpgaedit.l72
-rw-r--r--fpgaedit.y104
5 files changed, 155 insertions, 46 deletions
diff --git a/Makefile.am b/Makefile.am
index 970e769..58134fa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4,6 +4,8 @@ fpgaedit_SOURCES = fpgaedit_parser.c fpgaedit_parser.h \
fpgaedit_CFLAGS = -W -Wall
+fpgaedit_LDADD = $(READLINE_LIBS)
+
# only needed to recompile the commandlineparser
fpgaedit_parser.c fpgaedit_parser.h: fpgaedit_parser.gp
genparse -o fpgaedit_parser fpgaedit_parser.gp
diff --git a/configure.ac b/configure.ac
index 8c49b48..38abb45 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,25 @@ AC_PROG_YACC
# Checks for libraries.
+#
+# define helper function
+#
+AC_DEFUN([AC_CHECK_LIBRARY],
+ [ac_save_LIBS="$LIBS"
+ LIBS="$$3 $ac_save_LIBS"
+ AC_CHECK_LIB($1,$2)
+ $3=`echo "$LIBS" | sed "s% $ac_save_LIBS$%%"`
+ LIBS="$ac_save_LIBS"])
+
+
+# Checks for libraries.
+# FIXME: Replace `main' with a function in `-lncurses':
+AC_CHECK_LIBRARY(readline, main, READLINE_LIBS)
+if test x"${READLINE_LIBS}" = x""; then
+ AC_MSG_ERROR([You need the readline library])
+fi
+AC_SUBST(READLINE_LIBS)
+
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
diff --git a/fpgaedit.c b/fpgaedit.c
index f876697..1403e80 100644
--- a/fpgaedit.c
+++ b/fpgaedit.c
@@ -16,6 +16,7 @@ extern int yyparse();
#include "fpgaedit_parser.h"
int debug = 0;
+FILE *rl_instream;
FILE *fhan = NULL;
int main (int argc, char *argv[])
@@ -42,7 +43,8 @@ int main (int argc, char *argv[])
if (!fhan) printf ("opening \"%s\" failed\n%s\n",
argv[kom_arg->optind], strerror(errno));
}
-
+
+ rl_instream = stdin;
yyparse ();
return EXIT_SUCCESS;
diff --git a/fpgaedit.l b/fpgaedit.l
index 45f96a1..9481b0d 100644
--- a/fpgaedit.l
+++ b/fpgaedit.l
@@ -11,12 +11,84 @@
%option prefix="zz"
%{
+#include <unistd.h>
+#include <stdio.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
/* need this for the call to atol() below */
#include <math.h>
#include "y.tab.h"
extern int debug;
extern int in_line;
+
+extern void yyerror(const char *);
+
+/* Have input call the following function. */
+#undef YY_INPUT
+#define YY_INPUT(buf,result,max_size) \
+ rl_input((char *)buf, &result, max_size)
+
+/* Variables to help interface readline with bc. */
+static char *rl_line = (char *)NULL;
+static char *rl_start = (char *)NULL;
+static int rl_len = 0;
+
+/* Definitions for readline access. */
+extern FILE *rl_instream;
+
+/* rl_input puts upto MAX characters into BUF with the number put in
+ BUF placed in *RESULT. If the yy input file is the same as
+ rl_instream (stdin), use readline. Otherwise, just read it.
+*/
+
+static void
+rl_input (buf, result, max)
+ char *buf;
+ int *result;
+ int max;
+{
+ if (yyin != rl_instream) {
+ while ( (*result = read( fileno(yyin), buf, max )) < 0 )
+ if (errno != EINTR) {
+ yyerror( "read() in flex scanner failed" );
+ exit (1);
+ }
+ return;
+ }
+
+ /* Do we need a new string? */
+ if (rl_len == 0) {
+ if (rl_start)
+ free(rl_start);
+ rl_start = readline ("->");
+ if (rl_start == NULL) {
+ /* end of file */
+ *result = 0;
+ rl_len = 0;
+ return;
+ }
+ rl_line = rl_start;
+ rl_len = strlen (rl_line)+1;
+ if (rl_len != 1)
+ add_history (rl_line);
+ rl_line[rl_len-1] = '\n';
+ fflush (stdout);
+ }
+
+ if (rl_len <= max) {
+ strncpy (buf, rl_line, rl_len);
+ *result = rl_len;
+ rl_len = 0;
+ } else {
+ strncpy (buf, rl_line, max);
+ *result = max;
+ rl_line += max;
+ rl_len -= max;
+ }
+}
+
%}
DIGIT [0-9]
diff --git a/fpgaedit.y b/fpgaedit.y
index 879ec4b..07edb13 100644
--- a/fpgaedit.y
+++ b/fpgaedit.y
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <ctype.h>
extern int errno;
extern FILE *fhan;
@@ -17,7 +18,7 @@ typedef struct variable {
} variable;
int in_line = 0;
-int linelen = 16;
+#define LINELEN 16
int write_len;
variable *vlist;
@@ -120,7 +121,7 @@ mem: MD {
if (fhan)
{
if (fseek (fhan, $3, SEEK_SET) == -1)
- printf ("set offset %d failed\n", $3);
+ printf ("set offset %ld failed\n", $3);
else
{
unsigned int c = 0;
@@ -168,7 +169,7 @@ exp: num {
fseek (fhan, offset, SEEK_SET);
}
printf ("new values:");
- if (offset%(linelen*4))
+ if (offset%(LINELEN*4))
printf ("0x%08lx", offset);
}
}
@@ -184,7 +185,7 @@ exp: num {
if (fhan) {
offset = ftell (fhan);
- if (offset%linelen) printf (" ");
+ if (offset%LINELEN) printf (" ");
else printf ("\n0x%08lx ", offset);
fwrite (&c, write_len, 1, fhan);
fflush (fhan);
@@ -294,53 +295,66 @@ int get_variable (char *param) {
}
void hex_output (int offset, int len, int count) {
- if (fhan) {
- if (fseek (fhan, offset, SEEK_SET) == -1)
- printf ("set offset %d failed\n", offset);
- else {
- unsigned int c;
- int i, t;
-
- for (t = 0; t <= count; t++) {
- printf ("0x%08x ", offset + t*linelen);
- switch (len) {
- case 1:
- for (i = 0; i < linelen; i++) {
- c = 0;
- if (fread (&c, 1, 1,
- fhan) != 1) break;
- printf ("%02x ", c);
- }
- break;
- case 2:
- for (i = 0; i < linelen/2; i++) {
- c = 0;
- if (fread (&c, 2, 1,
- fhan) != 1) break;
- printf ("%04x ", c);
- }
- break;
-
- case 4:
- for (i = 0; i < linelen/4; i++) {
- c = 0;
- if (fread (&c, 4, 1,
- fhan) != 1) break;
- printf ("%08x ", c);
- }
- break;
- }
- printf ("\n");
+ unsigned int c;
+ int i=0, j, t;
+ unsigned char linebuf[LINELEN];
+
+ if (!fhan) {
+ printf ("please open file\n");
+ return;
+ }
+
+ if (fseek (fhan, offset, SEEK_SET) != 0) {
+ printf ("set offset %d failed\n", offset);
+ return;
+ }
+
+ for (t = 0; t <= count; t++) {
+ unsigned int *lineint = (unsigned int *)linebuf;
+ unsigned short *lineshort = (unsigned short *)linebuf;
+
+ printf ("%08x: ", offset + t*LINELEN);
+ switch (len) {
+ case 1:
+ for (i = 0; i < LINELEN; i++) {
+ c = 0;
+ if (fread (&c, 1, 1, fhan) != 1) break;
+ printf ("%02x ", c);
+ linebuf[i] = c;
+ }
+ break;
+ case 2:
+ for (i = 0; i < LINELEN; i+=2) {
+ c = 0;
+ if (fread (&c, 2, 1, fhan) != 1) break;
+ printf ("%04x ", c);
+ *lineshort++ = c;
+ }
+ break;
+
+ case 4:
+ for (i = 0; i < LINELEN; i+=4) {
+ c = 0;
+ if (fread (&c, 4, 1, fhan) != 1) break;
+ printf ("%08x ", c);
+ *lineint++ = c;
}
+ break;
+ }
+
+ for (j = 0; j < i; j++) {
+ if(isprint(linebuf[j]))
+ printf("%c",linebuf[j]);
+ else
+ printf(".");
}
+ printf ("\n");
+ if(i < LINELEN)
+ return;
}
- else printf ("please open file\n");
-
- return;
}
int yylex (void) {
- if (!in_line) printf ("fpgaedit> ");
in_line++;
return zzlex ();
}