int built_ins(char *in[]){ if( (strcmp(in[0], "reparse-rc") == 0)){ if(!read_rc){ fprintf(stderr,"rc file could not be read\n"); return 1; } else parse_rc(); return 1; } if(strncmp(in[0], "#", 1) == 0) return 1; if(strcmp(in[0], "exit") == 0) exit_clean(0, NULL); if(strncmp(in[0], "cd", 2) == 0 && in[1] == NULL && restricted == 0) { if(chdir(env->home) == -1){ perror("cd"); return 1; } else return 1; } if(strcmp(in[0], "cd") == 0 && in[1] != NULL && restricted == 0) { if(chdir(in[1]) == -1) { perror("cd"); return 1; } else return 1; } else return 0; }
/* alias and password should be set already in url */ static void create_the_bookmark(url_t *url) { listitem *li; /* we don't want to be asked again */ url_setalias(ftp->url, url->alias); if(strcmp(ftp->curdir, ftp->homedir) != 0) url_setdirectory(url, ftp->curdir); else url_setdirectory(url, 0); list_clear(gvBookmarks); { char *tmp = NULL; if (asprintf(&tmp, "%s/bookmarks", gvWorkingDirectory) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); return; } parse_rc(tmp, false); free(tmp); } li = list_search(gvBookmarks, (listsearchfunc)urlcmp, url); if(li) list_delitem(gvBookmarks, li); list_additem(gvBookmarks, url); bookmark_save(0); }
/* Process the value of the environment variable CFLOW_OPTIONS * and of the rc file. * Split the value into words and add them between (*ARGV_PTR)[0] and * (*ARGV_PTR[1]) modifying *ARGC_PTR accordingly. * NOTE: Since sourcerc() is not meant to take all SH command line processing * burden, only word splitting is performed and no kind of expansion * takes place. */ void sourcerc(int *argc_ptr, char ***argv_ptr) { char *env; int xargc = 1; char **xargv; xargv = xmalloc(2*sizeof *xargv); xargv[0] = **argv_ptr; xargv[1] = NULL; env = getenv("CFLOW_OPTIONS"); if (env) { int argc; char **argv; argcv_get(env, "", "#", &argc, &argv); expand_argcv(&xargc, &xargv, argc, argv); free(argv); } env = getenv("CFLOWRC"); if (env) parse_rc(&xargc, &xargv, env); else { char *home = getenv("HOME"); if (home) { int len = strlen(home); char *buf = malloc(len + sizeof(LOCAL_RC) + (home[len-1] != '/') ); if (!buf) return; strcpy(buf, home); if (home[len-1] != '/') buf[len++] = '/'; strcpy(buf+len, LOCAL_RC); parse_rc(&xargc, &xargv, buf); free(buf); } } if (xargc > 1) { expand_argcv(&xargc, &xargv, *argc_ptr-1, *argv_ptr+1); *argc_ptr = xargc; *argv_ptr = xargv; } }
unsigned short int _shell_reparse_rc(void){ if(!read_rc){ fprintf(stderr,"rc file could not be read\n"); return 2; }else parse_rc(); return 1; }
void cmd_source(int argc, char **argv) { int i; OPT_HELP_NEW(_("Read (source) a configuration file."), "source [options] <file>...", NULL); minargs(optind); for(i = optind; i < argc; i++) parse_rc(argv[i], true); init_ftp(); }
t_config *init_rc(char *progname) { t_config *config; if (!(config = malloc(sizeof(t_config)))) return (NULL); config->aliases = NULL; config->envvars = NULL; config->vars = NULL; config->file = load_rc(open_rc(progname)); parse_rc(config); return (config); }
void cmd_source(int argc, char **argv) { int i; OPT_HELP("Read (source) a configuration file. Usage:\n" " source [options] <file>...\n" "Options:\n" " -h, --help show this help\n"); minargs(optind); for(i = optind; i < argc; i++) parse_rc(argv[i], true); init_ftp(); }
int main(int argc, char* arg[]) { char fmt_PS1[4096]; /*Defaults for switches, may or may not change after get_options*/ logging = 0; restricted = 0; verbose = 0; read_rc = 1; history = 1; input = 0; get_options(argc,arg); init_env(); if(logging) open_log(); init_shell(); if(read_rc) parse_rc(); if(history) using_history(); if(!PS1){ PS1 = (char *) default_prompt; specified_PS1 = 0; } /* * Here's the main loop. * First we go ahead and get the current working directory * Next we see if the we're in a restricted shell, and change the * prompt accordingly. Then we get the string, and if all goes well * with that, write the history. Then we remove the \n terminator and * feed the string to the parser. After everything is parsed we pass the * string to check for builtins. If it was a built in, there's no need * to execute it, so we skip the execute function, otherwise we go ahead * and execute it. */ while(1){ if( (getcwd(env->cur_dir, sizeof(env->cur_dir)) == NULL)){ if(logging) write_to_log(SHELL_OOPS,"could not get current working directory"); perror("get current directory"); } memset(fmt_PS1, 0, 4096); format_prompt(fmt_PS1, 4096); input = readline(fmt_PS1); if(!input) exit_clean(1, "invalid input"); if(strlen(input) < 1) continue; if(history){ if(!history_filename) get_history_filename(); add_history(input); } parse(input, argv); if(built_ins(argv) == 1) continue; execute(argv); } }
void cmd_bookmark(int argc, char **argv) { struct option longopts[] = { {"save", optional_argument, 0, 's'}, {"edit", no_argument, 0, 'e'}, {"read", optional_argument, 0, 'r'}, {"delete", no_argument, 0, 'd'}, {"list", no_argument, 0, 'l'}, {"noupdate", no_argument, 0, 'u'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int action = BM_BOOKMARK; int c; char *bmfile = 0; optind = 0; while((c=getopt_long(argc, argv, "s::er::dluh", longopts, 0)) != EOF) { switch(c) { case 's': action = BM_SAVE; free(bmfile); bmfile = xstrdup(optarg); break; case 'e': action = BM_EDIT; break; case 'r': action = BM_READ; free(bmfile); bmfile = xstrdup(optarg); break; case 'd': action = BM_DELETE; break; case 'l': action = BM_LIST; break; case 'u': action = BM_TOGGLE_NOUPDATE; break; case 'h': print_bookmark_syntax(); /* fall through */ default: return; } } if(action == BM_SAVE) { bookmark_save(bmfile); if(bmfile) printf(_("bookmarks saved in %s\n"), bmfile); else printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory); return; } if(action == BM_READ) { char *tmp = 0; int ret; list_clear(gvBookmarks); if(bmfile) ret = parse_rc(bmfile, true); else { if (asprintf(&tmp, "%s/bookmarks", gvWorkingDirectory) == -1) { fprintf(stderr, _("Failed to allocate memory.\n")); return; } ret = parse_rc(tmp, false); } if(ret != -1) printf(_("bookmarks read from %s\n"), bmfile ? bmfile : tmp); free(tmp); return; } if(action == BM_EDIT) { invoke_shell("%s %s/bookmarks", gvEditor, gvWorkingDirectory); return; } if(action == BM_LIST) { listitem *li; printf(_("%zu bookmarks present in memory\n"), list_numitem(gvBookmarks)); for(li = gvBookmarks->first; li; li=li->next) { url_t *u = (url_t *)li->data; /* note: all info not printed */ printf("%-20s", u->alias ? u->alias : u->hostname); printf("%s://%s@%s", u->protocol ? u->protocol : "ftp", u->username, u->hostname); if(u->directory) { char* sp = shortpath(u->directory, 30, 0); printf("/%s", sp); free(sp); } putchar('\n'); } return; } if(action == BM_DELETE) { int i; bool del_done = false; minargs(optind); for(i = optind; i < argc; i++) { listitem *li; while((li = list_search(gvBookmarks, (listsearchfunc)urlcmp_name, argv[i])) != 0) { url_t *u = (url_t *)li->data; printf(_("deleted bookmark %s\n"), u->alias ? u->alias : u->hostname); list_delitem(gvBookmarks, li); del_done = true; } } if(del_done) { bookmark_save(0); printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory); } return; } if(action == BM_TOGGLE_NOUPDATE) { int i; bool toggle_done = false; if(argc == optind) { listitem *li; need_connected(); need_loggedin(); li = list_search(gvBookmarks, (listsearchfunc)urlcmp_name, ftp->url->alias); if(li) { url_t *u = (url_t *)li->data; u->noupdate = !u->noupdate; printf(_("%s: noupdate: %s\n"), u->alias ? u->alias : u->hostname, u->noupdate ? _("yes") : _("no")); toggle_done = true; } ftp->url->noupdate = !ftp->url->noupdate; if(!toggle_done) printf(_("%s: noupdate: %s\n"), ftp->url->alias ? ftp->url->alias : ftp->url->hostname, ftp->url->noupdate ? _("yes") : _("no")); } for(i = optind; i < argc; i++) { listitem *li; li = list_search(gvBookmarks, (listsearchfunc)urlcmp_name, argv[i]); if(li) { url_t *u = (url_t *)li->data; u->noupdate = !u->noupdate; printf(_("%s: noupdate: %s\n"), u->alias ? u->alias : u->hostname, u->noupdate ? _("yes") : _("no")); toggle_done = true; } } if(toggle_done) { bookmark_save(0); printf(_("bookmarks saved in %s/bookmarks\n"), gvWorkingDirectory); } return; } maxargs(1); need_connected(); need_loggedin(); create_bookmark(argc > 1 ? argv[1] : 0); }
int main(int argc, char *argv[]) { int c = 0, stop = 0, usec = 0, start_slot = -1; unsigned long frames, frames_old, fps; char *patches = NULL, *rom = NULL; unsigned long oldclk, newclk, startclk, fpsclk; FILE *file = NULL; enum demo_status demo_status = DEMO_OFF; unsigned int samples; class md *megad; bool first = true; bool forced_hz = false; bool forced_pal = false; // Parse the RC file if ((dgen_autoconf) && ((file = dgen_fopen_autorc(DGEN_READ)) != NULL)) { parse_rc(file, DGEN_AUTORC); fclose(file); } if ((file = dgen_fopen_rc(DGEN_READ)) != NULL) { parse_rc(file, DGEN_RC); fclose(file); file = NULL; pd_rc(); } else if (errno == ENOENT) { if ((file = dgen_fopen_rc(DGEN_APPEND)) != NULL) { fprintf(file, "# DGen " VER " configuration file.\n" "# See dgenrc(5) for more information.\n"); fclose(file); file = NULL; } } else fprintf(stderr, "rc: %s: %s\n", DGEN_RC, strerror(errno)); // Check all our options snprintf(temp, sizeof(temp), "%s%s", #ifdef __MINGW32__ "m" #endif "s:hvr:n:p:R:NPH:d:D:", pd_options); while((c = getopt(argc, argv, temp)) != EOF) { switch(c) { case 'v': // Show version and exit printf("DGen/SDL version "VER"\n"); return 0; case 'r': // Parse another RC file or stdin if ((strcmp(optarg, "-") == 0) || ((file = dgen_fopen(NULL, optarg, (DGEN_READ | DGEN_CURRENT))) != NULL)) { if (file == NULL) parse_rc(stdin, "(stdin)"); else { parse_rc(file, optarg); fclose(file); file = NULL; } pd_rc(); } else fprintf(stderr, "rc: %s: %s\n", optarg, strerror(errno)); break; case 'n': // Sleep for n microseconds dgen_nice = atoi(optarg); break; case 'p': // Game Genie patches patches = optarg; break; case 'R': // Region selection if (strlen(optarg) != 1) goto bad_region; switch (optarg[0] | 0x20) { case 'j': case 'x': case 'u': case 'e': case ' ': break; default: bad_region: fprintf(stderr, "main: invalid region `%s'.\n", optarg); return EXIT_FAILURE; } dgen_region = (optarg[0] & ~(0x20)); // Override PAL and Hz settings if region is specified. if (dgen_region) { int hz; int pal; md::region_info(dgen_region, &pal, &hz, 0, 0, 0); dgen_hz = hz; dgen_pal = pal; } forced_pal = false; forced_hz = false; break; case 'N': // NTSC mode dgen_hz = NTSC_HZ; dgen_pal = 0; forced_pal = true; break; case 'P': // PAL mode dgen_hz = PAL_HZ; dgen_pal = 1; forced_pal = true; break; case 'H': // Custom frame rate dgen_hz = atoi(optarg); if ((dgen_hz <= 0) || (dgen_hz > 1000)) { fprintf(stderr, "main: invalid frame rate (%ld).\n", (long)dgen_hz); dgen_hz = (dgen_pal ? 50 : 60); forced_hz = false; } else forced_hz = true; break; #ifdef __MINGW32__ case 'm': dgen_mingw_detach = 0; break; #endif case 'd': // Record demo if(file) { fprintf(stderr,"main: Can't record and play at the same time!\n"); break; } if(!(file = dgen_fopen("demos", optarg, DGEN_WRITE))) { fprintf(stderr, "main: Can't record demo file %s!\n", optarg); break; } demo_status = DEMO_RECORD; break; case 'D': // Play demo if(file) { fprintf(stderr,"main: Can't record and play at the same time!\n"); break; } if(!(file = dgen_fopen("demos", optarg, (DGEN_READ | DGEN_CURRENT)))) { fprintf(stderr, "main: Can't play demo file %s!\n", optarg); break; } demo_status = DEMO_PLAY; break; case '?': // Bad option! case 'h': // A cry for help :) help(); case 's': // Pick a savestate to autoload start_slot = atoi(optarg); break; default: // Pass it on to platform-dependent stuff pd_option(c, optarg); break; } } #ifdef __BEOS__ // BeOS snooze() sleeps in milliseconds, not microseconds dgen_nice /= 1000; #endif #ifdef __MINGW32__ if (dgen_mingw_detach) { FILE *cons; fprintf(stderr, "main: Detaching from console, use -m to prevent" " this.\n"); // Console isn't needed anymore. Redirect output to log file. cons = dgen_fopen(NULL, "log.txt", (DGEN_WRITE | DGEN_TEXT)); if (cons != NULL) { fflush(stdout); fflush(stderr); dup2(fileno(cons), fileno(stdout)); dup2(fileno(cons), fileno(stderr)); fclose(cons); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); cons = NULL; } FreeConsole(); } #endif // Initialize the platform-dependent stuff. if (!pd_graphics_init(dgen_sound, dgen_pal, dgen_hz)) { fprintf(stderr, "main: Couldn't initialize graphics!\n"); return 1; } if(dgen_sound) { long rate = dgen_soundrate; if (dgen_soundsegs < 0) dgen_soundsegs = 0; samples = (dgen_soundsegs * (rate / dgen_hz)); dgen_sound = pd_sound_init(rate, samples); } rom = argv[optind]; // Create the megadrive object. megad = new md(dgen_pal, dgen_region); if ((megad == NULL) || (!megad->okay())) { fprintf(stderr, "main: Mega Drive initialization failed.\n"); goto clean_up; } next_rom: // Load the requested ROM. if (rom != NULL) { if (megad->load(rom)) { pd_message("Unable to load \"%s\".", rom); if ((first) && ((optind + 1) == argc)) goto clean_up; } else pd_message("Loaded \"%s\".", rom); } else pd_message("No cartridge."); first = false; // Set untouched pads. megad->pad[0] = MD_PAD_UNTOUCHED; megad->pad[1] = MD_PAD_UNTOUCHED; #ifdef WITH_JOYSTICK if (dgen_joystick) megad->init_joysticks(); #endif // Load patches, if given. if (patches) { printf("main: Using patch codes \"%s\".\n", patches); megad->patch(patches, NULL, NULL, NULL); // Use them only once. patches = NULL; } // Fix checksum megad->fix_rom_checksum(); // Reset megad->reset(); // Automatic region settings from ROM header. if (!dgen_region) { uint8_t c = megad->region_guess(); int hz; int pal; md::region_info(c, &pal, &hz, 0, 0, 0); if (forced_hz) hz = dgen_hz; if (forced_pal) pal = dgen_pal; if ((hz != dgen_hz) || (pal != dgen_pal) || (c != megad->region)) { megad->region = c; dgen_hz = hz; dgen_pal = pal; printf("main: reconfiguring for region \"%c\": " "%dHz (%s)\n", c, hz, (pal ? "PAL" : "NTSC")); pd_graphics_reinit(dgen_sound, dgen_pal, dgen_hz); if (dgen_sound) { long rate = dgen_soundrate; pd_sound_deinit(); samples = (dgen_soundsegs * (rate / dgen_hz)); pd_sound_init(rate, samples); } megad->pal = pal; megad->init_pal(); megad->init_sound(); } } // Load up save RAM ram_load(*megad); // If -s option was given, load the requested slot if (start_slot >= 0) { slot = start_slot; md_load(*megad); } // If autoload is on, load save state 0 else if (dgen_autoload) { slot = 0; md_load(*megad); } // Start the timing refs startclk = pd_usecs(); oldclk = startclk; fpsclk = startclk; // Start audio if (dgen_sound) pd_sound_start(); // Show cartridge header if (dgen_show_carthead) pd_show_carthead(*megad); // Go around, and around, and around, and around... ;) frames = 0; frames_old = 0; fps = 0; while (!stop) { const unsigned int usec_frame = (1000000 / dgen_hz); unsigned long tmp; int frames_todo; newclk = pd_usecs(); if (pd_stopped()) { // Fix FPS count. tmp = (newclk - oldclk); startclk += tmp; fpsclk += tmp; oldclk = newclk; } // Update FPS count. tmp = ((newclk - fpsclk) & 0x3fffff); if (tmp >= 1000000) { fpsclk = newclk; if (frames_old > frames) fps = (frames_old - frames); else fps = (frames - frames_old); frames_old = frames; } if (dgen_frameskip == 0) { // Check whether megad->one_frame() must be called. if (pd_freeze) goto frozen; goto do_not_skip; } // Measure how many frames to do this round. usec += ((newclk - oldclk) & 0x3fffff); // no more than 4 secs frames_todo = (usec / usec_frame); usec %= usec_frame; oldclk = newclk; if (frames_todo == 0) { // No frame to do yet, relax the CPU until next one. tmp = (usec_frame - usec); if (tmp > 1000) { // Never sleep for longer than the 50Hz value // so events are checked often enough. if (tmp > (1000000 / 50)) tmp = (1000000 / 50); tmp -= 1000; #ifdef __BEOS__ snooze(tmp / 1000); #else usleep(tmp); #endif } } else { // Check whether megad->one_frame() must be called. if (pd_freeze) goto frozen; // Draw frames. while (frames_todo != 1) { do_demo(*megad, file, &demo_status); if (dgen_sound) { // Skip this frame, keep sound going. megad->one_frame(NULL, NULL, &sndi); pd_sound_write(); } else megad->one_frame(NULL, NULL, NULL); --frames_todo; stop |= (pd_handle_events(*megad) ^ 1); } --frames_todo; do_not_skip: do_demo(*megad, file, &demo_status); if (dgen_sound) { megad->one_frame(&mdscr, mdpal, &sndi); pd_sound_write(); } else megad->one_frame(&mdscr, mdpal, NULL); frozen: if ((mdpal) && (pal_dirty)) { pd_graphics_palette_update(); pal_dirty = 0; } pd_graphics_update(megad->plugged); ++frames; } stop |= (pd_handle_events(*megad) ^ 1); if (dgen_nice) { #ifdef __BEOS__ snooze(dgen_nice); #else usleep(dgen_nice); #endif } } // Pause audio. if (dgen_sound) pd_sound_pause(); #ifdef WITH_JOYSTICK if (dgen_joystick) megad->deinit_joysticks(); #endif // Print fps fpsclk = ((pd_usecs() - startclk) / 1000000); if (fpsclk == 0) fpsclk = 1; #ifdef WITH_DEBUGGER megad->debug_leave(); #endif printf("%lu frames per second (average %lu, optimal %ld)\n", fps, (frames / fpsclk), (long)dgen_hz); ram_save(*megad); if (dgen_autosave) { slot = 0; md_save(*megad); } megad->unplug(); if (file) { fclose(file); file = NULL; } if ((++optind) < argc) { rom = argv[optind]; stop = 0; goto next_rom; } clean_up: // Cleanup delete megad; pd_quit(); // Save configuration. if (dgen_autoconf) { if ((file = dgen_fopen_autorc(DGEN_WRITE)) == NULL) fputs("main: can't write " DGEN_AUTORC ".\n", stderr); else { fprintf(file, "# DGen/SDL v" VER "\n" "# This file is automatically overwritten.\n" "\n"); dump_rc(file); fclose(file); file = NULL; } } // Come back anytime :) return 0; }