fix 'ret' have not initialized
[mfgtools.git] / uuu / autocomplete.cpp
blob9ca497346fa5af92fc2d29bfbbb5818cbea47cc3
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include <iostream>
33 #include <stdio.h>
34 #include <thread>
35 #include <atomic>
36 #include <iomanip>
37 #include <map>
38 #include <mutex>
39 #include <vector>
40 #include <sstream>
41 #include <fstream>
42 #include <stdarg.h>
43 #include <time.h>
44 #include <string.h>
45 #include <signal.h>
46 #include "buildincmd.h"
48 #include "../libuuu/libuuu.h"
50 #ifndef _MSC_VER
51 #include <unistd.h>
52 #include <limits.h>
53 #else
54 #include <Windows.h>
55 #endif
57 using namespace std;
59 void linux_auto_arg(const char *space = " ", const char * filter = "")
61 string str = filter;
63 const char *param[] = { "-b", "-d", "-v", "-V", "-s", NULL };
64 int i = 0;
66 for (int i = 0; param[i]; i++)
68 if (str.find(param[i]) == string::npos)
69 cout << param[i] << space << endl;
73 int linux_autocomplete_ls(const char *path, void *p)
75 cout << path + 2 << endl;
76 return 0;
79 void linux_autocomplete(int argc, char **argv)
81 string last;
82 string cur;
84 if (argc == 3)
86 last = argv[2];
87 }else
89 cur = argv[2];
90 last = argv[3];
93 if (cur[0] == '-')
95 if (cur.size() == 1)
96 linux_auto_arg();
97 else
98 cout << cur << " " << endl;
100 return;
103 if (last.size()>=3)
105 if (last.substr(last.size() - 3) == "uuu" &&(cur.empty() || cur[0] == '-'))
107 linux_auto_arg();
108 cout << cur << endl;
111 }else if(last.empty())
113 linux_auto_arg();
115 else if (last == "-b")
117 return g_BuildScripts.PrintAutoComplete(cur);
119 }else if(last[0] == '-')
121 linux_auto_arg();
124 uuu_for_each_ls_file(linux_autocomplete_ls, cur.c_str(), NULL);
127 string get_next_word(string str, size_t &pos)
129 size_t start = 0;
130 start = str.find(' ', pos);
131 string sub = str.substr(pos, start - pos);
132 pos = start == string::npos ? start : start + 1;
133 return sub;
136 void power_shell_autocomplete(const char *p)
138 string pstr = p;
139 size_t pos = 0;
141 string file;
143 vector<string> argv; string params;
144 while (pos != string::npos && pos < pstr.size())
146 file = get_next_word(pstr, pos);
147 argv.push_back(file);
149 if (file.size() && file[0] == '-')
150 params += " " + file;
153 string last = argv[argv.size() - 1];
154 string prev = argv.size() > 1 ? argv[argv.size() - 2] : "";
155 if (last == "-b" || prev == "-b")
157 string cur;
158 if (prev == "-b")
159 cur = last;
161 if (g_BuildScripts.find(cur) == g_BuildScripts.end())
162 g_BuildScripts.PrintAutoComplete(cur, "");
164 last.clear();
166 else
168 if (last[0] == '-' || argv.size() == 1)
169 linux_auto_arg("", params.c_str());
172 if (argv.size() == 1)
173 last.clear();
175 uuu_for_each_ls_file(linux_autocomplete_ls, last.c_str(), NULL);
178 int auto_complete(int argc, char**argv)
180 if (argc == 4 || argc == 3)
182 string str = argv[1];
183 if (str.size() >= 3)
184 if (str.substr(str.size() - 3) == "uuu")
186 linux_autocomplete(argc, argv);
187 return 0;
191 if (argc >= 2)
193 string str = argv[1];
194 if (str == "-autocomplete")
196 power_shell_autocomplete(argc == 2 ? "" : argv[2]);
197 return 0;
201 return 1;
204 void print_autocomplete_help()
207 #ifndef _MSC_VER
209 cout << "Enjoy auto [tab] command complete by put below script into /etc/bash_completion.d/uuu" << endl;
210 cout << g_vt_kcyn;
211 cout << " _uuu_autocomplete()" <<endl;
212 cout << " {" << endl;
213 cout << " COMPREPLY=($(" << TARGET_PATH << " $1 $2 $3))" << endl;
214 cout << " }" << endl;
215 cout << " complete -o nospace -F _uuu_autocomplete uuu" << g_vt_default << endl << endl;
217 #else
219 printf("Powershell: Enjoy auto [tab] command complete by run below command or put into Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1\n");
221 HMODULE hModule = GetModuleHandleA(NULL);
222 char path[MAX_PATH];
223 GetModuleFileNameA(hModule, path, MAX_PATH);
225 printf(" Register-ArgumentCompleter -CommandName uuu -ScriptBlock {param($commandName,$parameterName,$wordToComplete,$commandAst,$fakeBoundParameter); %s -autocomplete $parameterName }\n\n",
226 path);
228 #endif