forked from man-hatter-done/Server-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal_command_handler_updates.patch
More file actions
106 lines (105 loc) · 4.73 KB
/
terminal_command_handler_updates.patch
File metadata and controls
106 lines (105 loc) · 4.73 KB
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
--- terminal_command_handler.py.orig2023-04-12 00:00:00.000000000 +0000
+++ terminal_command_handler.py 2023-04-12 00:10:00.000000000 +0000
@@ -420,6 +420,99 @@
return {"message": "Touch completed", "exit_code": 0}
return {"output": "", "exit_code": 0}
+ # grep - Search files for patterns
+ if command.startswith("grep "):
+ parts = command.split(" ", 1)
+ if len(parts) < 2:
+ return {"error": "Usage: grep [OPTIONS] PATTERN [FILE...]", "exit_code": 1}
+
+ # This is a simplified implementation - a real solution would parse all grep options
+ args = parts[1].strip()
+
+ # Process all arguments
+ pattern = None
+ files = []
+ is_pattern = True
+
+ # Very simple arg parser - this doesn't handle all grep options
+ for arg in args.split():
+ if arg.startswith('-'):
+ # This is an option, skip it for now
+ continue
+ elif is_pattern and pattern is None:
+ pattern = arg
+ is_pattern = False
+ else:
+ files.append(arg)
+
+ if not pattern:
+ return {"error": "No pattern specified", "exit_code": 1}
+
+ if not files:
+ # No files specified, let the shell handle it (might be piped input)
+ return None
+
+ # Process each file
+ results = []
+ found_matches = False
+
+ for file_path in files:
+ # Handle ~ in paths
+ if file_path.startswith("~"):
+ file_path = file_path.replace("~", home_dir, 1)
+ elif not file_path.startswith("/"):
+ # Relative path
+ file_path = os.path.join(home_dir, file_path)
+
+ # Verify the path is within the home directory for security
+ file_path = os.path.normpath(file_path)
+ if not file_path.startswith(home_dir):
+ results.append(f"grep: {file_path}: Access denied. Path outside of user directory.")
+ continue
+
+ try:
+ if not os.path.exists(file_path):
+ results.append(f"grep: {file_path}: No such file or directory")
+ continue
+
+ if os.path.isdir(file_path):
+ results.append(f"grep: {file_path}: Is a directory")
+ continue
+
+ # Read the file and grep for the pattern
+ with open(file_path, 'r', errors='replace') as f:
+ for i, line in enumerate(f):
+ if pattern in line:
+ found_matches = True
+ # If multiple files, prefix output with filename
+ if len(files) > 1:
+ results.append(f"{os.path.basename(file_path)}:{line.rstrip()}")
+ else:
+ results.append(line.rstrip())
+ except Exception as e:
+ results.append(f"grep: {file_path}: {str(e)}")
+
+ # Return the results
+ if results:
+ result_text = "\n".join(results)
+ exit_code = 0 if found_matches else 1
+
+ if callback:
+ callback(result_text + "\n")
+ callback("\n", exit_code=exit_code)
+ return {"message": "Grep completed", "exit_code": exit_code}
+ return {"output": result_text, "exit_code": exit_code}
+ else:
+ # No matches found
+ exit_code = 1
+ if callback:
+ callback("\n", exit_code=exit_code)
+ return {"message": "No matches found", "exit_code": exit_code}
+ return {"output": "", "exit_code": exit_code}
+
+ # find - Search for files in a directory hierarchy
+ if command.startswith("find "):
+ # For find command, we'll let the shell handle it as it's more complex
+ # Just performing a security check on the path
+ parts = command.split(" ", 2)
+ if len(parts) >= 2:
+ path = parts[1].strip()
+
# If not a built-in file operation, return None to execute as a normal shell command
return None