int main() { char name[MAX_NAME_LENGTH]; storename(name); while(1) { parse_input(); } return 0; int socket_id; char buffer[256]; int i; //create the socket socket_id = socket(AF_INET, SOCK_STREAM, 0); //bind to port address struct sockaddr_in sock; sock.sin_family = AF_INET; sock.sin_port = htons(5000); inet_aton( "127.0.0.1", &(sock.sin_addr) ); bind( socket_id, (struct sockaddr *)&sock, sizeof(sock)); //attempt connection i = connect(socket_id, (struct sockaddr *)&sock, sizeof(sock)); printf("<client> connect returned: %d\n", i); read(socket_id, buffer, sizeof(buffer)); printf("<client> received [%s]\n", buffer); return 0; }
static void do_something_server(char *wherenewline) { int n; *wherenewline = '\0'; if (match_arg(buf, "loc", &n)) { loc(n); } else if (match_arg(buf, "here", &n)) { here(n); } else if (match_arg(buf, "arr", &n)) { arrived(n); } else if (match_arg(buf, "dep", &n)) { departed(n); } else if (match_arg(buf, "poked", &n)) { pokedby(n); } else if (match_arg(buf, "said", &n)){ said(n); }else if (strcmp(buf, "ib") == 0) { saw_inventory_something = 0; printf("%s\n", lang_inv_heading); } else if (match_arg(buf, "i", &n)) { saw_inventory_something = 1; printf(" %s (#%d)\n", lang_thing[n], n); } else if (strcmp(buf, "ie") == 0) { if (!saw_inventory_something) printf("%s\n", lang_inv_nothing); } else if (strcmp(buf, "ok") == 0) { printf("%s\n", lang_ok); } else if (strcmp(buf, "ng") == 0) { printf("%s\n", lang_get_nosuch); } else if (strcmp(buf, "nd") == 0) { printf("%s\n", lang_drop_nosuch); } else if (strcmp(buf, "np") == 0) { printf("%s\n", lang_get_nosuch); } else if (match_arg(buf, "name", &n)) { char *p; if ((p = strchr(buf, ' ')) == NULL || (p = strchr(p + 1, ' ')) == NULL) fprintf(stderr, "error: malformed 'name' from server\n"); else storename(n, p + 1); } else if (match_arg(buf, "quit", &n)) { removename(n); } else if (strncmp(buf, "error ", 6) == 0) { printf("error from server: %s\n", buf + 6); } else { fprintf(stderr, "unexpected data from server: %s\n", buf); } n = wherenewline - buf; n++; if (bytes_in_buf > n && (buf[n] == '\r' || buf[n] == '\n')) n++; bytes_in_buf -= n; memmove(buf, buf + n, bytes_in_buf); }
/* \fcnfh get number of isotopes from file and set index @returns number of lines read */ int getmnfromfile(FILE *fp, struct atm_data *at, struct transit *tr, int nmb) { char line[maxline],*lp; int ison=0,i; struct isotopes *iso=tr->ds.iso; enum isodo *isodo=iso->isodo; //Set variable to handle proportional to isotopes int ipi=0,ipa=at->ipa=4; isolineinatm=(_Bool *)calloc(iso->n_i,sizeof(_Bool)); isoprop=(struct atm_isoprop *)calloc(ipa,sizeof(struct atm_isoprop)); at->begline=0; enum isodo atisodo; at->isodo = (enum isodo *)calloc(nmb,sizeof(enum isodo)); at->isoeq = (int *) calloc(nmb,sizeof(int)); at->m = (PREC_ZREC *) calloc(nmb,sizeof(PREC_ZREC)); at->n = (char **) calloc(nmb,sizeof(char *)); at->n[0] = (char *) calloc(nmb*maxeisoname,sizeof(char)); at->isoeq[0]=-1; for(i=1;i<nmb;i++){ at->n[i]=at->n[0]+i*maxeisoname; at->isoeq[i]=-1; } //while t,p data doesn't start, check for the various modifiers while(1){ switch(fgetupto_err(line,maxline,fp,&atmerr,atmfilename, at->begline++)){ case '\n': //Ignore comments and case '#': // blank lines continue; case 0: //Error if EOF transiterror(TERR_SERIOUS|TERR_ALLOWCONT, "readatminfo:: EOF unexpectedly found at line %i\n" "of file %s while no t,p data points have been read\n" ,at->begline,atmfilename); exit(EXIT_FAILURE); continue; case 'q': //Whether is mass or number abundance lp=line+1; while(*lp++==' '); lp--; switch(*lp|0x20){ case 'n': at->mass=0; break; case 'm': at->mass=1; break; default: transiterror(TERR_SERIOUS, "'q' option in the atmosphere file can only be followed\n" "by 'm' (for abundances by mass) or 'n' (for abundances by\n" "number). '%s' is invalid.\n" ,line); break; } continue; case 'z': //Zero radius value zerorad=atof(line+1); continue; case 'f': //An isotope is to be taken as //proportional to other. lp=line+1; while(*lp==' '||*lp=='\t') lp++; if(ipi==ipa) isoprop=(struct atm_isoprop *)realloc(isoprop,(ipa<<=1)* sizeof(struct atm_isoprop)); isoprop[ipi].m=getds(lp,0,isoprop[ipi].n,maxeisoname-1); //skip over recently read field, and go to next field. lp=nextfield(lp); //skip an optional equal '=' sign if(*lp=='=' && lp[1]==' ') lp=nextfield(lp); //get factor, which has to be between 0 and 1 isoprop[ipi].f=strtod(lp,NULL); if(isoprop[ipi].f<0 ) transiterror(TERR_CRITICAL, "Abundance ratio has to be positive in atmosphere\n" "file '%s' in line: %s" ,atmfilename,line); lp=nextfield(lp); //get name of reference and increase index i=0; while(*lp) isoprop[ipi].t[i++]=*lp++; isoprop[ipi].t[i]='\0'; //now check if that isotope is one of the given in the lineinfo //file isoprop[ipi].eq=-1; isisoline(isoprop[ipi].n,isoprop[ipi].m,&isoprop[ipi].eq,factor, iso->isof,isodo,iso->n_i); //advance index and go for the next line ipi++; continue; case 'u': //Change factorization of radius, temp, //or press switch(line[1]){ case 'r': at->rads.fct=atof(line+2); break; case 'p': at->atm.pfct=atof(line+2); break; case 't': at->atm.tfct=atof(line+2); break; default: transiterror(TERR_SERIOUS, "Invalid unit factor indication in atmosphere file\n"); exit(EXIT_FAILURE); } continue; case 'n': //Name or identifier for file data storename(at,line+1); continue; case 'i': //Isotope information lp=line+1; while(*lp==' '||*lp=='\t') lp++; //'i' has to come before 'f' if(ipi) transiterror(TERR_CRITICAL, "In line '%s'.\n" " 'f' lines have to come after all the 'i' lines in\n" " atmosphere file '%s'" ,line,atmfilename); //for each field while(*lp){ atisodo=atmfile; //Allocate if necessary if(ison==nmb){ nmb<<=1; at->isodo = (enum isodo *)realloc(at->isodo,nmb*sizeof(enum isodo)); at->isoeq = (int *) realloc(at->isoeq,nmb*sizeof(int)); at->m = (PREC_ZREC *) realloc(at->m, nmb*sizeof(PREC_ZREC)); at->n = (char **) realloc(at->n, nmb*sizeof(char *)); at->n[0] = (char *) realloc(at->n[0], nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb;i++) at->n[i]=at->n[0]+i*maxeisoname; for(i=nmb/2;i<nmb;i++) at->isoeq[i]=-1; } //get mass and name, checking that is correct. First see if this //isotope wants to be ignored. if(*lp=='!'){ lp++; atisodo=ignore; } at->m[ison]=getds(lp,0,at->n[ison],maxeisoname-1); if(at->m[ison]<0||at->n[ison]=='\0'){ transiterror(TERR_SERIOUS, "Invalid field in file %s, line %i while reading isotope" " info at:\n%s\n" ,atmfilename,at->begline,lp); } //now check if that isotope is one of the given in the lineinfo //file isisoline(at->n[ison],at->m[ison],at->isoeq+ison,atisodo, iso->isof,isodo,iso->n_i); at->isodo[ison++]=atisodo; //skip over recently read field, and go to next field. while(*lp!=' '&&*lp!='\0') lp++; while(*lp==' '||*lp=='\t') lp++; } continue; default: //T,P seems to be starting break; } break; } transitprint(3,verblevel, "Read all keywords in atmosphere file without problems\n"); //Check if there was at least an isotope identification and allocate new //arrays if(!ison) transiterror(TERR_SERIOUS, "No isotopes were found in atmosphere file, make sure to\n" "specify them in a line starting with the letter 'i'.\n" "First non-comment line read:\n%s\n" ,line); at->begpos=ftell(fp)-strlen(line)-1; //shorten extra length of arrays fonly=(struct fonly *)calloc(nfonly,sizeof(struct fonly)); at->ipa=ipa=ipi; isoprop=(struct atm_isoprop *)realloc(isoprop,ipa* sizeof(struct atm_isoprop)); //Makes at arrays bigger, so that they can hold the factorized values. nmb = at->n_aiso = ison + ipa; at->isodo = (enum isodo *)realloc(at->isodo,nmb*sizeof(enum isodo) ); at->isoeq = (int *) realloc(at->isoeq,nmb*sizeof(int) ); at->m = (PREC_ZREC *) realloc(at->m, nmb*sizeof(PREC_ZREC) ); at->n = (char **) realloc(at->n, nmb*sizeof(char *) ); at->n[0] = (char *) realloc(at->n[0], nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb;i++) at->n[i] = at->n[0] + i * maxeisoname; //initialize values for the factorized elements for(i=ison;i<nmb;i++){ strncpy(at->n[i],isoprop[i-ison].n,maxeisoname-1); at->n[i][maxeisoname-1] = '\0'; at->isoeq[i] = i-ison; at->m[i] = isoprop[i-ison].m; at->isodo[i] = factor; } //Resolve what to do with those isotopes that appear in the transition //database, but not in the atmosphere file. at->n_niso = checknonmatch(tr,at,isodo); //Set full isotope info in the transit structure nmb = iso->n_i + at->n_niso; iso->isodo = (enum isodo *)realloc(iso->isodo, nmb*sizeof(enum isodo)); iso->isof = (prop_isof *)realloc(iso->isof, nmb*sizeof(prop_isof)); iso->isof[iso->n_i].n = (char *)realloc(iso->isof[iso->n_i].n, (nmb-iso->n_i)*maxeisoname* sizeof(char)); for(i=1;i<nmb-iso->n_i;i++) iso->isof[iso->n_i+i].n = iso->isof[iso->n_i].n + i * maxeisoname; //Look for isotopes who have not been associated and see whether they //are supposed to be ignored. nmb = iso->n_i; ipi = 0; int lineignore=0; for(i=0 ; i<ison+ipa ; i++) //If the isotope is not associated to the linedb isotopes, then //associate it. Note that isotopes in linedb that are ignored will //be associated (see comments for Line \label{isodbassoc}). Hence //they won't be detected in this IF. Factor isotopes will also be //associated, and will be handled below if(at->isoeq[i] == -1){ //If they are not going to be ignored then associate them with the //following index available of post linedb isotopes. if(at->isodo[i] != ignore){ at->isoeq[i] = nmb; iso->isodo[nmb] = at->isodo[i]; iso->isof[nmb].m = at->m[i]; strcpy(iso->isof[nmb++].n, at->n[i]); } //otherwise, they might only be used as a reference to factor. else{ at->isoeq[i] = ipi; strcpy(fonly[ipi++].n, at->n[i]); } } //Just count the number of ignored isotopes that belonged to the line //isotopes. else if(at->isodo[i] == ignore) lineignore++; //If there is factor isotopes else if(at->isodo[i] == factor && isoprop[at->isoeq[i]].eq == -1){ if(at->isodo[i]==ignore) transiterror(TERR_CRITICAL, "Trying to ignore an factor isotope, that is not\n" "posible.\n"); isoprop[at->isoeq[i]].eq = nmb; iso->isodo[nmb] = at->isodo[i]; iso->isof[nmb].m = at->m[i]; strcpy(iso->isof[nmb++].n, at->n[i]); } //Reduce the array to get rid of nonline-ignored isotopes. (isov has //not even been allocated yet) iso->n_e = nmb; iso->isodo = (enum isodo *)realloc(iso->isodo, nmb*sizeof(enum isodo)); iso->isof = (prop_isof *)realloc(iso->isof, nmb*sizeof(prop_isof)); iso->isof[iso->n_i].n = (char *)realloc(iso->isof[iso->n_i].n, nmb*maxeisoname*sizeof(char)); for(i=1;i<nmb-iso->n_i;i++) iso->isof[iso->n_i+i].n = iso->isof[iso->n_i].n + i * maxeisoname; //Check that everything makes sense double cumulother=0; for(i=0 ; i<at->n_aiso ; i++) if(at->isodo[i]==factor){ int feq=at->isoeq[i]; if(strcasecmp(isoprop[feq].n,"other")==0) cumulother+=isoprop[feq].f; } //It doesn't make sense for cumulother to be anything different from //unity (except round-off error): you want to associate the //remainder of the atmosphere to some isotopic properties. if( cumulother!=0 && (int)(cumulother*ROUNDOFF+0.5)!=(int)(ROUNDOFF+0.5) ) transiterror(TERR_SERIOUS, "If you are specifying isotopes proportional to 'other'\n" "you have to complete unity (%g). It doesn't make sense\n" "otherwise\n" ,cumulother); transitASSERT(nmb+nfonly!=ison+ipa, "Oooops, number of ignored-nonline elements (%i), plus the\n" "number of ignored-line elements(%i), plus the number of\n" "nonignored (%i), doesn't match the number of elements\n" "found in fields 'i'(%i) and 'f'(%i) of the atmosphere\n" "file '%s'\n" ,nfonly,lineignore,nmb-lineignore,ison,ipa,atmfilename); transitASSERT(nmb!=iso->n_e, "Uyuyuyuyu! Problem in file %s, line %i,\n" "assertion failed: %i != %i!!\n" ,__FILE__,__LINE__,nmb,iso->n_e); //free unused array, store factor info in at structure and return line //where T,P start free(isolineinatm); at->isoprop=isoprop; return at->begline; }
/* \fcnfh Get keyword variables from atmosphere file (mass/number abundance bool; zero-radius offset; radius, temperature, and pressure units factor; atmfile name/info; list isotopes; list of proportional-abundance isotopes). Store molecules and proportional isotopes in atm_data struct. Determine which linedb isotope corresponds to such atm_data isotope. Solve non-matched linedb isotope cases. Put all non-ignore isotopes in transit.ds.iso structure. Return: Number of lines read */ int getmnfromfile(FILE *fp, /* Pointer to atmospheric file */ struct atm_data *at, /* atmosphere structure */ struct transit *tr, /* transit structure */ PREC_ZREC *f_remainder){ /* Remainder molecules' factor */ struct molecules *mol=tr->ds.mol; char line[maxline], *lp; int nimol=0, /* Number of molecules with abundance profile */ nmol=0, /* Total number of molecules */ i; /* Auxiliary for-loop index */ double cumulother = 0; /* Cumulative remainder-molecules' factor */ int ipi = 0; /* Number of remainder molecules */ /* Is the isotope defined in the atm file?: */ //isoprop = (struct atm_isoprop *)calloc(ipa, sizeof(struct atm_isoprop)); at->begline = 0; /* Line where the info begins */ /* Read and store the keyword atmospheric variables: */ while(1){ switch(fgetupto_err(line, maxline, fp, &atmerr, atmfilename, at->begline++)){ /* Ignore comments and blank lines: */ case '\n': case '#': continue; case 0: /* Throw error if EOF */ transiterror(TERR_SERIOUS|TERR_ALLOWCONT, "readatm :: EOF unexpectedly found at line %i " "of file %s while no t,p data points have been read.\n", at->begline, atmfilename); exit(EXIT_FAILURE); continue; /* Determine whether abundance is by mass or number: */ case 'q': lp = line + 1; while(*lp++ == ' '); /* Skip blank spaces */ lp--; switch(*lp|0x20){ case 'n': at->mass = 0; /* Number abundance (mixing ratio) */ break; case 'm': at->mass = 1; /* Mass abundance (mass mixing ratio) */ break; default: transiterror(TERR_SERIOUS, "'q' option in the atmosphere file can only be followed " "by 'm' (for abundances by mass) or 'n' (for abundances " "by number). '%s' is invalid.\n", line); break; } continue; /* Zero radius value: */ case 'z': zerorad = atof(line+1); continue; /* Radius, temperature, or pressure units factor: */ case 'u': switch(line[1]){ case 'r': at->rads.fct = atof(line+2); break; case 'p': at->atm.pfct = atof(line+2); break; case 't': at->atm.tfct = atof(line+2); break; default: transiterror(TERR_SERIOUS, "Invalid unit factor indication in " "atmosphere file.\n"); exit(EXIT_FAILURE); } continue; case 'n': /* Name or identifier for file data */ storename(at, line+1); continue; case 'i': /* Molecule names with an abundance profile: */ /* Count the number of wrds (molecules) in line: */ nimol = countfields(line+1, ' '); transitprint(15, verblevel, "The number of molecules is %d.\n", nimol); /* Allocate Molecules names: */ mol->name = (char **)calloc(nimol, sizeof(char *)); mol->name[0] = (char *)calloc(nimol*maxeisoname, sizeof(char)); for(i=1; i<nimol; i++) mol->name[i] = mol->name[0] + i*maxeisoname; transitprint(1, verblevel, "Molecules with abundance profile:\n "); lp = line; lp = nextfield(lp); /* Skip keyword */ /* Read and store names: */ for (i=0; i<nimol; i++){ getname(lp, mol->name[i]); lp = nextfield(lp); transitprint(1, verblevel, "%s, ", mol->name[i]); } transitprint(1, verblevel, "\b\b.\n"); continue; /* Molecules with abundance proportional to the remainder: */ case 'f': lp = line; lp = nextfield(lp); /* Skip keyword */ /* Current total number of molecules: */ nmol = ++ipi + nimol; /* Re-allocate to add the new molecule: */ mol->name = (char **)realloc(mol->name, nmol*sizeof(char *)); mol->name[0] = (char *)realloc(mol->name[0], nmol*maxeisoname*sizeof(char)); for (i=1; i<nmol; i++) mol->name[i] = mol->name[0] + i*maxeisoname; /* Re-allocate remainder factors: */ f_remainder = (PREC_ZREC *)realloc(f_remainder, ipi*sizeof(PREC_ZREC)); /* Read and store the molecule's name: */ getname(lp, mol->name[nmol-1]); lp = nextfield(lp); /* Move pointer to next field */ if(*lp == '=') /* Skip an optional equal '=' sign */ lp++; /* Read and store factor: */ f_remainder[ipi-1] = strtod(lp, NULL); transitprint(30, verblevel, "%s remainder factor: %.3f\n", mol->name[nmol-1], f_remainder[ipi-1]); if(f_remainder[ipi-1] < 0) transiterror(TERR_CRITICAL, "Abundance ratio has to be positive in atmosphere " "file '%s' in line: '%s'.\n", atmfilename, line); continue; /* End of keyword variables: */ default: break; } break; } transitprint(1, verblevel, "Molecules with abundance proportional to " "remainder:\n "); for(i=nimol; i<nmol; i++) transitprint(1, verblevel, "%s, ", mol->name[i]); transitprint(1, verblevel, "\b\b.\n"); transitprint(3, verblevel, "Read all keywords in atmosphere file without " "problems.\n"); /* Set total number of molecules in atmosphere: */ mol->nmol = at->n_aiso = nmol; /* Check that there was at least one isotope defined and re-allocate array sizes to their final size: */ if(!nimol) transiterror(TERR_SERIOUS, "No isotopes were found in atmosphere file, " "make sure to specify them in a line starting " "with the letter 'i'. First non-comment line " "read:\n%s\n", line); /* Set position of beginning of data: */ at->begpos = ftell(fp) - strlen(line) - 1; /* Calculate cumulative fraction of remainder molecules: */ for(i=0; i < nmol-nimol; i++) cumulother += f_remainder[i]; transitprint(30, verblevel, "Cumulative remainder fraction: %.4f.\n", cumulother); /* Check that cumulother sums to 1.0 (within allowed errors): */ if(nmol>nimol && abs(1.0 - cumulother) > ROUNDTHRESH) transiterror(TERR_SERIOUS, "Sum of remainder-molecules fractional " "abundance (%g) must add to 1.0 +/- %g.\n", cumulother, ROUNDTHRESH); /* Resolve what to do with those isotopes that appear in the line transition database, but not in the atmosphere file. Get the number of non-ignored isotopes in atm_data without linelist: */ //at->n_niso = checknonmatch(tr, at, isodo); /* FINDME: This will be a task in readline (if actually needed). */ return at->begline; }