File: io-utils.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (1195 lines) | stat: -rw-r--r-- 22,131 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
/*	Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.

This file is part of Oleo, the GNU Spreadsheet.

Oleo is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Oleo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Oleo; see the file COPYING.  If not, write to the Free Software Foundation, 675
Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <ctype.h>
#include "sysdef.h"
#include "io-utils.h"
#include "cell.h"
#include "ref.h"
#include "decompile.h"
#include "io-generic.h"
#include "io-abstract.h"
#include "lists.h"
#include "io-term.h"
#include "cmd.h"


/* Routines for formatting cell values */
struct user_fmt;
static char *pr_flt (double, struct user_fmt *, int);
static char *pr_int (long, struct user_fmt *, int);


/* Constants */
char *bname[] =
{
  "#FALSE", "#TRUE"
};

char numb_oflo[] = "########################################";

double __plinf;
double __neinf;
double __nan;

char nname[] = "#NOT_A_NUMBER";
char iname[] = "#INFINITY";
char mname[] = "#MINUS_INFINITY";

static double
divide (double a,
	double b)
{
  return a / b;
}

static RETSIGTYPE
ignore_sig (int sig)
{
  (void)signal (SIGFPE, ignore_sig);
}

/* It's ok of this fails and generates signals.  In that case, 
 * the same signal will occur when evaluating formulas and a
 * (less informative) error value substituted.  Note that this 
 * should be called before init_eval.
 */
void
init_infinity (void)
{
  (void)signal (SIGFPE, ignore_sig);
  __plinf = divide (1., 0.);
  (void)signal (SIGFPE, ignore_sig);
  __neinf = divide (-1., 0.);
  (void)signal (SIGFPE, ignore_sig);
  __nan = __plinf + __neinf;
}



/* Slightly larger than the maximum exponent we ever expect to see */
#define BIGFLT 309
#ifdef TEST
char print_buf[1024 * 8];
#else
char print_buf[BIGFLT + 20];
#endif



/* Structures/vars/functions for dealing with formatting floating-point
   numbers, etc */

struct user_fmt
  {
    char *p_hdr, *n_hdr;
    char *p_trl, *n_trl;
    char *zero, *comma, *decpt;
    unsigned char prec;
    double scale;
  };


struct user_fmt dol =
{
  "$", "($", 0, ")", "$0", ",", ".", PRC_FLT, 1};

struct user_fmt cma =
{
  0, "(", 0, ")", "0", ",", ".", PRC_FLT, 1};

struct user_fmt pct =
{
  0, "-", "%", "%", "0%", 0, ".", PRC_FLT, 100};

struct user_fmt fxt =
{
  0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1};

/* Variables */

struct user_fmt u[16] =
{
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
  {0, "-", 0, 0, "0", 0, ".", PRC_FLT, 1},
};


/* Turn a floating-point number into the canonical text form.  This scribbles
   on print_buf */

char *
flt_to_str (double val)
{
  double f;

  if (val == __plinf)
    return iname;
  if (val == __neinf)
    return mname;
  f = fabs (val);
  if (f >= 1e6 || (f > 0 && f <= 9.9999e-6))
    {
      sprintf (print_buf, "%e", val);
      return print_buf;
    }
  return pr_flt (val, &fxt, PRC_FLT);
}


char *
long_to_str (long val)
{
  sprintf (print_buf, "%ld", val);
  return print_buf;
}

/* create the human-readable version of the contents of a cell
   This scribbles on print-buf bigtime */

