~ubuntu-branches/ubuntu/natty/edbrowse/natty

« back to all changes in this revision

Viewing changes to stringfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Kapil Hari Paranjape
  • Date: 2007-12-05 10:39:04 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20071205103904-zr3kzzu0ym6stg16
Tags: 3.3.1-1
* New upstream version (3.3.1).
  - Messages have been internationalised.
  - Supports LANG environment variable.
  - Includes man page.
* Incorporate upstream man page:
  - debian/rules: Add rule to install upstream man page.
  - debian/edbrowse.1: Not required. Removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    if(!n)
40
40
        return EMPTYSTRING;
41
41
    if(!(s = malloc(n)))
42
 
        errorPrint("@error allocating %u bytes", n);
 
42
        i_printfExit(MSG_MemAllocError, n);
43
43
    return s;
44
44
}                               /* allocMem */
45
45
 
50
50
    if(!n)
51
51
        return EMPTYSTRING;
52
52
    if(!(s = calloc(n, 1)))
53
 
        errorPrint("@error callocating %u bytes", n);
 
53
        i_printfExit(MSG_MemCallocError, n);
54
54
    return s;
55
55
}                               /* allocZeroMem */
56
56
 
59
59
{
60
60
    void *s;
61
61
    if(!n)
62
 
        errorPrint("@reallocMem(p,0)");
 
62
        i_printfExit(MSG_ReallocP);
63
63
    if(!p)
64
 
        errorPrint("@reallocMem(0,%d)", n);
 
64
        i_printfExit(MSG_Realloc0, n);
65
65
    if(p == EMPTYSTRING)
66
66
        return allocMem(n);
67
67
    if(!(s = realloc(p, n)))
68
 
        errorPrint("@error re-allocating %u bytes", n);
 
68
        i_printfExit(MSG_ErrorRealloc, n);
69
69
    return s;
70
70
}                               /* reallocMem */
71
71
 
