Esempio n. 1
0
/*
 *	Dispatch to the correct command handler.
 *
 */
static int cmd_fdt_dispatch(BT_HANDLE hShell, int argc, char **argv) {

	BT_HANDLE hStdout = BT_ShellGetStdout(hShell);

	if(argc < 2) {
		do_usage(hStdout, argv[0]);
		return -1;
	}


	DISPATCH_TABLE *pCommand = NULL;
	BT_u32 i;

	for(i = 0; i < sizeof(g_Dispatch)/sizeof(DISPATCH_TABLE); i++) {
		if(!strcmp(g_Dispatch[i].name, argv[1])) {
			pCommand = &g_Dispatch[i];
			break;
		}
	}

	if(pCommand) {
		return pCommand->cmd_fn(hShell, argc-2, argv+2);	// Pass arguments after the subcomand.
	}

	return -1;
}
Esempio n. 2
0
int command_line_parse(int argc, char **argv, int *box_id, int *subjectid, int *starthour, int *stophour, int *startmin, int *stopmin, int *resp_wind, float *timeout_val, char **stimfname)
{
int i=0;
 for (i = 1; i < argc; i++){
   if (*argv[i] == '-'){
     if (strncmp(argv[i], "-B", 2) == 0)
       sscanf(argv[++i], "%i", box_id);
     else if (strncmp(argv[i], "-S", 2) == 0)
       sscanf(argv[++i], "%i", subjectid);
     else if (strncmp(argv[i], "-f", 2) == 0)
       flash=1;
     else if (strncmp(argv[i], "-w", 2) == 0)
       sscanf(argv[++i], "%d", resp_wind);
     else if (strncmp(argv[i], "-mirrorP", 8) == 0)
       mirror = 1;
     else if (strncmp(argv[i], "-t", 2) == 0)
       sscanf(argv[++i], "%f", timeout_val);
     else if (strncmp(argv[i], "-on", 3) == 0){
       sscanf(argv[++i], "%i:%i", starthour,startmin);}
     else if (strncmp(argv[i], "-off", 4) == 0){
       sscanf(argv[++i], "%i:%i", stophour, stopmin);}
     else if (strncmp(argv[i], "-x", 2) == 0){
       xresp = 1;}
     else if (strncmp(argv[i], "-help", 5) == 0)
       do_usage();
     else{
       fprintf(stderr, "Unknown option: %s\t", argv[i]);
       fprintf(stderr, "Try 'gng -help' for help\n");
     }
   }
   else
     *stimfname = argv[i];
 }
 return 1;
}
Esempio n. 3
0
/**********************************************************************
 ** parse the command line
 **********************************************************************/
int command_line_parse(int argc, char **argv, int *box_id, int *subjectid, int *starthour, int *stophour,int *startmin, int *stopmin, int *swapval, int *flash, char **stimfname)
{
  int i=0;
  
  for (i = 1; i < argc; i++){
    if (*argv[i] == '-'){
      if (strncmp(argv[i], "-B", 2) == 0) 
        sscanf(argv[++i], "%i", box_id);
      else if (strncmp(argv[i], "-S", 2) == 0)
        sscanf(argv[++i], "%i", subjectid);
      else if (strncmp(argv[i], "-on", 3) == 0){
	sscanf(argv[++i], "%i:%i", starthour,startmin);}
      else if (strncmp(argv[i], "-off", 4) == 0){
	sscanf(argv[++i], "%i:%i", stophour, stopmin);}
     else if (strncmp(argv[i], "-swap", 5) == 0){
        sscanf(argv[++i], "%i", swapval);}
      else if (strncmp(argv[i], "-f", 2) == 0){
	      *flash = 1;}
      else if (strncmp(argv[i], "-help", 5) == 0){
        do_usage();
      }
      else{
        fprintf(stderr, "Unknown option: %s\t", argv[i]);
        fprintf(stderr, "Try 'femxpeck -help'\n");
      }
    }
    else
      {
        *stimfname = argv[i];
      }
  }
  return 1;
}
Esempio n. 4
0
void do_exec(char *args[])
{
  if (!args[0])
    do_usage();

  my_end(0);
  execvp(args[0], args);
}
Esempio n. 5
0
int command_line_parse(int argc, char **argv, int *box_id, int *subjectid, int *starthour, int *stophour, int *startmin, int *stopmin, int *resp_wind, float *timeout_val, float *breakdur, float *ptarg,  int *new_trialtarg, float *targ_dB, float *dist_dB, char **stimfname)
{
  int i=0;
  
  for (i = 1; i < argc; i++){
    if (*argv[i] == '-'){
      if (strncmp(argv[i], "-B", 2) == 0)          
        sscanf(argv[++i], "%i", box_id);
      else if (strncmp(argv[i], "-S", 2) == 0)     
        sscanf(argv[++i], "%i", subjectid);
      else if (strncmp(argv[i], "-xnr", 4) == 0)   
	xresp = 1;
      else if (strncmp(argv[i], "-xoff", 5) == 0) 
	NOxresp = 1;
      else if (strncmp(argv[i], "-w", 2) == 0){     
	sscanf(argv[++i], "%i", resp_wind);
      }
      else if (strncmp(argv[i], "-t", 2) == 0){     
	sscanf(argv[++i], "%f", timeout_val);
      }
      else if (strncmp(argv[i], "-f", 2) == 0)    
	flash = 1;
      else if (strncmp(argv[i], "-n", 2) == 0)    
	sscanf(argv[++i], "%i", new_trialtarg);
      else if (strncmp(argv[i], "-q", 2) == 0)    
	cueflag = 1; 
      else if (strncmp(argv[i], "-on", 3) == 0)
        sscanf(argv[++i], "%i:%i", starthour, startmin);
      else if (strncmp(argv[i], "-off", 4) == 0) 
        sscanf(argv[++i], "%i:%i", stophour, stopmin);
      else if (strncmp(argv[i], "-b", 2) == 0)     
	sscanf(argv[++i], "%f", breakdur);
      else if (strncmp(argv[i], "-pt", 3) == 0)    
	sscanf(argv[++i], "%f", ptarg);
      else if (strncmp(argv[i], "-dBt", 4) == 0)  
	sscanf(argv[++i], "%f", targ_dB);
      else if (strncmp(argv[i], "-dBd", 4) == 0)   
	sscanf(argv[++i], "%f", dist_dB);
      else if (strncmp(argv[i], "-FULL", 5) == 0)
	fullflag = 1;
      else if (strncmp(argv[i], "-help", 5) == 0){
        do_usage();
      }
      else{
        fprintf(stderr, "Unknown option: %s\t", argv[i]);
        fprintf(stderr, "Try '2ac -help'\n");
      }
    }
    else
      {
        *stimfname = argv[i];
      }
  }
  return 1;
}
gint main (gint argc, gchar **argv)
{
	if (argc < 2)
		do_usage (argv[0]);

	camel_init (NULL, 0);

	if (!strcmp(argv[1], "compress"))
		return do_compress (argc, argv);
	else if (!strcmp(argv[1], "dump"))
		return do_dump (argc, argv);
	else if (!strcmp(argv[1], "info"))
		return do_info (argc, argv);
	else if (!strcmp(argv[1], "check"))
		return do_check (argc, argv);
	else if (!strcmp(argv[1], "perf"))
		return do_perf (argc, argv);

	do_usage (argv[0]);
	return 1;
}
Esempio n. 7
0
static void
do_nthreads(const char *name, const char *spec)
{
	int nt;
	char c;

	if (sscanf(spec, "%d %c", &nt, &c) != 1 
	    || nt <= 0)
		do_usage(name, 1);

	/* Set new resolution. */
	nthreads = nt;
}
Esempio n. 8
0
static int fdt_addr(BT_HANDLE hShell, int argc, char **argv) {

	BT_HANDLE hStdout = BT_ShellGetStdout(hShell);

	if(argc != 1) {
		do_usage(hStdout, argv[0]);
		return -1;
	}

	g_fdt_addr = (void *) strtoul(argv[0], NULL, 16);

	return 0;
}
Esempio n. 9
0
int command_line_parse(int argc, char **argv, int *box_id, int *subjectid, int *starthour, int *stophour, int *startmin, int *stopmin, int *resp_wind, float *timeout_val, char **stimfname, int *frcritical)
{
  int i=0;
  
  for (i = 1; i < argc; i++){
    if (*argv[i] == '-'){
      if (strncmp(argv[i], "-B", 2) == 0) 
        sscanf(argv[++i], "%i", box_id);
      else if (strncmp(argv[i], "-S", 2) == 0)
        sscanf(argv[++i], "%i", subjectid);
      else if (strncmp(argv[i], "-x", 2) == 0)
	xresp = 1;
      else if (strncmp(argv[i], "-FR", 3) == 0){
	sscanf(argv[++i], "%i", frcritical);
      }
      else if (strncmp(argv[i], "-noC", 4) == 0){
	no_correction_flag = 1;
      }
      else if (strncmp(argv[i], "-w", 2) == 0){
	sscanf(argv[++i], "%i", resp_wind);
      }
      else if (strncmp(argv[i], "-t", 2) == 0){ 
	sscanf(argv[++i], "%f", timeout_val);
      }
      else if (strncmp(argv[i], "-f", 2) == 0)
	flash = 1;
      else if (strncmp(argv[i], "-q", 2) == 0)
	cueflag = 1;
      else if (strncmp(argv[i], "-J", 2) == 0)
	dosecondaryR = 1;
      else if (strncmp(argv[i], "-on", 3) == 0)
        sscanf(argv[++i], "%i:%i", starthour, startmin);
      else if (strncmp(argv[i], "-off", 4) == 0)
        sscanf(argv[++i], "%i:%i", stophour, stopmin);
      else if (strncmp(argv[i], "-help", 5) == 0){
        do_usage();
      }
      else{
        fprintf(stderr, "Unknown option: %s\t", argv[i]);
        fprintf(stderr, "Try '2ac -help'\n");
      }
    }
    else
      {
        *stimfname = argv[i];
      }
  }
  return 1;
}
Esempio n. 10
0
/**
 * ./itrace -p [PID]
 *
 * opens an interactive session where you can trace a process single-step
 * the instructions will be disassembled via udis86
 * provided by sample code snyfer.c
 */