char *
print_cell (CELL *cp)
{
  int j;
  int p;
  long num;
  static char zeroes[] = "000000000000000";

  if (!cp)
    return "";

  j = GET_FMT (cp);

  if (j == FMT_DEF)
    j = default_fmt;
  if (j == FMT_HID || GET_TYP (cp) == 0)
    return "";

  if (GET_TYP (cp) == TYP_STR)
    return cp->cell_str;
  if (GET_TYP (cp) == TYP_BOL)
    {
#ifdef TEST
      if (cp->cell_bol < 0 || cp->cell_bol > 1)
	panic ("Bool %d out of range", cp->cell_bol);
#endif
      return bname[cp->cell_bol];
    }
  if (GET_TYP (cp) == TYP_ERR)
    {
#ifdef TEST
      if (cp->cell_err > ERR_MAX || cp->cell_err < 0)
	panic ("Error %d out of range", cp->cell_err);
#endif
      return ename[cp->cell_err];
    }
  if (GET_TYP (cp) == TYP_FLT)
    {
      p = GET_PRC (j);
      switch (j | PRC_FLT)
	{
	case FMT_GPH:
	  if (cp->cell_flt < 0)
	    {
	      j = '-';
	      num = -(cp->cell_flt);
	    }
	  else if (cp->cell_flt >= 1)
	    {
	      j = '+';
	      num = (cp->cell_flt);
	    }
	  else
	    {
	      j = '0';
	      num = 1;
	    }
	graph:
	  if (num >= sizeof (print_buf))
	    {
	      io_error_msg ("Cannot graph %d '%c'", p, j);
	      num = sizeof (print_buf) - 1;
	    }
	  print_buf[num] = '\0';
	  while (--num >= 0)
	    print_buf[num] = j;
	  return print_buf;

	case FMT_USR:
	  return pr_flt (cp->cell_flt, &u[p], u[p].prec);

	case FMT_GEN:
	  {
	    double f;

	    f = fabs (cp->cell_flt);
	    if (f >= 1e6 || (f > 0 && f <= 9.9999e-6))
	      goto handle_exp;
	    return pr_flt (cp->cell_flt, &fxt, p);
	  }

	case FMT_DOL:
	  return pr_flt (cp->cell_flt, &dol, p);

	case FMT_CMA:
	  return pr_flt (cp->cell_flt, &cma, p);

	case FMT_PCT:
	  return pr_flt (cp->cell_flt, &pct, p);

	case FMT_FXT:
	  return pr_flt (cp->cell_flt, &fxt, p);

	case FMT_EXP:
	handle_exp:
	  if (cp->cell_flt == __plinf)
	    return iname;
	  if (cp->cell_flt == __neinf)
	    return mname;
	  if (p == PRC_FLT)
	    sprintf (print_buf, "%e", cp->cell_flt);
	  else
	    sprintf (print_buf, "%.*e", p, cp->cell_flt);
	  return print_buf;
#ifdef TEST
	default:
	  panic ("Unknown format %d", j);
	  return 0;
#endif
	}
    }

  if (GET_TYP (cp) == TYP_INT)
    {
      p = GET_PRC (j);
      switch (j | PRC_FLT)
	{
	case FMT_GPH:
	  if (cp->cell_int < 0)
	    {
	      j = '-';
	      num = -(cp->cell_int);
	    }
	  else if (cp->cell_int >= 1)
	    {
	      j = '+';
	      num = (cp->cell_int);
	    }
	  else
	    {
	      j = '0';
	      num = 1;
	    }
	  goto graph;

	case FMT_USR:
	  return pr_int (cp->cell_int, &u[p], u[p].prec);

	case FMT_GEN:
	  sprintf (print_buf, "%ld", cp->cell_int);
	  return print_buf;

	case FMT_DOL:
	  return pr_int (cp->cell_int, &dol, p);

	case FMT_CMA:
	  return pr_int (cp->cell_int, &cma, p);

	case FMT_PCT:
	  return pr_int (cp->cell_int, &pct, p);

	case FMT_FXT:
	  if (p != PRC_FLT && p != 0)
	    sprintf (print_buf, "%ld.%.*s", cp->cell_int, p, zeroes);
	  else
	    sprintf (print_buf, "%ld", cp->cell_int);
	  return print_buf;

	case FMT_EXP:
	  if (p != PRC_FLT)
	    sprintf (print_buf, "%.*e", p, (double) (cp->cell_int));
	  else
	    sprintf (print_buf, "%e", (double) (cp->cell_int));
	  return print_buf;
#ifdef TEST
	default:
	  panic ("Unknown format %d", j);
	  return 0;
#endif
	}
    }
#ifdef TEST
  panic ("Unknown cell type %d", GET_TYP (cp));
#endif
  return 0;
}

