Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link-parser fixes #1536

Merged
merged 15 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions link-parser/command-line.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,42 @@ Switch default_switches[] =
{NULL, Cmd, NULL, NULL}
};

static void put_opts_in_local_vars(Command_Options *);

/*
* A way to record the options default values.
* Record the parse options default values.
*
* Set the string parse options to their static default value so they
* don't point to a static (test, debug) or dynamic (dialect) memory of
* the library (but anyway we need to do that for the test,debug and
* verbosity parse options because they might have been set by command
* line arguments just before this function is invoked.)
*/
void save_default_opts(Command_Options *copts)
{
put_opts_in_local_vars(copts);
saved_defaults = local;
// XXX Set defaults assuming stable library settings
saved_defaults.test = (char *)"";
saved_defaults.debug = (char *)"";
saved_defaults.dialect = (char *)"";
saved_defaults.verbosity = 1;
}

static void restore_default_local_vars(void)
{
local = saved_defaults;
}

// Return the value of the static verbosity.
// This avoids the need to define "verbosity" as extern, which may clash
// with the library "verbosity", and make the parse options "verbosity"
// available before the main loop in link-parser.c.
int get_verbosity(void)
{
return local.verbosity;
}

/**
* Gets rid of all the white space in the string s.
* Gets rid of all the white space in the string s.
*/
static void clean_up_string(char * s)
{
Expand All @@ -157,7 +175,7 @@ static void clean_up_string(char * s)
if (0 == w) break;
if (0 > (ssize_t)w)
{
prt_error("Unable to process UTF8 command input string.\n");
prt_error("Error: Unable to process UTF8 command input string.\n");
break;
}
len -= w;
Expand Down Expand Up @@ -194,7 +212,7 @@ static bool is_numerical_rhs(char *s)
if (0 == w) break;
if (0 > (ssize_t)w)
{
prt_error("Unable to process UTF8 command input string.\n");
prt_error("Error: Unable to process UTF8 command input string.\n");
break;
}
len -= w;
Expand Down Expand Up @@ -272,7 +290,6 @@ static const char *switch_value_string(const Switch *as)
#define HELPFILE_LANG_TEMPLATE_SIZE (sizeof(HELPFILE_LANG_TEMPLATE)-1)
#define HELPFILE_TEMPLATE_SIZE \
(sizeof(HELPFILE_BASE HELPFILE_EXT)+HELPFILE_LANG_TEMPLATE_SIZE)
#define D_USER_FILES 4 /* Debug level for files, see error.h. */
#define DEFAULT_HELP_LANG "en"

static FILE *help_file;
Expand Down Expand Up @@ -411,7 +428,7 @@ static FILE *open_help_file(int verbosity)
help_file = linkgrammar_open_data_file(help_filename);
}

