about summary refs log tree commit diff stats
path: root/marco.c
blob: 6b8cbf6be6b14a5f360b99512e314b073d680c3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <xombrero.h>

static const char *message[] = {
	"I fully support your right to put restrictions on how I can modify"
	" or distribute something you created. Calling these restrictions "
	"\"liberty,\" however, is just Orwellian doublespeak",
	"GET OFF MY INTERNET LAWN!!!!",
	"here is your list of restrictions -> freedom!!!!",
	"i am all for it being free; i am all for not allowing abuse",
	"webkit 1.4 is a huge step backwards",
	"i honestly dont get how people equate gpl with liberty or freedom",
	"I have no idea where you came up with the idea that you have to "
	"continuously patch OpenBSD. Last time I cared about that was when "
	"I was still dicking around with Linux.",
	"Problem with discussion is that it doesn't add any code to cvs.",
	"I can insult you some more but I am not sure you'd get it.",
	"the mouse is for copy/paste and the interwebs",
	"You could run openbsd and be done with it. Unlike linux is doesn't "
	"suck so that helps that decision.",
	"Twitter is the dumbest fucking thing that has ever happened to the Internet.",
	"[speaking of {open,libre}office] it is an obese crack whore with herpes boils "
	"that are about to explode"
};

const char *
marco_message(void)
{
	const char	*str;

	str = message[arc4random_uniform(sizeof(message)/sizeof(message[0]))];

	return str;
}

int
marco(struct tab *t, struct karg *args)
{
	char			*page, line[64 * 1024];

	if (t == NULL)
		show_oops(NULL, "marco invalid parameters");

	line[0] = '\0';
	snprintf(line, sizeof line, "%s", marco_message());

	page = get_html_page("Marco Sez...", line, "", 0);

	load_webkit_string(t, page, XT_URI_ABOUT_MARCO, 0);
	g_free(page);

	return (0);
}
Kartik K. Agaram <vc@akkartik.com> 2015-07-15 22:08:21 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2015-07-15 22:08:21 -0700 1793 - rudimentary sandboxing for scenarios in the environment' href='/akkartik/mu/commit/081run_interactive.cc?h=main&id=2c3afb53dda7c83c70be4c12bd76930fe568b860'>2c3afb53 ^
32cd40ec ^


76d22198 ^
b17c196d ^
363be37f ^
260c3de2 ^
f43c2125 ^


09454849 ^
f43c2125 ^
87fbfc2d ^
32cd40ec ^





87fbfc2d ^
d6c9fe63 ^
9fdda88b ^
363be37f ^
b17c196d ^
87fbfc2d ^
12c3ee1e ^
9fdda88b ^









76d22198 ^














0685569c ^












9fdda88b ^

90938fcb ^

9fdda88b ^
76d22198 ^
90938fcb ^

9fdda88b ^



76d22198 ^

76d22198 ^
76d22198 ^





d6c9fe63 ^

9fdda88b ^

9fdda88b ^



32cd40ec ^
9fdda88b ^




32cd40ec ^
0685569c ^



32cd40ec ^

a2a6e35e ^
32cd40ec ^








32cd40ec ^

32cd40ec ^












a2a6e35e ^
32cd40ec ^

2e8c5d39 ^

9fdda88b ^
be13beeb ^

be13beeb ^









be13beeb ^


76d22198 ^










b17c196d ^
548cd00a ^

b17c196d ^

548cd00a ^

76d22198 ^






0685569c ^











f0eb3556 ^


80608f98 ^



f0eb3556 ^






80608f98 ^
f0eb3556 ^



80608f98 ^
f0eb3556 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279


                                                                          

                                

                                                                


                              





                                                        
 



                                                                      


                                             
                                                    



                                                                 
                     
                                                                           
                                  
                                
                                                    
                                
                           
                                         



                                                                            

 

                                 
                                                                                      

                            
               
       

                                                                           

                                                                                  

                                                                 
                              
                                                          







                                                          


                                                                                              
                                                            
                                    
                                              
                       


                                                                                  
                                         
   
                                                





                                                                             
                  
                                            
                             
                                                                         
              
 
 









                                                                        














                                                                                                         












                                                                                                   

                           

                         
                              
                          

                                                   



                                                                                               

                                         
                                                  





                                                       

                                                            

                



                                                                                                    
                                                    




                                                                              
                                                  



                                                                 

                                                  
                                                                                                                                                                                             








                                                                          

                                                                          












                                                       
                                                                                         

                              

 
       

                                  









                                                


                      










                                                                      
                                                                    

                      

                               

 






                                                    











                                                                                                                     


                                                                          



                                 






                                                
                             



                                           
                              



                                                  
//: Helper for various programming environments: run arbitrary mu code and
//: return some result in string form.

