about summary refs log blame commit diff stats
path: root/037global_variables.cc
blob: 2cd67421253437c2cfc6a2a863a4fd73eaa7d0be (plain) (tree)
Ranger v.1.1.0
==============

Ranger is a free console file manager that gives you greater flexibility
and a good overview of your files without having to leave your *nix console.
It visualizes the directory tree in two dimensions: the directory hierarchy
on one, lists of files on the other, with a preview to the right so you know
where you'll be going.  The default keys are similar to those of Vim, Emacs
and Midnight Commander, though Ranger is easily controllable with just the
arrow keys or the mouse.

The program is written in Python (2.6 or 3.1) and uses curses for the
text-based user interface.


About
-----

* Author:          Roman Zimbelmann  <romanz@lavabit.com>
* Website:         http://savannah.nongnu.org/projects/ranger
* License:         GNU General Public License Version 3
* Version:         1.1.0

* Download URL of the newest stable version:
http://git.savannah.gnu.org/cgit/ranger.git/snapshot/ranger-stable.tar.gz

* Git Clone URL:
git clone http://git.sv.gnu.org/r/ranger.git


Features
--------

* Multi-column display (Miller Columns)
* Preview of the selected file/directory
* Common file operations (create/chmod/copy/delete/...)
* VIM-like console and hotkeys
* Automatically determine file types and run them with correct programs
* Change the directory of your shell after exiting ranger
* Tabs, Bookmarks, Mouse support


Dependencies
------------

* A *nix-like operating system
* Python 2.6 or Python 3.1 with the curses module

Optional:
* The "file" program
* A pager ("less" by default)


Getting Started
---------------

Ranger can be started without installing.  Just run the executable (in
a terminal.)  The switch "--clean" will prevent it from creating or
accessing configuration files.

Follow the instructions in the INSTALL file for installing ranger.

After starting ranger, you should see 4 columns. The third one is the main
column, the directory where you're currently at.  To the left you see the
parent directories and to the right there's a preview of the object you're
pointing at.  Now use the Arrow Keys to navigate, Enter to open a file
or type Q to quit.

To customize ranger, copy the files from ranger/defaults/ to ~/.ranger/
and modify them according to your wishes.


Troubleshooting, Getting Help
-----------------------------

If you encounter an error, try running ranger with --debug.  This will
sometimes display more detailed information about the error.  Also, try
deactivating optimization:

PYTHONOPTIMIZE="" ranger --debug

Report bugs on savannah:  (please include as much information as possible)
http://savannah.nongnu.org/bugs/?func=additem&group=ranger

Ask questions on the mailing list:
http://lists.nongnu.org/mailman/listinfo/ranger-users


Further Reading
---------------

Check the man page for information on common features and hotkeys.

The most detailed manual is accessible by pressing "?" from inside ranger.
It is also available at ranger/help/, contained in the *.py files.

The file ranger/defaults/keys.py contains all key combinations, so that's
another place you may want to check out.
d3f1 ^
a6517ed8 ^








83c67014 ^



a6517ed8 ^



83c67014 ^






a6061b9f ^


a6517ed8 ^




f75c7021 ^
fb7e66fd ^

a6517ed8 ^
f75c7021 ^
a6517ed8 ^
f75c7021 ^




a6517ed8 ^





a6061b9f ^






9b16f190 ^
















a6061b9f ^
9b16f190 ^

a6061b9f ^









f75c7021 ^








5c368edc ^
f75c7021 ^
cac416f4 ^
f75c7021 ^

83c67014 ^
cac416f4 ^
f75c7021 ^
cac416f4 ^
f75c7021 ^



cac416f4 ^



f75c7021 ^












4a943d4e ^


83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^








a6517ed8 ^
4a943d4e ^


83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^








cac416f4 ^
4a943d4e ^

83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^






83c67014 ^



4a943d4e ^


a6061b9f ^
4a943d4e ^

83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^




83c67014 ^



4a943d4e ^


9b16f190 ^
4a943d4e ^


83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^







fb7cd3f1 ^
4a943d4e ^

83c67014 ^
4a943d4e ^
83c67014 ^
4a943d4e ^




f75c7021 ^
4a943d4e ^

83c67014 ^
4a943d4e ^





f75c7021 ^
f75c7021 ^









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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305





                                                                             
                                                               
 


                             
                     
                     
                        



                       
                                                                 

    
 
                          


                                              
                                                                  
                                   







                                                                                               



                                                                        








                                                                                               
                                         







                                                                                                                         

                                                                                       



                                                                      








                                                                                                                                 



                                                



                                                                                                






                                                              


                                                                                                    




                                                  
                                              

                                                                                  
                                       
                 
       




                                                                                                                





                                                                                                       






                                                                                                    
















                                                                          
                                                                                  

                                      









                                                                                                        








                                                                 
                           
                                               
       

                                            
                                   
                                                
                                  
                                                  



                                               



     












                                                                         


                                                
                     
                          
                        








                                                                      
 


                                                
                     
                           
                        








                                                                       
 

                                             
                     
                     
                        






                                                     



                                


                                
 

                                                   
                     
                     
                        




                                                   



                                


                                
 


                                       
                     
                          
                        







                                     
 

                                               
                     
                                                                  
                        




                                
 

                                              
                     





                                
 









                                                             
//: Global variables.
//:
//: Global variables are just labels in the data segment.
//: However, they can only be used in imm32 and not disp32 operands. And they
//: can't be used with jump and call instructions.
//:
//: This layer has much the same structure as rewriting labels.

