esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#%TITLE% PUTCONF.MAC
#%NAME%
#  Utility functions for save/load configurations 
#
#%DESCRIPTION%
#  This macro file provides convenient functions for loading and saving
#  SPEC configuration. Be aware that only config files are saved and not
#  settings file: motor positions and limits are then not saved with the
#  config and they should be checked carefully when the configuration is
#  reloaded. 
#
#%EXAMPLE%
#%DL%
#%DT%putconf%DD% Save a configuration
#%DT%getconf%DD% Load a previously saved configuration
#%XDL%
#%END%

#%UU% [ conf-name ] 
#%MDESC%
#  Save configuration
#
def putconf '{

  local filename cname configfile confdir 
  
  if ($# == 0) {
     printf("\n")
     tty_cntl("md")
     printf(" <PutConf>\n")
     tty_cntl("me")
     cname = getval("\tName","")
  } else {
     cname = "$1"
  }

  confdir    = sprintf("%s/local/spec/userconf",BLISSADM)
  filename   = sprintf("%s/%s",confdir,cname)
  configfile = sprintf("%s/%s/config",SPECD,SPEC)

  if (file_info(filename)) {
     if (file_info(filename,"-w") && ($# == 0)) {
        printf("\t  File \"%s\" exists. \n",filename)
        _yn=yesno("\t  Do you want to overwrite it",0) 
        if (!_yn) exit
        close(filename)
        unix(sprintf("/bin/rm %s\n",filename))
     } else {
        printf("\t  File \"%s\" exists but is protected.\n",filename)
        exit
     }
  } else {
     if (!file_info(confdir,"-w")) {
        printf("\t  Directory %s is protected. Sorry\n",confdir)
        exit 
     }
  }
  

  open(filename)
  on(filename);offt
    printf("#%%CONFIG%% %s\n",cname)
    printf("#%%SPEC%% %s\n",SPEC)
    printf("#%%USER%% %s\n",USER)
    printf("#%%DATE%% %s\n",date())
    printf("#%%COMMENT%%\n")
  ont;off(filename)

  if ($# == 0) {
     printf("\n\t Enter a description:\n")
     while ( (var=input()) != "" ) {
        fprintf(filename,"#  %s\n",var)  
     }
  }

  on(filename);offt
    printf("#%%END%%\n")
    printf("#\n")
  ont;off(filename)

  unix(sprintf("cat %s >> %s",configfile,filename))
  unix(sprintf("chmod 777 %s 2>/dev/null",filename))

  close(filename)
}'

#%UU% [ conf-name ]
#%MDESC%
# Loads a previously saved configuration
def getconf '
  {
    local filename cname configfile confdir 

    if ($# == 0) {
        if ((cname = lsconf()) == -1 ) exit
    } else {
        cname = "$1"
    }

    confdir    = sprintf("%s/local/spec/userconf",BLISSADM)
    filename   = sprintf("%s/%s",confdir,cname)
    configfile = sprintf("%s/%s/config",SPECD,SPEC)


    wait(-1)
    user_waitall
    sync
    if (_extr_config(filename,configfile) == -1) {
       exit
    }
  }

   reconfig
   user_config
   _assign
   _assign_mono
'

#%IU%
#%MDESC%
#
def _extr_config(from,to) '{
    local tmpfile  
     
    tmpfile=sprintf("/tmp/config_%s.%s",SPEC,USER)

    if ( file_info(from,"-r") == 0 ) {
         printf("getconf: error reading %s file\n",from)
         return(-1)
    }

    linect=0
    while( (line = getline(from)) != -1 ) {
        if (linect==0) {
            if (index(line,"CONFIG")) {
              linect++
              header=1
            } else {
              printf("getconf: %s does not seem to be a config file\n",from)
              return(-1)
            }
        } 

        if (header==0)  {
          on(tmpfile);offt; print line;ont;off(tmpfile)
        }
        if (index(line,"END"))  header=0
    }
    getline(from,"close")
    close(tmpfile)

    unix(sprintf("cp %s %s",tmpfile,to))
    unix(sprintf("/bin/rm %s",tmpfile))
}'

#%IU%
#%MDESC%
#
def lsconf() '{
    local tmpfile confdir
    string array filelist[100][30]

    confdir    = sprintf("%s/local/spec/userconf",BLISSADM)
    tmpfile    = sprintf("/tmp/conf_%s_%s",SPEC,USER)

    unix(sprintf("/bin/ls %s > %s",confdir,tmpfile))

    filect=0

    while ( (onefile=getline(tmpfile)) != -1 ) {
         filelist[filect]=substr(onefile,0,index(onefile,"\n")-1)
         filect++
    }
    getline(tmpfile,"close")

    printf("\n  ")
    tty_cntl("us")
    printf("  %18s  %8s  %8s  %s%20s\n","File","SPEC","User","Date","")
    tty_cntl("ue")

    for (kk=0;kk<filect;kk++) {
         filename=sprintf("%s/%s",confdir,filelist[kk])
         linect=0
         c_omm=0
         fspec="";fdate="";fuser=""
         while ((line=getline(filename)) != -1) {
             if (linect==0) {
                 if (index(line,"CONFIG")) {
                     linect++
                 } else {
                     break
                 }
             }         
             if (idx=index(line,"SPEC")) { 
                fspec=substr(line,idx+6)
                fspec=substr(fspec,0,index(fspec,"\n")-1)
             }

             if (idx=index(line,"USER")) {
                fuser=substr(line,idx+6)
                fuser=substr(fuser,0,index(fuser,"\n")-1)
             }

             if (idx=index(line,"DATE")) {
                fdate=substr(line,idx+6)
                fdate=substr(fdate,0,index(fdate,"\n")-1)
             }

             if (index(line,"END")) {
                if (c_omm==0) {
                   tty_cntl("md");printf("  %20s ",filelist[kk]);tty_cntl("me")
                   printf(" %8s  %8s  %s\n",fspec,fuser,fdate)
                }
                getline(filename,"close")
                break
             }

             if (c_omm) printf("%30s%s","",substr(line,2))

             if (index(line,"COMMENT")) {
                tty_cntl("md");printf("  %20s ",filelist[kk]);tty_cntl("me")
                printf(" %8s  %8s  %s\n",fspec,fuser,fdate)
                c_omm=1
             }

         }
    }

    printf("\n\n\tChoose file: ")

    if ( (selection=input()) !="") { 
         return(selection) 
    } else {
         return(-1)
    }

    unix(sprintf("/bin/rm %s",tmpfile))
}'

#%MACROS%
#%IMACROS%
#%AUTHOR%
#  PUTCONF.MAC Vicente Rey 11/98
#%TOC%