if ((NULL == help_file) && (verbosity > D_USER_FILES))
if ((NULL == help_file) && (verbosity == D_USER_FILES))
{
prt_error("Error: Cannot open help file '%s': %s\n",
help_filename, strerror(errno));
Expand Down Expand Up @@ -514,7 +531,7 @@ static void display_help(const Switch *sp, Command_Options *copts)

if (feof(hf))
{
if (local.verbosity >= D_USER_FILES)
if (local.verbosity == D_USER_FILES)
prt_error("Error: Cannot find command \"%s\" in help file\n",
sp->string);
}
Expand Down Expand Up @@ -772,9 +789,9 @@ static int handle_help_command(const Switch *as, char *line,
{
rc = -1; /* Error indication. */
if (count > 1)
prt_error("Ambiguous command: \"%s\". %s\n", s, helpmsg);
prt_error("Error: Ambiguous command: \"%s\". %s\n", s, helpmsg);
else
prt_error("Undefined command: \"%s\". %s\n", s, helpmsg);
prt_error("Error: Undefined command: \"%s\". %s\n", s, helpmsg);
}
}
}
Expand Down Expand Up @@ -851,7 +868,7 @@ static int x_issue_special_command(char * line, Command_Options *copts, Dictiona

if (count > 1)
{
prt_error("Ambiguous command \"%s\". %s\n", s, helpmsg);
prt_error("Error: Ambiguous command \"%s\". %s\n", s, helpmsg);
return -1;
}
if (count == 1)
Expand All @@ -862,7 +879,7 @@ static int x_issue_special_command(char * line, Command_Options *copts, Dictiona
size_t junk = strcspn(s, WHITESPACE);
if (junk != strlen(s))
{
prt_error("Junk after a boolean variable: \"%s\". %s\n",
prt_error("Error: Junk after a boolean variable: \"%s\". %s\n",
&s[junk], helpmsg);
return -1;
}
Expand Down Expand Up @@ -999,7 +1016,7 @@ static int x_issue_special_command(char * line, Command_Options *copts, Dictiona
return -1;
}

static void put_opts_in_local_vars(Command_Options* copts)
void put_opts_in_local_vars(Command_Options* copts)
{
Parse_Options opts = copts->popts;
local.verbosity = parse_options_get_verbosity(opts);
Expand Down
3 changes: 3 additions & 0 deletions link-parser/command-line.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <link-grammar/link-includes.h>

#define D_USER_FILES 4 /* Debug level for files, see error.h. */

#define COMMENT_CHAR '%' /* input lines beginning with this are ignored */
#define WHITESPACE " \t\v\r\n" /* ASCII-only is sufficient here */
#define FIELD_WIDTH(str, width) (int)((width)+strlen(str)-utf8_strwidth(str))
Expand Down Expand Up @@ -61,6 +63,7 @@ typedef struct {
} Command_Options;

void put_local_vars_in_opts(Command_Options *);
void put_opts_in_local_vars(Command_Options *);
void setup_panic_parse_options(Command_Options *, int);

typedef enum
Expand Down
14 changes: 8 additions & 6 deletions link-parser/lg_readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ void find_history_filepath(const char *dictname, const char *argv0,
prt_error("Warning: xdg_get_home(XDG_BD_STATE) failed; "
"input history will not be supported.\n");
history_file = strdup("dev/null");
return;
}

history_file = hfile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else is needed here. I'll put it in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 1a4775a

Copy link
Member Author

@ampli ampli May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just noted that I had a typo in "dev/null" (missing initial /).
EDIT: Fixed in PR #1538.

if (get_verbosity() == D_USER_FILES)
prt_error("Debug: Using history file \"%s\"\n", history_file);
}

/**
Expand Down Expand Up @@ -375,8 +376,9 @@ static unsigned char lg_complete(EditLine *el, int ch)
{
if (fchdir(cwdfd) < 0)
{
/* This shouldn't happen, unless maybe the directory to which
* cwdfd reveres becomes unreadable after cwdfd is created. */
/* Unexpected error: may occur only if the directory
* referenced by cwdfd becomes unreadable after cwdfd
* has been established. */
printf("\nfchdir(): Cannot change directory back: %s\n",
strerror(errno));
}
Expand Down Expand Up @@ -419,7 +421,7 @@ char *lg_readline(const char *mb_prompt)
wc_prompt = malloc (sz*sizeof(wchar_t));
mbstowcs(wc_prompt, mb_prompt, sz);

hist = history_winit(); /* Init built-in history */
hist = history_winit(); /* Init built-in history */
el = el_init("link-parser", stdin, stdout, stderr);
history_w(hist, &ev, H_SETSIZE, 100);
history_w(hist, &ev, H_SETUNIQUE, 1);
Expand All @@ -442,7 +444,7 @@ char *lg_readline(const char *mb_prompt)
el_source(el, NULL); /* Source the user's defaults file. */
}

int numc = 1; /* Uninitialized at libedit. */
int numc = 1; /* Uninitialized at libedit. */
const wchar_t *wc_line = el_wgets(el, &numc);

/* Received end-of-file */
Expand All @@ -467,7 +469,7 @@ char *lg_readline(const char *mb_prompt)
/* fwprintf(stderr, L"==> got %d %ls", numc, wc_line); */

size_t byte_len = wcstombs(NULL, wc_line, 0) + 4;
free(mb_line); // free previous.
free(mb_line); // free previous.
if (byte_len == (size_t)-1)
{
prt_error("Error: Unable to process UTF8 in input string.\n");
Expand Down
11 changes: 6 additions & 5 deletions link-parser/lg_xdg.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct

xdg_definition xdg_def[] =
{
{ "/.local/state", "XDG_STATE_HOME" },
{ "/.local/state", "XDG_STATE_HOME" },
// Add more definitions if needed.
};

Expand Down Expand Up @@ -84,30 +84,31 @@ static bool is_sep(int c)
*/
static bool make_dirpath(const char *path)
{
char *dir = strdup(path);
struct stat sb;

#if defined(_WIN32) || defined(__CYGWIN__)
// Skip Windows UNC path \\X
if (is_sep(dir[0]) && is_sep(dir[1]))
if (is_sep(path[0]) && is_sep(path[1]))
{
const char *p;

// Start from the root or network share
for (p = dir + 2; *p != '\0'; p++)
for (p = path + 2; *p != '\0'; p++)
if (is_sep(*p)) break;
if (*p == '\0') return true; // No further subdirectories
}
#endif

char *dir = strdup(path);

for (char *p = dir+1; '\0' != *p; p++)
{
char sep = *p;
if (is_sep(*p))
{
if (is_sep(p[-1])) continue; // Ignore directory separator sequences
*p = '\0'; // Now dir is the path up to this point
//prt_error("DEBUG: mkdir: '%s'\n", dir);
//prt_error("Debug: mkdir: '%s'\n", dir);
if (mkdir(dir, S_IRWXU) == -1)
{
int save_errno = errno;
Expand Down
Loading