@@ -118,6 +118,9 @@ static void reduce_referrers(symbol *root);
118118static long max_stacksize (symbol * root ,int * recursion );
119119static int testsymbols (symbol * root ,int level ,int testlabs ,int testconst );
120120static void destructsymbols (symbol * root ,int level );
121+ static void store_builtin_string (const char * name ,const char * value );
122+ static void register_builtin_string (void );
123+ static void unregister_builtin_string (void );
121124static constvalue * find_constval_byval (constvalue_root * table ,cell val );
122125static symbol * fetchlab (char * name );
123126static void statement (int * lastindent ,int allow_decl );
@@ -796,6 +799,7 @@ int pc_compile(int argc, char *argv[])
796799 #endif
797800 delete_autolisttable ();
798801 delete_heaplisttable ();
802+ unregister_builtin_string ();
799803 if (errnum != 0 ) {
800804 if (strlen (errfname )== 0 )
801805 pc_printf ("\n%d Error%s.\n" ,errnum ,(errnum > 1 ) ? "s" : "" );
@@ -1252,8 +1256,17 @@ static void parseoptions(int argc,char **argv,char *oname,char *ename,char *pnam
12521256 error (200 ,argv [arg ],sNAMEMAX ); /* symbol too long, truncated to sNAMEMAX chars */
12531257 } /* if */
12541258 strlcpy (str ,argv [arg ],i + 1 ); /* str holds symbol name */
1255- i = atoi (ptr + 1 );
1256- add_builtin_constant (str ,i ,sGLOBAL ,0 );
1259+ if (* (ptr + 1 )== '\"' ) { /* string constant */
1260+ char strval [_MAX_PATH ];
1261+ strlcpy (strval ,ptr + 2 ,sizeof (strval )); /* copy string value (skip =") */
1262+ int len = strlen (strval );
1263+ if (len > 0 && strval [len - 1 ]== '\"' ) /* remove trailing quote */
1264+ strval [len - 1 ]= '\0' ;
1265+ store_builtin_string (str ,strval ); /* store as string constant */
1266+ } else { /* integar constant */
1267+ i = atoi (ptr + 1 ); /* convert numeric value */
1268+ add_builtin_constant (str ,i ,sGLOBAL ,0 ); /* store as integer constant */
1269+ }
12571270 } else if (oname ) {
12581271 strlcpy (str ,argv [arg ],sizeof (str )- 2 ); /* -2 because default extension is ".p" */
12591272 set_extension (str ,".p" ,FALSE);
@@ -1592,6 +1605,68 @@ static void setstringconstants()
15921605 add_builtin_string_constant ("__time" ,timebuf ,sGLOBAL );
15931606 strftime (datebuf ,sizeof (datebuf ),"%d %b %Y" ,localtime (& now ));
15941607 add_builtin_string_constant ("__date" ,datebuf ,sGLOBAL );
1608+
1609+ register_builtin_string ();
1610+ }
1611+
1612+ static void store_builtin_string (const char * name ,const char * value )
1613+ {
1614+ builtinstring * tbl = & builtin_strings ;
1615+ int newsize ;
1616+ void * p ;
1617+
1618+ assert (tbl != NULL );
1619+
1620+ /* Expand the array if necessary */
1621+ if (tbl -> count >= tbl -> size ) {
1622+ newsize = (tbl -> size == 0 ) ? 2 : tbl -> size * 2 ;
1623+ p = realloc (tbl -> entries ,newsize * sizeof (* tbl -> entries ));
1624+
1625+ if (p == NULL ) {
1626+ error (103 ); /* insufficient memory */
1627+ return ;
1628+ }
1629+
1630+ tbl -> entries = p ;
1631+ tbl -> size = newsize ;
1632+ }
1633+
1634+ /*
1635+ * Copy the name and value into the next available slot
1636+ */
1637+ strlcpy (tbl -> entries [tbl -> count ].name ,name ,sizeof (tbl -> entries [tbl -> count ].name ));
1638+ strlcpy (tbl -> entries [tbl -> count ].value ,value ,sizeof (tbl -> entries [tbl -> count ].value ));
1639+ tbl -> count ++ ;
1640+ }
1641+
1642+ static void register_builtin_string (void )
1643+ {
1644+ builtinstring * tbl = & builtin_strings ;
1645+
1646+ assert (tbl != NULL );
1647+
1648+ /*
1649+ * Add each remembered string constant to the global symbol table
1650+ */
1651+ for (int i = 0 ; i < tbl -> count ; ++ i ) {
1652+ char * name = tbl -> entries [i ].name ;
1653+ const char * value = tbl -> entries [i ].value ;
1654+ int vclass = sGLOBAL ;
1655+ add_builtin_string_constant (name , value , vclass );
1656+ }
1657+ }
1658+
1659+ static void unregister_builtin_string (void )
1660+ {
1661+ builtinstring * tbl = & builtin_strings ;
1662+
1663+ assert (tbl != NULL );
1664+
1665+ /* Release the entries array and reset counters */
1666+ free (tbl -> entries );
1667+ tbl -> entries = NULL ;
1668+ tbl -> count = 0 ;
1669+ tbl -> size = 0 ;
15951670}
15961671
15971672static int getclassspec (int initialtok ,int * fpublic ,int * fstatic ,int * fstock ,int * fconst )
0 commit comments