:(scenario run_interactive_code)
recipe main [
  2:address:array:character <- new [1:number <- copy 34:literal]
  run-interactive 2:address:array:character
]
+mem: storing 34 in location 1

:(scenario run_interactive_empty)
recipe main [
  1:address:array:character <- run-interactive 0:literal
]
# result is null
+mem: storing 0 in location 1

//: run code in 'interactive mode', i.e. with warnings off and return:
//:   stringified output in case we want to print it to screen
//:   any warnings encountered
//:   simulated screen any prints went to
:(before "End Primitive Recipe Declarations")
RUN_INTERACTIVE,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["run-interactive"] = RUN_INTERACTIVE;
//? cerr << "run-interactive: " << RUN_INTERACTIVE << '\n'; //? 1
:(before "End Primitive Recipe Implementations")
case RUN_INTERACTIVE: {
  assert(scalar(ingredients.at(0)));
  products.resize(3);
  bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(0));
  if (!new_code_pushed_to_stack) {
    products.at(0).push_back(0);
    products.at(1).push_back(warnings_from_trace());
    products.at(2).push_back(0);
    clean_up_interactive();
    break;  // done with this instruction
  }
  else {
    continue;  // not done with caller; don't increment current_step_index()
  }
}

:(before "End Globals")
bool Running_interactive = false;
long long int Old_screen = 0;  // we can support one iteration of screen inside screen
:(before "End Setup")
Running_interactive = false;
Old_screen = 0;
:(code)
// reads a string, tries to call it as code (treating it as a test), saving
// all warnings.
// returns true if successfully called (no errors found during load and transform)
bool run_interactive(long long int address) {
  if (Recipe_ordinal.find("interactive") == Recipe_ordinal.end())
    Recipe_ordinal["interactive"] = Next_recipe_ordinal++;
  Old_screen = Memory[SCREEN];
//?   cerr << "save screen: " << Old_screen << '\n'; //? 2
  // try to sandbox the run as best you can
  // todo: test this
  if (!Current_scenario) {
    // not already sandboxed
    for (long long int i = 1; i < Reserved_for_tests; ++i)
      Memory.erase(i);
    Name[Recipe_ordinal["interactive"]].clear();
  }
//?   cerr << "screen was at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1
  Name[Recipe_ordinal["interactive"]]["screen"] = SCREEN;
//?   cerr << "screen now at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1
  string command = trim(strip_comments(to_string(address)));
  if (command.empty()) return false;
  Recipe.erase(Recipe_ordinal["interactive"]);
  Hide_warnings = true;
  if (!Trace_stream) {
    Trace_file = "";  // if there wasn't already a stream we don't want to save it
    Trace_stream = new trace_stream;
    Trace_stream->collect_layer = "warn";
  }
  // call run(string) but without the scheduling
  // we won't create a local scope so that we can get to the new screen after
  // we return from 'interactive'.
  load(string("recipe interactive [\n") +
          "screen:address <- new-fake-screen 5, 5\n" +
          command + "\n" +
       "]\n");
  transform_all();
  if (trace_count("warn") > 0) return false;
  Running_interactive = true;
  Current_routine->calls.push_front(call(Recipe_ordinal["interactive"]));
  return true;
}

:(scenario "run_interactive_returns_stringified_result")
recipe main [
  # try to interactively add 2 and 2
  1:address:array:character <- new [add 2:literal, 2:literal]
  2:address:array:character <- run-interactive 1:address:array:character
  10:array:character <- copy 2:address:array:character/deref
]
# first letter in the output should be '4' in unicode
+mem: storing 52 in location 11

:(scenario "run_interactive_returns_string")
recipe main [
  # try to interactively add 2 and 2
  1:address:array:character <- new [
    100:address:array:character <- new [a]
    101:address:array:character <- new [b]
    102:address:array:character <- string-append 100:address:array:character, 101:address:array:character
  ]
  2:address:array:character <- run-interactive 1:address:array:character
  10:array:character <- copy 2:address:array:character/deref
]
# output contains "ab"
+mem: storing 97 in location 11
+mem: storing 98 in location 12

:(scenario "run_interactive_returns_warnings")
recipe main [
  # run a command that generates a warning
  1:address:array:character <- new [get 1234:number, foo:offset]
  2:address:array:character, 3:address:array:character <- run-interactive 1:address:array:character
  10:array:character <- copy 3:address:array:character/deref
]
# warning should be "unknown element foo in container number"
+mem: storing 117 in location 11
+mem: storing 110 in location 12
+mem: storing 107 in location 13
+mem: storing 110 in location 14

