Example #1
0
File: main.c Project: atheros/svc
static void handle_server_decoded_msg(const dstring* string) {
	dstrlist* list;
	dstring** argv;
	int argc;
	int error;

	list = dstrlex_parse(string, &error);
	if (list == NULL) {
		fprintf(stderr, "Failed to parse string command: %s\n", dstrlex_errstr(error));
		return;
	}

	/* convert list to vector */
	argc = list->size;
	argv = dlist_tovector(list);
	dlist_free(list);

	if (dcmpcs(argv[0], "SSET") == 0) {
		scmd_sset(string, argc, argv);
	} else if (dcmpcs(argv[0], "YARE") == 0) {
		scmd_yare(string, argc, argv);
	} else if (dcmpcs(argv[0], "PADD") == 0) {
		scmd_padd(string, argc, argv);
	} else if (dcmpcs(argv[0], "PDEL") == 0) {
		scmd_pdel(string, argc, argv);
	} else if (dcmpcs(argv[0], "PSET") == 0) {
		scmd_pset(string, argc, argv);
	} else if (dcmpcs(argv[0], "MESG") == 0) {
		scmd_mesg(string, argc, argv);
	} else {
		fprintf(stderr, "Unrecognized command from server '%s'.\n", argv[0]->data);
	}
	dvec_free(argv);
}
Example #2
0
File: main.c Project: atheros/svc
/**
 * Handle input command.
 *
 * This function should return 0, even if the command failed.
 * Return of != 0 means the event loop will quit.
 */