int main(int argc,
	 char* argv[])
{
  if(3==argc)
  {
    init_attach(argv[2]);
    create_interpreter();
    do_sniff();
  }else{
    do_usage();
    return -1;
  }

  return 0;
}
Esempio n. 11
0
int command_line_parse(int argc, char **argv, int *box_id, int *subjectid, int *starthour, int *stophour, int *startmin, int *stopmin, int *resp_wind, float *timeout_val, float *targlength, float *cueint, char **stimfname)
{
  int i=0;
  
  for (i = 1; i < argc; i++){
    if (*argv[i] == '-'){
      if (strncmp(argv[i], "-B", 2) == 0) 
        sscanf(argv[++i], "%i", box_id);
      else if (strncmp(argv[i], "-S", 2) == 0)
        sscanf(argv[++i], "%i", subjectid);
      else if (strncmp(argv[i], "-x", 2) == 0)
	xresp = 1;
      else if (strncmp(argv[i], "-w", 2) == 0){
	sscanf(argv[++i], "%i", resp_wind);
      }
      else if (strncmp(argv[i], "-trg", 4) == 0){ 
	sscanf(argv[++i], "%f", targlength);
      }      
      else if (strncmp(argv[i], "-t", 2) == 0){ 
	sscanf(argv[++i], "%f", timeout_val);
      }
      else if (strncmp(argv[i], "-qint", 5) == 0){ 
	sscanf(argv[++i], "%f", cueint);
      }
      else if (strncmp(argv[i], "-f", 2) == 0)
	flash = 1;
      else if (strncmp(argv[i], "-on", 3) == 0)
        sscanf(argv[++i], "%i:%i", starthour, startmin);
      else if (strncmp(argv[i], "-off", 4) == 0)
        sscanf(argv[++i], "%i:%i", stophour, stopmin);
      else if (strncmp(argv[i], "-help", 5) == 0){
        do_usage();
      }
      else{
        fprintf(stderr, "Unknown option: %s\t", argv[i]);
        fprintf(stderr, "Try 'SONGCHOICE -help'\n");
      }
    }
    else
      {
        *stimfname = argv[i];
      }
  }
  return 1;
}
Esempio n. 12
0
static void
do_resolution(const char *name, const char *spec)
{
	int x;
	int y;
	char c;
	char d;

	if (sscanf(spec, "%d%c%d %c", &x, &c, &y, &d) != 3
	    || x <= 0
	    || c != 'x'
	    || y <= 0)
		do_usage(name, 1);

	/* Set new resolution. */
	x_res = x;
	y_res = y;
}
Esempio n. 13
0
static int fdt_set(BT_HANDLE hShell, int argc, char **argv) {

	BT_HANDLE hStdout = BT_ShellGetStdout(hShell);
	int retval = 0;

	if(argc < 2) {
		do_usage(hStdout, argv[0]);
		return -1;
	}

	char *path = argv[0];
	char *property = argv[1];
	char *data = NULL;

	int nodeoffset;
	int len = 0;

	nodeoffset = fdt_path_offset(g_fdt_addr, path);
	if(nodeoffset < 0) {
		bt_fprintf(hStdout, "libfdt: fdt_path_offset() returned %s\n", fdt_strerror(nodeoffset));
		return -1;
	}

	if(argc == 3) {
		// Parse and allocate data as required.
	}

	int bAllocated = 0;
	retval = fdt_parse_property(hStdout, argv+2, argc-2, &data, &len, &bAllocated);
	if(retval < 0) {
		bt_fprintf(hStdout, "Error parsing property value string!\n");
		goto err_out;
	}

	fdt_setprop(g_fdt_addr, nodeoffset, property, data, len);


err_out:
	if(bAllocated) {
		BT_kFree(data);
	}

	return retval;
}
Esempio n. 14
0
void do_log(const char *logfile)
{
  FILE *f;
  uchar buf[4096];
  int size;

  if (!logfile)
    do_usage();

  f= my_fopen(logfile, O_WRONLY|O_APPEND|O_CREAT, MYF(MY_WME));
  if (!f)
    my_exit(1);

  while ((size= my_fread(stdin, buf, sizeof(buf), MYF(MY_WME))) > 0)
    if ((int)my_fwrite(f, buf, size, MYF(MY_WME)) != size)
      my_exit(1);

  my_fclose(f, MYF(0));
  my_exit(0);
}
Esempio n. 15
0
int main(int argc, char *argv[])
{
  struct passwd *user_info;
  MY_INIT(argv[0]);

  if (argc < 3)
    do_usage(argv[0]);

  user_info= my_check_user(argv[1], MYF(0));
  if (user_info ? my_set_user(argv[1], user_info, MYF(MY_WME))
                : my_errno == EINVAL)
    my_exit(1);

  if (strcmp(argv[2], "log") == 0)
    do_log(argv[3]);

  if (strcmp(argv[2], "exec") == 0)
    do_exec(argv+3);

  my_end(0);
  return 1;
}
Esempio n. 16
0
int
main(int argc, char *argv[])
{
	int optc;
	int option_differences = 0,
	    option_differences_cumulative = 0,
			option_exec = 0,
			option_beep = 0,
      option_color = 0,
        option_errexit = 0,
	    option_help = 0, option_version = 0;
	double interval = 2;
	char *command;
	wchar_t *wcommand = NULL;
	char **command_argv;
	int command_length = 0;	/* not including final \0 */
	int wcommand_columns = 0;	/* not including final \0 */
	int wcommand_characters = 0; /* not including final \0 */
    watch_usec_t next_loop; /* next loop time in us, used for precise time
                               keeping only */
	int pipefd[2];
	int status;
	pid_t child;

	setlocale(LC_ALL, "");
	progname = argv[0];

	while ((optc = getopt_long(argc, argv, "+bced::hn:pvtx", longopts, (int *) 0))
	       != EOF) {
		switch (optc) {
		case 'b':
			option_beep = 1;
			break;
    case 'c':
      option_color = 1;
      break;
		case 'd':
			option_differences = 1;
			if (optarg)
				option_differences_cumulative = 1;
			break;
        case 'e':
            option_errexit = 1;
            break;
		case 'h':
			option_help = 1;
			break;
		case 't':
			show_title = 0;
			break;
		case 'x':
		  option_exec = 1;
			break;
		case 'n':
			{
				char *str;
				interval = strtod(optarg, &str);
				if (!*optarg || *str)
					do_usage();
				if(interval < 0.1)
					interval = 0.1;
				if(interval > ~0u/1000000)
					interval = ~0u/1000000;
			}
			break;
		case 'p':
			precise_timekeeping = 1;
			break;
		case 'v':
			option_version = 1;
			break;
		default:
			do_usage();
			break;
		}
	}

	if (option_version) {
		fprintf(stderr, "%s\n", VERSION);
		if (!option_help)
			exit(0);
	}

	if (option_help) {
		fprintf(stderr, usage, progname);
		fputs("  -b, --beep\t\t\t\tbeep if the command has a non-zero exit\n", stderr);
		fputs("  -d, --differences[=cumulative]\thighlight changes between updates\n", stderr);
		fputs("\t\t(cumulative means highlighting is cumulative)\n", stderr);
		fputs("  -e, --errexit\t\t\t\texit watch if the command has a non-zero exit\n", stderr);
		fputs("  -h, --help\t\t\t\tprint a summary of the options\n", stderr);
		fputs("  -n, --interval=<seconds>\t\tseconds to wait between updates\n", stderr);
        fputs("  -p, --precise\t\t\t\tprecise timing, ignore command run time\n", stderr);
		fputs("  -v, --version\t\t\t\tprint the version number\n", stderr);
		fputs("  -t, --no-title\t\t\tturns off showing the header\n", stderr);
		fputs("  -x, --exec\t\t\t\tpass command to exec instead of sh\n", stderr);
		exit(0);
	}

	if (optind >= argc)
		do_usage();

	command_argv=&(argv[optind]); /* save for later */

	command = strdup(argv[optind++]);
	command_length = strlen(command);
	for (; optind < argc; optind++) {
		char *endp;
		int s = strlen(argv[optind]);
		command = realloc(command, command_length + s + 2);	/* space and \0 */
		endp = command + command_length;
		*endp = ' ';
		memcpy(endp + 1, argv[optind], s);
		command_length += 1 + s;	/* space then string length */
		command[command_length] = '\0';
	}

	// convert to wide for printing purposes
	//mbstowcs(NULL, NULL, 0);
	wcommand_characters = mbstowcs(NULL, command, 0);
	if(wcommand_characters < 0) {
		fprintf(stderr, "Unicode Handling Error\n");
		exit(1);
	}
	wcommand = (wchar_t*)malloc((wcommand_characters+1) * sizeof(wcommand));
	if(wcommand == NULL) {
		fprintf(stderr, "Unicode Handling Error (malloc)\n");
		exit(1);
	}
	mbstowcs(wcommand, command, wcommand_characters+1);
	wcommand_columns = wcswidth(wcommand, -1);



	get_terminal_size();

	/* Catch keyboard interrupts so we can put tty back in a sane state.  */
	signal(SIGINT, die);
	signal(SIGTERM, die);
	signal(SIGHUP, die);
	signal(SIGWINCH, winch_handler);

	/* Set up tty for curses use.  */
	curses_started = 1;
	initscr();
  if (option_color) {
    if (has_colors()) {
      start_color();
      use_default_colors();
      init_ansi_colors();
    } else
      option_color = 0;
  }
	nonl();
	noecho();
	cbreak();

	if (precise_timekeeping)
		next_loop = get_time_usec();

	for (;;) {
		time_t t = time(NULL);
		char *ts = ctime(&t);
		int tsl = strlen(ts);
		char *header;
		FILE *p;
		int x, y;
		int oldeolseen = 1;

		if (screen_size_changed) {
			get_terminal_size();
			resizeterm(height, width);
			clear();
			/* redrawwin(stdscr); */
			screen_size_changed = 0;
			first_screen = 1;
		}

		if (show_title) {
			// left justify interval and command,
			// right justify time, clipping all to fit window width

			int hlen = asprintf(&header, "Every %.1fs: ", interval);

			// the rules:
			//   width < tsl : print nothing
			//   width < tsl + hlen + 1: print ts
			//   width = tsl + hlen + 1: print header, ts
			//   width < tsl + hlen + 4: print header, ..., ts
			//   width < tsl + hlen + wcommand_columns: print header, truncated wcommand, ..., ts
			//   width > "": print header, wcomand, ts
			// this is slightly different from how it used to be
			if(width >= tsl) {
				if(width >= tsl + hlen + 1) {
					mvaddstr(0, 0, header);
					if(width >= tsl + hlen + 2) {
						if(width < tsl + hlen + 4) {
							mvaddstr(0, width - tsl - 4, "...  ");
						}else{
							if(width < tsl + hlen + wcommand_columns) {
								// print truncated
								int avail_columns = width - tsl - hlen;
								int using_columns = wcommand_columns;
								int using_characters = wcommand_characters;
								while(using_columns > avail_columns - 4) {
									using_characters--;
								using_columns = wcswidth(wcommand, using_characters);
								}
								mvaddnwstr(0, hlen, wcommand, using_characters);
								mvaddstr(0, width - tsl - 4, "... ");
							}else{
								mvaddwstr(0, hlen, wcommand);
							}
						}
					}
				}
				mvaddstr(0, width - tsl + 1, ts);
			}

			free(header);
		}

		/* allocate pipes */
		if (pipe(pipefd)<0) {
		  perror("pipe");
			do_exit(7);
	  }

		/* flush stdout and stderr, since we're about to do fd stuff */
		fflush(stdout);
		fflush(stderr);

		/* fork to prepare to run command */
		child=fork();

		if (child<0) { /* fork error */
		  perror("fork");
			do_exit(2);
		} else if (child==0) { /* in child */
			close (pipefd[0]); /* child doesn't need read side of pipe */
			close (1); /* prepare to replace stdout with pipe */
			if (dup2 (pipefd[1], 1)<0) { /* replace stdout with write side of pipe */
			  perror("dup2");
				exit(3);
			}
			dup2(1, 2); /* stderr should default to stdout */

			if (option_exec) { /* pass command to exec instead of system */
			  if (execvp(command_argv[0], command_argv)==-1) {
				  perror("exec");
				  exit(4);
				}
			} else {
		    status=system(command); /* watch manpage promises sh quoting */

			  /* propagate command exit status as child exit status */
			  if (!WIFEXITED(status)) { /* child exits nonzero if command does */
			    exit(1);
			  } else {
			    exit(WEXITSTATUS(status));
		    }
			}

		}

		/* otherwise, we're in parent */
		close(pipefd[1]); /* close write side of pipe */
		if ((p=fdopen(pipefd[0], "r"))==NULL) {
		  perror("fdopen");
			do_exit(5);
		}


		for (y = show_title; y < height; y++) {
			int eolseen = 0, tabpending = 0;
			wint_t carry = WEOF;
			for (x = 0; x < width; x++) {
				wint_t c = L' ';
				int attr = 0;

				if (!eolseen) {
					/* if there is a tab pending, just spit spaces until the
					   next stop instead of reading characters */
					if (!tabpending)
						do {
							if(carry == WEOF) {
								c = my_getwc(p);
							}else{
								c = carry;
								carry = WEOF;
							}
						}while (c != WEOF && !isprint(c) && c<128
						       && wcwidth(c) == 0
						       && c != L'\n'
						       && c != L'\t'
                   && (c != L'\033' || option_color != 1));
          if (c == L'\033' && option_color == 1) {
            x--;
            process_ansi(p);
            continue;
          }
					if (c == L'\n')
						if (!oldeolseen && x == 0) {
							x = -1;
							continue;
						} else
							eolseen = 1;
					else if (c == L'\t')
						tabpending = 1;
					if (x==width-1 && wcwidth(c)==2) {
						y++;
						x = -1; //process this double-width
						carry = c; //character on the next line
						continue; //because it won't fit here
					}
					if (c == WEOF || c == L'\n' || c == L'\t')
						c = L' ';
					if (tabpending && (((x + 1) % 8) == 0))
						tabpending = 0;
				}
				move(y, x);
				if (option_differences) {
						cchar_t oldc;
					in_wch(&oldc);
					attr = !first_screen
					    && ((wchar_t)c != oldc.chars[0]
						||
						(option_differences_cumulative
						 && (oldc.attr & A_ATTRIBUTES)));
				}
				if (attr)
					standout();
				addnwstr((wchar_t*)&c,1);
				if (attr)
					standend();
				if(wcwidth(c) == 0) { x--; }
				if(wcwidth(c) == 2) { x++; }
			}
			oldeolseen = eolseen;
		}

		fclose(p);

		/* harvest child process and get status, propagated from command */
		if (waitpid(child, &status, 0)<0) {
		  perror("waitpid");
			do_exit(8);
		};

		/* if child process exited in error, beep if option_beep is set */
		if ((!WIFEXITED(status) || WEXITSTATUS(status))) {
          if (option_beep) beep();
          if (option_errexit) do_exit(8);
		}

		first_screen = 0;
		refresh();
		if (precise_timekeeping) {
			watch_usec_t cur_time = get_time_usec();
			next_loop += USECS_PER_SEC*interval;
			if (cur_time < next_loop)
				usleep(next_loop - cur_time);
		} else
		usleep(interval * 1000000);
	}

	endwin();

	return 0;
}
Esempio n. 17
0
int   main (int argc, char *argv[])
#endif
{
    parsed_symbol symbol;
    int count = 0;

    globals.mode = NONE;
    globals.forward_dll = NULL;
    globals.input_name = NULL;
    globals.dumpsect = NULL;

    parse_options (argv);

    memset (&symbol, 0, sizeof (parsed_symbol));

    switch (globals.mode)
    {
    case DMGL:
        VERBOSE = TRUE;

        if (globals.input_name == NULL)
            fatal("No symbol name has been given\n");
        printf("%s\n", get_symbol_str(globals.input_name));
	break;

    case SPEC:
        if (globals.input_name == NULL)
            fatal("No file name has been given\n");
        set_module_name(TRUE);
	if (!dll_open (globals.input_name))
            break;

	output_spec_preamble ();
	output_header_preamble ();
	output_c_preamble ();

        while (dll_next_symbol (&symbol))
	{
	    count++;

	    if (NORMAL)
		printf ("Export %3d - '%s' ...%c", count, symbol.symbol,
			VERBOSE ? '\n' : ' ');

	    if (globals.do_code && symbol_searched(count, symbol.symbol))
	    {
		/* Attempt to get information about the symbol */
                BOOL result = symbol_demangle (&symbol) || symbol_search(&symbol);

                if (result && symbol.function_name)
		    /* Clean up the prototype */
		    symbol_clean_string (symbol.function_name);

		if (NORMAL)
                    puts (result ? "[OK]" : "[Not Found]");
	    }
	    else if (NORMAL)
		puts ("[Ignoring]");

	    output_spec_symbol (&symbol);
	    output_header_symbol (&symbol);
	    output_c_symbol (&symbol);

	    symbol_clear (&symbol);
	}

	output_makefile ();

	if (VERBOSE)
	    puts ("Finished, Cleaning up...");
        if (symbol_finish())
            return 1;
	break;
    case NONE:
	do_usage(0);
	break;
    case DUMP:
        if (globals.input_name == NULL)
            fatal("No file name has been given\n");
        set_module_name(FALSE);
	dump_file(globals.input_name);
	break;
    }

    return 0;
}
Esempio n. 18
0
int
main(int argc, char * const argv[])
{
	int stream_sock[] = {0, 0, 0};  /* tcp4, tcp6, UNIX */
	int stream_socklen = sizeof(stream_sock) / sizeof(stream_sock[0]);
	int dgram_sock[] = {0, 0};  /* udp4, udp6 */
	int dgram_socklen = sizeof(dgram_sock) / sizeof(dgram_sock[0]);
	char id;
	unsigned short listenport = 2003;
	int ch;
	size_t numaggregators;
	size_t numcomputes;
	server *internal_submission;
	char *listeninterface = NULL;
	server **servers;
	char *allowed_chars = NULL;
	int i;
	enum { SUB, CUM } smode = CUM;

	if (gethostname(relay_hostname, sizeof(relay_hostname)) < 0)
		snprintf(relay_hostname, sizeof(relay_hostname), "127.0.0.1");

	while ((ch = getopt(argc, argv, ":hvdmstf:i:l:p:w:b:q:S:c:H:")) != -1) {
		switch (ch) {
			case 'v':
				do_version();
				break;
			case 'd':
				if (mode == TEST) {
					mode = DEBUGTEST;
				} else {
					mode = DEBUG;
				}
				break;
			case 'm':
				smode = SUB;
				break;
			case 's':
				mode = SUBMISSION;
				break;
			case 't':
				if (mode == DEBUG) {
					mode = DEBUGTEST;
				} else {
					mode = TEST;
				}
				break;
			case 'f':
				config = optarg;
				break;
			case 'i':
				listeninterface = optarg;
				break;
			case 'l':
				relay_logfile = optarg;
				break;
			case 'p':
				listenport = (unsigned short)atoi(optarg);
				if (listenport == 0) {
					fprintf(stderr, "error: port needs to be a number >0\n");
					do_usage(1);
				}
				break;
			case 'w':
				workercnt = (char)atoi(optarg);
				if (workercnt <= 0) {
					fprintf(stderr, "error: workers needs to be a number >0\n");
					do_usage(1);
				}
				break;
			case 'b':
				batchsize = atoi(optarg);
				if (batchsize <= 0) {
					fprintf(stderr, "error: batch size needs to be a number >0\n");
					do_usage(1);
				}
				break;
			case 'q':
				queuesize = atoi(optarg);
				if (queuesize <= 0) {
					fprintf(stderr, "error: queue size needs to be a number >0\n");
					do_usage(1);
				}
				break;
			case 'S':
				collector_interval = atoi(optarg);
				if (collector_interval <= 0) {
					fprintf(stderr, "error: sending interval needs to be "
							"a number >0\n");
					do_usage(1);
				}
				break;
			case 'c':
				allowed_chars = optarg;
				break;
			case 'H':
				snprintf(relay_hostname, sizeof(relay_hostname), "%s", optarg);
				break;
			case '?':
			case ':':
				do_usage(1);
				break;
			case 'h':
			default:
				do_usage(0);
				break;
		}
	}
	if (optind == 1 || config == NULL)
		do_usage(1);


	/* seed randomiser for dispatcher and aggregator "splay" */
	srand(time(NULL));

	if (workercnt == 0)
		workercnt = mode == SUBMISSION ? 2 : get_cores();

	/* any_of failover maths need batchsize to be smaller than queuesize */
	if (batchsize > queuesize) {
		fprintf(stderr, "error: batchsize must be smaller than queuesize\n");
		exit(-1);
	}

	if (relay_logfile != NULL && mode != TEST && mode != DEBUGTEST) {
		FILE *f = fopen(relay_logfile, "a");
		if (f == NULL) {
			fprintf(stderr, "error: failed to open logfile '%s': %s\n",
					relay_logfile, strerror(errno));
			exit(-1);
		}
		relay_stdout = f;
		relay_stderr = f;
	} else {
		relay_stdout = stdout;
		relay_stderr = stderr;
	}
	relay_can_log = 1;

	logout("starting carbon-c-relay v%s (%s), pid=%d\n",
			VERSION, GIT_VERSION, getpid());
	fprintf(relay_stdout, "configuration:\n");
	fprintf(relay_stdout, "    relay hostname = %s\n", relay_hostname);
	fprintf(relay_stdout, "    listen port = %u\n", listenport);
	if (listeninterface != NULL)
		fprintf(relay_stdout, "    listen interface = %s\n", listeninterface);
	fprintf(relay_stdout, "    workers = %d\n", workercnt);
	fprintf(relay_stdout, "    send batch size = %d\n", batchsize);
	fprintf(relay_stdout, "    server queue size = %d\n", queuesize);
	fprintf(relay_stdout, "    statistics submission interval = %ds\n",
			collector_interval);
	if (allowed_chars != NULL)
		fprintf(relay_stdout, "    extra allowed characters = %s\n",
				allowed_chars);
	if (mode == DEBUG || mode == DEBUGTEST)
		fprintf(relay_stdout, "    debug = true\n");
	else if (mode == SUBMISSION)
		fprintf(relay_stdout, "    submission = true\n");
	fprintf(relay_stdout, "    routes configuration = %s\n", config);
	fprintf(relay_stdout, "\n");

	if (router_readconfig(&clusters, &routes,
				config, queuesize, batchsize) == 0)
	{
		logerr("failed to read configuration '%s'\n", config);
		return 1;
	}
	router_optimise(&routes);

	numaggregators = aggregator_numaggregators();
	numcomputes = aggregator_numcomputes();
#define dbg (mode == DEBUG || mode == DEBUGTEST ? 2 : 0)
	if (numaggregators > 10 && !dbg) {
		fprintf(relay_stdout, "parsed configuration follows:\n"
				"(%zd aggregations with %zd computations omitted "
				"for brevity)\n", numaggregators, numcomputes);
		router_printconfig(relay_stdout, 0, clusters, routes);
	} else {
		fprintf(relay_stdout, "parsed configuration follows:\n");
		router_printconfig(relay_stdout, 1 + dbg, clusters, routes);
	}
	fprintf(relay_stdout, "\n");

	/* shortcut for rule testing mode */
	if (mode == TEST || mode == DEBUGTEST) {
		char metricbuf[METRIC_BUFSIZ];
		char *p;

		fflush(relay_stdout);
		while (fgets(metricbuf, sizeof(metricbuf), stdin) != NULL) {
			if ((p = strchr(metricbuf, '\n')) != NULL)
				*p = '\0';
			router_test(metricbuf, routes);
		}

		exit(0);
	}

	if (signal(SIGINT, exit_handler) == SIG_ERR) {
		logerr("failed to create SIGINT handler: %s\n", strerror(errno));
		return 1;
	}
	if (signal(SIGTERM, exit_handler) == SIG_ERR) {
		logerr("failed to create SIGTERM handler: %s\n", strerror(errno));
		return 1;
	}
	if (signal(SIGQUIT, exit_handler) == SIG_ERR) {
		logerr("failed to create SIGQUIT handler: %s\n", strerror(errno));
		return 1;
	}
	if (signal(SIGHUP, hup_handler) == SIG_ERR) {
		logerr("failed to create SIGHUP handler: %s\n", strerror(errno));
		return 1;
	}
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
		logerr("failed to ignore SIGPIPE: %s\n", strerror(errno));
		return 1;
	}

	workers = malloc(sizeof(dispatcher *) * (1 + workercnt + 1));
	if (workers == NULL) {
		logerr("failed to allocate memory for workers\n");
		return 1;
	}

	if (bindlisten(stream_sock, &stream_socklen,
				dgram_sock, &dgram_socklen,
				listeninterface, listenport) < 0) {
		logerr("failed to bind on port %s:%d: %s\n",
				listeninterface == NULL ? "" : listeninterface,
				listenport, strerror(errno));
		return -1;
	}
	for (ch = 0; ch < stream_socklen; ch++) {
		if (dispatch_addlistener(stream_sock[ch]) != 0) {
			logerr("failed to add listener\n");
			return -1;
		}
	}
	for (ch = 0; ch < dgram_socklen; ch++) {
		if (dispatch_addlistener_udp(dgram_sock[ch]) != 0) {
			logerr("failed to listen to datagram socket\n");
			return -1;
		}
	}
	if ((workers[0] = dispatch_new_listener()) == NULL)
		logerr("failed to add listener\n");

	if (allowed_chars == NULL)
		allowed_chars = "-_:#";
	logout("starting %d workers\n", workercnt);
	for (id = 1; id < 1 + workercnt; id++) {
		workers[id + 0] = dispatch_new_connection(routes, allowed_chars);
		if (workers[id + 0] == NULL) {
			logerr("failed to add worker %d\n", id);
			break;
		}
	}
	workers[id + 0] = NULL;
	if (id < 1 + workercnt) {
		logerr("shutting down due to errors\n");
		keep_running = 0;
	}

	/* server used for delivering metrics produced inside the relay,
	 * that is collector (statistics) and aggregator (aggregations) */
	if ((internal_submission = server_new("internal", listenport, CON_PIPE,
					NULL, 3000 + (numcomputes * 3), batchsize)) == NULL)
	{
		logerr("failed to create internal submission queue, shutting down\n");
		keep_running = 0;
	}

	if (numaggregators > 0) {
		logout("starting aggregator\n");
		if (!aggregator_start(internal_submission)) {
			logerr("shutting down due to failure to start aggregator\n");
			keep_running = 0;
		}
	}

	logout("starting statistics collector\n");
	collector_start(&workers[1], clusters, internal_submission, smode == CUM);

	logout("startup sequence complete\n");

	/* workers do the work, just wait */
	while (keep_running)
		sleep(1);

	logout("shutting down...\n");
	/* make sure we don't accept anything new anymore */
	for (ch = 0; ch < stream_socklen; ch++)
		dispatch_removelistener(stream_sock[ch]);
	destroy_usock(listenport);
	logout("listeners for port %u closed\n", listenport);
	/* since workers will be freed, stop querying the structures */
	collector_stop();
	logout("collector stopped\n");
	if (numaggregators > 0) {
		aggregator_stop();
		logout("aggregator stopped\n");
	}
	server_shutdown(internal_submission);
	/* give a little time for whatever the collector/aggregator wrote,
	 * to be delivered by the dispatchers */
	usleep(500 * 1000);  /* 500ms */
	/* make sure we don't write to our servers any more */
	logout("stopped worker");
	for (id = 0; id < 1 + workercnt; id++)
		dispatch_stop(workers[id + 0]);
	for (id = 0; id < 1 + workercnt; id++) {
		dispatch_shutdown(workers[id + 0]);
		fprintf(relay_stdout, " %d", id + 1);
		fflush(relay_stdout);
	}
	fprintf(relay_stdout, "\n");
	router_shutdown();
	servers = router_getservers(clusters);
	logout("stopped server");
	for (i = 0; servers[i] != NULL; i++)
		server_stop(servers[i]);
	for (i = 0; servers[i] != NULL; i++) {
		server_shutdown(servers[i]);
		fprintf(relay_stdout, " %d", i + 1);
		fflush(relay_stdout);
	}
	fprintf(relay_stdout, "\n");
	logout("routing stopped\n");

	router_free(clusters, routes);
	free(workers);
	return 0;
}
Esempio n. 19
0
void
process_command(int descr, dbref player, char *command)
{
	char *arg1;
	char *arg2;
	char *full_command;
	char *p;					/* utility */
	char pbuf[BUFFER_LEN];
	char xbuf[BUFFER_LEN];
	char ybuf[BUFFER_LEN];
	struct timeval starttime;
	struct timeval endtime;
	double totaltime;

	if (command == 0)
		abort();

	/* robustify player */
	if (player < 0 || player >= db_top ||
		(Typeof(player) != TYPE_PLAYER && Typeof(player) != TYPE_THING)) {
		log_status("process_command: bad player %d", player);
		return;
	}

	if ((tp_log_commands || Wizard(OWNER(player)))) {
		if (!(FLAGS(player) & (INTERACTIVE | READMODE))) {
			if (!*command) {
				return; 
			}
			log_command("%s: %s", whowhere(player), command);
		} else {
			if (tp_log_interactive) {
				log_command("%s: %s%s", whowhere(player),
						(FLAGS(player) & (READMODE)) ? "[READ] " : "[INTERP] ", command);
			}
		}
	}

	if (FLAGS(player) & INTERACTIVE) {
		interactive(descr, player, command);
		return;
	}
	/* eat leading whitespace */
	while (*command && isspace(*command))
		command++;

	/* Disable null command once past READ line */
	if (!*command)
		return;

	/* check for single-character commands */
	if (!tp_enable_prefix) {
		if (*command == SAY_TOKEN) {
			snprintf(pbuf, sizeof(pbuf), "say %s", command + 1);
			command = &pbuf[0];
		} else if (*command == POSE_TOKEN) {
			snprintf(pbuf, sizeof(pbuf), "pose %s", command + 1);
			command = &pbuf[0];
		} else if (*command == EXIT_DELIMITER) {
			snprintf(pbuf, sizeof(pbuf), "delimiter %s", command + 1);
			command = &pbuf[0];
		}
	}

	/* profile how long command takes. */
	gettimeofday(&starttime, NULL);

	/* if player is a wizard, and uses overide token to start line... */
	/* ... then do NOT run actions, but run the command they specify. */
	if (!(TrueWizard(OWNER(player)) && (*command == OVERIDE_TOKEN))) {
		if (can_move(descr, player, command, 0)) {
			do_move(descr, player, command, 0);	/* command is exact match for exit */
			*match_args = 0;
			*match_cmdname = 0;
		} else {
			if (tp_enable_prefix) {
				if (*command == SAY_TOKEN) {
					snprintf(pbuf, sizeof(pbuf), "say %s", command + 1);
					command = &pbuf[0];
				} else if (*command == POSE_TOKEN) {
					snprintf(pbuf, sizeof(pbuf), "pose %s", command + 1);
					command = &pbuf[0];
				} else if (*command == EXIT_DELIMITER) {
					snprintf(pbuf, sizeof(pbuf), "delimiter %s", command + 1);
					command = &pbuf[0];
				} else {
					goto bad_pre_command;
				}
				if (can_move(descr, player, command, 0)) {
					do_move(descr, player, command, 0);	/* command is exact match for exit */
					*match_args = 0;
					*match_cmdname = 0;
				} else {
					goto bad_pre_command;
				}
			} else {
				goto bad_pre_command;
			}
		}
	} else {
	  bad_pre_command:
		if (TrueWizard(OWNER(player)) && (*command == OVERIDE_TOKEN))
			command++;
		full_command = strcpyn(xbuf, sizeof(xbuf), command);
		for (; *full_command && !isspace(*full_command); full_command++) ;
		if (*full_command)
			full_command++;

		/* find arg1 -- move over command word */
		command = strcpyn(ybuf, sizeof(ybuf), command);
		for (arg1 = command; *arg1 && !isspace(*arg1); arg1++) ;
		/* truncate command */
		if (*arg1)
			*arg1++ = '\0';

		/* remember command for programs */
		strcpyn(match_args, sizeof(match_args), full_command);
		strcpyn(match_cmdname, sizeof(match_cmdname), command);

		/* move over spaces */
		while (*arg1 && isspace(*arg1))
			arg1++;

		/* find end of arg1, start of arg2 */
		for (arg2 = arg1; *arg2 && *arg2 != ARG_DELIMITER; arg2++) ;

		/* truncate arg1 */
		for (p = arg2 - 1; p >= arg1 && isspace(*p); p--)
			*p = '\0';

		/* go past delimiter if present */
		if (*arg2)
			*arg2++ = '\0';
		while (*arg2 && isspace(*arg2))
			arg2++;

		switch (command[0]) {
		case '@':
			switch (command[1]) {
			case 'a':
			case 'A':
				/* @action, @armageddon, @attach */
				switch (command[2]) {
				case 'c':
				case 'C':
					Matched("@action");
					NOGUEST("@action", player);
					BUILDERONLY("@action", player);
					do_action(descr, player, arg1, arg2);
					break;
				case 'r':
				case 'R':
					if (strcmp(command, "@armageddon"))
						goto bad;
/*
					WIZARDONLY("@armageddon", player);
					PLAYERONLY("@armageddon", player);
*/
					do_armageddon(player, full_command);
					break;
				case 't':
				case 'T':
					Matched("@attach");
					NOGUEST("@attach", player);
					BUILDERONLY("@attach", player);
					do_attach(descr, player, arg1, arg2);
					break;
				default:
					goto bad;
				}
				break;
			case 'b':
			case 'B':
				/* @bless, @boot */
				switch (command[2]) {
				case 'l':
				case 'L':
					Matched("@bless");
					WIZARDONLY("@bless", player);
					PLAYERONLY("@bless", player);
					NOFORCE("@bless", force_level, player);
					do_bless(descr, player, arg1, arg2);
					break;
				case 'o':
				case 'O':
					Matched("@boot");
					WIZARDONLY("@boot", player);
					PLAYERONLY("@boot", player);
					do_boot(player, arg1);
					break;
				default:
					goto bad;
				}
				break;
			case 'c':
			case 'C':
				/* @chlock, @chown, @chown_lock, @clone,
				   @conlock, @contents, @create, @credits */
				switch (command[2]) {
				case 'h':
				case 'H':
					switch (command[3]) {
					case 'l':
					case 'L':
						Matched("@chlock");
						NOGUEST("@chlock", player);
						set_standard_lock(descr, player, arg1, MESGPROP_CHLOCK, "Chown Lock", arg2);
						break;
					case 'o':
					case 'O':
						if(strlen(command) < 7) {
							Matched("@chown");
							do_chown(descr, player, arg1, arg2);
						} else {
							Matched("@chown_lock");
							NOGUEST("@chown_lock", player);
							set_standard_lock(descr, player, arg1, MESGPROP_CHLOCK, "Chown Lock", arg2);
						}
						break;
					default:
						goto bad;
					}
					break;
				case 'l':
				case 'L':
					Matched("@clone");
					NOGUEST("@clone", player);
					BUILDERONLY("@clone", player);
					do_clone(descr, player, arg1);
					break;
				case 'o':
				case 'O':
					switch (command[4]) {
					case 'l':
					case 'L':
						Matched("@conlock");
						NOGUEST("@conlock", player);
						set_standard_lock(descr, player, arg1, MESGPROP_CONLOCK, "Container Lock", arg2);
						break;
					case 't':
					case 'T':
						Matched("@contents");
						do_contents(descr, player, arg1, arg2);
						break;
					default:
						goto bad;
					}
					break;
				case 'r':
				case 'R':
					if (string_compare(command, "@credits")) {
						Matched("@create");
						NOGUEST("@create", player);
						BUILDERONLY("@create", player);
						do_create(player, arg1, arg2);
					} else {
						do_credits(player);
					}
					break;
				default:
					goto bad;
				}
				break;
			case 'd':
			case 'D':
				/* @dbginfo, @describe, @dig, @doing,
				   @drop, @dump */
				switch (command[2]) {
#ifdef DISKBASE
				case 'b':
				case 'B':
					Matched("@dbginfo");
					WIZARDONLY("@dbginfo", player);
					diskbase_debug(player);
					break;
#endif
				case 'e':
				case 'E':
					Matched("@describe");
					NOGUEST("@describe", player);
					set_standard_property(descr, player, arg1, MESGPROP_DESC, "Object Description", arg2);
					break;
				case 'i':
				case 'I':
					Matched("@dig");
					NOGUEST("@dig", player);
					BUILDERONLY("@dig", player);
					do_dig(descr, player, arg1, arg2);
					break;
				case 'o':
				case 'O':
					Matched("@doing");
					if (!tp_who_doing)
						goto bad;
					NOGUEST("@doing", player);
					set_standard_property(descr, player, arg1, MESGPROP_DOING, "Doing", arg2);
					break;
				case 'r':
				case 'R':
					Matched("@drop");
					NOGUEST("@drop", player);
					set_standard_property(descr, player, arg1, MESGPROP_DROP, "Drop Message", arg2);
					break;
				case 'u':
				case 'U':
					Matched("@dump");
					WIZARDONLY("@dump", player);
					PLAYERONLY("@dump", player);
					do_dump(player, full_command);
					break;
				default:
					goto bad;
				}
				break;
			case 'e':
			case 'E':
				/* @edit, @entrances, @examine */
				switch (command[2]) {
				case 'd':
				case 'D':
					Matched("@edit");
					NOGUEST("@edit", player);
					PLAYERONLY("@edit", player);
					MUCKERONLY("@edit", player);
					do_edit(descr, player, arg1);
					break;
				case 'n':
				case 'N':
					Matched("@entrances");
					do_entrances(descr, player, arg1, arg2);
					break;
				case 'x':
				case 'X':
					Matched("@examine");
					GODONLY("@examine", player);
					sane_dump_object(player, arg1);
					break;
				default:
					goto bad;
				}
				break;
			case 'f':
			case 'F':
				/* @fail, @find, @flock, @force, @force_lock */
				switch (command[2]) {
				case 'a':
				case 'A':
					Matched("@fail");
					NOGUEST("@fail", player);
					set_standard_property(descr, player, arg1, MESGPROP_FAIL, "Fail Message", arg2);
					break;
				case 'i':
				case 'I':
					Matched("@find");
					do_find(player, arg1, arg2);
					break;
				case 'l':
				case 'L':
					Matched("@flock");
					NOGUEST("@flock", player);
					NOFORCE("@flock", force_level, player);
					set_standard_lock(descr, player, arg1, MESGPROP_FLOCK, "Force Lock", arg2);
					break;
				case 'o':
				case 'O':
					if(strlen(command) < 7) {
						Matched("@force");
						do_force(descr, player, arg1, arg2);
					} else {
						Matched("@force_lock");
						NOGUEST("@force_lock", player);
						NOFORCE("@force_lock", force_level, player);
						set_standard_lock(descr, player, arg1, MESGPROP_FLOCK, "Force Lock", arg2);
					}
					break;
				default:
					goto bad;
				}
				break;
			case 'h':
			case 'H':
				/* @hashes */
				Matched("@hashes");
				do_hashes(player, arg1);
				break;
			case 'i':
			case 'I':
				/* @idescribe */
				Matched("@idescribe");
				NOGUEST("@idescribe", player);
				set_standard_property(descr, player, arg1, MESGPROP_IDESC, "Inside Description", arg2);
				break;
			case 'k':
			case 'K':
				/* @kill */
				Matched("@kill");
				do_dequeue(descr, player, arg1);
				break;
			case 'l':
			case 'L':
				/* @link, @list, @lock */
				switch (command[2]) {
				case 'i':
				case 'I':
					switch (command[3]) {
					case 'n':
					case 'N':
						Matched("@link");
						NOGUEST("@link", player);
						do_link(descr, player, arg1, arg2);
						break;
					case 's':
					case 'S':
						Matched("@list");
						match_and_list(descr, player, arg1, arg2);
						break;
					default:
						goto bad;
					}
					break;
				case 'o':
				case 'O':
					Matched("@lock");
					NOGUEST("@lock", player);
					set_standard_lock(descr, player, arg1, MESGPROP_LOCK, "Lock", arg2);
					break;
				default:
					goto bad;
				}
				break;
			case 'm':
			case 'M':
				/* @mcpedit, @mcpprogram, @memory, @mpitops,
				   @muftops */
				switch (command[2]) {
#ifdef MCP_SUPPORT
				case 'c':
				case 'C':
					if (string_prefix("@mcpedit", command)) {
						Matched("@mcpedit");
						NOGUEST("@mcpedit", player);
						PLAYERONLY("@mcpedit", player);
						MUCKERONLY("@mcpedit", player);
						do_mcpedit(descr, player, arg1);
						break;
					} else {
						Matched("@mcpprogram");
						NOGUEST("@mcpprogram", player);
						PLAYERONLY("@mcpprogram", player);
						MUCKERONLY("@mcpprogram", player);
						do_mcpprogram(descr, player, arg1);
						break;
					}
#endif
#ifndef NO_MEMORY_COMMAND
				case 'e':
				case 'E':
					Matched("@memory");
					WIZARDONLY("@memory", player);
					do_memory(player);
					break;
#endif
				case 'p':
			    case 'P':
			        Matched("@mpitops");
				WIZARDONLY("@mpitops", player);
			        do_mpi_topprofs(player, arg1);
			        break;
			    case 'u':
			    case 'U':
			        Matched("@muftops");
				WIZARDONLY("@muftops", player);
			        do_muf_topprofs(player, arg1);
			        break;
				default:
					goto bad;
				}
				break;
			case 'n':
			case 'N':
				/* @name, @newpassword */
				switch (command[2]) {
				case 'a':
				case 'A':
					Matched("@name");
					NOGUEST("@name", player);
					do_name(descr, player, arg1, arg2);
					break;
				case 'e':
				case 'E':
					if (strcmp(command, "@newpassword"))
						goto bad;
					WIZARDONLY("@newpassword", player);
					PLAYERONLY("@newpassword", player);
					do_newpassword(player, arg1, arg2);
					break;
				default:
					goto bad;
				}
				break;
			case 'o':
			case 'O':
				/* @odrop, @oecho, @ofail, @open, @osuccess,
				   @owned */
				switch (command[2]) {
				case 'd':
				case 'D':
					Matched("@odrop");
					NOGUEST("@odrop", player);
					set_standard_property(descr, player, arg1, MESGPROP_ODROP, "ODrop Message", arg2);
					break;
				case 'e':
				case 'E':
					Matched("@oecho");
					NOGUEST("@oecho", player);
					set_standard_property(descr, player, arg1, MESGPROP_OECHO, "Outside-echo Prefix", arg2);
					break;
				case 'f':
				case 'F':
					Matched("@ofail");
					NOGUEST("@ofail", player);
					set_standard_property(descr, player, arg1, MESGPROP_OFAIL, "OFail Message", arg2);
					break;
				case 'p':
				case 'P':
					Matched("@open");
					NOGUEST("@open", player);
					BUILDERONLY("@open", player);
					do_open(descr, player, arg1, arg2);
					break;
				case 's':
				case 'S':
					Matched("@osuccess");
					NOGUEST("@osuccess", player);
					set_standard_property(descr, player, arg1, MESGPROP_OSUCC, "OSuccess Message", arg2);
					break;
				case 'w':
				case 'W':
					Matched("@owned");
					do_owned(player, arg1, arg2);
					break;
				default:
					goto bad;
				}
				break;
			case 'p':
			case 'P':
				/* @password, @pcreate, @pecho, @program, 
				   @propset, @ps */
				switch (command[2]) {
				case 'a':
				case 'A':
					Matched("@password");
					PLAYERONLY("@password", player);
					NOGUEST("@password", player);
					do_password(player, arg1, arg2);
					break;
				case 'c':
				case 'C':
					Matched("@pcreate");
					WIZARDONLY("@pcreate", player);
					PLAYERONLY("@pcreate", player);
					do_pcreate(player, arg1, arg2);
					break;
				case 'e':
				case 'E':
					Matched("@pecho");
					NOGUEST("@pecho", player);
					set_standard_property(descr, player, arg1, MESGPROP_PECHO, "Puppet-echo Prefix", arg2);
					break;
				case 'r':
				case 'R':
					if (string_prefix("@program", command)) {
						Matched("@program");
						NOGUEST("@program", player);
						PLAYERONLY("@program", player);
						MUCKERONLY("@program", player);
						do_prog(descr, player, arg1);
						break;
					} else {
						Matched("@propset");
						NOGUEST("@propset", player);
						do_propset(descr, player, arg1, arg2);
						break;
					}
				case 's':
				case 'S':
					Matched("@ps");
					list_events(player);
					break;
				default:
					goto bad;
				}
				break;
			case 'r':
			case 'R':
				/* @recycle, @reconfiguressl, @relink, @restart, @restrict */
				switch (command[3]) {
				case 'c':
				case 'C':
#ifdef USE_SSL
                                        if (!strcmp(command, "@reconfiguressl")) {
                                                WIZARDONLY("@reconfiguressl", player);
                                                PLAYERONLY("@reconfiguressl", player);
                                                do_reconfigure_ssl(player);
                                                break;
                                        }
#endif
                                        Matched("@recycle");
                                        NOGUEST("@recycle", player);
                                        do_recycle(descr, player, arg1);
                                        break;
				case 'l':
				case 'L':
					Matched("@relink");
					NOGUEST("@relink", player);
					do_relink(descr, player, arg1, arg2);
					break;
				case 's':
				case 'S':
					if (!strcmp(command, "@restart")) {
/*
						WIZARDONLY("@restart", player);
						PLAYERONLY("@restart", player);
*/
						do_restart(player);
					} else if (!strcmp(command, "@restrict")) {
						WIZARDONLY("@restrict", player);
						PLAYERONLY("@restrict", player);
						do_restrict(player, arg1);
					} else {
						goto bad;
					}
					break;
				default:
					goto bad;
				}
				break;
			case 's':
			case 'S':
				/* @sanity, @sanchange, @sanfix, @set,
				   @shutdown, @stats, @success, @sweep */
				switch (command[2]) {
				case 'a':
				case 'A':
					if (!strcmp(command, "@sanity")) {
						GODONLY("@sanity", player);
						sanity(player);
					} else if (!strcmp(command, "@sanchange")) {
						GODONLY("@sanchange", player);
						sanechange(player, full_command);
					} else if (!strcmp(command, "@sanfix")) {
						GODONLY("@sanfix", player);
						sanfix(player);
					} else {
						goto bad;
					}
					break;
				case 'e':
				case 'E':
					Matched("@set");
					NOGUEST("@set", player);
					do_set(descr, player, arg1, arg2);
					break;
				case 'h':
				case 'H':
					if (strcmp(command, "@shutdown"))
						goto bad;
/*
					WIZARDONLY("@shutdown", player);
					PLAYERONLY("@shutdown", player);
*/
					do_shutdown(player);
					break;
				case 't':
				case 'T':
					Matched("@stats");
					do_stats(player, arg1);
					break;
				case 'u':
				case 'U':
					Matched("@success");
					NOGUEST("@success", player);
					set_standard_property(descr, player, arg1, MESGPROP_SUCC, "Success Message", arg2);
					break;
				case 'w':
				case 'W':
					Matched("@sweep");
					do_sweep(descr, player, arg1);
					break;
				default:
					goto bad;
				}
				break;
			case 't':
			case 'T':
				/* @teleport, @toad, @trace, @tune */
				switch (command[2]) {
				case 'e':
				case 'E':
					Matched("@teleport");
					do_teleport(descr, player, arg1, arg2);
					break;
				case 'o':
				case 'O':
					if (!strcmp(command, "@toad")) {
						WIZARDONLY("@toad", player);
						PLAYERONLY("@toad", player);
						do_toad(descr, player, arg1, arg2);
					} else if (!strcmp(command, "@tops")) {
						WIZARDONLY("@tops", player);
						do_all_topprofs(player, arg1);
					} else {
						goto bad;
					}
					break;
				case 'r':
				case 'R':
					Matched("@trace");
					do_trace(descr, player, arg1, atoi(arg2));
					break;
				case 'u':
				case 'U':
					Matched("@tune");
					WIZARDONLY("@tune", player);
					PLAYERONLY("@tune", player);
					do_tune(player, arg1, arg2, !!strchr(full_command, ARG_DELIMITER));
					break;
				default:
					goto bad;
				}
				break;
			case 'u':
			case 'U':
				/* @unbless, @unlink, @unlock, @uncompile,
				   @usage */
				switch (command[2]) {
				case 'N':
				case 'n':
					if (string_prefix(command, "@unb")) {
						Matched("@unbless");
						WIZARDONLY("@unbless", player);
						PLAYERONLY("@unbless", player);
						NOFORCE("@unbless", force_level, player);
						do_unbless(descr, player, arg1, arg2);
					} else if (string_prefix(command, "@unli")) {
						Matched("@unlink");
						NOGUEST("@unlink", player);
						do_unlink(descr, player, arg1);
					} else if (string_prefix(command, "@unlo")) {
						Matched("@unlock");
						NOGUEST("@unlock", player);
						set_standard_lock(descr, player, arg1, MESGPROP_LOCK, "Lock", "");
					} else if (string_prefix(command, "@uncom")) {
						Matched("@uncompile");
						WIZARDONLY("@uncompile", player);
						PLAYERONLY("@uncompile", player);
						do_uncompile(player);
					} else {
						goto bad;
					}
					break;
#ifndef NO_USAGE_COMMAND
				case 'S':
				case 's':
					Matched("@usage");
					WIZARDONLY("@usage", player);
					do_usage(player);
					break;
#endif
				default:
					goto bad;
					break;
				}
				break;
			case 'v':
			case 'V':
				/* @version */
				Matched("@version");
				do_version(player);
				break;
			case 'w':
			case 'W':
				/* @wall */
				if (strcmp(command, "@wall"))
					goto bad;
				WIZARDONLY("@wall", player);
				PLAYERONLY("@wall", player);
				do_wall(player, full_command);
				break;
			default:
				goto bad;
			}
			break;
		case 'd':
		case 'D':
			/* disembark, drop */
			switch (command[1]) {
			case 'i':
			case 'I':
				Matched("disembark");
				do_leave(descr, player);
				break;
			case 'r':
			case 'R':
				Matched("drop");
				do_drop(descr, player, arg1, arg2);
				break;
			default:
				goto bad;
			}
			break;
		case 'e':
		case 'E':
			/* examine */
			Matched("examine");
			do_examine(descr, player, arg1, arg2);
			break;
		case 'g':
		case 'G':
			/* get, give, goto, gripe */
			switch (command[1]) {
			case 'e':
			case 'E':
				Matched("get");
				do_get(descr, player, arg1, arg2);
				break;
			case 'i':
			case 'I':
				Matched("give");
				do_give(descr, player, arg1, atoi(arg2));
				break;
			case 'o':
			case 'O':
				Matched("goto");
				do_move(descr, player, arg1, 0);
				break;
			case 'r':
			case 'R':
				if (string_compare(command, "gripe"))
					goto bad;
				do_gripe(player, full_command);
				break;
			default:
				goto bad;
			}
			break;
		case 'h':
		case 'H':
			/* help */
			Matched("help");
			do_help(player, arg1, arg2);
			break;
		case 'i':
		case 'I':
			/* inventory, info */
			if (string_compare(command, "info")) {
				Matched("inventory");
				do_inventory(player);
			} else {
				Matched("info");
				do_info(player, arg1, arg2);
			}
			break;
		case 'k':
		case 'K':
			/* kill */
			Matched("kill");
			do_kill(descr, player, arg1, atoi(arg2));
			break;
		case 'l':
		case 'L':
			/* leave, look */
			if (string_prefix("look", command)) {
				Matched("look");
				do_look_at(descr, player, arg1, arg2);
				break;
			} else {
				Matched("leave");
				do_leave(descr, player);
				break;
			}
		case 'm':
		case 'M':
			/* man, motd, move, mpi */
			if (string_prefix(command, "move")) {
				do_move(descr, player, arg1, 0);
				break;
			} else if (!string_compare(command, "motd")) {
				do_motd(player, full_command);
				break;
			} else if (!string_compare(command, "mpi")) {
				do_mpihelp(player, arg1, arg2);
				break;
			} else {
				if (string_compare(command, "man"))
					goto bad;
				do_man(player, (!*arg1 && !*arg2 && arg1 != arg2) ? "=" : arg1, arg2);
			}
			break;
		case 'n':
		case 'N':
			/* news */
			Matched("news");
			do_news(player, arg1, arg2);
			break;
		case 'p':
		case 'P':
			/* page, pose, put */
			switch (command[1]) {
			case 'a':
			case 'A':
				Matched("page");
				do_page(player, arg1, arg2);
				break;
			case 'o':
			case 'O':
				Matched("pose");
				do_pose(player, full_command);
				break;
			case 'u':
			case 'U':
				Matched("put");
				do_drop(descr, player, arg1, arg2);
				break;
			default:
				goto bad;
			}
			break;
		case 'r':
		case 'R':
			/* read, rob */
			switch (command[1]) {
			case 'e':
			case 'E':
				Matched("read");	/* undocumented alias for look */
				do_look_at(descr, player, arg1, arg2);
				break;
			case 'o':
			case 'O':
				Matched("rob");
				do_rob(descr, player, arg1);
				break;
			default:
				goto bad;
			}
			break;
		case 's':
		case 'S':
			/* say, score */
			switch (command[1]) {
			case 'a':
			case 'A':
				Matched("say");
				do_say(player, full_command);
				break;
			case 'c':
			case 'C':
				Matched("score");
				do_score(player);
				break;
			default:
				goto bad;
			}
			break;
		case 't':
		case 'T':
			/* take, throw */
			switch (command[1]) {
			case 'a':
			case 'A':
				Matched("take");
				do_get(descr, player, arg1, arg2);
				break;
			case 'h':
			case 'H':
				Matched("throw");
				do_drop(descr, player, arg1, arg2);
				break;
			default:
			goto bad;
			}
			break;
		case 'w':
		case 'W':
			/* whisper */
			Matched("whisper");
			do_whisper(descr, player, arg1, arg2);
			break;
		default:
		  bad:
			if (tp_m3_huh != 0)
			{
				char hbuf[BUFFER_LEN];
				snprintf(hbuf,BUFFER_LEN,"HUH? %s", command);
				if(can_move(descr, player, hbuf, 3)) {
					do_move(descr, player, hbuf, 3);
					*match_args = 0;
					*match_cmdname = 0;
					break;
				}
			}	
			notify(player, tp_huh_mesg);
			if (tp_log_failed_commands && !controls(player, LOCATION(player))) {
				log_status("HUH from %s(%d) in %s(%d)[%s]: %s %s",
						   NAME(player), player, NAME(LOCATION(player)),
						   LOCATION(player),
						   NAME(OWNER(LOCATION(player))), command, full_command);
			}
			break;
		}
	}

	/* calculate time command took. */
	gettimeofday(&endtime, NULL);
	if (starttime.tv_usec > endtime.tv_usec) {
		endtime.tv_usec += 1000000;
		endtime.tv_sec -= 1;
	}
	endtime.tv_usec -= starttime.tv_usec;
	endtime.tv_sec -= starttime.tv_sec;

	totaltime = endtime.tv_sec + (endtime.tv_usec * 1.0e-6);
	if (totaltime > (tp_cmd_log_threshold_msec / 1000.0)) {
		log2file(LOG_CMD_TIMES, "%6.3fs, %.16s: %s: %s",
					totaltime, ctime((time_t *)&starttime.tv_sec),
					whowhere(player), command);
	}
}
Esempio n. 20
0
/**
 * main program.  Processes all files and returns one string
 * for each file in strings[]. In the process, all strings are
 * used to construct the trie we use to get the substring with
 * great f value.
 */
