void terminateProgram(int info_fd, int log_fd, options* opt)
{
    // Close info file, SMC and clear resources
    CHECK_ERROR( CloseFile(info_fd) );
    CHECK_ERROR( CloseFile(log_fd) );
    smcClose();
    freeOptions(opt);
}
Exemple #2
0
main (int argc, char **argv) 
{
    char   c, fname[64], buf[64], resp[32], val[12], config[128];
    int	   i, j, debug=0, clear = 1, nimages=1, delay=0, interactive=1;
    int    nsegs, pagenum, nvals, imnum=0;
    void   *data;
    char   *cdata, *cp;
    int    *idata, *ip;
    short  *sdata, *sp;
    double stime, etime;


    /* Process command-line arguments.
     */
    strcpy (fname, "\0");
    for (i=1; i < argc; i++) {
        if (strncmp (argv[i], "-cache", 2) == 0) {
	    strcpy (fname, argv[++i]);
        } else if (strncmp (argv[i], "-delay", 2) == 0) {
	    delay = atoi (argv[++i]);
        } else if (strncmp (argv[i], "-x", 2) == 0) {
	    debug++;
        } else if (strncmp (argv[i], "-nimages", 2) == 0) {
	    nimages = atoi (argv[++i]);
        } else if (strncmp (argv[i], "-keep", 2) == 0) {
	    clear = 0;
        } else if (strncmp (argv[i], "-interactive", 2) == 0) {
	    interactive = 1;
        } else if (strncmp (argv[i], "-batch", 2) == 0) {
	    interactive = 0;
        } else if (strncmp (argv[i], "-help", 2) == 0) {
	    printHelp ();
	    exit (0);

	} else
	    printf ("Unrecognized option: %s\n", argv[i]);
    }


    /* Initialize the config string.
     */
    sprintf (config, "debug=%d,cache_file=%s", debug, fname);


    /*  Open/Attach to the cache.
     */
    printf ("Opening cache....\n");
    if ((smc = smcOpen (config)) == (smCache_t *)NULL)
	fprintf (stderr, "Error opening cache, invalid file?.\n");



    /*  Simulate the readout sequence>
     */
    for (i=1; i <= nimages; i++) {
	printf ("Generating image %d ....", i);
        simMetaData ();				/* pre-readout header	*/
        simData ();				/* pixel data		*/
        simMetaData ();				/* post-readout header	*/
	printf ("done.\n");

        sleep (delay); 				/* pause requested time */
    }


    /* Once we're done, see if we want drop into a command mode.
     */
    if (interactive) {
	imnum = nimages;

        /* Command loop */
        printf ("%s (%d)>  ", argv[0], getpid());
        while ((c = getchar())) {

	    switch (c) {
            case 'n':                       /*   new sequence		*/
		stime = smUtilTime();
                nimages = optargi ();
                nimages = max (1, nimages);
		for (i=0; i < nimages; i++) {
		    printf ("Generating readout %d (%d/%d) ....", 
			imnum++, smc->npages, smc->top);
        	    simMetaData ();
		        simData ();
		    simMetaData ();
		    printf ("done.\n");
		}
		etime = smUtilTime();
		printf ("Created %d images in %f sec\n",nimages,(etime-stime));
                break;

            case 'd':                       /*   detach from segment        */
                smcListPages (smc, -1);
                printf ("which page? ");  scanf ("%d", &pagenum);
                page = &smc->pdata[pagenum];
                if (page->ac_locked)
                    printf ("Page is locked for access.\n");
                else
                    smcDetach (smc, page, FALSE);
                break;

            case 'l':                       /*   list segments              */
                nsegs = optargi ();
                if (nsegs < 0 && smc->npages == 0) {
                    printf ("No pages yet allocated, showing first 8.\n");
                    smcListPages (smc, 8);
                } else
                    smcListPages (smc, nsegs);
                break;

            case 'i':                       /*   initialize cache           */
            case 'I':
                if (smc) {
		    int stat;
                    printf ("Initializing cache....\n");
                    stat = smcInitialize (smc);
                    if (stat && debug) 
                        smcPrintCacheInfo (smc, "After Initialize:");
                } else
                    printf ("Cannot initialize() before open()\n");
                break;

	    case '\n':
                printf ("%s (%d)>  ", argv[0], getpid());
	        continue;

            case 'p':                       /*   print cache info           */
                if (smc) {
                    smcPrintCacheInfo (smc, (char *)NULL);
                    smcPrintCfgInfo (&smc->sysConfig, (char *)NULL);
                } else
                    printf ("Null cache ptr, try opening a cache first.\n");
            break;


            case 'q':                       /*   quit                       */
            case 'C':                       /*   close cache                */
                printf ("free cache? ");    scanf ("%s", val);
                smc = smcClose (smc, is_true(val[0]));
                printf ("After smcClose(): smc = 0x%x\n", smc);
                break;

            case 'g':                       /*   print segment data         */
                smcListPages (smc, -1);
                printf ("which page? ");  scanf ("%d", &pagenum);
                page = &smc->pdata[pagenum];
                if (page->ac_locked)
                    printf ("Page is locked for access.\n");
                else {
                    printf ("how many lines? ");  scanf ("%d", &nvals);
                    if (page->type == TY_META) {
                        cdata = (char *)smcGetPageData (page);
                        for (cp=cdata, i=0; i < nvals; i++) {
                            printf ("%.80s", cp);
			    cp += 80;
			}
                    } else {
                        idata = (int *)smcGetPageData (page);
                        for (i=0; i < nvals; i++)
                            printf ("(%d) %s", idata[i],(i&&(i%4==0))?"\n":"");
                    }
                    printf ("\n");
                }
                break;


	    default:
	        printf ("Unknown command: '%c'\n", c);
	    }

            if (c == 'q' || c == 'C')
                break;
        }
    }

    printf ("Done\n");
    if (smc && !interactive) 
	smcClose (smc, clear);
}