static int handle_input_command(const dstring* string) {
	dstrlist* list;
	dstring** argv;
	unsigned int argc;
	int error;
	ENetPacket* packet;


	if (string->len == 0) {
		/* empty command */
		return 0;
	} else if (string->data[0] == '/') {
		/* local command */
		/* parse the command */
		list = dstrlex_parse(string, &error);
		if (list == NULL) {
			fprintf(stderr, "Failed to parse command string '%s': %s\n", string->data, dstrlex_errstr(error));
			return 0;
		}

		/* convert list to vector */
		argc = list->size;
		argv = dlist_tovector(list);
		dlist_free(list);

		/* select command to execute */
		if (dcmpcs(argv[0], "/connect") == 0) {
			error = cmd_connect(argc, argv);
		} else if (dcmpcs(argv[0], "/disconnect") == 0) {
			error = cmd_disconnect(argc, argv);
		} else if (dcmpcs(argv[0], "/quit") == 0) {
			error = cmd_quit(argc, argv);
		} else if (dcmpcs(argv[0], "/mute") == 0) {
			error = cmd_mute(argc, argv);
		} else if (dcmpcs(argv[0], "/deafen") == 0) {
			error = cmd_deafen(argc, argv);
		} else {
			fprintf(stderr, "Unknown command '%s'\n", argv[0]->data);
			error = 0;
		}

		dvec_free(argv);
		return error;
	} else if (client.state == SVCECLIENT_STATE_CONNECTED) {
		/* send to server if connected */
		mutex_lock(&client.network_lock);
		packet = enet_packet_create(string->data, string->len, ENET_PACKET_FLAG_RELIABLE);
		enet_peer_send(client.client, 0, packet);
		mutex_unlock(&client.network_lock);
	} else if (client.state == SVCECLIENT_STATE_CONNECTING) {
		/* server command but still connecting */
		fprintf(stderr, "Can't send command to server, still connecting.\n");
	} else {
		/* server command but not connected */
		fprintf(stderr, "Can't send command to server, not connected.\n");
	}


	return 0;
}
Example #3
0
// store the list of indexes, x is not destroyed
void dvec_sort_index(int *I, int n, double *X)
{
  double *Y=NULL;
  Y=dvec_allocate(n);
  dvec_copy(n,Y,X);
  dvec_sort(n,Y,I);
  Y=dvec_free(Y);
}
Example #4
0
// x[i] <=> x[I[i]]
void dvec_swap_index(int n, double *x, const int *I)
{
  double *y=NULL;
  y=dvec_allocate(n);
  dvec_copy_index(n,y,x,I);
  dvec_copy(n,x,y);
  y=dvec_free(y);
}
Example #5
0
dcomplex *zvec_bin_load(int *n, char* fmt, ...)
{
  int l;
  size_t k;
  dcomplex *zx=NULL;
  double *dx=NULL;
  char fname[FILE_NAME_LENGTH_MAX+1],*buf=NULL;
  va_list argp;
  FILE *fid;
   // file name
  va_start(argp,fmt); vsprintf(fname,fmt,argp);
  // open file
  if((fid=fopen(fname,"r"))==0){ fclose(fid); zx=NULL; (*n)=0; }
  else{
    // read header
    l=strlen("zvec");
    buf=malloc(sizeof(char)*(l+1));
    k=fread(buf,sizeof(char),l,fid);
    if(k==(size_t)l && strncmp(buf,"zvec",l)==0){ /* zvec */
      // read size
      k=fread(n,sizeof(int),1,fid);
      if(k!=1 || (*n)<=0){ ERROR_AT; printf("Failed to load the size from the file '%s'.\n",fname); exit(0); }
      // allocate
      zx=zvec_allocate((*n));
      // read data
      k=fread(zx,sizeof(dcomplex),(*n),fid);
      if(k!=(size_t)(*n)){ ERROR_AT; printf("Failed to load the data from the file '%s'.\n",fname); exit(0); }
      // close
      fclose(fid);
    }else if(k==(size_t)l && strncmp(buf,"dvec",l)==0){ /* dvec */
      fclose(fid);               // close
      dx=dvec_bin_load(n,fname); // load
      zx=zvec_allocate((*n));    // allocate
      zvec_copy_d((*n),zx,dx);   // copy
      dx=dvec_free(dx);          // free
    }else{ fclose(fid); zx=NULL; (*n)=0; }
  }
  // done
  free(buf);
  return zx;
}
Example #6
0
void
vec_free_x(struct dvec *v)
{
    struct plot *pl;

    if ((v == NULL) || (v->v_name == NULL))
        return;
    pl = v->v_plot;

    /* Now we have to take this dvec out of the plot list. */
    if (pl != NULL) {
        pl->pl_lookup_valid = FALSE;
        if (pl->pl_dvecs == v) {
            pl->pl_dvecs = v->v_next;
        } else {
            struct dvec *lv = pl->pl_dvecs;
            if (lv)
                for (; lv->v_next; lv = lv->v_next)
                    if (lv->v_next == v)
                        break;
            if (lv && lv->v_next)
                lv->v_next = v->v_next;
            else
                fprintf(cp_err,
                        "vec_free: Internal Error: %s not in plot\n",
                        v->v_name);
        }
        if (pl->pl_scale == v) {
            if (pl->pl_dvecs)
                pl->pl_scale = pl->pl_dvecs;    /* Random one... */
            else
                pl->pl_scale = NULL;
        }
    }

    dvec_free(v);
}
Example #7
0
int
DestroyGraph(int id)
{
    LISTGRAPH *list, *lastlist;
    struct _keyed *k, *nextk;
    struct dveclist *d, *nextd;
    struct dbcomm *db;

    list = GBucket[id % NUMGBUCKETS].list;
    lastlist = NULL;
    while (list) {
        if (list->graph.graphid == id) {  /* found it */

            /* Fix the iplot/trace dbs list */
            for (db = dbs; db && db->db_graphid != id; db = db->db_next)
                ;

            if (db && (db->db_type == DB_IPLOT ||
                       db->db_type == DB_IPLOTALL))
            {
                db->db_type = DB_DEADIPLOT;
                /* Delete this later */
                return (0);
            }

            /* adjust bucket pointers */
            if (lastlist)
                lastlist->next = list->next;
            else
                GBucket[id % NUMGBUCKETS].list = list->next;

            /* run through and de-allocate dynamically allocated keyed list */
            k = list->graph.keyed;
            while (k) {
                nextk = k->next;
                tfree(k->text);
                tfree(k);
                k = nextk;
            }

            /* de-allocate dveclist */
            d = list->graph.plotdata;
            while (d) {
                nextd = d->next;
                dvec_free(d->vector);
                tfree(d);
                d = nextd;
            }

            tfree(list->graph.commandline);
            tfree(list->graph.plotname);

            /* If device dependent space allocated, free it. */
            if (list->graph.devdep)
                tfree(list->graph.devdep);
            tfree(list);

            return (1);
        }
        lastlist = list;
        list = list->next;
    }

    internalerror("tried to destroy non-existent graph");
    return (0);
}