int main (int argc, char **argv)
{

    extern int optind;
    extern char *optarg;
    int opt;
    int i;
    int n_passes = MAX_PASSES;
    FILE *out = stdout;
    time_t now;

    time(&now);
    while ((opt = getopt(argc, argv, "dhPp:o:s")) != EOF) {
        switch(opt) {
        case 'h': do_usage(); exit(0);
        case 'P': flags |= FLAG_PROGRESS; break;
        case 'p': n_passes = atol(optarg); break;
        case 'd': flags |= FLAG_DEBUG; break;
        case 'o': out = fopen(optarg, "wb"); break;
        case 's': flags |= FLAG_PRINT_STATE; break;
        } /* switch */
    } /* while */

    if (n_passes > MAX_PASSES) n_passes = MAX_PASSES;
    if (n_passes < 1) n_passes = 1;

    if (argc > optind) {
        for (i = optind; i < argc; i++)
            process_file(argv[i]);
    } else  process_file(stdin_name);

    mark = strings_n; /* beginning of macros */

    /* print the strings in the begining */
    if (flags & FLAG_PRINT_STATE)
        print_strings();

    for(i = 0; i < n_passes; i++) {
        struct trie_node *root_trie, *max;
        int j;
        char *o;
        struct ref_buff *ref;

        if (flags & FLAG_DEBUG) {
            fprintf(stderr, D("PASS #%d:\n"), i);
        } /* if */

        /* INITIALIZE THE TRIE */
        assert(root_trie = new_trie());
        for (j = 0; j < strings_n; j++) {
            const byte *s;
            int l;

            for (s = strings[j], l = strings_sz[j]; l; s++, l--) {
                add_string(s, l, root_trie, j);
                if (*s == ESCAPE) {
                    s++; l--;
                } /* if */
            } /* for */
            if (flags & FLAG_PROGRESS) {
                static char *progress[] = {
                    "\\", "|", "/", "-",
                };
                fprintf(stderr,
                    "\r%s %d/%d",
                    progress[j % 4], j+1, strings_n);
            } /* if */
        } /* for */

        /* SEARCH FOR THE MOST EFFICIENT MACRO SUBSTITUTION */
        max = walk_trie(root_trie, savings_calculation);

        /* IF NOT FOUND, FINISH */
        if (max == root_trie) {
            if (flags & FLAG_DEBUG) {
                fprintf(stderr,
                    D("MACRO NOT FOUND, FINISHING\n"));
            } /* if */
            break;
        } /* if */

        if (flags & FLAG_DEBUG) {
            /* WRITE THE MACRO FOUND */
            fprintbuf(stderr,
                max->l, max->refs->b,
                D("MACRO FOUND: len=%d, nrep=%d, savings=%d"),
                max->l, max->n, savings_calculation(max));
        } /* if */

        /* copy the string macro as a new string. */
        strings[strings_n] = buffer + bs;
        memcpy(strings[strings_n], max->refs->b, max->l);
        bs += max->l;
        strings_sz[strings_n] = max->l;
        strings_n++;

        /* print the substitutions to be made. */
        if (flags & FLAG_DEBUG) {

#define FOREACHSUBST(X) \
            for (ref = max->refs; ref; ref = ref->nxt) { \
                int          ix = ref->ix; \
                const byte *src = ref->b + max->l; \
                byte       *dst = (byte *)ref->b; \
                const byte *end = strings[ix] + strings_sz[ix]; \
                int           n = end - src; \
                X \
            } /* for */

            FOREACHSUBST(
                    fprintf(stderr,
                        D("SUBST: string[%d], beg_hole=0x%lx, end_hole=0x%lx, hole_sz=%ld, end=0x%lx\n"),
                        ix, dst - strings[ix], src - strings[ix], src - dst, end - strings[ix]);
                ) /* FOREACHSUBST */
        } /* if */

        /* substitute the strings as macro calls */
        FOREACHSUBST(

                assert((strings[ix] <= dst) && (dst + MACRO_SIZE < src) && (src <= end));

                *dst++ = ESCAPE;
                *dst++ = i + OFFSET; /* i is the macro index */
                assert(n >= 0);
                while (n--) *dst++ = *src++;
                strings_sz[ix] -= max->l - MACRO_SIZE;

            ) /* FOREACHSUBST */
Esempio n. 21
0
static void
parse_cmdline(int argc, char * const argv[])
{
	int ch;
	int index = 0;

	struct option options[] = {
		{"help", no_argument, NULL, 'h'},
		{"version", no_argument, NULL, 'V'},
		{"geometry", required_argument, NULL, 'g'},
		{"resolution", required_argument, NULL, 'r'},
		{"center", required_argument, NULL, 'c'},
		{"width", required_argument, NULL, 'w'},
		{"height", required_argument, NULL, 'H'},
		{"method", required_argument, NULL, 'm'},
		{"nthreads", required_argument, NULL, 'n'},
		{"output", required_argument, NULL, 'o'},
	};

	while ((ch = getopt_long(argc, argv, 
	                         "hc:H:m:n:o:r:w:g:V", options, &index)) != -1) {
		switch (ch) {
		case 'h':
			do_usage(argv[0], 0);
			break;
		case 'V':
			do_version(argv[0]);
			break;
		case 'g':
			do_geometry(argv[0], optarg);
			break;
		case 'r':
			do_resolution(argv[0], optarg);
			break;
		case 'c':
			do_center(argv[0], optarg);
			break;
		case 'w':
			do_width(argv[0], optarg);
			break;
		case 'H':
			do_height(argv[0], optarg);
			break;
		case 'm':
			do_method(argv[0], optarg);
			break;
		case 'n':
			do_nthreads(argv[0], optarg);
			break;
		case 'o':
			do_output(argv[0], optarg);
			break;
		default:
			do_usage(argv[0], 1);
		}
	}

	if (plot == NULL)
		do_method(argv[0], "generic");
	if (!output) {
		fprintf(stderr, "no output file.\n");
		exit(1);
	}
}
Esempio n. 22
0
File: watch.c Progetto: Romutk/lab5
int
main(int argc, char *argv[])
{
	int optc;
	int option_differences = 0,
	    option_differences_cumulative = 0,
			option_exec = 0,
			option_beep = 0,
        option_errexit = 0,
	    option_help = 0, option_version = 0;
	double interval = 2;
	char *command;
	char **command_argv;
	int command_length = 0;	/* not including final \0 */
    watch_usec_t next_loop; /* next loop time in us, used for precise time
                               keeping only */
	int pipefd[2];
	int status;
	pid_t child;

	setlocale(LC_ALL, "");
	progname = argv[0];

	while ((optc = getopt_long(argc, argv, "+bed::hn:pvtx", longopts, (int *) 0))
	       != EOF) {
		switch (optc) {
		case 'b':
			option_beep = 1;
			break;
		case 'd':
			option_differences = 1;
			if (optarg)
				option_differences_cumulative = 1;
			break;
        case 'e':
            option_errexit = 1;
            break;
		case 'h':
			option_help = 1;
			break;
		case 't':
			show_title = 0;
			break;
		case 'x':
		  option_exec = 1;
			break;
		case 'n':
			{
				char *str;
				interval = strtod(optarg, &str);
				if (!*optarg || *str)
					do_usage();
				if(interval < 0.1)
					interval = 0.1;
				if(interval > ~0u/1000000)
					interval = ~0u/1000000;
			}
			break;
		case 'p':
			precise_timekeeping = 1;
			break;
		case 'v':
			option_version = 1;
			break;
		default:
			do_usage();
			break;
		}
	}

	if (option_version) {
		fprintf(stderr, "%s\n", VERSION);
		if (!option_help)
			exit(0);
	}

	if (option_help) {
		fprintf(stderr, usage, progname);
		fputs("  -b, --beep\t\t\t\tbeep if the command has a non-zero exit\n", stderr);
		fputs("  -d, --differences[=cumulative]\thighlight changes between updates\n", stderr);
		fputs("\t\t(cumulative means highlighting is cumulative)\n", stderr);
		fputs("  -e, --errexit\t\t\t\texit watch if the command has a non-zero exit\n", stderr);
		fputs("  -h, --help\t\t\t\tprint a summary of the options\n", stderr);
		fputs("  -n, --interval=<seconds>\t\tseconds to wait between updates\n", stderr);
        fputs("  -p, --precise\t\t\t\tprecise timing, ignore command run time\n", stderr);
		fputs("  -v, --version\t\t\t\tprint the version number\n", stderr);
		fputs("  -t, --no-title\t\t\tturns off showing the header\n", stderr);
		fputs("  -x, --exec\t\t\t\tpass command to exec instead of sh\n", stderr);
		exit(0);
	}

	if (optind >= argc)
		do_usage();

	command_argv=&(argv[optind]); /* save for later */

	command = strdup(argv[optind++]);
	command_length = strlen(command);
	for (; optind < argc; optind++) {
		char *endp;
		int s = strlen(argv[optind]);
		command = realloc(command, command_length + s + 2);	/* space and \0 */
		endp = command + command_length;
		*endp = ' ';
		memcpy(endp + 1, argv[optind], s);
		command_length += 1 + s;	/* space then string length */
		command[command_length] = '\0';
	}

	get_terminal_size();

	/* Catch keyboard interrupts so we can put tty back in a sane state.  */
	signal(SIGINT, die);
	signal(SIGTERM, die);
	signal(SIGHUP, die);
	signal(SIGWINCH, winch_handler);

	/* Set up tty for curses use.  */
	curses_started = 1;
	initscr();
	nonl();
	noecho();
	cbreak();

	if (precise_timekeeping)
		next_loop = get_time_usec();

	for (;;) {
		time_t t = time(NULL);
		char *ts = ctime(&t);
		int tsl = strlen(ts);
		char *header;
		FILE *p;
		int x, y;
		int oldeolseen = 1;

		if (screen_size_changed) {
			get_terminal_size();
			resizeterm(height, width);
			clear();
			/* redrawwin(stdscr); */
			screen_size_changed = 0;
			first_screen = 1;
		}

		if (show_title) {
			// left justify interval and command,
			// right justify time, clipping all to fit window width
			asprintf(&header, "Every %.1fs: %.*s",
				interval, min(width - 1, command_length), command);
			mvaddstr(0, 0, header);
			if (strlen(header) > (size_t) (width - tsl - 1))
				mvaddstr(0, width - tsl - 4, "...  ");
			mvaddstr(0, width - tsl + 1, ts);
			free(header);
		}

		/* allocate pipes */
		if (pipe(pipefd)<0) {
		  perror("pipe");
			do_exit(7);
	  }

		/* flush stdout and stderr, since we're about to do fd stuff */
		fflush(stdout);
		fflush(stderr);

		/* fork to prepare to run command */
		child=fork();

		if (child<0) { /* fork error */
		  perror("fork");
			do_exit(2);
		} else if (child==0) { /* in child */
			close (pipefd[0]); /* child doesn't need read side of pipe */
			close (1); /* prepare to replace stdout with pipe */
			if (dup2 (pipefd[1], 1)<0) { /* replace stdout with write side of pipe */
			  perror("dup2");
				exit(3);
			}
			dup2(1, 2); /* stderr should default to stdout */

			if (option_exec) { /* pass command to exec instead of system */
			  if (execvp(command_argv[0], command_argv)==-1) {
				  perror("exec");
				  exit(4);
				}
			} else {
		    status=system(command); /* watch manpage promises sh quoting */

			  /* propagate command exit status as child exit status */
			  if (!WIFEXITED(status)) { /* child exits nonzero if command does */
			    exit(1);
			  } else {
			    exit(WEXITSTATUS(status));
		    }
			}

		}

		/* otherwise, we're in parent */
		close(pipefd[1]); /* close write side of pipe */
		if ((p=fdopen(pipefd[0], "r"))==NULL) {
		  perror("fdopen");
			do_exit(5);
		}


		for (y = show_title; y < height; y++) {
			int eolseen = 0, tabpending = 0;
			for (x = 0; x < width; x++) {
				int c = ' ';
				int attr = 0;

				if (!eolseen) {
					/* if there is a tab pending, just spit spaces until the
					   next stop instead of reading characters */
					if (!tabpending)
						do
							c = getc(p);
						while (c != EOF && !isprint(c)
						       && c != '\n'
						       && c != '\t');
					if (c == '\n')
						if (!oldeolseen && x == 0) {
							x = -1;
							continue;
						} else
							eolseen = 1;
					else if (c == '\t')
						tabpending = 1;
					if (c == EOF || c == '\n' || c == '\t')
						c = ' ';
					if (tabpending && (((x + 1) % 8) == 0))
						tabpending = 0;
				}
				move(y, x);
				if (option_differences) {
					chtype oldch = inch();
					unsigned char oldc = oldch & A_CHARTEXT;
					attr = !first_screen
					    && ((char)c != oldc
						||
						(option_differences_cumulative
						 && (oldch & A_ATTRIBUTES)));
				}
				if (attr)
					standout();
				addch(c);
				if (attr)
					standend();
			}
			oldeolseen = eolseen;
		}

		fclose(p);

		/* harvest child process and get status, propagated from command */
		if (waitpid(child, &status, 0)<0) {
		  perror("waitpid");
			do_exit(8);
		};

		/* if child process exited in error, beep if option_beep is set */
		if ((!WIFEXITED(status) || WEXITSTATUS(status))) {
          if (option_beep) beep();
          if (option_errexit) do_exit(8);
		}

		first_screen = 0;
		refresh();
		if (precise_timekeeping) {
			watch_usec_t cur_time = get_time_usec();
			next_loop += USECS_PER_SEC*interval;
			if (cur_time < next_loop)
				usleep(next_loop - cur_time);
		} else
		usleep(interval * 1000000);
	}

	endwin();

	return 0;
}
Esempio n. 23
0
void parse_input_command(char *rl_cmd)
{
	int i;
	int argc = 0;
	int valid_cmd = 0;
	char *argv[MAX_ARGS];
	int last_arg_start = 0;
	int last_was_space = 0;
	int len;
	char *cmd;

	/* ^D quits */
	if (rl_cmd == NULL) {
		run_exit_cmd(0, NULL);
	}

	cmd = strdup(rl_cmd);

	/* Split cmd up into args
	 * destroying the original string mwahahahaha
	 */

	len = strlen(cmd);

	/* Span leading spaces */
	for (i=0; cmd[i] == ' '; i++)
		;
	last_arg_start = i;

	for (; i<=len; i++) {
		if (cmd[i] == ' ' || cmd[i] == '\0') {

			/* Allow multiple spaces */
			if (last_was_space) {
				continue;
			}

			cmd[i] = '\0';
			last_was_space = 1;

			argv[argc] = &cmd[last_arg_start];
			argc++;
		}
		else {
			if (last_was_space) {
				last_arg_start = i;
			}
			last_was_space = 0;
		}
	}

	/* Ignore null commands */
	if (strlen(argv[0]) == 0) {
		free(cmd);
	    return;
	}
#ifdef HAVE_READLINE_HISTORY_H
	add_history(rl_cmd);
#endif

	/* Dispatch command */
	for (i=0; i<num_cmds; i++) {
		if (strcasecmp(argv[0], cmd_list[i].cmd) == 0) {

			if (argc < cmd_list[i].min_args) {
				break;
			}
			cmd_list[i].cmd_runner(argc, argv);
			valid_cmd = 1;
		}
	}
	if (!valid_cmd) {
		do_usage();
	}
	free(cmd);
}
Esempio n. 24
0
void 
process_command(int descr, dbref player, char *command)
{
    char   *arg1;
    char   *arg2;
    char   *full_command, *commandline=command, *commandstuff;
    char   *p;                  /* utility */
    char    pbuf[BUFFER_LEN];
    char    xbuf[BUFFER_LEN];
    char    ybuf[BUFFER_LEN];
    const char *path;
    struct frame *tmpfr;

    if (command == 0)
	abort();

    /* robustify player */
    if (player < 0 || player >= db_top ||
	    (Typeof(player) != TYPE_PLAYER && Typeof(player) != TYPE_THING)) {
	log_status("process_command: bad player %d\n", player);
	return;
    }

    if ((tp_log_commands || (tp_log_guests && Guest(OWNER(player)))) &&
	    (!(FLAGS(player)&(INTERACTIVE | READMODE)))) {
	log_command("%s%s%s%s(%d) in %s(%d):%s %s\n",
		    Mage(OWNER(player)) ? "WIZ: " : "",
		    (Typeof(player) != TYPE_PLAYER) ? NAME(player) : "",
		    (Typeof(player) != TYPE_PLAYER) ? " by " : "",
		    NAME(OWNER(player)), (int) player,
		    NAME(DBFETCH(player)->location),
		    (int) DBFETCH(player)->location,
		    (FLAGS(player) & INTERACTIVE) ?
		    " [interactive]" : " ", command);
    }

    if (FLAGS(player) & INTERACTIVE) {
	interactive(descr, player, command);
	return;
    }
    /* eat leading whitespace */
    while (*command && isspace(*command))
	command++;

    commandstuff = command;
    /* check for single-character commands */
    if (*command == SAY_TOKEN || *command == '\'') {
	sprintf(pbuf, "say %s", command + 1);
	command = &pbuf[0];
    } else if (*command == POSE_TOKEN || *command == ';') {
	sprintf(pbuf, "pose %s", command + 1);
	command = &pbuf[0];
    } else if ((*command == '|' || (*commandstuff++ == '\\' && *commandstuff == '\\') ) && can_move(descr, player, "spoof", 0)) {
      if(*command = '\\')
         sprintf(pbuf, "spoof %s", command + 2);
      else
         sprintf(pbuf, "spoof %s", command + 1);
      command = &pbuf[0];
    }

    /* if player is a wizard, and uses overide token to start line...*/
    /* ... then do NOT run actions, but run the command they specify. */
    if (!strcmp(command, WHO_COMMAND)) {
       char xxbuf[BUFFER_LEN];

       strcpy(xxbuf, "@");
       strcat(xxbuf, WHO_COMMAND);
       strcat(xxbuf, " ");
       strcat(xxbuf, command + sizeof(WHO_COMMAND) - 1);
       if (can_move(descr, player, xxbuf, 5)) {
          do_move(descr, player, xxbuf, 5);
       } else {
          pdump_who_users(descr, command + sizeof(WHO_COMMAND) - 1);
       }
       return;
    }

    if (!( *command == OVERIDE_TOKEN && TMage(player) )) {
	if( can_move(descr, player, command, 0) ) {
	    do_move(descr, player, command, 0); /* command is exact match for exit */
	    *match_args = 0;
	    *match_cmdname = 0; 
	    return;
	}
    }

	if (*command == OVERIDE_TOKEN && TMage(player))
	    command++;

	full_command = strcpy(xbuf, command);
	for (; *full_command && !isspace(*full_command); full_command++);
	if (*full_command)
	    full_command++;

	/* find arg1 -- move over command word */
	command = strcpy(ybuf, command);
	for (arg1 = command; *arg1 && !isspace(*arg1); arg1++);
	/* truncate command */
	if (*arg1)
	    *arg1++ = '\0';

	/* remember command for programs */
	strcpy(match_cmdname, command);

	/* move over spaces */
	while (*arg1 && isspace(*arg1))
	    arg1++;

	/* find end of arg1, start of arg2 */
	for (arg2 = arg1; *arg2 && *arg2 != ARG_DELIMITER; arg2++);

	/* truncate arg1 */
	for (p = arg2 - 1; p >= arg1 && isspace(*p); p--)
	    *p = '\0';

	/* go past delimiter if present */
	if (*arg2)
	    *arg2++ = '\0';
	while (*arg2 && isspace(*arg2))
	    arg2++;

	strcpy(match_cmdname, command);
	strcpy(match_args, full_command);

	switch (command[0]) {
	    case '@':
		switch (command[1]) {
		    case 'a':
		    case 'A':
			/* @action, @attach */
			switch (command[2]) {
			    case 'c':
			    case 'C':
				Matched("@action");
				do_action(descr, player, arg1, arg2);
				break;
			    case 'r':
			    case 'R':
				if (string_compare(command, "@armageddon"))
				    goto bad;
				do_armageddon(player, full_command);
				break;
			    case 't':
			    case 'T':
				Matched("@attach");
				do_attach(descr, player, arg1, arg2);
				break;
                      case 'n':
                      case 'N':
                        Matched("@ansidescribe");
                        do_ansidescribe(descr, player, arg1, arg2);
                        break;
			    default:
				goto bad;
			}
			break;
		    case 'b':
		    case 'B':
			Matched("@boot");
			do_boot(player, arg1);
			break;
		    case 'c':
		    case 'C':
			/* chown, contents, create */
			switch (command[2]) {
			    case 'h':
			    case 'H':
				switch (command[3]) {
				    case 'l':
				    case 'L':
					Matched("@chlock");
					do_chlock(descr, player, arg1, arg2);
					break;
				    case 'o':
				    case 'O':
					Matched("@chown");
					do_chown(descr, player, arg1, arg2);
					break;
				    default:
					goto bad;
				}
				break;
			    case 'o':
			    case 'O':
				switch (command[4]) {
				    case 'l':
				    case 'L':
					Matched("@conlock");
					do_conlock(descr, player, arg1, arg2);
					break;
				    case 't':
				    case 'T':
					Matched("@contents");
					do_contents(descr, player, arg1, arg2);
					break;
				    default:
					goto bad;
				}
				break;
			    case 'r':
			    case 'R':
				if (string_compare(command, "@credits")) {
				    Matched("@create");
				    do_create(player, arg1, arg2);
				} else {
				    do_credits(player);
				}
				break;
			    default:
				goto bad;
			}
			break;
		    case 'd':
		    case 'D':
			/* describe, dequeue, dig, or dump */
			switch (command[2]) {
			    case 'b':
			    case 'B':
				Matched("@dbginfo");
				do_serverdebug(descr, player, arg1, arg2);
				break;
			    case 'e':
			    case 'E':
#ifdef DELTADUMPS
				if(command[3] == 'l' || command[3] == 'L') {
				    Matched("@delta");
				    do_delta(player);
				} else
#endif
				{
                          if( (command[3] == 's' || command[3] == 'S') &&
                              (command[4] == 't' || command[4] == 'T') ) {
                            Matched("@destroy");
 				    do_recycle(descr, player, arg1);
                          } else {
				    Matched("@describe");
				    do_describe(descr, player, arg1, arg2);
                          }
				}
				break;
			    case 'i':
			    case 'I':
				Matched("@dig");
				do_dig(descr, player, arg1, arg2);
				break;
#ifdef DELTADUMPS
			    case 'l':
			    case 'L':
				Matched("@dlt");
				do_delta(player);
				break;
#endif
			    case 'o':
			    case 'O':
				Matched("@doing");
				if (!tp_who_doing) goto bad;
				do_doing(descr, player, arg1, arg2);
				break;
			    case 'r':
			    case 'R':
				Matched("@drop");
				do_drop_message(descr, player, arg1, arg2);
				break;
			    case 'u':
			    case 'U':
				Matched("@dump");
				do_dump(player, full_command);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'e':
		    case 'E':
			switch (command[2]) {
			    case 'd':
			    case 'D':
				Matched("@edit");
				do_edit(descr, player, arg1);
				break;
			    case 'n':
			    case 'N':
				Matched("@entrances");
				do_entrances(descr, player, arg1, arg2);
				break;
			    case 'x':
			    case 'X':
				Matched("@examine");
				sane_dump_object(player, arg1);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'f':
		    case 'F':
			/* fail, find, force, or frob */
			switch (command[2]) {
			    case 'a':
			    case 'A':
				Matched("@fail");
				do_fail(descr, player, arg1, arg2);
				break;
			    case 'i':
			    case 'I':
				if(command[3] == 'x' || command[3] == 'X') {
				    Matched("@fixwizbits");
				    do_fixw(player, full_command);
				} else {
				    Matched("@find");
				    do_find(player, arg1, arg2);
				}
				break;
			    case 'l':
			    case 'L':
				Matched("@flock");
				do_flock(descr, player, arg1, arg2);
				break;
			    case 'o':
			    case 'O':
				Matched("@force");
				do_force(descr, player, arg1, arg2);
				break;
			    case 'r':
			    case 'R':
				if (string_compare(command, "@frob"))
				    goto bad;
				do_frob(descr, player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'h':
		    case 'H':
                  switch (command[2]) {
                     case 'T':
                     case 't':
                        Matched("@htmldescribe");
                        do_htmldescribe(descr, player, arg1, arg2);
                        break;
                     default:
 			      Matched("@hopper");
			      do_hopper(player, arg1);
                        break;
                  }
			break;
		    case 'i':
		    case 'I':
                  switch(command[2]) {
                     case 'h':
                     case 'H':
   	   		      Matched("@ihtmldescribe");
			      do_ihtmldescribe(descr, player, arg1, arg2);
			      break;
                     case 'a':
                     case 'A':
                        Matched("@iansidescribe");
                        do_iansidescribe(descr, player, arg1, arg2);
                        break;
                     default:
   			      Matched("@idescribe");
			      do_idescribe(descr, player, arg1, arg2);
			      break;
                  }
                  break;
		    case 'k':
		    case 'K':
			Matched("@kill");
			do_dequeue(descr, player, arg1);
			break;
		    case 'l':
		    case 'L':
			/* lock or link */
			switch (command[2]) {
			    case 'i':
			    case 'I':
				switch (command[3]) {
				    case 'n':
				    case 'N':
					Matched("@link");
					do_link(descr, player, arg1, arg2);
					break;
				    case 's':
				    case 'S':
					Matched("@list");
					match_and_list(descr, player, arg1, arg2, 1);
					break;
				    default:
					goto bad;
				}
				break;
			    case 'o':
			    case 'O':
				Matched("@lock");
				do_lock(descr, player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'm':
		    case 'M':
			switch (command[2]) {
			    case 'e':
			    case 'E':
				Matched("@memory");
				do_memory(player);
				break;
			    case 'c':
			    case 'C':
				switch (command[4]) {
				   case '2':
				     Matched("@mcp2muf");
				     match_and_list(descr, player, arg1, arg2, 0);
				     break;
				   default:
				     Matched("@mcp");
				     do_prog(descr, player, arg1, 1);
				     break;
				}
                     default:
                        goto bad;
			}
			break;
		    case 'n':
		    case 'N':
			/* @name or @newpassword */
			switch (command[2]) {
			    case 'a':
			    case 'A':
				Matched("@name");
				do_name(descr, player, arg1, arg2);
				break;
			    case 'e':
			    case 'E':
				if (string_compare(command, "@newpassword"))
				    goto bad;
				do_newpassword(player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'o':
		    case 'O':
			switch (command[2]) {
			    case 'd':
			    case 'D':
				Matched("@odrop");
				do_odrop(descr, player, arg1, arg2);
				break;
			    case 'e':
			    case 'E':
				Matched("@oecho");
				do_oecho(descr, player, arg1, arg2);
				break;
			    case 'f':
			    case 'F':
				Matched("@ofail");
				do_ofail(descr, player, arg1, arg2);
				break;
			    case 'p':
			    case 'P':
				Matched("@open");
				do_open(descr, player, arg1, arg2);
				break;
			    case 's':
			    case 'S':
				Matched("@osuccess");
				do_osuccess(descr, player, arg1, arg2);
				break;
			    case 'w':
			    case 'W':
				Matched("@owned");
				do_owned(player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'p':
		    case 'P':
			switch (command[2]) {
			    case 'a':
			    case 'A':
				Matched("@password");
				do_password(player, arg1, arg2);
				break;
			    case 'c':
			    case 'C':
				Matched("@pcreate");
				do_pcreate(player, arg1, arg2);
				break;
			    case 'e':
			    case 'E':
				Matched("@pecho");
				do_pecho(descr, player, arg1, arg2);
				break;
			    case 'r':
			    case 'R':
				if (string_prefix("@program", command)) {
				    do_prog(descr, player, arg1, 0);
				    break;
				} else if (string_prefix("@proginfo", command)) {
				    do_proginfo(player, arg1);
				    break;
				} else {
				    Matched("@propset");
				    do_propset(descr, player, arg1, arg2);
				    break;
				}
			    case 's':
			    case 'S':
				Matched("@ps");
				list_events(player);
				break;
			    case 'u':
			    case 'U':
				Matched("@purge");
				do_purge(descr, player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'r':
		    case 'R':
			switch (command[3]) {
			    case 'c':
			    case 'C':
				Matched("@recycle");
				do_recycle(descr, player, arg1);
				break;
			    case 's':
			    case 'S':
				if (!string_compare(command, "@restart")) {
				    do_restart(player, full_command);
				} else if (!string_compare(command, "@restrict")) {
				    do_restrict(player, arg1);
				} else {
				    goto bad;
				}
				break;
			    default:
				goto bad;
			}
			break;
		    case 's':
		    case 'S':
			/* set, shutdown, success */
			switch (command[2]) {
			    case 'a':
			    case 'A':
				if (!string_compare(command, "@sanity")) {
				    sanity(player);
				} else if (!string_compare(command, "@sanchange")) {
				    sanechange(player, full_command);
				} else if (!string_compare(command, "@sanfix")) {
				    sanfix(player);
				} else {
				    goto bad;
				}
				break;
			    case 'e':
			    case 'E':
				Matched("@set");
				do_set(descr, player, arg1, arg2);
				break;
			    case 'h':
			    case 'H':
				if (!string_compare(command, "@shout")) {
				    do_wall(player, full_command);
				    break;
				}
				if (string_compare(command, "@shutdown"))
				    goto bad;
				do_shutdown(player, arg1);
				break;
			    case 't':
			    case 'T':
				Matched("@stats");
				do_stats(player, arg1);
				break;
			    case 'u':
			    case 'U':
				Matched("@success");
				do_success(descr, player, arg1, arg2);
				break;
			    case 'w':
			    case 'W':
				Matched("@sweep");
				do_sweep(descr, player, arg1);
				break;
			    default:
				goto bad;
			}
			break;
		    case 't':
		    case 'T':
			switch (command[2]) {
			    case 'e':
			    case 'E':
				Matched("@teleport");
				do_teleport(descr, player, arg1, arg2);
				break;
                      case 'o':
                      case 'O':
				if (string_compare(command, "@toad"))
				    goto bad;
				do_frob(descr, player, arg1, arg2);
				break;
			    case 'r':
			    case 'R':
				Matched("@trace");
				do_trace(descr, player, arg1, atoi(arg2));
				break;
			    case 'u':
			    case 'U':
				Matched("@tune");
				do_tune(player, arg1, arg2);
				break;
			    default:
				goto bad;
			}
			break;
		    case 'u':
		    case 'U':
			switch (command[2]) {
			    case 'N':
			    case 'n':
				if (string_prefix(command, "@unlink")) {
				    do_unlink(descr, player, arg1);
				} else if (string_prefix(command, "@unlock")) {
				    do_unlock(descr, player, arg1);
				} else if (string_prefix(command, "@uncompile")) {
				    do_uncompile(player);
				} else {
				    goto bad;
				}
				break;

#ifndef NO_USAGE_COMMAND
			    case 'S':
			    case 's':
				Matched("@usage");
				do_usage(player);
				break;
#endif

			    default:
				goto bad;
				break;
			}
			break;
		    case 'v':
		    case 'V':
			Matched("@version");
			anotify_nolisten2(player, CRIMSON "ProtoMUCK " PROTOBASE PURPLE " (" RED VERSION WHITE " -- " AQUA NEONVER PURPLE ")" );
			break;
		    case 'w':
		    case 'W':
			if (string_compare(command, "@wall"))
			    goto bad;
			do_wall(player, full_command);
			break;
		    default:
			goto bad;
		}
		break;
          case '&':
            do_mush_set(descr, player, arg1, arg2, command);
            break;
	    case 'd':
	    case 'D':
		switch (command[1]) {
		    case 'b':
		    case 'B':
			Matched("dboot");
			do_dboot(player, arg1);
			break;
		    case 'i':
		    case 'I':
			Matched("dinfo");
			do_dinfo(player, arg1);
			break;
		    case 'r':
		    case 'R':
			Matched("drop");
			do_drop(descr, player, arg1, arg2);
			break;
		    case 'w':
		    case 'W':
			Matched("dwall");
			do_dwall(player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    case 'e':
	    case 'E':
		switch (command[1]) {
		    case 'm':
		    case 'M':
			Matched("emote");
			do_pose(descr, player, full_command);
			break;
		    case 'x':
		    case 'X':
		    case '\0':
			Matched("examine");
			do_examine(descr, player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    case 'g':
	    case 'G':
		/* get, give, go, or gripe */
		switch (command[1]) {
		    case 'e':
		    case 'E':
			Matched("get");
			do_get(descr, player, arg1, arg2);
			break;
		    case 'i':
		    case 'I':
			Matched("give");
			do_give(descr, player, arg1, atoi(arg2));
			break;
		    case 'o':
		    case 'O':
			Matched("goto");
			do_move(descr, player, arg1, 0);
			break;
		    case 'r':
		    case 'R':
			if(command[2]=='i' || command[2]=='I') {
				if (string_compare(command, "gripe"))
				    goto bad;
				do_gripe(player, full_command);
				break;
			}
		    default:
			goto bad;
		}
		break;
	    case 'h':
	    case 'H':
		switch (command[1]) {
		    case 'a':
		    case 'A':
			Matched("hand");
			do_drop(descr, player, arg1, arg2);
			break;
		    case 'e':
		    case 'E':
			Matched("help");
			do_help(player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    case 'i':
	    case 'I':
		if (string_compare(command, "info")) {
		    Matched("inventory");
		    do_inventory(player);
		} else {
		    do_info(player, arg1, arg2);
		}
		break;
	    case 'l':
	    case 'L':
		if (string_prefix("look", command)) {
		    do_look_at(descr, player, arg1, arg2);
		    break;
		} else {
		    Matched("leave");
		    do_leave(descr, player);
		    break;
		}
	    case 'm':
	    case 'M':
		if (string_prefix(command, "move")) {
		    do_move(descr, player, arg1, 0);
		    break;
		} else if (!string_compare(command, "motd")) {
		    do_motd(player, full_command);
		    break;
		} else if (!string_compare(command, "mpi")) {
		    do_mpihelp(player, arg1, arg2);
		    break;
		} else {
		    if (string_compare(command, "man"))
			goto bad;
		    do_man(player, arg1, arg2);
		}
		break;
	    case 'n':
	    case 'N':
		/* news */
		Matched("news");
		do_news(player, arg1, arg2);
		break;
	    case 'p':
	    case 'P':
		switch (command[1]) {
		    case 'a':
		    case 'A':
		    case '\0':
			Matched("page");
			do_page(descr, player, arg1, arg2);
			break;
		    case 'o':
		    case 'O':
			Matched("pose");
			do_pose(descr, player, full_command);
			break;
		    case 'u':
		    case 'U':
			Matched("put");
			do_drop(descr, player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    case 'r':
	    case 'R':
		switch (command[1]) {
		    case 'e':
		    case 'E':
			if(command[2] == 'q' || command[2] == 'Q'){
			    Matched("request");
			    request(player, NULL, commandline);
			} else {
			    Matched("read");    /* undocumented alias for look */
			    do_look_at(descr, player, arg1, arg2);
			}
			break;
		    default:
			goto bad;
		}
		break;
	    case 's':
	    case 'S':
		/* say, "score" */
		switch (command[1]) {
		    case 'a':
		    case 'A':
			Matched("say");
			do_say(descr, player, full_command);
			break;
		    case 'c':
		    case 'C':
		    case '\0':
			if( command[1] &&
			    (command[2] == 'a' || command[2] == 'A' )) {
			    Matched("scan");
			    do_sweep(descr, player, arg1);
			} else {
			    Matched("score");
			    do_score(player, 1);
			}
			break;
		    default:
			goto bad;
		}
		break;
	    case 't':
	    case 'T':
		switch (command[1]) {
		    case 'a':
		    case 'A':
			Matched("take");
			do_get(descr, player, arg1, arg2);
			break;
		    case 'h':
		    case 'H':
			Matched("throw");
			do_drop(descr, player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    case 'w':
	    case 'W':
		switch (command[1]){
		    case 'c':
		    case 'C':
			Matched("wc");
			do_wizchat(player, full_command);
			break;
		    case 'i':
		    case 'I':
			Matched("wizchat");
			do_wizchat(player, arg1);
			break;
		    case 'h':
		    case 'H':
		    case '\0':
			Matched("whisper");
			do_whisper(descr, player, arg1, arg2);
			break;
		    default:
			goto bad;
		}
		break;
	    default:
	bad:
 		if ( Typeof(tp_huh_command) == TYPE_PROGRAM && !(player == -1)) {
			tmpfr = interp(descr, player, DBFETCH(player)->location, tp_huh_command, (dbref)-4, FOREGROUND, STD_REGUID);
			if (!tmpfr)
				goto bad2;
			(void) interp_loop(player, tp_huh_command, tmpfr, 0);
            } else {
   		   bad2:
                  anotify_fmt(player, CINFO "%s",tp_huh_mesg);
			if (tp_log_failed_commands && !controls(player, DBFETCH(player)->location)) {
		    		log_status("HUH from %s(%d) in %s(%d)[%s]: %s %s\n", NAME(player), player,
                        NAME(DBFETCH(player)->location),
			      DBFETCH(player)->location,
			      NAME(OWNER(DBFETCH(player)->location)), command,
			      full_command);
			}
		}
		break;
	}

}
Esempio n. 25
0
local int proc_args(int argc, char *argv[])
{
    int i, o;

    args.iv = INTERVAL_RESET;
    args.serial = LATEST_SERIAL;
    args.serialoffset = 0;

    for (i = 1; i < argc; i++)
    {
        if (argv[i][0] != '-')
            break;
        o = argv[i][1];
        if (o == 'g')
            args.iv = INTERVAL_GAME;
        else if (o == 'r')
            args.iv = INTERVAL_RESET;
        else if (o == 'm')
            args.iv = INTERVAL_MAPROTATION;
        else if (o == 'f')
            args.iv = INTERVAL_FOREVER;
        else if (o == 'c')
            args.serial = LATEST_SERIAL;
        else if (o == 'l')
            args.serial = LATEST_SERIAL, args.serialoffset = -1;
        else if (o == 's')
            args.serial = atoi(argv[++i]);
        else if (o == 'n')
            args.numheader = 1;
        else if (o == 'd')
            args.dbfile = argv[++i];
        else
        {
            printf("unknown option: %s\n\n", argv[i]);
            do_usage();
        }
    }

    if (i != argc)
    {
        const char *subcmd = argv[i];
        if (!strcasecmp(subcmd, "arenas"))
            args.subcmd = cmd_arenas;
        else if (!strcasecmp(subcmd, "players"))
            args.subcmd = cmd_players;
        else if (!strcasecmp(subcmd, "serials"))
            args.subcmd = cmd_serials;
        else if (!strcasecmp(subcmd, "stats"))
            args.subcmd = cmd_stats;
        else if (!strcasecmp(subcmd, "erase"))
            args.subcmd = cmd_erase, args.needwrite = 1;
        else if (!strcasecmp(subcmd, "perase"))
            args.subcmd = cmd_perase, args.needwrite = 1;
        else if (!strcasecmp(subcmd, "erasehist"))
            args.subcmd = cmd_erasehist, args.needwrite = 1;
        else if (!strcasecmp(subcmd, "debug"))
            args.subcmd = cmd_debug;
    }

    return i-1;
}
Esempio n. 26
0
int
main(int argc, char *argv[])
{
	int optc;
	int option_differences = 0,
	    option_differences_cumulative = 0,
	    option_help = 0, option_version = 0;
	double interval = 2;
	char *command;
	int command_length = 0;	/* not including final \0 */

	setlocale(LC_ALL, "");
	progname = argv[0];

	while ((optc = getopt_long(argc, argv, "+d::hn:vt", longopts, (int *) 0))
	       != EOF) {
		switch (optc) {
		case 'd':
			option_differences = 1;
			if (optarg)
				option_differences_cumulative = 1;
			break;
		case 'h':
			option_help = 1;
			break;
		case 't':
			show_title = 0;
			break;
		case 'n':
			{
				char *str;
				interval = strtod(optarg, &str);
				if (!*optarg || *str)
					do_usage();
				if(interval < 0.1)
					interval = 0.1;
				if(interval > ~0u/1000000)
					interval = ~0u/1000000;
			}
			break;
		case 'v':
			option_version = 1;
			break;
		default:
			do_usage();
			break;
		}
	}

	if (option_version) {
		fprintf(stderr, "%s\n", VERSION);
		if (!option_help)
			exit(0);
	}

	if (option_help) {
		fprintf(stderr, usage, progname);
		fputs("  -d, --differences[=cumulative]\thighlight changes between updates\n", stderr);
		fputs("\t\t(cumulative means highlighting is cumulative)\n", stderr);
		fputs("  -h, --help\t\t\t\tprint a summary of the options\n", stderr);
		fputs("  -n, --interval=<seconds>\t\tseconds to wait between updates\n", stderr);
		fputs("  -v, --version\t\t\t\tprint the version number\n", stderr);
		fputs("  -t, --no-title\t\t\tturns off showing the header\n", stderr);
		exit(0);
	}

	if (optind >= argc)
		do_usage();

	command = strdup(argv[optind++]);
	command_length = strlen(command);
	for (; optind < argc; optind++) {
		char *endp;
		int s = strlen(argv[optind]);
		command = realloc(command, command_length + s + 2);	/* space and \0 */
		endp = command + command_length;
		*endp = ' ';
		memcpy(endp + 1, argv[optind], s);
		command_length += 1 + s;	/* space then string length */
		command[command_length] = '\0';
	}

	get_terminal_size();

	/* Catch keyboard interrupts so we can put tty back in a sane state.  */
	signal(SIGINT, die);
	signal(SIGTERM, die);
	signal(SIGHUP, die);
	signal(SIGWINCH, winch_handler);

	/* Set up tty for curses use.  */
	curses_started = 1;
	initscr();
	nonl();
	noecho();
	cbreak();

	for (;;) {
		time_t t = time(NULL);
		char *ts = ctime(&t);
		int tsl = strlen(ts);
		char *header;
		FILE *p;
		int x, y;
		int oldeolseen = 1;

		if (screen_size_changed) {
			get_terminal_size();
			resizeterm(height, width);
			clear();
			/* redrawwin(stdscr); */
			screen_size_changed = 0;
			first_screen = 1;
		}

		if (show_title) {
			// left justify interval and command,
			// right justify time, clipping all to fit window width
			asprintf(&header, "Every %.1fs: %.*s",
				interval, min(width - 1, command_length), command);
			mvaddstr(0, 0, header);
			if (strlen(header) > (size_t) (width - tsl - 1))
				mvaddstr(0, width - tsl - 4, "...  ");
			mvaddstr(0, width - tsl + 1, ts);
			free(header);
		}

		if (!(p = popen(command, "r"))) {
			perror("popen");
			do_exit(2);
		}

		for (y = show_title; y < height; y++) {
			int eolseen = 0, tabpending = 0;
			for (x = 0; x < width; x++) {
				int c = ' ';
				int attr = 0;

				if (!eolseen) {
					/* if there is a tab pending, just spit spaces until the
					   next stop instead of reading characters */
					if (!tabpending)
						do
							c = getc(p);
						while (c != EOF && !isprint(c)
						       && c != '\n'
						       && c != '\t');
					if (c == '\n')
						if (!oldeolseen && x == 0) {
							x = -1;
							continue;
						} else
							eolseen = 1;
					else if (c == '\t')
						tabpending = 1;
					if (c == EOF || c == '\n' || c == '\t')
						c = ' ';
					if (tabpending && (((x + 1) % 8) == 0))
						tabpending = 0;
				}
				move(y, x);
				if (option_differences) {
					chtype oldch = inch();
					char oldc = oldch & A_CHARTEXT;
					attr = !first_screen
					    && ((char)c != oldc
						||
						(option_differences_cumulative
						 && (oldch & A_ATTRIBUTES)));
				}
				if (attr)
					standout();
				addch(c);
				if (attr)
					standend();
			}
			oldeolseen = eolseen;
		}

		pclose(p);

		first_screen = 0;
		refresh();
		usleep(interval * 1000000);
	}

	endwin();

	return 0;
}
Esempio n. 27
0
int main(int argc,char**argv)
{
	int c;
	unsigned int argv_2=0;
    char is_frequency=0;
	unsigned long long map_address=0xf0000000;
    if(argc==1)
    {
        do_usage();
        exit(0);
    }
    while ((c = getopt_long (argc, argv, "01o:", longopts, NULL)) != -1)
	{
		switch(c)
		{
			case 0:
				fprintf(stderr,"turn on\n");
				argv_2=0;
				break;
			case 1:
				fprintf(stderr,"turn off\n");
				argv_2=1;
				break;
            case 'o':
                map_address += strtoul(optarg,NULL,16);
                fprintf(stderr,"offset is %X\n",map_address);

                if(map_address==0x50011016)
                    is_frequency=1;

                break;
            case 'h':
                do_usage();
                break;
			default:
                do_usage();
				break;
		}
	}
    if(is_frequency) {
    if( argc >= (optind+1) )
    {
        fprintf(stderr,"argv[optind]=%s optind=%d argc=%d\n",argv[optind],optind,argc);
        argv_2 = strtoul(argv[optind],NULL,10);
    }
    else {
        fprintf(stderr,"frequency value needed \n");
        exit(0);
    }
    }
	int fd;
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){   
		fprintf(stderr,"gpio: Error opening /dev/mem\n");
		exit(-1);
	}   
	addr = mmap(0, 20, PROT_READ| PROT_WRITE, MAP_SHARED, fd, map_address & ~MAP_MASK); 
	//addr=mmap(0, 20, PROT_READ| PROT_WRITE, MAP_SHARED, fd, MAP_ADDRESS & ~MAP_MASK); 

	if(addr == (void *) -1) {
		printf("map_base:%x \n",addr);FATAL; }
	//printf("Memory mapped at address %p.\n", addr);

	addr 	= addr + (map_address & MAP_MASK);
	//addr 	= addr + (MAP_ADDRESS & MAP_MASK);

	//printf("New Memory mapped at address %p.\n", addr);
    close(fd);
	//sprintf((char *)addr, "%c", atoi(argv[2]));//似乎會傳2個character
	if( is_frequency )
    	memcpy(addr,&argv_2,4);
    else
    {
        if( argc >= (optind+1) )
        {
            argv_2 = strtoul(argv[optind],NULL,10);
        }
        memcpy(addr,&argv_2,1);
    }
	
	/*  used to read from /dev/mem  */
	/*
	unsigned int tmp;
	memcpy(&tmp,addr,sizeof(char));
	fprintf(stderr,"f**k=%x\n",tmp);
	*/
	return 0;
}