/* Return the value of ROW,COL in a human-readable fashion
   In paticular, strings have "" around them, and are \\ed
 */
char *
cell_value_string (CELLREF row, CELLREF col)
{
  CELL *cp;

  cp = find_cell (row, col);
  if (!cp || !GET_TYP (cp))
    return "";
  switch (GET_TYP (cp))
    {
    case TYP_FLT:
      return flt_to_str (cp->cell_flt);

    case TYP_INT:
      sprintf (print_buf, "%ld", cp->cell_int);
      return print_buf;

    case TYP_STR:
      return backslash_a_string (cp->cell_str, 1);

    case TYP_BOL:
      return bname[cp->cell_bol];

    case TYP_ERR:
      return ename[cp->cell_err];
#ifdef TEST
    default:
      panic ("unknown type %d in cell_value_string", GET_TYP (cp));
#endif
    }
  return 0;
}

static char *
pr_int (long int val, struct user_fmt *fmt, int prec)
{
  char *pf, *pff, *pt;
  long int n;
  int nn = 0;

  pt = &print_buf[sizeof (print_buf) - 1];
  *pt = '\0';

  n = fmt->scale * ((val < 0) ? -val : val);
  if (n == 0)
    return fmt->zero ? fmt->zero : "";

  pf = pff = (val < 0) ? fmt->n_trl : fmt->p_trl;
  if (pf && *pf)
    {
      while (*pf)
	pf++;
      do
	*--pt = *--pf;
      while (pf != pff);
    }

  if (prec != PRC_FLT && prec != 0)
    {
      while (prec-- > 0)
	*--pt = '0';
      pf = pff = fmt->decpt;
      if (pf)
	{
	  while (*pf)
	    pf++;
	  do
	    *--pt = *--pf;
	  while (pf != pff);
	}
      /* *--pt='.'; */
    }
  do
    {
      *--pt = (n % 10) + '0';
      n /= 10;
      if (nn++ == 2 && n > 0)
	{
	  if (fmt->comma && *(fmt->comma))
	    {
	      for (pf = pff = fmt->comma; *pf; pf++)
		;
	      do
		*--pt = *--pf;
	      while (pf != pff);
	    }
	  nn = 0;
	}
    }
  while (n > 0);

  pf = pff = (val < 0) ? fmt->n_hdr : fmt->p_hdr;
  if (pf && *pf)
    {
      while (*pf)
	pf++;
      do
	*--pt = *--pf;
      while (pf != pff);
    }
  return pt;
}

