/*********************************************************************** * * * Copyright (C) 1988 * * University Corporation for Atmospheric Research * * All Rights Reserved * * * * NCAR GRAPHICS V2.00 * * * ***********************************************************************/ #include #include #include #include "wks.h" static int unit_list[MAX_UNITS]; static int mf_fd; /* metafile fd */ static int pipes[2]; static char *gks_output, *gks_translator; static int output_type; static int pid; #ifdef DEAD main() { int unit = 1; char *string = "wooga"; gksopenwks_(&unit); gkswritewks_(&unit, (int *)string); gksclosewks_(&unit); } #endif void opnwks_ ( int *unit, int *status ) { char *getenv(), *p; if (*unit >= MAX_UNITS) { fprintf(stderr, "Maximum workstations (%d) exceeded\n",MAX_UNITS); *status = 304; return; } if (unit_list[*unit]) { fprintf(stderr, "Attempt to reopen unit %d\n", *unit); *status = 304; return; } /* find out what output file name to use */ gks_output = getenv("NCARG_GKS_OUTPUT"); if (gks_output == NULL) { output_type = FILE_OUTPUT; gks_output = DEFAULT_GKS_OUTPUT; } else { for(p=gks_output; *p==' ' && *p != '\0'; p++); if (*p == '|') { for(p++; *p==' ' && *p != '\0'; p++); output_type = PIPE_OUTPUT; gks_translator = p; } else { output_type = FILE_OUTPUT; gks_output = p; } } /* if output is a translator, fork the proper one and link to it */ /* if output is a file, just open it for writing */ if ( output_type == PIPE_OUTPUT ) { if ( strlen(gks_translator) == 0 ) { gks_translator = DEFAULT_TRANSLATOR; } pipe(pipes); if ( (pid = fork()) == 0 ) { close(pipes[1]); close(0); dup(pipes[0]); execlp(gks_translator, gks_translator, "-", (char *) 0); *status = 304; return; } else { close(pipes[0]); mf_fd = pipes[1]; } } else if (output_type == FILE_OUTPUT) { if ( (mf_fd = open(gks_output, 2)) == -1) { if ( (mf_fd = creat(gks_output, 0644)) == -1) { fprintf(stderr, "Could not open <%s>\n", gks_output); *status = 304; return; } } } unit_list[*unit] = mf_fd; } void clswks_ ( int *unit, int *status ) { int child_status; if (*unit >= MAX_UNITS || unit_list[*unit] == 0) { fprintf(stderr, "Invalid close on unit %d\n", *unit); *status = 304; return; } else { if ( output_type == PIPE_OUTPUT ) wait(&child_status); close(mf_fd); unit_list[*unit] = 0; } } void wrtwks_ ( int *unit, int *buffer, int *status ) { int nb; char *p; int loc; static char locbuf[RECORDSIZE]; if (*unit >= MAX_UNITS || unit_list[*unit] == 0) { fprintf(stderr, "Invalid unit (%d)\n", *unit); *status = 304; return; } #if defined(vax) || (defined(mips) && defined(ultrix)) p = (char *) buffer; for (loc = 0; loc < RECORDSIZE; loc+=4) { locbuf[loc + 0] = p[loc + 3]; locbuf[loc + 1] = p[loc + 2]; locbuf[loc + 2] = p[loc + 1]; locbuf[loc + 3] = p[loc + 0]; } nb = write(unit_list[*unit], (char *) locbuf, RECORDSIZE); #else nb = write(unit_list[*unit], (char *) buffer, RECORDSIZE); #endif if (nb != RECORDSIZE) { fprintf(stderr, "Error writing metafile\n"); *status = 304; return; } }