:(code)
void test_global_variable() {
  run(
      "== code 0x1\n"
      "b9  x/imm32\n"
      "== data 0x2000\n"
      "x:\n"
      "  00 00 00 00\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: global variable 'x' is at address 0x00002000\n"
  );
}

:(before "End Transforms")
Transform.push_back(rewrite_global_variables);
:(code)
void rewrite_global_variables(program& p) {
  trace(3, "transform") << "-- rewrite global variables" << end();
  // Begin rewrite_global_variables
  map<string, uint32_t> address;
  compute_addresses_for_global_variables(p, address);
  if (trace_contains_errors()) return;
  drop_global_variables(p);
  replace_global_variables_with_addresses(p, address);
}

void compute_addresses_for_global_variables(const program& p, map<string, uint32_t>& address) {
  for (int i = 0;  i < SIZE(p.segments);  ++i) {
    if (p.segments.at(i).name != "code")
      compute_addresses_for_global_variables(p.segments.at(i), address);
  }
}

void compute_addresses_for_global_variables(const segment& s, map<string, uint32_t>& address) {
  int current_address = s.start;
  for (int i = 0;  i < SIZE(s.lines);  ++i) {
    const line& inst = s.lines.at(i);
    for (int j = 0;  j < SIZE(inst.words);  ++j) {
      const word& curr = inst.words.at(j);
      if (*curr.data.rbegin() != ':') {
        current_address += size_of(curr);
      }
      else {
        string variable = drop_last(curr.data);
        // ensure variables look sufficiently different from raw hex
        check_valid_name(variable);
        if (trace_contains_errors()) return;
        if (j > 0)
          raise << "'" << to_string(inst) << "': global variable names can only be the first word in a line.\n" << end();
        if (Labels_file.is_open())
          Labels_file << "0x" << HEXWORD << current_address << ' ' << variable << '\n';
        if (contains_key(address, variable)) {
          raise << "duplicate global '" << variable << "'\n" << end();
          return;
        }
        put(address, variable, current_address);
        trace(99, "transform") << "global variable '" << variable << "' is at address 0x" << HEXWORD << current_address << end();
        // no modifying current_address; global variable definitions won't be in the final binary
      }
    }
  }
}

void drop_global_variables(program& p) {
  for (int i = 0;  i < SIZE(p.segments);  ++i) {
    if (p.segments.at(i).name != "code")
      drop_labels(p.segments.at(i));
  }
}

void replace_global_variables_with_addresses(program& p, const map<string, uint32_t>& address) {
  if (p.segments.empty()) return;
  for (int i = 0;  i < SIZE(p.segments);  ++i) {
    segment& curr = p.segments.at(i);
    if (curr.name == "code")
      replace_global_variables_in_code_segment(curr, address);
    else
      replace_global_variables_in_data_segment(curr, address);
  }
}

void replace_global_variables_in_code_segment(segment& code, const map<string, uint32_t>& address) {
  for (int i = 0;  i < SIZE(code.lines);  ++i) {
    line& inst = code.lines.at(i);
    line new_inst;
    for (int j = 0;  j < SIZE(inst.words);  ++j) {
      const word& curr = inst.words.at(j);
      if (!contains_key(address, curr.data)) {
        if (!looks_like_hex_int(curr.data))
          raise << "missing reference to global '" << curr.data << "'\n" << end();
        new_inst.words.push_back(curr);
        continue;
      }
      if (!valid_use_of_global_variable(curr)) {
        raise << "'" << to_string(inst) << "': can't refer to global variable '" << curr.data << "'\n" << end();
        return;
      }
      emit_hex_bytes(new_inst, get(address, curr.data), 4);
    }
    inst.words.swap(new_inst.words);
    trace(99, "transform") << "instruction after transform: '" << data_to_string(inst) << "'" << end();
  }
}