static char *
pr_flt (double val, struct user_fmt *fmt, int prec)
{
  char *iptr;
  char *fptr;
  char *pptr;
  char *pf, *pff;
  double fract, integer, tmpval;
  int n;
  int isneg;
  int comlen;

  val *= fmt->scale;

  if (val == __plinf)
    return iname;
  if (val == __neinf)
    return mname;
  if (val != val)
    return nname;

  iptr = &print_buf[BIGFLT];
  fptr = &print_buf[BIGFLT];


  if (val == 0)
    return fmt->zero ? fmt->zero : "";

  if (val < 0)
    {
      isneg = 1;
      val = -val;
    }
  else
    isneg = 0;

  comlen = 0;
  if (fmt->comma && *(fmt->comma))
    for (pf = fmt->comma; *pf; comlen++, pf++)
      ;

  fract = modf (val, &integer);
  n = 0;
  do
    {
      if (iptr < &print_buf[comlen])
	return numb_oflo;
      tmpval = modf (integer / 10, &integer);
      *--iptr = '0' + (int) ((tmpval + .01) * 10);
      if (comlen && n++ == 2 && integer)
	{
	  n = 0;
	  pff = fmt->comma;
	  pf = pff + comlen;
	  do
	    *--iptr = *--pf;
	  while (pf != pff);
	}
    }
  while (integer);

  if (prec)
    {
      int p1;

      p1 = (prec == PRC_FLT) ? 15 : (prec > 0) ? prec : -prec;
      pf = fmt->decpt;
      while (pf && *pf)
	*fptr++ = *pf++;
      /* *fptr++='.'; */
      if (fract)
	{
	  do
	    {
	      fract = modf (fract * 10, &tmpval);
	      *fptr++ = '0' + (int) tmpval;
	    }
	  while (--p1 && fract);
	}
      if (prec > 0 && prec != PRC_FLT)
	while (p1--)
	  *fptr++ = '0';
      else
	{
	  fract = 0;
	  while (fptr[-1] == '0')
	    --fptr;
	  while (!isdigit (fptr[-1]))
	    --fptr;
	  *fptr = '\0';
	}
    }
  if (fract)
    {
      (void) modf (fract * 10, &tmpval);
      if (tmpval > 4)
	{
	  iptr[-1] = '0';
	  for (pptr = fptr - 1;; --pptr)
	    {
	      if (!isdigit (*pptr))
		continue;
	      else if (*pptr == '9')
		{
		  if (pptr == fptr - 1 && pptr > &print_buf[BIGFLT] && (prec < 0 || prec == PRC_FLT))
		    {
		      --fptr;
		      while (!isdigit (pptr[-1]))
			{
			  --fptr;
			  --pptr;
			}
		      *pptr = '\0';
		    }
		  else
		    *pptr = '0';
		}
	      else
		{
		  (*pptr)++;
		  break;
		}
	    }
	  if (pptr < iptr)
	    {
	      --iptr;
	      if (n == 3)
		{
		  char tmpch;

		  tmpch = *iptr++;
		  for (pf = pff = fmt->comma; *pf; pf++)
		    ;
		  do
		    *--iptr = *--pf;
		  while (pf != pff);
		  *--iptr = tmpch;
		}
	    }
	}
    }
  pf = pff = (isneg) ? fmt->n_hdr : fmt->p_hdr;
  if (pf && *pf)
    {
      while (*pf)
	pf++;
      do
	*--iptr = *--pf;
      while (pf != pff);
    }

  pf = (isneg) ? fmt->n_trl : fmt->p_trl;
  while (pf && *pf)
    *fptr++ = *pf++;
  *fptr = 0;
  return iptr;
}

char *
adjust_prc (char *oldp, CELL *cp, int width, int smallwid, int just)
{
  int fmt;
  int prc;
  struct user_fmt *ufmt;
  char *bptr;
  char *eptr;
  int len;

  fmt = GET_FMT (cp);
  if (fmt == FMT_DEF)
    fmt = default_fmt;
  prc = GET_PRC (fmt);
  switch (fmt | PRC_FLT)
    {
    case FMT_GPH:
    case FMT_HID:
      return numb_oflo;
    case FMT_DOL:
      ufmt = &dol;
      goto deal_fmt;

    case FMT_CMA:
      ufmt = &cma;
      goto deal_fmt;

    case FMT_PCT:
      ufmt = &cma;
      goto deal_fmt;

    case FMT_FXT:
      ufmt = &fxt;
      goto deal_fmt;

    case FMT_USR:
      ufmt = &u[prc];
      prc = ufmt->prec;
      goto deal_fmt;

    case FMT_GEN:
      if (prc != PRC_FLT)
	return numb_oflo;
      if (index (oldp, 'e') || !index (oldp, '.'))
	goto handle_exp;

      ufmt = &fxt;
      prc = PRC_FLT;
      goto deal_fmt;

    deal_fmt:
      if (prc != PRC_FLT)
	return numb_oflo;
      len = strlen (oldp);
      bptr = (char *)strstr (oldp, ufmt->decpt);
      if (!bptr)
	return numb_oflo;
      while (eptr = (char *)strstr (bptr + 1, ufmt->decpt))
	bptr = eptr;

      if (width < bptr - oldp)
	return numb_oflo;
      if (bptr - oldp + strlen (ufmt->decpt) >= width)
	prc = 0;
      else
	{
	  prc = width - (strlen (ufmt->decpt) + bptr - oldp);
	}
      bptr = pr_flt (cp->cell_flt, ufmt, -prc);
      len = strlen (bptr);
      if (len > width && prc > 0)
	{
	  bptr = pr_flt (cp->cell_flt, ufmt, -(prc - 1));
	  len = strlen (bptr);
	}
      if (len > width)
	return numb_oflo;
      break;

    case FMT_EXP:
    handle_exp:
      {
	double f;

	f = fabs (cp->cell_flt);
	if (f > 9.99999e99 || f < 1e-99)
	  len = width - 7;
	else			/* if(f>9.9999999e9 || f<1e-9) */
	  len = width - 6;
	/* else
				len=width-5; */
	if (cp->cell_flt < 0)
	  --len;
	if (len > 0)
	  {
	    sprintf (oldp, "%.*e", len, cp->cell_flt);
	    len = strlen (oldp);
	    if (len <= width)
	      {
		bptr = oldp;
		break;
	      }
	  }
      }
      return numb_oflo;
    default:
      bptr = 0;
      len = 0;
#ifdef TEST
      panic ("Unknown format %d in adjust_prc()", fmt);
#endif
      break;
    }

  /* If we get here, bptr points to a a string of len characters
	   (len<=width) that we want to output */
  if (len < smallwid)
    {
      if (just == JST_RGT || just == JST_CNT)
	{
	  int n;

	  n = (just == JST_RGT) ? smallwid - len : (1 + smallwid - len) / 2;
	  for (;;)
	    {
	      bptr[len + n] = bptr[len];
	      if (len-- == 0)
		break;
	    }
	  while (n-- >= 0)
	    bptr[n] = ' ';
	}
    }
  return bptr;
}


