home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hugs101.zip / hugs101sc.zip / hugsdist / src / commonui.c < prev    next >
C/C++ Source or Header  |  1995-03-02  |  11KB  |  382 lines

  1. /* --------------------------------------------------------------------------
  2.  * commonui.c:  Copyright (c) Mark P Jones 1991-1994.   All rights reserved.
  3.  *              See NOTICE for details and conditions of use etc...
  4.  *              Hugs version 1.0 August 1994, derived from Gofer 2.30a
  5.  *
  6.  * Parts of user interface common to both compiler and interpreter.
  7.  * ------------------------------------------------------------------------*/
  8.  
  9. /* --------------------------------------------------------------------------
  10.  * Local function prototypes:
  11.  * ------------------------------------------------------------------------*/
  12.  
  13. static Void   local toggleSet          Args((Char,Bool));
  14. static Void   local togglesIn          Args((Bool));
  15. static Void   local optionInfo          Args((Void));
  16. static Void   local processOption     Args((String));
  17. static Int    local argToInt          Args((String *));
  18.  
  19. static Void   local loadProject       Args((String));
  20. static Void   local clearProject      Args((Void));
  21. static Void   local addScriptName     Args((String));
  22. static Void   local addScript          Args((String,Long));
  23. static Void   local forgetScriptsFrom Args((Module));
  24.  
  25. static Void   local setLastEdit       Args((String,Int));
  26.  
  27. static Void   local failed          Args((Void));
  28.  
  29. static String local strCopy          Args((String));
  30. static Int    local substr          Args((String,String));
  31.  
  32. /* --------------------------------------------------------------------------
  33.  * Local data areas:
  34.  * ------------------------------------------------------------------------*/
  35.  
  36. static String scriptName[NUM_MODULES];    /* Script file names           */
  37. static Int    numScripts;        /* Number of scripts loaded       */
  38. static Int    namesUpto;        /* Number of script names set       */
  39.  
  40. static String currProject = 0;        /* Name of current project file       */
  41. static Bool   projectLoaded = FALSE;    /* TRUE => project file loaded       */
  42. static String scriptFile;        /* Name of current script (if any) */
  43.  
  44. #if RISCOS
  45. static Bool   useDots    = TRUE;    /* TRUE => use dots in progress    */
  46. #else
  47. static Bool   useDots    = FALSE;    /* TRUE => use dots in progress    */
  48. #endif
  49. static String lastEdit     = 0;        /* Name of file to edit (if any)   */
  50. static Int    lastLine     = 0;        /* Editor line number (if possible)*/
  51. static String prompt     = 0;        /* Prompt string (gofer only)       */
  52. static String outputFile = 0;        /* User spec. output file (gofc)   */
  53.  
  54. /* --------------------------------------------------------------------------
  55.  * Command line options:
  56.  * ------------------------------------------------------------------------*/
  57.  
  58. struct options {            /* command line option toggles       */
  59.     char   c;                /* table defined in main app.       */
  60.     String description;
  61.     Bool   *flag;
  62. };
  63. extern struct options toggle[];
  64.  
  65. static Void local toggleSet(c,state)    /* Set command line toggle       */
  66. Char c;
  67. Bool state; {
  68.     Int i;
  69.     for (i=0; toggle[i].c; ++i)
  70.     if (toggle[i].c == c) {
  71.         *toggle[i].flag = state;
  72.         return;
  73.     }
  74.     ERROR(0) "Unknown toggle `%c'", c
  75.     EEND;
  76. }
  77.  
  78. static Void local togglesIn(state)    /* Print current list of toggles in*/
  79. Bool state; {                /* given state               */
  80.     Int count = 0;
  81.     Int i;
  82.     for (i=0; toggle[i].c; ++i)
  83.     if (*toggle[i].flag == state) {
  84.         if (count==0)
  85.         putchar(state ? '+' : '-');
  86.         putchar(toggle[i].c);
  87.         count++;
  88.     }
  89.     if (count>0)
  90.     putchar(' ');
  91. }
  92.  
  93. static Void local optionInfo() {    /* Print information about command */
  94.     static String fmts = "%-5s%s\n";    /* line settings           */
  95.     static String fmtc = "%-5c%s\n";
  96.     Int    i;
  97.  
  98.     printf("TOGGLES: groups begin with +/- to turn options on/off resp.\n");
  99.     for (i=0; toggle[i].c; ++i)
  100.     printf(fmtc,toggle[i].c,toggle[i].description);
  101.  
  102.     printf("\nOTHER OPTIONS: (leading + or - makes no difference)\n");
  103.     printf(fmts,"hnum","Set heap size (cannot be changed within Hugs)");
  104.     printf(fmts,"pstr","Set prompt string to str");
  105.     printf(fmts,"rstr","Set repeat last expression string to str");
  106. #ifdef TECH_TOGGLES
  107.     printf(fmts,"xnum","Set maximum depth for evidence search");
  108. #endif
  109.  
  110.     printf("\nCurrent settings: ");
  111.     togglesIn(TRUE);
  112.     togglesIn(FALSE);
  113. #ifdef TECH_TOGGLES
  114.     printf("-h%d -p%s -x%d -r%s\n",heapSize,prompt,maxEvidLevel,repeatStr);
  115. #else
  116.     printf("-h%d -p%s -r%s\n",heapSize,prompt,repeatStr);
  117. #endif
  118. }
  119.  
  120. static Void local processOption(s)    /* process option string s       */
  121. String s; {
  122.     Bool state = (s[0]=='+' ? TRUE : FALSE);
  123.  
  124.     while (*++s)
  125.     switch (*s) {
  126.         case 'n' : if (s[1]) {
  127.                if (outputFile) free(outputFile);
  128.                outputFile = strCopy(s+1);
  129.                }
  130.                return;
  131.  
  132.         case 'p' : if (s[1]) {
  133.                if (prompt) free(prompt);
  134.                prompt = strCopy(s+1);
  135.                }
  136.                return;
  137.  
  138.         case 'r' : if (s[1]) {
  139.                if (repeatStr) free(repeatStr);
  140.                repeatStr = strCopy(s+1);
  141.                }
  142.                return;    
  143.  
  144.         case 'h' : if (heapBuilt()) {
  145.                ERROR(0) "Cannot change heap size"
  146.                EEND;
  147.                }
  148.                heapSize = argToInt(&s);
  149.                if (heapSize<MINIMUMHEAP)
  150.                heapSize = MINIMUMHEAP;
  151.                else if (MAXIMUMHEAP && heapSize>MAXIMUMHEAP)
  152.                heapSize = MAXIMUMHEAP;
  153.                break;
  154.  
  155. #ifdef TECH_TOGGLES
  156.         case 'x' : maxEvidLevel = argToInt(&s);
  157.                break;
  158. #endif
  159.  
  160.         default  : toggleSet(*s,state);
  161.                break;
  162.     }
  163. }
  164.  
  165. static Int local argToInt(sp)        /* read integer from argument str  */
  166. String *sp; {
  167.     Int num = 0;
  168.     while (isascii((*sp)[1]) && isdigit((*sp)[1])) {
  169.     num = 10*num + (*(++*sp) - '0');
  170.     }
  171.     return num;
  172. }
  173.  
  174. /* --------------------------------------------------------------------------
  175.  * Loading project and script files:
  176.  * ------------------------------------------------------------------------*/
  177.  
  178. static Void local loadProject(s)    /* Load project file          */
  179. String s; {
  180.     clearProject();
  181.     currProject = s;
  182.     projInput(currProject);
  183.     scriptFile = currProject;
  184.     forgetScriptsFrom(1);
  185.     while (s=readFilename())
  186.     addScriptName(s);
  187.     if (namesUpto<=1) {
  188.     ERROR(0) "Empty project file"
  189.     EEND;
  190.     }
  191.     scriptFile    = 0;
  192.     projectLoaded = TRUE;
  193. }
  194.  
  195. static Void local clearProject() {     /* clear name for current project   */
  196.     if (currProject)
  197.     free(currProject);
  198.     currProject   = 0;
  199.     projectLoaded = FALSE;
  200. }
  201.  
  202. static Void local addScriptName(s)     /* add script name to list of files */
  203. String s; {                   /* to be read in ...           */
  204.     if (s[0]=='-' || s[0]=='+')
  205.     processOption(s);
  206.     else if (namesUpto>=NUM_MODULES) {
  207.     ERROR(0) "Too many script files (maximum of %d allowed)",
  208.          NUM_MODULES
  209.     EEND;
  210.     }
  211.     else
  212.     scriptName[namesUpto++] = strCopy(s);
  213. }
  214.  
  215. static Void local addScript(fname,len) /* read single script file       */
  216. String fname;                   /* name of script file           */
  217. Long   len; {                   /* length of script file        */
  218.     scriptFile = fname;
  219.  
  220.     printf("Reading script file \"%s\":\n",fname);
  221.     setLastEdit(fname,0);
  222.  
  223.     parseScript(fname,len);           /* process script file           */
  224.     checkDefns();
  225.     typeCheckDefns();    
  226.     compileDefns();
  227.  
  228.     scriptFile = 0;
  229. }
  230.  
  231. static Void local forgetScriptsFrom(scno)/* remove scripts from system       */
  232. Module scno; {
  233.     Module i;
  234.     for (i=scno; i<namesUpto; ++i)
  235.     if (scriptName[i])
  236.         free(scriptName[i]);
  237.     dropModulesFrom(scno-1);         /* don't count prelude as module  */
  238.     namesUpto = scno;
  239.     if (numScripts>0)
  240.     numScripts = scno;
  241. }
  242.  
  243. static Void local setLastEdit(fname,line)/* keep name of last file to edit */
  244. String fname;
  245. Int    line; {
  246.     if (lastEdit)
  247.     free(lastEdit);
  248.     lastEdit = strCopy(fname);
  249.     lastLine = line;
  250. }
  251.  
  252. /* --------------------------------------------------------------------------
  253.  * Display progress towards goal:
  254.  * ------------------------------------------------------------------------*/
  255.  
  256. static Target currTarget;
  257. static Bool   aiming = FALSE;
  258. static Int    currPos;
  259. static Int    maxPos;
  260. static Int    charCount;
  261.  
  262. Void setGoal(what, t)               /* Set goal for what to be t       */
  263. String what;
  264. Target t; {
  265.     currTarget = (t?t:1);
  266.     aiming     = TRUE;
  267.     if (useDots) {
  268.     currPos = strlen(what);
  269.     maxPos  = getTerminalWidth() - 1;
  270.     printf("%s",what);
  271.     }
  272.     else
  273.     for (charCount=0; *what; charCount++)
  274.         putchar(*what++);
  275.     fflush(stdout);
  276. }
  277.  
  278. Void soFar(t)                   /* Indicate progress towards goal   */
  279. Target t; {                   /* has now reached t           */
  280.     if (useDots) {
  281.     Int newPos = (Int)((maxPos * ((long)t))/currTarget);
  282.  
  283.     if (newPos>maxPos)
  284.         newPos = maxPos;
  285.  
  286.     if (newPos>currPos) {
  287.         do
  288.         putchar('.');
  289.         while (newPos>++currPos);
  290.         fflush(stdout);
  291.     }
  292.     fflush(stdout);
  293.     }
  294. }
  295.  
  296. Void done() {                   /* Goal has now been achieved       */
  297.     if (useDots) {
  298.     while (maxPos>currPos++)
  299.         putchar('.');
  300.     putchar('\n');
  301.     aiming = FALSE;
  302.     }
  303.     else
  304.     for (; charCount>0; charCount--) {
  305.         putchar('\b');
  306.         putchar(' ');
  307.         putchar('\b');
  308.     }
  309.     fflush(stdout);
  310. }
  311.  
  312. static Void local failed() {           /* Goal cannot be reached due to    */
  313.     if (aiming) {               /* errors               */
  314.     aiming = FALSE;
  315.     putchar('\n');
  316.     fflush(stdout);
  317.     }
  318. }
  319.  
  320. /* --------------------------------------------------------------------------
  321.  * Send message to each component of system:
  322.  * ------------------------------------------------------------------------*/
  323.  
  324. Void everybody(what)        /* send command `what' to each component of*/
  325. Int what; {            /* system to respond as appropriate ...    */
  326.     machdep(what);        /* The order of calling each component is  */
  327.     storage(what);        /* important for the INSTALL command       */
  328.     input(what);
  329.     staticAnalysis(what);
  330.     typeChecker(what);
  331.     compiler(what);
  332.     machine(what);
  333.     builtIn(what);
  334. }
  335.  
  336. /* --------------------------------------------------------------------------
  337.  * Read value from environment variable:
  338.  * ------------------------------------------------------------------------*/
  339.  
  340. String fromEnv(var,def)        /* return value of:                */
  341. String var;            /*     environment variable named by var   */
  342. String def; {            /* or: default value given by def       */
  343.     String s = getenv(var);
  344.  
  345.     return (s ? s : def);
  346. }
  347.  
  348. /* --------------------------------------------------------------------------
  349.  * String manipulation routines:
  350.  * ------------------------------------------------------------------------*/
  351.  
  352. static String local strCopy(s)           /* make malloced copy of a string   */
  353. String s; {
  354.     if (s) {
  355.     char *t,*r;
  356.     if ((t=(char *)malloc(strlen(s)+1))==0) {
  357.         ERROR(0) "String storage space exhausted"
  358.         EEND;
  359.     }
  360.     for (r=t; *r++ = *s++; )
  361.         ;
  362.     return t;
  363.     }
  364.     return s;
  365. }
  366.  
  367. static Int local substr(s1,s2)        /* find posn of substring s1 in s2 */
  368. String s1, s2; {            /* (naive implementation)       */
  369.     String t;
  370.  
  371.     for (t=s2; *t; t++) {
  372.     Int i = 0;
  373.         while (s1[i] && s1[i]==t[i])
  374.         i++;
  375.     if (s1[i]=='\0')
  376.         return t-s2;
  377.     }
  378.     return (-1);
  379. }
  380.  
  381. /*-------------------------------------------------------------------------*/
  382.