xref: /plan9/sys/src/cmd/gs/src/zfile1.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989, 2000, 2001 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: zfile1.c,v 1.12 2004/07/14 15:59:51 giles Exp $ */
18 /* Special file operators */
19 
20 #include "memory_.h"
21 #include "string_.h"
22 #include "ghost.h"
23 #include "gp.h"
24 #include "ierrors.h"
25 #include "oper.h"
26 #include "ialloc.h"
27 #include "opdef.h"
28 #include "opcheck.h"
29 #include "store.h"
30 
31 /* <string> <string> <bool> .file_name_combine <string> true */
32 /* <string> <string> <bool> .file_name_combine <string> <string> false */
33 private int
zfile_name_combine(i_ctx_t * i_ctx_p)34 zfile_name_combine(i_ctx_t *i_ctx_p)
35 {
36     uint plen, flen, blen, blen0;
37     const byte *prefix, *fname;
38     byte *buffer;
39     os_ptr op = osp;
40     bool no_sibling;
41 
42     check_type(op[ 0], t_boolean);
43     check_type(op[-1], t_string);
44     check_type(op[-2], t_string);
45     plen = r_size(op - 2);
46     flen = r_size(op - 1);
47     blen = blen0 = plen + flen + 2; /* Inserts separator and ending zero byte. */
48     buffer = ialloc_string(blen, "zfile_name_combine");
49     if (buffer == 0)
50 	return_error(e_VMerror);
51     prefix = op[-2].value.const_bytes;
52     fname =  op[-1].value.const_bytes;
53     no_sibling = op[0].value.boolval;
54     if (gp_file_name_combine((const char *)prefix, plen,
55 			     (const char *)fname, flen, no_sibling,
56 		             (char *)buffer, &blen) != gp_combine_success) {
57 	make_bool(op, false);
58     } else {
59 	buffer = iresize_string(buffer, blen0, blen, "zfile_name_combine");
60 	if (buffer == 0)
61 	    return_error(e_VMerror);
62 	make_string(op - 2, a_all | icurrent_space, blen, buffer);
63 	make_bool(op - 1, true);
64 	pop(1);
65     }
66     return 0;
67 }
68 
69 /* This is compiled conditionally to let PS library to know
70  * whether it works with the new gp_combine_file_name.
71  */
72 
73 /* <string> .file_name_is_absolute <bool> */
74 private int
zfile_name_is_absolute(i_ctx_t * i_ctx_p)75 zfile_name_is_absolute(i_ctx_t *i_ctx_p)
76 {   os_ptr op = osp;
77 
78     check_type(op[0], t_string);
79     make_bool(op, (gp_file_name_root((const char *)op->value.const_bytes,
80 					r_size(op)) > 0));
81     return 0;
82 }
83 
84 private int
push_string(i_ctx_t * i_ctx_p,const char * v)85 push_string(i_ctx_t *i_ctx_p, const char *v)
86 {   os_ptr op = osp;
87     int len = strlen(v);
88 
89     push(1);
90     make_const_string(op, avm_foreign | a_readonly,
91 		      len, (const byte *)v);
92     return 0;
93 }
94 
95 /* - .file_name_separator <string> */
96 private int
zfile_name_separator(i_ctx_t * i_ctx_p)97 zfile_name_separator(i_ctx_t *i_ctx_p)
98 {   return push_string(i_ctx_p, gp_file_name_separator());
99 }
100 
101 /* - .file_name_directory_separator <string> */
102 private int
zfile_name_directory_separator(i_ctx_t * i_ctx_p)103 zfile_name_directory_separator(i_ctx_t *i_ctx_p)
104 {   return push_string(i_ctx_p, gp_file_name_directory_separator());
105 }
106 
107 /* - .file_name_current <string> */
108 private int
zfile_name_current(i_ctx_t * i_ctx_p)109 zfile_name_current(i_ctx_t *i_ctx_p)
110 {   return push_string(i_ctx_p, gp_file_name_current());
111 }
112 
113 /* - .file_name_parent <string> */
114 private int
zfile_name_parent(i_ctx_t * i_ctx_p)115 zfile_name_parent(i_ctx_t *i_ctx_p)
116 {   return push_string(i_ctx_p, gp_file_name_parent());
117 }
118 
119 const op_def zfile1_op_defs[] =
120 {
121     {"0.file_name_combine", zfile_name_combine},
122     {"0.file_name_is_absolute", zfile_name_is_absolute},
123     {"0.file_name_separator", zfile_name_separator},
124     {"0.file_name_directory_separator", zfile_name_directory_separator},
125     {"0.file_name_current", zfile_name_current},
126     {"0.file_name_parent", zfile_name_parent},
127     op_def_end(0)
128 };
129 
130