tizen 2.3.1 release
[framework/uifw/embryo.git] / src / bin / embryo_cc_sc1.c
index 2beeb38..0437e88 100644 (file)
@@ -34,7 +34,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifndef _MSC_VER
+#ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 
@@ -119,31 +119,7 @@ static char         binfname[PATH_MAX];    /* binary file name */
 int
 main(int argc, char *argv[], char *env[] __UNUSED__)
 {
-   char                argv0[PATH_MAX];
-   int                 i;
-
-   snprintf(argv0, sizeof(argv0), "%s", argv[0]);
-   /* Linux stores the name of the program in argv[0], but not the path.
-    * To adjust this, I store a string with the path in argv[0]. To do
-    * so, I try to get the current path with getcwd(), and if that fails
-    * I search for the PWD= setting in the environment.
-    */
-   if (getcwd(argv0, PATH_MAX))
-     {
-       i = strlen(argv0);
-       snprintf(argv0 + i, sizeof(argv0) - i, "/%s", argv[0]);
-     }
-   else
-     {
-       char               *pwd = getenv("PWD");
-
-       if (pwd)
-          snprintf(argv0, sizeof(argv0), "%s/%s", pwd, argv[0]);
-     }                         /* if */
-   argv[0] = argv0;            /* set location to new first parameter */
-
-   e_prefix_determine(argv0);
-
+   e_prefix_determine(argv[0]);
    return sc_compile(argc, argv);
 }
 
@@ -328,8 +304,6 @@ sc_compile(int argc, char *argv[])
    if (fd_out < 0)
      error(101, outfname);
 
-   unlink (outfname); /* kill this file as soon as it's (f)close'd */
-
    setconfig(argv[0]);         /* the path to the include files */
    lcl_ctrlchar = sc_ctrlchar;
    lcl_packstr = sc_packstr;
@@ -433,6 +407,7 @@ sc_compile(int argc, char *argv[])
      }                         /* if */
    if (outf)
       sc_closeasm(outf);
+   unlink (outfname);
    if (binf)
       sc_closebin(binf, errnum != 0);
 
@@ -514,7 +489,7 @@ sc_addtag(char *name)
 
    /* tagname currently unknown, add it */
    tag = last + 1;             /* guaranteed not to exist already */
-   if (isupper(*name))
+   if (sc_isupper(*name))
       tag |= (int)FIXEDTAG;
    append_constval(&tagname_tab, name, (cell) tag, 0);
    return tag;
@@ -615,7 +590,8 @@ parseoptions(int argc, char **argv, char *iname, char *oname,
       {
         /* include directory */
         i++;
-        strncpy(str, argv[i], sizeof(str));
+        strncpy(str, argv[i], sizeof(str) -1);
+        str[sizeof(str) - 1] = '\0';
 
         len = strlen(str);
         if (str[len - 1] != DIRSEP_CHAR)
@@ -678,6 +654,8 @@ setconfig(char *root)
    char               *ptr;
    int                 len;
 
+   path[sizeof(path) - 1] = 0;
+
    /* add the default "include" directory */
    if (root)
      {
@@ -828,7 +806,7 @@ parse(void)
          case tSTATIC:
             /* This can be a static function or a static global variable;
              * we know which of the two as soon as we have parsed up to the
-             * point where an opening paranthesis of a function would be
+             * point where an opening parenthesis of a function would be
              * expected. To back out after deciding it was a declaration of
              * a static variable after all, we have to store the symbol name
              * and tag.
@@ -1572,7 +1550,8 @@ decl_const(int vclass)
    if (lex(&val, &str) != tSYMBOL)     /* read in (new) token */
       error(20, str);          /* invalid symbol name */
    symbolline = fline;         /* save line where symbol was found */
-   strcpy(constname, str);     /* save symbol name */
+   strncpy(constname, str, sizeof(constname) - 1);     /* save symbol name */
+   constname[sizeof(constname) - 1] = 0;
    needtoken('=');
    constexpr(&val, &exprtag);  /* get value */
    needtoken(tTERM);
@@ -1622,7 +1601,8 @@ decl_enum(int vclass)
     * tag was set) */
    if (lex(&val, &str) == tSYMBOL)
      {                         /* read in (new) token */
-       strcpy(enumname, str);  /* save enum name (last constant) */
+       strncpy(enumname, str, sizeof(enumname) - 1);   /* save enum name (last constant) */
+       enumname[sizeof(enumname) - 1] = 0;
        if (!explicittag)
           tag = sc_addtag(enumname);
      }
@@ -1667,7 +1647,8 @@ decl_enum(int vclass)
        tok = lex(&val, &str);  /* read in (new) token */
        if (tok != tSYMBOL && tok != tLABEL)
           error(20, str);      /* invalid symbol name */
-       strcpy(constname, str); /* save symbol name */
+       strncpy(constname, str, sNAMEMAX); /* save symbol name */
+       constname[sNAMEMAX] = 0;
        size = increment;       /* default increment of 'val' */
        if (tok == tLABEL || matchtoken(':'))
           constexpr(&size, NULL);      /* get size */
@@ -1972,7 +1953,7 @@ tag2str(char *dest, int tag)
    tag &= TAGMASK;
    assert(tag >= 0);
    sprintf(dest, "0%x", tag);
-   return isdigit(dest[1]) ? &dest[1] : dest;
+   return sc_isdigit(dest[1]) ? &dest[1] : dest;
 }
 
 char       *
@@ -2018,7 +1999,7 @@ parse_funcname(char *fname, int *tag1, int *tag2, char *opname)
      }                         /* if */
    assert(!unary || *tag1 == 0);
    assert(*ptr != '\0');
