#include <stdlib.h>
#include <string.h>
-#ifndef _MSC_VER
+#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
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);
}
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;
} /* if */
if (outf)
sc_closeasm(outf);
+ unlink (outfname);
if (binf)
sc_closebin(binf, errnum != 0);
/* 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;
{
/* 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)
char *ptr;
int len;
+ path[sizeof(path) - 1] = 0;
+
/* add the default "include" directory */
if (root)
{
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.
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);
* 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);
}
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 */
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 *
} /* 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);
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)
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)
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;
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];
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 */
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;
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 */
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;
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);
errorset(sEXPRRELEASE);
if (localstaging)
{
- stgout(index);
+ stgout(idx);
stgset(FALSE); /* stop staging */
} /* if */
}
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 */
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;
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 */
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);
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 */
{
int wq[wqSIZE], skiplab;
cell save_decl;
- int save_nestlevel, index;
+ int save_nestlevel, idx;
int *ptr;
save_decl = declared;
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 */
needtoken(')');
} /* if */
stgmark(sENDREORDER); /* mark end of reversed evaluation */
- stgout(index);
+ stgout(idx);
stgset(FALSE); /* stop staging */
statement(NULL, FALSE);
jumplabel(wq[wqLOOP]);
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)
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 */
static void
doassert(void)
{
- int flab1, index;
+ int flab1, idx;
cell cidx;
value lval = { NULL, 0, 0, 0, 0, NULL };
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 */