00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifdef HAVE_CONFIG_H
00042 # include <config.h>
00043 #endif
00044
00045
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 #include <gtk/gtk.h>
00050
00051 #include "interface.h"
00052 #include "support.h"
00053 #include "main.h"
00054 #include "comboBox.h"
00055 #include "kbhit.h"
00056 #include "commonDefs.h"
00057 #include "debug.h"
00058 #include "comboHandlers.h"
00059 #include "help.h"
00060
00061 #include <getopt.h>
00062
00063
00064
00065
00066
00067 extern char *optarg;
00068
00069 extern int optind, opterr, optopt;
00070
00071 extern char **environ;
00072
00073
00074
00075
00076
00077 int
00078 main (int argc, char *argv[], char *env[])
00079 {
00080 gboolean gui =TRUE, history_set =FALSE, listops = FALSE;
00081
00082
00083 int opt =0;
00084
00085
00086
00087
00088 history_file = NULL;
00089 command = NULL;
00090 terminal_path = NULL;
00091 die = TRUE;
00092 run_in_term = FALSE;
00093 no_kbhit = FALSE;
00094 combo_mode = RF_HISTORY_MODE;
00095 keep_open = FALSE;
00096
00097
00098 gtk_set_locale ();
00099 gtk_init (&argc, &argv);
00100
00101 char *home_directory = getenv("HOME");
00102
00103
00104 dprint("run-free: debug mode enabled");
00105
00106
00107 while(1)
00108 {
00109 int option_index = 0;
00110 static struct option long_options[] = {
00111 {"bash", 0, 0, 'b'},
00112 {"command", 1, 0, 'c'},
00113 {"die", 0, 0, 'd'},
00114 {"help", 0, 0, 'h'},
00115 {"histlen", 1, 0, 0},
00116 {"history", 1, 0, 0},
00117 {"kbhit", 0, 0, 'k'},
00118 {"nokbhit", 0, 0, 'i'},
00119 {"keystrokes", 0, 0, 'y'},
00120 {"listops", 0, 0, 'l'},
00121 {"termstay", 0, 0, 'o'},
00122 {"runterm", 0, 0, 'r'},
00123 {"terminal", 1, 0, 't'},
00124 {"version", 0, 0, 'V'},
00125
00126 {0, 0, 0, 0}
00127 };
00128
00129
00130
00131
00132 opt = getopt_long(argc, argv, "bc:dhiklort:yV", long_options, &option_index);
00133
00134 if(opt == -1)
00135 break;
00136
00137 switch(opt)
00138 {
00139 case 0:
00140 if(strcmp(long_options[option_index].name, OPTARG_HISTORY) ==0)
00141 {
00142
00143 if(strcmp(optarg, OPTARG_HISTORY_FILETYPE_RF)==0)
00144 {
00145 history_file = g_malloc(strlen(home_directory)+strlen(RF_HISTORY_FILENAME)+2);
00146 sprintf(history_file, "%s/%s", home_directory, RF_HISTORY_FILENAME);
00147 }
00148 else if(strcmp(optarg, OPTARG_HISTORY_FILETYPE_BASH)==0)
00149 {
00150 history_file = g_malloc(strlen(home_directory)+strlen(BASH_HISTORY_FILENAME)+2);
00151 sprintf(history_file, "%s/%s", home_directory, BASH_HISTORY_FILENAME);
00152 }
00153 else
00154 {
00155 history_file = g_malloc(strlen(optarg)+1);
00156 sprintf(history_file,"%s", optarg);
00157 }
00158 history_set++;
00159 }
00160 else if(strcmp(long_options[option_index].name, OPTARG_HISTORY_LEN) ==0)
00161 {
00162 history_len = atol(optarg);
00163 }
00164 break;
00165 case 'b':
00166 bash_shell_mode = TRUE;
00167 break;
00168 case 'c':
00169
00170 if(optarg)
00171 {
00172 command = g_malloc(strlen(optarg)+1);
00173 sprintf(command, "%s", optarg);
00174 }
00175 break;
00176 case 'd':
00177 die = FALSE;
00178 break;
00179 case 'h':
00180 g_print("%s: version %s.\n%s\n", PACKAGE, VERSION, COPYRIGHT);
00181 g_print("%s\n\n", LICENSE);
00182 g_print("\nHELP:\n");
00183 char *help =
00184 "-b, --bash\t\tRun in Bash mode (reads ~/.bashrc before execution)"
00185 "\n"
00186 "-c, --command=COMMAND\tSet COMMAND in entry box on startup"
00187 "\n"
00188 "-d, --die\t\tDo NOT die after executing a command"
00189 "\n"
00190 "-h, --help\t\tThis help list"
00191 "\n"
00192 " --histlen=NUM\tShow NUM items from history in history list"
00193 "\n"
00194 " --history=FILEPATH\tUse history file from FILEPATH"
00195 "\n"
00196 "-i, --nokbhit\t\tDo NOT allow keyboard hit mode (DON'T show \"HIT ANY KEY\" on exit)"
00197 "\n"
00198 "-k, --kbhit\t\tRun in keyboard hit mode (show \"HIT ANY KEY\" and exit)"
00199 "\n"
00200 "-l, --listops\t\tList default (compiled in) options and exit"
00201 "\n"
00202 "-o, --termstay\t\tKeep the terminal open after command exec"
00203 "\n"
00204 "-r, --runterm\t\tSet run_in_term mode on startup"
00205 "\n"
00206 "-t, --terminal=TERMPATH\tUse TERMPATH program for run_in_terminal mode"
00207 "\n"
00208 "-y, --keystrokes\tDisplay the keystrokes for run-free"
00209 "\n"
00210 "-V, --version\t\tShow version and copyright information"
00211 "\n";
00212 g_print("%s", help);
00213 exit(0);
00214 break;
00215 case 'i':
00216 no_kbhit = TRUE;
00217 break;
00218 case 'k':
00219 gui = FALSE;
00220 break;
00221 case 'l':
00222 listops = TRUE;
00223 break;
00224 case 'o':
00225 keep_open = TRUE;
00226 break;
00227 case 'r':
00228 run_in_term = TRUE;
00229 break;
00230 case 't':
00231 if(optarg)
00232 {
00233 terminal_path = g_malloc(strlen(optarg)+1);
00234 sprintf(terminal_path, "%s", optarg);
00235 }
00236 break;
00237 case 'y':
00238 g_print("%s: version %s.\n%s\n", PACKAGE, VERSION, COPYRIGHT);
00239 g_print("%s\n\n", LICENSE);
00240 g_print("\nKEYSTROKES:\n");
00241 g_print("%s\n",KEYSTROKES);
00242
00243
00244
00245
00246 freeList(cbitems);
00247 g_free(history_file);
00248 g_free(home_directory);
00249 g_free(command);
00250 g_free(terminal_path);
00251 exit(0);
00252 break;
00253 case 'V':
00254 g_print("%s: version %s.\n%s\n", PACKAGE, VERSION, COPYRIGHT);
00255 g_print("%s\n\n", LICENSE);
00256 exit(0);
00257 break;
00258 default:
00259 dprint("an argument is not being delt with");
00260 break;
00261 }
00262 }
00263
00264
00265
00266 if(!history_set)
00267 {
00268
00269 #ifdef USE_USER_HISTORY_FILENAME
00270 dprint_s2("user history compile option",USER_HISTORY_FILENAME);
00271 history_file = g_malloc(strlen(USER_HISTORY_FILENAME)+1);
00272 sprintf(history_file, "%s", USER_HISTORY_FILENAME);
00273 #else
00274 dprint_s2("bash history compile option",BASH_HISTORY_FILENAME);
00275 history_file = g_malloc(strlen(home_directory)+strlen(BASH_HISTORY_FILENAME)+2);
00276 sprintf(history_file, "%s/%s", home_directory, BASH_HISTORY_FILENAME);
00277 #endif
00278 }
00279
00280 if(!history_len)
00281 history_len = DEFAULT_HISTORY_LEN;
00282
00283 if(terminal_path == NULL)
00284 {
00285 terminal_path = g_malloc(strlen(DEFAULT_TERMINAL_PATH)+1);
00286 sprintf(terminal_path, "%s", DEFAULT_TERMINAL_PATH);
00287 }
00288
00289 if(listops)
00290 {
00291 g_print("%s: version %s.\n%s\n", PACKAGE, VERSION, COPYRIGHT);
00292 g_print("%s\n\n", LICENSE);
00293 g_print("\n");
00294
00295 #ifdef USE_USER_HISTORY_FILENAME
00296 char *use_user_history_filename = "yes";
00297 #else
00298 char *use_user_history_filename = "no";
00299 #endif
00300
00301 g_print("debug mode \t\t\t\t=%s\n", DEBUG_MODE?"yes":"no");
00302 g_print("default history display length\t\t=%d\n", history_len);
00303 g_print("use user history file by default\t=%s\n",use_user_history_filename);
00304 g_print("name of default user history file: %s\n", USER_HISTORY_FILENAME);
00305 g_print("name of default bash history file:%s/%s\n", home_directory, BASH_HISTORY_FILENAME);
00306 g_print("run_in_term mode terminal path:%s\n", terminal_path);
00307 exit(0);
00308 }
00309
00310
00311
00312 if(!gui)
00313 {
00314 do_kbhit();
00315
00316
00317 freeList(cbitems);
00318 g_free(history_file);
00319 g_free(home_directory);
00320 g_free(command);
00321 g_free(terminal_path);
00322 }
00323 else
00324 {
00325
00326
00327
00328
00329
00330 mainWindow = create_mainWindow ();
00331 gtk_widget_show (mainWindow);
00332
00333
00334 fileselection = create_fileselection ();
00335
00336
00337 if(run_in_term)
00338 {
00339 run_in_term = FALSE;
00340 gtk_button_clicked(GTK_BUTTON(checkbutton));
00341 }
00342
00343
00344
00345 combo_init(RF_EXAMPLE_MODE);
00346 combo_init(RF_HISTORY_MODE);
00347
00348 if(command !=NULL)
00349 {
00350
00351 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo1)->entry), command);
00352 dprint_s2("startup command", command);
00353 }
00354
00355
00356 gtk_main ();
00357 }
00358
00359 return 0;
00360 }
00361
00362
00363 #include <sys/types.h>
00364 #include <unistd.h>
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 void run_command(char *command)
00384 {
00385
00386 char pre[64], post[64], *buffer;
00387
00388
00389 if(!command)
00390 return;
00391
00392
00393 if(run_in_term)
00394 {
00395
00396 if(bash_shell_mode == TRUE)
00397 {
00398
00399 strcpy(pre," -e bash -i -c \'");
00400 }
00401 else
00402 {
00403 strcpy(pre," -e sh -c \'");
00404 }
00405
00406 if(!no_kbhit)
00407 {
00408
00409 if(keep_open)
00410 if(bash_shell_mode == TRUE)
00411 strcpy(post, "; bash -i \'&");
00412 else
00413 strcpy(post, "; sh \'&");
00414 else
00415 strcpy(post, "; run-free -k \'&");
00416 }
00417 else
00418 if(keep_open)
00419 if(bash_shell_mode == TRUE)
00420 strcpy(post, "; bash -i \'&");
00421 else
00422 strcpy(post, "; sh \'&");
00423 else
00424 strcpy(post, "; \'&");
00425
00426
00427
00428 buffer = g_malloc(strlen(terminal_path)
00429 +strlen(pre)
00430 +strlen(command)
00431 +strlen(post)
00432 +1);
00433
00434
00435 sprintf(buffer, "%s%s%s%s", terminal_path, pre, command, post);
00436 }
00437 else
00438 {
00439
00440
00441 strcpy(pre, "");
00442 strcpy(post, " &");
00443
00444
00445 buffer = g_malloc( +strlen(pre)
00446 +strlen(command)
00447 +strlen(post)
00448 +1);
00449
00450
00451 sprintf(buffer, "%s%s%s", pre, command, post);
00452 }
00453
00454
00455
00456
00457 int pid = fork();
00458
00459
00460 if(pid == 0)
00461 {
00462
00463 writeHistory(command, history_file);
00464
00465
00466 char * argv[4];
00467 argv[0] = "sh";
00468 argv[1] = "-c";
00469 argv[2] = (char *)buffer;
00470 argv[3] = 0;
00471
00472
00473
00474
00475
00476
00477
00478
00479 execve("/bin/sh", argv, environ);
00480
00481
00482 g_free(buffer);
00483
00484
00485 exit(0);
00486 }
00487 else
00488 {
00489 g_free(buffer);
00490
00491 if(die)
00492 {
00493
00494 gtk_main_quit();
00495 }
00496 else
00497 {
00498
00499 combo_init(combo_mode);
00500 }
00501 }
00502 }
00503