-   for (name = opname; !isdigit(*ptr);)
+   for (name = opname; !sc_isdigit(*ptr);)
       *name++ = *ptr++;
    *name = '\0';
    *tag2 = (int)strtol(ptr, NULL, 16);
@@ -2033,7 +2014,7 @@ funcdisplayname(char *dest, char *funcname)
    constvalue         *tagsym[2];
    int                 unary;
 
-   if (isalpha(*funcname) || *funcname == '_' || *funcname == PUBLIC_CHAR
+   if (sc_isalpha(*funcname) || *funcname == '_' || *funcname == PUBLIC_CHAR
        || *funcname == '\0')
      {
        if (dest != funcname)
@@ -2493,7 +2474,8 @@ declargs(symbol * sym)
               case tSYMBOL:
                  if (argcnt >= sMAXARGS)
                     error(45); /* too many function arguments */
-                 strcpy(name, ptr);    /* save symbol name */
+                 strncpy(name, ptr, sizeof(name) - 1); /* save symbol name */
+                 name[sizeof(name) - 1] = 0;
                  if (name[0] == PUBLIC_CHAR)
                     error(56, name);   /* function arguments cannot be public */
                  if (numtags == 0)
@@ -2649,7 +2631,8 @@ doarg(char *name, int ident, int offset, int tags[], int numtags,
    cell                size;
    int                 idxtag[sDIMEN_MAX];
 
-   strcpy(arg->name, name);
+   strncpy(arg->name, name, sizeof(arg->name) - 1);
+   arg->name[sizeof(arg->name) - 1] = 0;
    arg->hasdefault = FALSE;    /* preset (most common case) */
    arg->defvalue.val = 0;      /* clear */
    arg->defvalue_tag = 0;
@@ -2705,7 +2688,7 @@ doarg(char *name, int ident, int offset, int tags[], int numtags,
                  arg->hasdefault = TRUE;       /* argument has default value */
                  arg->defvalue.array.size = litidx;
                  arg->defvalue.array.addr = -1;
-                 /* calulate size to reserve on the heap */
+                 /* calculate size to reserve on the heap */
                  arg->defvalue.array.arraysize = 1;
                  for (i = 0; i < arg->numdim; i++)
                     arg->defvalue.array.arraysize *= arg->dim[i];
@@ -2989,7 +2972,7 @@ destructsymbols(symbol * root, int level)
             operator_symname(symbolname, "~", sym->tag, 0, 1, 0);
             if ((opsym = findglb(symbolname)))
               {
-                 /* save PRI, in case of a return statment */
+                 /* save PRI, in case of a return statement */
                  if (!savepri)
                    {
                       push1(); /* right-hand operand is in PRI */
@@ -3035,23 +3018,24 @@ destructsymbols(symbol * root, int level)
 
 static constvalue  *
 insert_constval(constvalue * prev, constvalue * next, char *name,
-               cell val, short index)
+               cell val, short idx)
 {
    constvalue         *cur;
 
    if (!(cur = (constvalue *)malloc(sizeof(constvalue))))
       error(103);              /* insufficient memory (fatal error) */
    memset(cur, 0, sizeof(constvalue));
-   strcpy(cur->name, name);
+   strncpy(cur->name, name, sizeof(cur->name) - 1);
+   cur->name[sizeof(cur->name) - 1] = 0;
    cur->value = val;
-   cur->index = index;
+   cur->index = idx;
    cur->next = next;
    prev->next = cur;
    return cur;
 }
 
 constvalue *
-append_constval(constvalue * table, char *name, cell val, short index)
+append_constval(constvalue * table, char *name, cell val, short idx)
 {
    constvalue         *cur, *prev;
 
@@ -3059,17 +3043,17 @@ append_constval(constvalue * table, char *name, cell val, short index)
    for (prev = table, cur = table->next; cur;
        prev = cur, cur = cur->next)
       /* nothing */ ;
-   return insert_constval(prev, NULL, name, val, index);
+   return insert_constval(prev, NULL, name, val, idx);
 }
 
 constvalue *
-find_constval(constvalue * table, char *name, short index)
+find_constval(constvalue * table, char *name, short idx)
 {
    constvalue         *ptr = table->next;
 
    while (ptr)
      {
-       if (strcmp(name, ptr->name) == 0 && ptr->index == index)
+       if (strcmp(name, ptr->name) == 0 && ptr->index == idx)
           return ptr;
        ptr = ptr->next;
      }                         /* while */
@@ -3351,7 +3335,7 @@ static void
 doexpr(int comma, int chkeffect, int allowarray, int mark_endexpr,
        int *tag, int chkfuncresult)
 {
-   int                 constant, index, ident;
+   int                 constant, idx, ident;
    int                 localstaging = FALSE;
    cell                val;
 
@@ -3361,12 +3345,12 @@ doexpr(int comma, int chkeffect, int allowarray, int mark_endexpr,
        localstaging = TRUE;
        assert(stgidx == 0);
      }                         /* if */
-   index = stgidx;
+   idx = stgidx;
    errorset(sEXPRMARK);
    do
      {
        /* on second round through, mark the end of the previous expression */
-       if (index != stgidx)
+       if (idx != stgidx)
           endexpr(TRUE);
        sideeffect = FALSE;
        ident = expression(&constant, &val, tag, chkfuncresult);
@@ -3381,7 +3365,7 @@ doexpr(int comma, int chkeffect, int allowarray, int mark_endexpr,
    errorset(sEXPRRELEASE);
    if (localstaging)
      {
-       stgout(index);
+       stgout(idx);
        stgset(FALSE);          /* stop staging */
      }                         /* if */
 }
@@ -3391,14 +3375,14 @@ doexpr(int comma, int chkeffect, int allowarray, int mark_endexpr,
 int
 constexpr(cell * val, int *tag)
 {
-   int                 constant, index;
+   int                 constant, idx;
    cell                cidx;
 
    stgset(TRUE);               /* start stage-buffering */
-   stgget(&index, &cidx);      /* mark position in code generator */
+   stgget(&idx, &cidx);        /* mark position in code generator */
    errorset(sEXPRMARK);
    expression(&constant, val, tag, FALSE);
-   stgdel(index, cidx);                /* scratch generated code */
+   stgdel(idx, cidx);          /* scratch generated code */
    stgset(FALSE);              /* stop stage-buffering */
    if (constant == 0)
       error(8);                        /* must be constant expression */
@@ -3420,7 +3404,7 @@ constexpr(cell * val, int *tag)
 static void
 test(int label, int parens, int invert)
 {
-   int                 index, tok;
+   int                 idx, tok;
    cell                cidx;
    value               lval = { NULL, 0, 0, 0, 0, NULL };
    int                 localstaging = FALSE;
@@ -3430,9 +3414,9 @@ test(int label, int parens, int invert)
        stgset(TRUE);           /* start staging */
        localstaging = TRUE;
 #if !defined NDEBUG
-       stgget(&index, &cidx);  /* should start at zero if started
+       stgget(&idx, &cidx);    /* should start at zero if started
                                 * locally */
-       assert(index == 0);
+       assert(idx == 0);
 #endif
      }                         /* if */
 
@@ -3442,7 +3426,7 @@ test(int label, int parens, int invert)
       needtoken('(');
    do
      {
-       stgget(&index, &cidx);  /* mark position (of last expression) in
+       stgget(&idx, &cidx);    /* mark position (of last expression) in
                                 * code generator */
        if (hier14(&lval))
           rvalue(&lval);
@@ -3462,7 +3446,7 @@ test(int label, int parens, int invert)
    if (lval.ident == iCONSTEXPR)
      {                         /* constant expression */
        intest = (int)(long)popstk();   /* restore stack */
-       stgdel(index, cidx);
+       stgdel(idx, cidx);
        if (lval.constval)
          {                     /* code always executed */
             error(206);        /* redundant test: always non-zero */
@@ -3574,7 +3558,7 @@ dofor(void)
 {
    int                 wq[wqSIZE], skiplab;
    cell                save_decl;
-   int                 save_nestlevel, index;
+   int                 save_nestlevel, idx;
    int                *ptr;
 
    save_decl = declared;
@@ -3620,7 +3604,7 @@ dofor(void)
    assert(!staging);
    stgset(TRUE);               /* start staging */
    assert(stgidx == 0);
-   index = stgidx;
+   idx = stgidx;
    stgmark(sSTARTREORDER);
    stgmark((char)(sEXPRSTART + 0));    /* mark start of 2nd expression
                                         * in stage */
@@ -3639,7 +3623,7 @@ dofor(void)
        needtoken(')');
      }                         /* if */
    stgmark(sENDREORDER);       /* mark end of reversed evaluation */
-   stgout(index);
+   stgout(idx);
    stgset(FALSE);              /* stop staging */
    statement(NULL, FALSE);
    jumplabel(wq[wqLOOP]);
@@ -3797,7 +3781,7 @@ doswitch(void)
    while (tok != '}');
 
 #if !defined NDEBUG
-   /* verify that the case table is sorted (unfortunatly, duplicates can
+   /* verify that the case table is sorted (unfortunately, duplicates can
     * occur; there really shouldn't be duplicate cases, but the compiler
     * may not crash or drop into an assertion for a user error). */
    for (cse = caselist.next; cse && cse->next; cse = cse->next)
@@ -3813,12 +3797,14 @@ doswitch(void)
    if (swdefault == FALSE)
      {
        /* store lbl_exit as the "none-matched" label in the switch table */
-       strcpy(labelname, itoh(lbl_exit));
+       strncpy(labelname, itoh(lbl_exit), sizeof(labelname) - 1);
+        labelname[sizeof(labelname) - 1] = 0;
      }
    else
      {
        /* lbl_case holds the label of the "default" clause */
-       strcpy(labelname, itoh(lbl_case));
+        strncpy(labelname, itoh(lbl_case), sizeof(labelname) - 1);
+        labelname[sizeof(labelname) - 1] = 0;
      }                         /* if */
    ffcase(casecount, labelname, TRUE);
    /* generate the rest of the table */
@@ -3832,7 +3818,7 @@ doswitch(void)
 static void
 doassert(void)
 {
-   int                 flab1, index;
+   int                 flab1, idx;
    cell                cidx;
    value               lval = { NULL, 0, 0, 0, 0, NULL };
 
@@ -3849,12 +3835,12 @@ doassert(void)
    else
      {
        stgset(TRUE);           /* start staging */
-       stgget(&index, &cidx);  /* mark position in code generator */
+       stgget(&idx, &cidx);    /* mark position in code generator */
        do
          {
             if (hier14(&lval))
                rvalue(&lval);
-            stgdel(index, cidx);       /* just scrap the code */
+            stgdel(idx, cidx); /* just scrap the code */
          }
        while (matchtoken(','));
        stgset(FALSE);          /* stop staging */