void
set_usr_stats (int usr_n, char **usr_buf)
{
  int len;
  int i;
  char *p_in, *p_out;

  len = 0;
  for (i = 0; i < 7; i++)
    len += strlen (usr_buf[i]);
  u[usr_n].p_hdr = ck_malloc (len + 7);
  p_out = u[usr_n].p_hdr;
  if (usr_buf[0][0])
    {
      p_in = usr_buf[0];
      while (*p_out++ = *p_in++)
	;
    }
  else
    *p_out++ = '\0';

  if (usr_buf[1][0])
    {
      p_in = usr_buf[1];
      u[usr_n].n_hdr = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].n_hdr = 0;

  if (usr_buf[2][0])
    {
      p_in = usr_buf[2];
      u[usr_n].p_trl = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].p_trl = 0;

  if (usr_buf[3][0])
    {
      p_in = usr_buf[3];
      u[usr_n].n_trl = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].n_trl = 0;

  if (usr_buf[4][0])
    {
      p_in = usr_buf[4];
      u[usr_n].zero = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].zero = 0;

  if (usr_buf[5][0])
    {
      p_in = usr_buf[5];
      u[usr_n].comma = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].comma = 0;

  if (usr_buf[6][0])
    {
      p_in = usr_buf[6];
      u[usr_n].decpt = p_out;
      while (*p_out++ = *p_in++)
	;
    }
  else
    u[usr_n].decpt = 0;

  if (!stricmp (usr_buf[7], "float") || !stricmp (usr_buf[7], "f"))
    u[usr_n].prec = 15;
  else
    u[usr_n].prec = astol (&usr_buf[7]);

  u[usr_n].scale = astof (&usr_buf[8]);
}

int
usr_set_fmts (void)
{
  int n;
  int ret = 0;

  for (n = 0; n < 16; n++)
    if (u[n].p_hdr)
      ret |= 1 << n;
  return ret;
}