438
438
{
439
439
    int i = 0;
440
440
    if(!list)
441
 
        errorPrint("@stringInList(null,...)");
 
441
        i_printfExit(MSG_NullStrList);
442
442
    if(s)
443
443
        while(*list) {
444
444
            if(stringEqual(s, *list))
454
454
{
455
455
    int i = 0;
456
456
    if(!list)
457
 
        errorPrint("@stringInListCI(null,...)");
 
457
        i_printfExit(MSG_NullStrListCI);
458
458
    if(s)
459
459
        while(*list) {
460
460
            if(stringEqualCI(s, *list))
470
470
{
471
471
    char *s;
472
472
    if(!list)
473
 
        errorPrint("@charInList(null,...)");
 
473
        i_printfExit(MSG_NullCharInList);
474
474
    s = strchr(list, c);
475
475
    if(!s)
476
476
        return -1;
555
555
    return c == '"' || c == '\'';
556
556
}                               /* isquote */
557
557
 
558
 
/* Gather at most 5 parameters from a vararg list
559
 
 * and place them in local variables.
560
 
 * Only take as many as indicated by the percents in a sprintf string.
561
 
 * If we blindly take more, we risk core dumps, at least on the Sun. */
562
 
 
563
 
void
564
 
varargLocals(va_list p, const char *msg, long *locals)
565
 
{
566
 
    const char *s = msg;
567
 
    int cnt;
568
 
 
569
 
    for(cnt = 0; cnt < 5; msg = s) {
570
 
        s = strchr(msg, '%');
571
 
        if(!s)
572
 
            break;
573
 
        ++s;
574
 
        if(*s == '%') {
575
 
            ++s;
576
 
            continue;
577
 
        }
578
 
        if(strchr("-.0123456789lhusdfxco", *s)) {
579
 
            long n = va_arg(p, long);
580
 
            locals[cnt++] = n;
581
 
        }
582
 
    }                           /* while finding percents in msg */
583
 
 
584
 
/* a little protection, in case they pass too few arguments */
585
 
    while(cnt < 5)
586
 
        locals[cnt++] = (long)"argmissing";
587
 
}                               /* varargLocals */
588
 
 
589
558
/* print an error message */
590
559
void
591
560
errorPrint(const char *msg, ...)
592
561
{
593
562
    char bailflag = 0;
594
563
    va_list p;
595
 
    long a[5];
 
564
 
596
565
    va_start(p, msg);
597
 
    varargLocals(p, msg, a);
598
 
    va_end(p);
599
566
 
600
567
    if(*msg == '@') {
601
 
        ++msg;
602
568
        bailflag = 1;
 
569
        ++msg;
 
570
/* I should internationalize this, but it's never suppose to happen! */
603
571
        fprintf(stderr, "disaster, ");
604
572
    } else if(isdigitByte(*msg)) {
605
 
        ++msg;
606
573
        bailflag = *msg - '0';
 
574
        ++msg;
607
575
    }
608
576
 
609
 
    fprintf(stderr, msg, a[0], a[1], a[2], a[3], a[4]);
 
577
    vfprintf(stderr, msg, p);
610
578
    fprintf(stderr, "\n");
 
579
    va_end(p);
611
580
 
612
581
    if(bailflag)
613
582
        exit(bailflag);
617
586
debugPrint(int lev, const char *msg, ...)
618
587
{
619
588
    va_list p;
620
 
    long a[5];
621
589
    if(lev > debugLevel)
622
590
        return;
623
591
    va_start(p, msg);
624
 
    varargLocals(p, msg, a);
 
592
    vprintf(msg, p);
625
593
    va_end(p);
626
 
    printf(msg, a[0], a[1], a[2], a[3], a[4]);
627
 
    printf("\n");
 
594
    nl();
628
595
    if(lev == 0 && !memcmp(msg, "warning", 7))
629
596
        eeCheck();
630
597
}                               /* debugPrint */
631
598
 
632
 
char errorMsg[4000];
633
 
/* Show the error message, not just the question mark, after these commands. */
634
 
static const char showerror_cmd[] = "AbefMqrw^";
635
 
 
636
 
/* Set the error message.  Type h to see the message. */
637
 
void
638
 
setError(const char *msg, ...)
639
 
{
640
 
    va_list p;
641
 
    long a[5];
642
 
 
643
 
    if(!msg) {
644
 
        errorMsg[0] = 0;
645
 
        return;
646
 
    }
647
 
 
648
 
    va_start(p, msg);
649
 
    varargLocals(p, msg, a);
650
 
    va_end(p);
651
 
/* Yeah I know, there's a va_list sprintf call; this is old code. */
652
 
    sprintf(errorMsg, msg, a[0], a[1], a[2], a[3], a[4]);
653
 
 
654
 
/* sanity check */
655
 
    if(strlen(errorMsg) >= sizeof (errorMsg)) {
656
 
        printf("disaster, error message, length %d is too long\n",
657
 
           strlen(errorMsg));
658
 
        puts(errorMsg);
659
 
        exit(1);
660
 
    }
661
 
}                               /* setError */
662
 
 
663
 
void
664
 
showError(void)
665
 
{
666
 
    printf("%s\n", errorMsg[0] ? errorMsg : "no errors");
667
 
}                               /* showError */
668
 
 
669
 
void
670
 
showErrorConditional(char cmd)
671
 
{
672
 
    if(helpMessagesOn || strchr(showerror_cmd, cmd))
673
 
        showError();
674
 
    else
675
 
        printf("?\n");
676
 
}                               /* showErrorConditional */
677
 
 
678
 
void
679
 
showErrorAbort(void)
680
 
{
681
 
    errorPrint("1%s", errorMsg);
682
 
}                               /* showErrorAbort */
683
 
 
684
 
void
685
 
browseError(const char *msg, ...)
686
 
{
687
 
    va_list p;
688
 
    long a[5];
689
 
    if(ismc)
690
 
        return;
691
 
    if(browseLocal != 1)
692
 
        return;
693
 
    va_start(p, msg);
694
 
    varargLocals(p, msg, a);
695
 
    va_end(p);
696
 
    if(browseLine) {
697
 
        printf("line %d: ", browseLine);
698
 
        cw->labels[4] = browseLine;
699
 
    } else
700
 
        printf("browse error: ");
701
 
    printf(msg, a[0], a[1], a[2], a[3], a[4]);
702
 
    printf("\n");
703
 
    browseLocal = 2;
704
 
}                               /* browseError */
705
 
 
706
 
/* Javascript errors, we need to see these no matter what. */
707
 
void
708
 
runningError(const char *msg, ...)
709
 
{
710
 
    va_list p;
711
 
    long a[5];
712
 
    if(ismc)
713
 
        return;
714
 
    va_start(p, msg);
715
 
    varargLocals(p, msg, a);
716
 
    va_end(p);
717
 
    if(browseLine) {
718
 
        printf("line %d: ", browseLine);
719
 
        cw->labels[4] = browseLine;
720
 
    }
721
 
    printf(msg, a[0], a[1], a[2], a[3], a[4]);
722
 
    printf("\n");
723
 
    browseLocal = 2;
724
 
}                               /* runningError */
 
599
void
 
600
nl(void)
 
601
{
 
602
    puts("");
 
603
}                               /* nl */
725
604
 
726
605
/* Turn perl string into C string, and complain about nulls. */
727
606
int
743
622
{
744
623
    pst t;
745
624
    if(!s)
746
 
        errorPrint("@null pointer in pstLength");
 
625
        i_printfExit(MSG_NullPtr);
747
626
    t = s;
748
627
    while(*t != '\n')
749
628
        ++t;
777
656
    char *buf;
778
657
    char ftype = fileTypeByName(filename, false);
779
658
    if(ftype && ftype != 'f') {
780
 
        setError("%s is not a regular file", filename);
 
659
        setError(MSG_RegularFile, filename);
781
660
        return false;
782
661
    }
783
662
    fh = open(filename, O_RDONLY | O_BINARY);
784
663
    if(fh < 0) {
785
 
        setError("cannot open %s", filename);
 
664
        setError(MSG_NoOpen, filename);
786
665
        return false;
787
666
    }
788
667
    length = fileSizeByName(filename);
791
670
        return false;
792
671
    }                           /* should never hapen */
793
672
    if(length > maxFileSize) {
794
 
        setError("file is too large, limit 40MB");
 
673
        setError(MSG_LargeFile);
795
674
        close(fh);
796
675
        return false;
797
676
    }
801
680
        n = read(fh, buf, length);
802
681
    close(fh);                  /* don't need that any more */
803
682
    if(n < length) {
804
 
        setError("cannot read the contents of %s", filename);
 
683
        setError(MSG_NoRead2, filename);
805
684
        free(buf);
806
685
        return false;
807
686
    }
873
752
    char c;
874
753
    int mode;
875
754
    if(lstat(name, &buf)) {
876
 
        setError("cannot access %s", name);
 
755
        setError(MSG_NoAccess, name);
877
756
        return 0;
878
757
    }
879
758
    mode = buf.st_mode & S_IFMT;
908
787
{
909
788
    struct stat buf;
910
789
    if(stat(name, &buf)) {
911
 
        setError("cannot access %s", name);
 
790
        setError(MSG_NoAccess, name);
912
791
        return -1;
913
792
    }
914
793
    return buf.st_size;
919
798
{
920
799
    struct stat buf;
921
800
    if(stat(name, &buf)) {
922
 
        setError("cannot access %s", name);
 
801
        setError(MSG_NoAccess, name);
923
802
        return -1;
924
803
    }
925
804
    return buf.st_mtime;
934
813
    isInteractive = isatty(0);
935
814
    if(isInteractive) {
936
815
        if(ioctl(0, TTY_GET_COMMAND, &savettybuf))
937
 
            errorPrint("@canot use ioctl() to manage the tty");
 
816
            i_printfExit(MSG_IoctlError);
938
817
    }
939
818
}                               /* ttySaveSettings */
940
819
 
1058
937
            base = ".";
1059
938
        df = opendir(base);
1060
939
        if(!df) {
1061
 
            puts
1062
 
               ("warning, the directory is inaccessible - the listing will be empty");
 
940
            i_puts(MSG_NoDirNoList);
1063
941
            return 0;
1064
942
        }
1065
943
    }
1202
1080
            *t = 0;
1203
1081
            value = getenv(dollar + 1);
1204
1082
            if(!value) {
1205
 
                setError("environement variable %s not set", dollar + 1);
 
1083
                setError(MSG_NoEnvVar, dollar + 1);
1206
1084
                return false;
1207
1085
            }
1208
1086
            if(dollar + strlen(value) >= line1 + sizeof (line1) - 1)
1236
1114
        return true;
1237
1115
    }
1238
1116
    if(cut && dollar < cut) {
1239
 
        setError("cannot expand * ? or [] prior to the last /");
 
1117
        setError(MSG_EarlyExpand);
1240
1118
        return false;
1241
1119
    }
1242
1120
 
1244
1122
    if(cut) {
1245
1123
        *cut = 0;
1246
1124
        if(cut > line1 && fileTypeByName(line1, false) != 'd') {
1247
 
            setError("%s is not an accessible directory", line1);
 
1125
            setError(MSG_NoAccessDir, line1);
1248
1126
            return false;
1249
1127
        }
1250
1128
    }
1255
1133
    cc = badBrackets = false;
1256
1134
    while(c = *s) {
1257
1135
        if(t >= re + sizeof (re) - 3) {
1258
 
            setError("shell pattern is too long");
 
1136
            setError(MSG_ShellPatternLong);
1259
1137
            return false;
1260
1138
        }
1261
1139
        if(c == '\\') {
1262
 
            setError
1263
 
               ("sorry, I don't know how to expand filenames with \\ in them");
 
1140
            setError(MSG_ExpandBackslash);
1264
1141
            return false;
1265
1142
        }
1266
1143
/* things we need to escape */
1286
1163
    *t++ = '$';
1287
1164
    *t = 0;
1288
1165
    if(badBrackets | cc) {
1289
 
        setError("improperly formed [] pattern");
 
1166
        setError(MSG_ShellSyntax);
1290
1167
        return false;
1291
1168
    }
1292
1169
 
1293
1170
    debugPrint(7, "shell regexp %s", re);
1294
1171
    re_cc = pcre_compile(re, 0, &re_error, &re_offset, 0);
1295
1172
    if(!re_cc) {
1296
 
        setError("error compiling the shell pattern, %s", re_error);
 
1173
        setError(MSG_ShellCompile, re_error);
1297
1174
        return false;
1298
1175
    }
1299
1176
 
1312
1189
        re_count = pcre_exec(re_cc, 0, file, strlen(file), 0, 0, re_vector, 3);
1313
1190
        if(re_count < -1) {
1314
1191
            pcre_free(re_cc);
1315
 
            setError("unexpected error while evaluating the shell pattern");
 
1192
            setError(MSG_ShellExpand);
1316
1193
            return false;
1317
1194
        }
1318
1195
        if(re_count < 0)
1329
1206
    }
1330
1207
    pcre_free(re_cc);
1331
1208
    if(filecount != 1) {
1332
 
        setError(filecount ? "shell pattern matches more than one file" :
1333
 
           "shell pattern does not match any files");
 
1209
        setError((filecount > 0) + MSG_ShellNoMatch);
1334
1210
        return false;
1335
1211
    }
1336
1212
    if(cc)
1340
1216
    return true;
1341
1217
 
1342
1218
  longvar:
1343
 
    setError("line becomes too long when shell variables are expanded");
 
1219
    setError(MSG_ShellLineLong);
1344
1220
    return false;
1345
1221
}                               /* envFile */
1346
1222
 
1377
1253
        return f;
1378
1254
 
1379
1255
    if(*mode == 'r')
1380
 
        errorPrint("2cannot open %s", name);
 
1256
        i_printfExit(MSG_OpenFail, name);
1381
1257
    else if(*mode == 'w' || *mode == 'a')
1382
 
        errorPrint("2cannot create or write to %s", name);
 
1258
        i_printfExit(MSG_CreateFail, name);
1383
1259
    else
1384
 
        errorPrint("@calling fopen() with invalid mode %s", mode);
 
1260
        i_printfExit(MSG_InvalidFopen, mode);
1385
1261
    return 0;
1386
1262
}                               /* efopen */
1387
1263
 
1390
1266
{
1391
1267
    FILE *f;
1392
1268
    va_list p;
1393
 
    long a[5];
1394
 
 
 
1269
    f = efopen(fname, "a");
1395
1270
    va_start(p, message);
1396
 
    varargLocals(p, message, a);
 
1271
    vfprintf(f, message, p);
1397
1272
    va_end(p);
1398
 
 
1399
 
    f = efopen(fname, "a");
1400
 
    fprintf(f, message, a[0], a[1], a[2], a[3], a[4]);
1401
1273
    fprintf(f, "\n");
1402
1274
    fclose(f);
1403
1275
}                               /* appendFile */