Skip to content
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
10 changes: 10 additions & 0 deletions burp.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ char *strdup(const char *s) {
}
#endif

void swap_burps(burpT *oldP, burpT *newP)
{
burpT temp;

temp = *oldP;
*oldP = *newP;
*newP = temp;
}


// Reset a burp without freeing its buffer, reducing explicit memory management.
// When in doubt use burp_close() instead.
void burp_reset(burpT *burpP)
Expand Down
2 changes: 2 additions & 0 deletions burp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void burpn(burpT *burpP, const char *stringP, int lth);
void burps(burpT *burpP, const char *stringP);
void burp_esc_quote(burpT *burpP, int offset, int quote);
void burp_rtrim(burpT *burpP);
void swap_burps(burpT *oldP, burpT *newP);


#define INDENT(X) X.m_indent++
#define OUTDENT(X) { assert(0 < X.m_indent); X.m_indent--; }
Expand Down
15 changes: 3 additions & 12 deletions fix_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ extern void seen_global(const char *nameP, _BOOL local);

char g_regmatch_var_name[] = "BASH_REMATCH";

static void exchange_burp_buffers(burpT *oldP, burpT *newP)
{
burpT temp;

temp = *oldP;
*oldP = *newP;
*newP = temp;
}

// Finds and returns end of a quoted string (or NULL if improperly quoted)
char * endQuotedString(char *stringP)
{
Expand Down Expand Up @@ -483,7 +474,7 @@ static void replaceSingleQuotes(void)
} }
burpc(&g_new, c);
}
exchange_burp_buffers(&g_buffer, &g_new);
swap_burps(&g_buffer, &g_new);
}

// emitQuoted(): Quotes the supplied string
Expand Down Expand Up @@ -1691,7 +1682,7 @@ static char * fix_string1(fix_typeE want, fix_typeE *gotP)

// Everything has been written to g_new
// Make it look as if it never left g_buffer
exchange_burp_buffers(&g_buffer, &g_new);
swap_burps(&g_buffer, &g_new);

compactWhiteSpace();
rename_special();
Expand All @@ -1706,7 +1697,7 @@ static char * fix_string1(fix_typeE want, fix_typeE *gotP)
got = FIX_EXPRESSION;
g_new.m_lth = 0;
burps(&g_new, translationP);
exchange_burp_buffers(&g_buffer, &g_new);
swap_burps(&g_buffer, &g_new);
}
}
unmarkExpand();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#! /usr/bin/env python
from __future__ import print_function
class Bash2Py(object):
#
__slots__ = ["val"]
#
def __init__(self, value=''):
self.val = value
#
def setValue(self, value=None):
self.val = value
return value


def GetVariable(name, local=locals()):
if name in local:
return local[name]
Expand All @@ -22,18 +26,27 @@ def Make(name, local=locals()):
globals()[name] = ret
return ret

Make("y").setValue("yyyyy")
print("in f2, y is",y.val)
) :
def f1 () :
global x

Make("x").setValue("xxxxx")
print("in f1, x is",x.val)
def f2 () :
global x
global y

Make("y").setValue("yyyyy")
print("in f2, y is",y.val)

f2()

def f2 () :
global y

Make("y").setValue("yyyyy")
print("in f2, y is",y.val)

f3()

def f3 () :
global z

Make("z").setValue("zzzzz")
print("in f3, z is",z.val)


f1()
16 changes: 7 additions & 9 deletions translate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2680,7 +2680,6 @@ static void reset_locals ()

static void print_function_def (FUNCTION_DEF *func)
{
burpT temp;
COMMAND *cmdcopy;
REDIRECT *func_redirects;
char *nameP = func->name->word;
Expand All @@ -2692,9 +2691,7 @@ static void print_function_def (FUNCTION_DEF *func)
func_redirects = NULL;
burp(&g_output, "def %s (", nameP);

temp = save;
save = g_output;
g_output = temp;
swap_burps(&save, &g_output);
g_output.m_lth = 0;
burps(&g_output, " ");
INDENT(g_output);
Expand All @@ -2713,7 +2710,7 @@ static void print_function_def (FUNCTION_DEF *func)
g_function_parms_count = 0;
emit_command (cmdcopy->type == cm_group
? cmdcopy->value.Group->command
: cmdcopy);
: cmdcopy); // "g_output" now holds the function body

remove_unwind_protect ();

Expand All @@ -2729,7 +2726,7 @@ static void print_function_def (FUNCTION_DEF *func)
// Insert global variable declarations
if (g_variable_namesP) {
for (current_varP = g_variable_namesP; current_varP; current_varP = next_varP) {
burp(&save, " global %s\n", current_varP->m_nameP);
burp(&save, " global %s\n", current_varP->m_nameP); // the indent here should match INDENT, dont assume its 4
next_varP = current_varP->m_nextP;
free(current_varP->m_nameP);
free(current_varP);
Expand All @@ -2738,12 +2735,13 @@ static void print_function_def (FUNCTION_DEF *func)
burpc(&save, '\n');
}

// "save" holds old backlog + function header "def foo(...):" + global declarations
// "g_output" holds the function body. We need swap-merge these.
newline("");
OUTDENT(g_output);
newline("");
temp = save;
save = g_output;
g_output = temp;
burps(&save, g_output.m_P);
swap_burps(&save, &g_output);

g_is_inside_function = FALSE;

Expand Down