001: static class Program
002: {
003: /// <summary>
004: /// アプリケーションのメイン エントリ ポイントです。
005: /// </summary>
006: [STAThread]
007: static void Main(string[] args)
008: {
009: string version = "2.2016-08-02.1";
010:
011: // Read ini file
012: string _path = Application.ExecutablePath;
013: string _name = Path.GetFileNameWithoutExtension(_path);
014: string _dir = Path.GetDirectoryName(_path);
015: string _ahpfile = _dir + "\\" + _name + ".ahp";
016:
017: if (args.Length > 0 && args[0] == "/myarghelperversion")
018: {
019: // 引数として、/myarghelperversion が指定されました。
020: string asmblyname =
021: Assembly.GetExecutingAssembly().GetName().Name.ToString();
022:
023: MessageBox.Show(_name + ".exe is a copy of\n\n" +
024: asmblyname + ".exe " + version);
025: Application.Exit();
026: System.Environment.Exit(0);
027: }
028:
029: // 当然のことながら、args には、myarghelper.exe を複写
030: // してできた、たとえば、hoge.exe を実際に起動したとき
031: // の引数が格納されています。
032:
033: if (args.Length > 0 && args[0] == "//help")
034: {
035: // //help オプションが指定されました。このプログラ
036: // ムがどんな名前で複写されているかを考慮して、
037: // commentfile を決めて開きます。
038:
039: string commentfile = _dir + "\\" + _name + ".exe.txt";
040: if (File.Exists(commentfile))
041: {
042: MessageBox.Show(_name + ".exe.txt" + " is shown by the start command.");
043: System.Diagnostics.Process.Start(commentfile);
044: }
045: else
046: {
047: MessageBox.Show(commentfile + " does not exist.");
048: }
049: Application.Exit();
050: System.Environment.Exit(0);
051: }
052:
053: string computername =
054: System.Environment.GetEnvironmentVariable("COMPUTERNAME");
055: string ahpfileForThisMachine = _dir + "\\" + _name + "For" +
056: computername + ".ahp";
057:
058: if (System.IO.File.Exists(ahpfileForThisMachine) == true)
059: {
060: _ahpfile = ahpfileForThisMachine;
061: }
062:
063: if (System.IO.File.Exists(_ahpfile) == false)
064: {
065: MessageBox.Show(_name + ".exe の設定ファイル(拡張子 .ahp)が\n\n" +
066: _dir + "\n\nに在りません。" +
067: "\n\n" + _name + ".exe は myarghelper.exe から作成されました。",
068: _name + ".exe " + version);
069: Application.Exit();
070: System.Environment.Exit(0);
071: }
072:
073: string[] vlines = ReadValidLines(_ahpfile);
074: // ReadValidLines はコメント行を読み飛ばして、それ以外
075: // の行を配列の形で返します。
076:
077: if (vlines.Length < 1)
078: {
079: MessageBox.Show("ahpfile " +
080: _ahpfile + " is empty.", _name + ".exe " + version);
081: Application.Exit();
082: System.Environment.Exit(0);
083:
084: }
085:
086: string thisprog_arguments = "";
087:
088: Regex theRegCommandQuote = new Regex(@"^\""[^\""]+\""[ ]*(?<line>.*)$");
089: Match m1 = theRegCommandQuote.Match( Environment.CommandLine);
090:
091: if (m1.Success)
092: {
093: // MessageBox.Show(m1.Groups["line"].Value, "Debug");
094: thisprog_arguments = m1.Groups["line"].Value;
095: }
096: else
097: {
098: Regex theRegCommand = new Regex(@"^[^ ][^ ]*[ ]+(?<line>.*)$");
099: Match m2 = theRegCommand.Match(Environment.CommandLine);
100: if (m2.Success)
101: {
102: // MessageBox.Show(m2.Groups["line"].Value, "Debug");
103: thisprog_arguments = m2.Groups["line"].Value;
104: }
105: }
106: // ここの操作で thisprog_arguments に引数が格納されました。
107:
108: string currentDirectory = Directory.GetCurrentDirectory();
109: if (Regex.IsMatch(thisprog_arguments, @"^\.\\")) {
110: thisprog_arguments = Regex.Replace(thisprog_arguments,
111: @"^\.(.*)$", (currentDirectory + @"$1"));
112: }
113: else if (Regex.IsMatch(thisprog_arguments, @"^\""\.\\")) {
114: thisprog_arguments = Regex.Replace(thisprog_arguments,
115: @"^\""\.(.*)$", (@"\""" + currentDirectory + @"$1"));
116: }
117:
118: string filename = vlines[0];
119:
120: // [2016-08-02] 環境変数を利用できるようにしました。
121: // %XYZZYHOME% とかを vlines[0] の中に書くことができます。
122: Regex theEnvReg = new Regex(@"%(?<var>[^%]+)%");
123: bool checkenv = true;
124: while (checkenv)
125: {
126: Match mf = theEnvReg.Match(filename);
127: if (mf.Success)
128: {
129: string varname = mf.Result("${var}");
130: string value = System.Environment.GetEnvironmentVariable(varname);
131: if (value == null)
132: {
133: MessageBox.Show("ahpfile " +
134: _ahpfile + "\n\n\nEnvironmental variable " + varname +
135: " is not defined.",
136: _name,
137: MessageBoxButtons.OK, MessageBoxIcon.Error
138: );
139: Application.Exit();
140: System.Environment.Exit(0);
141: }
142: filename = filename.Replace("%" + varname + "%", value);
143: }
144: else
145: {
146: checkenv = false;
147: }
148: }
149:
150: filename = Regex.Replace(filename, @"^\.\\", _dir + "\\");
151: filename = Regex.Replace(filename, "\\\\", "\\");
152:
153: string ahpargument = "";
154: if (vlines.Length > 1) { ahpargument = vlines[1]; }
155:
156: string arguments = "";
157: bool replaceEnvironmenVariablesP = true;
158:
159: if (vlines.Length > 1)
160: {
161: if (vlines[1].Length > 0)
162: {
163: arguments = vlines[1];
164: if (Regex.IsMatch(arguments, @"^-norepenv")) {
165: replaceEnvironmenVariablesP = false;
166: arguments = Regex.Replace(arguments,
167: @"^-norepenv[ ]*(.*)$",@"$1");
168: }
169:
170: arguments = Regex.Replace(arguments, @"^\.\\", _dir + "\\");
171: arguments = Regex.Replace(arguments, @"\\\\", @"\");
172: //MessageBox.Show(arguments);
173:
174: if (Regex.IsMatch(arguments, @"\$\(Arguments\)"))
175: {
176: arguments = arguments.Replace("$(Arguments)", thisprog_arguments);
177:
178: }
179: else if (Regex.IsMatch(arguments, @"\$\(Argument1\)"))
180: {
181: string[] cargs = Environment.GetCommandLineArgs();
182: // cargs[0] は実行プログラムのパス、cargs[1]
183: // 以降に引数が格納されています。
184: int nargs = cargs.Length;
185: if (nargs > 1 && Regex.IsMatch(cargs[0], @"^\.\\"))
186: {
187: cargs[1] = Regex.Replace(cargs[1], @"^\.(.*)$",
188: (currentDirectory + @"$1"));
189: }
190:
191: int iarg = 1;
192: if (nargs > iarg)
193: {
194: arguments = arguments.Replace("$(Argument1)", cargs[iarg]);
195: iarg += 1;
196: }
197:
198: if (Regex.IsMatch(arguments, @"\$\(Argument2\)"))
199: {
200: if (nargs > iarg)
201: {
202: arguments = arguments.Replace("$(Argument2)", cargs[iarg]);
203: iarg += 1;
204: }
205:
206: if (Regex.IsMatch(arguments, @"\$\(Argument3\)"))
207: {
208: if (nargs > iarg)
209: {
210: arguments = arguments.Replace("$(Argument3)", cargs[iarg]);
211: iarg += 1;
212: }
213: }
214:
215: }
216:
217: if (nargs > iarg)
218: {
219: for (int i = iarg; i < nargs; i++)
220: {
221: if (cargs[i].Contains(" "))
222: {
223: arguments = arguments + " \"" + cargs[i] + "\"";
224: }
225: else
226: {
227: arguments = arguments + " " + cargs[i];
228: }
229: }
230: }
231: }
232: else
233: {
234: if (thisprog_arguments.Length > 0)
235: {
236: arguments = arguments + " " + thisprog_arguments;
237: }
238: //else
239: //{
240: // arguments = vlines[1];
241: //}
242: }
243: }
244: else
245: {
246: // ahpfile には引数が指定されていなかった。
247: arguments = thisprog_arguments;
248: }
249: }
250: else
251: {
252: arguments = thisprog_arguments;
253: }
254:
255: if (Regex.IsMatch(arguments, @"\$\(Argument.*\)"))
256: {
257: MessageBox.Show(_name + ".exe の設定ファイル " +
258: Path.GetFileName(_ahpfile) + " の記述に" +
259: "誤りがあります。\n\n" +
260: "vlines[1]: " + vlines[1] + "\n" +
261: "arguments: " + thisprog_arguments,
262: _name + ".exe " + version);
263: Application.Exit();
264: System.Environment.Exit(0);
265: }
266:
267: if (args.Length > 0 && args[0] == "/myarghelperinfo")
268: {
269: string messageformat = @"
270: ahpfile: {0}
271:
272: 実行プログラム名: {1}
273: 実プログラム引数: {2}
274: ";
275:
276: string asmblyname =
277: Assembly.GetExecutingAssembly().GetName().Name.ToString();
278:
279: MessageBox.Show(_name + ".exe is a copy of\n\n" +
280: asmblyname + ".exe " + version + "\n\n" +
281: String.Format(messageformat,
282: _ahpfile, filename, ahpargument)
283: , _name + ".exe");
284: Application.Exit();
285: System.Environment.Exit(0);
286: }
287:
288: // [2016-08-02] 環境変数を利用できるようにしました。
289: // %XYZZYHOME% とかを arguments の中に書くことができます。
290: // これが問題を引き起こすこともあります。
291: if (replaceEnvironmenVariablesP) {
292: checkenv = true;
293: while (checkenv)
294: {
295: Match ma = theEnvReg.Match(arguments);
296: if (ma.Success)
297: {
298: string varname = ma.Result("${var}");
299: string value = System.Environment.GetEnvironmentVariable(varname);
300:
301: if (value == null)
302: {
303: MessageBox.Show("ahpfile " +
304: _ahpfile + "\n\n\nEnvironmental variable " + varname +
305: " is not defined.", _name,
306: MessageBoxButtons.OK, MessageBoxIcon.Error
307: );
308: Application.Exit();
309: System.Environment.Exit(0);
310: }
311:
312: arguments = arguments.Replace("%" + varname + "%", value);
313: }
314: else
315: {
316: checkenv = false;
317: }
318: }
319: }
320:
321: // ここまでで、filename と arguments の作成を完了しています。
322:
323: // filename が start かそうでないかで動作を変えます。
324: string filename1 = filename.ToLower();
325: if (filename1 == "start" || filename1 == "start.exe")
326: {
327: System.Diagnostics.Process.Start(arguments);
328: }
329: else
330: {
331: // filename に格納されているファイルがあるかどうかチェックする
332: // 必要がないか。
333: if (System.IO.File.Exists(filename) == false)
334: {
335: MessageBox.Show("ahpfile " +
336: _ahpfile + "\n\n\n" + filename +
337: " dofes not exist.", _name,
338: MessageBoxButtons.OK,
339: MessageBoxIcon.Error
340: );
341: Application.Exit();
342: System.Environment.Exit(0);
343: }
344:
345: Process prcs = new Process();
346: prcs.StartInfo.FileName = filename;
347: prcs.StartInfo.Arguments = arguments;
348: prcs.StartInfo.UseShellExecute = true;
349: prcs.StartInfo.RedirectStandardOutput = false;
350: prcs.StartInfo.CreateNoWindow = false;
351: prcs.Start();
352: }
353:
354: // prcs.WaitForExit();
355:
356:
357: //Application.EnableVisualStyles();
358: //Application.SetCompatibleTextRenderingDefault(false);
359: //Application.Run(new Form1());
360: }
361:
362: static string[] ReadValidLines(string filename)
363: {
364: string[] lines = File.ReadAllLines(filename,
365: System.Text.Encoding.GetEncoding("shift_jis"));
366: List<string> vlines = new List<string>();
367: for (int i = 0; i < lines.Length; i++)
368: {
369: if (lines[i].Length > 0 && lines[i].Substring(0, 1) == "#")
370: continue;
371:
372: vlines.Add(lines[i]);
373: }
374: string[] nlines = new string[vlines.Count];
375: vlines.CopyTo(nlines, 0);
376: return nlines;
377: }
378:
379: static string cutshort(string path, int maxlen)
380: {
381: // Parent folder
382: string displayPath = path;
383: string parentDir = Path.GetDirectoryName(displayPath);
384: string lastDir = Path.GetFileName(path);
385:
386: Regex theEndWithYen = new Regex(@".*\\$");
387: Match theMatchEndWithYen = theEndWithYen.Match(parentDir);
388:
389: while (displayPath.Length > maxlen && theMatchEndWithYen.Success == false)
390: {
391: parentDir = Path.GetDirectoryName(parentDir);
392: displayPath = parentDir + "\\...\\" + lastDir;
393: theMatchEndWithYen = theEndWithYen.Match(parentDir);
394: }
395: return displayPath;
396: }