Skip to content

Commit 89c620e

Browse files
authored
Merge pull request #3 from stream1972/stream_devel
Support expressions of unlimited length on command line
2 parents 8da1d7d + 9c2dcfc commit 89c620e

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

src/Llr.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ int resn = 0, resnp = 0;
281281
char facnstr[80], facnpstr[80];
282282
char m_pgen_input[IBSIZE], m_pgen_output[IBSIZE], oldm_pgen_input[IBSIZE];
283283
char keywords[MAX_OPTIONS][IBSIZE], values[MAX_OPTIONS][IBSIZE];
284-
char multiplier[IBSIZE], base[IBSIZE], exponent[IBSIZE], exponent2[IBSIZE], addin[IBSIZE];
285284
char inifilebuf[IBSIZE];
286285
char sgd[sgkbufsize];
287286
char sgb[sgkbufsize];

src/lprime.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -402,89 +402,102 @@ int main (
402402

403403
case 'Q':
404404
case 'q':
405+
{
406+
char *multiplier, *base, *exponent, *addin, *exponent2;
407+
int exponent_len, exponent2_len;
408+
char *orig_p;
409+
410+
orig_p = p = strdup(p); // Will modity original argv string, better make a copy
411+
405412
if (!isdigit(*p)) // must begin with digits...
406413
goto errexpr;
407-
p2 = multiplier; // get the expected multiplier
414+
multiplier = p; // get the expected multiplier
408415
while (isdigit(*p)) // copy the digits
409-
*p2++ = *p++;
410-
*p2 = '\0';
416+
p++;
411417
if (*p == '\0') { // Multiplier alone
412-
strcpy (base, "2"); // No matter...
413-
strcpy (exponent, "0"); // Exponent = zero
414-
strcpy (addin, "+0"); // c = +zero
418+
base = "2"; // No matter...
419+
exponent = "0"; // Exponent = zero
420+
exponent_len = 1;
421+
addin = "+0"; // c = +zero
415422
goto DIGITSONLY;
416423
}
417424
if (*p == '^') { // the multiplier was ommitted
418-
strcpy (base, multiplier); // get the base in place
419-
strcpy (multiplier, "1"); // default multiplier = 1
420-
p++;
425+
base = multiplier; // get the base in place
426+
multiplier = "1"; // default multiplier = 1
421427
goto NOMULTIPLIER;
422428
}
423-
else if (*p++ != '*') // now, get the base
429+
if (*p != '*') // now, get the base
424430
goto errexpr;
431+
*p++ = '\0'; // terminate previous number and get the base
425432
if (!isdigit(*p)) // it must be a digit string
426433
goto errexpr;
427-
p2 = base;
434+
base = p;
428435
while (isdigit(*p)) // copy the digits
429-
*p2++ = *p++;
430-
*p2 = '\0';
431-
if (*p++ != '^') // must be power
436+
p++;
437+
if (*p != '^') // must be power
432438
goto errexpr;
433439
NOMULTIPLIER:
440+
*p++ = '\0'; // terminate previous number
434441
if (!isdigit(*p)) // must be digits...
435442
goto errexpr;
436-
p2 = exponent; // get the exponent
443+
exponent = p; // get the exponent
437444
while (isdigit(*p)) // copy the digits
438-
*p2++ = *p++;
439-
*p2 = '\0';
445+
p++;
446+
exponent_len = p - exponent; // cannot terminate exponent with \0 (need +/- for addin), so save its length
440447
if (*p != '+' && *p != '-') // must be plus or minus
441448
goto errexpr;
442-
p2 = addin;
443-
*p2++ = *p++; // copy the sign
449+
addin = p;
450+
p++; // copy the sign
444451
if (!isdigit(*p))
445452
goto errexpr;
446453
while (isdigit(*p)) // copy the c value
447-
*p2++ = *p++;
448-
*p2 = '\0';
449-
if ((*p != '\0') && ((addin[0] != '-') || (*p != '^'))) // must be end of string or diffnum...
450-
goto errexpr;
451-
dnflag = ' '; // restore the dnflag
452-
if (*p++ != '\0') {
453-
if (strcmp (base, addin+1)) // The second base must be the same...
454+
p++;
455+
if (*p == '\0') { // must be end of string or diffnum...
456+
dnflag = ' '; // restore the dnflag
457+
} else if (addin[0] == '-' && *p == '^') {
458+
*p++ = 0; // terminate second base
459+
if (strcmp (base, addin+1)) // The second base must be the same...
454460
goto errexpr;
455461
if (!isdigit(*p)) // must be digits...
456462
goto errexpr;
457-
p2 = exponent2; // get the exponent
463+
exponent2 = p; // get the exponent
458464
while (isdigit(*p)) // copy the digits
459-
*p2++ = *p++;
460-
*p2 = '\0';
465+
p++;
466+
exponent2_len = p - exponent2; // same problem with unterminated exponent
461467
if (*p != '+' && *p != '-') // must be plus or minus
462468
goto errexpr;
463469
dnflag = *p; // dnflag = sign
464-
p2 = addin;
465-
*p2++ = *p++; // copy the sign
470+
addin = p;
471+
p++; // copy the sign
466472
if (!isdigit(*p))
467473
goto errexpr;
468474
while (isdigit(*p)) // copy the c value
469-
*p2++ = *p++;
475+
p++;
476+
if (*p != '\0')
477+
goto errexpr;
478+
}
479+
else {
480+
goto errexpr;
470481
}
471-
*p2 = '\0';
472482
DIGITSONLY:
473483
in = fopen ("$temp.npg", "w"); // open the temporary input file
474484
if ((dnflag == '+') || (dnflag == '-')) {
475485
fprintf (in, "ABC $a^$b-$a^$c$d\n");// write ABC header and data
476-
fprintf (in, "%s %s %s %s\n", base, exponent, exponent2, addin);
486+
fprintf (in, "%s %.*s %.*s %s\n", base, exponent_len, exponent, exponent2_len, exponent2, addin);
477487
}
478488
else {
479489
fprintf (in, "ABC $a*$b^$c$d\n");// write ABC header and data
480-
fprintf (in, "%s %s %s %s\n", multiplier, base, exponent, addin);
490+
fprintf (in, "%s %s %.*s %s\n", multiplier, base, exponent_len, exponent, addin);
481491
}
482492
fclose (in);
483493
strcpy (m_pgen_input, "$temp.npg");
484494
strcpy (m_pgen_output, "$temp.res");
485495
PROCESSFILE = 1;
486496
SINGLETEST = 1;
497+
498+
free(orig_p);
487499
break;
500+
}
488501

489502
/* -H - help */
490503

0 commit comments

Comments
 (0)