void replace_global_variables_in_data_segment(segment& data, const map<string, uint32_t>& address) {
  for (int i = 0;  i < SIZE(data.lines);  ++i) {
    line& l = data.lines.at(i);
    line new_l;
    for (int j = 0;  j < SIZE(l.words);  ++j) {
      const word& curr = l.words.at(j);
      if (!contains_key(address, curr.data)) {
        if (looks_like_hex_int(curr.data)) {
          if (has_operand_metadata(curr, "imm32"))
            emit_hex_bytes(new_l, curr, 4);
          else if (has_operand_metadata(curr, "imm16"))
            emit_hex_bytes(new_l, curr, 2);
          else if (has_operand_metadata(curr, "imm8"))
            emit_hex_bytes(new_l, curr, 1);
          else if (has_operand_metadata(curr, "disp8"))
            raise << "can't use /disp8 in a non-code segment\n" << end();
          else if (has_operand_metadata(curr, "disp16"))
            raise << "can't use /disp16 in a non-code segment\n" << end();
          else if (has_operand_metadata(curr, "disp32"))
            raise << "can't use /disp32 in a non-code segment\n" << end();
          else
            new_l.words.push_back(curr);
        }
        else {
          raise << "missing reference to global '" << curr.data << "'\n" << end();
          new_l.words.push_back(curr);
        }
        continue;
      }
      trace(99, "transform") << curr.data << " maps to " << HEXWORD << get(address, curr.data) << end();
      emit_hex_bytes(new_l, get(address, curr.data), 4);
    }
    l.words.swap(new_l.words);
    trace(99, "transform") << "after transform: '" << data_to_string(l) << "'" << end();
  }
}

bool valid_use_of_global_variable(const word& curr) {
  if (has_operand_metadata(curr, "imm32")) return true;
  // End Valid Uses Of Global Variable(curr)
  return false;
}

//:: a more complex sanity check for how we use global variables
//: requires first saving some data early before we pack operands

:(after "Begin Transforms")
Transform.push_back(correlate_disp32_with_mod);
:(code)
void correlate_disp32_with_mod(program& p) {
  if (p.segments.empty()) return;
  segment& code = *find(p, "code");
  for (int i = 0;  i < SIZE(code.lines);  ++i) {
    line& inst = code.lines.at(i);
    for (int j = 0;  j < SIZE(inst.words);  ++j) {
      word& curr = inst.words.at(j);
      if (has_operand_metadata(curr, "disp32")
          && has_operand_metadata(inst, "mod"))
        curr.metadata.push_back("has_mod");
    }
  }
}

:(before "End Valid Uses Of Global Variable(curr)")
if (has_operand_metadata(curr, "disp32"))
  return has_metadata(curr, "has_mod");
// todo: more sophisticated check, to ensure we don't use global variable
// addresses as a real displacement added to other operands.

:(code)
bool has_metadata(const word& w, const string& m) {
  for (int i = 0;  i < SIZE(w.metadata);  ++i)
    if (w.metadata.at(i) == m) return true;
  return false;
}

void test_global_variable_disallowed_in_jump() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "eb/jump  x/disp8\n"
      "== data 0x2000\n"
      "x:\n"
      "  00 00 00 00\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: 'eb/jump x/disp8': can't refer to global variable 'x'\n"
      // sub-optimal error message; should be
//?       "error: can't jump to data (variable 'x')\n"
  );
}

void test_global_variable_disallowed_in_call() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "e8/call  x/disp32\n"
      "== data 0x2000\n"
      "x:\n"
      "  00 00 00 00\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: 'e8/call x/disp32': can't refer to global variable 'x'\n"
      // sub-optimal error message; should be
//?       "error: can't call to the data segment ('x')\n"
  );
}

void test_global_variable_in_data_segment() {
  run(
      "== code 0x1\n"
      "b9  x/imm32\n"
      "== data 0x2000\n"
      "x:\n"
      "  y/imm32\n"
      "y:\n"
      "  00 00 00 00\n"
  );
  // check that we loaded 'x' with the address of 'y'
  CHECK_TRACE_CONTENTS(
      "load: 0x00002000 -> 04\n"
      "load: 0x00002001 -> 20\n"
      "load: 0x00002002 -> 00\n"
      "load: 0x00002003 -> 00\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_raw_number_with_imm32_in_data_segment() {
  run(
      "== code 0x1\n"
      "b9  x/imm32\n"
      "== data 0x2000\n"
      "x:\n"
      "  1/imm32\n"
  );
  // check that we loaded 'x' with the address of 1
  CHECK_TRACE_CONTENTS(
      "load: 0x00002000 -> 01\n"
      "load: 0x00002001 -> 00\n"
      "load: 0x00002002 -> 00\n"
      "load: 0x00002003 -> 00\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_duplicate_global_variable() {
  Hide_errors = true;
  run(
      "== code 0x1\n"
      "40/increment-EAX\n"
      "== data 0x2000\n"
      "x:\n"
      "x:\n"
      "  00\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: duplicate global 'x'\n"
  );
}

void test_global_variable_disp32_with_modrm() {
  run(
      "== code 0x1\n"
      "8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX x/disp32\n"
      "== data 0x2000\n"
      "x:\n"
      "  00 00 00 00\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_global_variable_disp32_with_call() {
  transform(
      "== code 0x1\n"
      "foo:\n"
      "  e8/call bar/disp32\n"
      "bar:\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

string to_full_string(const line& in) {
  ostringstream out;
  for (int i = 0;  i < SIZE(in.words);  ++i) {
    if (i > 0) out << ' ';
    out << in.words.at(i).data;
    for (int j = 0;  j < SIZE(in.words.at(i).metadata);  ++j)
      out << '/' << in.words.at(i).metadata.at(j);
  }
  return out.str();
}