void
get_usr_stats (int usr_num, char **usr_buf)
{
  static char buf1[30];
  static char buf2[30];
  static char NullStr[] = "";

  usr_buf[0] = u[usr_num].p_hdr ? u[usr_num].p_hdr : NullStr;
  usr_buf[1] = u[usr_num].n_hdr ? u[usr_num].n_hdr : NullStr;
  usr_buf[2] = u[usr_num].p_trl ? u[usr_num].p_trl : NullStr;
  usr_buf[3] = u[usr_num].n_trl ? u[usr_num].n_trl : NullStr;
  usr_buf[4] = u[usr_num].zero ? u[usr_num].zero : NullStr;
  usr_buf[5] = u[usr_num].comma ? u[usr_num].comma : NullStr;
  usr_buf[6] = u[usr_num].decpt ? u[usr_num].decpt : NullStr;
  if (u[usr_num].prec == 15)
    usr_buf[7] = "float";
  else
    {
      sprintf (buf1, "%u", u[usr_num].prec);
      usr_buf[7] = buf1;
    }
  sprintf (buf2, "%.12g", u[usr_num].scale);
  usr_buf[8] = buf2;
}

/* Functions for printing out the names of cells and ranges */

char *
cell_name (CELLREF rr, CELLREF cc)
{
  static char strs[2][20];
  static num = 0;
  char *ptr;

  num = num ? 0 : 1;

  if (a0)
    {
      ptr = &strs[num][9];
      sprintf (ptr, "%u", rr);
      if (cc < MIN_COL + 26)
	*--ptr = 'A' - MIN_COL + cc;
#if MAX_COL>702
      else if (cc < MIN_COL + 702)
	{
	  cc -= MIN_COL + 26;
	  *--ptr = 'A' + cc % 26;
	  *--ptr = 'A' + cc / 26;
	}
      else if (cc < MIN_COL + 18278)
	{
	  cc -= MIN_COL + 702;
	  *--ptr = 'A' + cc % 26;
	  cc /= 26;
	  *--ptr = 'A' + cc % 26;
	  *--ptr = 'A' + cc / 26;
	}
      else
	{
	  cc -= MIN_COL + 18278;
	  *--ptr = 'A' + cc % 26;
	  cc /= 26;
	  *--ptr = 'A' + cc % 26;
	  cc /= 26;
	  *--ptr = 'A' + cc % 26;
	  *--ptr = 'A' + cc / 26;
	}
#else
      else
	{
	  cc -= MIN_COL + 26;
	  *--ptr = 'A' + cc % 26;
	  *--ptr = 'A' + cc / 26;
	}
#endif
    }
  else
    {
      ptr = &strs[num][0];
      sprintf (ptr, "r%uc%u", rr, cc);
    }
  return ptr;
}

char *
range_name (struct rng *rng)
{
  CELLREF lr, lc, hr, hc;
  static char buf[2][40];
  static num;
  char *ptr;

  ptr = &buf[num][0];
  num = num ? 0 : 1;

  lr = rng->lr;
  lc = rng->lc;
  hr = rng->hr;
  hc = rng->hc;

  if (a0)
    sprintf (ptr, "%s:%s", cell_name (lr, lc), cell_name (hr, hc));
  else
    {
      if (lr == hr && lc == hc)
	sprintf (ptr, "r%uc%u", lr, lc);
      else if (lr == hr && lc != hc)
	sprintf (ptr, "r%uc%u:%u", lr, lc, hc);
      else if (lr != hr && lc == hc)
	sprintf (ptr, "r%u:%uc%u", lr, hr, lc);
      else
	sprintf (ptr, "r%u:%uc%u:%u", lr, hr, lc, hc);
    }
  return ptr;
}


/* Parse a range, allowing variable names.
 * Return 1 on failure, 0 on succes.
 */
int
get_abs_rng (char **pptr, struct rng *retp)
{
  unsigned char n;
  struct rng ignored;

  if (!retp)
    retp = &ignored;

  while (**pptr == ' ')
    (*pptr)++;
  if (!**pptr)
    return 1;
  cur_row = curow;
  cur_col = cucol;
  n = parse_cell_or_range (pptr, retp);
  if (!n)
    {
      struct var *v;
      char *ptr;

      ptr = *pptr;
      while (ptr[n] && ptr[n] != ' ')
	n++;
      v = find_var (ptr, n);
      if (!v)
	return 1;
      (*pptr) += n;
      *retp = v->v_rng;
    }
  return 0;
}