:(before "End Globals")
string Most_recent_results;
:(before "End Setup")
Most_recent_results = "";
:(before "End of Instruction")
if (Running_interactive) {
  record_products(current_instruction(), products);
}
:(code)
void record_products(const instruction& instruction, const vector<vector<double> >& products) {
  ostringstream out;
  for (long long int i = 0; i < SIZE(products); ++i) {
    // string
    if (i < SIZE(instruction.products)) {
      if (is_string(instruction.products.at(i))) {
        assert(scalar(products.at(i)));
        out << to_string(products.at(i).at(0)) << '\n';
        continue;
      }
      // End Record Product Special-cases
    }
    for (long long int j = 0; j < SIZE(products.at(i)); ++j)
      out << products.at(i).at(j) << ' ';
    out << '\n';
  }
  Most_recent_results = out.str();
}
:(before "Complete Call Fallthrough")
if (current_instruction().operation == RUN_INTERACTIVE && !current_instruction().products.empty()) {
  assert(SIZE(current_instruction().products) <= 3);
  // Send the results of the most recently executed instruction, regardless of
  // call depth, to be converted to string and potentially printed to string.
  vector<double> result;
  result.push_back(new_string(Most_recent_results));
  write_memory(current_instruction().products.at(0), result);
  if (SIZE(current_instruction().products) >= 2) {
    vector<double> warnings;
    warnings.push_back(warnings_from_trace());
    write_memory(current_instruction().products.at(1), warnings);
  }
  if (SIZE(current_instruction().products) >= 3) {
    vector<double> screen;
//?     cerr << "returning screen " << Memory[SCREEN] << " to " << current_instruction().products.at(2).to_string() << " value " << current_instruction().products.at(2).value << '\n'; //? 1
    screen.push_back(Memory[SCREEN]);
    write_memory(current_instruction().products.at(2), screen);
  }
}

//: clean up reply after we've popped it off the call-stack
//: however, we need what was on the stack to decide whether to clean up
:(after "Starting Reply")
bool must_clean_up_interactive = (current_recipe_name() == "interactive");
:(after "Falling Through End Of Recipe")
bool must_clean_up_interactive = (current_recipe_name() == "interactive");
:(before "End Reply")
if (must_clean_up_interactive) clean_up_interactive();
:(before "Complete Call Fallthrough")
if (must_clean_up_interactive) clean_up_interactive();
:(code)
void clean_up_interactive() {
  Hide_warnings = false;
  Running_interactive = false;
  // hack: assume collect_layer isn't set anywhere else
  if (Trace_stream->collect_layer == "warn") {
    delete Trace_stream;
    Trace_stream = NULL;
  }
//?   cerr << "restore screen: " << Memory[SCREEN] << " to " << Old_screen << '\n'; //? 1
  Memory[SCREEN] = Old_screen;
  Old_screen = 0;
}

:(code)
string strip_comments(string in) {
  ostringstream result;
  for (long long int i = 0; i < SIZE(in); ++i) {
    if (in.at(i) != '#') {
      result << in.at(i);
    }
    else {
      while (i < SIZE(in) && in.at(i) != '\n')
        ++i;
      if (i < SIZE(in) && in.at(i) == '\n') ++i;
    }
  }
  return result.str();
}

string to_string(long long int address) {
  long long int size = Memory[address];
  if (size == 0) return "";
  ostringstream tmp;
  for (long long int curr = address+1; curr <= address+size; ++curr) {
    // todo: unicode
    tmp << (char)(int)Memory[curr];
  }
  return tmp.str();
}

long long int stringified_value_of_location(long long int address) {
  // convert to string
  ostringstream out;
  out << Memory[address];
  return new_string(out.str());
}

bool is_string(const reagent& x) {
  return x.types.size() == 3
      && x.types.at(0) == Type_ordinal["address"]
      && x.types.at(1) == Type_ordinal["array"]
      && x.types.at(2) == Type_ordinal["character"];
}

long long int warnings_from_trace() {
  if (!Trace_stream) return 0;
  if (trace_count("warn") <= 0) return 0;
  ostringstream out;
  for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
    if (p->label != "warn") continue;
    out << p->contents;
    if (*--p->contents.end() != '\n') out << '\n';
  }
  assert(!out.str().empty());
  return new_string(out.str());
}

//: simpler version of run-interactive: doesn't do any running, just loads
//: recipes and reports warnings.
:(before "End Globals")
bool Loading_interactive = false;
:(before "End Setup")
Loading_interactive = false;
:(before "End Primitive Recipe Declarations")
RELOAD,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["reload"] = RELOAD;
:(before "End Primitive Recipe Implementations")
case RELOAD: {
  assert(scalar(ingredients.at(0)));
  Loading_interactive = true;
  Hide_warnings = true;
  load(to_string(ingredients.at(0).at(0)));
  transform_all();
  Hide_warnings = false;
  Loading_interactive = false;
  products.resize(1);
  products.at(0).push_back(warnings_from_trace());
  break;
}