Add support for tab-completion when selecting by rule
[alpine.git] / pico / fileio.c
blob2847a2d91711f73c16193fcf1fe8660b5ea98774
1 /*
2 * ========================================================================
3 * Copyright 2006 University of Washington
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
13 * Program: file reading routines
18 * The routines in this file read and write ASCII files from the disk. All of
19 * the knowledge about files are here. A better message writing scheme should
20 * be used.
22 #include "headers.h"
23 #include "../pith/charconv/filesys.h"
26 #if defined(bsd) || defined(lnx)
27 extern int errno;
28 #endif
32 FIOINFO g_pico_fio;
35 * Open a file for reading.
37 int
38 ffropen(char *fn)
40 int status;
42 if ((status = fexist(g_pico_fio.name = fn, "r", (off_t *)NULL)) == FIOSUC){
43 g_pico_fio.flags = FIOINFO_READ;
44 if((g_pico_fio.fp = our_fopen(g_pico_fio.name, "r")) == NULL)
45 status = FIOFNF;
48 return (status);
53 * Write a line to the already opened file. The "buf" points to the buffer,
54 * and the "nbuf" is its length, less the free newline. Return the status.
55 * Check only at the newline.
57 int
58 ffputline(CELL buf[], int nbuf)
60 register int i;
61 EML eml;
63 for(i = 0; i < nbuf; ++i)
64 if(write_a_wide_char((UCS) buf[i].c, g_pico_fio.fp) == EOF)
65 break;
67 if(i == nbuf)
68 write_a_wide_char((UCS) '\n', g_pico_fio.fp);
70 if(ferror(g_pico_fio.fp)){
71 eml.s = errstr(errno);
72 emlwwrite(_("Write error: %s"), &eml);
73 sleep(5);
74 return FIOERR;
77 return FIOSUC;
82 * Read a line from a file, and store the bytes in the supplied buffer. The
83 * "nbuf" is the length of the buffer. Complain about long lines and lines
84 * at the end of the file that don't have a newline present. Check for I/O
85 * errors too. Return status.
87 * Translate the line from the user's locale charset to UCS-4.
89 int
90 ffgetline(UCS buf[], size_t nbuf, size_t *charsreturned, int msg)
92 size_t i;
93 UCS ucs;
94 static int displayed = 0;
96 if(charsreturned)
97 *charsreturned = 0;
99 i = 0;
101 while((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) != CCONV_EOF && ucs != '\n'){
103 * Don't blat the CR should the newline be CRLF and we're
104 * running on a unix system. NOTE: this takes care of itself
105 * under DOS since the non-binary open turns newlines into '\n'.
107 if(ucs == '\r'){
108 if((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) == CCONV_EOF || ucs == '\n')
109 break;
111 if(i < nbuf-2) /* Bare CR. Insert it and go on... */
112 buf[i++] = '\r'; /* else, we're up a creek */
115 if(i >= nbuf-2){
116 buf[nbuf - 2] = ucs; /* store last char read */
117 buf[nbuf - 1] = '\0'; /* and terminate it */
118 if(charsreturned)
119 *charsreturned = nbuf - 1;
121 if(msg && displayed == 0){
122 emlwrite("File has long line", NULL);
123 displayed = 1;
126 return FIOLNG;
129 buf[i++] = ucs;
132 if(ucs == CCONV_EOF){
133 if(ferror(g_pico_fio.fp)){
134 emlwrite("File read error", NULL);
135 if(charsreturned)
136 *charsreturned = i;
138 return FIOERR;
141 if(i != 0)
142 emlwrite("File doesn't end with newline. Adding one.", NULL);
143 else{
144 if(charsreturned)
145 *charsreturned = i;
146 return FIOEOF;
150 buf[i] = '\0';
152 if(charsreturned)
153 *charsreturned = i;
155 return FIOSUC;