-
Notifications
You must be signed in to change notification settings - Fork 35
add -u flag, error if variable undefined #77
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* var.c: provide "public" functions for adding and removing variables from the symbol table */ | ||
|
|
||
| #include <stdio.h> | ||
| #include "rc.h" | ||
|
|
||
| #include "input.h" | ||
|
|
@@ -52,14 +52,21 @@ extern bool varassign_string(char *extdef) { | |
| return TRUE; | ||
| } | ||
|
|
||
| char* get_message(char *format, char *msg) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole helper function actually is unneeded -- and allocating error messages with malloc will leak memory. Incidentally,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a while since I ran this, but AFAICR trip.rc only succeeds with this flag OFF. Because there are undefined variables being used in there 😃 . Notwithstanding I have been using this branch as my daily drive. I will have a look at this again.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. trip.rc can exercise features selectively. It's not all-or-nothing. |
||
| size_t needed = snprintf(NULL, 0, format, msg); | ||
| char *buffer = malloc(needed+1); | ||
| sprintf(buffer, format, msg); | ||
| return buffer; | ||
| } | ||
|
|
||
| /* | ||
| Return a List based on a name lookup. If the list is in external (string) form, | ||
| convert it to internal (List) form. Treat $n (n is an integer) specially as $*(n). | ||
| Also check to see if $status is being dereferenced. (we lazily evaluate the List | ||
| associated with $status) | ||
| */ | ||
|
|
||
| extern List *varlookup(char *name) { | ||
| List *varlookupAux(char *name, bool strict) { | ||
| Variable *look; | ||
| List *ret, *l; | ||
| int sub; | ||
|
|
@@ -79,19 +86,29 @@ extern List *varlookup(char *name) { | |
| return ret; | ||
| } | ||
| look = lookup_var(name); | ||
| if (look == NULL) | ||
| return NULL; /* not found */ | ||
| if (look == NULL) { | ||
| if (strict && dashewe) | ||
| rc_error(get_message("Undefined variable '%s'\n", name)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent capitalization
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you prefer |
||
| return NULL; /* not found */ | ||
| } | ||
| if (look->def != NULL) | ||
| return look->def; | ||
| if (look->extdef == NULL) | ||
| return NULL; /* variable was set to null, e.g., a=() echo foo */ | ||
| ret = parse_var(look->extdef); | ||
| if (ret == NULL) { | ||
| look->extdef = NULL; | ||
| if (dashewe) rc_error(get_message("undefined variable '%s'\n", name)); | ||
| return NULL; | ||
| } | ||
| return look->def = ret; | ||
| } | ||
| extern List *varlookup(char *name) { | ||
| return varlookupAux(name, TRUE); | ||
| } | ||
| extern List *varlookupNonStrict(char *name) { | ||
| return varlookupAux(name, FALSE); | ||
| } | ||
|
|
||
| /* lookup a variable in external (string) form, converting if necessary. Used by makeenv() */ | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer the
nallochelper, do not include stdio or usemallochere.nallocis scoped to command-lifetime allocations.