char *
col_to_str (CELLREF col)
{
  static char strs[2][10];
  static num;
  char *ptr;

  ptr = &strs[num][9];
  num = num ? 0 : 1;

  if (col < MIN_COL + 26)
    *--ptr = 'A' - MIN_COL + col;
#if MAX_COL>702
  else if (col < MIN_COL + 702)
    {
      col -= MIN_COL + 26;
      *--ptr = 'A' + col % 26;
      *--ptr = 'A' + col / 26;
    }
  else if (col < MIN_COL + 18278)
    {
      col -= MIN_COL + 702;
      *--ptr = 'A' + col % 26;
      col /= 26;
      *--ptr = 'A' + col % 26;
      *--ptr = 'A' + col / 26;
    }
  else
    {
      col -= MIN_COL + 18278;
      *--ptr = 'A' + col % 26;
      col /= 26;
      *--ptr = 'A' + col % 26;
      col /= 26;
      *--ptr = 'A' + col % 26;
      *--ptr = 'A' + col / 26;
    }
#else
  else
    {
      col -= MIN_COL + 26;
      *--ptr = 'A' + col % 26;
      *--ptr = 'A' + col / 26;
    }
#endif
  return ptr;
}

void
clear_spreadsheet (void)
{
  int n;

  flush_everything ();
  /* flush_widths(); */
  flush_all_timers ();
  for (n = 0; n < 16; n++)
    {
      if (u[n].p_hdr)
	{
	  free (u[n].p_hdr);
	  u[n].p_hdr = 0;
	  u[n].prec = PRC_FLT;
	  u[n].scale = 1;
	}
    }
  default_width = saved_default_width;
  default_height = saved_default_height;
  default_jst = JST_LFT;
  default_fmt = FMT_GEN;
  default_lock = LCK_UNL;
}

char *ename[] =
{
  "#WHAT?",
  "#ERROR", "#BAD_INPUT", "#NON_NUMBER", "#NON_STRING",
  "#NON_BOOL", "#NON_RANGE", "#OUT_OF_RANGE", "#NO_VALUES",
  "#DIV_BY_ZERO", "#BAD_NAME", "#NOT_AVAIL", "#PARSE_ERROR",
  "#NEED_OPEN", "#NEED_CLOSE", "#NEED_QUOTE", "#UNK_CHAR",
  "#UNK_FUNC",
  0
};

char tname[] = "#TRUE";
char fname[] = "#FALSE";


int
words_imatch (char ** ptr, char * key)
{
  char * str = * ptr;

  while (isspace (*str))
    ++str;
  while (isspace (*key))
    ++key;

  while (1)
    {
      while (*str && *key && (toupper (*str) == toupper (*key)))
	{
	  ++str;
	  ++key;
	}
      /* If we're not at word breaks in both strings... */
      if (!((!*str || isspace (*str)) && (!*key || isspace (*key))))
	return 0;
      else
	{
	  while (isspace (*key))
	    ++key;
	  while (isspace (*str))
	    ++str;
	  if (!*key)
	    {
	      *ptr = str;
	      return 1;
	    }
	}
    }
}

int
parray_len (char ** array)
{
  int x;
  for (x = 0; array[x]; ++x) ;
  return x;
}


/* Return the index of CHECK in KEYS or -1.  Case and whitespeace insenstive.
 */

int
words_member (char ** keys, int len, char * check)
{
  int x;
  for (x = 0; x < len; ++x)
    {
      char * ch = check;
      if (words_imatch (&ch, keys[x]))
	if (!*ch)
	  return x;
    }
  return -1;
}

int
prompt_len (char * prompt)
{
  char * pos;
  if (!prompt)
    return 0;
  for (pos = prompt; *pos && (*pos != '\n'); ++pos)
    ;
  return pos - prompt;
}


int
says_default (char * str)
{
  char * key = "ault";
  if (strincmp(str, "def", 3))
    return 0;
  str += 3;
  while (*str && *key)
    {
      int c = isupper (*str) ? tolower (*str) : *str;
      if (c != *key)
	return 0;
      ++key;
      ++str;
    }
  while (isspace(*str))
    ++str;
  return !*str;
}