Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
16ddb166
Commit
16ddb166
authored
Jul 30, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2462 from cremno/mirb-malloc-history_path
mirb: allocate `history_path` dynamically
parents
23b6e22c
82a33564
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
25 deletions
+53
-25
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
+53
-25
No files found.
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
View file @
16ddb166
...
@@ -29,18 +29,48 @@
...
@@ -29,18 +29,48 @@
#define MIRB_USING_HISTORY()
#define MIRB_USING_HISTORY()
#endif
#endif
#ifdef ENABLE_READLINE
#include <limits.h>
static
const
char
*
history_file_name
=
".mirb_history"
;
char
history_path
[
PATH_MAX
];
#endif
#include "mruby.h"
#include "mruby.h"
#include "mruby/array.h"
#include "mruby/array.h"
#include "mruby/proc.h"
#include "mruby/proc.h"
#include "mruby/compile.h"
#include "mruby/compile.h"
#include "mruby/string.h"
#include "mruby/string.h"
#ifdef ENABLE_READLINE
static
const
char
history_file_name
[]
=
".mirb_history"
;
static
char
*
get_history_path
(
mrb_state
*
mrb
)
{
char
*
path
=
NULL
;
const
char
*
home
=
getenv
(
"HOME"
);
#ifdef _WIN32
if
(
home
!=
NULL
)
{
home
=
getenv
(
"USERPROFILE"
);
}
#endif
if
(
home
!=
NULL
)
{
int
len
=
snprintf
(
NULL
,
0
,
"%s/%s"
,
home
,
history_file_name
);
if
(
len
>=
0
)
{
size_t
size
=
len
+
1
;
path
=
(
char
*
)
mrb_malloc_simple
(
mrb
,
size
);
if
(
path
!=
NULL
)
{
int
n
=
snprintf
(
path
,
size
,
"%s/%s"
,
home
,
history_file_name
);
if
(
n
!=
len
)
{
mrb_free
(
mrb
,
path
);
path
=
NULL
;
}
}
}
}
return
path
;
}
#endif
static
void
static
void
p
(
mrb_state
*
mrb
,
mrb_value
obj
,
int
prompt
)
p
(
mrb_state
*
mrb
,
mrb_value
obj
,
int
prompt
)
{
{
...
@@ -283,7 +313,7 @@ main(int argc, char **argv)
...
@@ -283,7 +313,7 @@ main(int argc, char **argv)
int
last_char
;
int
last_char
;
int
char_index
;
int
char_index
;
#else
#else
char
*
h
ome
=
NULL
;
char
*
h
istory_path
;
#endif
#endif
mrbc_context
*
cxt
;
mrbc_context
*
cxt
;
struct
mrb_parser_state
*
parser
;
struct
mrb_parser_state
*
parser
;
...
@@ -310,6 +340,18 @@ main(int argc, char **argv)
...
@@ -310,6 +340,18 @@ main(int argc, char **argv)
return
n
;
return
n
;
}
}
#ifdef ENABLE_READLINE
history_path
=
get_history_path
(
mrb
);
if
(
history_path
==
NULL
)
{
fputs
(
"failed to get history path
\n
"
,
stderr
);
mrb_close
(
mrb
);
return
EXIT_FAILURE
;
}
MIRB_USING_HISTORY
();
MIRB_READ_HISTORY
(
history_path
);
#endif
print_hint
();
print_hint
();
cxt
=
mrbc_context_new
(
mrb
);
cxt
=
mrbc_context_new
(
mrb
);
...
@@ -320,22 +362,6 @@ main(int argc, char **argv)
...
@@ -320,22 +362,6 @@ main(int argc, char **argv)
ai
=
mrb_gc_arena_save
(
mrb
);
ai
=
mrb_gc_arena_save
(
mrb
);
#ifdef ENABLE_READLINE
MIRB_USING_HISTORY
();
home
=
getenv
(
"HOME"
);
#ifdef _WIN32
if
(
!
home
)
home
=
getenv
(
"USERPROFILE"
);
#endif
if
(
home
)
{
strcpy
(
history_path
,
home
);
strcat
(
history_path
,
"/"
);
strcat
(
history_path
,
history_file_name
);
MIRB_READ_HISTORY
(
history_path
);
}
#endif
while
(
TRUE
)
{
while
(
TRUE
)
{
#ifndef ENABLE_READLINE
#ifndef ENABLE_READLINE
print_cmdline
(
code_block_open
);
print_cmdline
(
code_block_open
);
...
@@ -436,12 +462,14 @@ main(int argc, char **argv)
...
@@ -436,12 +462,14 @@ main(int argc, char **argv)
mrb_parser_free
(
parser
);
mrb_parser_free
(
parser
);
cxt
->
lineno
++
;
cxt
->
lineno
++
;
}
}
mrbc_context_free
(
mrb
,
cxt
);
mrb_close
(
mrb
);
#ifdef ENABLE_READLINE
#ifdef ENABLE_READLINE
MIRB_WRITE_HISTORY
(
history_path
);
MIRB_WRITE_HISTORY
(
history_path
);
mrb_free
(
mrb
,
history_path
);
#endif
#endif
mrbc_context_free
(
mrb
,
cxt
);
mrb_close
(
mrb
);
return
0
;
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment